OSDN Git Service

gcc/fortran/
[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 /* True if any CFI directives were emitted at the current insn.  */
804 static bool any_cfis_emitted;
805
806 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
807    or to the CIE if LABEL is NULL.  */
808
809 static void
810 add_fde_cfi (const char *label, dw_cfi_ref cfi)
811 {
812   dw_cfi_ref *list_head;
813
814   if (emit_cfa_remember)
815     {
816       dw_cfi_ref cfi_remember;
817
818       /* Emit the state save.  */
819       emit_cfa_remember = false;
820       cfi_remember = new_cfi ();
821       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
822       add_fde_cfi (label, cfi_remember);
823     }
824
825   list_head = &cie_cfi_head;
826
827   if (dwarf2out_do_cfi_asm ())
828     {
829       if (label)
830         {
831           dw_fde_ref fde = current_fde ();
832
833           gcc_assert (fde != NULL);
834
835           /* We still have to add the cfi to the list so that lookup_cfa
836              works later on.  When -g2 and above we even need to force
837              emitting of CFI labels and add to list a DW_CFA_set_loc for
838              convert_cfa_to_fb_loc_list purposes.  If we're generating
839              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
840              convert_cfa_to_fb_loc_list.  */
841           if (dwarf_version == 2
842               && debug_info_level > DINFO_LEVEL_TERSE
843               && (write_symbols == DWARF2_DEBUG
844                   || write_symbols == VMS_AND_DWARF2_DEBUG))
845             {
846               switch (cfi->dw_cfi_opc)
847                 {
848                 case DW_CFA_def_cfa_offset:
849                 case DW_CFA_def_cfa_offset_sf:
850                 case DW_CFA_def_cfa_register:
851                 case DW_CFA_def_cfa:
852                 case DW_CFA_def_cfa_sf:
853                 case DW_CFA_def_cfa_expression:
854                 case DW_CFA_restore_state:
855                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
856                     label = dwarf2out_cfi_label (true);
857
858                   if (fde->dw_fde_current_label == NULL
859                       || strcmp (label, fde->dw_fde_current_label) != 0)
860                     {
861                       dw_cfi_ref xcfi;
862
863                       label = xstrdup (label);
864
865                       /* Set the location counter to the new label.  */
866                       xcfi = new_cfi ();
867                       /* It doesn't metter whether DW_CFA_set_loc
868                          or DW_CFA_advance_loc4 is added here, those aren't
869                          emitted into assembly, only looked up by
870                          convert_cfa_to_fb_loc_list.  */
871                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
872                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
873                       add_cfi (&fde->dw_fde_cfi, xcfi);
874                       fde->dw_fde_current_label = label;
875                     }
876                   break;
877                 default:
878                   break;
879                 }
880             }
881
882           output_cfi_directive (cfi);
883
884           list_head = &fde->dw_fde_cfi;
885           any_cfis_emitted = true;
886         }
887       /* ??? If this is a CFI for the CIE, we don't emit.  This
888          assumes that the standard CIE contents that the assembler
889          uses matches the standard CIE contents that the compiler
890          uses.  This is probably a bad assumption.  I'm not quite
891          sure how to address this for now.  */
892     }
893   else if (label)
894     {
895       dw_fde_ref fde = current_fde ();
896
897       gcc_assert (fde != NULL);
898
899       if (*label == 0)
900         label = dwarf2out_cfi_label (false);
901
902       if (fde->dw_fde_current_label == NULL
903           || strcmp (label, fde->dw_fde_current_label) != 0)
904         {
905           dw_cfi_ref xcfi;
906
907           label = xstrdup (label);
908
909           /* Set the location counter to the new label.  */
910           xcfi = new_cfi ();
911           /* If we have a current label, advance from there, otherwise
912              set the location directly using set_loc.  */
913           xcfi->dw_cfi_opc = fde->dw_fde_current_label
914                              ? DW_CFA_advance_loc4
915                              : DW_CFA_set_loc;
916           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
917           add_cfi (&fde->dw_fde_cfi, xcfi);
918
919           fde->dw_fde_current_label = label;
920         }
921
922       list_head = &fde->dw_fde_cfi;
923       any_cfis_emitted = true;
924     }
925
926   add_cfi (list_head, cfi);
927 }
928
929 /* Subroutine of lookup_cfa.  */
930
931 static void
932 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
933 {
934   switch (cfi->dw_cfi_opc)
935     {
936     case DW_CFA_def_cfa_offset:
937     case DW_CFA_def_cfa_offset_sf:
938       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
939       break;
940     case DW_CFA_def_cfa_register:
941       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942       break;
943     case DW_CFA_def_cfa:
944     case DW_CFA_def_cfa_sf:
945       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
946       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
947       break;
948     case DW_CFA_def_cfa_expression:
949       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
950       break;
951
952     case DW_CFA_remember_state:
953       gcc_assert (!remember->in_use);
954       *remember = *loc;
955       remember->in_use = 1;
956       break;
957     case DW_CFA_restore_state:
958       gcc_assert (remember->in_use);
959       *loc = *remember;
960       remember->in_use = 0;
961       break;
962
963     default:
964       break;
965     }
966 }
967
968 /* Find the previous value for the CFA.  */
969
970 static void
971 lookup_cfa (dw_cfa_location *loc)
972 {
973   dw_cfi_ref cfi;
974   dw_fde_ref fde;
975   dw_cfa_location remember;
976
977   memset (loc, 0, sizeof (*loc));
978   loc->reg = INVALID_REGNUM;
979   remember = *loc;
980
981   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
982     lookup_cfa_1 (cfi, loc, &remember);
983
984   fde = current_fde ();
985   if (fde)
986     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
987       lookup_cfa_1 (cfi, loc, &remember);
988 }
989
990 /* The current rule for calculating the DWARF2 canonical frame address.  */
991 static dw_cfa_location cfa;
992
993 /* The register used for saving registers to the stack, and its offset
994    from the CFA.  */
995 static dw_cfa_location cfa_store;
996
997 /* The current save location around an epilogue.  */
998 static dw_cfa_location cfa_remember;
999
1000 /* The running total of the size of arguments pushed onto the stack.  */
1001 static HOST_WIDE_INT args_size;
1002
1003 /* The last args_size we actually output.  */
1004 static HOST_WIDE_INT old_args_size;
1005
1006 /* Entry point to update the canonical frame address (CFA).
1007    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1008    calculated from REG+OFFSET.  */
1009
1010 void
1011 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1012 {
1013   dw_cfa_location loc;
1014   loc.indirect = 0;
1015   loc.base_offset = 0;
1016   loc.reg = reg;
1017   loc.offset = offset;
1018   def_cfa_1 (label, &loc);
1019 }
1020
1021 /* Determine if two dw_cfa_location structures define the same data.  */
1022
1023 static bool
1024 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1025 {
1026   return (loc1->reg == loc2->reg
1027           && loc1->offset == loc2->offset
1028           && loc1->indirect == loc2->indirect
1029           && (loc1->indirect == 0
1030               || loc1->base_offset == loc2->base_offset));
1031 }
1032
1033 /* This routine does the actual work.  The CFA is now calculated from
1034    the dw_cfa_location structure.  */
1035
1036 static void
1037 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1038 {
1039   dw_cfi_ref cfi;
1040   dw_cfa_location old_cfa, loc;
1041
1042   cfa = *loc_p;
1043   loc = *loc_p;
1044
1045   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1046     cfa_store.offset = loc.offset;
1047
1048   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1049   lookup_cfa (&old_cfa);
1050
1051   /* If nothing changed, no need to issue any call frame instructions.  */
1052   if (cfa_equal_p (&loc, &old_cfa))
1053     return;
1054
1055   cfi = new_cfi ();
1056
1057   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1058     {
1059       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1060          the CFA register did not change but the offset did.  The data
1061          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1062          in the assembler via the .cfi_def_cfa_offset directive.  */
1063       if (loc.offset < 0)
1064         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1065       else
1066         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1067       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1068     }
1069
1070 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1071   else if (loc.offset == old_cfa.offset
1072            && old_cfa.reg != INVALID_REGNUM
1073            && !loc.indirect
1074            && !old_cfa.indirect)
1075     {
1076       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1077          indicating the CFA register has changed to <register> but the
1078          offset has not changed.  */
1079       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1080       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1081     }
1082 #endif
1083
1084   else if (loc.indirect == 0)
1085     {
1086       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1087          indicating the CFA register has changed to <register> with
1088          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1089          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1090          directive.  */
1091       if (loc.offset < 0)
1092         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1093       else
1094         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1095       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1096       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1097     }
1098   else
1099     {
1100       /* Construct a DW_CFA_def_cfa_expression instruction to
1101          calculate the CFA using a full location expression since no
1102          register-offset pair is available.  */
1103       struct dw_loc_descr_struct *loc_list;
1104
1105       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1106       loc_list = build_cfa_loc (&loc, 0);
1107       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1108     }
1109
1110   add_fde_cfi (label, cfi);
1111 }
1112
1113 /* Add the CFI for saving a register.  REG is the CFA column number.
1114    LABEL is passed to add_fde_cfi.
1115    If SREG is -1, the register is saved at OFFSET from the CFA;
1116    otherwise it is saved in SREG.  */
1117
1118 static void
1119 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1120 {
1121   dw_cfi_ref cfi = new_cfi ();
1122   dw_fde_ref fde = current_fde ();
1123
1124   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1125
1126   /* When stack is aligned, store REG using DW_CFA_expression with
1127      FP.  */
1128   if (fde
1129       && fde->stack_realign
1130       && sreg == INVALID_REGNUM)
1131     {
1132       cfi->dw_cfi_opc = DW_CFA_expression;
1133       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1134       cfi->dw_cfi_oprnd2.dw_cfi_loc
1135         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1136     }
1137   else if (sreg == INVALID_REGNUM)
1138     {
1139       if (need_data_align_sf_opcode (offset))
1140         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1141       else if (reg & ~0x3f)
1142         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1143       else
1144         cfi->dw_cfi_opc = DW_CFA_offset;
1145       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1146     }
1147   else if (sreg == reg)
1148     cfi->dw_cfi_opc = DW_CFA_same_value;
1149   else
1150     {
1151       cfi->dw_cfi_opc = DW_CFA_register;
1152       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1153     }
1154
1155   add_fde_cfi (label, cfi);
1156 }
1157
1158 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1159    This CFI tells the unwinder that it needs to restore the window registers
1160    from the previous frame's window save area.
1161
1162    ??? Perhaps we should note in the CIE where windows are saved (instead of
1163    assuming 0(cfa)) and what registers are in the window.  */
1164
1165 void
1166 dwarf2out_window_save (const char *label)
1167 {
1168   dw_cfi_ref cfi = new_cfi ();
1169
1170   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1171   add_fde_cfi (label, cfi);
1172 }
1173
1174 /* Entry point for saving a register to the stack.  REG is the GCC register
1175    number.  LABEL and OFFSET are passed to reg_save.  */
1176
1177 void
1178 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1179 {
1180   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1181 }
1182
1183 /* Entry point for saving the return address in the stack.
1184    LABEL and OFFSET are passed to reg_save.  */
1185
1186 void
1187 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1188 {
1189   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1190 }
1191
1192 /* Entry point for saving the return address in a register.
1193    LABEL and SREG are passed to reg_save.  */
1194
1195 void
1196 dwarf2out_return_reg (const char *label, unsigned int sreg)
1197 {
1198   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1199 }
1200
1201 /* Record the initial position of the return address.  RTL is
1202    INCOMING_RETURN_ADDR_RTX.  */
1203
1204 static void
1205 initial_return_save (rtx rtl)
1206 {
1207   unsigned int reg = INVALID_REGNUM;
1208   HOST_WIDE_INT offset = 0;
1209
1210   switch (GET_CODE (rtl))
1211     {
1212     case REG:
1213       /* RA is in a register.  */
1214       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1215       break;
1216
1217     case MEM:
1218       /* RA is on the stack.  */
1219       rtl = XEXP (rtl, 0);
1220       switch (GET_CODE (rtl))
1221         {
1222         case REG:
1223           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1224           offset = 0;
1225           break;
1226
1227         case PLUS:
1228           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1229           offset = INTVAL (XEXP (rtl, 1));
1230           break;
1231
1232         case MINUS:
1233           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1234           offset = -INTVAL (XEXP (rtl, 1));
1235           break;
1236
1237         default:
1238           gcc_unreachable ();
1239         }
1240
1241       break;
1242
1243     case PLUS:
1244       /* The return address is at some offset from any value we can
1245          actually load.  For instance, on the SPARC it is in %i7+8. Just
1246          ignore the offset for now; it doesn't matter for unwinding frames.  */
1247       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1248       initial_return_save (XEXP (rtl, 0));
1249       return;
1250
1251     default:
1252       gcc_unreachable ();
1253     }
1254
1255   if (reg != DWARF_FRAME_RETURN_COLUMN)
1256     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1257 }
1258
1259 /* Given a SET, calculate the amount of stack adjustment it
1260    contains.  */
1261
1262 static HOST_WIDE_INT
1263 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1264                      HOST_WIDE_INT cur_offset)
1265 {
1266   const_rtx src = SET_SRC (pattern);
1267   const_rtx dest = SET_DEST (pattern);
1268   HOST_WIDE_INT offset = 0;
1269   enum rtx_code code;
1270
1271   if (dest == stack_pointer_rtx)
1272     {
1273       code = GET_CODE (src);
1274
1275       /* Assume (set (reg sp) (reg whatever)) sets args_size
1276          level to 0.  */
1277       if (code == REG && src != stack_pointer_rtx)
1278         {
1279           offset = -cur_args_size;
1280 #ifndef STACK_GROWS_DOWNWARD
1281           offset = -offset;
1282 #endif
1283           return offset - cur_offset;
1284         }
1285
1286       if (! (code == PLUS || code == MINUS)
1287           || XEXP (src, 0) != stack_pointer_rtx
1288           || !CONST_INT_P (XEXP (src, 1)))
1289         return 0;
1290
1291       /* (set (reg sp) (plus (reg sp) (const_int))) */
1292       offset = INTVAL (XEXP (src, 1));
1293       if (code == PLUS)
1294         offset = -offset;
1295       return offset;
1296     }
1297
1298   if (MEM_P (src) && !MEM_P (dest))
1299     dest = src;
1300   if (MEM_P (dest))
1301     {
1302       /* (set (mem (pre_dec (reg sp))) (foo)) */
1303       src = XEXP (dest, 0);
1304       code = GET_CODE (src);
1305
1306       switch (code)
1307         {
1308         case PRE_MODIFY:
1309         case POST_MODIFY:
1310           if (XEXP (src, 0) == stack_pointer_rtx)
1311             {
1312               rtx val = XEXP (XEXP (src, 1), 1);
1313               /* We handle only adjustments by constant amount.  */
1314               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1315                           && CONST_INT_P (val));
1316               offset = -INTVAL (val);
1317               break;
1318             }
1319           return 0;
1320
1321         case PRE_DEC:
1322         case POST_DEC:
1323           if (XEXP (src, 0) == stack_pointer_rtx)
1324             {
1325               offset = GET_MODE_SIZE (GET_MODE (dest));
1326               break;
1327             }
1328           return 0;
1329
1330         case PRE_INC:
1331         case POST_INC:
1332           if (XEXP (src, 0) == stack_pointer_rtx)
1333             {
1334               offset = -GET_MODE_SIZE (GET_MODE (dest));
1335               break;
1336             }
1337           return 0;
1338
1339         default:
1340           return 0;
1341         }
1342     }
1343   else
1344     return 0;
1345
1346   return offset;
1347 }
1348
1349 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1350    indexed by INSN_UID.  */
1351
1352 static HOST_WIDE_INT *barrier_args_size;
1353
1354 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1355
1356 static HOST_WIDE_INT
1357 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1358                              VEC (rtx, heap) **next)
1359 {
1360   HOST_WIDE_INT offset = 0;
1361   int i;
1362
1363   if (! RTX_FRAME_RELATED_P (insn))
1364     {
1365       if (prologue_epilogue_contains (insn))
1366         /* Nothing */;
1367       else if (GET_CODE (PATTERN (insn)) == SET)
1368         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1369       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1370                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1371         {
1372           /* There may be stack adjustments inside compound insns.  Search
1373              for them.  */
1374           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1375             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1376               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1377                                              cur_args_size, offset);
1378         }
1379     }
1380   else
1381     {
1382       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1383
1384       if (expr)
1385         {
1386           expr = XEXP (expr, 0);
1387           if (GET_CODE (expr) == PARALLEL
1388               || GET_CODE (expr) == SEQUENCE)
1389             for (i = 1; i < XVECLEN (expr, 0); i++)
1390               {
1391                 rtx elem = XVECEXP (expr, 0, i);
1392
1393                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1394                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1395               }
1396         }
1397     }
1398
1399 #ifndef STACK_GROWS_DOWNWARD
1400   offset = -offset;
1401 #endif
1402
1403   cur_args_size += offset;
1404   if (cur_args_size < 0)
1405     cur_args_size = 0;
1406
1407   if (JUMP_P (insn))
1408     {
1409       rtx dest = JUMP_LABEL (insn);
1410
1411       if (dest)
1412         {
1413           if (barrier_args_size [INSN_UID (dest)] < 0)
1414             {
1415               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1416               VEC_safe_push (rtx, heap, *next, dest);
1417             }
1418         }
1419     }
1420
1421   return cur_args_size;
1422 }
1423
1424 /* Walk the whole function and compute args_size on BARRIERs.  */
1425
1426 static void
1427 compute_barrier_args_size (void)
1428 {
1429   int max_uid = get_max_uid (), i;
1430   rtx insn;
1431   VEC (rtx, heap) *worklist, *next, *tmp;
1432
1433   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1434   for (i = 0; i < max_uid; i++)
1435     barrier_args_size[i] = -1;
1436
1437   worklist = VEC_alloc (rtx, heap, 20);
1438   next = VEC_alloc (rtx, heap, 20);
1439   insn = get_insns ();
1440   barrier_args_size[INSN_UID (insn)] = 0;
1441   VEC_quick_push (rtx, worklist, insn);
1442   for (;;)
1443     {
1444       while (!VEC_empty (rtx, worklist))
1445         {
1446           rtx prev, body, first_insn;
1447           HOST_WIDE_INT cur_args_size;
1448
1449           first_insn = insn = VEC_pop (rtx, worklist);
1450           cur_args_size = barrier_args_size[INSN_UID (insn)];
1451           prev = prev_nonnote_insn (insn);
1452           if (prev && BARRIER_P (prev))
1453             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1454
1455           for (; insn; insn = NEXT_INSN (insn))
1456             {
1457               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1458                 continue;
1459               if (BARRIER_P (insn))
1460                 break;
1461
1462               if (LABEL_P (insn))
1463                 {
1464                   if (insn == first_insn)
1465                     continue;
1466                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1467                     {
1468                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1469                       continue;
1470                     }
1471                   else
1472                     {
1473                       /* The insns starting with this label have been
1474                          already scanned or are in the worklist.  */
1475                       break;
1476                     }
1477                 }
1478
1479               body = PATTERN (insn);
1480               if (GET_CODE (body) == SEQUENCE)
1481                 {
1482                   HOST_WIDE_INT dest_args_size = cur_args_size;
1483                   for (i = 1; i < XVECLEN (body, 0); i++)
1484                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1485                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1486                       dest_args_size
1487                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488                                                        dest_args_size, &next);
1489                     else
1490                       cur_args_size
1491                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1492                                                        cur_args_size, &next);
1493
1494                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1495                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496                                                  dest_args_size, &next);
1497                   else
1498                     cur_args_size
1499                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1500                                                      cur_args_size, &next);
1501                 }
1502               else
1503                 cur_args_size
1504                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1505             }
1506         }
1507
1508       if (VEC_empty (rtx, next))
1509         break;
1510
1511       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1512       tmp = next;
1513       next = worklist;
1514       worklist = tmp;
1515       VEC_truncate (rtx, next, 0);
1516     }
1517
1518   VEC_free (rtx, heap, worklist);
1519   VEC_free (rtx, heap, next);
1520 }
1521
1522 /* Add a CFI to update the running total of the size of arguments
1523    pushed onto the stack.  */
1524
1525 static void
1526 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1527 {
1528   dw_cfi_ref cfi;
1529
1530   if (size == old_args_size)
1531     return;
1532
1533   old_args_size = size;
1534
1535   cfi = new_cfi ();
1536   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1537   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1538   add_fde_cfi (label, cfi);
1539 }
1540
1541 /* Record a stack adjustment of OFFSET bytes.  */
1542
1543 static void
1544 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1545 {
1546   if (cfa.reg == STACK_POINTER_REGNUM)
1547     cfa.offset += offset;
1548
1549   if (cfa_store.reg == STACK_POINTER_REGNUM)
1550     cfa_store.offset += offset;
1551
1552   if (ACCUMULATE_OUTGOING_ARGS)
1553     return;
1554
1555 #ifndef STACK_GROWS_DOWNWARD
1556   offset = -offset;
1557 #endif
1558
1559   args_size += offset;
1560   if (args_size < 0)
1561     args_size = 0;
1562
1563   def_cfa_1 (label, &cfa);
1564   if (flag_asynchronous_unwind_tables)
1565     dwarf2out_args_size (label, args_size);
1566 }
1567
1568 /* Check INSN to see if it looks like a push or a stack adjustment, and
1569    make a note of it if it does.  EH uses this information to find out
1570    how much extra space it needs to pop off the stack.  */
1571
1572 static void
1573 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1574 {
1575   HOST_WIDE_INT offset;
1576   const char *label;
1577   int i;
1578
1579   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1580      with this function.  Proper support would require all frame-related
1581      insns to be marked, and to be able to handle saving state around
1582      epilogues textually in the middle of the function.  */
1583   if (prologue_epilogue_contains (insn))
1584     return;
1585
1586   /* If INSN is an instruction from target of an annulled branch, the
1587      effects are for the target only and so current argument size
1588      shouldn't change at all.  */
1589   if (final_sequence
1590       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1591       && INSN_FROM_TARGET_P (insn))
1592     return;
1593
1594   /* If only calls can throw, and we have a frame pointer,
1595      save up adjustments until we see the CALL_INSN.  */
1596   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1597     {
1598       if (CALL_P (insn) && !after_p)
1599         {
1600           /* Extract the size of the args from the CALL rtx itself.  */
1601           insn = PATTERN (insn);
1602           if (GET_CODE (insn) == PARALLEL)
1603             insn = XVECEXP (insn, 0, 0);
1604           if (GET_CODE (insn) == SET)
1605             insn = SET_SRC (insn);
1606           gcc_assert (GET_CODE (insn) == CALL);
1607           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1608         }
1609       return;
1610     }
1611
1612   if (CALL_P (insn) && !after_p)
1613     {
1614       if (!flag_asynchronous_unwind_tables)
1615         dwarf2out_args_size ("", args_size);
1616       return;
1617     }
1618   else if (BARRIER_P (insn))
1619     {
1620       /* Don't call compute_barrier_args_size () if the only
1621          BARRIER is at the end of function.  */
1622       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1623         compute_barrier_args_size ();
1624       if (barrier_args_size == NULL)
1625         offset = 0;
1626       else
1627         {
1628           offset = barrier_args_size[INSN_UID (insn)];
1629           if (offset < 0)
1630             offset = 0;
1631         }
1632
1633       offset -= args_size;
1634 #ifndef STACK_GROWS_DOWNWARD
1635       offset = -offset;
1636 #endif
1637     }
1638   else if (GET_CODE (PATTERN (insn)) == SET)
1639     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1640   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1641            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1642     {
1643       /* There may be stack adjustments inside compound insns.  Search
1644          for them.  */
1645       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1646         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1647           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1648                                          args_size, offset);
1649     }
1650   else
1651     return;
1652
1653   if (offset == 0)
1654     return;
1655
1656   label = dwarf2out_cfi_label (false);
1657   dwarf2out_stack_adjust (offset, label);
1658 }
1659
1660 /* We delay emitting a register save until either (a) we reach the end
1661    of the prologue or (b) the register is clobbered.  This clusters
1662    register saves so that there are fewer pc advances.  */
1663
1664 struct GTY(()) queued_reg_save {
1665   struct queued_reg_save *next;
1666   rtx reg;
1667   HOST_WIDE_INT cfa_offset;
1668   rtx saved_reg;
1669 };
1670
1671 static GTY(()) struct queued_reg_save *queued_reg_saves;
1672
1673 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1674 struct GTY(()) reg_saved_in_data {
1675   rtx orig_reg;
1676   rtx saved_in_reg;
1677 };
1678
1679 /* A list of registers saved in other registers.
1680    The list intentionally has a small maximum capacity of 4; if your
1681    port needs more than that, you might consider implementing a
1682    more efficient data structure.  */
1683 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1684 static GTY(()) size_t num_regs_saved_in_regs;
1685
1686 static const char *last_reg_save_label;
1687
1688 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1689    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1690
1691 static void
1692 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1693 {
1694   struct queued_reg_save *q;
1695
1696   /* Duplicates waste space, but it's also necessary to remove them
1697      for correctness, since the queue gets output in reverse
1698      order.  */
1699   for (q = queued_reg_saves; q != NULL; q = q->next)
1700     if (REGNO (q->reg) == REGNO (reg))
1701       break;
1702
1703   if (q == NULL)
1704     {
1705       q = ggc_alloc_queued_reg_save ();
1706       q->next = queued_reg_saves;
1707       queued_reg_saves = q;
1708     }
1709
1710   q->reg = reg;
1711   q->cfa_offset = offset;
1712   q->saved_reg = sreg;
1713
1714   last_reg_save_label = label;
1715 }
1716
1717 /* Output all the entries in QUEUED_REG_SAVES.  */
1718
1719 void
1720 dwarf2out_flush_queued_reg_saves (void)
1721 {
1722   struct queued_reg_save *q;
1723
1724   for (q = queued_reg_saves; q; q = q->next)
1725     {
1726       size_t i;
1727       unsigned int reg, sreg;
1728
1729       for (i = 0; i < num_regs_saved_in_regs; i++)
1730         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1731           break;
1732       if (q->saved_reg && i == num_regs_saved_in_regs)
1733         {
1734           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1735           num_regs_saved_in_regs++;
1736         }
1737       if (i != num_regs_saved_in_regs)
1738         {
1739           regs_saved_in_regs[i].orig_reg = q->reg;
1740           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1741         }
1742
1743       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1744       if (q->saved_reg)
1745         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1746       else
1747         sreg = INVALID_REGNUM;
1748       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1749     }
1750
1751   queued_reg_saves = NULL;
1752   last_reg_save_label = NULL;
1753 }
1754
1755 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1756    location for?  Or, does it clobber a register which we've previously
1757    said that some other register is saved in, and for which we now
1758    have a new location for?  */
1759
1760 static bool
1761 clobbers_queued_reg_save (const_rtx insn)
1762 {
1763   struct queued_reg_save *q;
1764
1765   for (q = queued_reg_saves; q; q = q->next)
1766     {
1767       size_t i;
1768       if (modified_in_p (q->reg, insn))
1769         return true;
1770       for (i = 0; i < num_regs_saved_in_regs; i++)
1771         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1772             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1773           return true;
1774     }
1775
1776   return false;
1777 }
1778
1779 /* Entry point for saving the first register into the second.  */
1780
1781 void
1782 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1783 {
1784   size_t i;
1785   unsigned int regno, sregno;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1789       break;
1790   if (i == num_regs_saved_in_regs)
1791     {
1792       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1793       num_regs_saved_in_regs++;
1794     }
1795   regs_saved_in_regs[i].orig_reg = reg;
1796   regs_saved_in_regs[i].saved_in_reg = sreg;
1797
1798   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1799   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1800   reg_save (label, regno, sregno, 0);
1801 }
1802
1803 /* What register, if any, is currently saved in REG?  */
1804
1805 static rtx
1806 reg_saved_in (rtx reg)
1807 {
1808   unsigned int regn = REGNO (reg);
1809   size_t i;
1810   struct queued_reg_save *q;
1811
1812   for (q = queued_reg_saves; q; q = q->next)
1813     if (q->saved_reg && regn == REGNO (q->saved_reg))
1814       return q->reg;
1815
1816   for (i = 0; i < num_regs_saved_in_regs; i++)
1817     if (regs_saved_in_regs[i].saved_in_reg
1818         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1819       return regs_saved_in_regs[i].orig_reg;
1820
1821   return NULL_RTX;
1822 }
1823
1824
1825 /* A temporary register holding an integral value used in adjusting SP
1826    or setting up the store_reg.  The "offset" field holds the integer
1827    value, not an offset.  */
1828 static dw_cfa_location cfa_temp;
1829
1830 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1831
1832 static void
1833 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1834 {
1835   memset (&cfa, 0, sizeof (cfa));
1836
1837   switch (GET_CODE (pat))
1838     {
1839     case PLUS:
1840       cfa.reg = REGNO (XEXP (pat, 0));
1841       cfa.offset = INTVAL (XEXP (pat, 1));
1842       break;
1843
1844     case REG:
1845       cfa.reg = REGNO (pat);
1846       break;
1847
1848     case MEM:
1849       cfa.indirect = 1;
1850       pat = XEXP (pat, 0);
1851       if (GET_CODE (pat) == PLUS)
1852         {
1853           cfa.base_offset = INTVAL (XEXP (pat, 1));
1854           pat = XEXP (pat, 0);
1855         }
1856       cfa.reg = REGNO (pat);
1857       break;
1858
1859     default:
1860       /* Recurse and define an expression.  */
1861       gcc_unreachable ();
1862     }
1863
1864   def_cfa_1 (label, &cfa);
1865 }
1866
1867 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1868
1869 static void
1870 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1871 {
1872   rtx src, dest;
1873
1874   gcc_assert (GET_CODE (pat) == SET);
1875   dest = XEXP (pat, 0);
1876   src = XEXP (pat, 1);
1877
1878   switch (GET_CODE (src))
1879     {
1880     case PLUS:
1881       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1882       cfa.offset -= INTVAL (XEXP (src, 1));
1883       break;
1884
1885     case REG:
1886         break;
1887
1888     default:
1889         gcc_unreachable ();
1890     }
1891
1892   cfa.reg = REGNO (dest);
1893   gcc_assert (cfa.indirect == 0);
1894
1895   def_cfa_1 (label, &cfa);
1896 }
1897
1898 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1899
1900 static void
1901 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1902 {
1903   HOST_WIDE_INT offset;
1904   rtx src, addr, span;
1905
1906   src = XEXP (set, 1);
1907   addr = XEXP (set, 0);
1908   gcc_assert (MEM_P (addr));
1909   addr = XEXP (addr, 0);
1910
1911   /* As documented, only consider extremely simple addresses.  */
1912   switch (GET_CODE (addr))
1913     {
1914     case REG:
1915       gcc_assert (REGNO (addr) == cfa.reg);
1916       offset = -cfa.offset;
1917       break;
1918     case PLUS:
1919       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1920       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1921       break;
1922     default:
1923       gcc_unreachable ();
1924     }
1925
1926   span = targetm.dwarf_register_span (src);
1927
1928   /* ??? We'd like to use queue_reg_save, but we need to come up with
1929      a different flushing heuristic for epilogues.  */
1930   if (!span)
1931     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1932   else
1933     {
1934       /* We have a PARALLEL describing where the contents of SRC live.
1935          Queue register saves for each piece of the PARALLEL.  */
1936       int par_index;
1937       int limit;
1938       HOST_WIDE_INT span_offset = offset;
1939
1940       gcc_assert (GET_CODE (span) == PARALLEL);
1941
1942       limit = XVECLEN (span, 0);
1943       for (par_index = 0; par_index < limit; par_index++)
1944         {
1945           rtx elem = XVECEXP (span, 0, par_index);
1946
1947           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1948                     INVALID_REGNUM, span_offset);
1949           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1950         }
1951     }
1952 }
1953
1954 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1955
1956 static void
1957 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1958 {
1959   rtx src, dest;
1960   unsigned sregno, dregno;
1961
1962   src = XEXP (set, 1);
1963   dest = XEXP (set, 0);
1964
1965   if (src == pc_rtx)
1966     sregno = DWARF_FRAME_RETURN_COLUMN;
1967   else
1968     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1969
1970   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1971
1972   /* ??? We'd like to use queue_reg_save, but we need to come up with
1973      a different flushing heuristic for epilogues.  */
1974   reg_save (label, sregno, dregno, 0);
1975 }
1976
1977 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1978
1979 static void
1980 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1981 {
1982   rtx src, dest, span;
1983   dw_cfi_ref cfi = new_cfi ();
1984
1985   dest = SET_DEST (set);
1986   src = SET_SRC (set);
1987
1988   gcc_assert (REG_P (src));
1989   gcc_assert (MEM_P (dest));
1990
1991   span = targetm.dwarf_register_span (src);
1992   gcc_assert (!span);
1993
1994   cfi->dw_cfi_opc = DW_CFA_expression;
1995   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1996   cfi->dw_cfi_oprnd2.dw_cfi_loc
1997     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1998                           VAR_INIT_STATUS_INITIALIZED);
1999
2000   /* ??? We'd like to use queue_reg_save, were the interface different,
2001      and, as above, we could manage flushing for epilogues.  */
2002   add_fde_cfi (label, cfi);
2003 }
2004
2005 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2006
2007 static void
2008 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2009 {
2010   dw_cfi_ref cfi = new_cfi ();
2011   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2012
2013   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2014   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2015
2016   add_fde_cfi (label, cfi);
2017 }
2018
2019 /* Record call frame debugging information for an expression EXPR,
2020    which either sets SP or FP (adjusting how we calculate the frame
2021    address) or saves a register to the stack or another register.
2022    LABEL indicates the address of EXPR.
2023
2024    This function encodes a state machine mapping rtxes to actions on
2025    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2026    users need not read the source code.
2027
2028   The High-Level Picture
2029
2030   Changes in the register we use to calculate the CFA: Currently we
2031   assume that if you copy the CFA register into another register, we
2032   should take the other one as the new CFA register; this seems to
2033   work pretty well.  If it's wrong for some target, it's simple
2034   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2035
2036   Changes in the register we use for saving registers to the stack:
2037   This is usually SP, but not always.  Again, we deduce that if you
2038   copy SP into another register (and SP is not the CFA register),
2039   then the new register is the one we will be using for register
2040   saves.  This also seems to work.
2041
2042   Register saves: There's not much guesswork about this one; if
2043   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2044   register save, and the register used to calculate the destination
2045   had better be the one we think we're using for this purpose.
2046   It's also assumed that a copy from a call-saved register to another
2047   register is saving that register if RTX_FRAME_RELATED_P is set on
2048   that instruction.  If the copy is from a call-saved register to
2049   the *same* register, that means that the register is now the same
2050   value as in the caller.
2051
2052   Except: If the register being saved is the CFA register, and the
2053   offset is nonzero, we are saving the CFA, so we assume we have to
2054   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2055   the intent is to save the value of SP from the previous frame.
2056
2057   In addition, if a register has previously been saved to a different
2058   register,
2059
2060   Invariants / Summaries of Rules
2061
2062   cfa          current rule for calculating the CFA.  It usually
2063                consists of a register and an offset.
2064   cfa_store    register used by prologue code to save things to the stack
2065                cfa_store.offset is the offset from the value of
2066                cfa_store.reg to the actual CFA
2067   cfa_temp     register holding an integral value.  cfa_temp.offset
2068                stores the value, which will be used to adjust the
2069                stack pointer.  cfa_temp is also used like cfa_store,
2070                to track stores to the stack via fp or a temp reg.
2071
2072   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2073                with cfa.reg as the first operand changes the cfa.reg and its
2074                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2075                cfa_temp.offset.
2076
2077   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2078                expression yielding a constant.  This sets cfa_temp.reg
2079                and cfa_temp.offset.
2080
2081   Rule 5:      Create a new register cfa_store used to save items to the
2082                stack.
2083
2084   Rules 10-14: Save a register to the stack.  Define offset as the
2085                difference of the original location and cfa_store's
2086                location (or cfa_temp's location if cfa_temp is used).
2087
2088   Rules 16-20: If AND operation happens on sp in prologue, we assume
2089                stack is realigned.  We will use a group of DW_OP_XXX
2090                expressions to represent the location of the stored
2091                register instead of CFA+offset.
2092
2093   The Rules
2094
2095   "{a,b}" indicates a choice of a xor b.
2096   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2097
2098   Rule 1:
2099   (set <reg1> <reg2>:cfa.reg)
2100   effects: cfa.reg = <reg1>
2101            cfa.offset unchanged
2102            cfa_temp.reg = <reg1>
2103            cfa_temp.offset = cfa.offset
2104
2105   Rule 2:
2106   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2107                               {<const_int>,<reg>:cfa_temp.reg}))
2108   effects: cfa.reg = sp if fp used
2109            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2110            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2111              if cfa_store.reg==sp
2112
2113   Rule 3:
2114   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2115   effects: cfa.reg = fp
2116            cfa_offset += +/- <const_int>
2117
2118   Rule 4:
2119   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2120   constraints: <reg1> != fp
2121                <reg1> != sp
2122   effects: cfa.reg = <reg1>
2123            cfa_temp.reg = <reg1>
2124            cfa_temp.offset = cfa.offset
2125
2126   Rule 5:
2127   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2128   constraints: <reg1> != fp
2129                <reg1> != sp
2130   effects: cfa_store.reg = <reg1>
2131            cfa_store.offset = cfa.offset - cfa_temp.offset
2132
2133   Rule 6:
2134   (set <reg> <const_int>)
2135   effects: cfa_temp.reg = <reg>
2136            cfa_temp.offset = <const_int>
2137
2138   Rule 7:
2139   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2140   effects: cfa_temp.reg = <reg1>
2141            cfa_temp.offset |= <const_int>
2142
2143   Rule 8:
2144   (set <reg> (high <exp>))
2145   effects: none
2146
2147   Rule 9:
2148   (set <reg> (lo_sum <exp> <const_int>))
2149   effects: cfa_temp.reg = <reg>
2150            cfa_temp.offset = <const_int>
2151
2152   Rule 10:
2153   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2154   effects: cfa_store.offset -= <const_int>
2155            cfa.offset = cfa_store.offset if cfa.reg == sp
2156            cfa.reg = sp
2157            cfa.base_offset = -cfa_store.offset
2158
2159   Rule 11:
2160   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2161   effects: cfa_store.offset += -/+ mode_size(mem)
2162            cfa.offset = cfa_store.offset if cfa.reg == sp
2163            cfa.reg = sp
2164            cfa.base_offset = -cfa_store.offset
2165
2166   Rule 12:
2167   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2168
2169        <reg2>)
2170   effects: cfa.reg = <reg1>
2171            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2172
2173   Rule 13:
2174   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2175   effects: cfa.reg = <reg1>
2176            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2177
2178   Rule 14:
2179   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2180   effects: cfa.reg = <reg1>
2181            cfa.base_offset = -cfa_temp.offset
2182            cfa_temp.offset -= mode_size(mem)
2183
2184   Rule 15:
2185   (set <reg> {unspec, unspec_volatile})
2186   effects: target-dependent
2187
2188   Rule 16:
2189   (set sp (and: sp <const_int>))
2190   constraints: cfa_store.reg == sp
2191   effects: current_fde.stack_realign = 1
2192            cfa_store.offset = 0
2193            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2194
2195   Rule 17:
2196   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2197   effects: cfa_store.offset += -/+ mode_size(mem)
2198
2199   Rule 18:
2200   (set (mem ({pre_inc, pre_dec} sp)) fp)
2201   constraints: fde->stack_realign == 1
2202   effects: cfa_store.offset = 0
2203            cfa.reg != HARD_FRAME_POINTER_REGNUM
2204
2205   Rule 19:
2206   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2207   constraints: fde->stack_realign == 1
2208                && cfa.offset == 0
2209                && cfa.indirect == 0
2210                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2211   effects: Use DW_CFA_def_cfa_expression to define cfa
2212            cfa.reg == fde->drap_reg  */
2213
2214 static void
2215 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2216 {
2217   rtx src, dest, span;
2218   HOST_WIDE_INT offset;
2219   dw_fde_ref fde;
2220
2221   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2222      the PARALLEL independently. The first element is always processed if
2223      it is a SET. This is for backward compatibility.   Other elements
2224      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2225      flag is set in them.  */
2226   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2227     {
2228       int par_index;
2229       int limit = XVECLEN (expr, 0);
2230       rtx elem;
2231
2232       /* PARALLELs have strict read-modify-write semantics, so we
2233          ought to evaluate every rvalue before changing any lvalue.
2234          It's cumbersome to do that in general, but there's an
2235          easy approximation that is enough for all current users:
2236          handle register saves before register assignments.  */
2237       if (GET_CODE (expr) == PARALLEL)
2238         for (par_index = 0; par_index < limit; par_index++)
2239           {
2240             elem = XVECEXP (expr, 0, par_index);
2241             if (GET_CODE (elem) == SET
2242                 && MEM_P (SET_DEST (elem))
2243                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2244               dwarf2out_frame_debug_expr (elem, label);
2245           }
2246
2247       for (par_index = 0; par_index < limit; par_index++)
2248         {
2249           elem = XVECEXP (expr, 0, par_index);
2250           if (GET_CODE (elem) == SET
2251               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2252               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2253             dwarf2out_frame_debug_expr (elem, label);
2254           else if (GET_CODE (elem) == SET
2255                    && par_index != 0
2256                    && !RTX_FRAME_RELATED_P (elem))
2257             {
2258               /* Stack adjustment combining might combine some post-prologue
2259                  stack adjustment into a prologue stack adjustment.  */
2260               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2261
2262               if (offset != 0)
2263                 dwarf2out_stack_adjust (offset, label);
2264             }
2265         }
2266       return;
2267     }
2268
2269   gcc_assert (GET_CODE (expr) == SET);
2270
2271   src = SET_SRC (expr);
2272   dest = SET_DEST (expr);
2273
2274   if (REG_P (src))
2275     {
2276       rtx rsi = reg_saved_in (src);
2277       if (rsi)
2278         src = rsi;
2279     }
2280
2281   fde = current_fde ();
2282
2283   switch (GET_CODE (dest))
2284     {
2285     case REG:
2286       switch (GET_CODE (src))
2287         {
2288           /* Setting FP from SP.  */
2289         case REG:
2290           if (cfa.reg == (unsigned) REGNO (src))
2291             {
2292               /* Rule 1 */
2293               /* Update the CFA rule wrt SP or FP.  Make sure src is
2294                  relative to the current CFA register.
2295
2296                  We used to require that dest be either SP or FP, but the
2297                  ARM copies SP to a temporary register, and from there to
2298                  FP.  So we just rely on the backends to only set
2299                  RTX_FRAME_RELATED_P on appropriate insns.  */
2300               cfa.reg = REGNO (dest);
2301               cfa_temp.reg = cfa.reg;
2302               cfa_temp.offset = cfa.offset;
2303             }
2304           else
2305             {
2306               /* Saving a register in a register.  */
2307               gcc_assert (!fixed_regs [REGNO (dest)]
2308                           /* For the SPARC and its register window.  */
2309                           || (DWARF_FRAME_REGNUM (REGNO (src))
2310                               == DWARF_FRAME_RETURN_COLUMN));
2311
2312               /* After stack is aligned, we can only save SP in FP
2313                  if drap register is used.  In this case, we have
2314                  to restore stack pointer with the CFA value and we
2315                  don't generate this DWARF information.  */
2316               if (fde
2317                   && fde->stack_realign
2318                   && REGNO (src) == STACK_POINTER_REGNUM)
2319                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2320                             && fde->drap_reg != INVALID_REGNUM
2321                             && cfa.reg != REGNO (src));
2322               else
2323                 queue_reg_save (label, src, dest, 0);
2324             }
2325           break;
2326
2327         case PLUS:
2328         case MINUS:
2329         case LO_SUM:
2330           if (dest == stack_pointer_rtx)
2331             {
2332               /* Rule 2 */
2333               /* Adjusting SP.  */
2334               switch (GET_CODE (XEXP (src, 1)))
2335                 {
2336                 case CONST_INT:
2337                   offset = INTVAL (XEXP (src, 1));
2338                   break;
2339                 case REG:
2340                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2341                               == cfa_temp.reg);
2342                   offset = cfa_temp.offset;
2343                   break;
2344                 default:
2345                   gcc_unreachable ();
2346                 }
2347
2348               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2349                 {
2350                   /* Restoring SP from FP in the epilogue.  */
2351                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2352                   cfa.reg = STACK_POINTER_REGNUM;
2353                 }
2354               else if (GET_CODE (src) == LO_SUM)
2355                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2356                 ;
2357               else
2358                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2359
2360               if (GET_CODE (src) != MINUS)
2361                 offset = -offset;
2362               if (cfa.reg == STACK_POINTER_REGNUM)
2363                 cfa.offset += offset;
2364               if (cfa_store.reg == STACK_POINTER_REGNUM)
2365                 cfa_store.offset += offset;
2366             }
2367           else if (dest == hard_frame_pointer_rtx)
2368             {
2369               /* Rule 3 */
2370               /* Either setting the FP from an offset of the SP,
2371                  or adjusting the FP */
2372               gcc_assert (frame_pointer_needed);
2373
2374               gcc_assert (REG_P (XEXP (src, 0))
2375                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2376                           && CONST_INT_P (XEXP (src, 1)));
2377               offset = INTVAL (XEXP (src, 1));
2378               if (GET_CODE (src) != MINUS)
2379                 offset = -offset;
2380               cfa.offset += offset;
2381               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2382             }
2383           else
2384             {
2385               gcc_assert (GET_CODE (src) != MINUS);
2386
2387               /* Rule 4 */
2388               if (REG_P (XEXP (src, 0))
2389                   && REGNO (XEXP (src, 0)) == cfa.reg
2390                   && CONST_INT_P (XEXP (src, 1)))
2391                 {
2392                   /* Setting a temporary CFA register that will be copied
2393                      into the FP later on.  */
2394                   offset = - INTVAL (XEXP (src, 1));
2395                   cfa.offset += offset;
2396                   cfa.reg = REGNO (dest);
2397                   /* Or used to save regs to the stack.  */
2398                   cfa_temp.reg = cfa.reg;
2399                   cfa_temp.offset = cfa.offset;
2400                 }
2401
2402               /* Rule 5 */
2403               else if (REG_P (XEXP (src, 0))
2404                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2405                        && XEXP (src, 1) == stack_pointer_rtx)
2406                 {
2407                   /* Setting a scratch register that we will use instead
2408                      of SP for saving registers to the stack.  */
2409                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2410                   cfa_store.reg = REGNO (dest);
2411                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2412                 }
2413
2414               /* Rule 9 */
2415               else if (GET_CODE (src) == LO_SUM
2416                        && CONST_INT_P (XEXP (src, 1)))
2417                 {
2418                   cfa_temp.reg = REGNO (dest);
2419                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2420                 }
2421               else
2422                 gcc_unreachable ();
2423             }
2424           break;
2425
2426           /* Rule 6 */
2427         case CONST_INT:
2428           cfa_temp.reg = REGNO (dest);
2429           cfa_temp.offset = INTVAL (src);
2430           break;
2431
2432           /* Rule 7 */
2433         case IOR:
2434           gcc_assert (REG_P (XEXP (src, 0))
2435                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2436                       && CONST_INT_P (XEXP (src, 1)));
2437
2438           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2439             cfa_temp.reg = REGNO (dest);
2440           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2441           break;
2442
2443           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2444              which will fill in all of the bits.  */
2445           /* Rule 8 */
2446         case HIGH:
2447           break;
2448
2449           /* Rule 15 */
2450         case UNSPEC:
2451         case UNSPEC_VOLATILE:
2452           gcc_assert (targetm.dwarf_handle_frame_unspec);
2453           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2454           return;
2455
2456           /* Rule 16 */
2457         case AND:
2458           /* If this AND operation happens on stack pointer in prologue,
2459              we assume the stack is realigned and we extract the
2460              alignment.  */
2461           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2462             {
2463               /* We interpret reg_save differently with stack_realign set.
2464                  Thus we must flush whatever we have queued first.  */
2465               dwarf2out_flush_queued_reg_saves ();
2466
2467               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2468               fde->stack_realign = 1;
2469               fde->stack_realignment = INTVAL (XEXP (src, 1));
2470               cfa_store.offset = 0;
2471
2472               if (cfa.reg != STACK_POINTER_REGNUM
2473                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2474                 fde->drap_reg = cfa.reg;
2475             }
2476           return;
2477
2478         default:
2479           gcc_unreachable ();
2480         }
2481
2482       def_cfa_1 (label, &cfa);
2483       break;
2484
2485     case MEM:
2486
2487       /* Saving a register to the stack.  Make sure dest is relative to the
2488          CFA register.  */
2489       switch (GET_CODE (XEXP (dest, 0)))
2490         {
2491           /* Rule 10 */
2492           /* With a push.  */
2493         case PRE_MODIFY:
2494           /* We can't handle variable size modifications.  */
2495           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2496                       == CONST_INT);
2497           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2498
2499           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2500                       && cfa_store.reg == STACK_POINTER_REGNUM);
2501
2502           cfa_store.offset += offset;
2503           if (cfa.reg == STACK_POINTER_REGNUM)
2504             cfa.offset = cfa_store.offset;
2505
2506           offset = -cfa_store.offset;
2507           break;
2508
2509           /* Rule 11 */
2510         case PRE_INC:
2511         case PRE_DEC:
2512           offset = GET_MODE_SIZE (GET_MODE (dest));
2513           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2514             offset = -offset;
2515
2516           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2517                        == STACK_POINTER_REGNUM)
2518                       && cfa_store.reg == STACK_POINTER_REGNUM);
2519
2520           cfa_store.offset += offset;
2521
2522           /* Rule 18: If stack is aligned, we will use FP as a
2523              reference to represent the address of the stored
2524              regiser.  */
2525           if (fde
2526               && fde->stack_realign
2527               && src == hard_frame_pointer_rtx)
2528             {
2529               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2530               cfa_store.offset = 0;
2531             }
2532
2533           if (cfa.reg == STACK_POINTER_REGNUM)
2534             cfa.offset = cfa_store.offset;
2535
2536           offset = -cfa_store.offset;
2537           break;
2538
2539           /* Rule 12 */
2540           /* With an offset.  */
2541         case PLUS:
2542         case MINUS:
2543         case LO_SUM:
2544           {
2545             int regno;
2546
2547             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2548                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2549             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2550             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2551               offset = -offset;
2552
2553             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2554
2555             if (cfa_store.reg == (unsigned) regno)
2556               offset -= cfa_store.offset;
2557             else
2558               {
2559                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2560                 offset -= cfa_temp.offset;
2561               }
2562           }
2563           break;
2564
2565           /* Rule 13 */
2566           /* Without an offset.  */
2567         case REG:
2568           {
2569             int regno = REGNO (XEXP (dest, 0));
2570
2571             if (cfa_store.reg == (unsigned) regno)
2572               offset = -cfa_store.offset;
2573             else
2574               {
2575                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2576                 offset = -cfa_temp.offset;
2577               }
2578           }
2579           break;
2580
2581           /* Rule 14 */
2582         case POST_INC:
2583           gcc_assert (cfa_temp.reg
2584                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2585           offset = -cfa_temp.offset;
2586           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2587           break;
2588
2589         default:
2590           gcc_unreachable ();
2591         }
2592
2593         /* Rule 17 */
2594         /* If the source operand of this MEM operation is not a
2595            register, basically the source is return address.  Here
2596            we only care how much stack grew and we don't save it.  */
2597       if (!REG_P (src))
2598         break;
2599
2600       if (REGNO (src) != STACK_POINTER_REGNUM
2601           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2602           && (unsigned) REGNO (src) == cfa.reg)
2603         {
2604           /* We're storing the current CFA reg into the stack.  */
2605
2606           if (cfa.offset == 0)
2607             {
2608               /* Rule 19 */
2609               /* If stack is aligned, putting CFA reg into stack means
2610                  we can no longer use reg + offset to represent CFA.
2611                  Here we use DW_CFA_def_cfa_expression instead.  The
2612                  result of this expression equals to the original CFA
2613                  value.  */
2614               if (fde
2615                   && fde->stack_realign
2616                   && cfa.indirect == 0
2617                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2618                 {
2619                   dw_cfa_location cfa_exp;
2620
2621                   gcc_assert (fde->drap_reg == cfa.reg);
2622
2623                   cfa_exp.indirect = 1;
2624                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2625                   cfa_exp.base_offset = offset;
2626                   cfa_exp.offset = 0;
2627
2628                   fde->drap_reg_saved = 1;
2629
2630                   def_cfa_1 (label, &cfa_exp);
2631                   break;
2632                 }
2633
2634               /* If the source register is exactly the CFA, assume
2635                  we're saving SP like any other register; this happens
2636                  on the ARM.  */
2637               def_cfa_1 (label, &cfa);
2638               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2639               break;
2640             }
2641           else
2642             {
2643               /* Otherwise, we'll need to look in the stack to
2644                  calculate the CFA.  */
2645               rtx x = XEXP (dest, 0);
2646
2647               if (!REG_P (x))
2648                 x = XEXP (x, 0);
2649               gcc_assert (REG_P (x));
2650
2651               cfa.reg = REGNO (x);
2652               cfa.base_offset = offset;
2653               cfa.indirect = 1;
2654               def_cfa_1 (label, &cfa);
2655               break;
2656             }
2657         }
2658
2659       def_cfa_1 (label, &cfa);
2660       {
2661         span = targetm.dwarf_register_span (src);
2662
2663         if (!span)
2664           queue_reg_save (label, src, NULL_RTX, offset);
2665         else
2666           {
2667             /* We have a PARALLEL describing where the contents of SRC
2668                live.  Queue register saves for each piece of the
2669                PARALLEL.  */
2670             int par_index;
2671             int limit;
2672             HOST_WIDE_INT span_offset = offset;
2673
2674             gcc_assert (GET_CODE (span) == PARALLEL);
2675
2676             limit = XVECLEN (span, 0);
2677             for (par_index = 0; par_index < limit; par_index++)
2678               {
2679                 rtx elem = XVECEXP (span, 0, par_index);
2680
2681                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2682                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2683               }
2684           }
2685       }
2686       break;
2687
2688     default:
2689       gcc_unreachable ();
2690     }
2691 }
2692
2693 /* Record call frame debugging information for INSN, which either
2694    sets SP or FP (adjusting how we calculate the frame address) or saves a
2695    register to the stack.  If INSN is NULL_RTX, initialize our state.
2696
2697    If AFTER_P is false, we're being called before the insn is emitted,
2698    otherwise after.  Call instructions get invoked twice.  */
2699
2700 void
2701 dwarf2out_frame_debug (rtx insn, bool after_p)
2702 {
2703   const char *label;
2704   rtx note, n;
2705   bool handled_one = false;
2706
2707   if (insn == NULL_RTX)
2708     {
2709       size_t i;
2710
2711       /* Flush any queued register saves.  */
2712       dwarf2out_flush_queued_reg_saves ();
2713
2714       /* Set up state for generating call frame debug info.  */
2715       lookup_cfa (&cfa);
2716       gcc_assert (cfa.reg
2717                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2718
2719       cfa.reg = STACK_POINTER_REGNUM;
2720       cfa_store = cfa;
2721       cfa_temp.reg = -1;
2722       cfa_temp.offset = 0;
2723
2724       for (i = 0; i < num_regs_saved_in_regs; i++)
2725         {
2726           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2727           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2728         }
2729       num_regs_saved_in_regs = 0;
2730
2731       if (barrier_args_size)
2732         {
2733           XDELETEVEC (barrier_args_size);
2734           barrier_args_size = NULL;
2735         }
2736       return;
2737     }
2738
2739   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2740     dwarf2out_flush_queued_reg_saves ();
2741
2742   if (!RTX_FRAME_RELATED_P (insn))
2743     {
2744       /* ??? This should be done unconditionally since stack adjustments
2745          matter if the stack pointer is not the CFA register anymore but
2746          is still used to save registers.  */
2747       if (!ACCUMULATE_OUTGOING_ARGS)
2748         dwarf2out_notice_stack_adjust (insn, after_p);
2749       return;
2750     }
2751
2752   label = dwarf2out_cfi_label (false);
2753   any_cfis_emitted = false;
2754
2755   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2756     switch (REG_NOTE_KIND (note))
2757       {
2758       case REG_FRAME_RELATED_EXPR:
2759         insn = XEXP (note, 0);
2760         goto found;
2761
2762       case REG_CFA_DEF_CFA:
2763         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2764         handled_one = true;
2765         break;
2766
2767       case REG_CFA_ADJUST_CFA:
2768         n = XEXP (note, 0);
2769         if (n == NULL)
2770           {
2771             n = PATTERN (insn);
2772             if (GET_CODE (n) == PARALLEL)
2773               n = XVECEXP (n, 0, 0);
2774           }
2775         dwarf2out_frame_debug_adjust_cfa (n, label);
2776         handled_one = true;
2777         break;
2778
2779       case REG_CFA_OFFSET:
2780         n = XEXP (note, 0);
2781         if (n == NULL)
2782           n = single_set (insn);
2783         dwarf2out_frame_debug_cfa_offset (n, label);
2784         handled_one = true;
2785         break;
2786
2787       case REG_CFA_REGISTER:
2788         n = XEXP (note, 0);
2789         if (n == NULL)
2790           {
2791             n = PATTERN (insn);
2792             if (GET_CODE (n) == PARALLEL)
2793               n = XVECEXP (n, 0, 0);
2794           }
2795         dwarf2out_frame_debug_cfa_register (n, label);
2796         handled_one = true;
2797         break;
2798
2799       case REG_CFA_EXPRESSION:
2800         n = XEXP (note, 0);
2801         if (n == NULL)
2802           n = single_set (insn);
2803         dwarf2out_frame_debug_cfa_expression (n, label);
2804         handled_one = true;
2805         break;
2806
2807       case REG_CFA_RESTORE:
2808         n = XEXP (note, 0);
2809         if (n == NULL)
2810           {
2811             n = PATTERN (insn);
2812             if (GET_CODE (n) == PARALLEL)
2813               n = XVECEXP (n, 0, 0);
2814             n = XEXP (n, 0);
2815           }
2816         dwarf2out_frame_debug_cfa_restore (n, label);
2817         handled_one = true;
2818         break;
2819
2820       case REG_CFA_SET_VDRAP:
2821         n = XEXP (note, 0);
2822         if (REG_P (n))
2823           {
2824             dw_fde_ref fde = current_fde ();
2825             if (fde)
2826               {
2827                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2828                 if (REG_P (n))
2829                   fde->vdrap_reg = REGNO (n);
2830               }
2831           }
2832         handled_one = true;
2833         break;
2834
2835       default:
2836         break;
2837       }
2838   if (handled_one)
2839     {
2840       if (any_cfis_emitted)
2841         dwarf2out_flush_queued_reg_saves ();
2842       return;
2843     }
2844
2845   insn = PATTERN (insn);
2846  found:
2847   dwarf2out_frame_debug_expr (insn, label);
2848
2849   /* Check again.  A parallel can save and update the same register.
2850      We could probably check just once, here, but this is safer than
2851      removing the check above.  */
2852   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2853     dwarf2out_flush_queued_reg_saves ();
2854 }
2855
2856 /* Determine if we need to save and restore CFI information around this
2857    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2858    we do need to save/restore, then emit the save now, and insert a
2859    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2860
2861 void
2862 dwarf2out_cfi_begin_epilogue (rtx insn)
2863 {
2864   bool saw_frp = false;
2865   rtx i;
2866
2867   /* Scan forward to the return insn, noticing if there are possible
2868      frame related insns.  */
2869   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2870     {
2871       if (!INSN_P (i))
2872         continue;
2873
2874       /* Look for both regular and sibcalls to end the block.  */
2875       if (returnjump_p (i))
2876         break;
2877       if (CALL_P (i) && SIBLING_CALL_P (i))
2878         break;
2879
2880       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2881         {
2882           int idx;
2883           rtx seq = PATTERN (i);
2884
2885           if (returnjump_p (XVECEXP (seq, 0, 0)))
2886             break;
2887           if (CALL_P (XVECEXP (seq, 0, 0))
2888               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2889             break;
2890
2891           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2892             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2893               saw_frp = true;
2894         }
2895
2896       if (RTX_FRAME_RELATED_P (i))
2897         saw_frp = true;
2898     }
2899
2900   /* If the port doesn't emit epilogue unwind info, we don't need a
2901      save/restore pair.  */
2902   if (!saw_frp)
2903     return;
2904
2905   /* Otherwise, search forward to see if the return insn was the last
2906      basic block of the function.  If so, we don't need save/restore.  */
2907   gcc_assert (i != NULL);
2908   i = next_real_insn (i);
2909   if (i == NULL)
2910     return;
2911
2912   /* Insert the restore before that next real insn in the stream, and before
2913      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2914      properly nested.  This should be after any label or alignment.  This
2915      will be pushed into the CFI stream by the function below.  */
2916   while (1)
2917     {
2918       rtx p = PREV_INSN (i);
2919       if (!NOTE_P (p))
2920         break;
2921       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2922         break;
2923       i = p;
2924     }
2925   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2926
2927   emit_cfa_remember = true;
2928
2929   /* And emulate the state save.  */
2930   gcc_assert (!cfa_remember.in_use);
2931   cfa_remember = cfa;
2932   cfa_remember.in_use = 1;
2933 }
2934
2935 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2936    required.  */
2937
2938 void
2939 dwarf2out_frame_debug_restore_state (void)
2940 {
2941   dw_cfi_ref cfi = new_cfi ();
2942   const char *label = dwarf2out_cfi_label (false);
2943
2944   cfi->dw_cfi_opc = DW_CFA_restore_state;
2945   add_fde_cfi (label, cfi);
2946
2947   gcc_assert (cfa_remember.in_use);
2948   cfa = cfa_remember;
2949   cfa_remember.in_use = 0;
2950 }
2951
2952 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2953 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2954  (enum dwarf_call_frame_info cfi);
2955
2956 static enum dw_cfi_oprnd_type
2957 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2958 {
2959   switch (cfi)
2960     {
2961     case DW_CFA_nop:
2962     case DW_CFA_GNU_window_save:
2963     case DW_CFA_remember_state:
2964     case DW_CFA_restore_state:
2965       return dw_cfi_oprnd_unused;
2966
2967     case DW_CFA_set_loc:
2968     case DW_CFA_advance_loc1:
2969     case DW_CFA_advance_loc2:
2970     case DW_CFA_advance_loc4:
2971     case DW_CFA_MIPS_advance_loc8:
2972       return dw_cfi_oprnd_addr;
2973
2974     case DW_CFA_offset:
2975     case DW_CFA_offset_extended:
2976     case DW_CFA_def_cfa:
2977     case DW_CFA_offset_extended_sf:
2978     case DW_CFA_def_cfa_sf:
2979     case DW_CFA_restore:
2980     case DW_CFA_restore_extended:
2981     case DW_CFA_undefined:
2982     case DW_CFA_same_value:
2983     case DW_CFA_def_cfa_register:
2984     case DW_CFA_register:
2985     case DW_CFA_expression:
2986       return dw_cfi_oprnd_reg_num;
2987
2988     case DW_CFA_def_cfa_offset:
2989     case DW_CFA_GNU_args_size:
2990     case DW_CFA_def_cfa_offset_sf:
2991       return dw_cfi_oprnd_offset;
2992
2993     case DW_CFA_def_cfa_expression:
2994       return dw_cfi_oprnd_loc;
2995
2996     default:
2997       gcc_unreachable ();
2998     }
2999 }
3000
3001 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3002 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3003  (enum dwarf_call_frame_info cfi);
3004
3005 static enum dw_cfi_oprnd_type
3006 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3007 {
3008   switch (cfi)
3009     {
3010     case DW_CFA_def_cfa:
3011     case DW_CFA_def_cfa_sf:
3012     case DW_CFA_offset:
3013     case DW_CFA_offset_extended_sf:
3014     case DW_CFA_offset_extended:
3015       return dw_cfi_oprnd_offset;
3016
3017     case DW_CFA_register:
3018       return dw_cfi_oprnd_reg_num;
3019
3020     case DW_CFA_expression:
3021       return dw_cfi_oprnd_loc;
3022
3023     default:
3024       return dw_cfi_oprnd_unused;
3025     }
3026 }
3027
3028 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3029    switch to the data section instead, and write out a synthetic start label
3030    for collect2 the first time around.  */
3031
3032 static void
3033 switch_to_eh_frame_section (bool back)
3034 {
3035   tree label;
3036
3037 #ifdef EH_FRAME_SECTION_NAME
3038   if (eh_frame_section == 0)
3039     {
3040       int flags;
3041
3042       if (EH_TABLES_CAN_BE_READ_ONLY)
3043         {
3044           int fde_encoding;
3045           int per_encoding;
3046           int lsda_encoding;
3047
3048           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3049                                                        /*global=*/0);
3050           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3051                                                        /*global=*/1);
3052           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3053                                                         /*global=*/0);
3054           flags = ((! flag_pic
3055                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3056                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3057                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3058                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3059                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3060                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3061                    ? 0 : SECTION_WRITE);
3062         }
3063       else
3064         flags = SECTION_WRITE;
3065       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3066     }
3067 #endif /* EH_FRAME_SECTION_NAME */
3068
3069   if (eh_frame_section)
3070     switch_to_section (eh_frame_section);
3071   else
3072     {
3073       /* We have no special eh_frame section.  Put the information in
3074          the data section and emit special labels to guide collect2.  */
3075       switch_to_section (data_section);
3076
3077       if (!back)
3078         {
3079           label = get_file_function_name ("F");
3080           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3081           targetm.asm_out.globalize_label (asm_out_file,
3082                                            IDENTIFIER_POINTER (label));
3083           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3084         }
3085     }
3086 }
3087
3088 /* Switch [BACK] to the eh or debug frame table section, depending on
3089    FOR_EH.  */
3090
3091 static void
3092 switch_to_frame_table_section (int for_eh, bool back)
3093 {
3094   if (for_eh)
3095     switch_to_eh_frame_section (back);
3096   else
3097     {
3098       if (!debug_frame_section)
3099         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3100                                            SECTION_DEBUG, NULL);
3101       switch_to_section (debug_frame_section);
3102     }
3103 }
3104
3105 /* Output a Call Frame Information opcode and its operand(s).  */
3106
3107 static void
3108 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3109 {
3110   unsigned long r;
3111   HOST_WIDE_INT off;
3112
3113   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3114     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3115                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3116                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3117                          ((unsigned HOST_WIDE_INT)
3118                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3119   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3120     {
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3122       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3123                            "DW_CFA_offset, column %#lx", r);
3124       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3125       dw2_asm_output_data_uleb128 (off, NULL);
3126     }
3127   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3128     {
3129       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3131                            "DW_CFA_restore, column %#lx", r);
3132     }
3133   else
3134     {
3135       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3136                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3137
3138       switch (cfi->dw_cfi_opc)
3139         {
3140         case DW_CFA_set_loc:
3141           if (for_eh)
3142             dw2_asm_output_encoded_addr_rtx (
3143                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3144                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3145                 false, NULL);
3146           else
3147             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3148                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3149           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3150           break;
3151
3152         case DW_CFA_advance_loc1:
3153           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3154                                 fde->dw_fde_current_label, NULL);
3155           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3156           break;
3157
3158         case DW_CFA_advance_loc2:
3159           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3160                                 fde->dw_fde_current_label, NULL);
3161           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3162           break;
3163
3164         case DW_CFA_advance_loc4:
3165           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3166                                 fde->dw_fde_current_label, NULL);
3167           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3168           break;
3169
3170         case DW_CFA_MIPS_advance_loc8:
3171           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3172                                 fde->dw_fde_current_label, NULL);
3173           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3174           break;
3175
3176         case DW_CFA_offset_extended:
3177           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3178           dw2_asm_output_data_uleb128 (r, NULL);
3179           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3180           dw2_asm_output_data_uleb128 (off, NULL);
3181           break;
3182
3183         case DW_CFA_def_cfa:
3184           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3185           dw2_asm_output_data_uleb128 (r, NULL);
3186           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3187           break;
3188
3189         case DW_CFA_offset_extended_sf:
3190           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3191           dw2_asm_output_data_uleb128 (r, NULL);
3192           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3193           dw2_asm_output_data_sleb128 (off, NULL);
3194           break;
3195
3196         case DW_CFA_def_cfa_sf:
3197           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3198           dw2_asm_output_data_uleb128 (r, NULL);
3199           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3200           dw2_asm_output_data_sleb128 (off, NULL);
3201           break;
3202
3203         case DW_CFA_restore_extended:
3204         case DW_CFA_undefined:
3205         case DW_CFA_same_value:
3206         case DW_CFA_def_cfa_register:
3207           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208           dw2_asm_output_data_uleb128 (r, NULL);
3209           break;
3210
3211         case DW_CFA_register:
3212           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3213           dw2_asm_output_data_uleb128 (r, NULL);
3214           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3215           dw2_asm_output_data_uleb128 (r, NULL);
3216           break;
3217
3218         case DW_CFA_def_cfa_offset:
3219         case DW_CFA_GNU_args_size:
3220           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3221           break;
3222
3223         case DW_CFA_def_cfa_offset_sf:
3224           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3225           dw2_asm_output_data_sleb128 (off, NULL);
3226           break;
3227
3228         case DW_CFA_GNU_window_save:
3229           break;
3230
3231         case DW_CFA_def_cfa_expression:
3232         case DW_CFA_expression:
3233           output_cfa_loc (cfi);
3234           break;
3235
3236         case DW_CFA_GNU_negative_offset_extended:
3237           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3238           gcc_unreachable ();
3239
3240         default:
3241           break;
3242         }
3243     }
3244 }
3245
3246 /* Similar, but do it via assembler directives instead.  */
3247
3248 static void
3249 output_cfi_directive (dw_cfi_ref cfi)
3250 {
3251   unsigned long r, r2;
3252
3253   switch (cfi->dw_cfi_opc)
3254     {
3255     case DW_CFA_advance_loc:
3256     case DW_CFA_advance_loc1:
3257     case DW_CFA_advance_loc2:
3258     case DW_CFA_advance_loc4:
3259     case DW_CFA_MIPS_advance_loc8:
3260     case DW_CFA_set_loc:
3261       /* Should only be created by add_fde_cfi in a code path not
3262          followed when emitting via directives.  The assembler is
3263          going to take care of this for us.  */
3264       gcc_unreachable ();
3265
3266     case DW_CFA_offset:
3267     case DW_CFA_offset_extended:
3268     case DW_CFA_offset_extended_sf:
3269       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3270       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3271                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3272       break;
3273
3274     case DW_CFA_restore:
3275     case DW_CFA_restore_extended:
3276       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3277       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3278       break;
3279
3280     case DW_CFA_undefined:
3281       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3282       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3283       break;
3284
3285     case DW_CFA_same_value:
3286       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3287       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3288       break;
3289
3290     case DW_CFA_def_cfa:
3291     case DW_CFA_def_cfa_sf:
3292       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3293       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3294                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3295       break;
3296
3297     case DW_CFA_def_cfa_register:
3298       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3299       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3300       break;
3301
3302     case DW_CFA_register:
3303       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3304       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3305       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3306       break;
3307
3308     case DW_CFA_def_cfa_offset:
3309     case DW_CFA_def_cfa_offset_sf:
3310       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3311                HOST_WIDE_INT_PRINT_DEC"\n",
3312                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3313       break;
3314
3315     case DW_CFA_remember_state:
3316       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3317       break;
3318     case DW_CFA_restore_state:
3319       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3320       break;
3321
3322     case DW_CFA_GNU_args_size:
3323       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3324       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3325       if (flag_debug_asm)
3326         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3327                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3328       fputc ('\n', asm_out_file);
3329       break;
3330
3331     case DW_CFA_GNU_window_save:
3332       fprintf (asm_out_file, "\t.cfi_window_save\n");
3333       break;
3334
3335     case DW_CFA_def_cfa_expression:
3336     case DW_CFA_expression:
3337       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3338       output_cfa_loc_raw (cfi);
3339       fputc ('\n', asm_out_file);
3340       break;
3341
3342     default:
3343       gcc_unreachable ();
3344     }
3345 }
3346
3347 DEF_VEC_P (dw_cfi_ref);
3348 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3349
3350 /* Output CFIs to bring current FDE to the same state as after executing
3351    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3352    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3353    other arguments to pass to output_cfi.  */
3354
3355 static void
3356 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3357 {
3358   struct dw_cfi_struct cfi_buf;
3359   dw_cfi_ref cfi2;
3360   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3361   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3362   unsigned int len, idx;
3363
3364   for (;; cfi = cfi->dw_cfi_next)
3365     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3366       {
3367       case DW_CFA_advance_loc:
3368       case DW_CFA_advance_loc1:
3369       case DW_CFA_advance_loc2:
3370       case DW_CFA_advance_loc4:
3371       case DW_CFA_MIPS_advance_loc8:
3372       case DW_CFA_set_loc:
3373         /* All advances should be ignored.  */
3374         break;
3375       case DW_CFA_remember_state:
3376         {
3377           dw_cfi_ref args_size = cfi_args_size;
3378
3379           /* Skip everything between .cfi_remember_state and
3380              .cfi_restore_state.  */
3381           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3382             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3383               break;
3384             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3385               args_size = cfi2;
3386             else
3387               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3388
3389           if (cfi2 == NULL)
3390             goto flush_all;
3391           else
3392             {
3393               cfi = cfi2;
3394               cfi_args_size = args_size;
3395             }
3396           break;
3397         }
3398       case DW_CFA_GNU_args_size:
3399         cfi_args_size = cfi;
3400         break;
3401       case DW_CFA_GNU_window_save:
3402         goto flush_all;
3403       case DW_CFA_offset:
3404       case DW_CFA_offset_extended:
3405       case DW_CFA_offset_extended_sf:
3406       case DW_CFA_restore:
3407       case DW_CFA_restore_extended:
3408       case DW_CFA_undefined:
3409       case DW_CFA_same_value:
3410       case DW_CFA_register:
3411       case DW_CFA_val_offset:
3412       case DW_CFA_val_offset_sf:
3413       case DW_CFA_expression:
3414       case DW_CFA_val_expression:
3415       case DW_CFA_GNU_negative_offset_extended:
3416         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3417           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3418                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3419         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3420         break;
3421       case DW_CFA_def_cfa:
3422       case DW_CFA_def_cfa_sf:
3423       case DW_CFA_def_cfa_expression:
3424         cfi_cfa = cfi;
3425         cfi_cfa_offset = cfi;
3426         break;
3427       case DW_CFA_def_cfa_register:
3428         cfi_cfa = cfi;
3429         break;
3430       case DW_CFA_def_cfa_offset:
3431       case DW_CFA_def_cfa_offset_sf:
3432         cfi_cfa_offset = cfi;
3433         break;
3434       case DW_CFA_nop:
3435         gcc_assert (cfi == NULL);
3436       flush_all:
3437         len = VEC_length (dw_cfi_ref, regs);
3438         for (idx = 0; idx < len; idx++)
3439           {
3440             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3441             if (cfi2 != NULL
3442                 && cfi2->dw_cfi_opc != DW_CFA_restore
3443                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3444               {
3445                 if (do_cfi_asm)
3446                   output_cfi_directive (cfi2);
3447                 else
3448                   output_cfi (cfi2, fde, for_eh);
3449               }
3450           }
3451         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3452           {
3453             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3454             cfi_buf = *cfi_cfa;
3455             switch (cfi_cfa_offset->dw_cfi_opc)
3456               {
3457               case DW_CFA_def_cfa_offset:
3458                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3459                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3460                 break;
3461               case DW_CFA_def_cfa_offset_sf:
3462                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3463                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3464                 break;
3465               case DW_CFA_def_cfa:
3466               case DW_CFA_def_cfa_sf:
3467                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3468                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3469                 break;
3470               default:
3471                 gcc_unreachable ();
3472               }
3473             cfi_cfa = &cfi_buf;
3474           }
3475         else if (cfi_cfa_offset)
3476           cfi_cfa = cfi_cfa_offset;
3477         if (cfi_cfa)
3478           {
3479             if (do_cfi_asm)
3480               output_cfi_directive (cfi_cfa);
3481             else
3482               output_cfi (cfi_cfa, fde, for_eh);
3483           }
3484         cfi_cfa = NULL;
3485         cfi_cfa_offset = NULL;
3486         if (cfi_args_size
3487             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3488           {
3489             if (do_cfi_asm)
3490               output_cfi_directive (cfi_args_size);
3491             else
3492               output_cfi (cfi_args_size, fde, for_eh);
3493           }
3494         cfi_args_size = NULL;
3495         if (cfi == NULL)
3496           {
3497             VEC_free (dw_cfi_ref, heap, regs);
3498             return;
3499           }
3500         else if (do_cfi_asm)
3501           output_cfi_directive (cfi);
3502         else
3503           output_cfi (cfi, fde, for_eh);
3504         break;
3505       default:
3506         gcc_unreachable ();
3507     }
3508 }
3509
3510 /* Output one FDE.  */
3511
3512 static void
3513 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3514             char *section_start_label, int fde_encoding, char *augmentation,
3515             bool any_lsda_needed, int lsda_encoding)
3516 {
3517   const char *begin, *end;
3518   static unsigned int j;
3519   char l1[20], l2[20];
3520   dw_cfi_ref cfi;
3521
3522   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3523                                      /* empty */ 0);
3524   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3525                                   for_eh + j);
3526   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3527   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3528   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3529     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3530                          " indicating 64-bit DWARF extension");
3531   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3532                         "FDE Length");
3533   ASM_OUTPUT_LABEL (asm_out_file, l1);
3534
3535   if (for_eh)
3536     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3537   else
3538     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3539                            debug_frame_section, "FDE CIE offset");
3540
3541   if (!fde->dw_fde_switched_sections)
3542     {
3543       begin = fde->dw_fde_begin;
3544       end = fde->dw_fde_end;
3545     }
3546   else
3547     {
3548       /* For the first section, prefer dw_fde_begin over
3549          dw_fde_{hot,cold}_section_label, as the latter
3550          might be separated from the real start of the
3551          function by alignment padding.  */
3552       if (!second)
3553         begin = fde->dw_fde_begin;
3554       else if (fde->dw_fde_switched_cold_to_hot)
3555         begin = fde->dw_fde_hot_section_label;
3556       else
3557         begin = fde->dw_fde_unlikely_section_label;
3558       if (second ^ fde->dw_fde_switched_cold_to_hot)
3559         end = fde->dw_fde_unlikely_section_end_label;
3560       else
3561         end = fde->dw_fde_hot_section_end_label;
3562     }
3563
3564   if (for_eh)
3565     {
3566       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3567       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3568       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3569                                        "FDE initial location");
3570       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3571                             end, begin, "FDE address range");
3572     }
3573   else
3574     {
3575       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3576       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3577     }
3578
3579   if (augmentation[0])
3580     {
3581       if (any_lsda_needed)
3582         {
3583           int size = size_of_encoded_value (lsda_encoding);
3584
3585           if (lsda_encoding == DW_EH_PE_aligned)
3586             {
3587               int offset = (  4         /* Length */
3588                             + 4         /* CIE offset */
3589                             + 2 * size_of_encoded_value (fde_encoding)
3590                             + 1         /* Augmentation size */ );
3591               int pad = -offset & (PTR_SIZE - 1);
3592
3593               size += pad;
3594               gcc_assert (size_of_uleb128 (size) == 1);
3595             }
3596
3597           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3598
3599           if (fde->uses_eh_lsda)
3600             {
3601               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3602                                            fde->funcdef_number);
3603               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3604                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3605                                                false,
3606                                                "Language Specific Data Area");
3607             }
3608           else
3609             {
3610               if (lsda_encoding == DW_EH_PE_aligned)
3611                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3612               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3613                                    "Language Specific Data Area (none)");
3614             }
3615         }
3616       else
3617         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3618     }
3619
3620   /* Loop through the Call Frame Instructions associated with
3621      this FDE.  */
3622   fde->dw_fde_current_label = begin;
3623   if (!fde->dw_fde_switched_sections)
3624     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3625       output_cfi (cfi, fde, for_eh);
3626   else if (!second)
3627     {
3628       if (fde->dw_fde_switch_cfi)
3629         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3630           {
3631             output_cfi (cfi, fde, for_eh);
3632             if (cfi == fde->dw_fde_switch_cfi)
3633               break;
3634           }
3635     }
3636   else
3637     {
3638       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3639
3640       if (fde->dw_fde_switch_cfi)
3641         {
3642           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3643           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3644           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3645           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3646         }
3647       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3648         output_cfi (cfi, fde, for_eh);
3649     }
3650
3651   /* If we are to emit a ref/link from function bodies to their frame tables,
3652      do it now.  This is typically performed to make sure that tables
3653      associated with functions are dragged with them and not discarded in
3654      garbage collecting links. We need to do this on a per function basis to
3655      cope with -ffunction-sections.  */
3656
3657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3658   /* Switch to the function section, emit the ref to the tables, and
3659      switch *back* into the table section.  */
3660   switch_to_section (function_section (fde->decl));
3661   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3662   switch_to_frame_table_section (for_eh, true);
3663 #endif
3664
3665   /* Pad the FDE out to an address sized boundary.  */
3666   ASM_OUTPUT_ALIGN (asm_out_file,
3667                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3668   ASM_OUTPUT_LABEL (asm_out_file, l2);
3669
3670   j += 2;
3671 }
3672
3673 /* Return true if frame description entry FDE is needed for EH.  */
3674
3675 static bool
3676 fde_needed_for_eh_p (dw_fde_ref fde)
3677 {
3678   if (flag_asynchronous_unwind_tables)
3679     return true;
3680
3681   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3682     return true;
3683
3684   if (fde->uses_eh_lsda)
3685     return true;
3686
3687   /* If exceptions are enabled, we have collected nothrow info.  */
3688   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3689     return false;
3690
3691   return true;
3692 }
3693
3694 /* Output the call frame information used to record information
3695    that relates to calculating the frame pointer, and records the
3696    location of saved registers.  */
3697
3698 static void
3699 output_call_frame_info (int for_eh)
3700 {
3701   unsigned int i;
3702   dw_fde_ref fde;
3703   dw_cfi_ref cfi;
3704   char l1[20], l2[20], section_start_label[20];
3705   bool any_lsda_needed = false;
3706   char augmentation[6];
3707   int augmentation_size;
3708   int fde_encoding = DW_EH_PE_absptr;
3709   int per_encoding = DW_EH_PE_absptr;
3710   int lsda_encoding = DW_EH_PE_absptr;
3711   int return_reg;
3712   rtx personality = NULL;
3713   int dw_cie_version;
3714
3715   /* Don't emit a CIE if there won't be any FDEs.  */
3716   if (fde_table_in_use == 0)
3717     return;
3718
3719   /* Nothing to do if the assembler's doing it all.  */
3720   if (dwarf2out_do_cfi_asm ())
3721     return;
3722
3723   /* If we don't have any functions we'll want to unwind out of, don't emit
3724      any EH unwind information.  If we make FDEs linkonce, we may have to
3725      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3726      want to avoid having an FDE kept around when the function it refers to
3727      is discarded.  Example where this matters: a primary function template
3728      in C++ requires EH information, an explicit specialization doesn't.  */
3729   if (for_eh)
3730     {
3731       bool any_eh_needed = false;
3732
3733       for (i = 0; i < fde_table_in_use; i++)
3734         if (fde_table[i].uses_eh_lsda)
3735           any_eh_needed = any_lsda_needed = true;
3736         else if (fde_needed_for_eh_p (&fde_table[i]))
3737           any_eh_needed = true;
3738         else if (TARGET_USES_WEAK_UNWIND_INFO)
3739           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3740                                              1, 1);
3741
3742       if (!any_eh_needed)
3743         return;
3744     }
3745
3746   /* We're going to be generating comments, so turn on app.  */
3747   if (flag_debug_asm)
3748     app_enable ();
3749
3750   /* Switch to the proper frame section, first time.  */
3751   switch_to_frame_table_section (for_eh, false);
3752
3753   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3754   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3755
3756   /* Output the CIE.  */
3757   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3758   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3759   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3760     dw2_asm_output_data (4, 0xffffffff,
3761       "Initial length escape value indicating 64-bit DWARF extension");
3762   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3763                         "Length of Common Information Entry");
3764   ASM_OUTPUT_LABEL (asm_out_file, l1);
3765
3766   /* Now that the CIE pointer is PC-relative for EH,
3767      use 0 to identify the CIE.  */
3768   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3769                        (for_eh ? 0 : DWARF_CIE_ID),
3770                        "CIE Identifier Tag");
3771
3772   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3773      use CIE version 1, unless that would produce incorrect results
3774      due to overflowing the return register column.  */
3775   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3776   dw_cie_version = 1;
3777   if (return_reg >= 256 || dwarf_version > 2)
3778     dw_cie_version = 3;
3779   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3780
3781   augmentation[0] = 0;
3782   augmentation_size = 0;
3783
3784   personality = current_unit_personality;
3785   if (for_eh)
3786     {
3787       char *p;
3788
3789       /* Augmentation:
3790          z      Indicates that a uleb128 is present to size the
3791                 augmentation section.
3792          L      Indicates the encoding (and thus presence) of
3793                 an LSDA pointer in the FDE augmentation.
3794          R      Indicates a non-default pointer encoding for
3795                 FDE code pointers.
3796          P      Indicates the presence of an encoding + language
3797                 personality routine in the CIE augmentation.  */
3798
3799       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3800       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3801       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3802
3803       p = augmentation + 1;
3804       if (personality)
3805         {
3806           *p++ = 'P';
3807           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3808           assemble_external_libcall (personality);
3809         }
3810       if (any_lsda_needed)
3811         {
3812           *p++ = 'L';
3813           augmentation_size += 1;
3814         }
3815       if (fde_encoding != DW_EH_PE_absptr)
3816         {
3817           *p++ = 'R';
3818           augmentation_size += 1;
3819         }
3820       if (p > augmentation + 1)
3821         {
3822           augmentation[0] = 'z';
3823           *p = '\0';
3824         }
3825
3826       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3827       if (personality && per_encoding == DW_EH_PE_aligned)
3828         {
3829           int offset = (  4             /* Length */
3830                         + 4             /* CIE Id */
3831                         + 1             /* CIE version */
3832                         + strlen (augmentation) + 1     /* Augmentation */
3833                         + size_of_uleb128 (1)           /* Code alignment */
3834                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3835                         + 1             /* RA column */
3836                         + 1             /* Augmentation size */
3837                         + 1             /* Personality encoding */ );
3838           int pad = -offset & (PTR_SIZE - 1);
3839
3840           augmentation_size += pad;
3841
3842           /* Augmentations should be small, so there's scarce need to
3843              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3844           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3845         }
3846     }
3847
3848   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3849   if (dw_cie_version >= 4)
3850     {
3851       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3852       dw2_asm_output_data (1, 0, "CIE Segment Size");
3853     }
3854   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3855   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3856                                "CIE Data Alignment Factor");
3857
3858   if (dw_cie_version == 1)
3859     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3860   else
3861     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3862
3863   if (augmentation[0])
3864     {
3865       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3866       if (personality)
3867         {
3868           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3869                                eh_data_format_name (per_encoding));
3870           dw2_asm_output_encoded_addr_rtx (per_encoding,
3871                                            personality,
3872                                            true, NULL);
3873         }
3874
3875       if (any_lsda_needed)
3876         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3877                              eh_data_format_name (lsda_encoding));
3878
3879       if (fde_encoding != DW_EH_PE_absptr)
3880         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3881                              eh_data_format_name (fde_encoding));
3882     }
3883
3884   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3885     output_cfi (cfi, NULL, for_eh);
3886
3887   /* Pad the CIE out to an address sized boundary.  */
3888   ASM_OUTPUT_ALIGN (asm_out_file,
3889                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3890   ASM_OUTPUT_LABEL (asm_out_file, l2);
3891
3892   /* Loop through all of the FDE's.  */
3893   for (i = 0; i < fde_table_in_use; i++)
3894     {
3895       unsigned int k;
3896       fde = &fde_table[i];
3897
3898       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3899       if (for_eh && !fde_needed_for_eh_p (fde))
3900         continue;
3901
3902       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3903         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3904                     augmentation, any_lsda_needed, lsda_encoding);
3905     }
3906
3907   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3908     dw2_asm_output_data (4, 0, "End of Table");
3909 #ifdef MIPS_DEBUGGING_INFO
3910   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3911      get a value of 0.  Putting .align 0 after the label fixes it.  */
3912   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3913 #endif
3914
3915   /* Turn off app to make assembly quicker.  */
3916   if (flag_debug_asm)
3917     app_disable ();
3918 }
3919
3920 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3921
3922 static void
3923 dwarf2out_do_cfi_startproc (bool second)
3924 {
3925   int enc;
3926   rtx ref;
3927   rtx personality = get_personality_function (current_function_decl);
3928
3929   fprintf (asm_out_file, "\t.cfi_startproc\n");
3930
3931   if (personality)
3932     {
3933       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3934       ref = personality;
3935
3936       /* ??? The GAS support isn't entirely consistent.  We have to
3937          handle indirect support ourselves, but PC-relative is done
3938          in the assembler.  Further, the assembler can't handle any
3939          of the weirder relocation types.  */
3940       if (enc & DW_EH_PE_indirect)
3941         ref = dw2_force_const_mem (ref, true);
3942
3943       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3944       output_addr_const (asm_out_file, ref);
3945       fputc ('\n', asm_out_file);
3946     }
3947
3948   if (crtl->uses_eh_lsda)
3949     {
3950       char lab[20];
3951
3952       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3953       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3954                                    current_function_funcdef_no);
3955       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3956       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3957
3958       if (enc & DW_EH_PE_indirect)
3959         ref = dw2_force_const_mem (ref, true);
3960
3961       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3962       output_addr_const (asm_out_file, ref);
3963       fputc ('\n', asm_out_file);
3964     }
3965 }
3966
3967 /* Output a marker (i.e. a label) for the beginning of a function, before
3968    the prologue.  */
3969
3970 void
3971 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3972                           const char *file ATTRIBUTE_UNUSED)
3973 {
3974   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3975   char * dup_label;
3976   dw_fde_ref fde;
3977   section *fnsec;
3978
3979   current_function_func_begin_label = NULL;
3980
3981 #ifdef TARGET_UNWIND_INFO
3982   /* ??? current_function_func_begin_label is also used by except.c
3983      for call-site information.  We must emit this label if it might
3984      be used.  */
3985   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3986       && ! dwarf2out_do_frame ())
3987     return;
3988 #else
3989   if (! dwarf2out_do_frame ())
3990     return;
3991 #endif
3992
3993   fnsec = function_section (current_function_decl);
3994   switch_to_section (fnsec);
3995   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3996                                current_function_funcdef_no);
3997   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3998                           current_function_funcdef_no);
3999   dup_label = xstrdup (label);
4000   current_function_func_begin_label = dup_label;
4001
4002 #ifdef TARGET_UNWIND_INFO
4003   /* We can elide the fde allocation if we're not emitting debug info.  */
4004   if (! dwarf2out_do_frame ())
4005     return;
4006 #endif
4007
4008   /* Expand the fde table if necessary.  */
4009   if (fde_table_in_use == fde_table_allocated)
4010     {
4011       fde_table_allocated += FDE_TABLE_INCREMENT;
4012       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4013       memset (fde_table + fde_table_in_use, 0,
4014               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4015     }
4016
4017   /* Record the FDE associated with this function.  */
4018   current_funcdef_fde = fde_table_in_use;
4019
4020   /* Add the new FDE at the end of the fde_table.  */
4021   fde = &fde_table[fde_table_in_use++];
4022   fde->decl = current_function_decl;
4023   fde->dw_fde_begin = dup_label;
4024   fde->dw_fde_current_label = dup_label;
4025   fde->dw_fde_hot_section_label = NULL;
4026   fde->dw_fde_hot_section_end_label = NULL;
4027   fde->dw_fde_unlikely_section_label = NULL;
4028   fde->dw_fde_unlikely_section_end_label = NULL;
4029   fde->dw_fde_switched_sections = 0;
4030   fde->dw_fde_switched_cold_to_hot = 0;
4031   fde->dw_fde_end = NULL;
4032   fde->dw_fde_vms_end_prologue = NULL;
4033   fde->dw_fde_vms_begin_epilogue = NULL;
4034   fde->dw_fde_cfi = NULL;
4035   fde->dw_fde_switch_cfi = NULL;
4036   fde->funcdef_number = current_function_funcdef_no;
4037   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4038   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4039   fde->nothrow = crtl->nothrow;
4040   fde->drap_reg = INVALID_REGNUM;
4041   fde->vdrap_reg = INVALID_REGNUM;
4042   if (flag_reorder_blocks_and_partition)
4043     {
4044       section *unlikelysec;
4045       if (first_function_block_is_cold)
4046         fde->in_std_section = 1;
4047       else
4048         fde->in_std_section
4049           = (fnsec == text_section
4050              || (cold_text_section && fnsec == cold_text_section));
4051       unlikelysec = unlikely_text_section ();
4052       fde->cold_in_std_section
4053         = (unlikelysec == text_section
4054            || (cold_text_section && unlikelysec == cold_text_section));
4055     }
4056   else
4057     {
4058       fde->in_std_section
4059         = (fnsec == text_section
4060            || (cold_text_section && fnsec == cold_text_section));
4061       fde->cold_in_std_section = 0;
4062     }
4063
4064   args_size = old_args_size = 0;
4065
4066   /* We only want to output line number information for the genuine dwarf2
4067      prologue case, not the eh frame case.  */
4068 #ifdef DWARF2_DEBUGGING_INFO
4069   if (file)
4070     dwarf2out_source_line (line, file, 0, true);
4071 #endif
4072
4073   if (dwarf2out_do_cfi_asm ())
4074     dwarf2out_do_cfi_startproc (false);
4075   else
4076     {
4077       rtx personality = get_personality_function (current_function_decl);
4078       if (!current_unit_personality)
4079         current_unit_personality = personality;
4080
4081       /* We cannot keep a current personality per function as without CFI
4082          asm, at the point where we emit the CFI data, there is no current
4083          function anymore.  */
4084       if (personality && current_unit_personality != personality)
4085         sorry ("multiple EH personalities are supported only with assemblers "
4086                "supporting .cfi_personality directive");
4087     }
4088 }
4089
4090 /* Output a marker (i.e. a label) for the end of the generated code
4091    for a function prologue.  This gets called *after* the prologue code has
4092    been generated.  */
4093
4094 void
4095 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4096                         const char *file ATTRIBUTE_UNUSED)
4097 {
4098   dw_fde_ref fde;
4099   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4100
4101   /* Output a label to mark the endpoint of the code generated for this
4102      function.  */
4103   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4104                                current_function_funcdef_no);
4105   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4106                           current_function_funcdef_no);
4107   fde = &fde_table[fde_table_in_use - 1];
4108   fde->dw_fde_vms_end_prologue = xstrdup (label);
4109 }
4110
4111 /* Output a marker (i.e. a label) for the beginning of the generated code
4112    for a function epilogue.  This gets called *before* the prologue code has
4113    been generated.  */
4114
4115 void
4116 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4117                           const char *file ATTRIBUTE_UNUSED)
4118 {
4119   dw_fde_ref fde;
4120   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4121
4122   fde = &fde_table[fde_table_in_use - 1];
4123   if (fde->dw_fde_vms_begin_epilogue)
4124     return;
4125
4126   /* Output a label to mark the endpoint of the code generated for this
4127      function.  */
4128   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4129                                current_function_funcdef_no);
4130   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4131                           current_function_funcdef_no);
4132   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4133 }
4134
4135 /* Output a marker (i.e. a label) for the absolute end of the generated code
4136    for a function definition.  This gets called *after* the epilogue code has
4137    been generated.  */
4138
4139 void
4140 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4141                         const char *file ATTRIBUTE_UNUSED)
4142 {
4143   dw_fde_ref fde;
4144   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4145
4146   last_var_location_insn = NULL_RTX;
4147
4148   if (dwarf2out_do_cfi_asm ())
4149     fprintf (asm_out_file, "\t.cfi_endproc\n");
4150
4151   /* Output a label to mark the endpoint of the code generated for this
4152      function.  */
4153   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4154                                current_function_funcdef_no);
4155   ASM_OUTPUT_LABEL (asm_out_file, label);
4156   fde = current_fde ();
4157   gcc_assert (fde != NULL);
4158   fde->dw_fde_end = xstrdup (label);
4159 }
4160
4161 void
4162 dwarf2out_frame_init (void)
4163 {
4164   /* Allocate the initial hunk of the fde_table.  */
4165   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4166   fde_table_allocated = FDE_TABLE_INCREMENT;
4167   fde_table_in_use = 0;
4168
4169   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4170      sake of lookup_cfa.  */
4171
4172   /* On entry, the Canonical Frame Address is at SP.  */
4173   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4174
4175   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4176     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4177 }
4178
4179 void
4180 dwarf2out_frame_finish (void)
4181 {
4182   /* Output call frame information.  */
4183   if (DWARF2_FRAME_INFO)
4184     output_call_frame_info (0);
4185
4186 #ifndef TARGET_UNWIND_INFO
4187   /* Output another copy for the unwinder.  */
4188   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4189     output_call_frame_info (1);
4190 #endif
4191 }
4192
4193 /* Note that the current function section is being used for code.  */
4194
4195 static void
4196 dwarf2out_note_section_used (void)
4197 {
4198   section *sec = current_function_section ();
4199   if (sec == text_section)
4200     text_section_used = true;
4201   else if (sec == cold_text_section)
4202     cold_text_section_used = true;
4203 }
4204
4205 void
4206 dwarf2out_switch_text_section (void)
4207 {
4208   dw_fde_ref fde = current_fde ();
4209
4210   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4211
4212   fde->dw_fde_switched_sections = 1;
4213   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4214
4215   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4216   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4217   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4218   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4219   have_multiple_function_sections = true;
4220
4221   /* Reset the current label on switching text sections, so that we
4222      don't attempt to advance_loc4 between labels in different sections.  */
4223   fde->dw_fde_current_label = NULL;
4224
4225   /* There is no need to mark used sections when not debugging.  */
4226   if (cold_text_section != NULL)
4227     dwarf2out_note_section_used ();
4228
4229   if (dwarf2out_do_cfi_asm ())
4230     fprintf (asm_out_file, "\t.cfi_endproc\n");
4231
4232   /* Now do the real section switch.  */
4233   switch_to_section (current_function_section ());
4234
4235   if (dwarf2out_do_cfi_asm ())
4236     {
4237       dwarf2out_do_cfi_startproc (true);
4238       /* As this is a different FDE, insert all current CFI instructions
4239          again.  */
4240       output_cfis (fde->dw_fde_cfi, true, fde, true);
4241     }
4242   else
4243     {
4244       dw_cfi_ref cfi = fde->dw_fde_cfi;
4245
4246       cfi = fde->dw_fde_cfi;
4247       if (cfi)
4248         while (cfi->dw_cfi_next != NULL)
4249           cfi = cfi->dw_cfi_next;
4250       fde->dw_fde_switch_cfi = cfi;
4251     }
4252 }
4253 \f
4254 /* And now, the subset of the debugging information support code necessary
4255    for emitting location expressions.  */
4256
4257 /* Data about a single source file.  */
4258 struct GTY(()) dwarf_file_data {
4259   const char * filename;
4260   int emitted_number;
4261 };
4262
4263 typedef struct dw_val_struct *dw_val_ref;
4264 typedef struct die_struct *dw_die_ref;
4265 typedef const struct die_struct *const_dw_die_ref;
4266 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4267 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4268
4269 typedef struct GTY(()) deferred_locations_struct
4270 {
4271   tree variable;
4272   dw_die_ref die;
4273 } deferred_locations;
4274
4275 DEF_VEC_O(deferred_locations);
4276 DEF_VEC_ALLOC_O(deferred_locations,gc);
4277
4278 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4279
4280 DEF_VEC_P(dw_die_ref);
4281 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4282
4283 /* Each DIE may have a series of attribute/value pairs.  Values
4284    can take on several forms.  The forms that are used in this
4285    implementation are listed below.  */
4286
4287 enum dw_val_class
4288 {
4289   dw_val_class_addr,
4290   dw_val_class_offset,
4291   dw_val_class_loc,
4292   dw_val_class_loc_list,
4293   dw_val_class_range_list,
4294   dw_val_class_const,
4295   dw_val_class_unsigned_const,
4296   dw_val_class_const_double,
4297   dw_val_class_vec,
4298   dw_val_class_flag,
4299   dw_val_class_die_ref,
4300   dw_val_class_fde_ref,
4301   dw_val_class_lbl_id,
4302   dw_val_class_lineptr,
4303   dw_val_class_str,
4304   dw_val_class_macptr,
4305   dw_val_class_file,
4306   dw_val_class_data8,
4307   dw_val_class_decl_ref,
4308   dw_val_class_vms_delta
4309 };
4310
4311 /* Describe a floating point constant value, or a vector constant value.  */
4312
4313 typedef struct GTY(()) dw_vec_struct {
4314   unsigned char * GTY((length ("%h.length"))) array;
4315   unsigned length;
4316   unsigned elt_size;
4317 }
4318 dw_vec_const;
4319
4320 /* The dw_val_node describes an attribute's value, as it is
4321    represented internally.  */
4322
4323 typedef struct GTY(()) dw_val_struct {
4324   enum dw_val_class val_class;
4325   union dw_val_struct_union
4326     {
4327       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4328       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4329       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4330       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4331       HOST_WIDE_INT GTY ((default)) val_int;
4332       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4333       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4334       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4335       struct dw_val_die_union
4336         {
4337           dw_die_ref die;
4338           int external;
4339         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4340       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4341       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4342       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4343       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4344       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4345       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4346       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4347       struct dw_val_vms_delta_union
4348         {
4349           char * lbl1;
4350           char * lbl2;
4351         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4352     }
4353   GTY ((desc ("%1.val_class"))) v;
4354 }
4355 dw_val_node;
4356
4357 /* Locations in memory are described using a sequence of stack machine
4358    operations.  */
4359
4360 typedef struct GTY(()) dw_loc_descr_struct {
4361   dw_loc_descr_ref dw_loc_next;
4362   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4363   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4364      from DW_OP_addr with a dtp-relative symbol relocation.  */
4365   unsigned int dtprel : 1;
4366   int dw_loc_addr;
4367   dw_val_node dw_loc_oprnd1;
4368   dw_val_node dw_loc_oprnd2;
4369 }
4370 dw_loc_descr_node;
4371
4372 /* Location lists are ranges + location descriptions for that range,
4373    so you can track variables that are in different places over
4374    their entire life.  */
4375 typedef struct GTY(()) dw_loc_list_struct {
4376   dw_loc_list_ref dw_loc_next;
4377   const char *begin; /* Label for begin address of range */
4378   const char *end;  /* Label for end address of range */
4379   char *ll_symbol; /* Label for beginning of location list.
4380                       Only on head of list */
4381   const char *section; /* Section this loclist is relative to */
4382   dw_loc_descr_ref expr;
4383 } dw_loc_list_node;
4384
4385 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4386
4387 /* Convert a DWARF stack opcode into its string name.  */
4388
4389 static const char *
4390 dwarf_stack_op_name (unsigned int op)
4391 {
4392   switch (op)
4393     {
4394     case DW_OP_addr:
4395       return "DW_OP_addr";
4396     case DW_OP_deref:
4397       return "DW_OP_deref";
4398     case DW_OP_const1u:
4399       return "DW_OP_const1u";
4400     case DW_OP_const1s:
4401       return "DW_OP_const1s";
4402     case DW_OP_const2u:
4403       return "DW_OP_const2u";
4404     case DW_OP_const2s:
4405       return "DW_OP_const2s";
4406     case DW_OP_const4u:
4407       return "DW_OP_const4u";
4408     case DW_OP_const4s:
4409       return "DW_OP_const4s";
4410     case DW_OP_const8u:
4411       return "DW_OP_const8u";
4412     case DW_OP_const8s:
4413       return "DW_OP_const8s";
4414     case DW_OP_constu:
4415       return "DW_OP_constu";
4416     case DW_OP_consts:
4417       return "DW_OP_consts";
4418     case DW_OP_dup:
4419       return "DW_OP_dup";
4420     case DW_OP_drop:
4421       return "DW_OP_drop";
4422     case DW_OP_over:
4423       return "DW_OP_over";
4424     case DW_OP_pick:
4425       return "DW_OP_pick";
4426     case DW_OP_swap:
4427       return "DW_OP_swap";
4428     case DW_OP_rot:
4429       return "DW_OP_rot";
4430     case DW_OP_xderef:
4431       return "DW_OP_xderef";
4432     case DW_OP_abs:
4433       return "DW_OP_abs";
4434     case DW_OP_and:
4435       return "DW_OP_and";
4436     case DW_OP_div:
4437       return "DW_OP_div";
4438     case DW_OP_minus:
4439       return "DW_OP_minus";
4440     case DW_OP_mod:
4441       return "DW_OP_mod";
4442     case DW_OP_mul:
4443       return "DW_OP_mul";
4444     case DW_OP_neg:
4445       return "DW_OP_neg";
4446     case DW_OP_not:
4447       return "DW_OP_not";
4448     case DW_OP_or:
4449       return "DW_OP_or";
4450     case DW_OP_plus:
4451       return "DW_OP_plus";
4452     case DW_OP_plus_uconst:
4453       return "DW_OP_plus_uconst";
4454     case DW_OP_shl:
4455       return "DW_OP_shl";
4456     case DW_OP_shr:
4457       return "DW_OP_shr";
4458     case DW_OP_shra:
4459       return "DW_OP_shra";
4460     case DW_OP_xor:
4461       return "DW_OP_xor";
4462     case DW_OP_bra:
4463       return "DW_OP_bra";
4464     case DW_OP_eq:
4465       return "DW_OP_eq";
4466     case DW_OP_ge:
4467       return "DW_OP_ge";
4468     case DW_OP_gt:
4469       return "DW_OP_gt";
4470     case DW_OP_le:
4471       return "DW_OP_le";
4472     case DW_OP_lt:
4473       return "DW_OP_lt";
4474     case DW_OP_ne:
4475       return "DW_OP_ne";
4476     case DW_OP_skip:
4477       return "DW_OP_skip";
4478     case DW_OP_lit0:
4479       return "DW_OP_lit0";
4480     case DW_OP_lit1:
4481       return "DW_OP_lit1";
4482     case DW_OP_lit2:
4483       return "DW_OP_lit2";
4484     case DW_OP_lit3:
4485       return "DW_OP_lit3";
4486     case DW_OP_lit4:
4487       return "DW_OP_lit4";
4488     case DW_OP_lit5:
4489       return "DW_OP_lit5";
4490     case DW_OP_lit6:
4491       return "DW_OP_lit6";
4492     case DW_OP_lit7:
4493       return "DW_OP_lit7";
4494     case DW_OP_lit8:
4495       return "DW_OP_lit8";
4496     case DW_OP_lit9:
4497       return "DW_OP_lit9";
4498     case DW_OP_lit10:
4499       return "DW_OP_lit10";
4500     case DW_OP_lit11:
4501       return "DW_OP_lit11";
4502     case DW_OP_lit12:
4503       return "DW_OP_lit12";
4504     case DW_OP_lit13:
4505       return "DW_OP_lit13";
4506     case DW_OP_lit14:
4507       return "DW_OP_lit14";
4508     case DW_OP_lit15:
4509       return "DW_OP_lit15";
4510     case DW_OP_lit16:
4511       return "DW_OP_lit16";
4512     case DW_OP_lit17:
4513       return "DW_OP_lit17";
4514     case DW_OP_lit18:
4515       return "DW_OP_lit18";
4516     case DW_OP_lit19:
4517       return "DW_OP_lit19";
4518     case DW_OP_lit20:
4519       return "DW_OP_lit20";
4520     case DW_OP_lit21:
4521       return "DW_OP_lit21";
4522     case DW_OP_lit22:
4523       return "DW_OP_lit22";
4524     case DW_OP_lit23:
4525       return "DW_OP_lit23";
4526     case DW_OP_lit24:
4527       return "DW_OP_lit24";
4528     case DW_OP_lit25:
4529       return "DW_OP_lit25";
4530     case DW_OP_lit26:
4531       return "DW_OP_lit26";
4532     case DW_OP_lit27:
4533       return "DW_OP_lit27";
4534     case DW_OP_lit28:
4535       return "DW_OP_lit28";
4536     case DW_OP_lit29:
4537       return "DW_OP_lit29";
4538     case DW_OP_lit30:
4539       return "DW_OP_lit30";
4540     case DW_OP_lit31:
4541       return "DW_OP_lit31";
4542     case DW_OP_reg0:
4543       return "DW_OP_reg0";
4544     case DW_OP_reg1:
4545       return "DW_OP_reg1";
4546     case DW_OP_reg2:
4547       return "DW_OP_reg2";
4548     case DW_OP_reg3:
4549       return "DW_OP_reg3";
4550     case DW_OP_reg4:
4551       return "DW_OP_reg4";
4552     case DW_OP_reg5:
4553       return "DW_OP_reg5";
4554     case DW_OP_reg6:
4555       return "DW_OP_reg6";
4556     case DW_OP_reg7:
4557       return "DW_OP_reg7";
4558     case DW_OP_reg8:
4559       return "DW_OP_reg8";
4560     case DW_OP_reg9:
4561       return "DW_OP_reg9";
4562     case DW_OP_reg10:
4563       return "DW_OP_reg10";
4564     case DW_OP_reg11:
4565       return "DW_OP_reg11";
4566     case DW_OP_reg12:
4567       return "DW_OP_reg12";
4568     case DW_OP_reg13:
4569       return "DW_OP_reg13";
4570     case DW_OP_reg14:
4571       return "DW_OP_reg14";
4572     case DW_OP_reg15:
4573       return "DW_OP_reg15";
4574     case DW_OP_reg16:
4575       return "DW_OP_reg16";
4576     case DW_OP_reg17:
4577       return "DW_OP_reg17";
4578     case DW_OP_reg18:
4579       return "DW_OP_reg18";
4580     case DW_OP_reg19:
4581       return "DW_OP_reg19";
4582     case DW_OP_reg20:
4583       return "DW_OP_reg20";
4584     case DW_OP_reg21:
4585       return "DW_OP_reg21";
4586     case DW_OP_reg22:
4587       return "DW_OP_reg22";
4588     case DW_OP_reg23:
4589       return "DW_OP_reg23";
4590     case DW_OP_reg24:
4591       return "DW_OP_reg24";
4592     case DW_OP_reg25:
4593       return "DW_OP_reg25";
4594     case DW_OP_reg26:
4595       return "DW_OP_reg26";
4596     case DW_OP_reg27:
4597       return "DW_OP_reg27";
4598     case DW_OP_reg28:
4599       return "DW_OP_reg28";
4600     case DW_OP_reg29:
4601       return "DW_OP_reg29";
4602     case DW_OP_reg30:
4603       return "DW_OP_reg30";
4604     case DW_OP_reg31:
4605       return "DW_OP_reg31";
4606     case DW_OP_breg0:
4607       return "DW_OP_breg0";
4608     case DW_OP_breg1:
4609       return "DW_OP_breg1";
4610     case DW_OP_breg2:
4611       return "DW_OP_breg2";
4612     case DW_OP_breg3:
4613       return "DW_OP_breg3";
4614     case DW_OP_breg4:
4615       return "DW_OP_breg4";
4616     case DW_OP_breg5:
4617       return "DW_OP_breg5";
4618     case DW_OP_breg6:
4619       return "DW_OP_breg6";
4620     case DW_OP_breg7:
4621       return "DW_OP_breg7";
4622     case DW_OP_breg8:
4623       return "DW_OP_breg8";
4624     case DW_OP_breg9:
4625       return "DW_OP_breg9";
4626     case DW_OP_breg10:
4627       return "DW_OP_breg10";
4628     case DW_OP_breg11:
4629       return "DW_OP_breg11";
4630     case DW_OP_breg12:
4631       return "DW_OP_breg12";
4632     case DW_OP_breg13:
4633       return "DW_OP_breg13";
4634     case DW_OP_breg14:
4635       return "DW_OP_breg14";
4636     case DW_OP_breg15:
4637       return "DW_OP_breg15";
4638     case DW_OP_breg16:
4639       return "DW_OP_breg16";
4640     case DW_OP_breg17:
4641       return "DW_OP_breg17";
4642     case DW_OP_breg18:
4643       return "DW_OP_breg18";
4644     case DW_OP_breg19:
4645       return "DW_OP_breg19";
4646     case DW_OP_breg20:
4647       return "DW_OP_breg20";
4648     case DW_OP_breg21:
4649       return "DW_OP_breg21";
4650     case DW_OP_breg22:
4651       return "DW_OP_breg22";
4652     case DW_OP_breg23:
4653       return "DW_OP_breg23";
4654     case DW_OP_breg24:
4655       return "DW_OP_breg24";
4656     case DW_OP_breg25:
4657       return "DW_OP_breg25";
4658     case DW_OP_breg26:
4659       return "DW_OP_breg26";
4660     case DW_OP_breg27:
4661       return "DW_OP_breg27";
4662     case DW_OP_breg28:
4663       return "DW_OP_breg28";
4664     case DW_OP_breg29:
4665       return "DW_OP_breg29";
4666     case DW_OP_breg30:
4667       return "DW_OP_breg30";
4668     case DW_OP_breg31:
4669       return "DW_OP_breg31";
4670     case DW_OP_regx:
4671       return "DW_OP_regx";
4672     case DW_OP_fbreg:
4673       return "DW_OP_fbreg";
4674     case DW_OP_bregx:
4675       return "DW_OP_bregx";
4676     case DW_OP_piece:
4677       return "DW_OP_piece";
4678     case DW_OP_deref_size:
4679       return "DW_OP_deref_size";
4680     case DW_OP_xderef_size:
4681       return "DW_OP_xderef_size";
4682     case DW_OP_nop:
4683       return "DW_OP_nop";
4684
4685     case DW_OP_push_object_address:
4686       return "DW_OP_push_object_address";
4687     case DW_OP_call2:
4688       return "DW_OP_call2";
4689     case DW_OP_call4:
4690       return "DW_OP_call4";
4691     case DW_OP_call_ref:
4692       return "DW_OP_call_ref";
4693     case DW_OP_implicit_value:
4694       return "DW_OP_implicit_value";
4695     case DW_OP_stack_value:
4696       return "DW_OP_stack_value";
4697     case DW_OP_form_tls_address:
4698       return "DW_OP_form_tls_address";
4699     case DW_OP_call_frame_cfa:
4700       return "DW_OP_call_frame_cfa";
4701     case DW_OP_bit_piece:
4702       return "DW_OP_bit_piece";
4703
4704     case DW_OP_GNU_push_tls_address:
4705       return "DW_OP_GNU_push_tls_address";
4706     case DW_OP_GNU_uninit:
4707       return "DW_OP_GNU_uninit";
4708     case DW_OP_GNU_encoded_addr:
4709       return "DW_OP_GNU_encoded_addr";
4710     case DW_OP_GNU_implicit_pointer:
4711       return "DW_OP_GNU_implicit_pointer";
4712
4713     default:
4714       return "OP_<unknown>";
4715     }
4716 }
4717
4718 /* Return a pointer to a newly allocated location description.  Location
4719    descriptions are simple expression terms that can be strung
4720    together to form more complicated location (address) descriptions.  */
4721
4722 static inline dw_loc_descr_ref
4723 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4724                unsigned HOST_WIDE_INT oprnd2)
4725 {
4726   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4727
4728   descr->dw_loc_opc = op;
4729   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4730   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4731   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4732   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4733
4734   return descr;
4735 }
4736
4737 /* Return a pointer to a newly allocated location description for
4738    REG and OFFSET.  */
4739
4740 static inline dw_loc_descr_ref
4741 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4742 {
4743   if (reg <= 31)
4744     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4745                           offset, 0);
4746   else
4747     return new_loc_descr (DW_OP_bregx, reg, offset);
4748 }
4749
4750 /* Add a location description term to a location description expression.  */
4751
4752 static inline void
4753 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4754 {
4755   dw_loc_descr_ref *d;
4756
4757   /* Find the end of the chain.  */
4758   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4759     ;
4760
4761   *d = descr;
4762 }
4763
4764 /* Add a constant OFFSET to a location expression.  */
4765
4766 static void
4767 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4768 {
4769   dw_loc_descr_ref loc;
4770   HOST_WIDE_INT *p;
4771
4772   gcc_assert (*list_head != NULL);
4773
4774   if (!offset)
4775     return;
4776
4777   /* Find the end of the chain.  */
4778   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4779     ;
4780
4781   p = NULL;
4782   if (loc->dw_loc_opc == DW_OP_fbreg
4783       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4784     p = &loc->dw_loc_oprnd1.v.val_int;
4785   else if (loc->dw_loc_opc == DW_OP_bregx)
4786     p = &loc->dw_loc_oprnd2.v.val_int;
4787
4788   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4789      offset.  Don't optimize if an signed integer overflow would happen.  */
4790   if (p != NULL
4791       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4792           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4793     *p += offset;
4794
4795   else if (offset > 0)
4796     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4797
4798   else
4799     {
4800       loc->dw_loc_next = int_loc_descriptor (-offset);
4801       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4802     }
4803 }
4804
4805 /* Add a constant OFFSET to a location list.  */
4806
4807 static void
4808 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4809 {
4810   dw_loc_list_ref d;
4811   for (d = list_head; d != NULL; d = d->dw_loc_next)
4812     loc_descr_plus_const (&d->expr, offset);
4813 }
4814
4815 #define DWARF_REF_SIZE  \
4816   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4817
4818 /* Return the size of a location descriptor.  */
4819
4820 static unsigned long
4821 size_of_loc_descr (dw_loc_descr_ref loc)
4822 {
4823   unsigned long size = 1;
4824
4825   switch (loc->dw_loc_opc)
4826     {
4827     case DW_OP_addr:
4828       size += DWARF2_ADDR_SIZE;
4829       break;
4830     case DW_OP_const1u:
4831     case DW_OP_const1s:
4832       size += 1;
4833       break;
4834     case DW_OP_const2u:
4835     case DW_OP_const2s:
4836       size += 2;
4837       break;
4838     case DW_OP_const4u:
4839     case DW_OP_const4s:
4840       size += 4;
4841       break;
4842     case DW_OP_const8u:
4843     case DW_OP_const8s:
4844       size += 8;
4845       break;
4846     case DW_OP_constu:
4847       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4848       break;
4849     case DW_OP_consts:
4850       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4851       break;
4852     case DW_OP_pick:
4853       size += 1;
4854       break;
4855     case DW_OP_plus_uconst:
4856       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4857       break;
4858     case DW_OP_skip:
4859     case DW_OP_bra:
4860       size += 2;
4861       break;
4862     case DW_OP_breg0:
4863     case DW_OP_breg1:
4864     case DW_OP_breg2:
4865     case DW_OP_breg3:
4866     case DW_OP_breg4:
4867     case DW_OP_breg5:
4868     case DW_OP_breg6:
4869     case DW_OP_breg7:
4870     case DW_OP_breg8:
4871     case DW_OP_breg9:
4872     case DW_OP_breg10:
4873     case DW_OP_breg11:
4874     case DW_OP_breg12:
4875     case DW_OP_breg13:
4876     case DW_OP_breg14:
4877     case DW_OP_breg15:
4878     case DW_OP_breg16:
4879     case DW_OP_breg17:
4880     case DW_OP_breg18:
4881     case DW_OP_breg19:
4882     case DW_OP_breg20:
4883     case DW_OP_breg21:
4884     case DW_OP_breg22:
4885     case DW_OP_breg23:
4886     case DW_OP_breg24:
4887     case DW_OP_breg25:
4888     case DW_OP_breg26:
4889     case DW_OP_breg27:
4890     case DW_OP_breg28:
4891     case DW_OP_breg29:
4892     case DW_OP_breg30:
4893     case DW_OP_breg31:
4894       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4895       break;
4896     case DW_OP_regx:
4897       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4898       break;
4899     case DW_OP_fbreg:
4900       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4901       break;
4902     case DW_OP_bregx:
4903       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4904       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4905       break;
4906     case DW_OP_piece:
4907       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4908       break;
4909     case DW_OP_bit_piece:
4910       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4911       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4912       break;
4913     case DW_OP_deref_size:
4914     case DW_OP_xderef_size:
4915       size += 1;
4916       break;
4917     case DW_OP_call2:
4918       size += 2;
4919       break;
4920     case DW_OP_call4:
4921       size += 4;
4922       break;
4923     case DW_OP_call_ref:
4924       size += DWARF_REF_SIZE;
4925       break;
4926     case DW_OP_implicit_value:
4927       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4928               + loc->dw_loc_oprnd1.v.val_unsigned;
4929       break;
4930     case DW_OP_GNU_implicit_pointer:
4931       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4932       break;
4933     default:
4934       break;
4935     }
4936
4937   return size;
4938 }
4939
4940 /* Return the size of a series of location descriptors.  */
4941
4942 static unsigned long
4943 size_of_locs (dw_loc_descr_ref loc)
4944 {
4945   dw_loc_descr_ref l;
4946   unsigned long size;
4947
4948   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4949      field, to avoid writing to a PCH file.  */
4950   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4951     {
4952       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4953         break;
4954       size += size_of_loc_descr (l);
4955     }
4956   if (! l)
4957     return size;
4958
4959   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4960     {
4961       l->dw_loc_addr = size;
4962       size += size_of_loc_descr (l);
4963     }
4964
4965   return size;
4966 }
4967
4968 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4969 static void get_ref_die_offset_label (char *, dw_die_ref);
4970
4971 /* Output location description stack opcode's operands (if any).  */
4972
4973 static void
4974 output_loc_operands (dw_loc_descr_ref loc)
4975 {
4976   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4977   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4978
4979   switch (loc->dw_loc_opc)
4980     {
4981 #ifdef DWARF2_DEBUGGING_INFO
4982     case DW_OP_const2u:
4983     case DW_OP_const2s:
4984       dw2_asm_output_data (2, val1->v.val_int, NULL);
4985       break;
4986     case DW_OP_const4u:
4987       if (loc->dtprel)
4988         {
4989           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4990           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4991                                                val1->v.val_addr);
4992           fputc ('\n', asm_out_file);
4993           break;
4994         }
4995       /* FALLTHRU */
4996     case DW_OP_const4s:
4997       dw2_asm_output_data (4, val1->v.val_int, NULL);
4998       break;
4999     case DW_OP_const8u:
5000       if (loc->dtprel)
5001         {
5002           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5003           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5004                                                val1->v.val_addr);
5005           fputc ('\n', asm_out_file);
5006           break;
5007         }
5008       /* FALLTHRU */
5009     case DW_OP_const8s:
5010       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5011       dw2_asm_output_data (8, val1->v.val_int, NULL);
5012       break;
5013     case DW_OP_skip:
5014     case DW_OP_bra:
5015       {
5016         int offset;
5017
5018         gcc_assert (val1->val_class == dw_val_class_loc);
5019         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5020
5021         dw2_asm_output_data (2, offset, NULL);
5022       }
5023       break;
5024     case DW_OP_implicit_value:
5025       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5026       switch (val2->val_class)
5027         {
5028         case dw_val_class_const:
5029           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5030           break;
5031         case dw_val_class_vec:
5032           {
5033             unsigned int elt_size = val2->v.val_vec.elt_size;
5034             unsigned int len = val2->v.val_vec.length;
5035             unsigned int i;
5036             unsigned char *p;
5037
5038             if (elt_size > sizeof (HOST_WIDE_INT))
5039               {
5040                 elt_size /= 2;
5041                 len *= 2;
5042               }
5043             for (i = 0, p = val2->v.val_vec.array;
5044                  i < len;
5045                  i++, p += elt_size)
5046               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5047                                    "fp or vector constant word %u", i);
5048           }
5049           break;
5050         case dw_val_class_const_double:
5051           {
5052             unsigned HOST_WIDE_INT first, second;
5053
5054             if (WORDS_BIG_ENDIAN)
5055               {
5056                 first = val2->v.val_double.high;
5057                 second = val2->v.val_double.low;
5058               }
5059             else
5060               {
5061                 first = val2->v.val_double.low;
5062                 second = val2->v.val_double.high;
5063               }
5064             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5065                                  first, NULL);
5066             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5067                                  second, NULL);
5068           }
5069           break;
5070         case dw_val_class_addr:
5071           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5072           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5073           break;
5074         default:
5075           gcc_unreachable ();
5076         }
5077       break;
5078 #else
5079     case DW_OP_const2u:
5080     case DW_OP_const2s:
5081     case DW_OP_const4u:
5082     case DW_OP_const4s:
5083     case DW_OP_const8u:
5084     case DW_OP_const8s:
5085     case DW_OP_skip:
5086     case DW_OP_bra:
5087     case DW_OP_implicit_value:
5088       /* We currently don't make any attempt to make sure these are
5089          aligned properly like we do for the main unwind info, so
5090          don't support emitting things larger than a byte if we're
5091          only doing unwinding.  */
5092       gcc_unreachable ();
5093 #endif
5094     case DW_OP_const1u:
5095     case DW_OP_const1s:
5096       dw2_asm_output_data (1, val1->v.val_int, NULL);
5097       break;
5098     case DW_OP_constu:
5099       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5100       break;
5101     case DW_OP_consts:
5102       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5103       break;
5104     case DW_OP_pick:
5105       dw2_asm_output_data (1, val1->v.val_int, NULL);
5106       break;
5107     case DW_OP_plus_uconst:
5108       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5109       break;
5110     case DW_OP_breg0:
5111     case DW_OP_breg1:
5112     case DW_OP_breg2:
5113     case DW_OP_breg3:
5114     case DW_OP_breg4:
5115     case DW_OP_breg5:
5116     case DW_OP_breg6:
5117     case DW_OP_breg7:
5118     case DW_OP_breg8:
5119     case DW_OP_breg9:
5120     case DW_OP_breg10:
5121     case DW_OP_breg11:
5122     case DW_OP_breg12:
5123     case DW_OP_breg13:
5124     case DW_OP_breg14:
5125     case DW_OP_breg15:
5126     case DW_OP_breg16:
5127     case DW_OP_breg17:
5128     case DW_OP_breg18:
5129     case DW_OP_breg19:
5130     case DW_OP_breg20:
5131     case DW_OP_breg21:
5132     case DW_OP_breg22:
5133     case DW_OP_breg23:
5134     case DW_OP_breg24:
5135     case DW_OP_breg25:
5136     case DW_OP_breg26:
5137     case DW_OP_breg27:
5138     case DW_OP_breg28:
5139     case DW_OP_breg29:
5140     case DW_OP_breg30:
5141     case DW_OP_breg31:
5142       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5143       break;
5144     case DW_OP_regx:
5145       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5146       break;
5147     case DW_OP_fbreg:
5148       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5149       break;
5150     case DW_OP_bregx:
5151       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5152       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5153       break;
5154     case DW_OP_piece:
5155       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5156       break;
5157     case DW_OP_bit_piece:
5158       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5159       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5160       break;
5161     case DW_OP_deref_size:
5162     case DW_OP_xderef_size:
5163       dw2_asm_output_data (1, val1->v.val_int, NULL);
5164       break;
5165
5166     case DW_OP_addr:
5167       if (loc->dtprel)
5168         {
5169           if (targetm.asm_out.output_dwarf_dtprel)
5170             {
5171               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5172                                                    DWARF2_ADDR_SIZE,
5173                                                    val1->v.val_addr);
5174               fputc ('\n', asm_out_file);
5175             }
5176           else
5177             gcc_unreachable ();
5178         }
5179       else
5180         {
5181 #ifdef DWARF2_DEBUGGING_INFO
5182           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5183 #else
5184           gcc_unreachable ();
5185 #endif
5186         }
5187       break;
5188
5189     case DW_OP_GNU_implicit_pointer:
5190       {
5191         char label[MAX_ARTIFICIAL_LABEL_BYTES
5192                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5193         gcc_assert (val1->val_class == dw_val_class_die_ref);
5194         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5195         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5196         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5197       }
5198       break;
5199
5200     default:
5201       /* Other codes have no operands.  */
5202       break;
5203     }
5204 }
5205
5206 /* Output a sequence of location operations.  */
5207
5208 static void
5209 output_loc_sequence (dw_loc_descr_ref loc)
5210 {
5211   for (; loc != NULL; loc = loc->dw_loc_next)
5212     {
5213       /* Output the opcode.  */
5214       dw2_asm_output_data (1, loc->dw_loc_opc,
5215                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5216
5217       /* Output the operand(s) (if any).  */
5218       output_loc_operands (loc);
5219     }
5220 }
5221
5222 /* Output location description stack opcode's operands (if any).
5223    The output is single bytes on a line, suitable for .cfi_escape.  */
5224
5225 static void
5226 output_loc_operands_raw (dw_loc_descr_ref loc)
5227 {
5228   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5229   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5230
5231   switch (loc->dw_loc_opc)
5232     {
5233     case DW_OP_addr:
5234     case DW_OP_implicit_value:
5235       /* We cannot output addresses in .cfi_escape, only bytes.  */
5236       gcc_unreachable ();
5237
5238     case DW_OP_const1u:
5239     case DW_OP_const1s:
5240     case DW_OP_pick:
5241     case DW_OP_deref_size:
5242     case DW_OP_xderef_size:
5243       fputc (',', asm_out_file);
5244       dw2_asm_output_data_raw (1, val1->v.val_int);
5245       break;
5246
5247     case DW_OP_const2u:
5248     case DW_OP_const2s:
5249       fputc (',', asm_out_file);
5250       dw2_asm_output_data_raw (2, val1->v.val_int);
5251       break;
5252
5253     case DW_OP_const4u:
5254     case DW_OP_const4s:
5255       fputc (',', asm_out_file);
5256       dw2_asm_output_data_raw (4, val1->v.val_int);
5257       break;
5258
5259     case DW_OP_const8u:
5260     case DW_OP_const8s:
5261       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5262       fputc (',', asm_out_file);
5263       dw2_asm_output_data_raw (8, val1->v.val_int);
5264       break;
5265
5266     case DW_OP_skip:
5267     case DW_OP_bra:
5268       {
5269         int offset;
5270
5271         gcc_assert (val1->val_class == dw_val_class_loc);
5272         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5273
5274         fputc (',', asm_out_file);
5275         dw2_asm_output_data_raw (2, offset);
5276       }
5277       break;
5278
5279     case DW_OP_constu:
5280     case DW_OP_plus_uconst:
5281     case DW_OP_regx:
5282     case DW_OP_piece:
5283       fputc (',', asm_out_file);
5284       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5285       break;
5286
5287     case DW_OP_bit_piece:
5288       fputc (',', asm_out_file);
5289       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5290       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5291       break;
5292
5293     case DW_OP_consts:
5294     case DW_OP_breg0:
5295     case DW_OP_breg1:
5296     case DW_OP_breg2:
5297     case DW_OP_breg3:
5298     case DW_OP_breg4:
5299     case DW_OP_breg5:
5300     case DW_OP_breg6:
5301     case DW_OP_breg7:
5302     case DW_OP_breg8:
5303     case DW_OP_breg9:
5304     case DW_OP_breg10:
5305     case DW_OP_breg11:
5306     case DW_OP_breg12:
5307     case DW_OP_breg13:
5308     case DW_OP_breg14:
5309     case DW_OP_breg15:
5310     case DW_OP_breg16:
5311     case DW_OP_breg17:
5312     case DW_OP_breg18:
5313     case DW_OP_breg19:
5314     case DW_OP_breg20:
5315     case DW_OP_breg21:
5316     case DW_OP_breg22:
5317     case DW_OP_breg23:
5318     case DW_OP_breg24:
5319     case DW_OP_breg25:
5320     case DW_OP_breg26:
5321     case DW_OP_breg27:
5322     case DW_OP_breg28:
5323     case DW_OP_breg29:
5324     case DW_OP_breg30:
5325     case DW_OP_breg31:
5326     case DW_OP_fbreg:
5327       fputc (',', asm_out_file);
5328       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5329       break;
5330
5331     case DW_OP_bregx:
5332       fputc (',', asm_out_file);
5333       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5334       fputc (',', asm_out_file);
5335       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5336       break;
5337
5338     case DW_OP_GNU_implicit_pointer:
5339       gcc_unreachable ();
5340       break;
5341
5342     default:
5343       /* Other codes have no operands.  */
5344       break;
5345     }
5346 }
5347
5348 static void
5349 output_loc_sequence_raw (dw_loc_descr_ref loc)
5350 {
5351   while (1)
5352     {
5353       /* Output the opcode.  */
5354       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5355       output_loc_operands_raw (loc);
5356
5357       if (!loc->dw_loc_next)
5358         break;
5359       loc = loc->dw_loc_next;
5360
5361       fputc (',', asm_out_file);
5362     }
5363 }
5364
5365 /* This routine will generate the correct assembly data for a location
5366    description based on a cfi entry with a complex address.  */
5367
5368 static void
5369 output_cfa_loc (dw_cfi_ref cfi)
5370 {
5371   dw_loc_descr_ref loc;
5372   unsigned long size;
5373
5374   if (cfi->dw_cfi_opc == DW_CFA_expression)
5375     {
5376       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5377       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5378     }
5379   else
5380     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5381
5382   /* Output the size of the block.  */
5383   size = size_of_locs (loc);
5384   dw2_asm_output_data_uleb128 (size, NULL);
5385
5386   /* Now output the operations themselves.  */
5387   output_loc_sequence (loc);
5388 }
5389
5390 /* Similar, but used for .cfi_escape.  */
5391
5392 static void
5393 output_cfa_loc_raw (dw_cfi_ref cfi)
5394 {
5395   dw_loc_descr_ref loc;
5396   unsigned long size;
5397
5398   if (cfi->dw_cfi_opc == DW_CFA_expression)
5399     {
5400       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5401       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5402     }
5403   else
5404     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5405
5406   /* Output the size of the block.  */
5407   size = size_of_locs (loc);
5408   dw2_asm_output_data_uleb128_raw (size);
5409   fputc (',', asm_out_file);
5410
5411   /* Now output the operations themselves.  */
5412   output_loc_sequence_raw (loc);
5413 }
5414
5415 /* This function builds a dwarf location descriptor sequence from a
5416    dw_cfa_location, adding the given OFFSET to the result of the
5417    expression.  */
5418
5419 static struct dw_loc_descr_struct *
5420 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5421 {
5422   struct dw_loc_descr_struct *head, *tmp;
5423
5424   offset += cfa->offset;
5425
5426   if (cfa->indirect)
5427     {
5428       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5429       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5430       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5431       add_loc_descr (&head, tmp);
5432       if (offset != 0)
5433         {
5434           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5435           add_loc_descr (&head, tmp);
5436         }
5437     }
5438   else
5439     head = new_reg_loc_descr (cfa->reg, offset);
5440
5441   return head;
5442 }
5443
5444 /* This function builds a dwarf location descriptor sequence for
5445    the address at OFFSET from the CFA when stack is aligned to
5446    ALIGNMENT byte.  */
5447
5448 static struct dw_loc_descr_struct *
5449 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5450 {
5451   struct dw_loc_descr_struct *head;
5452   unsigned int dwarf_fp
5453     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5454
5455  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5456   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5457     {
5458       head = new_reg_loc_descr (dwarf_fp, 0);
5459       add_loc_descr (&head, int_loc_descriptor (alignment));
5460       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5461       loc_descr_plus_const (&head, offset);
5462     }
5463   else
5464     head = new_reg_loc_descr (dwarf_fp, offset);
5465   return head;
5466 }
5467
5468 /* This function fills in aa dw_cfa_location structure from a dwarf location
5469    descriptor sequence.  */
5470
5471 static void
5472 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5473 {
5474   struct dw_loc_descr_struct *ptr;
5475   cfa->offset = 0;
5476   cfa->base_offset = 0;
5477   cfa->indirect = 0;
5478   cfa->reg = -1;
5479
5480   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5481     {
5482       enum dwarf_location_atom op = ptr->dw_loc_opc;
5483
5484       switch (op)
5485         {
5486         case DW_OP_reg0:
5487         case DW_OP_reg1:
5488         case DW_OP_reg2:
5489         case DW_OP_reg3:
5490         case DW_OP_reg4:
5491         case DW_OP_reg5:
5492         case DW_OP_reg6:
5493         case DW_OP_reg7:
5494         case DW_OP_reg8:
5495         case DW_OP_reg9:
5496         case DW_OP_reg10:
5497         case DW_OP_reg11:
5498         case DW_OP_reg12:
5499         case DW_OP_reg13:
5500         case DW_OP_reg14:
5501         case DW_OP_reg15:
5502         case DW_OP_reg16:
5503         case DW_OP_reg17:
5504         case DW_OP_reg18:
5505         case DW_OP_reg19:
5506         case DW_OP_reg20:
5507         case DW_OP_reg21:
5508         case DW_OP_reg22:
5509         case DW_OP_reg23:
5510         case DW_OP_reg24:
5511         case DW_OP_reg25:
5512         case DW_OP_reg26:
5513         case DW_OP_reg27:
5514         case DW_OP_reg28:
5515         case DW_OP_reg29:
5516         case DW_OP_reg30:
5517         case DW_OP_reg31:
5518           cfa->reg = op - DW_OP_reg0;
5519           break;
5520         case DW_OP_regx:
5521           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5522           break;
5523         case DW_OP_breg0:
5524         case DW_OP_breg1:
5525         case DW_OP_breg2:
5526         case DW_OP_breg3:
5527         case DW_OP_breg4:
5528         case DW_OP_breg5:
5529         case DW_OP_breg6:
5530         case DW_OP_breg7:
5531         case DW_OP_breg8:
5532         case DW_OP_breg9:
5533         case DW_OP_breg10:
5534         case DW_OP_breg11:
5535         case DW_OP_breg12:
5536         case DW_OP_breg13:
5537         case DW_OP_breg14:
5538         case DW_OP_breg15:
5539         case DW_OP_breg16:
5540         case DW_OP_breg17:
5541         case DW_OP_breg18:
5542         case DW_OP_breg19:
5543         case DW_OP_breg20:
5544         case DW_OP_breg21:
5545         case DW_OP_breg22:
5546         case DW_OP_breg23:
5547         case DW_OP_breg24:
5548         case DW_OP_breg25:
5549         case DW_OP_breg26:
5550         case DW_OP_breg27:
5551         case DW_OP_breg28:
5552         case DW_OP_breg29:
5553         case DW_OP_breg30:
5554         case DW_OP_breg31:
5555           cfa->reg = op - DW_OP_breg0;
5556           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5557           break;
5558         case DW_OP_bregx:
5559           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5560           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5561           break;
5562         case DW_OP_deref:
5563           cfa->indirect = 1;
5564           break;
5565         case DW_OP_plus_uconst:
5566           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5567           break;
5568         default:
5569           internal_error ("DW_LOC_OP %s not implemented",
5570                           dwarf_stack_op_name (ptr->dw_loc_opc));
5571         }
5572     }
5573 }
5574 \f
5575 /* And now, the support for symbolic debugging information.  */
5576
5577 /* .debug_str support.  */
5578 static int output_indirect_string (void **, void *);
5579
5580 static void dwarf2out_init (const char *);
5581 static void dwarf2out_finish (const char *);
5582 static void dwarf2out_assembly_start (void);
5583 static void dwarf2out_define (unsigned int, const char *);
5584 static void dwarf2out_undef (unsigned int, const char *);
5585 static void dwarf2out_start_source_file (unsigned, const char *);
5586 static void dwarf2out_end_source_file (unsigned);
5587 static void dwarf2out_function_decl (tree);
5588 static void dwarf2out_begin_block (unsigned, unsigned);
5589 static void dwarf2out_end_block (unsigned, unsigned);
5590 static bool dwarf2out_ignore_block (const_tree);
5591 static void dwarf2out_global_decl (tree);
5592 static void dwarf2out_type_decl (tree, int);
5593 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5594 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5595                                                  dw_die_ref);
5596 static void dwarf2out_abstract_function (tree);
5597 static void dwarf2out_var_location (rtx);
5598 static void dwarf2out_direct_call (tree);
5599 static void dwarf2out_virtual_call_token (tree, int);
5600 static void dwarf2out_copy_call_info (rtx, rtx);
5601 static void dwarf2out_virtual_call (int);
5602 static void dwarf2out_begin_function (tree);
5603 static void dwarf2out_set_name (tree, tree);
5604
5605 /* The debug hooks structure.  */
5606
5607 const struct gcc_debug_hooks dwarf2_debug_hooks =
5608 {
5609   dwarf2out_init,
5610   dwarf2out_finish,
5611   dwarf2out_assembly_start,
5612   dwarf2out_define,
5613   dwarf2out_undef,
5614   dwarf2out_start_source_file,
5615   dwarf2out_end_source_file,
5616   dwarf2out_begin_block,
5617   dwarf2out_end_block,
5618   dwarf2out_ignore_block,
5619   dwarf2out_source_line,
5620   dwarf2out_begin_prologue,
5621 #if VMS_DEBUGGING_INFO
5622   dwarf2out_vms_end_prologue,
5623   dwarf2out_vms_begin_epilogue,
5624 #else
5625   debug_nothing_int_charstar,
5626   debug_nothing_int_charstar,
5627 #endif
5628   dwarf2out_end_epilogue,
5629   dwarf2out_begin_function,
5630   debug_nothing_int,            /* end_function */
5631   dwarf2out_function_decl,      /* function_decl */
5632   dwarf2out_global_decl,
5633   dwarf2out_type_decl,          /* type_decl */
5634   dwarf2out_imported_module_or_decl,
5635   debug_nothing_tree,           /* deferred_inline_function */
5636   /* The DWARF 2 backend tries to reduce debugging bloat by not
5637      emitting the abstract description of inline functions until
5638      something tries to reference them.  */
5639   dwarf2out_abstract_function,  /* outlining_inline_function */
5640   debug_nothing_rtx,            /* label */
5641   debug_nothing_int,            /* handle_pch */
5642   dwarf2out_var_location,
5643   dwarf2out_switch_text_section,
5644   dwarf2out_direct_call,
5645   dwarf2out_virtual_call_token,
5646   dwarf2out_copy_call_info,
5647   dwarf2out_virtual_call,
5648   dwarf2out_set_name,
5649   1                             /* start_end_main_source_file */
5650 };
5651 \f
5652 /* NOTE: In the comments in this file, many references are made to
5653    "Debugging Information Entries".  This term is abbreviated as `DIE'
5654    throughout the remainder of this file.  */
5655
5656 /* An internal representation of the DWARF output is built, and then
5657    walked to generate the DWARF debugging info.  The walk of the internal
5658    representation is done after the entire program has been compiled.
5659    The types below are used to describe the internal representation.  */
5660
5661 /* Various DIE's use offsets relative to the beginning of the
5662    .debug_info section to refer to each other.  */
5663
5664 typedef long int dw_offset;
5665
5666 /* Define typedefs here to avoid circular dependencies.  */
5667
5668 typedef struct dw_attr_struct *dw_attr_ref;
5669 typedef struct dw_line_info_struct *dw_line_info_ref;
5670 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5671 typedef struct pubname_struct *pubname_ref;
5672 typedef struct dw_ranges_struct *dw_ranges_ref;
5673 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5674 typedef struct comdat_type_struct *comdat_type_node_ref;
5675
5676 /* Each entry in the line_info_table maintains the file and
5677    line number associated with the label generated for that
5678    entry.  The label gives the PC value associated with
5679    the line number entry.  */
5680
5681 typedef struct GTY(()) dw_line_info_struct {
5682   unsigned long dw_file_num;
5683   unsigned long dw_line_num;
5684 }
5685 dw_line_info_entry;
5686
5687 /* Line information for functions in separate sections; each one gets its
5688    own sequence.  */
5689 typedef struct GTY(()) dw_separate_line_info_struct {
5690   unsigned long dw_file_num;
5691   unsigned long dw_line_num;
5692   unsigned long function;
5693 }
5694 dw_separate_line_info_entry;
5695
5696 /* Each DIE attribute has a field specifying the attribute kind,
5697    a link to the next attribute in the chain, and an attribute value.
5698    Attributes are typically linked below the DIE they modify.  */
5699
5700 typedef struct GTY(()) dw_attr_struct {
5701   enum dwarf_attribute dw_attr;
5702   dw_val_node dw_attr_val;
5703 }
5704 dw_attr_node;
5705
5706 DEF_VEC_O(dw_attr_node);
5707 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5708
5709 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5710    The children of each node form a circular list linked by
5711    die_sib.  die_child points to the node *before* the "first" child node.  */
5712
5713 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5714   union die_symbol_or_type_node
5715     {
5716       char * GTY ((tag ("0"))) die_symbol;
5717       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5718     }
5719   GTY ((desc ("dwarf_version >= 4"))) die_id;
5720   VEC(dw_attr_node,gc) * die_attr;
5721   dw_die_ref die_parent;
5722   dw_die_ref die_child;
5723   dw_die_ref die_sib;
5724   dw_die_ref die_definition; /* ref from a specification to its definition */
5725   dw_offset die_offset;
5726   unsigned long die_abbrev;
5727   int die_mark;
5728   /* Die is used and must not be pruned as unused.  */
5729   int die_perennial_p;
5730   unsigned int decl_id;
5731   enum dwarf_tag die_tag;
5732 }
5733 die_node;
5734
5735 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5736 #define FOR_EACH_CHILD(die, c, expr) do {       \
5737   c = die->die_child;                           \
5738   if (c) do {                                   \
5739     c = c->die_sib;                             \
5740     expr;                                       \
5741   } while (c != die->die_child);                \
5742 } while (0)
5743
5744 /* The pubname structure */
5745
5746 typedef struct GTY(()) pubname_struct {
5747   dw_die_ref die;
5748   const char *name;
5749 }
5750 pubname_entry;
5751
5752 DEF_VEC_O(pubname_entry);
5753 DEF_VEC_ALLOC_O(pubname_entry, gc);
5754
5755 struct GTY(()) dw_ranges_struct {
5756   /* If this is positive, it's a block number, otherwise it's a
5757      bitwise-negated index into dw_ranges_by_label.  */
5758   int num;
5759 };
5760
5761 struct GTY(()) dw_ranges_by_label_struct {
5762   const char *begin;
5763   const char *end;
5764 };
5765
5766 /* The comdat type node structure.  */
5767 typedef struct GTY(()) comdat_type_struct
5768 {
5769   dw_die_ref root_die;
5770   dw_die_ref type_die;
5771   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5772   struct comdat_type_struct *next;
5773 }
5774 comdat_type_node;
5775
5776 /* The limbo die list structure.  */
5777 typedef struct GTY(()) limbo_die_struct {
5778   dw_die_ref die;
5779   tree created_for;
5780   struct limbo_die_struct *next;
5781 }
5782 limbo_die_node;
5783
5784 typedef struct GTY(()) skeleton_chain_struct
5785 {
5786   dw_die_ref old_die;
5787   dw_die_ref new_die;
5788   struct skeleton_chain_struct *parent;
5789 }
5790 skeleton_chain_node;
5791
5792 /* How to start an assembler comment.  */
5793 #ifndef ASM_COMMENT_START
5794 #define ASM_COMMENT_START ";#"
5795 #endif
5796
5797 /* Define a macro which returns nonzero for a TYPE_DECL which was
5798    implicitly generated for a tagged type.
5799
5800    Note that unlike the gcc front end (which generates a NULL named
5801    TYPE_DECL node for each complete tagged type, each array type, and
5802    each function type node created) the g++ front end generates a
5803    _named_ TYPE_DECL node for each tagged type node created.
5804    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5805    generate a DW_TAG_typedef DIE for them.  */
5806
5807 #define TYPE_DECL_IS_STUB(decl)                         \
5808   (DECL_NAME (decl) == NULL_TREE                        \
5809    || (DECL_ARTIFICIAL (decl)                           \
5810        && is_tagged_type (TREE_TYPE (decl))             \
5811        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5812            /* This is necessary for stub decls that     \
5813               appear in nested inline functions.  */    \
5814            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5815                && (decl_ultimate_origin (decl)          \
5816                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5817
5818 /* Information concerning the compilation unit's programming
5819    language, and compiler version.  */
5820
5821 /* Fixed size portion of the DWARF compilation unit header.  */
5822 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5823   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5824
5825 /* Fixed size portion of the DWARF comdat type unit header.  */
5826 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5827   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5828    + DWARF_OFFSET_SIZE)
5829
5830 /* Fixed size portion of public names info.  */
5831 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5832
5833 /* Fixed size portion of the address range info.  */
5834 #define DWARF_ARANGES_HEADER_SIZE                                       \
5835   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5836                 DWARF2_ADDR_SIZE * 2)                                   \
5837    - DWARF_INITIAL_LENGTH_SIZE)
5838
5839 /* Size of padding portion in the address range info.  It must be
5840    aligned to twice the pointer size.  */
5841 #define DWARF_ARANGES_PAD_SIZE \
5842   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5843                 DWARF2_ADDR_SIZE * 2)                              \
5844    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5845
5846 /* Use assembler line directives if available.  */
5847 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5848 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5849 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5850 #else
5851 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5852 #endif
5853 #endif
5854
5855 /* Minimum line offset in a special line info. opcode.
5856    This value was chosen to give a reasonable range of values.  */
5857 #define DWARF_LINE_BASE  -10
5858
5859 /* First special line opcode - leave room for the standard opcodes.  */
5860 #define DWARF_LINE_OPCODE_BASE  10
5861
5862 /* Range of line offsets in a special line info. opcode.  */
5863 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5864
5865 /* Flag that indicates the initial value of the is_stmt_start flag.
5866    In the present implementation, we do not mark any lines as
5867    the beginning of a source statement, because that information
5868    is not made available by the GCC front-end.  */
5869 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5870
5871 /* Maximum number of operations per instruction bundle.  */
5872 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5873 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5874 #endif
5875
5876 /* This location is used by calc_die_sizes() to keep track
5877    the offset of each DIE within the .debug_info section.  */
5878 static unsigned long next_die_offset;
5879
5880 /* Record the root of the DIE's built for the current compilation unit.  */
5881 static GTY(()) dw_die_ref single_comp_unit_die;
5882
5883 /* A list of type DIEs that have been separated into comdat sections.  */
5884 static GTY(()) comdat_type_node *comdat_type_list;
5885
5886 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5887 static GTY(()) limbo_die_node *limbo_die_list;
5888
5889 /* A list of DIEs for which we may have to generate
5890    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5891 static GTY(()) limbo_die_node *deferred_asm_name;
5892
5893 /* Filenames referenced by this compilation unit.  */
5894 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5895
5896 /* A hash table of references to DIE's that describe declarations.
5897    The key is a DECL_UID() which is a unique number identifying each decl.  */
5898 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5899
5900 /* A hash table of references to DIE's that describe COMMON blocks.
5901    The key is DECL_UID() ^ die_parent.  */
5902 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5903
5904 typedef struct GTY(()) die_arg_entry_struct {
5905     dw_die_ref die;
5906     tree arg;
5907 } die_arg_entry;
5908
5909 DEF_VEC_O(die_arg_entry);
5910 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5911
5912 /* Node of the variable location list.  */
5913 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5914   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5915      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5916      in mode of the EXPR_LIST node and first EXPR_LIST operand
5917      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5918      location or NULL for padding.  For larger bitsizes,
5919      mode is 0 and first operand is a CONCAT with bitsize
5920      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5921      NULL as second operand.  */
5922   rtx GTY (()) loc;
5923   const char * GTY (()) label;
5924   struct var_loc_node * GTY (()) next;
5925 };
5926
5927 /* Variable location list.  */
5928 struct GTY (()) var_loc_list_def {
5929   struct var_loc_node * GTY (()) first;
5930
5931   /* Pointer to the last but one or last element of the
5932      chained list.  If the list is empty, both first and
5933      last are NULL, if the list contains just one node
5934      or the last node certainly is not redundant, it points
5935      to the last node, otherwise points to the last but one.
5936      Do not mark it for GC because it is marked through the chain.  */
5937   struct var_loc_node * GTY ((skip ("%h"))) last;
5938
5939   /* DECL_UID of the variable decl.  */
5940   unsigned int decl_id;
5941 };
5942 typedef struct var_loc_list_def var_loc_list;
5943
5944
5945 /* Table of decl location linked lists.  */
5946 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5947
5948 /* A pointer to the base of a list of references to DIE's that
5949    are uniquely identified by their tag, presence/absence of
5950    children DIE's, and list of attribute/value pairs.  */
5951 static GTY((length ("abbrev_die_table_allocated")))
5952   dw_die_ref *abbrev_die_table;
5953
5954 /* Number of elements currently allocated for abbrev_die_table.  */
5955 static GTY(()) unsigned abbrev_die_table_allocated;
5956
5957 /* Number of elements in type_die_table currently in use.  */
5958 static GTY(()) unsigned abbrev_die_table_in_use;
5959
5960 /* Size (in elements) of increments by which we may expand the
5961    abbrev_die_table.  */
5962 #define ABBREV_DIE_TABLE_INCREMENT 256
5963
5964 /* A pointer to the base of a table that contains line information
5965    for each source code line in .text in the compilation unit.  */
5966 static GTY((length ("line_info_table_allocated")))
5967      dw_line_info_ref line_info_table;
5968
5969 /* Number of elements currently allocated for line_info_table.  */
5970 static GTY(()) unsigned line_info_table_allocated;
5971
5972 /* Number of elements in line_info_table currently in use.  */
5973 static GTY(()) unsigned line_info_table_in_use;
5974
5975 /* A pointer to the base of a table that contains line information
5976    for each source code line outside of .text in the compilation unit.  */
5977 static GTY ((length ("separate_line_info_table_allocated")))
5978      dw_separate_line_info_ref separate_line_info_table;
5979
5980 /* Number of elements currently allocated for separate_line_info_table.  */
5981 static GTY(()) unsigned separate_line_info_table_allocated;
5982
5983 /* Number of elements in separate_line_info_table currently in use.  */
5984 static GTY(()) unsigned separate_line_info_table_in_use;
5985
5986 /* Size (in elements) of increments by which we may expand the
5987    line_info_table.  */
5988 #define LINE_INFO_TABLE_INCREMENT 1024
5989
5990 /* A pointer to the base of a table that contains a list of publicly
5991    accessible names.  */
5992 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5993
5994 /* A pointer to the base of a table that contains a list of publicly
5995    accessible types.  */
5996 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5997
5998 /* Array of dies for which we should generate .debug_arange info.  */
5999 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6000
6001 /* Number of elements currently allocated for arange_table.  */
6002 static GTY(()) unsigned arange_table_allocated;
6003
6004 /* Number of elements in arange_table currently in use.  */
6005 static GTY(()) unsigned arange_table_in_use;
6006
6007 /* Size (in elements) of increments by which we may expand the
6008    arange_table.  */
6009 #define ARANGE_TABLE_INCREMENT 64
6010
6011 /* Array of dies for which we should generate .debug_ranges info.  */
6012 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6013
6014 /* Number of elements currently allocated for ranges_table.  */
6015 static GTY(()) unsigned ranges_table_allocated;
6016
6017 /* Number of elements in ranges_table currently in use.  */
6018 static GTY(()) unsigned ranges_table_in_use;
6019
6020 /* Array of pairs of labels referenced in ranges_table.  */
6021 static GTY ((length ("ranges_by_label_allocated")))
6022      dw_ranges_by_label_ref ranges_by_label;
6023
6024 /* Number of elements currently allocated for ranges_by_label.  */
6025 static GTY(()) unsigned ranges_by_label_allocated;
6026
6027 /* Number of elements in ranges_by_label currently in use.  */
6028 static GTY(()) unsigned ranges_by_label_in_use;
6029
6030 /* Size (in elements) of increments by which we may expand the
6031    ranges_table.  */
6032 #define RANGES_TABLE_INCREMENT 64
6033
6034 /* Whether we have location lists that need outputting */
6035 static GTY(()) bool have_location_lists;
6036
6037 /* Unique label counter.  */
6038 static GTY(()) unsigned int loclabel_num;
6039
6040 /* Unique label counter for point-of-call tables.  */
6041 static GTY(()) unsigned int poc_label_num;
6042
6043 /* The direct call table structure.  */
6044
6045 typedef struct GTY(()) dcall_struct {
6046   unsigned int poc_label_num;
6047   tree poc_decl;
6048   dw_die_ref targ_die;
6049 }
6050 dcall_entry;
6051
6052 DEF_VEC_O(dcall_entry);
6053 DEF_VEC_ALLOC_O(dcall_entry, gc);
6054
6055 /* The virtual call table structure.  */
6056
6057 typedef struct GTY(()) vcall_struct {
6058   unsigned int poc_label_num;
6059   unsigned int vtable_slot;
6060 }
6061 vcall_entry;
6062
6063 DEF_VEC_O(vcall_entry);
6064 DEF_VEC_ALLOC_O(vcall_entry, gc);
6065
6066 /* Pointers to the direct and virtual call tables.  */
6067 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6068 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6069
6070 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6071
6072 struct GTY (()) vcall_insn {
6073   int insn_uid;
6074   unsigned int vtable_slot;
6075 };
6076
6077 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6078
6079 /* Record whether the function being analyzed contains inlined functions.  */
6080 static int current_function_has_inlines;
6081
6082 /* The last file entry emitted by maybe_emit_file().  */
6083 static GTY(()) struct dwarf_file_data * last_emitted_file;
6084
6085 /* Number of internal labels generated by gen_internal_sym().  */
6086 static GTY(()) int label_num;
6087
6088 /* Cached result of previous call to lookup_filename.  */
6089 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6090
6091 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6092
6093 /* Offset from the "steady-state frame pointer" to the frame base,
6094    within the current function.  */
6095 static HOST_WIDE_INT frame_pointer_fb_offset;
6096
6097 /* Forward declarations for functions defined in this file.  */
6098
6099 static int is_pseudo_reg (const_rtx);
6100 static tree type_main_variant (tree);
6101 static int is_tagged_type (const_tree);
6102 static const char *dwarf_tag_name (unsigned);
6103 static const char *dwarf_attr_name (unsigned);
6104 static const char *dwarf_form_name (unsigned);
6105 static tree decl_ultimate_origin (const_tree);
6106 static tree decl_class_context (tree);
6107 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6108 static inline enum dw_val_class AT_class (dw_attr_ref);
6109 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6110 static inline unsigned AT_flag (dw_attr_ref);
6111 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6112 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6113 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6114 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6115 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6116                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6117 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6118                                unsigned int, unsigned char *);
6119 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6120 static hashval_t debug_str_do_hash (const void *);
6121 static int debug_str_eq (const void *, const void *);
6122 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6123 static inline const char *AT_string (dw_attr_ref);
6124 static enum dwarf_form AT_string_form (dw_attr_ref);
6125 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6126 static void add_AT_specification (dw_die_ref, dw_die_ref);
6127 static inline dw_die_ref AT_ref (dw_attr_ref);
6128 static inline int AT_ref_external (dw_attr_ref);
6129 static inline void set_AT_ref_external (dw_attr_ref, int);
6130 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6131 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6132 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6133 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6134                              dw_loc_list_ref);
6135 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6136 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6137 static inline rtx AT_addr (dw_attr_ref);
6138 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6139 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6140 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6141 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6142                            unsigned HOST_WIDE_INT);
6143 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6144                                unsigned long);
6145 static inline const char *AT_lbl (dw_attr_ref);
6146 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6147 static const char *get_AT_low_pc (dw_die_ref);
6148 static const char *get_AT_hi_pc (dw_die_ref);
6149 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6150 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6151 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6152 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6153 static bool is_cxx (void);
6154 static bool is_fortran (void);
6155 static bool is_ada (void);
6156 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6157 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6158 static void add_child_die (dw_die_ref, dw_die_ref);
6159 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6160 static dw_die_ref lookup_type_die (tree);
6161 static void equate_type_number_to_die (tree, dw_die_ref);
6162 static hashval_t decl_die_table_hash (const void *);
6163 static int decl_die_table_eq (const void *, const void *);
6164 static dw_die_ref lookup_decl_die (tree);
6165 static hashval_t common_block_die_table_hash (const void *);
6166 static int common_block_die_table_eq (const void *, const void *);
6167 static hashval_t decl_loc_table_hash (const void *);
6168 static int decl_loc_table_eq (const void *, const void *);
6169 static var_loc_list *lookup_decl_loc (const_tree);
6170 static void equate_decl_number_to_die (tree, dw_die_ref);
6171 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6172 static void print_spaces (FILE *);
6173 static void print_die (dw_die_ref, FILE *);
6174 static void print_dwarf_line_table (FILE *);
6175 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6176 static dw_die_ref pop_compile_unit (dw_die_ref);
6177 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6178 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6179 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6180 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6181 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6182 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6183 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6184                                    struct md5_ctx *, int *);
6185 struct checksum_attributes;
6186 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6187 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6188 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6189 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6190 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6191 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6192 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6193 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6194 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6195 static void compute_section_prefix (dw_die_ref);
6196 static int is_type_die (dw_die_ref);
6197 static int is_comdat_die (dw_die_ref);
6198 static int is_symbol_die (dw_die_ref);
6199 static void assign_symbol_names (dw_die_ref);
6200 static void break_out_includes (dw_die_ref);
6201 static int is_declaration_die (dw_die_ref);
6202 static int should_move_die_to_comdat (dw_die_ref);
6203 static dw_die_ref clone_as_declaration (dw_die_ref);
6204 static dw_die_ref clone_die (dw_die_ref);
6205 static dw_die_ref clone_tree (dw_die_ref);
6206 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6207 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6208 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6209 static dw_die_ref generate_skeleton (dw_die_ref);
6210 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6211                                                          dw_die_ref);
6212 static void break_out_comdat_types (dw_die_ref);
6213 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6214 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6215 static void copy_decls_for_unworthy_types (dw_die_ref);
6216
6217 static hashval_t htab_cu_hash (const void *);
6218 static int htab_cu_eq (const void *, const void *);
6219 static void htab_cu_del (void *);
6220 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6221 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6222 static void add_sibling_attributes (dw_die_ref);
6223 static void build_abbrev_table (dw_die_ref);
6224 static void output_location_lists (dw_die_ref);
6225 static int constant_size (unsigned HOST_WIDE_INT);
6226 static unsigned long size_of_die (dw_die_ref);
6227 static void calc_die_sizes (dw_die_ref);
6228 static void mark_dies (dw_die_ref);
6229 static void unmark_dies (dw_die_ref);
6230 static void unmark_all_dies (dw_die_ref);
6231 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6232 static unsigned long size_of_aranges (void);
6233 static enum dwarf_form value_format (dw_attr_ref);
6234 static void output_value_format (dw_attr_ref);
6235 static void output_abbrev_section (void);
6236 static void output_die_symbol (dw_die_ref);
6237 static void output_die (dw_die_ref);
6238 static void output_compilation_unit_header (void);
6239 static void output_comp_unit (dw_die_ref, int);
6240 static void output_comdat_type_unit (comdat_type_node *);
6241 static const char *dwarf2_name (tree, int);
6242 static void add_pubname (tree, dw_die_ref);
6243 static void add_pubname_string (const char *, dw_die_ref);
6244 static void add_pubtype (tree, dw_die_ref);
6245 static void output_pubnames (VEC (pubname_entry,gc) *);
6246 static void add_arange (tree, dw_die_ref);
6247 static void output_aranges (void);
6248 static unsigned int add_ranges_num (int);
6249 static unsigned int add_ranges (const_tree);
6250 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6251                                   bool *);
6252 static void output_ranges (void);
6253 static void output_line_info (void);
6254 static void output_file_names (void);
6255 static dw_die_ref base_type_die (tree);
6256 static int is_base_type (tree);
6257 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6258 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6259 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6260 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6261 static int type_is_enum (const_tree);
6262 static unsigned int dbx_reg_number (const_rtx);
6263 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6264 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6265 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6266                                                 enum var_init_status);
6267 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6268                                                      enum var_init_status);
6269 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6270                                          enum var_init_status);
6271 static int is_based_loc (const_rtx);
6272 static int resolve_one_addr (rtx *, void *);
6273 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6274                                                enum var_init_status);
6275 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6276                                         enum var_init_status);
6277 static dw_loc_list_ref loc_list_from_tree (tree, int);
6278 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6279 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6280 static tree field_type (const_tree);
6281 static unsigned int simple_type_align_in_bits (const_tree);
6282 static unsigned int simple_decl_align_in_bits (const_tree);
6283 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6284 static HOST_WIDE_INT field_byte_offset (const_tree);
6285 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6286                                          dw_loc_list_ref);
6287 static void add_data_member_location_attribute (dw_die_ref, tree);
6288 static bool add_const_value_attribute (dw_die_ref, rtx);
6289 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6290 static void insert_double (double_int, unsigned char *);
6291 static void insert_float (const_rtx, unsigned char *);
6292 static rtx rtl_for_decl_location (tree);
6293 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6294                                                    enum dwarf_attribute);
6295 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6296 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6297 static void add_name_attribute (dw_die_ref, const char *);
6298 static void add_comp_dir_attribute (dw_die_ref);
6299 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6300 static void add_subscript_info (dw_die_ref, tree, bool);
6301 static void add_byte_size_attribute (dw_die_ref, tree);
6302 static void add_bit_offset_attribute (dw_die_ref, tree);
6303 static void add_bit_size_attribute (dw_die_ref, tree);
6304 static void add_prototyped_attribute (dw_die_ref, tree);
6305 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6306 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6307 static void add_src_coords_attributes (dw_die_ref, tree);
6308 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6309 static void push_decl_scope (tree);
6310 static void pop_decl_scope (void);
6311 static dw_die_ref scope_die_for (tree, dw_die_ref);
6312 static inline int local_scope_p (dw_die_ref);
6313 static inline int class_scope_p (dw_die_ref);
6314 static inline int class_or_namespace_scope_p (dw_die_ref);
6315 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6316 static void add_calling_convention_attribute (dw_die_ref, tree);
6317 static const char *type_tag (const_tree);
6318 static tree member_declared_type (const_tree);
6319 #if 0
6320 static const char *decl_start_label (tree);
6321 #endif
6322 static void gen_array_type_die (tree, dw_die_ref);
6323 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6324 #if 0
6325 static void gen_entry_point_die (tree, dw_die_ref);
6326 #endif
6327 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6328 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6329 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6330 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6331 static void gen_formal_types_die (tree, dw_die_ref);
6332 static void gen_subprogram_die (tree, dw_die_ref);
6333 static void gen_variable_die (tree, tree, dw_die_ref);
6334 static void gen_const_die (tree, dw_die_ref);
6335 static void gen_label_die (tree, dw_die_ref);
6336 static void gen_lexical_block_die (tree, dw_die_ref, int);
6337 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6338 static void gen_field_die (tree, dw_die_ref);
6339 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6340 static dw_die_ref gen_compile_unit_die (const char *);
6341 static void gen_inheritance_die (tree, tree, dw_die_ref);
6342 static void gen_member_die (tree, dw_die_ref);
6343 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6344                                                 enum debug_info_usage);
6345 static void gen_subroutine_type_die (tree, dw_die_ref);
6346 static void gen_typedef_die (tree, dw_die_ref);
6347 static void gen_type_die (tree, dw_die_ref);
6348 static void gen_block_die (tree, dw_die_ref, int);
6349 static void decls_for_scope (tree, dw_die_ref, int);
6350 static int is_redundant_typedef (const_tree);
6351 static bool is_naming_typedef_decl (const_tree);
6352 static inline dw_die_ref get_context_die (tree);
6353 static void gen_namespace_die (tree, dw_die_ref);
6354 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6355 static dw_die_ref force_decl_die (tree);
6356 static dw_die_ref force_type_die (tree);
6357 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6358 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6359 static struct dwarf_file_data * lookup_filename (const char *);
6360 static void retry_incomplete_types (void);
6361 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6362 static void gen_generic_params_dies (tree);
6363 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6364 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6365 static void splice_child_die (dw_die_ref, dw_die_ref);
6366 static int file_info_cmp (const void *, const void *);
6367 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6368                                      const char *, const char *);
6369 static void output_loc_list (dw_loc_list_ref);
6370 static char *gen_internal_sym (const char *);
6371
6372 static void prune_unmark_dies (dw_die_ref);
6373 static void prune_unused_types_mark (dw_die_ref, int);
6374 static void prune_unused_types_walk (dw_die_ref);
6375 static void prune_unused_types_walk_attribs (dw_die_ref);
6376 static void prune_unused_types_prune (dw_die_ref);
6377 static void prune_unused_types (void);
6378 static int maybe_emit_file (struct dwarf_file_data *fd);
6379 static inline const char *AT_vms_delta1 (dw_attr_ref);
6380 static inline const char *AT_vms_delta2 (dw_attr_ref);
6381 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6382                                      const char *, const char *);
6383 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6384 static void gen_remaining_tmpl_value_param_die_attribute (void);
6385
6386 /* Section names used to hold DWARF debugging information.  */
6387 #ifndef DEBUG_INFO_SECTION
6388 #define DEBUG_INFO_SECTION      ".debug_info"
6389 #endif
6390 #ifndef DEBUG_ABBREV_SECTION
6391 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6392 #endif
6393 #ifndef DEBUG_ARANGES_SECTION
6394 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6395 #endif
6396 #ifndef DEBUG_MACINFO_SECTION
6397 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6398 #endif
6399 #ifndef DEBUG_LINE_SECTION
6400 #define DEBUG_LINE_SECTION      ".debug_line"
6401 #endif
6402 #ifndef DEBUG_LOC_SECTION
6403 #define DEBUG_LOC_SECTION       ".debug_loc"
6404 #endif
6405 #ifndef DEBUG_PUBNAMES_SECTION
6406 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6407 #endif
6408 #ifndef DEBUG_PUBTYPES_SECTION
6409 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6410 #endif
6411 #ifndef DEBUG_DCALL_SECTION
6412 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6413 #endif
6414 #ifndef DEBUG_VCALL_SECTION
6415 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6416 #endif
6417 #ifndef DEBUG_STR_SECTION
6418 #define DEBUG_STR_SECTION       ".debug_str"
6419 #endif
6420 #ifndef DEBUG_RANGES_SECTION
6421 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6422 #endif
6423
6424 /* Standard ELF section names for compiled code and data.  */
6425 #ifndef TEXT_SECTION_NAME
6426 #define TEXT_SECTION_NAME       ".text"
6427 #endif
6428
6429 /* Section flags for .debug_str section.  */
6430 #define DEBUG_STR_SECTION_FLAGS \
6431   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6432    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6433    : SECTION_DEBUG)
6434
6435 /* Labels we insert at beginning sections we can reference instead of
6436    the section names themselves.  */
6437
6438 #ifndef TEXT_SECTION_LABEL
6439 #define TEXT_SECTION_LABEL              "Ltext"
6440 #endif
6441 #ifndef COLD_TEXT_SECTION_LABEL
6442 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6443 #endif
6444 #ifndef DEBUG_LINE_SECTION_LABEL
6445 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6446 #endif
6447 #ifndef DEBUG_INFO_SECTION_LABEL
6448 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6449 #endif
6450 #ifndef DEBUG_ABBREV_SECTION_LABEL
6451 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6452 #endif
6453 #ifndef DEBUG_LOC_SECTION_LABEL
6454 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6455 #endif
6456 #ifndef DEBUG_RANGES_SECTION_LABEL
6457 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6458 #endif
6459 #ifndef DEBUG_MACINFO_SECTION_LABEL
6460 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6461 #endif
6462
6463
6464 /* Definitions of defaults for formats and names of various special
6465    (artificial) labels which may be generated within this file (when the -g
6466    options is used and DWARF2_DEBUGGING_INFO is in effect.
6467    If necessary, these may be overridden from within the tm.h file, but
6468    typically, overriding these defaults is unnecessary.  */
6469
6470 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6471 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6472 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6473 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6474 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6475 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6476 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6477 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6478 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6479 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6480
6481 #ifndef TEXT_END_LABEL
6482 #define TEXT_END_LABEL          "Letext"
6483 #endif
6484 #ifndef COLD_END_LABEL
6485 #define COLD_END_LABEL          "Letext_cold"
6486 #endif
6487 #ifndef BLOCK_BEGIN_LABEL
6488 #define BLOCK_BEGIN_LABEL       "LBB"
6489 #endif
6490 #ifndef BLOCK_END_LABEL
6491 #define BLOCK_END_LABEL         "LBE"
6492 #endif
6493 #ifndef LINE_CODE_LABEL
6494 #define LINE_CODE_LABEL         "LM"
6495 #endif
6496 #ifndef SEPARATE_LINE_CODE_LABEL
6497 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6498 #endif
6499
6500 \f
6501 /* Return the root of the DIE's built for the current compilation unit.  */
6502 static dw_die_ref
6503 comp_unit_die (void)
6504 {
6505   if (!single_comp_unit_die)
6506     single_comp_unit_die = gen_compile_unit_die (NULL);
6507   return single_comp_unit_die;
6508 }
6509
6510 /* We allow a language front-end to designate a function that is to be
6511    called to "demangle" any name before it is put into a DIE.  */
6512
6513 static const char *(*demangle_name_func) (const char *);
6514
6515 void
6516 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6517 {
6518   demangle_name_func = func;
6519 }
6520
6521 /* Test if rtl node points to a pseudo register.  */
6522
6523 static inline int
6524 is_pseudo_reg (const_rtx rtl)
6525 {
6526   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6527           || (GET_CODE (rtl) == SUBREG
6528               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6529 }
6530
6531 /* Return a reference to a type, with its const and volatile qualifiers
6532    removed.  */
6533
6534 static inline tree
6535 type_main_variant (tree type)
6536 {
6537   type = TYPE_MAIN_VARIANT (type);
6538
6539   /* ??? There really should be only one main variant among any group of
6540      variants of a given type (and all of the MAIN_VARIANT values for all
6541      members of the group should point to that one type) but sometimes the C
6542      front-end messes this up for array types, so we work around that bug
6543      here.  */
6544   if (TREE_CODE (type) == ARRAY_TYPE)
6545     while (type != TYPE_MAIN_VARIANT (type))
6546       type = TYPE_MAIN_VARIANT (type);
6547
6548   return type;
6549 }
6550
6551 /* Return nonzero if the given type node represents a tagged type.  */
6552
6553 static inline int
6554 is_tagged_type (const_tree type)
6555 {
6556   enum tree_code code = TREE_CODE (type);
6557
6558   return (code == RECORD_TYPE || code == UNION_TYPE
6559           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6560 }
6561
6562 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6563
6564 static void
6565 get_ref_die_offset_label (char *label, dw_die_ref ref)
6566 {
6567   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6568 }
6569
6570 /* Convert a DIE tag into its string name.  */
6571
6572 static const char *
6573 dwarf_tag_name (unsigned int tag)
6574 {
6575   switch (tag)
6576     {
6577     case DW_TAG_padding:
6578       return "DW_TAG_padding";
6579     case DW_TAG_array_type:
6580       return "DW_TAG_array_type";
6581     case DW_TAG_class_type:
6582       return "DW_TAG_class_type";
6583     case DW_TAG_entry_point:
6584       return "DW_TAG_entry_point";
6585     case DW_TAG_enumeration_type:
6586       return "DW_TAG_enumeration_type";
6587     case DW_TAG_formal_parameter:
6588       return "DW_TAG_formal_parameter";
6589     case DW_TAG_imported_declaration:
6590       return "DW_TAG_imported_declaration";
6591     case DW_TAG_label:
6592       return "DW_TAG_label";
6593     case DW_TAG_lexical_block:
6594       return "DW_TAG_lexical_block";
6595     case DW_TAG_member:
6596       return "DW_TAG_member";
6597     case DW_TAG_pointer_type:
6598       return "DW_TAG_pointer_type";
6599     case DW_TAG_reference_type:
6600       return "DW_TAG_reference_type";
6601     case DW_TAG_compile_unit:
6602       return "DW_TAG_compile_unit";
6603     case DW_TAG_string_type:
6604       return "DW_TAG_string_type";
6605     case DW_TAG_structure_type:
6606       return "DW_TAG_structure_type";
6607     case DW_TAG_subroutine_type:
6608       return "DW_TAG_subroutine_type";
6609     case DW_TAG_typedef:
6610       return "DW_TAG_typedef";
6611     case DW_TAG_union_type:
6612       return "DW_TAG_union_type";
6613     case DW_TAG_unspecified_parameters:
6614       return "DW_TAG_unspecified_parameters";
6615     case DW_TAG_variant:
6616       return "DW_TAG_variant";
6617     case DW_TAG_common_block:
6618       return "DW_TAG_common_block";
6619     case DW_TAG_common_inclusion:
6620       return "DW_TAG_common_inclusion";
6621     case DW_TAG_inheritance:
6622       return "DW_TAG_inheritance";
6623     case DW_TAG_inlined_subroutine:
6624       return "DW_TAG_inlined_subroutine";
6625     case DW_TAG_module:
6626       return "DW_TAG_module";
6627     case DW_TAG_ptr_to_member_type:
6628       return "DW_TAG_ptr_to_member_type";
6629     case DW_TAG_set_type:
6630       return "DW_TAG_set_type";
6631     case DW_TAG_subrange_type:
6632       return "DW_TAG_subrange_type";
6633     case DW_TAG_with_stmt:
6634       return "DW_TAG_with_stmt";
6635     case DW_TAG_access_declaration:
6636       return "DW_TAG_access_declaration";
6637     case DW_TAG_base_type:
6638       return "DW_TAG_base_type";
6639     case DW_TAG_catch_block:
6640       return "DW_TAG_catch_block";
6641     case DW_TAG_const_type:
6642       return "DW_TAG_const_type";
6643     case DW_TAG_constant:
6644       return "DW_TAG_constant";
6645     case DW_TAG_enumerator:
6646       return "DW_TAG_enumerator";
6647     case DW_TAG_file_type:
6648       return "DW_TAG_file_type";
6649     case DW_TAG_friend:
6650       return "DW_TAG_friend";
6651     case DW_TAG_namelist:
6652       return "DW_TAG_namelist";
6653     case DW_TAG_namelist_item:
6654       return "DW_TAG_namelist_item";
6655     case DW_TAG_packed_type:
6656       return "DW_TAG_packed_type";
6657     case DW_TAG_subprogram:
6658       return "DW_TAG_subprogram";
6659     case DW_TAG_template_type_param:
6660       return "DW_TAG_template_type_param";
6661     case DW_TAG_template_value_param:
6662       return "DW_TAG_template_value_param";
6663     case DW_TAG_thrown_type:
6664       return "DW_TAG_thrown_type";
6665     case DW_TAG_try_block:
6666       return "DW_TAG_try_block";
6667     case DW_TAG_variant_part:
6668       return "DW_TAG_variant_part";
6669     case DW_TAG_variable:
6670       return "DW_TAG_variable";
6671     case DW_TAG_volatile_type:
6672       return "DW_TAG_volatile_type";
6673     case DW_TAG_dwarf_procedure:
6674       return "DW_TAG_dwarf_procedure";
6675     case DW_TAG_restrict_type:
6676       return "DW_TAG_restrict_type";
6677     case DW_TAG_interface_type:
6678       return "DW_TAG_interface_type";
6679     case DW_TAG_namespace:
6680       return "DW_TAG_namespace";
6681     case DW_TAG_imported_module:
6682       return "DW_TAG_imported_module";
6683     case DW_TAG_unspecified_type:
6684       return "DW_TAG_unspecified_type";
6685     case DW_TAG_partial_unit:
6686       return "DW_TAG_partial_unit";
6687     case DW_TAG_imported_unit:
6688       return "DW_TAG_imported_unit";
6689     case DW_TAG_condition:
6690       return "DW_TAG_condition";
6691     case DW_TAG_shared_type:
6692       return "DW_TAG_shared_type";
6693     case DW_TAG_type_unit:
6694       return "DW_TAG_type_unit";
6695     case DW_TAG_rvalue_reference_type:
6696       return "DW_TAG_rvalue_reference_type";
6697     case DW_TAG_template_alias:
6698       return "DW_TAG_template_alias";
6699     case DW_TAG_GNU_template_parameter_pack:
6700       return "DW_TAG_GNU_template_parameter_pack";
6701     case DW_TAG_GNU_formal_parameter_pack:
6702       return "DW_TAG_GNU_formal_parameter_pack";
6703     case DW_TAG_MIPS_loop:
6704       return "DW_TAG_MIPS_loop";
6705     case DW_TAG_format_label:
6706       return "DW_TAG_format_label";
6707     case DW_TAG_function_template:
6708       return "DW_TAG_function_template";
6709     case DW_TAG_class_template:
6710       return "DW_TAG_class_template";
6711     case DW_TAG_GNU_BINCL:
6712       return "DW_TAG_GNU_BINCL";
6713     case DW_TAG_GNU_EINCL:
6714       return "DW_TAG_GNU_EINCL";
6715     case DW_TAG_GNU_template_template_param:
6716       return "DW_TAG_GNU_template_template_param";
6717     default:
6718       return "DW_TAG_<unknown>";
6719     }
6720 }
6721
6722 /* Convert a DWARF attribute code into its string name.  */
6723
6724 static const char *
6725 dwarf_attr_name (unsigned int attr)
6726 {
6727   switch (attr)
6728     {
6729     case DW_AT_sibling:
6730       return "DW_AT_sibling";
6731     case DW_AT_location:
6732       return "DW_AT_location";
6733     case DW_AT_name:
6734       return "DW_AT_name";
6735     case DW_AT_ordering:
6736       return "DW_AT_ordering";
6737     case DW_AT_subscr_data:
6738       return "DW_AT_subscr_data";
6739     case DW_AT_byte_size:
6740       return "DW_AT_byte_size";
6741     case DW_AT_bit_offset:
6742       return "DW_AT_bit_offset";
6743     case DW_AT_bit_size:
6744       return "DW_AT_bit_size";
6745     case DW_AT_element_list:
6746       return "DW_AT_element_list";
6747     case DW_AT_stmt_list:
6748       return "DW_AT_stmt_list";
6749     case DW_AT_low_pc:
6750       return "DW_AT_low_pc";
6751     case DW_AT_high_pc:
6752       return "DW_AT_high_pc";
6753     case DW_AT_language:
6754       return "DW_AT_language";
6755     case DW_AT_member:
6756       return "DW_AT_member";
6757     case DW_AT_discr:
6758       return "DW_AT_discr";
6759     case DW_AT_discr_value:
6760       return "DW_AT_discr_value";
6761     case DW_AT_visibility:
6762       return "DW_AT_visibility";
6763     case DW_AT_import:
6764       return "DW_AT_import";
6765     case DW_AT_string_length:
6766       return "DW_AT_string_length";
6767     case DW_AT_common_reference:
6768       return "DW_AT_common_reference";
6769     case DW_AT_comp_dir:
6770       return "DW_AT_comp_dir";
6771     case DW_AT_const_value:
6772       return "DW_AT_const_value";
6773     case DW_AT_containing_type:
6774       return "DW_AT_containing_type";
6775     case DW_AT_default_value:
6776       return "DW_AT_default_value";
6777     case DW_AT_inline:
6778       return "DW_AT_inline";
6779     case DW_AT_is_optional:
6780       return "DW_AT_is_optional";
6781     case DW_AT_lower_bound:
6782       return "DW_AT_lower_bound";
6783     case DW_AT_producer:
6784       return "DW_AT_producer";
6785     case DW_AT_prototyped:
6786       return "DW_AT_prototyped";
6787     case DW_AT_return_addr:
6788       return "DW_AT_return_addr";
6789     case DW_AT_start_scope:
6790       return "DW_AT_start_scope";
6791     case DW_AT_bit_stride:
6792       return "DW_AT_bit_stride";
6793     case DW_AT_upper_bound:
6794       return "DW_AT_upper_bound";
6795     case DW_AT_abstract_origin:
6796       return "DW_AT_abstract_origin";
6797     case DW_AT_accessibility:
6798       return "DW_AT_accessibility";
6799     case DW_AT_address_class:
6800       return "DW_AT_address_class";
6801     case DW_AT_artificial:
6802       return "DW_AT_artificial";
6803     case DW_AT_base_types:
6804       return "DW_AT_base_types";
6805     case DW_AT_calling_convention:
6806       return "DW_AT_calling_convention";
6807     case DW_AT_count:
6808       return "DW_AT_count";
6809     case DW_AT_data_member_location:
6810       return "DW_AT_data_member_location";
6811     case DW_AT_decl_column:
6812       return "DW_AT_decl_column";
6813     case DW_AT_decl_file:
6814       return "DW_AT_decl_file";
6815     case DW_AT_decl_line:
6816       return "DW_AT_decl_line";
6817     case DW_AT_declaration:
6818       return "DW_AT_declaration";
6819     case DW_AT_discr_list:
6820       return "DW_AT_discr_list";
6821     case DW_AT_encoding:
6822       return "DW_AT_encoding";
6823     case DW_AT_external:
6824       return "DW_AT_external";
6825     case DW_AT_explicit:
6826       return "DW_AT_explicit";
6827     case DW_AT_frame_base:
6828       return "DW_AT_frame_base";
6829     case DW_AT_friend:
6830       return "DW_AT_friend";
6831     case DW_AT_identifier_case:
6832       return "DW_AT_identifier_case";
6833     case DW_AT_macro_info:
6834       return "DW_AT_macro_info";
6835     case DW_AT_namelist_items:
6836       return "DW_AT_namelist_items";
6837     case DW_AT_priority:
6838       return "DW_AT_priority";
6839     case DW_AT_segment:
6840       return "DW_AT_segment";
6841     case DW_AT_specification:
6842       return "DW_AT_specification";
6843     case DW_AT_static_link:
6844       return "DW_AT_static_link";
6845     case DW_AT_type:
6846       return "DW_AT_type";
6847     case DW_AT_use_location:
6848       return "DW_AT_use_location";
6849     case DW_AT_variable_parameter:
6850       return "DW_AT_variable_parameter";
6851     case DW_AT_virtuality:
6852       return "DW_AT_virtuality";
6853     case DW_AT_vtable_elem_location:
6854       return "DW_AT_vtable_elem_location";
6855
6856     case DW_AT_allocated:
6857       return "DW_AT_allocated";
6858     case DW_AT_associated:
6859       return "DW_AT_associated";
6860     case DW_AT_data_location:
6861       return "DW_AT_data_location";
6862     case DW_AT_byte_stride:
6863       return "DW_AT_byte_stride";
6864     case DW_AT_entry_pc:
6865       return "DW_AT_entry_pc";
6866     case DW_AT_use_UTF8:
6867       return "DW_AT_use_UTF8";
6868     case DW_AT_extension:
6869       return "DW_AT_extension";
6870     case DW_AT_ranges:
6871       return "DW_AT_ranges";
6872     case DW_AT_trampoline:
6873       return "DW_AT_trampoline";
6874     case DW_AT_call_column:
6875       return "DW_AT_call_column";
6876     case DW_AT_call_file:
6877       return "DW_AT_call_file";
6878     case DW_AT_call_line:
6879       return "DW_AT_call_line";
6880     case DW_AT_object_pointer:
6881       return "DW_AT_object_pointer";
6882
6883     case DW_AT_signature:
6884       return "DW_AT_signature";
6885     case DW_AT_main_subprogram:
6886       return "DW_AT_main_subprogram";
6887     case DW_AT_data_bit_offset:
6888       return "DW_AT_data_bit_offset";
6889     case DW_AT_const_expr:
6890       return "DW_AT_const_expr";
6891     case DW_AT_enum_class:
6892       return "DW_AT_enum_class";
6893     case DW_AT_linkage_name:
6894       return "DW_AT_linkage_name";
6895
6896     case DW_AT_MIPS_fde:
6897       return "DW_AT_MIPS_fde";
6898     case DW_AT_MIPS_loop_begin:
6899       return "DW_AT_MIPS_loop_begin";
6900     case DW_AT_MIPS_tail_loop_begin:
6901       return "DW_AT_MIPS_tail_loop_begin";
6902     case DW_AT_MIPS_epilog_begin:
6903       return "DW_AT_MIPS_epilog_begin";
6904 #if VMS_DEBUGGING_INFO
6905     case DW_AT_HP_prologue:
6906       return "DW_AT_HP_prologue";
6907 #else
6908     case DW_AT_MIPS_loop_unroll_factor:
6909       return "DW_AT_MIPS_loop_unroll_factor";
6910 #endif
6911     case DW_AT_MIPS_software_pipeline_depth:
6912       return "DW_AT_MIPS_software_pipeline_depth";
6913     case DW_AT_MIPS_linkage_name:
6914       return "DW_AT_MIPS_linkage_name";
6915 #if VMS_DEBUGGING_INFO
6916     case DW_AT_HP_epilogue:
6917       return "DW_AT_HP_epilogue";
6918 #else
6919     case DW_AT_MIPS_stride:
6920       return "DW_AT_MIPS_stride";
6921 #endif
6922     case DW_AT_MIPS_abstract_name:
6923       return "DW_AT_MIPS_abstract_name";
6924     case DW_AT_MIPS_clone_origin:
6925       return "DW_AT_MIPS_clone_origin";
6926     case DW_AT_MIPS_has_inlines:
6927       return "DW_AT_MIPS_has_inlines";
6928
6929     case DW_AT_sf_names:
6930       return "DW_AT_sf_names";
6931     case DW_AT_src_info:
6932       return "DW_AT_src_info";
6933     case DW_AT_mac_info:
6934       return "DW_AT_mac_info";
6935     case DW_AT_src_coords:
6936       return "DW_AT_src_coords";
6937     case DW_AT_body_begin:
6938       return "DW_AT_body_begin";
6939     case DW_AT_body_end:
6940       return "DW_AT_body_end";
6941     case DW_AT_GNU_vector:
6942       return "DW_AT_GNU_vector";
6943     case DW_AT_GNU_guarded_by:
6944       return "DW_AT_GNU_guarded_by";
6945     case DW_AT_GNU_pt_guarded_by:
6946       return "DW_AT_GNU_pt_guarded_by";
6947     case DW_AT_GNU_guarded:
6948       return "DW_AT_GNU_guarded";
6949     case DW_AT_GNU_pt_guarded:
6950       return "DW_AT_GNU_pt_guarded";
6951     case DW_AT_GNU_locks_excluded:
6952       return "DW_AT_GNU_locks_excluded";
6953     case DW_AT_GNU_exclusive_locks_required:
6954       return "DW_AT_GNU_exclusive_locks_required";
6955     case DW_AT_GNU_shared_locks_required:
6956       return "DW_AT_GNU_shared_locks_required";
6957     case DW_AT_GNU_odr_signature:
6958       return "DW_AT_GNU_odr_signature";
6959     case DW_AT_GNU_template_name:
6960       return "DW_AT_GNU_template_name";
6961
6962     case DW_AT_VMS_rtnbeg_pd_address:
6963       return "DW_AT_VMS_rtnbeg_pd_address";
6964
6965     default:
6966       return "DW_AT_<unknown>";
6967     }
6968 }
6969
6970 /* Convert a DWARF value form code into its string name.  */
6971
6972 static const char *
6973 dwarf_form_name (unsigned int form)
6974 {
6975   switch (form)
6976     {
6977     case DW_FORM_addr:
6978       return "DW_FORM_addr";
6979     case DW_FORM_block2:
6980       return "DW_FORM_block2";
6981     case DW_FORM_block4:
6982       return "DW_FORM_block4";
6983     case DW_FORM_data2:
6984       return "DW_FORM_data2";
6985     case DW_FORM_data4:
6986       return "DW_FORM_data4";
6987     case DW_FORM_data8:
6988       return "DW_FORM_data8";
6989     case DW_FORM_string:
6990       return "DW_FORM_string";
6991     case DW_FORM_block:
6992       return "DW_FORM_block";
6993     case DW_FORM_block1:
6994       return "DW_FORM_block1";
6995     case DW_FORM_data1:
6996       return "DW_FORM_data1";
6997     case DW_FORM_flag:
6998       return "DW_FORM_flag";
6999     case DW_FORM_sdata:
7000       return "DW_FORM_sdata";
7001     case DW_FORM_strp:
7002       return "DW_FORM_strp";
7003     case DW_FORM_udata:
7004       return "DW_FORM_udata";
7005     case DW_FORM_ref_addr:
7006       return "DW_FORM_ref_addr";
7007     case DW_FORM_ref1:
7008       return "DW_FORM_ref1";
7009     case DW_FORM_ref2:
7010       return "DW_FORM_ref2";
7011     case DW_FORM_ref4:
7012       return "DW_FORM_ref4";
7013     case DW_FORM_ref8:
7014       return "DW_FORM_ref8";
7015     case DW_FORM_ref_udata:
7016       return "DW_FORM_ref_udata";
7017     case DW_FORM_indirect:
7018       return "DW_FORM_indirect";
7019     case DW_FORM_sec_offset:
7020       return "DW_FORM_sec_offset";
7021     case DW_FORM_exprloc:
7022       return "DW_FORM_exprloc";
7023     case DW_FORM_flag_present:
7024       return "DW_FORM_flag_present";
7025     case DW_FORM_ref_sig8:
7026       return "DW_FORM_ref_sig8";
7027     default:
7028       return "DW_FORM_<unknown>";
7029     }
7030 }
7031 \f
7032 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7033    instance of an inlined instance of a decl which is local to an inline
7034    function, so we have to trace all of the way back through the origin chain
7035    to find out what sort of node actually served as the original seed for the
7036    given block.  */
7037
7038 static tree
7039 decl_ultimate_origin (const_tree decl)
7040 {
7041   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7042     return NULL_TREE;
7043
7044   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7045      nodes in the function to point to themselves; ignore that if
7046      we're trying to output the abstract instance of this function.  */
7047   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7048     return NULL_TREE;
7049
7050   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7051      most distant ancestor, this should never happen.  */
7052   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7053
7054   return DECL_ABSTRACT_ORIGIN (decl);
7055 }
7056
7057 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7058    of a virtual function may refer to a base class, so we check the 'this'
7059    parameter.  */
7060
7061 static tree
7062 decl_class_context (tree decl)
7063 {
7064   tree context = NULL_TREE;
7065
7066   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7067     context = DECL_CONTEXT (decl);
7068   else
7069     context = TYPE_MAIN_VARIANT
7070       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7071
7072   if (context && !TYPE_P (context))
7073     context = NULL_TREE;
7074
7075   return context;
7076 }
7077 \f
7078 /* Add an attribute/value pair to a DIE.  */
7079
7080 static inline void
7081 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7082 {
7083   /* Maybe this should be an assert?  */
7084   if (die == NULL)
7085     return;
7086
7087   if (die->die_attr == NULL)
7088     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7089   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7090 }
7091
7092 static inline enum dw_val_class
7093 AT_class (dw_attr_ref a)
7094 {
7095   return a->dw_attr_val.val_class;
7096 }
7097
7098 /* Add a flag value attribute to a DIE.  */
7099
7100 static inline void
7101 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7102 {
7103   dw_attr_node attr;
7104
7105   attr.dw_attr = attr_kind;
7106   attr.dw_attr_val.val_class = dw_val_class_flag;
7107   attr.dw_attr_val.v.val_flag = flag;
7108   add_dwarf_attr (die, &attr);
7109 }
7110
7111 static inline unsigned
7112 AT_flag (dw_attr_ref a)
7113 {
7114   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7115   return a->dw_attr_val.v.val_flag;
7116 }
7117
7118 /* Add a signed integer attribute value to a DIE.  */
7119
7120 static inline void
7121 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7122 {
7123   dw_attr_node attr;
7124
7125   attr.dw_attr = attr_kind;
7126   attr.dw_attr_val.val_class = dw_val_class_const;
7127   attr.dw_attr_val.v.val_int = int_val;
7128   add_dwarf_attr (die, &attr);
7129 }
7130
7131 static inline HOST_WIDE_INT
7132 AT_int (dw_attr_ref a)
7133 {
7134   gcc_assert (a && AT_class (a) == dw_val_class_const);
7135   return a->dw_attr_val.v.val_int;
7136 }
7137
7138 /* Add an unsigned integer attribute value to a DIE.  */
7139
7140 static inline void
7141 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7142                  unsigned HOST_WIDE_INT unsigned_val)
7143 {
7144   dw_attr_node attr;
7145
7146   attr.dw_attr = attr_kind;
7147   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7148   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7149   add_dwarf_attr (die, &attr);
7150 }
7151
7152 static inline unsigned HOST_WIDE_INT
7153 AT_unsigned (dw_attr_ref a)
7154 {
7155   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7156   return a->dw_attr_val.v.val_unsigned;
7157 }
7158
7159 /* Add an unsigned double integer attribute value to a DIE.  */
7160
7161 static inline void
7162 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7163                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7164 {
7165   dw_attr_node attr;
7166
7167   attr.dw_attr = attr_kind;
7168   attr.dw_attr_val.val_class = dw_val_class_const_double;
7169   attr.dw_attr_val.v.val_double.high = high;
7170   attr.dw_attr_val.v.val_double.low = low;
7171   add_dwarf_attr (die, &attr);
7172 }
7173
7174 /* Add a floating point attribute value to a DIE and return it.  */
7175
7176 static inline void
7177 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7178             unsigned int length, unsigned int elt_size, unsigned char *array)
7179 {
7180   dw_attr_node attr;
7181
7182   attr.dw_attr = attr_kind;
7183   attr.dw_attr_val.val_class = dw_val_class_vec;
7184   attr.dw_attr_val.v.val_vec.length = length;
7185   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7186   attr.dw_attr_val.v.val_vec.array = array;
7187   add_dwarf_attr (die, &attr);
7188 }
7189
7190 /* Add an 8-byte data attribute value to a DIE.  */
7191
7192 static inline void
7193 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7194               unsigned char data8[8])
7195 {
7196   dw_attr_node attr;
7197
7198   attr.dw_attr = attr_kind;
7199   attr.dw_attr_val.val_class = dw_val_class_data8;
7200   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7201   add_dwarf_attr (die, &attr);
7202 }
7203
7204 /* Hash and equality functions for debug_str_hash.  */
7205
7206 static hashval_t
7207 debug_str_do_hash (const void *x)
7208 {
7209   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7210 }
7211
7212 static int
7213 debug_str_eq (const void *x1, const void *x2)
7214 {
7215   return strcmp ((((const struct indirect_string_node *)x1)->str),
7216                  (const char *)x2) == 0;
7217 }
7218
7219 /* Add STR to the indirect string hash table.  */
7220
7221 static struct indirect_string_node *
7222 find_AT_string (const char *str)
7223 {
7224   struct indirect_string_node *node;
7225   void **slot;
7226
7227   if (! debug_str_hash)
7228     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7229                                       debug_str_eq, NULL);
7230
7231   slot = htab_find_slot_with_hash (debug_str_hash, str,
7232                                    htab_hash_string (str), INSERT);
7233   if (*slot == NULL)
7234     {
7235       node = ggc_alloc_cleared_indirect_string_node ();
7236       node->str = ggc_strdup (str);
7237       *slot = node;
7238     }
7239   else
7240     node = (struct indirect_string_node *) *slot;
7241
7242   node->refcount++;
7243   return node;
7244 }
7245
7246 /* Add a string attribute value to a DIE.  */
7247
7248 static inline void
7249 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7250 {
7251   dw_attr_node attr;
7252   struct indirect_string_node *node;
7253
7254   node = find_AT_string (str);
7255
7256   attr.dw_attr = attr_kind;
7257   attr.dw_attr_val.val_class = dw_val_class_str;
7258   attr.dw_attr_val.v.val_str = node;
7259   add_dwarf_attr (die, &attr);
7260 }
7261
7262 /* Create a label for an indirect string node, ensuring it is going to
7263    be output, unless its reference count goes down to zero.  */
7264
7265 static inline void
7266 gen_label_for_indirect_string (struct indirect_string_node *node)
7267 {
7268   char label[32];
7269
7270   if (node->label)
7271     return;
7272
7273   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7274   ++dw2_string_counter;
7275   node->label = xstrdup (label);
7276 }
7277
7278 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7279    debug string STR.  */
7280
7281 static inline rtx
7282 get_debug_string_label (const char *str)
7283 {
7284   struct indirect_string_node *node = find_AT_string (str);
7285
7286   debug_str_hash_forced = true;
7287
7288   gen_label_for_indirect_string (node);
7289
7290   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7291 }
7292
7293 static inline const char *
7294 AT_string (dw_attr_ref a)
7295 {
7296   gcc_assert (a && AT_class (a) == dw_val_class_str);
7297   return a->dw_attr_val.v.val_str->str;
7298 }
7299
7300 /* Find out whether a string should be output inline in DIE
7301    or out-of-line in .debug_str section.  */
7302
7303 static enum dwarf_form
7304 AT_string_form (dw_attr_ref a)
7305 {
7306   struct indirect_string_node *node;
7307   unsigned int len;
7308
7309   gcc_assert (a && AT_class (a) == dw_val_class_str);
7310
7311   node = a->dw_attr_val.v.val_str;
7312   if (node->form)
7313     return node->form;
7314
7315   len = strlen (node->str) + 1;
7316
7317   /* If the string is shorter or equal to the size of the reference, it is
7318      always better to put it inline.  */
7319   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7320     return node->form = DW_FORM_string;
7321
7322   /* If we cannot expect the linker to merge strings in .debug_str
7323      section, only put it into .debug_str if it is worth even in this
7324      single module.  */
7325   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7326       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7327       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7328     return node->form = DW_FORM_string;
7329
7330   gen_label_for_indirect_string (node);
7331
7332   return node->form = DW_FORM_strp;
7333 }
7334
7335 /* Add a DIE reference attribute value to a DIE.  */
7336
7337 static inline void
7338 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7339 {
7340   dw_attr_node attr;
7341
7342   attr.dw_attr = attr_kind;
7343   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7344   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7345   attr.dw_attr_val.v.val_die_ref.external = 0;
7346   add_dwarf_attr (die, &attr);
7347 }
7348
7349 /* Add an AT_specification attribute to a DIE, and also make the back
7350    pointer from the specification to the definition.  */
7351
7352 static inline void
7353 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7354 {
7355   add_AT_die_ref (die, DW_AT_specification, targ_die);
7356   gcc_assert (!targ_die->die_definition);
7357   targ_die->die_definition = die;
7358 }
7359
7360 static inline dw_die_ref
7361 AT_ref (dw_attr_ref a)
7362 {
7363   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7364   return a->dw_attr_val.v.val_die_ref.die;
7365 }
7366
7367 static inline int
7368 AT_ref_external (dw_attr_ref a)
7369 {
7370   if (a && AT_class (a) == dw_val_class_die_ref)
7371     return a->dw_attr_val.v.val_die_ref.external;
7372
7373   return 0;
7374 }
7375
7376 static inline void
7377 set_AT_ref_external (dw_attr_ref a, int i)
7378 {
7379   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7380   a->dw_attr_val.v.val_die_ref.external = i;
7381 }
7382
7383 /* Add an FDE reference attribute value to a DIE.  */
7384
7385 static inline void
7386 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7387 {
7388   dw_attr_node attr;
7389
7390   attr.dw_attr = attr_kind;
7391   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7392   attr.dw_attr_val.v.val_fde_index = targ_fde;
7393   add_dwarf_attr (die, &attr);
7394 }
7395
7396 /* Add a location description attribute value to a DIE.  */
7397
7398 static inline void
7399 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7400 {
7401   dw_attr_node attr;
7402
7403   attr.dw_attr = attr_kind;
7404   attr.dw_attr_val.val_class = dw_val_class_loc;
7405   attr.dw_attr_val.v.val_loc = loc;
7406   add_dwarf_attr (die, &attr);
7407 }
7408
7409 static inline dw_loc_descr_ref
7410 AT_loc (dw_attr_ref a)
7411 {
7412   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7413   return a->dw_attr_val.v.val_loc;
7414 }
7415
7416 static inline void
7417 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7418 {
7419   dw_attr_node attr;
7420
7421   attr.dw_attr = attr_kind;
7422   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7423   attr.dw_attr_val.v.val_loc_list = loc_list;
7424   add_dwarf_attr (die, &attr);
7425   have_location_lists = true;
7426 }
7427
7428 static inline dw_loc_list_ref
7429 AT_loc_list (dw_attr_ref a)
7430 {
7431   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7432   return a->dw_attr_val.v.val_loc_list;
7433 }
7434
7435 static inline dw_loc_list_ref *
7436 AT_loc_list_ptr (dw_attr_ref a)
7437 {
7438   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7439   return &a->dw_attr_val.v.val_loc_list;
7440 }
7441
7442 /* Add an address constant attribute value to a DIE.  */
7443
7444 static inline void
7445 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7446 {
7447   dw_attr_node attr;
7448
7449   attr.dw_attr = attr_kind;
7450   attr.dw_attr_val.val_class = dw_val_class_addr;
7451   attr.dw_attr_val.v.val_addr = addr;
7452   add_dwarf_attr (die, &attr);
7453 }
7454
7455 /* Get the RTX from to an address DIE attribute.  */
7456
7457 static inline rtx
7458 AT_addr (dw_attr_ref a)
7459 {
7460   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7461   return a->dw_attr_val.v.val_addr;
7462 }
7463
7464 /* Add a file attribute value to a DIE.  */
7465
7466 static inline void
7467 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7468              struct dwarf_file_data *fd)
7469 {
7470   dw_attr_node attr;
7471
7472   attr.dw_attr = attr_kind;
7473   attr.dw_attr_val.val_class = dw_val_class_file;
7474   attr.dw_attr_val.v.val_file = fd;
7475   add_dwarf_attr (die, &attr);
7476 }
7477
7478 /* Get the dwarf_file_data from a file DIE attribute.  */
7479
7480 static inline struct dwarf_file_data *
7481 AT_file (dw_attr_ref a)
7482 {
7483   gcc_assert (a && AT_class (a) == dw_val_class_file);
7484   return a->dw_attr_val.v.val_file;
7485 }
7486
7487 /* Add a vms delta attribute value to a DIE.  */
7488
7489 static inline void
7490 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7491                   const char *lbl1, const char *lbl2)
7492 {
7493   dw_attr_node attr;
7494
7495   attr.dw_attr = attr_kind;
7496   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7497   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7498   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7499   add_dwarf_attr (die, &attr);
7500 }
7501
7502 /* Add a label identifier attribute value to a DIE.  */
7503
7504 static inline void
7505 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7506 {
7507   dw_attr_node attr;
7508
7509   attr.dw_attr = attr_kind;
7510   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7511   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7512   add_dwarf_attr (die, &attr);
7513 }
7514
7515 /* Add a section offset attribute value to a DIE, an offset into the
7516    debug_line section.  */
7517
7518 static inline void
7519 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7520                 const char *label)
7521 {
7522   dw_attr_node attr;
7523
7524   attr.dw_attr = attr_kind;
7525   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7526   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7527   add_dwarf_attr (die, &attr);
7528 }
7529
7530 /* Add a section offset attribute value to a DIE, an offset into the
7531    debug_macinfo section.  */
7532
7533 static inline void
7534 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7535                const char *label)
7536 {
7537   dw_attr_node attr;
7538
7539   attr.dw_attr = attr_kind;
7540   attr.dw_attr_val.val_class = dw_val_class_macptr;
7541   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7542   add_dwarf_attr (die, &attr);
7543 }
7544
7545 /* Add an offset attribute value to a DIE.  */
7546
7547 static inline void
7548 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7549                unsigned HOST_WIDE_INT offset)
7550 {
7551   dw_attr_node attr;
7552
7553   attr.dw_attr = attr_kind;
7554   attr.dw_attr_val.val_class = dw_val_class_offset;
7555   attr.dw_attr_val.v.val_offset = offset;
7556   add_dwarf_attr (die, &attr);
7557 }
7558
7559 /* Add an range_list attribute value to a DIE.  */
7560
7561 static void
7562 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7563                    long unsigned int offset)
7564 {
7565   dw_attr_node attr;
7566
7567   attr.dw_attr = attr_kind;
7568   attr.dw_attr_val.val_class = dw_val_class_range_list;
7569   attr.dw_attr_val.v.val_offset = offset;
7570   add_dwarf_attr (die, &attr);
7571 }
7572
7573 /* Return the start label of a delta attribute.  */
7574
7575 static inline const char *
7576 AT_vms_delta1 (dw_attr_ref a)
7577 {
7578   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7579   return a->dw_attr_val.v.val_vms_delta.lbl1;
7580 }
7581
7582 /* Return the end label of a delta attribute.  */
7583
7584 static inline const char *
7585 AT_vms_delta2 (dw_attr_ref a)
7586 {
7587   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7588   return a->dw_attr_val.v.val_vms_delta.lbl2;
7589 }
7590
7591 static inline const char *
7592 AT_lbl (dw_attr_ref a)
7593 {
7594   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7595                     || AT_class (a) == dw_val_class_lineptr
7596                     || AT_class (a) == dw_val_class_macptr));
7597   return a->dw_attr_val.v.val_lbl_id;
7598 }
7599
7600 /* Get the attribute of type attr_kind.  */
7601
7602 static dw_attr_ref
7603 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7604 {
7605   dw_attr_ref a;
7606   unsigned ix;
7607   dw_die_ref spec = NULL;
7608
7609   if (! die)
7610     return NULL;
7611
7612   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7613     if (a->dw_attr == attr_kind)
7614       return a;
7615     else if (a->dw_attr == DW_AT_specification
7616              || a->dw_attr == DW_AT_abstract_origin)
7617       spec = AT_ref (a);
7618
7619   if (spec)
7620     return get_AT (spec, attr_kind);
7621
7622   return NULL;
7623 }
7624
7625 /* Return the "low pc" attribute value, typically associated with a subprogram
7626    DIE.  Return null if the "low pc" attribute is either not present, or if it
7627    cannot be represented as an assembler label identifier.  */
7628
7629 static inline const char *
7630 get_AT_low_pc (dw_die_ref die)
7631 {
7632   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7633
7634   return a ? AT_lbl (a) : NULL;
7635 }
7636
7637 /* Return the "high pc" attribute value, typically associated with a subprogram
7638    DIE.  Return null if the "high pc" attribute is either not present, or if it
7639    cannot be represented as an assembler label identifier.  */
7640
7641 static inline const char *
7642 get_AT_hi_pc (dw_die_ref die)
7643 {
7644   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7645
7646   return a ? AT_lbl (a) : NULL;
7647 }
7648
7649 /* Return the value of the string attribute designated by ATTR_KIND, or
7650    NULL if it is not present.  */
7651
7652 static inline const char *
7653 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7654 {
7655   dw_attr_ref a = get_AT (die, attr_kind);
7656
7657   return a ? AT_string (a) : NULL;
7658 }
7659
7660 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7661    if it is not present.  */
7662
7663 static inline int
7664 get_AT_flag (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_flag (a) : 0;
7669 }
7670
7671 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7672    if it is not present.  */
7673
7674 static inline unsigned
7675 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7676 {
7677   dw_attr_ref a = get_AT (die, attr_kind);
7678
7679   return a ? AT_unsigned (a) : 0;
7680 }
7681
7682 static inline dw_die_ref
7683 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7684 {
7685   dw_attr_ref a = get_AT (die, attr_kind);
7686
7687   return a ? AT_ref (a) : NULL;
7688 }
7689
7690 static inline struct dwarf_file_data *
7691 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7692 {
7693   dw_attr_ref a = get_AT (die, attr_kind);
7694
7695   return a ? AT_file (a) : NULL;
7696 }
7697
7698 /* Return TRUE if the language is C++.  */
7699
7700 static inline bool
7701 is_cxx (void)
7702 {
7703   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7704
7705   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7706 }
7707
7708 /* Return TRUE if the language is Fortran.  */
7709
7710 static inline bool
7711 is_fortran (void)
7712 {
7713   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7714
7715   return (lang == DW_LANG_Fortran77
7716           || lang == DW_LANG_Fortran90
7717           || lang == DW_LANG_Fortran95);
7718 }
7719
7720 /* Return TRUE if the language is Ada.  */
7721
7722 static inline bool
7723 is_ada (void)
7724 {
7725   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7726
7727   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7728 }
7729
7730 /* Remove the specified attribute if present.  */
7731
7732 static void
7733 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7734 {
7735   dw_attr_ref a;
7736   unsigned ix;
7737
7738   if (! die)
7739     return;
7740
7741   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7742     if (a->dw_attr == attr_kind)
7743       {
7744         if (AT_class (a) == dw_val_class_str)
7745           if (a->dw_attr_val.v.val_str->refcount)
7746             a->dw_attr_val.v.val_str->refcount--;
7747
7748         /* VEC_ordered_remove should help reduce the number of abbrevs
7749            that are needed.  */
7750         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7751         return;
7752       }
7753 }
7754
7755 /* Remove CHILD from its parent.  PREV must have the property that
7756    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7757
7758 static void
7759 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7760 {
7761   gcc_assert (child->die_parent == prev->die_parent);
7762   gcc_assert (prev->die_sib == child);
7763   if (prev == child)
7764     {
7765       gcc_assert (child->die_parent->die_child == child);
7766       prev = NULL;
7767     }
7768   else
7769     prev->die_sib = child->die_sib;
7770   if (child->die_parent->die_child == child)
7771     child->die_parent->die_child = prev;
7772 }
7773
7774 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7775    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7776
7777 static void
7778 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7779 {
7780   dw_die_ref parent = old_child->die_parent;
7781
7782   gcc_assert (parent == prev->die_parent);
7783   gcc_assert (prev->die_sib == old_child);
7784
7785   new_child->die_parent = parent;
7786   if (prev == old_child)
7787     {
7788       gcc_assert (parent->die_child == old_child);
7789       new_child->die_sib = new_child;
7790     }
7791   else
7792     {
7793       prev->die_sib = new_child;
7794       new_child->die_sib = old_child->die_sib;
7795     }
7796   if (old_child->die_parent->die_child == old_child)
7797     old_child->die_parent->die_child = new_child;
7798 }
7799
7800 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7801
7802 static void
7803 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7804 {
7805   dw_die_ref c;
7806   new_parent->die_child = old_parent->die_child;
7807   old_parent->die_child = NULL;
7808   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7809 }
7810
7811 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7812    matches TAG.  */
7813
7814 static void
7815 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7816 {
7817   dw_die_ref c;
7818
7819   c = die->die_child;
7820   if (c) do {
7821     dw_die_ref prev = c;
7822     c = c->die_sib;
7823     while (c->die_tag == tag)
7824       {
7825         remove_child_with_prev (c, prev);
7826         /* Might have removed every child.  */
7827         if (c == c->die_sib)
7828           return;
7829         c = c->die_sib;
7830       }
7831   } while (c != die->die_child);
7832 }
7833
7834 /* Add a CHILD_DIE as the last child of DIE.  */
7835
7836 static void
7837 add_child_die (dw_die_ref die, dw_die_ref child_die)
7838 {
7839   /* FIXME this should probably be an assert.  */
7840   if (! die || ! child_die)
7841     return;
7842   gcc_assert (die != child_die);
7843
7844   child_die->die_parent = die;
7845   if (die->die_child)
7846     {
7847       child_die->die_sib = die->die_child->die_sib;
7848       die->die_child->die_sib = child_die;
7849     }
7850   else
7851     child_die->die_sib = child_die;
7852   die->die_child = child_die;
7853 }
7854
7855 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7856    is the specification, to the end of PARENT's list of children.
7857    This is done by removing and re-adding it.  */
7858
7859 static void
7860 splice_child_die (dw_die_ref parent, dw_die_ref child)
7861 {
7862   dw_die_ref p;
7863
7864   /* We want the declaration DIE from inside the class, not the
7865      specification DIE at toplevel.  */
7866   if (child->die_parent != parent)
7867     {
7868       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7869
7870       if (tmp)
7871         child = tmp;
7872     }
7873
7874   gcc_assert (child->die_parent == parent
7875               || (child->die_parent
7876                   == get_AT_ref (parent, DW_AT_specification)));
7877
7878   for (p = child->die_parent->die_child; ; p = p->die_sib)
7879     if (p->die_sib == child)
7880       {
7881         remove_child_with_prev (child, p);
7882         break;
7883       }
7884
7885   add_child_die (parent, child);
7886 }
7887
7888 /* Return a pointer to a newly created DIE node.  */
7889
7890 static inline dw_die_ref
7891 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7892 {
7893   dw_die_ref die = ggc_alloc_cleared_die_node ();
7894
7895   die->die_tag = tag_value;
7896
7897   if (parent_die != NULL)
7898     add_child_die (parent_die, die);
7899   else
7900     {
7901       limbo_die_node *limbo_node;
7902
7903       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7904       limbo_node->die = die;
7905       limbo_node->created_for = t;
7906       limbo_node->next = limbo_die_list;
7907       limbo_die_list = limbo_node;
7908     }
7909
7910   return die;
7911 }
7912
7913 /* Return the DIE associated with the given type specifier.  */
7914
7915 static inline dw_die_ref
7916 lookup_type_die (tree type)
7917 {
7918   return TYPE_SYMTAB_DIE (type);
7919 }
7920
7921 /* Equate a DIE to a given type specifier.  */
7922
7923 static inline void
7924 equate_type_number_to_die (tree type, dw_die_ref type_die)
7925 {
7926   TYPE_SYMTAB_DIE (type) = type_die;
7927 }
7928
7929 /* Returns a hash value for X (which really is a die_struct).  */
7930
7931 static hashval_t
7932 decl_die_table_hash (const void *x)
7933 {
7934   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7935 }
7936
7937 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7938
7939 static int
7940 decl_die_table_eq (const void *x, const void *y)
7941 {
7942   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7943 }
7944
7945 /* Return the DIE associated with a given declaration.  */
7946
7947 static inline dw_die_ref
7948 lookup_decl_die (tree decl)
7949 {
7950   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7951 }
7952
7953 /* Returns a hash value for X (which really is a var_loc_list).  */
7954
7955 static hashval_t
7956 decl_loc_table_hash (const void *x)
7957 {
7958   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7959 }
7960
7961 /* Return nonzero if decl_id of var_loc_list X is the same as
7962    UID of decl *Y.  */
7963
7964 static int
7965 decl_loc_table_eq (const void *x, const void *y)
7966 {
7967   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7968 }
7969
7970 /* Return the var_loc list associated with a given declaration.  */
7971
7972 static inline var_loc_list *
7973 lookup_decl_loc (const_tree decl)
7974 {
7975   if (!decl_loc_table)
7976     return NULL;
7977   return (var_loc_list *)
7978     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7979 }
7980
7981 /* Equate a DIE to a particular declaration.  */
7982
7983 static void
7984 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7985 {
7986   unsigned int decl_id = DECL_UID (decl);
7987   void **slot;
7988
7989   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7990   *slot = decl_die;
7991   decl_die->decl_id = decl_id;
7992 }
7993
7994 /* Return how many bits covers PIECE EXPR_LIST.  */
7995
7996 static int
7997 decl_piece_bitsize (rtx piece)
7998 {
7999   int ret = (int) GET_MODE (piece);
8000   if (ret)
8001     return ret;
8002   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8003               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8004   return INTVAL (XEXP (XEXP (piece, 0), 0));
8005 }
8006
8007 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8008
8009 static rtx *
8010 decl_piece_varloc_ptr (rtx piece)
8011 {
8012   if ((int) GET_MODE (piece))
8013     return &XEXP (piece, 0);
8014   else
8015     return &XEXP (XEXP (piece, 0), 1);
8016 }
8017
8018 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8019    Next is the chain of following piece nodes.  */
8020
8021 static rtx
8022 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8023 {
8024   if (bitsize <= (int) MAX_MACHINE_MODE)
8025     return alloc_EXPR_LIST (bitsize, loc_note, next);
8026   else
8027     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8028                                                GEN_INT (bitsize),
8029                                                loc_note), next);
8030 }
8031
8032 /* Return rtx that should be stored into loc field for
8033    LOC_NOTE and BITPOS/BITSIZE.  */
8034
8035 static rtx
8036 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8037                       HOST_WIDE_INT bitsize)
8038 {
8039   if (bitsize != -1)
8040     {
8041       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8042       if (bitpos != 0)
8043         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8044     }
8045   return loc_note;
8046 }
8047
8048 /* This function either modifies location piece list *DEST in
8049    place (if SRC and INNER is NULL), or copies location piece list
8050    *SRC to *DEST while modifying it.  Location BITPOS is modified
8051    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8052    not copied and if needed some padding around it is added.
8053    When modifying in place, DEST should point to EXPR_LIST where
8054    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8055    to the start of the whole list and INNER points to the EXPR_LIST
8056    where earlier pieces cover PIECE_BITPOS bits.  */
8057
8058 static void
8059 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8060                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8061                    HOST_WIDE_INT bitsize, rtx loc_note)
8062 {
8063   int diff;
8064   bool copy = inner != NULL;
8065
8066   if (copy)
8067     {
8068       /* First copy all nodes preceeding the current bitpos.  */
8069       while (src != inner)
8070         {
8071           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8072                                    decl_piece_bitsize (*src), NULL_RTX);
8073           dest = &XEXP (*dest, 1);
8074           src = &XEXP (*src, 1);
8075         }
8076     }
8077   /* Add padding if needed.  */
8078   if (bitpos != piece_bitpos)
8079     {
8080       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8081                                copy ? NULL_RTX : *dest);
8082       dest = &XEXP (*dest, 1);
8083     }
8084   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8085     {
8086       gcc_assert (!copy);
8087       /* A piece with correct bitpos and bitsize already exist,
8088          just update the location for it and return.  */
8089       *decl_piece_varloc_ptr (*dest) = loc_note;
8090       return;
8091     }
8092   /* Add the piece that changed.  */
8093   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8094   dest = &XEXP (*dest, 1);
8095   /* Skip over pieces that overlap it.  */
8096   diff = bitpos - piece_bitpos + bitsize;
8097   if (!copy)
8098     src = dest;
8099   while (diff > 0 && *src)
8100     {
8101       rtx piece = *src;
8102       diff -= decl_piece_bitsize (piece);
8103       if (copy)
8104         src = &XEXP (piece, 1);
8105       else
8106         {
8107           *src = XEXP (piece, 1);
8108           free_EXPR_LIST_node (piece);
8109         }
8110     }
8111   /* Add padding if needed.  */
8112   if (diff < 0 && *src)
8113     {
8114       if (!copy)
8115         dest = src;
8116       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8117       dest = &XEXP (*dest, 1);
8118     }
8119   if (!copy)
8120     return;
8121   /* Finally copy all nodes following it.  */
8122   while (*src)
8123     {
8124       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8125                                decl_piece_bitsize (*src), NULL_RTX);
8126       dest = &XEXP (*dest, 1);
8127       src = &XEXP (*src, 1);
8128     }
8129 }
8130
8131 /* Add a variable location node to the linked list for DECL.  */
8132
8133 static struct var_loc_node *
8134 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8135 {
8136   unsigned int decl_id;
8137   var_loc_list *temp;
8138   void **slot;
8139   struct var_loc_node *loc = NULL;
8140   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8141
8142   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8143     {
8144       tree realdecl = DECL_DEBUG_EXPR (decl);
8145       if (realdecl && handled_component_p (realdecl))
8146         {
8147           HOST_WIDE_INT maxsize;
8148           tree innerdecl;
8149           innerdecl
8150             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8151           if (!DECL_P (innerdecl)
8152               || DECL_IGNORED_P (innerdecl)
8153               || TREE_STATIC (innerdecl)
8154               || bitsize <= 0
8155               || bitpos + bitsize > 256
8156               || bitsize != maxsize)
8157             return NULL;
8158           decl = innerdecl;
8159         }
8160     }
8161
8162   decl_id = DECL_UID (decl);
8163   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8164   if (*slot == NULL)
8165     {
8166       temp = ggc_alloc_cleared_var_loc_list ();
8167       temp->decl_id = decl_id;
8168       *slot = temp;
8169     }
8170   else
8171     temp = (var_loc_list *) *slot;
8172
8173   if (temp->last)
8174     {
8175       struct var_loc_node *last = temp->last, *unused = NULL;
8176       rtx *piece_loc = NULL, last_loc_note;
8177       int piece_bitpos = 0;
8178       if (last->next)
8179         {
8180           last = last->next;
8181           gcc_assert (last->next == NULL);
8182         }
8183       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8184         {
8185           piece_loc = &last->loc;
8186           do
8187             {
8188               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8189               if (piece_bitpos + cur_bitsize > bitpos)
8190                 break;
8191               piece_bitpos += cur_bitsize;
8192               piece_loc = &XEXP (*piece_loc, 1);
8193             }
8194           while (*piece_loc);
8195         }
8196       /* TEMP->LAST here is either pointer to the last but one or
8197          last element in the chained list, LAST is pointer to the
8198          last element.  */
8199       if (label && strcmp (last->label, label) == 0)
8200         {
8201           /* For SRA optimized variables if there weren't any real
8202              insns since last note, just modify the last node.  */
8203           if (piece_loc != NULL)
8204             {
8205               adjust_piece_list (piece_loc, NULL, NULL,
8206                                  bitpos, piece_bitpos, bitsize, loc_note);
8207               return NULL;
8208             }
8209           /* If the last note doesn't cover any instructions, remove it.  */
8210           if (temp->last != last)
8211             {
8212               temp->last->next = NULL;
8213               unused = last;
8214               last = temp->last;
8215               gcc_assert (strcmp (last->label, label) != 0);
8216             }
8217           else
8218             {
8219               gcc_assert (temp->first == temp->last);
8220               memset (temp->last, '\0', sizeof (*temp->last));
8221               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8222               return temp->last;
8223             }
8224         }
8225       if (bitsize == -1 && NOTE_P (last->loc))
8226         last_loc_note = last->loc;
8227       else if (piece_loc != NULL
8228                && *piece_loc != NULL_RTX
8229                && piece_bitpos == bitpos
8230                && decl_piece_bitsize (*piece_loc) == bitsize)
8231         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8232       else
8233         last_loc_note = NULL_RTX;
8234       /* If the current location is the same as the end of the list,
8235          and either both or neither of the locations is uninitialized,
8236          we have nothing to do.  */
8237       if (last_loc_note == NULL_RTX
8238           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8239                             NOTE_VAR_LOCATION_LOC (loc_note)))
8240           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8241                != NOTE_VAR_LOCATION_STATUS (loc_note))
8242               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8243                    == VAR_INIT_STATUS_UNINITIALIZED)
8244                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8245                       == VAR_INIT_STATUS_UNINITIALIZED))))
8246         {
8247           /* Add LOC to the end of list and update LAST.  If the last
8248              element of the list has been removed above, reuse its
8249              memory for the new node, otherwise allocate a new one.  */
8250           if (unused)
8251             {
8252               loc = unused;
8253               memset (loc, '\0', sizeof (*loc));
8254             }
8255           else
8256             loc = ggc_alloc_cleared_var_loc_node ();
8257           if (bitsize == -1 || piece_loc == NULL)
8258             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8259           else
8260             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8261                                bitpos, piece_bitpos, bitsize, loc_note);
8262           last->next = loc;
8263           /* Ensure TEMP->LAST will point either to the new last but one
8264              element of the chain, or to the last element in it.  */
8265           if (last != temp->last)
8266             temp->last = last;
8267         }
8268       else if (unused)
8269         ggc_free (unused);
8270     }
8271   else
8272     {
8273       loc = ggc_alloc_cleared_var_loc_node ();
8274       temp->first = loc;
8275       temp->last = loc;
8276       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8277     }
8278   return loc;
8279 }
8280 \f
8281 /* Keep track of the number of spaces used to indent the
8282    output of the debugging routines that print the structure of
8283    the DIE internal representation.  */
8284 static int print_indent;
8285
8286 /* Indent the line the number of spaces given by print_indent.  */
8287
8288 static inline void
8289 print_spaces (FILE *outfile)
8290 {
8291   fprintf (outfile, "%*s", print_indent, "");
8292 }
8293
8294 /* Print a type signature in hex.  */
8295
8296 static inline void
8297 print_signature (FILE *outfile, char *sig)
8298 {
8299   int i;
8300
8301   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8302     fprintf (outfile, "%02x", sig[i] & 0xff);
8303 }
8304
8305 /* Print the information associated with a given DIE, and its children.
8306    This routine is a debugging aid only.  */
8307
8308 static void
8309 print_die (dw_die_ref die, FILE *outfile)
8310 {
8311   dw_attr_ref a;
8312   dw_die_ref c;
8313   unsigned ix;
8314
8315   print_spaces (outfile);
8316   fprintf (outfile, "DIE %4ld: %s\n",
8317            die->die_offset, dwarf_tag_name (die->die_tag));
8318   print_spaces (outfile);
8319   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8320   fprintf (outfile, " offset: %ld\n", die->die_offset);
8321   if (dwarf_version >= 4 && die->die_id.die_type_node)
8322     {
8323       print_spaces (outfile);
8324       fprintf (outfile, "  signature: ");
8325       print_signature (outfile, die->die_id.die_type_node->signature);
8326       fprintf (outfile, "\n");
8327     }
8328
8329   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8330     {
8331       print_spaces (outfile);
8332       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8333
8334       switch (AT_class (a))
8335         {
8336         case dw_val_class_addr:
8337           fprintf (outfile, "address");
8338           break;
8339         case dw_val_class_offset:
8340           fprintf (outfile, "offset");
8341           break;
8342         case dw_val_class_loc:
8343           fprintf (outfile, "location descriptor");
8344           break;
8345         case dw_val_class_loc_list:
8346           fprintf (outfile, "location list -> label:%s",
8347                    AT_loc_list (a)->ll_symbol);
8348           break;
8349         case dw_val_class_range_list:
8350           fprintf (outfile, "range list");
8351           break;
8352         case dw_val_class_const:
8353           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8354           break;
8355         case dw_val_class_unsigned_const:
8356           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8357           break;
8358         case dw_val_class_const_double:
8359           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8360                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8361                    a->dw_attr_val.v.val_double.high,
8362                    a->dw_attr_val.v.val_double.low);
8363           break;
8364         case dw_val_class_vec:
8365           fprintf (outfile, "floating-point or vector constant");
8366           break;
8367         case dw_val_class_flag:
8368           fprintf (outfile, "%u", AT_flag (a));
8369           break;
8370         case dw_val_class_die_ref:
8371           if (AT_ref (a) != NULL)
8372             {
8373               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8374                 {
8375                   fprintf (outfile, "die -> signature: ");
8376                   print_signature (outfile,
8377                                    AT_ref (a)->die_id.die_type_node->signature);
8378                 }
8379               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8380                 fprintf (outfile, "die -> label: %s",
8381                          AT_ref (a)->die_id.die_symbol);
8382               else
8383                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8384             }
8385           else
8386             fprintf (outfile, "die -> <null>");
8387           break;
8388         case dw_val_class_vms_delta:
8389           fprintf (outfile, "delta: @slotcount(%s-%s)",
8390                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8391           break;
8392         case dw_val_class_lbl_id:
8393         case dw_val_class_lineptr:
8394         case dw_val_class_macptr:
8395           fprintf (outfile, "label: %s", AT_lbl (a));
8396           break;
8397         case dw_val_class_str:
8398           if (AT_string (a) != NULL)
8399             fprintf (outfile, "\"%s\"", AT_string (a));
8400           else
8401             fprintf (outfile, "<null>");
8402           break;
8403         case dw_val_class_file:
8404           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8405                    AT_file (a)->emitted_number);
8406           break;
8407         case dw_val_class_data8:
8408           {
8409             int i;
8410
8411             for (i = 0; i < 8; i++)
8412               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8413             break;
8414           }
8415         default:
8416           break;
8417         }
8418
8419       fprintf (outfile, "\n");
8420     }
8421
8422   if (die->die_child != NULL)
8423     {
8424       print_indent += 4;
8425       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8426       print_indent -= 4;
8427     }
8428   if (print_indent == 0)
8429     fprintf (outfile, "\n");
8430 }
8431
8432 /* Print the contents of the source code line number correspondence table.
8433    This routine is a debugging aid only.  */
8434
8435 static void
8436 print_dwarf_line_table (FILE *outfile)
8437 {
8438   unsigned i;
8439   dw_line_info_ref line_info;
8440
8441   fprintf (outfile, "\n\nDWARF source line information\n");
8442   for (i = 1; i < line_info_table_in_use; i++)
8443     {
8444       line_info = &line_info_table[i];
8445       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8446                line_info->dw_file_num,
8447                line_info->dw_line_num);
8448     }
8449
8450   fprintf (outfile, "\n\n");
8451 }
8452
8453 /* Print the information collected for a given DIE.  */
8454
8455 DEBUG_FUNCTION void
8456 debug_dwarf_die (dw_die_ref die)
8457 {
8458   print_die (die, stderr);
8459 }
8460
8461 /* Print all DWARF information collected for the compilation unit.
8462    This routine is a debugging aid only.  */
8463
8464 DEBUG_FUNCTION void
8465 debug_dwarf (void)
8466 {
8467   print_indent = 0;
8468   print_die (comp_unit_die (), stderr);
8469   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8470     print_dwarf_line_table (stderr);
8471 }
8472 \f
8473 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8474    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8475    DIE that marks the start of the DIEs for this include file.  */
8476
8477 static dw_die_ref
8478 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8479 {
8480   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8481   dw_die_ref new_unit = gen_compile_unit_die (filename);
8482
8483   new_unit->die_sib = old_unit;
8484   return new_unit;
8485 }
8486
8487 /* Close an include-file CU and reopen the enclosing one.  */
8488
8489 static dw_die_ref
8490 pop_compile_unit (dw_die_ref old_unit)
8491 {
8492   dw_die_ref new_unit = old_unit->die_sib;
8493
8494   old_unit->die_sib = NULL;
8495   return new_unit;
8496 }
8497
8498 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8499 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8500
8501 /* Calculate the checksum of a location expression.  */
8502
8503 static inline void
8504 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8505 {
8506   int tem;
8507
8508   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8509   CHECKSUM (tem);
8510   CHECKSUM (loc->dw_loc_oprnd1);
8511   CHECKSUM (loc->dw_loc_oprnd2);
8512 }
8513
8514 /* Calculate the checksum of an attribute.  */
8515
8516 static void
8517 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8518 {
8519   dw_loc_descr_ref loc;
8520   rtx r;
8521
8522   CHECKSUM (at->dw_attr);
8523
8524   /* We don't care that this was compiled with a different compiler
8525      snapshot; if the output is the same, that's what matters.  */
8526   if (at->dw_attr == DW_AT_producer)
8527     return;
8528
8529   switch (AT_class (at))
8530     {
8531     case dw_val_class_const:
8532       CHECKSUM (at->dw_attr_val.v.val_int);
8533       break;
8534     case dw_val_class_unsigned_const:
8535       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8536       break;
8537     case dw_val_class_const_double:
8538       CHECKSUM (at->dw_attr_val.v.val_double);
8539       break;
8540     case dw_val_class_vec:
8541       CHECKSUM (at->dw_attr_val.v.val_vec);
8542       break;
8543     case dw_val_class_flag:
8544       CHECKSUM (at->dw_attr_val.v.val_flag);
8545       break;
8546     case dw_val_class_str:
8547       CHECKSUM_STRING (AT_string (at));
8548       break;
8549
8550     case dw_val_class_addr:
8551       r = AT_addr (at);
8552       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8553       CHECKSUM_STRING (XSTR (r, 0));
8554       break;
8555
8556     case dw_val_class_offset:
8557       CHECKSUM (at->dw_attr_val.v.val_offset);
8558       break;
8559
8560     case dw_val_class_loc:
8561       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8562         loc_checksum (loc, ctx);
8563       break;
8564
8565     case dw_val_class_die_ref:
8566       die_checksum (AT_ref (at), ctx, mark);
8567       break;
8568
8569     case dw_val_class_fde_ref:
8570     case dw_val_class_vms_delta:
8571     case dw_val_class_lbl_id:
8572     case dw_val_class_lineptr:
8573     case dw_val_class_macptr:
8574       break;
8575
8576     case dw_val_class_file:
8577       CHECKSUM_STRING (AT_file (at)->filename);
8578       break;
8579
8580     case dw_val_class_data8:
8581       CHECKSUM (at->dw_attr_val.v.val_data8);
8582       break;
8583
8584     default:
8585       break;
8586     }
8587 }
8588
8589 /* Calculate the checksum of a DIE.  */
8590
8591 static void
8592 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8593 {
8594   dw_die_ref c;
8595   dw_attr_ref a;
8596   unsigned ix;
8597
8598   /* To avoid infinite recursion.  */
8599   if (die->die_mark)
8600     {
8601       CHECKSUM (die->die_mark);
8602       return;
8603     }
8604   die->die_mark = ++(*mark);
8605
8606   CHECKSUM (die->die_tag);
8607
8608   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8609     attr_checksum (a, ctx, mark);
8610
8611   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8612 }
8613
8614 #undef CHECKSUM
8615 #undef CHECKSUM_STRING
8616
8617 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8618 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8619 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8620 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8621 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8622 #define CHECKSUM_ATTR(FOO) \
8623   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8624
8625 /* Calculate the checksum of a number in signed LEB128 format.  */
8626
8627 static void
8628 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8629 {
8630   unsigned char byte;
8631   bool more;
8632
8633   while (1)
8634     {
8635       byte = (value & 0x7f);
8636       value >>= 7;
8637       more = !((value == 0 && (byte & 0x40) == 0)
8638                 || (value == -1 && (byte & 0x40) != 0));
8639       if (more)
8640         byte |= 0x80;
8641       CHECKSUM (byte);
8642       if (!more)
8643         break;
8644     }
8645 }
8646
8647 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8648
8649 static void
8650 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8651 {
8652   while (1)
8653     {
8654       unsigned char byte = (value & 0x7f);
8655       value >>= 7;
8656       if (value != 0)
8657         /* More bytes to follow.  */
8658         byte |= 0x80;
8659       CHECKSUM (byte);
8660       if (value == 0)
8661         break;
8662     }
8663 }
8664
8665 /* Checksum the context of the DIE.  This adds the names of any
8666    surrounding namespaces or structures to the checksum.  */
8667
8668 static void
8669 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8670 {
8671   const char *name;
8672   dw_die_ref spec;
8673   int tag = die->die_tag;
8674
8675   if (tag != DW_TAG_namespace
8676       && tag != DW_TAG_structure_type
8677       && tag != DW_TAG_class_type)
8678     return;
8679
8680   name = get_AT_string (die, DW_AT_name);
8681
8682   spec = get_AT_ref (die, DW_AT_specification);
8683   if (spec != NULL)
8684     die = spec;
8685
8686   if (die->die_parent != NULL)
8687     checksum_die_context (die->die_parent, ctx);
8688
8689   CHECKSUM_ULEB128 ('C');
8690   CHECKSUM_ULEB128 (tag);
8691   if (name != NULL)
8692     CHECKSUM_STRING (name);
8693 }
8694
8695 /* Calculate the checksum of a location expression.  */
8696
8697 static inline void
8698 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8699 {
8700   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8701      were emitted as a DW_FORM_sdata instead of a location expression.  */
8702   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8703     {
8704       CHECKSUM_ULEB128 (DW_FORM_sdata);
8705       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8706       return;
8707     }
8708
8709   /* Otherwise, just checksum the raw location expression.  */
8710   while (loc != NULL)
8711     {
8712       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8713       CHECKSUM (loc->dw_loc_oprnd1);
8714       CHECKSUM (loc->dw_loc_oprnd2);
8715       loc = loc->dw_loc_next;
8716     }
8717 }
8718
8719 /* Calculate the checksum of an attribute.  */
8720
8721 static void
8722 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8723                        struct md5_ctx *ctx, int *mark)
8724 {
8725   dw_loc_descr_ref loc;
8726   rtx r;
8727
8728   if (AT_class (at) == dw_val_class_die_ref)
8729     {
8730       dw_die_ref target_die = AT_ref (at);
8731
8732       /* For pointer and reference types, we checksum only the (qualified)
8733          name of the target type (if there is a name).  For friend entries,
8734          we checksum only the (qualified) name of the target type or function.
8735          This allows the checksum to remain the same whether the target type
8736          is complete or not.  */
8737       if ((at->dw_attr == DW_AT_type
8738            && (tag == DW_TAG_pointer_type
8739                || tag == DW_TAG_reference_type
8740                || tag == DW_TAG_rvalue_reference_type
8741                || tag == DW_TAG_ptr_to_member_type))
8742           || (at->dw_attr == DW_AT_friend
8743               && tag == DW_TAG_friend))
8744         {
8745           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8746
8747           if (name_attr != NULL)
8748             {
8749               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8750
8751               if (decl == NULL)
8752                 decl = target_die;
8753               CHECKSUM_ULEB128 ('N');
8754               CHECKSUM_ULEB128 (at->dw_attr);
8755               if (decl->die_parent != NULL)
8756                 checksum_die_context (decl->die_parent, ctx);
8757               CHECKSUM_ULEB128 ('E');
8758               CHECKSUM_STRING (AT_string (name_attr));
8759               return;
8760             }
8761         }
8762
8763       /* For all other references to another DIE, we check to see if the
8764          target DIE has already been visited.  If it has, we emit a
8765          backward reference; if not, we descend recursively.  */
8766       if (target_die->die_mark > 0)
8767         {
8768           CHECKSUM_ULEB128 ('R');
8769           CHECKSUM_ULEB128 (at->dw_attr);
8770           CHECKSUM_ULEB128 (target_die->die_mark);
8771         }
8772       else
8773         {
8774           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8775
8776           if (decl == NULL)
8777             decl = target_die;
8778           target_die->die_mark = ++(*mark);
8779           CHECKSUM_ULEB128 ('T');
8780           CHECKSUM_ULEB128 (at->dw_attr);
8781           if (decl->die_parent != NULL)
8782             checksum_die_context (decl->die_parent, ctx);
8783           die_checksum_ordered (target_die, ctx, mark);
8784         }
8785       return;
8786     }
8787
8788   CHECKSUM_ULEB128 ('A');
8789   CHECKSUM_ULEB128 (at->dw_attr);
8790
8791   switch (AT_class (at))
8792     {
8793     case dw_val_class_const:
8794       CHECKSUM_ULEB128 (DW_FORM_sdata);
8795       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8796       break;
8797
8798     case dw_val_class_unsigned_const:
8799       CHECKSUM_ULEB128 (DW_FORM_sdata);
8800       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8801       break;
8802
8803     case dw_val_class_const_double:
8804       CHECKSUM_ULEB128 (DW_FORM_block);
8805       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8806       CHECKSUM (at->dw_attr_val.v.val_double);
8807       break;
8808
8809     case dw_val_class_vec:
8810       CHECKSUM_ULEB128 (DW_FORM_block);
8811       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8812       CHECKSUM (at->dw_attr_val.v.val_vec);
8813       break;
8814
8815     case dw_val_class_flag:
8816       CHECKSUM_ULEB128 (DW_FORM_flag);
8817       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8818       break;
8819
8820     case dw_val_class_str:
8821       CHECKSUM_ULEB128 (DW_FORM_string);
8822       CHECKSUM_STRING (AT_string (at));
8823       break;
8824
8825     case dw_val_class_addr:
8826       r = AT_addr (at);
8827       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8828       CHECKSUM_ULEB128 (DW_FORM_string);
8829       CHECKSUM_STRING (XSTR (r, 0));
8830       break;
8831
8832     case dw_val_class_offset:
8833       CHECKSUM_ULEB128 (DW_FORM_sdata);
8834       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8835       break;
8836
8837     case dw_val_class_loc:
8838       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8839         loc_checksum_ordered (loc, ctx);
8840       break;
8841
8842     case dw_val_class_fde_ref:
8843     case dw_val_class_lbl_id:
8844     case dw_val_class_lineptr:
8845     case dw_val_class_macptr:
8846       break;
8847
8848     case dw_val_class_file:
8849       CHECKSUM_ULEB128 (DW_FORM_string);
8850       CHECKSUM_STRING (AT_file (at)->filename);
8851       break;
8852
8853     case dw_val_class_data8:
8854       CHECKSUM (at->dw_attr_val.v.val_data8);
8855       break;
8856
8857     default:
8858       break;
8859     }
8860 }
8861
8862 struct checksum_attributes
8863 {
8864   dw_attr_ref at_name;
8865   dw_attr_ref at_type;
8866   dw_attr_ref at_friend;
8867   dw_attr_ref at_accessibility;
8868   dw_attr_ref at_address_class;
8869   dw_attr_ref at_allocated;
8870   dw_attr_ref at_artificial;
8871   dw_attr_ref at_associated;
8872   dw_attr_ref at_binary_scale;
8873   dw_attr_ref at_bit_offset;
8874   dw_attr_ref at_bit_size;
8875   dw_attr_ref at_bit_stride;
8876   dw_attr_ref at_byte_size;
8877   dw_attr_ref at_byte_stride;
8878   dw_attr_ref at_const_value;
8879   dw_attr_ref at_containing_type;
8880   dw_attr_ref at_count;
8881   dw_attr_ref at_data_location;
8882   dw_attr_ref at_data_member_location;
8883   dw_attr_ref at_decimal_scale;
8884   dw_attr_ref at_decimal_sign;
8885   dw_attr_ref at_default_value;
8886   dw_attr_ref at_digit_count;
8887   dw_attr_ref at_discr;
8888   dw_attr_ref at_discr_list;
8889   dw_attr_ref at_discr_value;
8890   dw_attr_ref at_encoding;
8891   dw_attr_ref at_endianity;
8892   dw_attr_ref at_explicit;
8893   dw_attr_ref at_is_optional;
8894   dw_attr_ref at_location;
8895   dw_attr_ref at_lower_bound;
8896   dw_attr_ref at_mutable;
8897   dw_attr_ref at_ordering;
8898   dw_attr_ref at_picture_string;
8899   dw_attr_ref at_prototyped;
8900   dw_attr_ref at_small;
8901   dw_attr_ref at_segment;
8902   dw_attr_ref at_string_length;
8903   dw_attr_ref at_threads_scaled;
8904   dw_attr_ref at_upper_bound;
8905   dw_attr_ref at_use_location;
8906   dw_attr_ref at_use_UTF8;
8907   dw_attr_ref at_variable_parameter;
8908   dw_attr_ref at_virtuality;
8909   dw_attr_ref at_visibility;
8910   dw_attr_ref at_vtable_elem_location;
8911 };
8912
8913 /* Collect the attributes that we will want to use for the checksum.  */
8914
8915 static void
8916 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8917 {
8918   dw_attr_ref a;
8919   unsigned ix;
8920
8921   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8922     {
8923       switch (a->dw_attr)
8924         {
8925         case DW_AT_name:
8926           attrs->at_name = a;
8927           break;
8928         case DW_AT_type:
8929           attrs->at_type = a;
8930           break;
8931         case DW_AT_friend:
8932           attrs->at_friend = a;
8933           break;
8934         case DW_AT_accessibility:
8935           attrs->at_accessibility = a;
8936           break;
8937         case DW_AT_address_class:
8938           attrs->at_address_class = a;
8939           break;
8940         case DW_AT_allocated:
8941           attrs->at_allocated = a;
8942           break;
8943         case DW_AT_artificial:
8944           attrs->at_artificial = a;
8945           break;
8946         case DW_AT_associated:
8947           attrs->at_associated = a;
8948           break;
8949         case DW_AT_binary_scale:
8950           attrs->at_binary_scale = a;
8951           break;
8952         case DW_AT_bit_offset:
8953           attrs->at_bit_offset = a;
8954           break;
8955         case DW_AT_bit_size:
8956           attrs->at_bit_size = a;
8957           break;
8958         case DW_AT_bit_stride:
8959           attrs->at_bit_stride = a;
8960           break;
8961         case DW_AT_byte_size:
8962           attrs->at_byte_size = a;
8963           break;
8964         case DW_AT_byte_stride:
8965           attrs->at_byte_stride = a;
8966           break;
8967         case DW_AT_const_value:
8968           attrs->at_const_value = a;
8969           break;
8970         case DW_AT_containing_type:
8971           attrs->at_containing_type = a;
8972           break;
8973         case DW_AT_count:
8974           attrs->at_count = a;
8975           break;
8976         case DW_AT_data_location:
8977           attrs->at_data_location = a;
8978           break;
8979         case DW_AT_data_member_location:
8980           attrs->at_data_member_location = a;
8981           break;
8982         case DW_AT_decimal_scale:
8983           attrs->at_decimal_scale = a;
8984           break;
8985         case DW_AT_decimal_sign:
8986           attrs->at_decimal_sign = a;
8987           break;
8988         case DW_AT_default_value:
8989           attrs->at_default_value = a;
8990           break;
8991         case DW_AT_digit_count:
8992           attrs->at_digit_count = a;
8993           break;
8994         case DW_AT_discr:
8995           attrs->at_discr = a;
8996           break;
8997         case DW_AT_discr_list:
8998           attrs->at_discr_list = a;
8999           break;
9000         case DW_AT_discr_value:
9001           attrs->at_discr_value = a;
9002           break;
9003         case DW_AT_encoding:
9004           attrs->at_encoding = a;
9005           break;
9006         case DW_AT_endianity:
9007           attrs->at_endianity = a;
9008           break;
9009         case DW_AT_explicit:
9010           attrs->at_explicit = a;
9011           break;
9012         case DW_AT_is_optional:
9013           attrs->at_is_optional = a;
9014           break;
9015         case DW_AT_location:
9016           attrs->at_location = a;
9017           break;
9018         case DW_AT_lower_bound:
9019           attrs->at_lower_bound = a;
9020           break;
9021         case DW_AT_mutable:
9022           attrs->at_mutable = a;
9023           break;
9024         case DW_AT_ordering:
9025           attrs->at_ordering = a;
9026           break;
9027         case DW_AT_picture_string:
9028           attrs->at_picture_string = a;
9029           break;
9030         case DW_AT_prototyped:
9031           attrs->at_prototyped = a;
9032           break;
9033         case DW_AT_small:
9034           attrs->at_small = a;
9035           break;
9036         case DW_AT_segment:
9037           attrs->at_segment = a;
9038           break;
9039         case DW_AT_string_length:
9040           attrs->at_string_length = a;
9041           break;
9042         case DW_AT_threads_scaled:
9043           attrs->at_threads_scaled = a;
9044           break;
9045         case DW_AT_upper_bound:
9046           attrs->at_upper_bound = a;
9047           break;
9048         case DW_AT_use_location:
9049           attrs->at_use_location = a;
9050           break;
9051         case DW_AT_use_UTF8:
9052           attrs->at_use_UTF8 = a;
9053           break;
9054         case DW_AT_variable_parameter:
9055           attrs->at_variable_parameter = a;
9056           break;
9057         case DW_AT_virtuality:
9058           attrs->at_virtuality = a;
9059           break;
9060         case DW_AT_visibility:
9061           attrs->at_visibility = a;
9062           break;
9063         case DW_AT_vtable_elem_location:
9064           attrs->at_vtable_elem_location = a;
9065           break;
9066         default:
9067           break;
9068         }
9069     }
9070 }
9071
9072 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9073
9074 static void
9075 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9076 {
9077   dw_die_ref c;
9078   dw_die_ref decl;
9079   struct checksum_attributes attrs;
9080
9081   CHECKSUM_ULEB128 ('D');
9082   CHECKSUM_ULEB128 (die->die_tag);
9083
9084   memset (&attrs, 0, sizeof (attrs));
9085
9086   decl = get_AT_ref (die, DW_AT_specification);
9087   if (decl != NULL)
9088     collect_checksum_attributes (&attrs, decl);
9089   collect_checksum_attributes (&attrs, die);
9090
9091   CHECKSUM_ATTR (attrs.at_name);
9092   CHECKSUM_ATTR (attrs.at_accessibility);
9093   CHECKSUM_ATTR (attrs.at_address_class);
9094   CHECKSUM_ATTR (attrs.at_allocated);
9095   CHECKSUM_ATTR (attrs.at_artificial);
9096   CHECKSUM_ATTR (attrs.at_associated);
9097   CHECKSUM_ATTR (attrs.at_binary_scale);
9098   CHECKSUM_ATTR (attrs.at_bit_offset);
9099   CHECKSUM_ATTR (attrs.at_bit_size);
9100   CHECKSUM_ATTR (attrs.at_bit_stride);
9101   CHECKSUM_ATTR (attrs.at_byte_size);
9102   CHECKSUM_ATTR (attrs.at_byte_stride);
9103   CHECKSUM_ATTR (attrs.at_const_value);
9104   CHECKSUM_ATTR (attrs.at_containing_type);
9105   CHECKSUM_ATTR (attrs.at_count);
9106   CHECKSUM_ATTR (attrs.at_data_location);
9107   CHECKSUM_ATTR (attrs.at_data_member_location);
9108   CHECKSUM_ATTR (attrs.at_decimal_scale);
9109   CHECKSUM_ATTR (attrs.at_decimal_sign);
9110   CHECKSUM_ATTR (attrs.at_default_value);
9111   CHECKSUM_ATTR (attrs.at_digit_count);
9112   CHECKSUM_ATTR (attrs.at_discr);
9113   CHECKSUM_ATTR (attrs.at_discr_list);
9114   CHECKSUM_ATTR (attrs.at_discr_value);
9115   CHECKSUM_ATTR (attrs.at_encoding);
9116   CHECKSUM_ATTR (attrs.at_endianity);
9117   CHECKSUM_ATTR (attrs.at_explicit);
9118   CHECKSUM_ATTR (attrs.at_is_optional);
9119   CHECKSUM_ATTR (attrs.at_location);
9120   CHECKSUM_ATTR (attrs.at_lower_bound);
9121   CHECKSUM_ATTR (attrs.at_mutable);
9122   CHECKSUM_ATTR (attrs.at_ordering);
9123   CHECKSUM_ATTR (attrs.at_picture_string);
9124   CHECKSUM_ATTR (attrs.at_prototyped);
9125   CHECKSUM_ATTR (attrs.at_small);
9126   CHECKSUM_ATTR (attrs.at_segment);
9127   CHECKSUM_ATTR (attrs.at_string_length);
9128   CHECKSUM_ATTR (attrs.at_threads_scaled);
9129   CHECKSUM_ATTR (attrs.at_upper_bound);
9130   CHECKSUM_ATTR (attrs.at_use_location);
9131   CHECKSUM_ATTR (attrs.at_use_UTF8);
9132   CHECKSUM_ATTR (attrs.at_variable_parameter);
9133   CHECKSUM_ATTR (attrs.at_virtuality);
9134   CHECKSUM_ATTR (attrs.at_visibility);
9135   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9136   CHECKSUM_ATTR (attrs.at_type);
9137   CHECKSUM_ATTR (attrs.at_friend);
9138
9139   /* Checksum the child DIEs, except for nested types and member functions.  */
9140   c = die->die_child;
9141   if (c) do {
9142     dw_attr_ref name_attr;
9143
9144     c = c->die_sib;
9145     name_attr = get_AT (c, DW_AT_name);
9146     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9147         && name_attr != NULL)
9148       {
9149         CHECKSUM_ULEB128 ('S');
9150         CHECKSUM_ULEB128 (c->die_tag);
9151         CHECKSUM_STRING (AT_string (name_attr));
9152       }
9153     else
9154       {
9155         /* Mark this DIE so it gets processed when unmarking.  */
9156         if (c->die_mark == 0)
9157           c->die_mark = -1;
9158         die_checksum_ordered (c, ctx, mark);
9159       }
9160   } while (c != die->die_child);
9161
9162   CHECKSUM_ULEB128 (0);
9163 }
9164
9165 #undef CHECKSUM
9166 #undef CHECKSUM_STRING
9167 #undef CHECKSUM_ATTR
9168 #undef CHECKSUM_LEB128
9169 #undef CHECKSUM_ULEB128
9170
9171 /* Generate the type signature for DIE.  This is computed by generating an
9172    MD5 checksum over the DIE's tag, its relevant attributes, and its
9173    children.  Attributes that are references to other DIEs are processed
9174    by recursion, using the MARK field to prevent infinite recursion.
9175    If the DIE is nested inside a namespace or another type, we also
9176    need to include that context in the signature.  The lower 64 bits
9177    of the resulting MD5 checksum comprise the signature.  */
9178
9179 static void
9180 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9181 {
9182   int mark;
9183   const char *name;
9184   unsigned char checksum[16];
9185   struct md5_ctx ctx;
9186   dw_die_ref decl;
9187
9188   name = get_AT_string (die, DW_AT_name);
9189   decl = get_AT_ref (die, DW_AT_specification);
9190
9191   /* First, compute a signature for just the type name (and its surrounding
9192      context, if any.  This is stored in the type unit DIE for link-time
9193      ODR (one-definition rule) checking.  */
9194
9195   if (is_cxx() && name != NULL)
9196     {
9197       md5_init_ctx (&ctx);
9198
9199       /* Checksum the names of surrounding namespaces and structures.  */
9200       if (decl != NULL && decl->die_parent != NULL)
9201         checksum_die_context (decl->die_parent, &ctx);
9202
9203       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9204       md5_process_bytes (name, strlen (name) + 1, &ctx);
9205       md5_finish_ctx (&ctx, checksum);
9206
9207       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9208     }
9209
9210   /* Next, compute the complete type signature.  */
9211
9212   md5_init_ctx (&ctx);
9213   mark = 1;
9214   die->die_mark = mark;
9215
9216   /* Checksum the names of surrounding namespaces and structures.  */
9217   if (decl != NULL && decl->die_parent != NULL)
9218     checksum_die_context (decl->die_parent, &ctx);
9219
9220   /* Checksum the DIE and its children.  */
9221   die_checksum_ordered (die, &ctx, &mark);
9222   unmark_all_dies (die);
9223   md5_finish_ctx (&ctx, checksum);
9224
9225   /* Store the signature in the type node and link the type DIE and the
9226      type node together.  */
9227   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9228           DWARF_TYPE_SIGNATURE_SIZE);
9229   die->die_id.die_type_node = type_node;
9230   type_node->type_die = die;
9231
9232   /* If the DIE is a specification, link its declaration to the type node
9233      as well.  */
9234   if (decl != NULL)
9235     decl->die_id.die_type_node = type_node;
9236 }
9237
9238 /* Do the location expressions look same?  */
9239 static inline int
9240 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9241 {
9242   return loc1->dw_loc_opc == loc2->dw_loc_opc
9243          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9244          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9245 }
9246
9247 /* Do the values look the same?  */
9248 static int
9249 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9250 {
9251   dw_loc_descr_ref loc1, loc2;
9252   rtx r1, r2;
9253
9254   if (v1->val_class != v2->val_class)
9255     return 0;
9256
9257   switch (v1->val_class)
9258     {
9259     case dw_val_class_const:
9260       return v1->v.val_int == v2->v.val_int;
9261     case dw_val_class_unsigned_const:
9262       return v1->v.val_unsigned == v2->v.val_unsigned;
9263     case dw_val_class_const_double:
9264       return v1->v.val_double.high == v2->v.val_double.high
9265              && v1->v.val_double.low == v2->v.val_double.low;
9266     case dw_val_class_vec:
9267       if (v1->v.val_vec.length != v2->v.val_vec.length
9268           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9269         return 0;
9270       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9271                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9272         return 0;
9273       return 1;
9274     case dw_val_class_flag:
9275       return v1->v.val_flag == v2->v.val_flag;
9276     case dw_val_class_str:
9277       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9278
9279     case dw_val_class_addr:
9280       r1 = v1->v.val_addr;
9281       r2 = v2->v.val_addr;
9282       if (GET_CODE (r1) != GET_CODE (r2))
9283         return 0;
9284       return !rtx_equal_p (r1, r2);
9285
9286     case dw_val_class_offset:
9287       return v1->v.val_offset == v2->v.val_offset;
9288
9289     case dw_val_class_loc:
9290       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9291            loc1 && loc2;
9292            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9293         if (!same_loc_p (loc1, loc2, mark))
9294           return 0;
9295       return !loc1 && !loc2;
9296
9297     case dw_val_class_die_ref:
9298       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9299
9300     case dw_val_class_fde_ref:
9301     case dw_val_class_vms_delta:
9302     case dw_val_class_lbl_id:
9303     case dw_val_class_lineptr:
9304     case dw_val_class_macptr:
9305       return 1;
9306
9307     case dw_val_class_file:
9308       return v1->v.val_file == v2->v.val_file;
9309
9310     case dw_val_class_data8:
9311       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9312
9313     default:
9314       return 1;
9315     }
9316 }
9317
9318 /* Do the attributes look the same?  */
9319
9320 static int
9321 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9322 {
9323   if (at1->dw_attr != at2->dw_attr)
9324     return 0;
9325
9326   /* We don't care that this was compiled with a different compiler
9327      snapshot; if the output is the same, that's what matters. */
9328   if (at1->dw_attr == DW_AT_producer)
9329     return 1;
9330
9331   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9332 }
9333
9334 /* Do the dies look the same?  */
9335
9336 static int
9337 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9338 {
9339   dw_die_ref c1, c2;
9340   dw_attr_ref a1;
9341   unsigned ix;
9342
9343   /* To avoid infinite recursion.  */
9344   if (die1->die_mark)
9345     return die1->die_mark == die2->die_mark;
9346   die1->die_mark = die2->die_mark = ++(*mark);
9347
9348   if (die1->die_tag != die2->die_tag)
9349     return 0;
9350
9351   if (VEC_length (dw_attr_node, die1->die_attr)
9352       != VEC_length (dw_attr_node, die2->die_attr))
9353     return 0;
9354
9355   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9356     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9357       return 0;
9358
9359   c1 = die1->die_child;
9360   c2 = die2->die_child;
9361   if (! c1)
9362     {
9363       if (c2)
9364         return 0;
9365     }
9366   else
9367     for (;;)
9368       {
9369         if (!same_die_p (c1, c2, mark))
9370           return 0;
9371         c1 = c1->die_sib;
9372         c2 = c2->die_sib;
9373         if (c1 == die1->die_child)
9374           {
9375             if (c2 == die2->die_child)
9376               break;
9377             else
9378               return 0;
9379           }
9380     }
9381
9382   return 1;
9383 }
9384
9385 /* Do the dies look the same?  Wrapper around same_die_p.  */
9386
9387 static int
9388 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9389 {
9390   int mark = 0;
9391   int ret = same_die_p (die1, die2, &mark);
9392
9393   unmark_all_dies (die1);
9394   unmark_all_dies (die2);
9395
9396   return ret;
9397 }
9398
9399 /* The prefix to attach to symbols on DIEs in the current comdat debug
9400    info section.  */
9401 static char *comdat_symbol_id;
9402
9403 /* The index of the current symbol within the current comdat CU.  */
9404 static unsigned int comdat_symbol_number;
9405
9406 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9407    children, and set comdat_symbol_id accordingly.  */
9408
9409 static void
9410 compute_section_prefix (dw_die_ref unit_die)
9411 {
9412   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9413   const char *base = die_name ? lbasename (die_name) : "anonymous";
9414   char *name = XALLOCAVEC (char, strlen (base) + 64);
9415   char *p;
9416   int i, mark;
9417   unsigned char checksum[16];
9418   struct md5_ctx ctx;
9419
9420   /* Compute the checksum of the DIE, then append part of it as hex digits to
9421      the name filename of the unit.  */
9422
9423   md5_init_ctx (&ctx);
9424   mark = 0;
9425   die_checksum (unit_die, &ctx, &mark);
9426   unmark_all_dies (unit_die);
9427   md5_finish_ctx (&ctx, checksum);
9428
9429   sprintf (name, "%s.", base);
9430   clean_symbol_name (name);
9431
9432   p = name + strlen (name);
9433   for (i = 0; i < 4; i++)
9434     {
9435       sprintf (p, "%.2x", checksum[i]);
9436       p += 2;
9437     }
9438
9439   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9440   comdat_symbol_number = 0;
9441 }
9442
9443 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9444
9445 static int
9446 is_type_die (dw_die_ref die)
9447 {
9448   switch (die->die_tag)
9449     {
9450     case DW_TAG_array_type:
9451     case DW_TAG_class_type:
9452     case DW_TAG_interface_type:
9453     case DW_TAG_enumeration_type:
9454     case DW_TAG_pointer_type:
9455     case DW_TAG_reference_type:
9456     case DW_TAG_rvalue_reference_type:
9457     case DW_TAG_string_type:
9458     case DW_TAG_structure_type:
9459     case DW_TAG_subroutine_type:
9460     case DW_TAG_union_type:
9461     case DW_TAG_ptr_to_member_type:
9462     case DW_TAG_set_type:
9463     case DW_TAG_subrange_type:
9464     case DW_TAG_base_type:
9465     case DW_TAG_const_type:
9466     case DW_TAG_file_type:
9467     case DW_TAG_packed_type:
9468     case DW_TAG_volatile_type:
9469     case DW_TAG_typedef:
9470       return 1;
9471     default:
9472       return 0;
9473     }
9474 }
9475
9476 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9477    Basically, we want to choose the bits that are likely to be shared between
9478    compilations (types) and leave out the bits that are specific to individual
9479    compilations (functions).  */
9480
9481 static int
9482 is_comdat_die (dw_die_ref c)
9483 {
9484   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9485      we do for stabs.  The advantage is a greater likelihood of sharing between
9486      objects that don't include headers in the same order (and therefore would
9487      put the base types in a different comdat).  jason 8/28/00 */
9488
9489   if (c->die_tag == DW_TAG_base_type)
9490     return 0;
9491
9492   if (c->die_tag == DW_TAG_pointer_type
9493       || c->die_tag == DW_TAG_reference_type
9494       || c->die_tag == DW_TAG_rvalue_reference_type
9495       || c->die_tag == DW_TAG_const_type
9496       || c->die_tag == DW_TAG_volatile_type)
9497     {
9498       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9499
9500       return t ? is_comdat_die (t) : 0;
9501     }
9502
9503   return is_type_die (c);
9504 }
9505
9506 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9507    compilation unit.  */
9508
9509 static int
9510 is_symbol_die (dw_die_ref c)
9511 {
9512   return (is_type_die (c)
9513           || is_declaration_die (c)
9514           || c->die_tag == DW_TAG_namespace
9515           || c->die_tag == DW_TAG_module);
9516 }
9517
9518 /* Returns true iff C is a compile-unit DIE.  */
9519
9520 static inline bool
9521 is_cu_die (dw_die_ref c)
9522 {
9523   return c && c->die_tag == DW_TAG_compile_unit;
9524 }
9525
9526 static char *
9527 gen_internal_sym (const char *prefix)
9528 {
9529   char buf[256];
9530
9531   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9532   return xstrdup (buf);
9533 }
9534
9535 /* Assign symbols to all worthy DIEs under DIE.  */
9536
9537 static void
9538 assign_symbol_names (dw_die_ref die)
9539 {
9540   dw_die_ref c;
9541
9542   if (is_symbol_die (die))
9543     {
9544       if (comdat_symbol_id)
9545         {
9546           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9547
9548           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9549                    comdat_symbol_id, comdat_symbol_number++);
9550           die->die_id.die_symbol = xstrdup (p);
9551         }
9552       else
9553         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9554     }
9555
9556   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9557 }
9558
9559 struct cu_hash_table_entry
9560 {
9561   dw_die_ref cu;
9562   unsigned min_comdat_num, max_comdat_num;
9563   struct cu_hash_table_entry *next;
9564 };
9565
9566 /* Routines to manipulate hash table of CUs.  */
9567 static hashval_t
9568 htab_cu_hash (const void *of)
9569 {
9570   const struct cu_hash_table_entry *const entry =
9571     (const struct cu_hash_table_entry *) of;
9572
9573   return htab_hash_string (entry->cu->die_id.die_symbol);
9574 }
9575
9576 static int
9577 htab_cu_eq (const void *of1, const void *of2)
9578 {
9579   const struct cu_hash_table_entry *const entry1 =
9580     (const struct cu_hash_table_entry *) of1;
9581   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9582
9583   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9584 }
9585
9586 static void
9587 htab_cu_del (void *what)
9588 {
9589   struct cu_hash_table_entry *next,
9590     *entry = (struct cu_hash_table_entry *) what;
9591
9592   while (entry)
9593     {
9594       next = entry->next;
9595       free (entry);
9596       entry = next;
9597     }
9598 }
9599
9600 /* Check whether we have already seen this CU and set up SYM_NUM
9601    accordingly.  */
9602 static int
9603 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9604 {
9605   struct cu_hash_table_entry dummy;
9606   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9607
9608   dummy.max_comdat_num = 0;
9609
9610   slot = (struct cu_hash_table_entry **)
9611     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9612         INSERT);
9613   entry = *slot;
9614
9615   for (; entry; last = entry, entry = entry->next)
9616     {
9617       if (same_die_p_wrap (cu, entry->cu))
9618         break;
9619     }
9620
9621   if (entry)
9622     {
9623       *sym_num = entry->min_comdat_num;
9624       return 1;
9625     }
9626
9627   entry = XCNEW (struct cu_hash_table_entry);
9628   entry->cu = cu;
9629   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9630   entry->next = *slot;
9631   *slot = entry;
9632
9633   return 0;
9634 }
9635
9636 /* Record SYM_NUM to record of CU in HTABLE.  */
9637 static void
9638 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9639 {
9640   struct cu_hash_table_entry **slot, *entry;
9641
9642   slot = (struct cu_hash_table_entry **)
9643     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9644         NO_INSERT);
9645   entry = *slot;
9646
9647   entry->max_comdat_num = sym_num;
9648 }
9649
9650 /* Traverse the DIE (which is always comp_unit_die), and set up
9651    additional compilation units for each of the include files we see
9652    bracketed by BINCL/EINCL.  */
9653
9654 static void
9655 break_out_includes (dw_die_ref die)
9656 {
9657   dw_die_ref c;
9658   dw_die_ref unit = NULL;
9659   limbo_die_node *node, **pnode;
9660   htab_t cu_hash_table;
9661
9662   c = die->die_child;
9663   if (c) do {
9664     dw_die_ref prev = c;
9665     c = c->die_sib;
9666     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9667            || (unit && is_comdat_die (c)))
9668       {
9669         dw_die_ref next = c->die_sib;
9670
9671         /* This DIE is for a secondary CU; remove it from the main one.  */
9672         remove_child_with_prev (c, prev);
9673
9674         if (c->die_tag == DW_TAG_GNU_BINCL)
9675           unit = push_new_compile_unit (unit, c);
9676         else if (c->die_tag == DW_TAG_GNU_EINCL)
9677           unit = pop_compile_unit (unit);
9678         else
9679           add_child_die (unit, c);
9680         c = next;
9681         if (c == die->die_child)
9682           break;
9683       }
9684   } while (c != die->die_child);
9685
9686 #if 0
9687   /* We can only use this in debugging, since the frontend doesn't check
9688      to make sure that we leave every include file we enter.  */
9689   gcc_assert (!unit);
9690 #endif
9691
9692   assign_symbol_names (die);
9693   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9694   for (node = limbo_die_list, pnode = &limbo_die_list;
9695        node;
9696        node = node->next)
9697     {
9698       int is_dupl;
9699
9700       compute_section_prefix (node->die);
9701       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9702                         &comdat_symbol_number);
9703       assign_symbol_names (node->die);
9704       if (is_dupl)
9705         *pnode = node->next;
9706       else
9707         {
9708           pnode = &node->next;
9709           record_comdat_symbol_number (node->die, cu_hash_table,
9710                 comdat_symbol_number);
9711         }
9712     }
9713   htab_delete (cu_hash_table);
9714 }
9715
9716 /* Return non-zero if this DIE is a declaration.  */
9717
9718 static int
9719 is_declaration_die (dw_die_ref die)
9720 {
9721   dw_attr_ref a;
9722   unsigned ix;
9723
9724   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9725     if (a->dw_attr == DW_AT_declaration)
9726       return 1;
9727
9728   return 0;
9729 }
9730
9731 /* Return non-zero if this DIE is nested inside a subprogram.  */
9732
9733 static int
9734 is_nested_in_subprogram (dw_die_ref die)
9735 {
9736   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9737
9738   if (decl == NULL)
9739     decl = die;
9740   return local_scope_p (decl);
9741 }
9742
9743 /* Return non-zero if this is a type DIE that should be moved to a
9744    COMDAT .debug_types section.  */
9745
9746 static int
9747 should_move_die_to_comdat (dw_die_ref die)
9748 {
9749   switch (die->die_tag)
9750     {
9751     case DW_TAG_class_type:
9752     case DW_TAG_structure_type:
9753     case DW_TAG_enumeration_type:
9754     case DW_TAG_union_type:
9755       /* Don't move declarations, inlined instances, or types nested in a
9756          subprogram.  */
9757       if (is_declaration_die (die)
9758           || get_AT (die, DW_AT_abstract_origin)
9759           || is_nested_in_subprogram (die))
9760         return 0;
9761       return 1;
9762     case DW_TAG_array_type:
9763     case DW_TAG_interface_type:
9764     case DW_TAG_pointer_type:
9765     case DW_TAG_reference_type:
9766     case DW_TAG_rvalue_reference_type:
9767     case DW_TAG_string_type:
9768     case DW_TAG_subroutine_type:
9769     case DW_TAG_ptr_to_member_type:
9770     case DW_TAG_set_type:
9771     case DW_TAG_subrange_type:
9772     case DW_TAG_base_type:
9773     case DW_TAG_const_type:
9774     case DW_TAG_file_type:
9775     case DW_TAG_packed_type:
9776     case DW_TAG_volatile_type:
9777     case DW_TAG_typedef:
9778     default:
9779       return 0;
9780     }
9781 }
9782
9783 /* Make a clone of DIE.  */
9784
9785 static dw_die_ref
9786 clone_die (dw_die_ref die)
9787 {
9788   dw_die_ref clone;
9789   dw_attr_ref a;
9790   unsigned ix;
9791
9792   clone = ggc_alloc_cleared_die_node ();
9793   clone->die_tag = die->die_tag;
9794
9795   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9796     add_dwarf_attr (clone, a);
9797
9798   return clone;
9799 }
9800
9801 /* Make a clone of the tree rooted at DIE.  */
9802
9803 static dw_die_ref
9804 clone_tree (dw_die_ref die)
9805 {
9806   dw_die_ref c;
9807   dw_die_ref clone = clone_die (die);
9808
9809   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9810
9811   return clone;
9812 }
9813
9814 /* Make a clone of DIE as a declaration.  */
9815
9816 static dw_die_ref
9817 clone_as_declaration (dw_die_ref die)
9818 {
9819   dw_die_ref clone;
9820   dw_die_ref decl;
9821   dw_attr_ref a;
9822   unsigned ix;
9823
9824   /* If the DIE is already a declaration, just clone it.  */
9825   if (is_declaration_die (die))
9826     return clone_die (die);
9827
9828   /* If the DIE is a specification, just clone its declaration DIE.  */
9829   decl = get_AT_ref (die, DW_AT_specification);
9830   if (decl != NULL)
9831     return clone_die (decl);
9832
9833   clone = ggc_alloc_cleared_die_node ();
9834   clone->die_tag = die->die_tag;
9835
9836   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9837     {
9838       /* We don't want to copy over all attributes.
9839          For example we don't want DW_AT_byte_size because otherwise we will no
9840          longer have a declaration and GDB will treat it as a definition.  */
9841
9842       switch (a->dw_attr)
9843         {
9844         case DW_AT_artificial:
9845         case DW_AT_containing_type:
9846         case DW_AT_external:
9847         case DW_AT_name:
9848         case DW_AT_type:
9849         case DW_AT_virtuality:
9850         case DW_AT_linkage_name:
9851         case DW_AT_MIPS_linkage_name:
9852           add_dwarf_attr (clone, a);
9853           break;
9854         case DW_AT_byte_size:
9855         default:
9856           break;
9857         }
9858     }
9859
9860   if (die->die_id.die_type_node)
9861     add_AT_die_ref (clone, DW_AT_signature, die);
9862
9863   add_AT_flag (clone, DW_AT_declaration, 1);
9864   return clone;
9865 }
9866
9867 /* Copy the declaration context to the new compile unit DIE.  This includes
9868    any surrounding namespace or type declarations.  If the DIE has an
9869    AT_specification attribute, it also includes attributes and children
9870    attached to the specification.  */
9871
9872 static void
9873 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9874 {
9875   dw_die_ref decl;
9876   dw_die_ref new_decl;
9877
9878   decl = get_AT_ref (die, DW_AT_specification);
9879   if (decl == NULL)
9880     decl = die;
9881   else
9882     {
9883       unsigned ix;
9884       dw_die_ref c;
9885       dw_attr_ref a;
9886
9887       /* Copy the type node pointer from the new DIE to the original
9888          declaration DIE so we can forward references later.  */
9889       decl->die_id.die_type_node = die->die_id.die_type_node;
9890
9891       remove_AT (die, DW_AT_specification);
9892
9893       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9894         {
9895           if (a->dw_attr != DW_AT_name
9896               && a->dw_attr != DW_AT_declaration
9897               && a->dw_attr != DW_AT_external)
9898             add_dwarf_attr (die, a);
9899         }
9900
9901       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9902     }
9903
9904   if (decl->die_parent != NULL
9905       && decl->die_parent->die_tag != DW_TAG_compile_unit
9906       && decl->die_parent->die_tag != DW_TAG_type_unit)
9907     {
9908       new_decl = copy_ancestor_tree (unit, decl, NULL);
9909       if (new_decl != NULL)
9910         {
9911           remove_AT (new_decl, DW_AT_signature);
9912           add_AT_specification (die, new_decl);
9913         }
9914     }
9915 }
9916
9917 /* Generate the skeleton ancestor tree for the given NODE, then clone
9918    the DIE and add the clone into the tree.  */
9919
9920 static void
9921 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9922 {
9923   if (node->new_die != NULL)
9924     return;
9925
9926   node->new_die = clone_as_declaration (node->old_die);
9927
9928   if (node->parent != NULL)
9929     {
9930       generate_skeleton_ancestor_tree (node->parent);
9931       add_child_die (node->parent->new_die, node->new_die);
9932     }
9933 }
9934
9935 /* Generate a skeleton tree of DIEs containing any declarations that are
9936    found in the original tree.  We traverse the tree looking for declaration
9937    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9938
9939 static void
9940 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9941 {
9942   skeleton_chain_node node;
9943   dw_die_ref c;
9944   dw_die_ref first;
9945   dw_die_ref prev = NULL;
9946   dw_die_ref next = NULL;
9947
9948   node.parent = parent;
9949
9950   first = c = parent->old_die->die_child;
9951   if (c)
9952     next = c->die_sib;
9953   if (c) do {
9954     if (prev == NULL || prev->die_sib == c)
9955       prev = c;
9956     c = next;
9957     next = (c == first ? NULL : c->die_sib);
9958     node.old_die = c;
9959     node.new_die = NULL;
9960     if (is_declaration_die (c))
9961       {
9962         /* Clone the existing DIE, move the original to the skeleton
9963            tree (which is in the main CU), and put the clone, with
9964            all the original's children, where the original came from.  */
9965         dw_die_ref clone = clone_die (c);
9966         move_all_children (c, clone);
9967
9968         replace_child (c, clone, prev);
9969         generate_skeleton_ancestor_tree (parent);
9970         add_child_die (parent->new_die, c);
9971         node.new_die = c;
9972         c = clone;
9973       }
9974     generate_skeleton_bottom_up (&node);
9975   } while (next != NULL);
9976 }
9977
9978 /* Wrapper function for generate_skeleton_bottom_up.  */
9979
9980 static dw_die_ref
9981 generate_skeleton (dw_die_ref die)
9982 {
9983   skeleton_chain_node node;
9984
9985   node.old_die = die;
9986   node.new_die = NULL;
9987   node.parent = NULL;
9988
9989   /* If this type definition is nested inside another type,
9990      always leave at least a declaration in its place.  */
9991   if (die->die_parent != NULL && is_type_die (die->die_parent))
9992     node.new_die = clone_as_declaration (die);
9993
9994   generate_skeleton_bottom_up (&node);
9995   return node.new_die;
9996 }
9997
9998 /* Remove the DIE from its parent, possibly replacing it with a cloned
9999    declaration.  The original DIE will be moved to a new compile unit
10000    so that existing references to it follow it to the new location.  If
10001    any of the original DIE's descendants is a declaration, we need to
10002    replace the original DIE with a skeleton tree and move the
10003    declarations back into the skeleton tree.  */
10004
10005 static dw_die_ref
10006 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10007 {
10008   dw_die_ref skeleton;
10009
10010   skeleton = generate_skeleton (child);
10011   if (skeleton == NULL)
10012     remove_child_with_prev (child, prev);
10013   else
10014     {
10015       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10016       replace_child (child, skeleton, prev);
10017     }
10018
10019   return skeleton;
10020 }
10021
10022 /* Traverse the DIE and set up additional .debug_types sections for each
10023    type worthy of being placed in a COMDAT section.  */
10024
10025 static void
10026 break_out_comdat_types (dw_die_ref die)
10027 {
10028   dw_die_ref c;
10029   dw_die_ref first;
10030   dw_die_ref prev = NULL;
10031   dw_die_ref next = NULL;
10032   dw_die_ref unit = NULL;
10033
10034   first = c = die->die_child;
10035   if (c)
10036     next = c->die_sib;
10037   if (c) do {
10038     if (prev == NULL || prev->die_sib == c)
10039       prev = c;
10040     c = next;
10041     next = (c == first ? NULL : c->die_sib);
10042     if (should_move_die_to_comdat (c))
10043       {
10044         dw_die_ref replacement;
10045         comdat_type_node_ref type_node;
10046
10047         /* Create a new type unit DIE as the root for the new tree, and
10048            add it to the list of comdat types.  */
10049         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10050         add_AT_unsigned (unit, DW_AT_language,
10051                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10052         type_node = ggc_alloc_cleared_comdat_type_node ();
10053         type_node->root_die = unit;
10054         type_node->next = comdat_type_list;
10055         comdat_type_list = type_node;
10056
10057         /* Generate the type signature.  */
10058         generate_type_signature (c, type_node);
10059
10060         /* Copy the declaration context, attributes, and children of the
10061            declaration into the new compile unit DIE.  */
10062         copy_declaration_context (unit, c);
10063
10064         /* Remove this DIE from the main CU.  */
10065         replacement = remove_child_or_replace_with_skeleton (c, prev);
10066
10067         /* Break out nested types into their own type units.  */
10068         break_out_comdat_types (c);
10069
10070         /* Add the DIE to the new compunit.  */
10071         add_child_die (unit, c);
10072
10073         if (replacement != NULL)
10074           c = replacement;
10075       }
10076     else if (c->die_tag == DW_TAG_namespace
10077              || c->die_tag == DW_TAG_class_type
10078              || c->die_tag == DW_TAG_structure_type
10079              || c->die_tag == DW_TAG_union_type)
10080       {
10081         /* Look for nested types that can be broken out.  */
10082         break_out_comdat_types (c);
10083       }
10084   } while (next != NULL);
10085 }
10086
10087 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10088
10089 struct decl_table_entry
10090 {
10091   dw_die_ref orig;
10092   dw_die_ref copy;
10093 };
10094
10095 /* Routines to manipulate hash table of copied declarations.  */
10096
10097 static hashval_t
10098 htab_decl_hash (const void *of)
10099 {
10100   const struct decl_table_entry *const entry =
10101     (const struct decl_table_entry *) of;
10102
10103   return htab_hash_pointer (entry->orig);
10104 }
10105
10106 static int
10107 htab_decl_eq (const void *of1, const void *of2)
10108 {
10109   const struct decl_table_entry *const entry1 =
10110     (const struct decl_table_entry *) of1;
10111   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10112
10113   return entry1->orig == entry2;
10114 }
10115
10116 static void
10117 htab_decl_del (void *what)
10118 {
10119   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10120
10121   free (entry);
10122 }
10123
10124 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10125    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10126    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10127    to check if the ancestor has already been copied into UNIT.  */
10128
10129 static dw_die_ref
10130 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10131 {
10132   dw_die_ref parent = die->die_parent;
10133   dw_die_ref new_parent = unit;
10134   dw_die_ref copy;
10135   void **slot = NULL;
10136   struct decl_table_entry *entry = NULL;
10137
10138   if (decl_table)
10139     {
10140       /* Check if the entry has already been copied to UNIT.  */
10141       slot = htab_find_slot_with_hash (decl_table, die,
10142                                        htab_hash_pointer (die), INSERT);
10143       if (*slot != HTAB_EMPTY_ENTRY)
10144         {
10145           entry = (struct decl_table_entry *) *slot;
10146           return entry->copy;
10147         }
10148
10149       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10150       entry = XCNEW (struct decl_table_entry);
10151       entry->orig = die;
10152       entry->copy = NULL;
10153       *slot = entry;
10154     }
10155
10156   if (parent != NULL)
10157     {
10158       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10159       if (spec != NULL)
10160         parent = spec;
10161       if (parent->die_tag != DW_TAG_compile_unit
10162           && parent->die_tag != DW_TAG_type_unit)
10163         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10164     }
10165
10166   copy = clone_as_declaration (die);
10167   add_child_die (new_parent, copy);
10168
10169   if (decl_table != NULL)
10170     {
10171       /* Record the pointer to the copy.  */
10172       entry->copy = copy;
10173     }
10174
10175   return copy;
10176 }
10177
10178 /* Walk the DIE and its children, looking for references to incomplete
10179    or trivial types that are unmarked (i.e., that are not in the current
10180    type_unit).  */
10181
10182 static void
10183 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10184 {
10185   dw_die_ref c;
10186   dw_attr_ref a;
10187   unsigned ix;
10188
10189   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10190     {
10191       if (AT_class (a) == dw_val_class_die_ref)
10192         {
10193           dw_die_ref targ = AT_ref (a);
10194           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10195           void **slot;
10196           struct decl_table_entry *entry;
10197
10198           if (targ->die_mark != 0 || type_node != NULL)
10199             continue;
10200
10201           slot = htab_find_slot_with_hash (decl_table, targ,
10202                                            htab_hash_pointer (targ), INSERT);
10203
10204           if (*slot != HTAB_EMPTY_ENTRY)
10205             {
10206               /* TARG has already been copied, so we just need to
10207                  modify the reference to point to the copy.  */
10208               entry = (struct decl_table_entry *) *slot;
10209               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10210             }
10211           else
10212             {
10213               dw_die_ref parent = unit;
10214               dw_die_ref copy = clone_tree (targ);
10215
10216               /* Make sure the cloned tree is marked as part of the
10217                  type unit.  */
10218               mark_dies (copy);
10219
10220               /* Record in DECL_TABLE that TARG has been copied.
10221                  Need to do this now, before the recursive call,
10222                  because DECL_TABLE may be expanded and SLOT
10223                  would no longer be a valid pointer.  */
10224               entry = XCNEW (struct decl_table_entry);
10225               entry->orig = targ;
10226               entry->copy = copy;
10227               *slot = entry;
10228
10229               /* If TARG has surrounding context, copy its ancestor tree
10230                  into the new type unit.  */
10231               if (targ->die_parent != NULL
10232                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10233                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10234                 parent = copy_ancestor_tree (unit, targ->die_parent,
10235                                              decl_table);
10236
10237               add_child_die (parent, copy);
10238               a->dw_attr_val.v.val_die_ref.die = copy;
10239
10240               /* Make sure the newly-copied DIE is walked.  If it was
10241                  installed in a previously-added context, it won't
10242                  get visited otherwise.  */
10243               if (parent != unit)
10244                 {
10245                   /* Find the highest point of the newly-added tree,
10246                      mark each node along the way, and walk from there.  */
10247                   parent->die_mark = 1;
10248                   while (parent->die_parent
10249                          && parent->die_parent->die_mark == 0)
10250                     {
10251                       parent = parent->die_parent;
10252                       parent->die_mark = 1;
10253                     }
10254                   copy_decls_walk (unit, parent, decl_table);
10255                 }
10256             }
10257         }
10258     }
10259
10260   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10261 }
10262
10263 /* Copy declarations for "unworthy" types into the new comdat section.
10264    Incomplete types, modified types, and certain other types aren't broken
10265    out into comdat sections of their own, so they don't have a signature,
10266    and we need to copy the declaration into the same section so that we
10267    don't have an external reference.  */
10268
10269 static void
10270 copy_decls_for_unworthy_types (dw_die_ref unit)
10271 {
10272   htab_t decl_table;
10273
10274   mark_dies (unit);
10275   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10276   copy_decls_walk (unit, unit, decl_table);
10277   htab_delete (decl_table);
10278   unmark_dies (unit);
10279 }
10280
10281 /* Traverse the DIE and add a sibling attribute if it may have the
10282    effect of speeding up access to siblings.  To save some space,
10283    avoid generating sibling attributes for DIE's without children.  */
10284
10285 static void
10286 add_sibling_attributes (dw_die_ref die)
10287 {
10288   dw_die_ref c;
10289
10290   if (! die->die_child)
10291     return;
10292
10293   if (die->die_parent && die != die->die_parent->die_child)
10294     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10295
10296   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10297 }
10298
10299 /* Output all location lists for the DIE and its children.  */
10300
10301 static void
10302 output_location_lists (dw_die_ref die)
10303 {
10304   dw_die_ref c;
10305   dw_attr_ref a;
10306   unsigned ix;
10307
10308   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10309     if (AT_class (a) == dw_val_class_loc_list)
10310       output_loc_list (AT_loc_list (a));
10311
10312   FOR_EACH_CHILD (die, c, output_location_lists (c));
10313 }
10314
10315 /* The format of each DIE (and its attribute value pairs) is encoded in an
10316    abbreviation table.  This routine builds the abbreviation table and assigns
10317    a unique abbreviation id for each abbreviation entry.  The children of each
10318    die are visited recursively.  */
10319
10320 static void
10321 build_abbrev_table (dw_die_ref die)
10322 {
10323   unsigned long abbrev_id;
10324   unsigned int n_alloc;
10325   dw_die_ref c;
10326   dw_attr_ref a;
10327   unsigned ix;
10328
10329   /* Scan the DIE references, and mark as external any that refer to
10330      DIEs from other CUs (i.e. those which are not marked).  */
10331   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10332     if (AT_class (a) == dw_val_class_die_ref
10333         && AT_ref (a)->die_mark == 0)
10334       {
10335         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10336         set_AT_ref_external (a, 1);
10337       }
10338
10339   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10340     {
10341       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10342       dw_attr_ref die_a, abbrev_a;
10343       unsigned ix;
10344       bool ok = true;
10345
10346       if (abbrev->die_tag != die->die_tag)
10347         continue;
10348       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10349         continue;
10350
10351       if (VEC_length (dw_attr_node, abbrev->die_attr)
10352           != VEC_length (dw_attr_node, die->die_attr))
10353         continue;
10354
10355       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10356         {
10357           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10358           if ((abbrev_a->dw_attr != die_a->dw_attr)
10359               || (value_format (abbrev_a) != value_format (die_a)))
10360             {
10361               ok = false;
10362               break;
10363             }
10364         }
10365       if (ok)
10366         break;
10367     }
10368
10369   if (abbrev_id >= abbrev_die_table_in_use)
10370     {
10371       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10372         {
10373           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10374           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10375                                             n_alloc);
10376
10377           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10378                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10379           abbrev_die_table_allocated = n_alloc;
10380         }
10381
10382       ++abbrev_die_table_in_use;
10383       abbrev_die_table[abbrev_id] = die;
10384     }
10385
10386   die->die_abbrev = abbrev_id;
10387   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10388 }
10389 \f
10390 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10391
10392 static int
10393 constant_size (unsigned HOST_WIDE_INT value)
10394 {
10395   int log;
10396
10397   if (value == 0)
10398     log = 0;
10399   else
10400     log = floor_log2 (value);
10401
10402   log = log / 8;
10403   log = 1 << (floor_log2 (log) + 1);
10404
10405   return log;
10406 }
10407
10408 /* Return the size of a DIE as it is represented in the
10409    .debug_info section.  */
10410
10411 static unsigned long
10412 size_of_die (dw_die_ref die)
10413 {
10414   unsigned long size = 0;
10415   dw_attr_ref a;
10416   unsigned ix;
10417
10418   size += size_of_uleb128 (die->die_abbrev);
10419   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10420     {
10421       switch (AT_class (a))
10422         {
10423         case dw_val_class_addr:
10424           size += DWARF2_ADDR_SIZE;
10425           break;
10426         case dw_val_class_offset:
10427           size += DWARF_OFFSET_SIZE;
10428           break;
10429         case dw_val_class_loc:
10430           {
10431             unsigned long lsize = size_of_locs (AT_loc (a));
10432
10433             /* Block length.  */
10434             if (dwarf_version >= 4)
10435               size += size_of_uleb128 (lsize);
10436             else
10437               size += constant_size (lsize);
10438             size += lsize;
10439           }
10440           break;
10441         case dw_val_class_loc_list:
10442           size += DWARF_OFFSET_SIZE;
10443           break;
10444         case dw_val_class_range_list:
10445           size += DWARF_OFFSET_SIZE;
10446           break;
10447         case dw_val_class_const:
10448           size += size_of_sleb128 (AT_int (a));
10449           break;
10450         case dw_val_class_unsigned_const:
10451           size += constant_size (AT_unsigned (a));
10452           break;
10453         case dw_val_class_const_double:
10454           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10455           if (HOST_BITS_PER_WIDE_INT >= 64)
10456             size++; /* block */
10457           break;
10458         case dw_val_class_vec:
10459           size += constant_size (a->dw_attr_val.v.val_vec.length
10460                                  * a->dw_attr_val.v.val_vec.elt_size)
10461                   + a->dw_attr_val.v.val_vec.length
10462                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10463           break;
10464         case dw_val_class_flag:
10465           if (dwarf_version >= 4)
10466             /* Currently all add_AT_flag calls pass in 1 as last argument,
10467                so DW_FORM_flag_present can be used.  If that ever changes,
10468                we'll need to use DW_FORM_flag and have some optimization
10469                in build_abbrev_table that will change those to
10470                DW_FORM_flag_present if it is set to 1 in all DIEs using
10471                the same abbrev entry.  */
10472             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10473           else
10474             size += 1;
10475           break;
10476         case dw_val_class_die_ref:
10477           if (AT_ref_external (a))
10478             {
10479               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10480                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10481                  is sized by target address length, whereas in DWARF3
10482                  it's always sized as an offset.  */
10483               if (dwarf_version >= 4)
10484                 size += DWARF_TYPE_SIGNATURE_SIZE;
10485               else if (dwarf_version == 2)
10486                 size += DWARF2_ADDR_SIZE;
10487               else
10488                 size += DWARF_OFFSET_SIZE;
10489             }
10490           else
10491             size += DWARF_OFFSET_SIZE;
10492           break;
10493         case dw_val_class_fde_ref:
10494           size += DWARF_OFFSET_SIZE;
10495           break;
10496         case dw_val_class_lbl_id:
10497           size += DWARF2_ADDR_SIZE;
10498           break;
10499         case dw_val_class_lineptr:
10500         case dw_val_class_macptr:
10501           size += DWARF_OFFSET_SIZE;
10502           break;
10503         case dw_val_class_str:
10504           if (AT_string_form (a) == DW_FORM_strp)
10505             size += DWARF_OFFSET_SIZE;
10506           else
10507             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10508           break;
10509         case dw_val_class_file:
10510           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10511           break;
10512         case dw_val_class_data8:
10513           size += 8;
10514           break;
10515         case dw_val_class_vms_delta:
10516           size += DWARF_OFFSET_SIZE;
10517           break;
10518         default:
10519           gcc_unreachable ();
10520         }
10521     }
10522
10523   return size;
10524 }
10525
10526 /* Size the debugging information associated with a given DIE.  Visits the
10527    DIE's children recursively.  Updates the global variable next_die_offset, on
10528    each time through.  Uses the current value of next_die_offset to update the
10529    die_offset field in each DIE.  */
10530
10531 static void
10532 calc_die_sizes (dw_die_ref die)
10533 {
10534   dw_die_ref c;
10535
10536   die->die_offset = next_die_offset;
10537   next_die_offset += size_of_die (die);
10538
10539   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10540
10541   if (die->die_child != NULL)
10542     /* Count the null byte used to terminate sibling lists.  */
10543     next_die_offset += 1;
10544 }
10545
10546 /* Set the marks for a die and its children.  We do this so
10547    that we know whether or not a reference needs to use FORM_ref_addr; only
10548    DIEs in the same CU will be marked.  We used to clear out the offset
10549    and use that as the flag, but ran into ordering problems.  */
10550
10551 static void
10552 mark_dies (dw_die_ref die)
10553 {
10554   dw_die_ref c;
10555
10556   gcc_assert (!die->die_mark);
10557
10558   die->die_mark = 1;
10559   FOR_EACH_CHILD (die, c, mark_dies (c));
10560 }
10561
10562 /* Clear the marks for a die and its children.  */
10563
10564 static void
10565 unmark_dies (dw_die_ref die)
10566 {
10567   dw_die_ref c;
10568
10569   if (dwarf_version < 4)
10570     gcc_assert (die->die_mark);
10571
10572   die->die_mark = 0;
10573   FOR_EACH_CHILD (die, c, unmark_dies (c));
10574 }
10575
10576 /* Clear the marks for a die, its children and referred dies.  */
10577
10578 static void
10579 unmark_all_dies (dw_die_ref die)
10580 {
10581   dw_die_ref c;
10582   dw_attr_ref a;
10583   unsigned ix;
10584
10585   if (!die->die_mark)
10586     return;
10587   die->die_mark = 0;
10588
10589   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10590
10591   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10592     if (AT_class (a) == dw_val_class_die_ref)
10593       unmark_all_dies (AT_ref (a));
10594 }
10595
10596 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10597    generated for the compilation unit.  */
10598
10599 static unsigned long
10600 size_of_pubnames (VEC (pubname_entry, gc) * names)
10601 {
10602   unsigned long size;
10603   unsigned i;
10604   pubname_ref p;
10605
10606   size = DWARF_PUBNAMES_HEADER_SIZE;
10607   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10608     if (names != pubtype_table
10609         || p->die->die_offset != 0
10610         || !flag_eliminate_unused_debug_types)
10611       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10612
10613   size += DWARF_OFFSET_SIZE;
10614   return size;
10615 }
10616
10617 /* Return the size of the information in the .debug_aranges section.  */
10618
10619 static unsigned long
10620 size_of_aranges (void)
10621 {
10622   unsigned long size;
10623
10624   size = DWARF_ARANGES_HEADER_SIZE;
10625
10626   /* Count the address/length pair for this compilation unit.  */
10627   if (text_section_used)
10628     size += 2 * DWARF2_ADDR_SIZE;
10629   if (cold_text_section_used)
10630     size += 2 * DWARF2_ADDR_SIZE;
10631   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10632
10633   /* Count the two zero words used to terminated the address range table.  */
10634   size += 2 * DWARF2_ADDR_SIZE;
10635   return size;
10636 }
10637 \f
10638 /* Select the encoding of an attribute value.  */
10639
10640 static enum dwarf_form
10641 value_format (dw_attr_ref a)
10642 {
10643   switch (a->dw_attr_val.val_class)
10644     {
10645     case dw_val_class_addr:
10646       /* Only very few attributes allow DW_FORM_addr.  */
10647       switch (a->dw_attr)
10648         {
10649         case DW_AT_low_pc:
10650         case DW_AT_high_pc:
10651         case DW_AT_entry_pc:
10652         case DW_AT_trampoline:
10653           return DW_FORM_addr;
10654         default:
10655           break;
10656         }
10657       switch (DWARF2_ADDR_SIZE)
10658         {
10659         case 1:
10660           return DW_FORM_data1;
10661         case 2:
10662           return DW_FORM_data2;
10663         case 4:
10664           return DW_FORM_data4;
10665         case 8:
10666           return DW_FORM_data8;
10667         default:
10668           gcc_unreachable ();
10669         }
10670     case dw_val_class_range_list:
10671     case dw_val_class_loc_list:
10672       if (dwarf_version >= 4)
10673         return DW_FORM_sec_offset;
10674       /* FALLTHRU */
10675     case dw_val_class_vms_delta:
10676     case dw_val_class_offset:
10677       switch (DWARF_OFFSET_SIZE)
10678         {
10679         case 4:
10680           return DW_FORM_data4;
10681         case 8:
10682           return DW_FORM_data8;
10683         default:
10684           gcc_unreachable ();
10685         }
10686     case dw_val_class_loc:
10687       if (dwarf_version >= 4)
10688         return DW_FORM_exprloc;
10689       switch (constant_size (size_of_locs (AT_loc (a))))
10690         {
10691         case 1:
10692           return DW_FORM_block1;
10693         case 2:
10694           return DW_FORM_block2;
10695         default:
10696           gcc_unreachable ();
10697         }
10698     case dw_val_class_const:
10699       return DW_FORM_sdata;
10700     case dw_val_class_unsigned_const:
10701       switch (constant_size (AT_unsigned (a)))
10702         {
10703         case 1:
10704           return DW_FORM_data1;
10705         case 2:
10706           return DW_FORM_data2;
10707         case 4:
10708           return DW_FORM_data4;
10709         case 8:
10710           return DW_FORM_data8;
10711         default:
10712           gcc_unreachable ();
10713         }
10714     case dw_val_class_const_double:
10715       switch (HOST_BITS_PER_WIDE_INT)
10716         {
10717         case 8:
10718           return DW_FORM_data2;
10719         case 16:
10720           return DW_FORM_data4;
10721         case 32:
10722           return DW_FORM_data8;
10723         case 64:
10724         default:
10725           return DW_FORM_block1;
10726         }
10727     case dw_val_class_vec:
10728       switch (constant_size (a->dw_attr_val.v.val_vec.length
10729                              * a->dw_attr_val.v.val_vec.elt_size))
10730         {
10731         case 1:
10732           return DW_FORM_block1;
10733         case 2:
10734           return DW_FORM_block2;
10735         case 4:
10736           return DW_FORM_block4;
10737         default:
10738           gcc_unreachable ();
10739         }
10740     case dw_val_class_flag:
10741       if (dwarf_version >= 4)
10742         {
10743           /* Currently all add_AT_flag calls pass in 1 as last argument,
10744              so DW_FORM_flag_present can be used.  If that ever changes,
10745              we'll need to use DW_FORM_flag and have some optimization
10746              in build_abbrev_table that will change those to
10747              DW_FORM_flag_present if it is set to 1 in all DIEs using
10748              the same abbrev entry.  */
10749           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10750           return DW_FORM_flag_present;
10751         }
10752       return DW_FORM_flag;
10753     case dw_val_class_die_ref:
10754       if (AT_ref_external (a))
10755         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10756       else
10757         return DW_FORM_ref;
10758     case dw_val_class_fde_ref:
10759       return DW_FORM_data;
10760     case dw_val_class_lbl_id:
10761       return DW_FORM_addr;
10762     case dw_val_class_lineptr:
10763     case dw_val_class_macptr:
10764       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10765     case dw_val_class_str:
10766       return AT_string_form (a);
10767     case dw_val_class_file:
10768       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10769         {
10770         case 1:
10771           return DW_FORM_data1;
10772         case 2:
10773           return DW_FORM_data2;
10774         case 4:
10775           return DW_FORM_data4;
10776         default:
10777           gcc_unreachable ();
10778         }
10779
10780     case dw_val_class_data8:
10781       return DW_FORM_data8;
10782
10783     default:
10784       gcc_unreachable ();
10785     }
10786 }
10787
10788 /* Output the encoding of an attribute value.  */
10789
10790 static void
10791 output_value_format (dw_attr_ref a)
10792 {
10793   enum dwarf_form form = value_format (a);
10794
10795   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10796 }
10797
10798 /* Output the .debug_abbrev section which defines the DIE abbreviation
10799    table.  */
10800
10801 static void
10802 output_abbrev_section (void)
10803 {
10804   unsigned long abbrev_id;
10805
10806   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10807     {
10808       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10809       unsigned ix;
10810       dw_attr_ref a_attr;
10811
10812       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10813       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10814                                    dwarf_tag_name (abbrev->die_tag));
10815
10816       if (abbrev->die_child != NULL)
10817         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10818       else
10819         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10820
10821       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10822            ix++)
10823         {
10824           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10825                                        dwarf_attr_name (a_attr->dw_attr));
10826           output_value_format (a_attr);
10827         }
10828
10829       dw2_asm_output_data (1, 0, NULL);
10830       dw2_asm_output_data (1, 0, NULL);
10831     }
10832
10833   /* Terminate the table.  */
10834   dw2_asm_output_data (1, 0, NULL);
10835 }
10836
10837 /* Output a symbol we can use to refer to this DIE from another CU.  */
10838
10839 static inline void
10840 output_die_symbol (dw_die_ref die)
10841 {
10842   char *sym = die->die_id.die_symbol;
10843
10844   if (sym == 0)
10845     return;
10846
10847   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10848     /* We make these global, not weak; if the target doesn't support
10849        .linkonce, it doesn't support combining the sections, so debugging
10850        will break.  */
10851     targetm.asm_out.globalize_label (asm_out_file, sym);
10852
10853   ASM_OUTPUT_LABEL (asm_out_file, sym);
10854 }
10855
10856 /* Return a new location list, given the begin and end range, and the
10857    expression.  */
10858
10859 static inline dw_loc_list_ref
10860 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10861               const char *section)
10862 {
10863   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10864
10865   retlist->begin = begin;
10866   retlist->end = end;
10867   retlist->expr = expr;
10868   retlist->section = section;
10869
10870   return retlist;
10871 }
10872
10873 /* Generate a new internal symbol for this location list node, if it
10874    hasn't got one yet.  */
10875
10876 static inline void
10877 gen_llsym (dw_loc_list_ref list)
10878 {
10879   gcc_assert (!list->ll_symbol);
10880   list->ll_symbol = gen_internal_sym ("LLST");
10881 }
10882
10883 /* Output the location list given to us.  */
10884
10885 static void
10886 output_loc_list (dw_loc_list_ref list_head)
10887 {
10888   dw_loc_list_ref curr = list_head;
10889
10890   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10891
10892   /* Walk the location list, and output each range + expression.  */
10893   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10894     {
10895       unsigned long size;
10896       /* Don't output an entry that starts and ends at the same address.  */
10897       if (strcmp (curr->begin, curr->end) == 0)
10898         continue;
10899       if (!have_multiple_function_sections)
10900         {
10901           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10902                                 "Location list begin address (%s)",
10903                                 list_head->ll_symbol);
10904           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10905                                 "Location list end address (%s)",
10906                                 list_head->ll_symbol);
10907         }
10908       else
10909         {
10910           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10911                                "Location list begin address (%s)",
10912                                list_head->ll_symbol);
10913           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10914                                "Location list end address (%s)",
10915                                list_head->ll_symbol);
10916         }
10917       size = size_of_locs (curr->expr);
10918
10919       /* Output the block length for this list of location operations.  */
10920       gcc_assert (size <= 0xffff);
10921       dw2_asm_output_data (2, size, "%s", "Location expression size");
10922
10923       output_loc_sequence (curr->expr);
10924     }
10925
10926   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10927                        "Location list terminator begin (%s)",
10928                        list_head->ll_symbol);
10929   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10930                        "Location list terminator end (%s)",
10931                        list_head->ll_symbol);
10932 }
10933
10934 /* Output a type signature.  */
10935
10936 static inline void
10937 output_signature (const char *sig, const char *name)
10938 {
10939   int i;
10940
10941   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10942     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10943 }
10944
10945 /* Output the DIE and its attributes.  Called recursively to generate
10946    the definitions of each child DIE.  */
10947
10948 static void
10949 output_die (dw_die_ref die)
10950 {
10951   dw_attr_ref a;
10952   dw_die_ref c;
10953   unsigned long size;
10954   unsigned ix;
10955
10956   /* If someone in another CU might refer to us, set up a symbol for
10957      them to point to.  */
10958   if (dwarf_version < 4 && die->die_id.die_symbol)
10959     output_die_symbol (die);
10960
10961   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10962                                (unsigned long)die->die_offset,
10963                                dwarf_tag_name (die->die_tag));
10964
10965   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10966     {
10967       const char *name = dwarf_attr_name (a->dw_attr);
10968
10969       switch (AT_class (a))
10970         {
10971         case dw_val_class_addr:
10972           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10973           break;
10974
10975         case dw_val_class_offset:
10976           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10977                                "%s", name);
10978           break;
10979
10980         case dw_val_class_range_list:
10981           {
10982             char *p = strchr (ranges_section_label, '\0');
10983
10984             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10985                      a->dw_attr_val.v.val_offset);
10986             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10987                                    debug_ranges_section, "%s", name);
10988             *p = '\0';
10989           }
10990           break;
10991
10992         case dw_val_class_loc:
10993           size = size_of_locs (AT_loc (a));
10994
10995           /* Output the block length for this list of location operations.  */
10996           if (dwarf_version >= 4)
10997             dw2_asm_output_data_uleb128 (size, "%s", name);
10998           else
10999             dw2_asm_output_data (constant_size (size), size, "%s", name);
11000
11001           output_loc_sequence (AT_loc (a));
11002           break;
11003
11004         case dw_val_class_const:
11005           /* ??? It would be slightly more efficient to use a scheme like is
11006              used for unsigned constants below, but gdb 4.x does not sign
11007              extend.  Gdb 5.x does sign extend.  */
11008           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11009           break;
11010
11011         case dw_val_class_unsigned_const:
11012           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11013                                AT_unsigned (a), "%s", name);
11014           break;
11015
11016         case dw_val_class_const_double:
11017           {
11018             unsigned HOST_WIDE_INT first, second;
11019
11020             if (HOST_BITS_PER_WIDE_INT >= 64)
11021               dw2_asm_output_data (1,
11022                                    2 * HOST_BITS_PER_WIDE_INT
11023                                    / HOST_BITS_PER_CHAR,
11024                                    NULL);
11025
11026             if (WORDS_BIG_ENDIAN)
11027               {
11028                 first = a->dw_attr_val.v.val_double.high;
11029                 second = a->dw_attr_val.v.val_double.low;
11030               }
11031             else
11032               {
11033                 first = a->dw_attr_val.v.val_double.low;
11034                 second = a->dw_attr_val.v.val_double.high;
11035               }
11036
11037             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11038                                  first, name);
11039             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11040                                  second, NULL);
11041           }
11042           break;
11043
11044         case dw_val_class_vec:
11045           {
11046             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11047             unsigned int len = a->dw_attr_val.v.val_vec.length;
11048             unsigned int i;
11049             unsigned char *p;
11050
11051             dw2_asm_output_data (constant_size (len * elt_size),
11052                                  len * elt_size, "%s", name);
11053             if (elt_size > sizeof (HOST_WIDE_INT))
11054               {
11055                 elt_size /= 2;
11056                 len *= 2;
11057               }
11058             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11059                  i < len;
11060                  i++, p += elt_size)
11061               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11062                                    "fp or vector constant word %u", i);
11063             break;
11064           }
11065
11066         case dw_val_class_flag:
11067           if (dwarf_version >= 4)
11068             {
11069               /* Currently all add_AT_flag calls pass in 1 as last argument,
11070                  so DW_FORM_flag_present can be used.  If that ever changes,
11071                  we'll need to use DW_FORM_flag and have some optimization
11072                  in build_abbrev_table that will change those to
11073                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11074                  the same abbrev entry.  */
11075               gcc_assert (AT_flag (a) == 1);
11076               if (flag_debug_asm)
11077                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11078                          ASM_COMMENT_START, name);
11079               break;
11080             }
11081           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11082           break;
11083
11084         case dw_val_class_loc_list:
11085           {
11086             char *sym = AT_loc_list (a)->ll_symbol;
11087
11088             gcc_assert (sym);
11089             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11090                                    "%s", name);
11091           }
11092           break;
11093
11094         case dw_val_class_die_ref:
11095           if (AT_ref_external (a))
11096             {
11097               if (dwarf_version >= 4)
11098                 {
11099                   comdat_type_node_ref type_node =
11100                     AT_ref (a)->die_id.die_type_node;
11101
11102                   gcc_assert (type_node);
11103                   output_signature (type_node->signature, name);
11104                 }
11105               else
11106                 {
11107                   char *sym = AT_ref (a)->die_id.die_symbol;
11108                   int size;
11109
11110                   gcc_assert (sym);
11111                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11112                      length, whereas in DWARF3 it's always sized as an
11113                      offset.  */
11114                   if (dwarf_version == 2)
11115                     size = DWARF2_ADDR_SIZE;
11116                   else
11117                     size = DWARF_OFFSET_SIZE;
11118                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11119                                          name);
11120                 }
11121             }
11122           else
11123             {
11124               gcc_assert (AT_ref (a)->die_offset);
11125               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11126                                    "%s", name);
11127             }
11128           break;
11129
11130         case dw_val_class_fde_ref:
11131           {
11132             char l1[20];
11133
11134             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11135                                          a->dw_attr_val.v.val_fde_index * 2);
11136             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11137                                    "%s", name);
11138           }
11139           break;
11140
11141         case dw_val_class_vms_delta:
11142           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11143                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11144                                     "%s", name);
11145           break;
11146
11147         case dw_val_class_lbl_id:
11148           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11149           break;
11150
11151         case dw_val_class_lineptr:
11152           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11153                                  debug_line_section, "%s", name);
11154           break;
11155
11156         case dw_val_class_macptr:
11157           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11158                                  debug_macinfo_section, "%s", name);
11159           break;
11160
11161         case dw_val_class_str:
11162           if (AT_string_form (a) == DW_FORM_strp)
11163             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11164                                    a->dw_attr_val.v.val_str->label,
11165                                    debug_str_section,
11166                                    "%s: \"%s\"", name, AT_string (a));
11167           else
11168             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11169           break;
11170
11171         case dw_val_class_file:
11172           {
11173             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11174
11175             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11176                                  a->dw_attr_val.v.val_file->filename);
11177             break;
11178           }
11179
11180         case dw_val_class_data8:
11181           {
11182             int i;
11183
11184             for (i = 0; i < 8; i++)
11185               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11186                                    i == 0 ? "%s" : NULL, name);
11187             break;
11188           }
11189
11190         default:
11191           gcc_unreachable ();
11192         }
11193     }
11194
11195   FOR_EACH_CHILD (die, c, output_die (c));
11196
11197   /* Add null byte to terminate sibling list.  */
11198   if (die->die_child != NULL)
11199     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11200                          (unsigned long) die->die_offset);
11201 }
11202
11203 /* Output the compilation unit that appears at the beginning of the
11204    .debug_info section, and precedes the DIE descriptions.  */
11205
11206 static void
11207 output_compilation_unit_header (void)
11208 {
11209   int ver = dwarf_version;
11210
11211   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11212     dw2_asm_output_data (4, 0xffffffff,
11213       "Initial length escape value indicating 64-bit DWARF extension");
11214   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11215                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11216                        "Length of Compilation Unit Info");
11217   dw2_asm_output_data (2, ver, "DWARF version number");
11218   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11219                          debug_abbrev_section,
11220                          "Offset Into Abbrev. Section");
11221   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11222 }
11223
11224 /* Output the compilation unit DIE and its children.  */
11225
11226 static void
11227 output_comp_unit (dw_die_ref die, int output_if_empty)
11228 {
11229   const char *secname;
11230   char *oldsym, *tmp;
11231
11232   /* Unless we are outputting main CU, we may throw away empty ones.  */
11233   if (!output_if_empty && die->die_child == NULL)
11234     return;
11235
11236   /* Even if there are no children of this DIE, we must output the information
11237      about the compilation unit.  Otherwise, on an empty translation unit, we
11238      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11239      will then complain when examining the file.  First mark all the DIEs in
11240      this CU so we know which get local refs.  */
11241   mark_dies (die);
11242
11243   build_abbrev_table (die);
11244
11245   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11246   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11247   calc_die_sizes (die);
11248
11249   oldsym = die->die_id.die_symbol;
11250   if (oldsym)
11251     {
11252       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11253
11254       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11255       secname = tmp;
11256       die->die_id.die_symbol = NULL;
11257       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11258     }
11259   else
11260     switch_to_section (debug_info_section);
11261
11262   /* Output debugging information.  */
11263   output_compilation_unit_header ();
11264   output_die (die);
11265
11266   /* Leave the marks on the main CU, so we can check them in
11267      output_pubnames.  */
11268   if (oldsym)
11269     {
11270       unmark_dies (die);
11271       die->die_id.die_symbol = oldsym;
11272     }
11273 }
11274
11275 /* Output a comdat type unit DIE and its children.  */
11276
11277 static void
11278 output_comdat_type_unit (comdat_type_node *node)
11279 {
11280   const char *secname;
11281   char *tmp;
11282   int i;
11283 #if defined (OBJECT_FORMAT_ELF)
11284   tree comdat_key;
11285 #endif
11286
11287   /* First mark all the DIEs in this CU so we know which get local refs.  */
11288   mark_dies (node->root_die);
11289
11290   build_abbrev_table (node->root_die);
11291
11292   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11293   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11294   calc_die_sizes (node->root_die);
11295
11296 #if defined (OBJECT_FORMAT_ELF)
11297   secname = ".debug_types";
11298   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11299   sprintf (tmp, "wt.");
11300   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11301     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11302   comdat_key = get_identifier (tmp);
11303   targetm.asm_out.named_section (secname,
11304                                  SECTION_DEBUG | SECTION_LINKONCE,
11305                                  comdat_key);
11306 #else
11307   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11308   sprintf (tmp, ".gnu.linkonce.wt.");
11309   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11310     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11311   secname = tmp;
11312   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11313 #endif
11314
11315   /* Output debugging information.  */
11316   output_compilation_unit_header ();
11317   output_signature (node->signature, "Type Signature");
11318   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11319                        "Offset to Type DIE");
11320   output_die (node->root_die);
11321
11322   unmark_dies (node->root_die);
11323 }
11324
11325 /* Return the DWARF2/3 pubname associated with a decl.  */
11326
11327 static const char *
11328 dwarf2_name (tree decl, int scope)
11329 {
11330   if (DECL_NAMELESS (decl))
11331     return NULL;
11332   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11333 }
11334
11335 /* Add a new entry to .debug_pubnames if appropriate.  */
11336
11337 static void
11338 add_pubname_string (const char *str, dw_die_ref die)
11339 {
11340   if (targetm.want_debug_pub_sections)
11341     {
11342       pubname_entry e;
11343
11344       e.die = die;
11345       e.name = xstrdup (str);
11346       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11347     }
11348 }
11349
11350 static void
11351 add_pubname (tree decl, dw_die_ref die)
11352 {
11353   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11354     {
11355       const char *name = dwarf2_name (decl, 1);
11356       if (name)
11357         add_pubname_string (name, die);
11358     }
11359 }
11360
11361 /* Add a new entry to .debug_pubtypes if appropriate.  */
11362
11363 static void
11364 add_pubtype (tree decl, dw_die_ref die)
11365 {
11366   pubname_entry e;
11367
11368   if (!targetm.want_debug_pub_sections)
11369     return;
11370
11371   e.name = NULL;
11372   if ((TREE_PUBLIC (decl)
11373        || is_cu_die (die->die_parent))
11374       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11375     {
11376       e.die = die;
11377       if (TYPE_P (decl))
11378         {
11379           if (TYPE_NAME (decl))
11380             {
11381               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11382                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11383               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11384                        && DECL_NAME (TYPE_NAME (decl)))
11385                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11386               else
11387                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11388             }
11389         }
11390       else
11391         {
11392           e.name = dwarf2_name (decl, 1);
11393           if (e.name)
11394             e.name = xstrdup (e.name);
11395         }
11396
11397       /* If we don't have a name for the type, there's no point in adding
11398          it to the table.  */
11399       if (e.name && e.name[0] != '\0')
11400         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11401     }
11402 }
11403
11404 /* Output the public names table used to speed up access to externally
11405    visible names; or the public types table used to find type definitions.  */
11406
11407 static void
11408 output_pubnames (VEC (pubname_entry, gc) * names)
11409 {
11410   unsigned i;
11411   unsigned long pubnames_length = size_of_pubnames (names);
11412   pubname_ref pub;
11413
11414   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11415     dw2_asm_output_data (4, 0xffffffff,
11416       "Initial length escape value indicating 64-bit DWARF extension");
11417   if (names == pubname_table)
11418     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11419                          "Length of Public Names Info");
11420   else
11421     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11422                          "Length of Public Type Names Info");
11423   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11424   dw2_asm_output_data (2, 2, "DWARF Version");
11425   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11426                          debug_info_section,
11427                          "Offset of Compilation Unit Info");
11428   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11429                        "Compilation Unit Length");
11430
11431   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11432     {
11433       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11434       if (names == pubname_table)
11435         gcc_assert (pub->die->die_mark);
11436
11437       if (names != pubtype_table
11438           || pub->die->die_offset != 0
11439           || !flag_eliminate_unused_debug_types)
11440         {
11441           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11442                                "DIE offset");
11443
11444           dw2_asm_output_nstring (pub->name, -1, "external name");
11445         }
11446     }
11447
11448   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11449 }
11450
11451 /* Add a new entry to .debug_aranges if appropriate.  */
11452
11453 static void
11454 add_arange (tree decl, dw_die_ref die)
11455 {
11456   if (! DECL_SECTION_NAME (decl))
11457     return;
11458
11459   if (arange_table_in_use == arange_table_allocated)
11460     {
11461       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11462       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11463                                     arange_table_allocated);
11464       memset (arange_table + arange_table_in_use, 0,
11465               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11466     }
11467
11468   arange_table[arange_table_in_use++] = die;
11469 }
11470
11471 /* Output the information that goes into the .debug_aranges table.
11472    Namely, define the beginning and ending address range of the
11473    text section generated for this compilation unit.  */
11474
11475 static void
11476 output_aranges (void)
11477 {
11478   unsigned i;
11479   unsigned long aranges_length = size_of_aranges ();
11480
11481   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11482     dw2_asm_output_data (4, 0xffffffff,
11483       "Initial length escape value indicating 64-bit DWARF extension");
11484   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11485                        "Length of Address Ranges Info");
11486   /* Version number for aranges is still 2, even in DWARF3.  */
11487   dw2_asm_output_data (2, 2, "DWARF Version");
11488   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11489                          debug_info_section,
11490                          "Offset of Compilation Unit Info");
11491   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11492   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11493
11494   /* We need to align to twice the pointer size here.  */
11495   if (DWARF_ARANGES_PAD_SIZE)
11496     {
11497       /* Pad using a 2 byte words so that padding is correct for any
11498          pointer size.  */
11499       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11500                            2 * DWARF2_ADDR_SIZE);
11501       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11502         dw2_asm_output_data (2, 0, NULL);
11503     }
11504
11505   /* It is necessary not to output these entries if the sections were
11506      not used; if the sections were not used, the length will be 0 and
11507      the address may end up as 0 if the section is discarded by ld
11508      --gc-sections, leaving an invalid (0, 0) entry that can be
11509      confused with the terminator.  */
11510   if (text_section_used)
11511     {
11512       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11513       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11514                             text_section_label, "Length");
11515     }
11516   if (cold_text_section_used)
11517     {
11518       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11519                            "Address");
11520       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11521                             cold_text_section_label, "Length");
11522     }
11523
11524   for (i = 0; i < arange_table_in_use; i++)
11525     {
11526       dw_die_ref die = arange_table[i];
11527
11528       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11529       gcc_assert (die->die_mark);
11530
11531       if (die->die_tag == DW_TAG_subprogram)
11532         {
11533           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11534                                "Address");
11535           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11536                                 get_AT_low_pc (die), "Length");
11537         }
11538       else
11539         {
11540           /* A static variable; extract the symbol from DW_AT_location.
11541              Note that this code isn't currently hit, as we only emit
11542              aranges for functions (jason 9/23/99).  */
11543           dw_attr_ref a = get_AT (die, DW_AT_location);
11544           dw_loc_descr_ref loc;
11545
11546           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11547
11548           loc = AT_loc (a);
11549           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11550
11551           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11552                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11553           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11554                                get_AT_unsigned (die, DW_AT_byte_size),
11555                                "Length");
11556         }
11557     }
11558
11559   /* Output the terminator words.  */
11560   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11561   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11562 }
11563
11564 /* Add a new entry to .debug_ranges.  Return the offset at which it
11565    was placed.  */
11566
11567 static unsigned int
11568 add_ranges_num (int num)
11569 {
11570   unsigned int in_use = ranges_table_in_use;
11571
11572   if (in_use == ranges_table_allocated)
11573     {
11574       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11575       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11576                                     ranges_table_allocated);
11577       memset (ranges_table + ranges_table_in_use, 0,
11578               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11579     }
11580
11581   ranges_table[in_use].num = num;
11582   ranges_table_in_use = in_use + 1;
11583
11584   return in_use * 2 * DWARF2_ADDR_SIZE;
11585 }
11586
11587 /* Add a new entry to .debug_ranges corresponding to a block, or a
11588    range terminator if BLOCK is NULL.  */
11589
11590 static unsigned int
11591 add_ranges (const_tree block)
11592 {
11593   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11594 }
11595
11596 /* Add a new entry to .debug_ranges corresponding to a pair of
11597    labels.  */
11598
11599 static void
11600 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11601                       bool *added)
11602 {
11603   unsigned int in_use = ranges_by_label_in_use;
11604   unsigned int offset;
11605
11606   if (in_use == ranges_by_label_allocated)
11607     {
11608       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11609       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11610                                        ranges_by_label,
11611                                        ranges_by_label_allocated);
11612       memset (ranges_by_label + ranges_by_label_in_use, 0,
11613               RANGES_TABLE_INCREMENT
11614               * sizeof (struct dw_ranges_by_label_struct));
11615     }
11616
11617   ranges_by_label[in_use].begin = begin;
11618   ranges_by_label[in_use].end = end;
11619   ranges_by_label_in_use = in_use + 1;
11620
11621   offset = add_ranges_num (-(int)in_use - 1);
11622   if (!*added)
11623     {
11624       add_AT_range_list (die, DW_AT_ranges, offset);
11625       *added = true;
11626     }
11627 }
11628
11629 static void
11630 output_ranges (void)
11631 {
11632   unsigned i;
11633   static const char *const start_fmt = "Offset %#x";
11634   const char *fmt = start_fmt;
11635
11636   for (i = 0; i < ranges_table_in_use; i++)
11637     {
11638       int block_num = ranges_table[i].num;
11639
11640       if (block_num > 0)
11641         {
11642           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11643           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11644
11645           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11646           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11647
11648           /* If all code is in the text section, then the compilation
11649              unit base address defaults to DW_AT_low_pc, which is the
11650              base of the text section.  */
11651           if (!have_multiple_function_sections)
11652             {
11653               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11654                                     text_section_label,
11655                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11656               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11657                                     text_section_label, NULL);
11658             }
11659
11660           /* Otherwise, the compilation unit base address is zero,
11661              which allows us to use absolute addresses, and not worry
11662              about whether the target supports cross-section
11663              arithmetic.  */
11664           else
11665             {
11666               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11667                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11668               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11669             }
11670
11671           fmt = NULL;
11672         }
11673
11674       /* Negative block_num stands for an index into ranges_by_label.  */
11675       else if (block_num < 0)
11676         {
11677           int lab_idx = - block_num - 1;
11678
11679           if (!have_multiple_function_sections)
11680             {
11681               gcc_unreachable ();
11682 #if 0
11683               /* If we ever use add_ranges_by_labels () for a single
11684                  function section, all we have to do is to take out
11685                  the #if 0 above.  */
11686               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11687                                     ranges_by_label[lab_idx].begin,
11688                                     text_section_label,
11689                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11690               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11691                                     ranges_by_label[lab_idx].end,
11692                                     text_section_label, NULL);
11693 #endif
11694             }
11695           else
11696             {
11697               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11698                                    ranges_by_label[lab_idx].begin,
11699                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11700               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11701                                    ranges_by_label[lab_idx].end,
11702                                    NULL);
11703             }
11704         }
11705       else
11706         {
11707           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11708           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11709           fmt = start_fmt;
11710         }
11711     }
11712 }
11713
11714 /* Data structure containing information about input files.  */
11715 struct file_info
11716 {
11717   const char *path;     /* Complete file name.  */
11718   const char *fname;    /* File name part.  */
11719   int length;           /* Length of entire string.  */
11720   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11721   int dir_idx;          /* Index in directory table.  */
11722 };
11723
11724 /* Data structure containing information about directories with source
11725    files.  */
11726 struct dir_info
11727 {
11728   const char *path;     /* Path including directory name.  */
11729   int length;           /* Path length.  */
11730   int prefix;           /* Index of directory entry which is a prefix.  */
11731   int count;            /* Number of files in this directory.  */
11732   int dir_idx;          /* Index of directory used as base.  */
11733 };
11734
11735 /* Callback function for file_info comparison.  We sort by looking at
11736    the directories in the path.  */
11737
11738 static int
11739 file_info_cmp (const void *p1, const void *p2)
11740 {
11741   const struct file_info *const s1 = (const struct file_info *) p1;
11742   const struct file_info *const s2 = (const struct file_info *) p2;
11743   const unsigned char *cp1;
11744   const unsigned char *cp2;
11745
11746   /* Take care of file names without directories.  We need to make sure that
11747      we return consistent values to qsort since some will get confused if
11748      we return the same value when identical operands are passed in opposite
11749      orders.  So if neither has a directory, return 0 and otherwise return
11750      1 or -1 depending on which one has the directory.  */
11751   if ((s1->path == s1->fname || s2->path == s2->fname))
11752     return (s2->path == s2->fname) - (s1->path == s1->fname);
11753
11754   cp1 = (const unsigned char *) s1->path;
11755   cp2 = (const unsigned char *) s2->path;
11756
11757   while (1)
11758     {
11759       ++cp1;
11760       ++cp2;
11761       /* Reached the end of the first path?  If so, handle like above.  */
11762       if ((cp1 == (const unsigned char *) s1->fname)
11763           || (cp2 == (const unsigned char *) s2->fname))
11764         return ((cp2 == (const unsigned char *) s2->fname)
11765                 - (cp1 == (const unsigned char *) s1->fname));
11766
11767       /* Character of current path component the same?  */
11768       else if (*cp1 != *cp2)
11769         return *cp1 - *cp2;
11770     }
11771 }
11772
11773 struct file_name_acquire_data
11774 {
11775   struct file_info *files;
11776   int used_files;
11777   int max_files;
11778 };
11779
11780 /* Traversal function for the hash table.  */
11781
11782 static int
11783 file_name_acquire (void ** slot, void *data)
11784 {
11785   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11786   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11787   struct file_info *fi;
11788   const char *f;
11789
11790   gcc_assert (fnad->max_files >= d->emitted_number);
11791
11792   if (! d->emitted_number)
11793     return 1;
11794
11795   gcc_assert (fnad->max_files != fnad->used_files);
11796
11797   fi = fnad->files + fnad->used_files++;
11798
11799   /* Skip all leading "./".  */
11800   f = d->filename;
11801   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11802     f += 2;
11803
11804   /* Create a new array entry.  */
11805   fi->path = f;
11806   fi->length = strlen (f);
11807   fi->file_idx = d;
11808
11809   /* Search for the file name part.  */
11810   f = strrchr (f, DIR_SEPARATOR);
11811 #if defined (DIR_SEPARATOR_2)
11812   {
11813     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11814
11815     if (g != NULL)
11816       {
11817         if (f == NULL || f < g)
11818           f = g;
11819       }
11820   }
11821 #endif
11822
11823   fi->fname = f == NULL ? fi->path : f + 1;
11824   return 1;
11825 }
11826
11827 /* Output the directory table and the file name table.  We try to minimize
11828    the total amount of memory needed.  A heuristic is used to avoid large
11829    slowdowns with many input files.  */
11830
11831 static void
11832 output_file_names (void)
11833 {
11834   struct file_name_acquire_data fnad;
11835   int numfiles;
11836   struct file_info *files;
11837   struct dir_info *dirs;
11838   int *saved;
11839   int *savehere;
11840   int *backmap;
11841   int ndirs;
11842   int idx_offset;
11843   int i;
11844
11845   if (!last_emitted_file)
11846     {
11847       dw2_asm_output_data (1, 0, "End directory table");
11848       dw2_asm_output_data (1, 0, "End file name table");
11849       return;
11850     }
11851
11852   numfiles = last_emitted_file->emitted_number;
11853
11854   /* Allocate the various arrays we need.  */
11855   files = XALLOCAVEC (struct file_info, numfiles);
11856   dirs = XALLOCAVEC (struct dir_info, numfiles);
11857
11858   fnad.files = files;
11859   fnad.used_files = 0;
11860   fnad.max_files = numfiles;
11861   htab_traverse (file_table, file_name_acquire, &fnad);
11862   gcc_assert (fnad.used_files == fnad.max_files);
11863
11864   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11865
11866   /* Find all the different directories used.  */
11867   dirs[0].path = files[0].path;
11868   dirs[0].length = files[0].fname - files[0].path;
11869   dirs[0].prefix = -1;
11870   dirs[0].count = 1;
11871   dirs[0].dir_idx = 0;
11872   files[0].dir_idx = 0;
11873   ndirs = 1;
11874
11875   for (i = 1; i < numfiles; i++)
11876     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11877         && memcmp (dirs[ndirs - 1].path, files[i].path,
11878                    dirs[ndirs - 1].length) == 0)
11879       {
11880         /* Same directory as last entry.  */
11881         files[i].dir_idx = ndirs - 1;
11882         ++dirs[ndirs - 1].count;
11883       }
11884     else
11885       {
11886         int j;
11887
11888         /* This is a new directory.  */
11889         dirs[ndirs].path = files[i].path;
11890         dirs[ndirs].length = files[i].fname - files[i].path;
11891         dirs[ndirs].count = 1;
11892         dirs[ndirs].dir_idx = ndirs;
11893         files[i].dir_idx = ndirs;
11894
11895         /* Search for a prefix.  */
11896         dirs[ndirs].prefix = -1;
11897         for (j = 0; j < ndirs; j++)
11898           if (dirs[j].length < dirs[ndirs].length
11899               && dirs[j].length > 1
11900               && (dirs[ndirs].prefix == -1
11901                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11902               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11903             dirs[ndirs].prefix = j;
11904
11905         ++ndirs;
11906       }
11907
11908   /* Now to the actual work.  We have to find a subset of the directories which
11909      allow expressing the file name using references to the directory table
11910      with the least amount of characters.  We do not do an exhaustive search
11911      where we would have to check out every combination of every single
11912      possible prefix.  Instead we use a heuristic which provides nearly optimal
11913      results in most cases and never is much off.  */
11914   saved = XALLOCAVEC (int, ndirs);
11915   savehere = XALLOCAVEC (int, ndirs);
11916
11917   memset (saved, '\0', ndirs * sizeof (saved[0]));
11918   for (i = 0; i < ndirs; i++)
11919     {
11920       int j;
11921       int total;
11922
11923       /* We can always save some space for the current directory.  But this
11924          does not mean it will be enough to justify adding the directory.  */
11925       savehere[i] = dirs[i].length;
11926       total = (savehere[i] - saved[i]) * dirs[i].count;
11927
11928       for (j = i + 1; j < ndirs; j++)
11929         {
11930           savehere[j] = 0;
11931           if (saved[j] < dirs[i].length)
11932             {
11933               /* Determine whether the dirs[i] path is a prefix of the
11934                  dirs[j] path.  */
11935               int k;
11936
11937               k = dirs[j].prefix;
11938               while (k != -1 && k != (int) i)
11939                 k = dirs[k].prefix;
11940
11941               if (k == (int) i)
11942                 {
11943                   /* Yes it is.  We can possibly save some memory by
11944                      writing the filenames in dirs[j] relative to
11945                      dirs[i].  */
11946                   savehere[j] = dirs[i].length;
11947                   total += (savehere[j] - saved[j]) * dirs[j].count;
11948                 }
11949             }
11950         }
11951
11952       /* Check whether we can save enough to justify adding the dirs[i]
11953          directory.  */
11954       if (total > dirs[i].length + 1)
11955         {
11956           /* It's worthwhile adding.  */
11957           for (j = i; j < ndirs; j++)
11958             if (savehere[j] > 0)
11959               {
11960                 /* Remember how much we saved for this directory so far.  */
11961                 saved[j] = savehere[j];
11962
11963                 /* Remember the prefix directory.  */
11964                 dirs[j].dir_idx = i;
11965               }
11966         }
11967     }
11968
11969   /* Emit the directory name table.  */
11970   idx_offset = dirs[0].length > 0 ? 1 : 0;
11971   for (i = 1 - idx_offset; i < ndirs; i++)
11972     dw2_asm_output_nstring (dirs[i].path,
11973                             dirs[i].length
11974                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11975                             "Directory Entry: %#x", i + idx_offset);
11976
11977   dw2_asm_output_data (1, 0, "End directory table");
11978
11979   /* We have to emit them in the order of emitted_number since that's
11980      used in the debug info generation.  To do this efficiently we
11981      generate a back-mapping of the indices first.  */
11982   backmap = XALLOCAVEC (int, numfiles);
11983   for (i = 0; i < numfiles; i++)
11984     backmap[files[i].file_idx->emitted_number - 1] = i;
11985
11986   /* Now write all the file names.  */
11987   for (i = 0; i < numfiles; i++)
11988     {
11989       int file_idx = backmap[i];
11990       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11991
11992 #ifdef VMS_DEBUGGING_INFO
11993 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11994
11995       /* Setting these fields can lead to debugger miscomparisons,
11996          but VMS Debug requires them to be set correctly.  */
11997
11998       int ver;
11999       long long cdt;
12000       long siz;
12001       int maxfilelen = strlen (files[file_idx].path)
12002                                + dirs[dir_idx].length
12003                                + MAX_VMS_VERSION_LEN + 1;
12004       char *filebuf = XALLOCAVEC (char, maxfilelen);
12005
12006       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12007       snprintf (filebuf, maxfilelen, "%s;%d",
12008                 files[file_idx].path + dirs[dir_idx].length, ver);
12009
12010       dw2_asm_output_nstring
12011         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12012
12013       /* Include directory index.  */
12014       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12015
12016       /* Modification time.  */
12017       dw2_asm_output_data_uleb128
12018         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12019           ? cdt : 0,
12020          NULL);
12021
12022       /* File length in bytes.  */
12023       dw2_asm_output_data_uleb128
12024         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12025           ? siz : 0,
12026          NULL);
12027 #else
12028       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12029                               "File Entry: %#x", (unsigned) i + 1);
12030
12031       /* Include directory index.  */
12032       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12033
12034       /* Modification time.  */
12035       dw2_asm_output_data_uleb128 (0, NULL);
12036
12037       /* File length in bytes.  */
12038       dw2_asm_output_data_uleb128 (0, NULL);
12039 #endif /* VMS_DEBUGGING_INFO */
12040     }
12041
12042   dw2_asm_output_data (1, 0, "End file name table");
12043 }
12044
12045
12046 /* Output the source line number correspondence information.  This
12047    information goes into the .debug_line section.  */
12048
12049 static void
12050 output_line_info (void)
12051 {
12052   char l1[20], l2[20], p1[20], p2[20];
12053   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12054   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12055   unsigned opc;
12056   unsigned n_op_args;
12057   unsigned long lt_index;
12058   unsigned long current_line;
12059   long line_offset;
12060   long line_delta;
12061   unsigned long current_file;
12062   unsigned long function;
12063   int ver = dwarf_version;
12064
12065   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12066   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12067   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12068   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12069
12070   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12071     dw2_asm_output_data (4, 0xffffffff,
12072       "Initial length escape value indicating 64-bit DWARF extension");
12073   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12074                         "Length of Source Line Info");
12075   ASM_OUTPUT_LABEL (asm_out_file, l1);
12076
12077   dw2_asm_output_data (2, ver, "DWARF Version");
12078   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12079   ASM_OUTPUT_LABEL (asm_out_file, p1);
12080
12081   /* Define the architecture-dependent minimum instruction length (in
12082    bytes).  In this implementation of DWARF, this field is used for
12083    information purposes only.  Since GCC generates assembly language,
12084    we have no a priori knowledge of how many instruction bytes are
12085    generated for each source line, and therefore can use only the
12086    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12087    commands.  Accordingly, we fix this as `1', which is "correct
12088    enough" for all architectures, and don't let the target override.  */
12089   dw2_asm_output_data (1, 1,
12090                        "Minimum Instruction Length");
12091
12092   if (ver >= 4)
12093     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12094                          "Maximum Operations Per Instruction");
12095   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12096                        "Default is_stmt_start flag");
12097   dw2_asm_output_data (1, DWARF_LINE_BASE,
12098                        "Line Base Value (Special Opcodes)");
12099   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12100                        "Line Range Value (Special Opcodes)");
12101   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12102                        "Special Opcode Base");
12103
12104   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12105     {
12106       switch (opc)
12107         {
12108         case DW_LNS_advance_pc:
12109         case DW_LNS_advance_line:
12110         case DW_LNS_set_file:
12111         case DW_LNS_set_column:
12112         case DW_LNS_fixed_advance_pc:
12113           n_op_args = 1;
12114           break;
12115         default:
12116           n_op_args = 0;
12117           break;
12118         }
12119
12120       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12121                            opc, n_op_args);
12122     }
12123
12124   /* Write out the information about the files we use.  */
12125   output_file_names ();
12126   ASM_OUTPUT_LABEL (asm_out_file, p2);
12127
12128   /* We used to set the address register to the first location in the text
12129      section here, but that didn't accomplish anything since we already
12130      have a line note for the opening brace of the first function.  */
12131
12132   /* Generate the line number to PC correspondence table, encoded as
12133      a series of state machine operations.  */
12134   current_file = 1;
12135   current_line = 1;
12136
12137   if (cfun && in_cold_section_p)
12138     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12139   else
12140     strcpy (prev_line_label, text_section_label);
12141   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12142     {
12143       dw_line_info_ref line_info = &line_info_table[lt_index];
12144
12145 #if 0
12146       /* Disable this optimization for now; GDB wants to see two line notes
12147          at the beginning of a function so it can find the end of the
12148          prologue.  */
12149
12150       /* Don't emit anything for redundant notes.  Just updating the
12151          address doesn't accomplish anything, because we already assume
12152          that anything after the last address is this line.  */
12153       if (line_info->dw_line_num == current_line
12154           && line_info->dw_file_num == current_file)
12155         continue;
12156 #endif
12157
12158       /* Emit debug info for the address of the current line.
12159
12160          Unfortunately, we have little choice here currently, and must always
12161          use the most general form.  GCC does not know the address delta
12162          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12163          attributes which will give an upper bound on the address range.  We
12164          could perhaps use length attributes to determine when it is safe to
12165          use DW_LNS_fixed_advance_pc.  */
12166
12167       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12168       if (0)
12169         {
12170           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12171           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12172                                "DW_LNS_fixed_advance_pc");
12173           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12174         }
12175       else
12176         {
12177           /* This can handle any delta.  This takes
12178              4+DWARF2_ADDR_SIZE bytes.  */
12179           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12180           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12181           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12182           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12183         }
12184
12185       strcpy (prev_line_label, line_label);
12186
12187       /* Emit debug info for the source file of the current line, if
12188          different from the previous line.  */
12189       if (line_info->dw_file_num != current_file)
12190         {
12191           current_file = line_info->dw_file_num;
12192           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12193           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12194         }
12195
12196       /* Emit debug info for the current line number, choosing the encoding
12197          that uses the least amount of space.  */
12198       if (line_info->dw_line_num != current_line)
12199         {
12200           line_offset = line_info->dw_line_num - current_line;
12201           line_delta = line_offset - DWARF_LINE_BASE;
12202           current_line = line_info->dw_line_num;
12203           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12204             /* This can handle deltas from -10 to 234, using the current
12205                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12206                takes 1 byte.  */
12207             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12208                                  "line %lu", current_line);
12209           else
12210             {
12211               /* This can handle any delta.  This takes at least 4 bytes,
12212                  depending on the value being encoded.  */
12213               dw2_asm_output_data (1, DW_LNS_advance_line,
12214                                    "advance to line %lu", current_line);
12215               dw2_asm_output_data_sleb128 (line_offset, NULL);
12216               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12217             }
12218         }
12219       else
12220         /* We still need to start a new row, so output a copy insn.  */
12221         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12222     }
12223
12224   /* Emit debug info for the address of the end of the function.  */
12225   if (0)
12226     {
12227       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12228                            "DW_LNS_fixed_advance_pc");
12229       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12230     }
12231   else
12232     {
12233       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12234       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12235       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12236       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12237     }
12238
12239   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12240   dw2_asm_output_data_uleb128 (1, NULL);
12241   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12242
12243   function = 0;
12244   current_file = 1;
12245   current_line = 1;
12246   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12247     {
12248       dw_separate_line_info_ref line_info
12249         = &separate_line_info_table[lt_index];
12250
12251 #if 0
12252       /* Don't emit anything for redundant notes.  */
12253       if (line_info->dw_line_num == current_line
12254           && line_info->dw_file_num == current_file
12255           && line_info->function == function)
12256         goto cont;
12257 #endif
12258
12259       /* Emit debug info for the address of the current line.  If this is
12260          a new function, or the first line of a function, then we need
12261          to handle it differently.  */
12262       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12263                                    lt_index);
12264       if (function != line_info->function)
12265         {
12266           function = line_info->function;
12267
12268           /* Set the address register to the first line in the function.  */
12269           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12270           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12271           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12272           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12273         }
12274       else
12275         {
12276           /* ??? See the DW_LNS_advance_pc comment above.  */
12277           if (0)
12278             {
12279               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12280                                    "DW_LNS_fixed_advance_pc");
12281               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12282             }
12283           else
12284             {
12285               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12286               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12287               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12288               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12289             }
12290         }
12291
12292       strcpy (prev_line_label, line_label);
12293
12294       /* Emit debug info for the source file of the current line, if
12295          different from the previous line.  */
12296       if (line_info->dw_file_num != current_file)
12297         {
12298           current_file = line_info->dw_file_num;
12299           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12300           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12301         }
12302
12303       /* Emit debug info for the current line number, choosing the encoding
12304          that uses the least amount of space.  */
12305       if (line_info->dw_line_num != current_line)
12306         {
12307           line_offset = line_info->dw_line_num - current_line;
12308           line_delta = line_offset - DWARF_LINE_BASE;
12309           current_line = line_info->dw_line_num;
12310           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12311             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12312                                  "line %lu", current_line);
12313           else
12314             {
12315               dw2_asm_output_data (1, DW_LNS_advance_line,
12316                                    "advance to line %lu", current_line);
12317               dw2_asm_output_data_sleb128 (line_offset, NULL);
12318               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12319             }
12320         }
12321       else
12322         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12323
12324 #if 0
12325     cont:
12326 #endif
12327
12328       lt_index++;
12329
12330       /* If we're done with a function, end its sequence.  */
12331       if (lt_index == separate_line_info_table_in_use
12332           || separate_line_info_table[lt_index].function != function)
12333         {
12334           current_file = 1;
12335           current_line = 1;
12336
12337           /* Emit debug info for the address of the end of the function.  */
12338           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12339           if (0)
12340             {
12341               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12342                                    "DW_LNS_fixed_advance_pc");
12343               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12344             }
12345           else
12346             {
12347               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12348               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12349               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12350               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12351             }
12352
12353           /* Output the marker for the end of this sequence.  */
12354           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12355           dw2_asm_output_data_uleb128 (1, NULL);
12356           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12357         }
12358     }
12359
12360   /* Output the marker for the end of the line number info.  */
12361   ASM_OUTPUT_LABEL (asm_out_file, l2);
12362 }
12363
12364 /* Return the size of the .debug_dcall table for the compilation unit.  */
12365
12366 static unsigned long
12367 size_of_dcall_table (void)
12368 {
12369   unsigned long size;
12370   unsigned int i;
12371   dcall_entry *p;
12372   tree last_poc_decl = NULL;
12373
12374   /* Header:  version + debug info section pointer + pointer size.  */
12375   size = 2 + DWARF_OFFSET_SIZE + 1;
12376
12377   /* Each entry:  code label + DIE offset.  */
12378   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12379     {
12380       gcc_assert (p->targ_die != NULL);
12381       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12382       if (p->poc_decl != last_poc_decl)
12383         {
12384           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12385           gcc_assert (poc_die);
12386           last_poc_decl = p->poc_decl;
12387           if (poc_die)
12388             size += (DWARF_OFFSET_SIZE
12389                      + size_of_uleb128 (poc_die->die_offset));
12390         }
12391       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12392     }
12393
12394   return size;
12395 }
12396
12397 /* Output the direct call table used to disambiguate PC values when
12398    identical function have been merged.  */
12399
12400 static void
12401 output_dcall_table (void)
12402 {
12403   unsigned i;
12404   unsigned long dcall_length = size_of_dcall_table ();
12405   dcall_entry *p;
12406   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12407   tree last_poc_decl = NULL;
12408
12409   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12410     dw2_asm_output_data (4, 0xffffffff,
12411       "Initial length escape value indicating 64-bit DWARF extension");
12412   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12413                        "Length of Direct Call Table");
12414   dw2_asm_output_data (2, 4, "Version number");
12415   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12416                          debug_info_section,
12417                          "Offset of Compilation Unit Info");
12418   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12419
12420   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12421     {
12422       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12423       if (p->poc_decl != last_poc_decl)
12424         {
12425           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12426           last_poc_decl = p->poc_decl;
12427           if (poc_die)
12428             {
12429               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12430               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12431                                            "Caller DIE offset");
12432             }
12433         }
12434       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12435       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12436       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12437                                    "Callee DIE offset");
12438     }
12439 }
12440 \f
12441 /* Return the size of the .debug_vcall table for the compilation unit.  */
12442
12443 static unsigned long
12444 size_of_vcall_table (void)
12445 {
12446   unsigned long size;
12447   unsigned int i;
12448   vcall_entry *p;
12449
12450   /* Header:  version + pointer size.  */
12451   size = 2 + 1;
12452
12453   /* Each entry:  code label + vtable slot index.  */
12454   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12455     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12456
12457   return size;
12458 }
12459
12460 /* Output the virtual call table used to disambiguate PC values when
12461    identical function have been merged.  */
12462
12463 static void
12464 output_vcall_table (void)
12465 {
12466   unsigned i;
12467   unsigned long vcall_length = size_of_vcall_table ();
12468   vcall_entry *p;
12469   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12470
12471   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12472     dw2_asm_output_data (4, 0xffffffff,
12473       "Initial length escape value indicating 64-bit DWARF extension");
12474   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12475                        "Length of Virtual Call Table");
12476   dw2_asm_output_data (2, 4, "Version number");
12477   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12478
12479   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12480     {
12481       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12482       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12483       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12484     }
12485 }
12486 \f
12487 /* Given a pointer to a tree node for some base type, return a pointer to
12488    a DIE that describes the given type.
12489
12490    This routine must only be called for GCC type nodes that correspond to
12491    Dwarf base (fundamental) types.  */
12492
12493 static dw_die_ref
12494 base_type_die (tree type)
12495 {
12496   dw_die_ref base_type_result;
12497   enum dwarf_type encoding;
12498
12499   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12500     return 0;
12501
12502   /* If this is a subtype that should not be emitted as a subrange type,
12503      use the base type.  See subrange_type_for_debug_p.  */
12504   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12505     type = TREE_TYPE (type);
12506
12507   switch (TREE_CODE (type))
12508     {
12509     case INTEGER_TYPE:
12510       if ((dwarf_version >= 4 || !dwarf_strict)
12511           && TYPE_NAME (type)
12512           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12513           && DECL_IS_BUILTIN (TYPE_NAME (type))
12514           && DECL_NAME (TYPE_NAME (type)))
12515         {
12516           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12517           if (strcmp (name, "char16_t") == 0
12518               || strcmp (name, "char32_t") == 0)
12519             {
12520               encoding = DW_ATE_UTF;
12521               break;
12522             }
12523         }
12524       if (TYPE_STRING_FLAG (type))
12525         {
12526           if (TYPE_UNSIGNED (type))
12527             encoding = DW_ATE_unsigned_char;
12528           else
12529             encoding = DW_ATE_signed_char;
12530         }
12531       else if (TYPE_UNSIGNED (type))
12532         encoding = DW_ATE_unsigned;
12533       else
12534         encoding = DW_ATE_signed;
12535       break;
12536
12537     case REAL_TYPE:
12538       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12539         {
12540           if (dwarf_version >= 3 || !dwarf_strict)
12541             encoding = DW_ATE_decimal_float;
12542           else
12543             encoding = DW_ATE_lo_user;
12544         }
12545       else
12546         encoding = DW_ATE_float;
12547       break;
12548
12549     case FIXED_POINT_TYPE:
12550       if (!(dwarf_version >= 3 || !dwarf_strict))
12551         encoding = DW_ATE_lo_user;
12552       else if (TYPE_UNSIGNED (type))
12553         encoding = DW_ATE_unsigned_fixed;
12554       else
12555         encoding = DW_ATE_signed_fixed;
12556       break;
12557
12558       /* Dwarf2 doesn't know anything about complex ints, so use
12559          a user defined type for it.  */
12560     case COMPLEX_TYPE:
12561       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12562         encoding = DW_ATE_complex_float;
12563       else
12564         encoding = DW_ATE_lo_user;
12565       break;
12566
12567     case BOOLEAN_TYPE:
12568       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12569       encoding = DW_ATE_boolean;
12570       break;
12571
12572     default:
12573       /* No other TREE_CODEs are Dwarf fundamental types.  */
12574       gcc_unreachable ();
12575     }
12576
12577   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12578
12579   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12580                    int_size_in_bytes (type));
12581   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12582
12583   return base_type_result;
12584 }
12585
12586 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12587    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12588
12589 static inline int
12590 is_base_type (tree type)
12591 {
12592   switch (TREE_CODE (type))
12593     {
12594     case ERROR_MARK:
12595     case VOID_TYPE:
12596     case INTEGER_TYPE:
12597     case REAL_TYPE:
12598     case FIXED_POINT_TYPE:
12599     case COMPLEX_TYPE:
12600     case BOOLEAN_TYPE:
12601       return 1;
12602
12603     case ARRAY_TYPE:
12604     case RECORD_TYPE:
12605     case UNION_TYPE:
12606     case QUAL_UNION_TYPE:
12607     case ENUMERAL_TYPE:
12608     case FUNCTION_TYPE:
12609     case METHOD_TYPE:
12610     case POINTER_TYPE:
12611     case REFERENCE_TYPE:
12612     case OFFSET_TYPE:
12613     case LANG_TYPE:
12614     case VECTOR_TYPE:
12615       return 0;
12616
12617     default:
12618       gcc_unreachable ();
12619     }
12620
12621   return 0;
12622 }
12623
12624 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12625    node, return the size in bits for the type if it is a constant, or else
12626    return the alignment for the type if the type's size is not constant, or
12627    else return BITS_PER_WORD if the type actually turns out to be an
12628    ERROR_MARK node.  */
12629
12630 static inline unsigned HOST_WIDE_INT
12631 simple_type_size_in_bits (const_tree type)
12632 {
12633   if (TREE_CODE (type) == ERROR_MARK)
12634     return BITS_PER_WORD;
12635   else if (TYPE_SIZE (type) == NULL_TREE)
12636     return 0;
12637   else if (host_integerp (TYPE_SIZE (type), 1))
12638     return tree_low_cst (TYPE_SIZE (type), 1);
12639   else
12640     return TYPE_ALIGN (type);
12641 }
12642
12643 /* Similarly, but return a double_int instead of UHWI.  */
12644
12645 static inline double_int
12646 double_int_type_size_in_bits (const_tree type)
12647 {
12648   if (TREE_CODE (type) == ERROR_MARK)
12649     return uhwi_to_double_int (BITS_PER_WORD);
12650   else if (TYPE_SIZE (type) == NULL_TREE)
12651     return double_int_zero;
12652   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12653     return tree_to_double_int (TYPE_SIZE (type));
12654   else
12655     return uhwi_to_double_int (TYPE_ALIGN (type));
12656 }
12657
12658 /*  Given a pointer to a tree node for a subrange type, return a pointer
12659     to a DIE that describes the given type.  */
12660
12661 static dw_die_ref
12662 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12663 {
12664   dw_die_ref subrange_die;
12665   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12666
12667   if (context_die == NULL)
12668     context_die = comp_unit_die ();
12669
12670   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12671
12672   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12673     {
12674       /* The size of the subrange type and its base type do not match,
12675          so we need to generate a size attribute for the subrange type.  */
12676       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12677     }
12678
12679   if (low)
12680     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12681   if (high)
12682     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12683
12684   return subrange_die;
12685 }
12686
12687 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12688    entry that chains various modifiers in front of the given type.  */
12689
12690 static dw_die_ref
12691 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12692                    dw_die_ref context_die)
12693 {
12694   enum tree_code code = TREE_CODE (type);
12695   dw_die_ref mod_type_die;
12696   dw_die_ref sub_die = NULL;
12697   tree item_type = NULL;
12698   tree qualified_type;
12699   tree name, low, high;
12700
12701   if (code == ERROR_MARK)
12702     return NULL;
12703
12704   /* See if we already have the appropriately qualified variant of
12705      this type.  */
12706   qualified_type
12707     = get_qualified_type (type,
12708                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12709                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12710
12711   if (qualified_type == sizetype
12712       && TYPE_NAME (qualified_type)
12713       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12714     {
12715 #ifdef ENABLE_CHECKING
12716       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12717                   == INTEGER_TYPE
12718                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12719                      == TYPE_PRECISION (qualified_type)
12720                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12721                      == TYPE_UNSIGNED (qualified_type));
12722 #endif
12723       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12724     }
12725
12726   /* If we do, then we can just use its DIE, if it exists.  */
12727   if (qualified_type)
12728     {
12729       mod_type_die = lookup_type_die (qualified_type);
12730       if (mod_type_die)
12731         return mod_type_die;
12732     }
12733
12734   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12735
12736   /* Handle C typedef types.  */
12737   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12738       && !DECL_ARTIFICIAL (name))
12739     {
12740       tree dtype = TREE_TYPE (name);
12741
12742       if (qualified_type == dtype)
12743         {
12744           /* For a named type, use the typedef.  */
12745           gen_type_die (qualified_type, context_die);
12746           return lookup_type_die (qualified_type);
12747         }
12748       else if (is_const_type < TYPE_READONLY (dtype)
12749                || is_volatile_type < TYPE_VOLATILE (dtype)
12750                || (is_const_type <= TYPE_READONLY (dtype)
12751                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12752                    && DECL_ORIGINAL_TYPE (name) != type))
12753         /* cv-unqualified version of named type.  Just use the unnamed
12754            type to which it refers.  */
12755         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12756                                   is_const_type, is_volatile_type,
12757                                   context_die);
12758       /* Else cv-qualified version of named type; fall through.  */
12759     }
12760
12761   if (is_const_type)
12762     {
12763       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12764       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12765     }
12766   else if (is_volatile_type)
12767     {
12768       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12769       sub_die = modified_type_die (type, 0, 0, context_die);
12770     }
12771   else if (code == POINTER_TYPE)
12772     {
12773       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12774       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12775                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12776       item_type = TREE_TYPE (type);
12777       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12778         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12779                          TYPE_ADDR_SPACE (item_type));
12780     }
12781   else if (code == REFERENCE_TYPE)
12782     {
12783       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12784         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12785                                 type);
12786       else
12787         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12788       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12789                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12790       item_type = TREE_TYPE (type);
12791       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12792         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12793                          TYPE_ADDR_SPACE (item_type));
12794     }
12795   else if (code == INTEGER_TYPE
12796            && TREE_TYPE (type) != NULL_TREE
12797            && subrange_type_for_debug_p (type, &low, &high))
12798     {
12799       mod_type_die = subrange_type_die (type, low, high, context_die);
12800       item_type = TREE_TYPE (type);
12801     }
12802   else if (is_base_type (type))
12803     mod_type_die = base_type_die (type);
12804   else
12805     {
12806       gen_type_die (type, context_die);
12807
12808       /* We have to get the type_main_variant here (and pass that to the
12809          `lookup_type_die' routine) because the ..._TYPE node we have
12810          might simply be a *copy* of some original type node (where the
12811          copy was created to help us keep track of typedef names) and
12812          that copy might have a different TYPE_UID from the original
12813          ..._TYPE node.  */
12814       if (TREE_CODE (type) != VECTOR_TYPE)
12815         return lookup_type_die (type_main_variant (type));
12816       else
12817         /* Vectors have the debugging information in the type,
12818            not the main variant.  */
12819         return lookup_type_die (type);
12820     }
12821
12822   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12823      don't output a DW_TAG_typedef, since there isn't one in the
12824      user's program; just attach a DW_AT_name to the type.
12825      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12826      if the base type already has the same name.  */
12827   if (name
12828       && ((TREE_CODE (name) != TYPE_DECL
12829            && (qualified_type == TYPE_MAIN_VARIANT (type)
12830                || (!is_const_type && !is_volatile_type)))
12831           || (TREE_CODE (name) == TYPE_DECL
12832               && TREE_TYPE (name) == qualified_type
12833               && DECL_NAME (name))))
12834     {
12835       if (TREE_CODE (name) == TYPE_DECL)
12836         /* Could just call add_name_and_src_coords_attributes here,
12837            but since this is a builtin type it doesn't have any
12838            useful source coordinates anyway.  */
12839         name = DECL_NAME (name);
12840       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12841     }
12842   /* This probably indicates a bug.  */
12843   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12844     add_name_attribute (mod_type_die, "__unknown__");
12845
12846   if (qualified_type)
12847     equate_type_number_to_die (qualified_type, mod_type_die);
12848
12849   if (item_type)
12850     /* We must do this after the equate_type_number_to_die call, in case
12851        this is a recursive type.  This ensures that the modified_type_die
12852        recursion will terminate even if the type is recursive.  Recursive
12853        types are possible in Ada.  */
12854     sub_die = modified_type_die (item_type,
12855                                  TYPE_READONLY (item_type),
12856                                  TYPE_VOLATILE (item_type),
12857                                  context_die);
12858
12859   if (sub_die != NULL)
12860     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12861
12862   return mod_type_die;
12863 }
12864
12865 /* Generate DIEs for the generic parameters of T.
12866    T must be either a generic type or a generic function.
12867    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12868
12869 static void
12870 gen_generic_params_dies (tree t)
12871 {
12872   tree parms, args;
12873   int parms_num, i;
12874   dw_die_ref die = NULL;
12875
12876   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12877     return;
12878
12879   if (TYPE_P (t))
12880     die = lookup_type_die (t);
12881   else if (DECL_P (t))
12882     die = lookup_decl_die (t);
12883
12884   gcc_assert (die);
12885
12886   parms = lang_hooks.get_innermost_generic_parms (t);
12887   if (!parms)
12888     /* T has no generic parameter. It means T is neither a generic type
12889        or function. End of story.  */
12890     return;
12891
12892   parms_num = TREE_VEC_LENGTH (parms);
12893   args = lang_hooks.get_innermost_generic_args (t);
12894   for (i = 0; i < parms_num; i++)
12895     {
12896       tree parm, arg, arg_pack_elems;
12897
12898       parm = TREE_VEC_ELT (parms, i);
12899       arg = TREE_VEC_ELT (args, i);
12900       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12901       gcc_assert (parm && TREE_VALUE (parm) && arg);
12902
12903       if (parm && TREE_VALUE (parm) && arg)
12904         {
12905           /* If PARM represents a template parameter pack,
12906              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12907              by DW_TAG_template_*_parameter DIEs for the argument
12908              pack elements of ARG. Note that ARG would then be
12909              an argument pack.  */
12910           if (arg_pack_elems)
12911             template_parameter_pack_die (TREE_VALUE (parm),
12912                                          arg_pack_elems,
12913                                          die);
12914           else
12915             generic_parameter_die (TREE_VALUE (parm), arg,
12916                                    true /* Emit DW_AT_name */, die);
12917         }
12918     }
12919 }
12920
12921 /* Create and return a DIE for PARM which should be
12922    the representation of a generic type parameter.
12923    For instance, in the C++ front end, PARM would be a template parameter.
12924    ARG is the argument to PARM.
12925    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12926    name of the PARM.
12927    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12928    as a child node.  */
12929
12930 static dw_die_ref
12931 generic_parameter_die (tree parm, tree arg,
12932                        bool emit_name_p,
12933                        dw_die_ref parent_die)
12934 {
12935   dw_die_ref tmpl_die = NULL;
12936   const char *name = NULL;
12937
12938   if (!parm || !DECL_NAME (parm) || !arg)
12939     return NULL;
12940
12941   /* We support non-type generic parameters and arguments,
12942      type generic parameters and arguments, as well as
12943      generic generic parameters (a.k.a. template template parameters in C++)
12944      and arguments.  */
12945   if (TREE_CODE (parm) == PARM_DECL)
12946     /* PARM is a nontype generic parameter  */
12947     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12948   else if (TREE_CODE (parm) == TYPE_DECL)
12949     /* PARM is a type generic parameter.  */
12950     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12951   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12952     /* PARM is a generic generic parameter.
12953        Its DIE is a GNU extension. It shall have a
12954        DW_AT_name attribute to represent the name of the template template
12955        parameter, and a DW_AT_GNU_template_name attribute to represent the
12956        name of the template template argument.  */
12957     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12958                         parent_die, parm);
12959   else
12960     gcc_unreachable ();
12961
12962   if (tmpl_die)
12963     {
12964       tree tmpl_type;
12965
12966       /* If PARM is a generic parameter pack, it means we are
12967          emitting debug info for a template argument pack element.
12968          In other terms, ARG is a template argument pack element.
12969          In that case, we don't emit any DW_AT_name attribute for
12970          the die.  */
12971       if (emit_name_p)
12972         {
12973           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12974           gcc_assert (name);
12975           add_AT_string (tmpl_die, DW_AT_name, name);
12976         }
12977
12978       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12979         {
12980           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12981              TMPL_DIE should have a child DW_AT_type attribute that is set
12982              to the type of the argument to PARM, which is ARG.
12983              If PARM is a type generic parameter, TMPL_DIE should have a
12984              child DW_AT_type that is set to ARG.  */
12985           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12986           add_type_attribute (tmpl_die, tmpl_type, 0,
12987                               TREE_THIS_VOLATILE (tmpl_type),
12988                               parent_die);
12989         }
12990       else
12991         {
12992           /* So TMPL_DIE is a DIE representing a
12993              a generic generic template parameter, a.k.a template template
12994              parameter in C++ and arg is a template.  */
12995
12996           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12997              to the name of the argument.  */
12998           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12999           if (name)
13000             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13001         }
13002
13003       if (TREE_CODE (parm) == PARM_DECL)
13004         /* So PARM is a non-type generic parameter.
13005            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13006            attribute of TMPL_DIE which value represents the value
13007            of ARG.
13008            We must be careful here:
13009            The value of ARG might reference some function decls.
13010            We might currently be emitting debug info for a generic
13011            type and types are emitted before function decls, we don't
13012            know if the function decls referenced by ARG will actually be
13013            emitted after cgraph computations.
13014            So must defer the generation of the DW_AT_const_value to
13015            after cgraph is ready.  */
13016         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13017     }
13018
13019   return tmpl_die;
13020 }
13021
13022 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13023    PARM_PACK must be a template parameter pack. The returned DIE
13024    will be child DIE of PARENT_DIE.  */
13025
13026 static dw_die_ref
13027 template_parameter_pack_die (tree parm_pack,
13028                              tree parm_pack_args,
13029                              dw_die_ref parent_die)
13030 {
13031   dw_die_ref die;
13032   int j;
13033
13034   gcc_assert (parent_die && parm_pack);
13035
13036   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13037   add_name_and_src_coords_attributes (die, parm_pack);
13038   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13039     generic_parameter_die (parm_pack,
13040                            TREE_VEC_ELT (parm_pack_args, j),
13041                            false /* Don't emit DW_AT_name */,
13042                            die);
13043   return die;
13044 }
13045
13046 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13047    an enumerated type.  */
13048
13049 static inline int
13050 type_is_enum (const_tree type)
13051 {
13052   return TREE_CODE (type) == ENUMERAL_TYPE;
13053 }
13054
13055 /* Return the DBX register number described by a given RTL node.  */
13056
13057 static unsigned int
13058 dbx_reg_number (const_rtx rtl)
13059 {
13060   unsigned regno = REGNO (rtl);
13061
13062   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13063
13064 #ifdef LEAF_REG_REMAP
13065   if (current_function_uses_only_leaf_regs)
13066     {
13067       int leaf_reg = LEAF_REG_REMAP (regno);
13068       if (leaf_reg != -1)
13069         regno = (unsigned) leaf_reg;
13070     }
13071 #endif
13072
13073   return DBX_REGISTER_NUMBER (regno);
13074 }
13075
13076 /* Optionally add a DW_OP_piece term to a location description expression.
13077    DW_OP_piece is only added if the location description expression already
13078    doesn't end with DW_OP_piece.  */
13079
13080 static void
13081 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13082 {
13083   dw_loc_descr_ref loc;
13084
13085   if (*list_head != NULL)
13086     {
13087       /* Find the end of the chain.  */
13088       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13089         ;
13090
13091       if (loc->dw_loc_opc != DW_OP_piece)
13092         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13093     }
13094 }
13095
13096 /* Return a location descriptor that designates a machine register or
13097    zero if there is none.  */
13098
13099 static dw_loc_descr_ref
13100 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13101 {
13102   rtx regs;
13103
13104   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13105     return 0;
13106
13107   /* We only use "frame base" when we're sure we're talking about the
13108      post-prologue local stack frame.  We do this by *not* running
13109      register elimination until this point, and recognizing the special
13110      argument pointer and soft frame pointer rtx's.
13111      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13112   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13113       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13114     {
13115       dw_loc_descr_ref result = NULL;
13116
13117       if (dwarf_version >= 4 || !dwarf_strict)
13118         {
13119           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13120           if (result)
13121             add_loc_descr (&result,
13122                            new_loc_descr (DW_OP_stack_value, 0, 0));
13123         }
13124       return result;
13125     }
13126
13127   regs = targetm.dwarf_register_span (rtl);
13128
13129   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13130     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13131   else
13132     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13133 }
13134
13135 /* Return a location descriptor that designates a machine register for
13136    a given hard register number.  */
13137
13138 static dw_loc_descr_ref
13139 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13140 {
13141   dw_loc_descr_ref reg_loc_descr;
13142
13143   if (regno <= 31)
13144     reg_loc_descr
13145       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13146   else
13147     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13148
13149   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13150     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13151
13152   return reg_loc_descr;
13153 }
13154
13155 /* Given an RTL of a register, return a location descriptor that
13156    designates a value that spans more than one register.  */
13157
13158 static dw_loc_descr_ref
13159 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13160                              enum var_init_status initialized)
13161 {
13162   int nregs, size, i;
13163   unsigned reg;
13164   dw_loc_descr_ref loc_result = NULL;
13165
13166   reg = REGNO (rtl);
13167 #ifdef LEAF_REG_REMAP
13168   if (current_function_uses_only_leaf_regs)
13169     {
13170       int leaf_reg = LEAF_REG_REMAP (reg);
13171       if (leaf_reg != -1)
13172         reg = (unsigned) leaf_reg;
13173     }
13174 #endif
13175   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13176   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13177
13178   /* Simple, contiguous registers.  */
13179   if (regs == NULL_RTX)
13180     {
13181       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13182
13183       loc_result = NULL;
13184       while (nregs--)
13185         {
13186           dw_loc_descr_ref t;
13187
13188           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13189                                       VAR_INIT_STATUS_INITIALIZED);
13190           add_loc_descr (&loc_result, t);
13191           add_loc_descr_op_piece (&loc_result, size);
13192           ++reg;
13193         }
13194       return loc_result;
13195     }
13196
13197   /* Now onto stupid register sets in non contiguous locations.  */
13198
13199   gcc_assert (GET_CODE (regs) == PARALLEL);
13200
13201   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13202   loc_result = NULL;
13203
13204   for (i = 0; i < XVECLEN (regs, 0); ++i)
13205     {
13206       dw_loc_descr_ref t;
13207
13208       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13209                                   VAR_INIT_STATUS_INITIALIZED);
13210       add_loc_descr (&loc_result, t);
13211       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13212       add_loc_descr_op_piece (&loc_result, size);
13213     }
13214
13215   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13216     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13217   return loc_result;
13218 }
13219
13220 /* Return a location descriptor that designates a constant.  */
13221
13222 static dw_loc_descr_ref
13223 int_loc_descriptor (HOST_WIDE_INT i)
13224 {
13225   enum dwarf_location_atom op;
13226
13227   /* Pick the smallest representation of a constant, rather than just
13228      defaulting to the LEB encoding.  */
13229   if (i >= 0)
13230     {
13231       if (i <= 31)
13232         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13233       else if (i <= 0xff)
13234         op = DW_OP_const1u;
13235       else if (i <= 0xffff)
13236         op = DW_OP_const2u;
13237       else if (HOST_BITS_PER_WIDE_INT == 32
13238                || i <= 0xffffffff)
13239         op = DW_OP_const4u;
13240       else
13241         op = DW_OP_constu;
13242     }
13243   else
13244     {
13245       if (i >= -0x80)
13246         op = DW_OP_const1s;
13247       else if (i >= -0x8000)
13248         op = DW_OP_const2s;
13249       else if (HOST_BITS_PER_WIDE_INT == 32
13250                || i >= -0x80000000)
13251         op = DW_OP_const4s;
13252       else
13253         op = DW_OP_consts;
13254     }
13255
13256   return new_loc_descr (op, i, 0);
13257 }
13258
13259 /* Return loc description representing "address" of integer value.
13260    This can appear only as toplevel expression.  */
13261
13262 static dw_loc_descr_ref
13263 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13264 {
13265   int litsize;
13266   dw_loc_descr_ref loc_result = NULL;
13267
13268   if (!(dwarf_version >= 4 || !dwarf_strict))
13269     return NULL;
13270
13271   if (i >= 0)
13272     {
13273       if (i <= 31)
13274         litsize = 1;
13275       else if (i <= 0xff)
13276         litsize = 2;
13277       else if (i <= 0xffff)
13278         litsize = 3;
13279       else if (HOST_BITS_PER_WIDE_INT == 32
13280                || i <= 0xffffffff)
13281         litsize = 5;
13282       else
13283         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13284     }
13285   else
13286     {
13287       if (i >= -0x80)
13288         litsize = 2;
13289       else if (i >= -0x8000)
13290         litsize = 3;
13291       else if (HOST_BITS_PER_WIDE_INT == 32
13292                || i >= -0x80000000)
13293         litsize = 5;
13294       else
13295         litsize = 1 + size_of_sleb128 (i);
13296     }
13297   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13298      is more compact.  For DW_OP_stack_value we need:
13299      litsize + 1 (DW_OP_stack_value)
13300      and for DW_OP_implicit_value:
13301      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13302   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13303     {
13304       loc_result = int_loc_descriptor (i);
13305       add_loc_descr (&loc_result,
13306                      new_loc_descr (DW_OP_stack_value, 0, 0));
13307       return loc_result;
13308     }
13309
13310   loc_result = new_loc_descr (DW_OP_implicit_value,
13311                               size, 0);
13312   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13313   loc_result->dw_loc_oprnd2.v.val_int = i;
13314   return loc_result;
13315 }
13316
13317 /* Return a location descriptor that designates a base+offset location.  */
13318
13319 static dw_loc_descr_ref
13320 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13321                  enum var_init_status initialized)
13322 {
13323   unsigned int regno;
13324   dw_loc_descr_ref result;
13325   dw_fde_ref fde = current_fde ();
13326
13327   /* We only use "frame base" when we're sure we're talking about the
13328      post-prologue local stack frame.  We do this by *not* running
13329      register elimination until this point, and recognizing the special
13330      argument pointer and soft frame pointer rtx's.  */
13331   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13332     {
13333       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13334
13335       if (elim != reg)
13336         {
13337           if (GET_CODE (elim) == PLUS)
13338             {
13339               offset += INTVAL (XEXP (elim, 1));
13340               elim = XEXP (elim, 0);
13341             }
13342           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13343                        && (elim == hard_frame_pointer_rtx
13344                            || elim == stack_pointer_rtx))
13345                       || elim == (frame_pointer_needed
13346                                   ? hard_frame_pointer_rtx
13347                                   : stack_pointer_rtx));
13348
13349           /* If drap register is used to align stack, use frame
13350              pointer + offset to access stack variables.  If stack
13351              is aligned without drap, use stack pointer + offset to
13352              access stack variables.  */
13353           if (crtl->stack_realign_tried
13354               && reg == frame_pointer_rtx)
13355             {
13356               int base_reg
13357                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13358                                       ? HARD_FRAME_POINTER_REGNUM
13359                                       : STACK_POINTER_REGNUM);
13360               return new_reg_loc_descr (base_reg, offset);
13361             }
13362
13363           offset += frame_pointer_fb_offset;
13364           return new_loc_descr (DW_OP_fbreg, offset, 0);
13365         }
13366     }
13367   else if (!optimize
13368            && fde
13369            && (fde->drap_reg == REGNO (reg)
13370                || fde->vdrap_reg == REGNO (reg)))
13371     {
13372       /* Use cfa+offset to represent the location of arguments passed
13373          on the stack when drap is used to align stack.
13374          Only do this when not optimizing, for optimized code var-tracking
13375          is supposed to track where the arguments live and the register
13376          used as vdrap or drap in some spot might be used for something
13377          else in other part of the routine.  */
13378       return new_loc_descr (DW_OP_fbreg, offset, 0);
13379     }
13380
13381   regno = dbx_reg_number (reg);
13382   if (regno <= 31)
13383     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13384                             offset, 0);
13385   else
13386     result = new_loc_descr (DW_OP_bregx, regno, offset);
13387
13388   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13389     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13390
13391   return result;
13392 }
13393
13394 /* Return true if this RTL expression describes a base+offset calculation.  */
13395
13396 static inline int
13397 is_based_loc (const_rtx rtl)
13398 {
13399   return (GET_CODE (rtl) == PLUS
13400           && ((REG_P (XEXP (rtl, 0))
13401                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13402                && CONST_INT_P (XEXP (rtl, 1)))));
13403 }
13404
13405 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13406    failed.  */
13407
13408 static dw_loc_descr_ref
13409 tls_mem_loc_descriptor (rtx mem)
13410 {
13411   tree base;
13412   dw_loc_descr_ref loc_result;
13413
13414   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13415     return NULL;
13416
13417   base = get_base_address (MEM_EXPR (mem));
13418   if (base == NULL
13419       || TREE_CODE (base) != VAR_DECL
13420       || !DECL_THREAD_LOCAL_P (base))
13421     return NULL;
13422
13423   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13424   if (loc_result == NULL)
13425     return NULL;
13426
13427   if (INTVAL (MEM_OFFSET (mem)))
13428     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13429
13430   return loc_result;
13431 }
13432
13433 /* Output debug info about reason why we failed to expand expression as dwarf
13434    expression.  */
13435
13436 static void
13437 expansion_failed (tree expr, rtx rtl, char const *reason)
13438 {
13439   if (dump_file && (dump_flags & TDF_DETAILS))
13440     {
13441       fprintf (dump_file, "Failed to expand as dwarf: ");
13442       if (expr)
13443         print_generic_expr (dump_file, expr, dump_flags);
13444       if (rtl)
13445         {
13446           fprintf (dump_file, "\n");
13447           print_rtl (dump_file, rtl);
13448         }
13449       fprintf (dump_file, "\nReason: %s\n", reason);
13450     }
13451 }
13452
13453 /* Helper function for const_ok_for_output, called either directly
13454    or via for_each_rtx.  */
13455
13456 static int
13457 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13458 {
13459   rtx rtl = *rtlp;
13460
13461   if (GET_CODE (rtl) == UNSPEC)
13462     {
13463       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13464          we can't express it in the debug info.  */
13465 #ifdef ENABLE_CHECKING
13466       inform (current_function_decl
13467               ? DECL_SOURCE_LOCATION (current_function_decl)
13468               : UNKNOWN_LOCATION,
13469               "non-delegitimized UNSPEC %d found in variable location",
13470               XINT (rtl, 1));
13471 #endif
13472       expansion_failed (NULL_TREE, rtl,
13473                         "UNSPEC hasn't been delegitimized.\n");
13474       return 1;
13475     }
13476
13477   if (GET_CODE (rtl) != SYMBOL_REF)
13478     return 0;
13479
13480   if (CONSTANT_POOL_ADDRESS_P (rtl))
13481     {
13482       bool marked;
13483       get_pool_constant_mark (rtl, &marked);
13484       /* If all references to this pool constant were optimized away,
13485          it was not output and thus we can't represent it.  */
13486       if (!marked)
13487         {
13488           expansion_failed (NULL_TREE, rtl,
13489                             "Constant was removed from constant pool.\n");
13490           return 1;
13491         }
13492     }
13493
13494   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13495     return 1;
13496
13497   /* Avoid references to external symbols in debug info, on several targets
13498      the linker might even refuse to link when linking a shared library,
13499      and in many other cases the relocations for .debug_info/.debug_loc are
13500      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13501      to be defined within the same shared library or executable are fine.  */
13502   if (SYMBOL_REF_EXTERNAL_P (rtl))
13503     {
13504       tree decl = SYMBOL_REF_DECL (rtl);
13505
13506       if (decl == NULL || !targetm.binds_local_p (decl))
13507         {
13508           expansion_failed (NULL_TREE, rtl,
13509                             "Symbol not defined in current TU.\n");
13510           return 1;
13511         }
13512     }
13513
13514   return 0;
13515 }
13516
13517 /* Return true if constant RTL can be emitted in DW_OP_addr or
13518    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13519    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13520
13521 static bool
13522 const_ok_for_output (rtx rtl)
13523 {
13524   if (GET_CODE (rtl) == SYMBOL_REF)
13525     return const_ok_for_output_1 (&rtl, NULL) == 0;
13526
13527   if (GET_CODE (rtl) == CONST)
13528     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13529
13530   return true;
13531 }
13532
13533 /* The following routine converts the RTL for a variable or parameter
13534    (resident in memory) into an equivalent Dwarf representation of a
13535    mechanism for getting the address of that same variable onto the top of a
13536    hypothetical "address evaluation" stack.
13537
13538    When creating memory location descriptors, we are effectively transforming
13539    the RTL for a memory-resident object into its Dwarf postfix expression
13540    equivalent.  This routine recursively descends an RTL tree, turning
13541    it into Dwarf postfix code as it goes.
13542
13543    MODE is the mode of the memory reference, needed to handle some
13544    autoincrement addressing modes.
13545
13546    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13547    location list for RTL.
13548
13549    Return 0 if we can't represent the location.  */
13550
13551 static dw_loc_descr_ref
13552 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13553                     enum var_init_status initialized)
13554 {
13555   dw_loc_descr_ref mem_loc_result = NULL;
13556   enum dwarf_location_atom op;
13557   dw_loc_descr_ref op0, op1;
13558
13559   /* Note that for a dynamically sized array, the location we will generate a
13560      description of here will be the lowest numbered location which is
13561      actually within the array.  That's *not* necessarily the same as the
13562      zeroth element of the array.  */
13563
13564   rtl = targetm.delegitimize_address (rtl);
13565
13566   switch (GET_CODE (rtl))
13567     {
13568     case POST_INC:
13569     case POST_DEC:
13570     case POST_MODIFY:
13571       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13572
13573     case SUBREG:
13574       /* The case of a subreg may arise when we have a local (register)
13575          variable or a formal (register) parameter which doesn't quite fill
13576          up an entire register.  For now, just assume that it is
13577          legitimate to make the Dwarf info refer to the whole register which
13578          contains the given subreg.  */
13579       if (!subreg_lowpart_p (rtl))
13580         break;
13581       rtl = SUBREG_REG (rtl);
13582       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13583         break;
13584       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13585         break;
13586       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13587       break;
13588
13589     case REG:
13590       /* Whenever a register number forms a part of the description of the
13591          method for calculating the (dynamic) address of a memory resident
13592          object, DWARF rules require the register number be referred to as
13593          a "base register".  This distinction is not based in any way upon
13594          what category of register the hardware believes the given register
13595          belongs to.  This is strictly DWARF terminology we're dealing with
13596          here. Note that in cases where the location of a memory-resident
13597          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13598          OP_CONST (0)) the actual DWARF location descriptor that we generate
13599          may just be OP_BASEREG (basereg).  This may look deceptively like
13600          the object in question was allocated to a register (rather than in
13601          memory) so DWARF consumers need to be aware of the subtle
13602          distinction between OP_REG and OP_BASEREG.  */
13603       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13604         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13605       else if (stack_realign_drap
13606                && crtl->drap_reg
13607                && crtl->args.internal_arg_pointer == rtl
13608                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13609         {
13610           /* If RTL is internal_arg_pointer, which has been optimized
13611              out, use DRAP instead.  */
13612           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13613                                             VAR_INIT_STATUS_INITIALIZED);
13614         }
13615       break;
13616
13617     case SIGN_EXTEND:
13618     case ZERO_EXTEND:
13619       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13620                                 VAR_INIT_STATUS_INITIALIZED);
13621       if (op0 == 0)
13622         break;
13623       else
13624         {
13625           int shift = DWARF2_ADDR_SIZE
13626                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13627           shift *= BITS_PER_UNIT;
13628           if (GET_CODE (rtl) == SIGN_EXTEND)
13629             op = DW_OP_shra;
13630           else
13631             op = DW_OP_shr;
13632           mem_loc_result = op0;
13633           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13634           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13635           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13636           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13637         }
13638       break;
13639
13640     case MEM:
13641       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13642                                            VAR_INIT_STATUS_INITIALIZED);
13643       if (mem_loc_result == NULL)
13644         mem_loc_result = tls_mem_loc_descriptor (rtl);
13645       if (mem_loc_result != 0)
13646         {
13647           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13648             {
13649               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13650               return 0;
13651             }
13652           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13653             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13654           else
13655             add_loc_descr (&mem_loc_result,
13656                            new_loc_descr (DW_OP_deref_size,
13657                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13658         }
13659       else
13660         {
13661           rtx new_rtl = avoid_constant_pool_reference (rtl);
13662           if (new_rtl != rtl)
13663             return mem_loc_descriptor (new_rtl, mode, initialized);
13664         }
13665       break;
13666
13667     case LO_SUM:
13668          rtl = XEXP (rtl, 1);
13669
13670       /* ... fall through ...  */
13671
13672     case LABEL_REF:
13673       /* Some ports can transform a symbol ref into a label ref, because
13674          the symbol ref is too far away and has to be dumped into a constant
13675          pool.  */
13676     case CONST:
13677     case SYMBOL_REF:
13678       if (GET_CODE (rtl) == SYMBOL_REF
13679           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13680         {
13681           dw_loc_descr_ref temp;
13682
13683           /* If this is not defined, we have no way to emit the data.  */
13684           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13685             break;
13686
13687           /* We used to emit DW_OP_addr here, but that's wrong, since
13688              DW_OP_addr should be relocated by the debug info consumer,
13689              while DW_OP_GNU_push_tls_address operand should not.  */
13690           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13691                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13692           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13693           temp->dw_loc_oprnd1.v.val_addr = rtl;
13694           temp->dtprel = true;
13695
13696           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13697           add_loc_descr (&mem_loc_result, temp);
13698
13699           break;
13700         }
13701
13702       if (!const_ok_for_output (rtl))
13703         break;
13704
13705     symref:
13706       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13707       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13708       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13709       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13710       break;
13711
13712     case CONCAT:
13713     case CONCATN:
13714     case VAR_LOCATION:
13715     case DEBUG_IMPLICIT_PTR:
13716       expansion_failed (NULL_TREE, rtl,
13717                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13718       return 0;
13719
13720     case PRE_MODIFY:
13721       /* Extract the PLUS expression nested inside and fall into
13722          PLUS code below.  */
13723       rtl = XEXP (rtl, 1);
13724       goto plus;
13725
13726     case PRE_INC:
13727     case PRE_DEC:
13728       /* Turn these into a PLUS expression and fall into the PLUS code
13729          below.  */
13730       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13731                           GEN_INT (GET_CODE (rtl) == PRE_INC
13732                                    ? GET_MODE_UNIT_SIZE (mode)
13733                                    : -GET_MODE_UNIT_SIZE (mode)));
13734
13735       /* ... fall through ...  */
13736
13737     case PLUS:
13738     plus:
13739       if (is_based_loc (rtl))
13740         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13741                                           INTVAL (XEXP (rtl, 1)),
13742                                           VAR_INIT_STATUS_INITIALIZED);
13743       else
13744         {
13745           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13746                                                VAR_INIT_STATUS_INITIALIZED);
13747           if (mem_loc_result == 0)
13748             break;
13749
13750           if (CONST_INT_P (XEXP (rtl, 1)))
13751             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13752           else
13753             {
13754               dw_loc_descr_ref mem_loc_result2
13755                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13756                                       VAR_INIT_STATUS_INITIALIZED);
13757               if (mem_loc_result2 == 0)
13758                 break;
13759               add_loc_descr (&mem_loc_result, mem_loc_result2);
13760               add_loc_descr (&mem_loc_result,
13761                              new_loc_descr (DW_OP_plus, 0, 0));
13762             }
13763         }
13764       break;
13765
13766     /* If a pseudo-reg is optimized away, it is possible for it to
13767        be replaced with a MEM containing a multiply or shift.  */
13768     case MINUS:
13769       op = DW_OP_minus;
13770       goto do_binop;
13771
13772     case MULT:
13773       op = DW_OP_mul;
13774       goto do_binop;
13775
13776     case DIV:
13777       op = DW_OP_div;
13778       goto do_binop;
13779
13780     case UMOD:
13781       op = DW_OP_mod;
13782       goto do_binop;
13783
13784     case ASHIFT:
13785       op = DW_OP_shl;
13786       goto do_binop;
13787
13788     case ASHIFTRT:
13789       op = DW_OP_shra;
13790       goto do_binop;
13791
13792     case LSHIFTRT:
13793       op = DW_OP_shr;
13794       goto do_binop;
13795
13796     case AND:
13797       op = DW_OP_and;
13798       goto do_binop;
13799
13800     case IOR:
13801       op = DW_OP_or;
13802       goto do_binop;
13803
13804     case XOR:
13805       op = DW_OP_xor;
13806       goto do_binop;
13807
13808     do_binop:
13809       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13810                                 VAR_INIT_STATUS_INITIALIZED);
13811       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13812                                 VAR_INIT_STATUS_INITIALIZED);
13813
13814       if (op0 == 0 || op1 == 0)
13815         break;
13816
13817       mem_loc_result = op0;
13818       add_loc_descr (&mem_loc_result, op1);
13819       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13820       break;
13821
13822     case MOD:
13823       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13824                                 VAR_INIT_STATUS_INITIALIZED);
13825       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13826                                 VAR_INIT_STATUS_INITIALIZED);
13827
13828       if (op0 == 0 || op1 == 0)
13829         break;
13830
13831       mem_loc_result = op0;
13832       add_loc_descr (&mem_loc_result, op1);
13833       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13834       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13835       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13836       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13837       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13838       break;
13839
13840     case NOT:
13841       op = DW_OP_not;
13842       goto do_unop;
13843
13844     case ABS:
13845       op = DW_OP_abs;
13846       goto do_unop;
13847
13848     case NEG:
13849       op = DW_OP_neg;
13850       goto do_unop;
13851
13852     do_unop:
13853       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13854                                 VAR_INIT_STATUS_INITIALIZED);
13855
13856       if (op0 == 0)
13857         break;
13858
13859       mem_loc_result = op0;
13860       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13861       break;
13862
13863     case CONST_INT:
13864       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13865       break;
13866
13867     case EQ:
13868       op = DW_OP_eq;
13869       goto do_scompare;
13870
13871     case GE:
13872       op = DW_OP_ge;
13873       goto do_scompare;
13874
13875     case GT:
13876       op = DW_OP_gt;
13877       goto do_scompare;
13878
13879     case LE:
13880       op = DW_OP_le;
13881       goto do_scompare;
13882
13883     case LT:
13884       op = DW_OP_lt;
13885       goto do_scompare;
13886
13887     case NE:
13888       op = DW_OP_ne;
13889       goto do_scompare;
13890
13891     do_scompare:
13892       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13893           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13894         break;
13895       else
13896         {
13897           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13898
13899           if (op_mode == VOIDmode)
13900             op_mode = GET_MODE (XEXP (rtl, 1));
13901           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13902             break;
13903
13904           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13905                                     VAR_INIT_STATUS_INITIALIZED);
13906           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13907                                     VAR_INIT_STATUS_INITIALIZED);
13908
13909           if (op0 == 0 || op1 == 0)
13910             break;
13911
13912           if (op_mode != VOIDmode
13913               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13914             {
13915               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13916               shift *= BITS_PER_UNIT;
13917               /* For eq/ne, if the operands are known to be zero-extended,
13918                  there is no need to do the fancy shifting up.  */
13919               if (op == DW_OP_eq || op == DW_OP_ne)
13920                 {
13921                   dw_loc_descr_ref last0, last1;
13922                   for (last0 = op0;
13923                        last0->dw_loc_next != NULL;
13924                        last0 = last0->dw_loc_next)
13925                     ;
13926                   for (last1 = op1;
13927                        last1->dw_loc_next != NULL;
13928                        last1 = last1->dw_loc_next)
13929                     ;
13930                   /* deref_size zero extends, and for constants we can check
13931                      whether they are zero extended or not.  */
13932                   if (((last0->dw_loc_opc == DW_OP_deref_size
13933                         && last0->dw_loc_oprnd1.v.val_int
13934                            <= GET_MODE_SIZE (op_mode))
13935                        || (CONST_INT_P (XEXP (rtl, 0))
13936                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13937                                == (INTVAL (XEXP (rtl, 0))
13938                                    & GET_MODE_MASK (op_mode))))
13939                       && ((last1->dw_loc_opc == DW_OP_deref_size
13940                            && last1->dw_loc_oprnd1.v.val_int
13941                               <= GET_MODE_SIZE (op_mode))
13942                           || (CONST_INT_P (XEXP (rtl, 1))
13943                               && (unsigned HOST_WIDE_INT)
13944                                  INTVAL (XEXP (rtl, 1))
13945                                  == (INTVAL (XEXP (rtl, 1))
13946                                      & GET_MODE_MASK (op_mode)))))
13947                     goto do_compare;
13948                 }
13949               add_loc_descr (&op0, int_loc_descriptor (shift));
13950               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13951               if (CONST_INT_P (XEXP (rtl, 1)))
13952                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13953               else
13954                 {
13955                   add_loc_descr (&op1, int_loc_descriptor (shift));
13956                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13957                 }
13958             }
13959         }
13960
13961     do_compare:
13962       mem_loc_result = op0;
13963       add_loc_descr (&mem_loc_result, op1);
13964       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13965       if (STORE_FLAG_VALUE != 1)
13966         {
13967           add_loc_descr (&mem_loc_result,
13968                          int_loc_descriptor (STORE_FLAG_VALUE));
13969           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13970         }
13971       break;
13972
13973     case GEU:
13974       op = DW_OP_ge;
13975       goto do_ucompare;
13976
13977     case GTU:
13978       op = DW_OP_gt;
13979       goto do_ucompare;
13980
13981     case LEU:
13982       op = DW_OP_le;
13983       goto do_ucompare;
13984
13985     case LTU:
13986       op = DW_OP_lt;
13987       goto do_ucompare;
13988
13989     do_ucompare:
13990       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13991           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13992         break;
13993       else
13994         {
13995           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13996
13997           if (op_mode == VOIDmode)
13998             op_mode = GET_MODE (XEXP (rtl, 1));
13999           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14000             break;
14001
14002           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14003                                     VAR_INIT_STATUS_INITIALIZED);
14004           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14005                                     VAR_INIT_STATUS_INITIALIZED);
14006
14007           if (op0 == 0 || op1 == 0)
14008             break;
14009
14010           if (op_mode != VOIDmode
14011               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14012             {
14013               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14014               dw_loc_descr_ref last0, last1;
14015               for (last0 = op0;
14016                    last0->dw_loc_next != NULL;
14017                    last0 = last0->dw_loc_next)
14018                 ;
14019               for (last1 = op1;
14020                    last1->dw_loc_next != NULL;
14021                    last1 = last1->dw_loc_next)
14022                 ;
14023               if (CONST_INT_P (XEXP (rtl, 0)))
14024                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14025               /* deref_size zero extends, so no need to mask it again.  */
14026               else if (last0->dw_loc_opc != DW_OP_deref_size
14027                        || last0->dw_loc_oprnd1.v.val_int
14028                           > GET_MODE_SIZE (op_mode))
14029                 {
14030                   add_loc_descr (&op0, int_loc_descriptor (mask));
14031                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14032                 }
14033               if (CONST_INT_P (XEXP (rtl, 1)))
14034                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14035               /* deref_size zero extends, so no need to mask it again.  */
14036               else if (last1->dw_loc_opc != DW_OP_deref_size
14037                        || last1->dw_loc_oprnd1.v.val_int
14038                           > GET_MODE_SIZE (op_mode))
14039                 {
14040                   add_loc_descr (&op1, int_loc_descriptor (mask));
14041                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14042                 }
14043             }
14044           else
14045             {
14046               HOST_WIDE_INT bias = 1;
14047               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14048               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14049               if (CONST_INT_P (XEXP (rtl, 1)))
14050                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14051                                           + INTVAL (XEXP (rtl, 1)));
14052               else
14053                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14054                                                     bias, 0));
14055             }
14056         }
14057       goto do_compare;
14058
14059     case SMIN:
14060     case SMAX:
14061     case UMIN:
14062     case UMAX:
14063       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14064           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14065           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14066         break;
14067
14068       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14069                                 VAR_INIT_STATUS_INITIALIZED);
14070       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14071                                 VAR_INIT_STATUS_INITIALIZED);
14072
14073       if (op0 == 0 || op1 == 0)
14074         break;
14075
14076       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14077       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14078       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14079       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14080         {
14081           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14082             {
14083               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14084               add_loc_descr (&op0, int_loc_descriptor (mask));
14085               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14086               add_loc_descr (&op1, int_loc_descriptor (mask));
14087               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14088             }
14089           else
14090             {
14091               HOST_WIDE_INT bias = 1;
14092               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14093               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14094               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14095             }
14096         }
14097       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14098         {
14099           int shift = DWARF2_ADDR_SIZE
14100                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14101           shift *= BITS_PER_UNIT;
14102           add_loc_descr (&op0, int_loc_descriptor (shift));
14103           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14104           add_loc_descr (&op1, int_loc_descriptor (shift));
14105           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14106         }
14107
14108       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14109         op = DW_OP_lt;
14110       else
14111         op = DW_OP_gt;
14112       mem_loc_result = op0;
14113       add_loc_descr (&mem_loc_result, op1);
14114       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14115       {
14116         dw_loc_descr_ref bra_node, drop_node;
14117
14118         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14119         add_loc_descr (&mem_loc_result, bra_node);
14120         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14121         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14122         add_loc_descr (&mem_loc_result, drop_node);
14123         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14124         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14125       }
14126       break;
14127
14128     case ZERO_EXTRACT:
14129     case SIGN_EXTRACT:
14130       if (CONST_INT_P (XEXP (rtl, 1))
14131           && CONST_INT_P (XEXP (rtl, 2))
14132           && ((unsigned) INTVAL (XEXP (rtl, 1))
14133               + (unsigned) INTVAL (XEXP (rtl, 2))
14134               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14135           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14136           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14137         {
14138           int shift, size;
14139           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14140                                     VAR_INIT_STATUS_INITIALIZED);
14141           if (op0 == 0)
14142             break;
14143           if (GET_CODE (rtl) == SIGN_EXTRACT)
14144             op = DW_OP_shra;
14145           else
14146             op = DW_OP_shr;
14147           mem_loc_result = op0;
14148           size = INTVAL (XEXP (rtl, 1));
14149           shift = INTVAL (XEXP (rtl, 2));
14150           if (BITS_BIG_ENDIAN)
14151             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14152                     - shift - size;
14153           if (shift + size != (int) DWARF2_ADDR_SIZE)
14154             {
14155               add_loc_descr (&mem_loc_result,
14156                              int_loc_descriptor (DWARF2_ADDR_SIZE
14157                                                  - shift - size));
14158               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14159             }
14160           if (size != (int) DWARF2_ADDR_SIZE)
14161             {
14162               add_loc_descr (&mem_loc_result,
14163                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14164               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14165             }
14166         }
14167       break;
14168
14169     case COMPARE:
14170     case IF_THEN_ELSE:
14171     case ROTATE:
14172     case ROTATERT:
14173     case TRUNCATE:
14174       /* In theory, we could implement the above.  */
14175       /* DWARF cannot represent the unsigned compare operations
14176          natively.  */
14177     case SS_MULT:
14178     case US_MULT:
14179     case SS_DIV:
14180     case US_DIV:
14181     case SS_PLUS:
14182     case US_PLUS:
14183     case SS_MINUS:
14184     case US_MINUS:
14185     case SS_NEG:
14186     case US_NEG:
14187     case SS_ABS:
14188     case SS_ASHIFT:
14189     case US_ASHIFT:
14190     case SS_TRUNCATE:
14191     case US_TRUNCATE:
14192     case UDIV:
14193     case UNORDERED:
14194     case ORDERED:
14195     case UNEQ:
14196     case UNGE:
14197     case UNGT:
14198     case UNLE:
14199     case UNLT:
14200     case LTGT:
14201     case FLOAT_EXTEND:
14202     case FLOAT_TRUNCATE:
14203     case FLOAT:
14204     case UNSIGNED_FLOAT:
14205     case FIX:
14206     case UNSIGNED_FIX:
14207     case FRACT_CONVERT:
14208     case UNSIGNED_FRACT_CONVERT:
14209     case SAT_FRACT:
14210     case UNSIGNED_SAT_FRACT:
14211     case SQRT:
14212     case BSWAP:
14213     case FFS:
14214     case CLZ:
14215     case CTZ:
14216     case POPCOUNT:
14217     case PARITY:
14218     case ASM_OPERANDS:
14219     case VEC_MERGE:
14220     case VEC_SELECT:
14221     case VEC_CONCAT:
14222     case VEC_DUPLICATE:
14223     case UNSPEC:
14224     case HIGH:
14225       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14226          can't express it in the debug info.  This can happen e.g. with some
14227          TLS UNSPECs.  */
14228       break;
14229
14230     case CONST_STRING:
14231       resolve_one_addr (&rtl, NULL);
14232       goto symref;
14233
14234     default:
14235 #ifdef ENABLE_CHECKING
14236       print_rtl (stderr, rtl);
14237       gcc_unreachable ();
14238 #else
14239       break;
14240 #endif
14241     }
14242
14243   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14244     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14245
14246   return mem_loc_result;
14247 }
14248
14249 /* Return a descriptor that describes the concatenation of two locations.
14250    This is typically a complex variable.  */
14251
14252 static dw_loc_descr_ref
14253 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14254 {
14255   dw_loc_descr_ref cc_loc_result = NULL;
14256   dw_loc_descr_ref x0_ref
14257     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14258   dw_loc_descr_ref x1_ref
14259     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14260
14261   if (x0_ref == 0 || x1_ref == 0)
14262     return 0;
14263
14264   cc_loc_result = x0_ref;
14265   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14266
14267   add_loc_descr (&cc_loc_result, x1_ref);
14268   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14269
14270   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14271     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14272
14273   return cc_loc_result;
14274 }
14275
14276 /* Return a descriptor that describes the concatenation of N
14277    locations.  */
14278
14279 static dw_loc_descr_ref
14280 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14281 {
14282   unsigned int i;
14283   dw_loc_descr_ref cc_loc_result = NULL;
14284   unsigned int n = XVECLEN (concatn, 0);
14285
14286   for (i = 0; i < n; ++i)
14287     {
14288       dw_loc_descr_ref ref;
14289       rtx x = XVECEXP (concatn, 0, i);
14290
14291       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14292       if (ref == NULL)
14293         return NULL;
14294
14295       add_loc_descr (&cc_loc_result, ref);
14296       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14297     }
14298
14299   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14300     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14301
14302   return cc_loc_result;
14303 }
14304
14305 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14306    for DEBUG_IMPLICIT_PTR RTL.  */
14307
14308 static dw_loc_descr_ref
14309 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14310 {
14311   dw_loc_descr_ref ret;
14312   dw_die_ref ref;
14313
14314   if (dwarf_strict)
14315     return NULL;
14316   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14317               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14318               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14319   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14320   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14321   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14322   if (ref)
14323     {
14324       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14325       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14326       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14327     }
14328   else
14329     {
14330       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14331       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14332     }
14333   return ret;
14334 }
14335
14336 /* Output a proper Dwarf location descriptor for a variable or parameter
14337    which is either allocated in a register or in a memory location.  For a
14338    register, we just generate an OP_REG and the register number.  For a
14339    memory location we provide a Dwarf postfix expression describing how to
14340    generate the (dynamic) address of the object onto the address stack.
14341
14342    MODE is mode of the decl if this loc_descriptor is going to be used in
14343    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14344    allowed, VOIDmode otherwise.
14345
14346    If we don't know how to describe it, return 0.  */
14347
14348 static dw_loc_descr_ref
14349 loc_descriptor (rtx rtl, enum machine_mode mode,
14350                 enum var_init_status initialized)
14351 {
14352   dw_loc_descr_ref loc_result = NULL;
14353
14354   switch (GET_CODE (rtl))
14355     {
14356     case SUBREG:
14357       /* The case of a subreg may arise when we have a local (register)
14358          variable or a formal (register) parameter which doesn't quite fill
14359          up an entire register.  For now, just assume that it is
14360          legitimate to make the Dwarf info refer to the whole register which
14361          contains the given subreg.  */
14362       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14363       break;
14364
14365     case REG:
14366       loc_result = reg_loc_descriptor (rtl, initialized);
14367       break;
14368
14369     case MEM:
14370       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14371                                        initialized);
14372       if (loc_result == NULL)
14373         loc_result = tls_mem_loc_descriptor (rtl);
14374       if (loc_result == NULL)
14375         {
14376           rtx new_rtl = avoid_constant_pool_reference (rtl);
14377           if (new_rtl != rtl)
14378             loc_result = loc_descriptor (new_rtl, mode, initialized);
14379         }
14380       break;
14381
14382     case CONCAT:
14383       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14384                                           initialized);
14385       break;
14386
14387     case CONCATN:
14388       loc_result = concatn_loc_descriptor (rtl, initialized);
14389       break;
14390
14391     case VAR_LOCATION:
14392       /* Single part.  */
14393       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14394         {
14395           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14396           if (GET_CODE (loc) == EXPR_LIST)
14397             loc = XEXP (loc, 0);
14398           loc_result = loc_descriptor (loc, mode, initialized);
14399           break;
14400         }
14401
14402       rtl = XEXP (rtl, 1);
14403       /* FALLTHRU */
14404
14405     case PARALLEL:
14406       {
14407         rtvec par_elems = XVEC (rtl, 0);
14408         int num_elem = GET_NUM_ELEM (par_elems);
14409         enum machine_mode mode;
14410         int i;
14411
14412         /* Create the first one, so we have something to add to.  */
14413         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14414                                      VOIDmode, initialized);
14415         if (loc_result == NULL)
14416           return NULL;
14417         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14418         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14419         for (i = 1; i < num_elem; i++)
14420           {
14421             dw_loc_descr_ref temp;
14422
14423             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14424                                    VOIDmode, initialized);
14425             if (temp == NULL)
14426               return NULL;
14427             add_loc_descr (&loc_result, temp);
14428             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14429             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14430           }
14431       }
14432       break;
14433
14434     case CONST_INT:
14435       if (mode != VOIDmode && mode != BLKmode)
14436         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14437                                                     INTVAL (rtl));
14438       break;
14439
14440     case CONST_DOUBLE:
14441       if (mode == VOIDmode)
14442         mode = GET_MODE (rtl);
14443
14444       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14445         {
14446           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14447
14448           /* Note that a CONST_DOUBLE rtx could represent either an integer
14449              or a floating-point constant.  A CONST_DOUBLE is used whenever
14450              the constant requires more than one word in order to be
14451              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14452           loc_result = new_loc_descr (DW_OP_implicit_value,
14453                                       GET_MODE_SIZE (mode), 0);
14454           if (SCALAR_FLOAT_MODE_P (mode))
14455             {
14456               unsigned int length = GET_MODE_SIZE (mode);
14457               unsigned char *array
14458                   = (unsigned char*) ggc_alloc_atomic (length);
14459
14460               insert_float (rtl, array);
14461               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14462               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14463               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14464               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14465             }
14466           else
14467             {
14468               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14469               loc_result->dw_loc_oprnd2.v.val_double
14470                 = rtx_to_double_int (rtl);
14471             }
14472         }
14473       break;
14474
14475     case CONST_VECTOR:
14476       if (mode == VOIDmode)
14477         mode = GET_MODE (rtl);
14478
14479       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14480         {
14481           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14482           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14483           unsigned char *array = (unsigned char *)
14484             ggc_alloc_atomic (length * elt_size);
14485           unsigned int i;
14486           unsigned char *p;
14487
14488           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14489           switch (GET_MODE_CLASS (mode))
14490             {
14491             case MODE_VECTOR_INT:
14492               for (i = 0, p = array; i < length; i++, p += elt_size)
14493                 {
14494                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14495                   double_int val = rtx_to_double_int (elt);
14496
14497                   if (elt_size <= sizeof (HOST_WIDE_INT))
14498                     insert_int (double_int_to_shwi (val), elt_size, p);
14499                   else
14500                     {
14501                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14502                       insert_double (val, p);
14503                     }
14504                 }
14505               break;
14506
14507             case MODE_VECTOR_FLOAT:
14508               for (i = 0, p = array; i < length; i++, p += elt_size)
14509                 {
14510                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14511                   insert_float (elt, p);
14512                 }
14513               break;
14514
14515             default:
14516               gcc_unreachable ();
14517             }
14518
14519           loc_result = new_loc_descr (DW_OP_implicit_value,
14520                                       length * elt_size, 0);
14521           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14522           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14523           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14524           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14525         }
14526       break;
14527
14528     case CONST:
14529       if (mode == VOIDmode
14530           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14531           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14532           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14533         {
14534           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14535           break;
14536         }
14537       /* FALLTHROUGH */
14538     case SYMBOL_REF:
14539       if (!const_ok_for_output (rtl))
14540         break;
14541     case LABEL_REF:
14542       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14543           && (dwarf_version >= 4 || !dwarf_strict))
14544         {
14545           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14546           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14547           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14548           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14549           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14550         }
14551       break;
14552
14553     case DEBUG_IMPLICIT_PTR:
14554       loc_result = implicit_ptr_descriptor (rtl, 0);
14555       break;
14556
14557     case PLUS:
14558       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14559           && CONST_INT_P (XEXP (rtl, 1)))
14560         {
14561           loc_result
14562             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14563           break;
14564         }
14565       /* FALLTHRU */
14566     default:
14567       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14568           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14569           && (dwarf_version >= 4 || !dwarf_strict))
14570         {
14571           /* Value expression.  */
14572           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14573           if (loc_result)
14574             add_loc_descr (&loc_result,
14575                            new_loc_descr (DW_OP_stack_value, 0, 0));
14576         }
14577       break;
14578     }
14579
14580   return loc_result;
14581 }
14582
14583 /* We need to figure out what section we should use as the base for the
14584    address ranges where a given location is valid.
14585    1. If this particular DECL has a section associated with it, use that.
14586    2. If this function has a section associated with it, use that.
14587    3. Otherwise, use the text section.
14588    XXX: If you split a variable across multiple sections, we won't notice.  */
14589
14590 static const char *
14591 secname_for_decl (const_tree decl)
14592 {
14593   const char *secname;
14594
14595   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14596     {
14597       tree sectree = DECL_SECTION_NAME (decl);
14598       secname = TREE_STRING_POINTER (sectree);
14599     }
14600   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14601     {
14602       tree sectree = DECL_SECTION_NAME (current_function_decl);
14603       secname = TREE_STRING_POINTER (sectree);
14604     }
14605   else if (cfun && in_cold_section_p)
14606     secname = crtl->subsections.cold_section_label;
14607   else
14608     secname = text_section_label;
14609
14610   return secname;
14611 }
14612
14613 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14614
14615 static bool
14616 decl_by_reference_p (tree decl)
14617 {
14618   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14619            || TREE_CODE (decl) == VAR_DECL)
14620           && DECL_BY_REFERENCE (decl));
14621 }
14622
14623 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14624    for VARLOC.  */
14625
14626 static dw_loc_descr_ref
14627 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14628                enum var_init_status initialized)
14629 {
14630   int have_address = 0;
14631   dw_loc_descr_ref descr;
14632   enum machine_mode mode;
14633
14634   if (want_address != 2)
14635     {
14636       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14637       /* Single part.  */
14638       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14639         {
14640           varloc = PAT_VAR_LOCATION_LOC (varloc);
14641           if (GET_CODE (varloc) == EXPR_LIST)
14642             varloc = XEXP (varloc, 0);
14643           mode = GET_MODE (varloc);
14644           if (MEM_P (varloc))
14645             {
14646               rtx addr = XEXP (varloc, 0);
14647               descr = mem_loc_descriptor (addr, mode, initialized);
14648               if (descr)
14649                 have_address = 1;
14650               else
14651                 {
14652                   rtx x = avoid_constant_pool_reference (varloc);
14653                   if (x != varloc)
14654                     descr = mem_loc_descriptor (x, mode, initialized);
14655                 }
14656             }
14657           else
14658             descr = mem_loc_descriptor (varloc, mode, initialized);
14659         }
14660       else
14661         return 0;
14662     }
14663   else
14664     {
14665       if (GET_CODE (varloc) == VAR_LOCATION)
14666         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14667       else
14668         mode = DECL_MODE (loc);
14669       descr = loc_descriptor (varloc, mode, initialized);
14670       have_address = 1;
14671     }
14672
14673   if (!descr)
14674     return 0;
14675
14676   if (want_address == 2 && !have_address
14677       && (dwarf_version >= 4 || !dwarf_strict))
14678     {
14679       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14680         {
14681           expansion_failed (loc, NULL_RTX,
14682                             "DWARF address size mismatch");
14683           return 0;
14684         }
14685       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14686       have_address = 1;
14687     }
14688   /* Show if we can't fill the request for an address.  */
14689   if (want_address && !have_address)
14690     {
14691       expansion_failed (loc, NULL_RTX,
14692                         "Want address and only have value");
14693       return 0;
14694     }
14695
14696   /* If we've got an address and don't want one, dereference.  */
14697   if (!want_address && have_address)
14698     {
14699       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14700       enum dwarf_location_atom op;
14701
14702       if (size > DWARF2_ADDR_SIZE || size == -1)
14703         {
14704           expansion_failed (loc, NULL_RTX,
14705                             "DWARF address size mismatch");
14706           return 0;
14707         }
14708       else if (size == DWARF2_ADDR_SIZE)
14709         op = DW_OP_deref;
14710       else
14711         op = DW_OP_deref_size;
14712
14713       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14714     }
14715
14716   return descr;
14717 }
14718
14719 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14720    if it is not possible.  */
14721
14722 static dw_loc_descr_ref
14723 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14724 {
14725   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14726     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14727   else if (dwarf_version >= 3 || !dwarf_strict)
14728     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14729   else
14730     return NULL;
14731 }
14732
14733 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14734    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14735
14736 static dw_loc_descr_ref
14737 dw_sra_loc_expr (tree decl, rtx loc)
14738 {
14739   rtx p;
14740   unsigned int padsize = 0;
14741   dw_loc_descr_ref descr, *descr_tail;
14742   unsigned HOST_WIDE_INT decl_size;
14743   rtx varloc;
14744   enum var_init_status initialized;
14745
14746   if (DECL_SIZE (decl) == NULL
14747       || !host_integerp (DECL_SIZE (decl), 1))
14748     return NULL;
14749
14750   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14751   descr = NULL;
14752   descr_tail = &descr;
14753
14754   for (p = loc; p; p = XEXP (p, 1))
14755     {
14756       unsigned int bitsize = decl_piece_bitsize (p);
14757       rtx loc_note = *decl_piece_varloc_ptr (p);
14758       dw_loc_descr_ref cur_descr;
14759       dw_loc_descr_ref *tail, last = NULL;
14760       unsigned int opsize = 0;
14761
14762       if (loc_note == NULL_RTX
14763           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14764         {
14765           padsize += bitsize;
14766           continue;
14767         }
14768       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14769       varloc = NOTE_VAR_LOCATION (loc_note);
14770       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14771       if (cur_descr == NULL)
14772         {
14773           padsize += bitsize;
14774           continue;
14775         }
14776
14777       /* Check that cur_descr either doesn't use
14778          DW_OP_*piece operations, or their sum is equal
14779          to bitsize.  Otherwise we can't embed it.  */
14780       for (tail = &cur_descr; *tail != NULL;
14781            tail = &(*tail)->dw_loc_next)
14782         if ((*tail)->dw_loc_opc == DW_OP_piece)
14783           {
14784             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14785                       * BITS_PER_UNIT;
14786             last = *tail;
14787           }
14788         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14789           {
14790             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14791             last = *tail;
14792           }
14793
14794       if (last != NULL && opsize != bitsize)
14795         {
14796           padsize += bitsize;
14797           continue;
14798         }
14799
14800       /* If there is a hole, add DW_OP_*piece after empty DWARF
14801          expression, which means that those bits are optimized out.  */
14802       if (padsize)
14803         {
14804           if (padsize > decl_size)
14805             return NULL;
14806           decl_size -= padsize;
14807           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14808           if (*descr_tail == NULL)
14809             return NULL;
14810           descr_tail = &(*descr_tail)->dw_loc_next;
14811           padsize = 0;
14812         }
14813       *descr_tail = cur_descr;
14814       descr_tail = tail;
14815       if (bitsize > decl_size)
14816         return NULL;
14817       decl_size -= bitsize;
14818       if (last == NULL)
14819         {
14820           HOST_WIDE_INT offset = 0;
14821           if (GET_CODE (varloc) == VAR_LOCATION
14822               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14823             {
14824               varloc = PAT_VAR_LOCATION_LOC (varloc);
14825               if (GET_CODE (varloc) == EXPR_LIST)
14826                 varloc = XEXP (varloc, 0);
14827             }
14828           do 
14829             {
14830               if (GET_CODE (varloc) == CONST
14831                   || GET_CODE (varloc) == SIGN_EXTEND
14832                   || GET_CODE (varloc) == ZERO_EXTEND)
14833                 varloc = XEXP (varloc, 0);
14834               else if (GET_CODE (varloc) == SUBREG)
14835                 varloc = SUBREG_REG (varloc);
14836               else
14837                 break;
14838             }
14839           while (1);
14840           /* DW_OP_bit_size offset should be zero for register
14841              or implicit location descriptions and empty location
14842              descriptions, but for memory addresses needs big endian
14843              adjustment.  */
14844           if (MEM_P (varloc))
14845             {
14846               unsigned HOST_WIDE_INT memsize
14847                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14848               if (memsize != bitsize)
14849                 {
14850                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14851                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14852                     return NULL;
14853                   if (memsize < bitsize)
14854                     return NULL;
14855                   if (BITS_BIG_ENDIAN)
14856                     offset = memsize - bitsize;
14857                 }
14858             }
14859
14860           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14861           if (*descr_tail == NULL)
14862             return NULL;
14863           descr_tail = &(*descr_tail)->dw_loc_next;
14864         }
14865     }
14866
14867   /* If there were any non-empty expressions, add padding till the end of
14868      the decl.  */
14869   if (descr != NULL && decl_size != 0)
14870     {
14871       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14872       if (*descr_tail == NULL)
14873         return NULL;
14874     }
14875   return descr;
14876 }
14877
14878 /* Return the dwarf representation of the location list LOC_LIST of
14879    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14880    function.  */
14881
14882 static dw_loc_list_ref
14883 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14884 {
14885   const char *endname, *secname;
14886   rtx varloc;
14887   enum var_init_status initialized;
14888   struct var_loc_node *node;
14889   dw_loc_descr_ref descr;
14890   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14891   dw_loc_list_ref list = NULL;
14892   dw_loc_list_ref *listp = &list;
14893
14894   /* Now that we know what section we are using for a base,
14895      actually construct the list of locations.
14896      The first location information is what is passed to the
14897      function that creates the location list, and the remaining
14898      locations just get added on to that list.
14899      Note that we only know the start address for a location
14900      (IE location changes), so to build the range, we use
14901      the range [current location start, next location start].
14902      This means we have to special case the last node, and generate
14903      a range of [last location start, end of function label].  */
14904
14905   secname = secname_for_decl (decl);
14906
14907   for (node = loc_list->first; node; node = node->next)
14908     if (GET_CODE (node->loc) == EXPR_LIST
14909         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14910       {
14911         if (GET_CODE (node->loc) == EXPR_LIST)
14912           {
14913             /* This requires DW_OP_{,bit_}piece, which is not usable
14914                inside DWARF expressions.  */
14915             if (want_address != 2)
14916               continue;
14917             descr = dw_sra_loc_expr (decl, node->loc);
14918             if (descr == NULL)
14919               continue;
14920           }
14921         else
14922           {
14923             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14924             varloc = NOTE_VAR_LOCATION (node->loc);
14925             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14926           }
14927         if (descr)
14928           {
14929             /* The variable has a location between NODE->LABEL and
14930                NODE->NEXT->LABEL.  */
14931             if (node->next)
14932               endname = node->next->label;
14933             /* If the variable has a location at the last label
14934                it keeps its location until the end of function.  */
14935             else if (!current_function_decl)
14936               endname = text_end_label;
14937             else
14938               {
14939                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14940                                              current_function_funcdef_no);
14941                 endname = ggc_strdup (label_id);
14942               }
14943
14944             *listp = new_loc_list (descr, node->label, endname, secname);
14945             listp = &(*listp)->dw_loc_next;
14946           }
14947       }
14948
14949   /* Try to avoid the overhead of a location list emitting a location
14950      expression instead, but only if we didn't have more than one
14951      location entry in the first place.  If some entries were not
14952      representable, we don't want to pretend a single entry that was
14953      applies to the entire scope in which the variable is
14954      available.  */
14955   if (list && loc_list->first->next)
14956     gen_llsym (list);
14957
14958   return list;
14959 }
14960
14961 /* Return if the loc_list has only single element and thus can be represented
14962    as location description.   */
14963
14964 static bool
14965 single_element_loc_list_p (dw_loc_list_ref list)
14966 {
14967   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14968   return !list->ll_symbol;
14969 }
14970
14971 /* To each location in list LIST add loc descr REF.  */
14972
14973 static void
14974 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14975 {
14976   dw_loc_descr_ref copy;
14977   add_loc_descr (&list->expr, ref);
14978   list = list->dw_loc_next;
14979   while (list)
14980     {
14981       copy = ggc_alloc_dw_loc_descr_node ();
14982       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14983       add_loc_descr (&list->expr, copy);
14984       while (copy->dw_loc_next)
14985         {
14986           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14987           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14988           copy->dw_loc_next = new_copy;
14989           copy = new_copy;
14990         }
14991       list = list->dw_loc_next;
14992     }
14993 }
14994
14995 /* Given two lists RET and LIST
14996    produce location list that is result of adding expression in LIST
14997    to expression in RET on each possition in program.
14998    Might be destructive on both RET and LIST.
14999
15000    TODO: We handle only simple cases of RET or LIST having at most one
15001    element. General case would inolve sorting the lists in program order
15002    and merging them that will need some additional work.
15003    Adding that will improve quality of debug info especially for SRA-ed
15004    structures.  */
15005
15006 static void
15007 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15008 {
15009   if (!list)
15010     return;
15011   if (!*ret)
15012     {
15013       *ret = list;
15014       return;
15015     }
15016   if (!list->dw_loc_next)
15017     {
15018       add_loc_descr_to_each (*ret, list->expr);
15019       return;
15020     }
15021   if (!(*ret)->dw_loc_next)
15022     {
15023       add_loc_descr_to_each (list, (*ret)->expr);
15024       *ret = list;
15025       return;
15026     }
15027   expansion_failed (NULL_TREE, NULL_RTX,
15028                     "Don't know how to merge two non-trivial"
15029                     " location lists.\n");
15030   *ret = NULL;
15031   return;
15032 }
15033
15034 /* LOC is constant expression.  Try a luck, look it up in constant
15035    pool and return its loc_descr of its address.  */
15036
15037 static dw_loc_descr_ref
15038 cst_pool_loc_descr (tree loc)
15039 {
15040   /* Get an RTL for this, if something has been emitted.  */
15041   rtx rtl = lookup_constant_def (loc);
15042   enum machine_mode mode;
15043
15044   if (!rtl || !MEM_P (rtl))
15045     {
15046       gcc_assert (!rtl);
15047       return 0;
15048     }
15049   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15050
15051   /* TODO: We might get more coverage if we was actually delaying expansion
15052      of all expressions till end of compilation when constant pools are fully
15053      populated.  */
15054   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15055     {
15056       expansion_failed (loc, NULL_RTX,
15057                         "CST value in contant pool but not marked.");
15058       return 0;
15059     }
15060   mode = GET_MODE (rtl);
15061   rtl = XEXP (rtl, 0);
15062   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15063 }
15064
15065 /* Return dw_loc_list representing address of addr_expr LOC
15066    by looking for innder INDIRECT_REF expression and turing it
15067    into simple arithmetics.  */
15068
15069 static dw_loc_list_ref
15070 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15071 {
15072   tree obj, offset;
15073   HOST_WIDE_INT bitsize, bitpos, bytepos;
15074   enum machine_mode mode;
15075   int volatilep;
15076   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15077   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15078
15079   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15080                              &bitsize, &bitpos, &offset, &mode,
15081                              &unsignedp, &volatilep, false);
15082   STRIP_NOPS (obj);
15083   if (bitpos % BITS_PER_UNIT)
15084     {
15085       expansion_failed (loc, NULL_RTX, "bitfield access");
15086       return 0;
15087     }
15088   if (!INDIRECT_REF_P (obj))
15089     {
15090       expansion_failed (obj,
15091                         NULL_RTX, "no indirect ref in inner refrence");
15092       return 0;
15093     }
15094   if (!offset && !bitpos)
15095     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15096   else if (toplev
15097            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15098            && (dwarf_version >= 4 || !dwarf_strict))
15099     {
15100       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15101       if (!list_ret)
15102         return 0;
15103       if (offset)
15104         {
15105           /* Variable offset.  */
15106           list_ret1 = loc_list_from_tree (offset, 0);
15107           if (list_ret1 == 0)
15108             return 0;
15109           add_loc_list (&list_ret, list_ret1);
15110           if (!list_ret)
15111             return 0;
15112           add_loc_descr_to_each (list_ret,
15113                                  new_loc_descr (DW_OP_plus, 0, 0));
15114         }
15115       bytepos = bitpos / BITS_PER_UNIT;
15116       if (bytepos > 0)
15117         add_loc_descr_to_each (list_ret,
15118                                new_loc_descr (DW_OP_plus_uconst,
15119                                               bytepos, 0));
15120       else if (bytepos < 0)
15121         loc_list_plus_const (list_ret, bytepos);
15122       add_loc_descr_to_each (list_ret,
15123                              new_loc_descr (DW_OP_stack_value, 0, 0));
15124     }
15125   return list_ret;
15126 }
15127
15128
15129 /* Generate Dwarf location list representing LOC.
15130    If WANT_ADDRESS is false, expression computing LOC will be computed
15131    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15132    if WANT_ADDRESS is 2, expression computing address useable in location
15133      will be returned (i.e. DW_OP_reg can be used
15134      to refer to register values).  */
15135
15136 static dw_loc_list_ref
15137 loc_list_from_tree (tree loc, int want_address)
15138 {
15139   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15140   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15141   int have_address = 0;
15142   enum dwarf_location_atom op;
15143
15144   /* ??? Most of the time we do not take proper care for sign/zero
15145      extending the values properly.  Hopefully this won't be a real
15146      problem...  */
15147
15148   switch (TREE_CODE (loc))
15149     {
15150     case ERROR_MARK:
15151       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15152       return 0;
15153
15154     case PLACEHOLDER_EXPR:
15155       /* This case involves extracting fields from an object to determine the
15156          position of other fields.  We don't try to encode this here.  The
15157          only user of this is Ada, which encodes the needed information using
15158          the names of types.  */
15159       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15160       return 0;
15161
15162     case CALL_EXPR:
15163       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15164       /* There are no opcodes for these operations.  */
15165       return 0;
15166
15167     case PREINCREMENT_EXPR:
15168     case PREDECREMENT_EXPR:
15169     case POSTINCREMENT_EXPR:
15170     case POSTDECREMENT_EXPR:
15171       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15172       /* There are no opcodes for these operations.  */
15173       return 0;
15174
15175     case ADDR_EXPR:
15176       /* If we already want an address, see if there is INDIRECT_REF inside
15177          e.g. for &this->field.  */
15178       if (want_address)
15179         {
15180           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15181                        (loc, want_address == 2);
15182           if (list_ret)
15183             have_address = 1;
15184           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15185                    && (ret = cst_pool_loc_descr (loc)))
15186             have_address = 1;
15187         }
15188         /* Otherwise, process the argument and look for the address.  */
15189       if (!list_ret && !ret)
15190         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15191       else
15192         {
15193           if (want_address)
15194             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15195           return NULL;
15196         }
15197       break;
15198
15199     case VAR_DECL:
15200       if (DECL_THREAD_LOCAL_P (loc))
15201         {
15202           rtx rtl;
15203           enum dwarf_location_atom first_op;
15204           enum dwarf_location_atom second_op;
15205           bool dtprel = false;
15206
15207           if (targetm.have_tls)
15208             {
15209               /* If this is not defined, we have no way to emit the
15210                  data.  */
15211               if (!targetm.asm_out.output_dwarf_dtprel)
15212                 return 0;
15213
15214                /* The way DW_OP_GNU_push_tls_address is specified, we
15215                   can only look up addresses of objects in the current
15216                   module.  We used DW_OP_addr as first op, but that's
15217                   wrong, because DW_OP_addr is relocated by the debug
15218                   info consumer, while DW_OP_GNU_push_tls_address
15219                   operand shouldn't be.  */
15220               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15221                 return 0;
15222               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15223               dtprel = true;
15224               second_op = DW_OP_GNU_push_tls_address;
15225             }
15226           else
15227             {
15228               if (!targetm.emutls.debug_form_tls_address
15229                   || !(dwarf_version >= 3 || !dwarf_strict))
15230                 return 0;
15231               /* We stuffed the control variable into the DECL_VALUE_EXPR
15232                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15233                  no longer appear in gimple code.  We used the control
15234                  variable in specific so that we could pick it up here.  */
15235               loc = DECL_VALUE_EXPR (loc);
15236               first_op = DW_OP_addr;
15237               second_op = DW_OP_form_tls_address;
15238             }
15239
15240           rtl = rtl_for_decl_location (loc);
15241           if (rtl == NULL_RTX)
15242             return 0;
15243
15244           if (!MEM_P (rtl))
15245             return 0;
15246           rtl = XEXP (rtl, 0);
15247           if (! CONSTANT_P (rtl))
15248             return 0;
15249
15250           ret = new_loc_descr (first_op, 0, 0);
15251           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15252           ret->dw_loc_oprnd1.v.val_addr = rtl;
15253           ret->dtprel = dtprel;
15254
15255           ret1 = new_loc_descr (second_op, 0, 0);
15256           add_loc_descr (&ret, ret1);
15257
15258           have_address = 1;
15259           break;
15260         }
15261       /* FALLTHRU */
15262
15263     case PARM_DECL:
15264       if (DECL_HAS_VALUE_EXPR_P (loc))
15265         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15266                                    want_address);
15267       /* FALLTHRU */
15268
15269     case RESULT_DECL:
15270     case FUNCTION_DECL:
15271       {
15272         rtx rtl;
15273         var_loc_list *loc_list = lookup_decl_loc (loc);
15274
15275         if (loc_list && loc_list->first)
15276           {
15277             list_ret = dw_loc_list (loc_list, loc, want_address);
15278             have_address = want_address != 0;
15279             break;
15280           }
15281         rtl = rtl_for_decl_location (loc);
15282         if (rtl == NULL_RTX)
15283           {
15284             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15285             return 0;
15286           }
15287         else if (CONST_INT_P (rtl))
15288           {
15289             HOST_WIDE_INT val = INTVAL (rtl);
15290             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15291               val &= GET_MODE_MASK (DECL_MODE (loc));
15292             ret = int_loc_descriptor (val);
15293           }
15294         else if (GET_CODE (rtl) == CONST_STRING)
15295           {
15296             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15297             return 0;
15298           }
15299         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15300           {
15301             ret = new_loc_descr (DW_OP_addr, 0, 0);
15302             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15303             ret->dw_loc_oprnd1.v.val_addr = rtl;
15304           }
15305         else
15306           {
15307             enum machine_mode mode;
15308
15309             /* Certain constructs can only be represented at top-level.  */
15310             if (want_address == 2)
15311               {
15312                 ret = loc_descriptor (rtl, VOIDmode,
15313                                       VAR_INIT_STATUS_INITIALIZED);
15314                 have_address = 1;
15315               }
15316             else
15317               {
15318                 mode = GET_MODE (rtl);
15319                 if (MEM_P (rtl))
15320                   {
15321                     rtl = XEXP (rtl, 0);
15322                     have_address = 1;
15323                   }
15324                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15325               }
15326             if (!ret)
15327               expansion_failed (loc, rtl,
15328                                 "failed to produce loc descriptor for rtl");
15329           }
15330       }
15331       break;
15332
15333     case MEM_REF:
15334       /* ??? FIXME.  */
15335       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15336         return 0;
15337       /* Fallthru.  */
15338     case INDIRECT_REF:
15339       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15340       have_address = 1;
15341       break;
15342
15343     case COMPOUND_EXPR:
15344       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15345
15346     CASE_CONVERT:
15347     case VIEW_CONVERT_EXPR:
15348     case SAVE_EXPR:
15349     case MODIFY_EXPR:
15350       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15351
15352     case COMPONENT_REF:
15353     case BIT_FIELD_REF:
15354     case ARRAY_REF:
15355     case ARRAY_RANGE_REF:
15356     case REALPART_EXPR:
15357     case IMAGPART_EXPR:
15358       {
15359         tree obj, offset;
15360         HOST_WIDE_INT bitsize, bitpos, bytepos;
15361         enum machine_mode mode;
15362         int volatilep;
15363         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15364
15365         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15366                                    &unsignedp, &volatilep, false);
15367
15368         gcc_assert (obj != loc);
15369
15370         list_ret = loc_list_from_tree (obj,
15371                                        want_address == 2
15372                                        && !bitpos && !offset ? 2 : 1);
15373         /* TODO: We can extract value of the small expression via shifting even
15374            for nonzero bitpos.  */
15375         if (list_ret == 0)
15376           return 0;
15377         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15378           {
15379             expansion_failed (loc, NULL_RTX,
15380                               "bitfield access");
15381             return 0;
15382           }
15383
15384         if (offset != NULL_TREE)
15385           {
15386             /* Variable offset.  */
15387             list_ret1 = loc_list_from_tree (offset, 0);
15388             if (list_ret1 == 0)
15389               return 0;
15390             add_loc_list (&list_ret, list_ret1);
15391             if (!list_ret)
15392               return 0;
15393             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15394           }
15395
15396         bytepos = bitpos / BITS_PER_UNIT;
15397         if (bytepos > 0)
15398           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15399         else if (bytepos < 0)
15400           loc_list_plus_const (list_ret, bytepos);
15401
15402         have_address = 1;
15403         break;
15404       }
15405
15406     case INTEGER_CST:
15407       if ((want_address || !host_integerp (loc, 0))
15408           && (ret = cst_pool_loc_descr (loc)))
15409         have_address = 1;
15410       else if (want_address == 2
15411                && host_integerp (loc, 0)
15412                && (ret = address_of_int_loc_descriptor
15413                            (int_size_in_bytes (TREE_TYPE (loc)),
15414                             tree_low_cst (loc, 0))))
15415         have_address = 1;
15416       else if (host_integerp (loc, 0))
15417         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15418       else
15419         {
15420           expansion_failed (loc, NULL_RTX,
15421                             "Integer operand is not host integer");
15422           return 0;
15423         }
15424       break;
15425
15426     case CONSTRUCTOR:
15427     case REAL_CST:
15428     case STRING_CST:
15429     case COMPLEX_CST:
15430       if ((ret = cst_pool_loc_descr (loc)))
15431         have_address = 1;
15432       else
15433       /* We can construct small constants here using int_loc_descriptor.  */
15434         expansion_failed (loc, NULL_RTX,
15435                           "constructor or constant not in constant pool");
15436       break;
15437
15438     case TRUTH_AND_EXPR:
15439     case TRUTH_ANDIF_EXPR:
15440     case BIT_AND_EXPR:
15441       op = DW_OP_and;
15442       goto do_binop;
15443
15444     case TRUTH_XOR_EXPR:
15445     case BIT_XOR_EXPR:
15446       op = DW_OP_xor;
15447       goto do_binop;
15448
15449     case TRUTH_OR_EXPR:
15450     case TRUTH_ORIF_EXPR:
15451     case BIT_IOR_EXPR:
15452       op = DW_OP_or;
15453       goto do_binop;
15454
15455     case FLOOR_DIV_EXPR:
15456     case CEIL_DIV_EXPR:
15457     case ROUND_DIV_EXPR:
15458     case TRUNC_DIV_EXPR:
15459       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15460         return 0;
15461       op = DW_OP_div;
15462       goto do_binop;
15463
15464     case MINUS_EXPR:
15465       op = DW_OP_minus;
15466       goto do_binop;
15467
15468     case FLOOR_MOD_EXPR:
15469     case CEIL_MOD_EXPR:
15470     case ROUND_MOD_EXPR:
15471     case TRUNC_MOD_EXPR:
15472       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15473         {
15474           op = DW_OP_mod;
15475           goto do_binop;
15476         }
15477       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15478       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15479       if (list_ret == 0 || list_ret1 == 0)
15480         return 0;
15481
15482       add_loc_list (&list_ret, list_ret1);
15483       if (list_ret == 0)
15484         return 0;
15485       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15486       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15487       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15488       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15489       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15490       break;
15491
15492     case MULT_EXPR:
15493       op = DW_OP_mul;
15494       goto do_binop;
15495
15496     case LSHIFT_EXPR:
15497       op = DW_OP_shl;
15498       goto do_binop;
15499
15500     case RSHIFT_EXPR:
15501       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15502       goto do_binop;
15503
15504     case POINTER_PLUS_EXPR:
15505     case PLUS_EXPR:
15506       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15507         {
15508           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15509           if (list_ret == 0)
15510             return 0;
15511
15512           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15513           break;
15514         }
15515
15516       op = DW_OP_plus;
15517       goto do_binop;
15518
15519     case LE_EXPR:
15520       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15521         return 0;
15522
15523       op = DW_OP_le;
15524       goto do_binop;
15525
15526     case GE_EXPR:
15527       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15528         return 0;
15529
15530       op = DW_OP_ge;
15531       goto do_binop;
15532
15533     case LT_EXPR:
15534       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15535         return 0;
15536
15537       op = DW_OP_lt;
15538       goto do_binop;
15539
15540     case GT_EXPR:
15541       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15542         return 0;
15543
15544       op = DW_OP_gt;
15545       goto do_binop;
15546
15547     case EQ_EXPR:
15548       op = DW_OP_eq;
15549       goto do_binop;
15550
15551     case NE_EXPR:
15552       op = DW_OP_ne;
15553       goto do_binop;
15554
15555     do_binop:
15556       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15557       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15558       if (list_ret == 0 || list_ret1 == 0)
15559         return 0;
15560
15561       add_loc_list (&list_ret, list_ret1);
15562       if (list_ret == 0)
15563         return 0;
15564       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15565       break;
15566
15567     case TRUTH_NOT_EXPR:
15568     case BIT_NOT_EXPR:
15569       op = DW_OP_not;
15570       goto do_unop;
15571
15572     case ABS_EXPR:
15573       op = DW_OP_abs;
15574       goto do_unop;
15575
15576     case NEGATE_EXPR:
15577       op = DW_OP_neg;
15578       goto do_unop;
15579
15580     do_unop:
15581       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15582       if (list_ret == 0)
15583         return 0;
15584
15585       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15586       break;
15587
15588     case MIN_EXPR:
15589     case MAX_EXPR:
15590       {
15591         const enum tree_code code =
15592           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15593
15594         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15595                       build2 (code, integer_type_node,
15596                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15597                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15598       }
15599
15600       /* ... fall through ...  */
15601
15602     case COND_EXPR:
15603       {
15604         dw_loc_descr_ref lhs
15605           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15606         dw_loc_list_ref rhs
15607           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15608         dw_loc_descr_ref bra_node, jump_node, tmp;
15609
15610         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15611         if (list_ret == 0 || lhs == 0 || rhs == 0)
15612           return 0;
15613
15614         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15615         add_loc_descr_to_each (list_ret, bra_node);
15616
15617         add_loc_list (&list_ret, rhs);
15618         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15619         add_loc_descr_to_each (list_ret, jump_node);
15620
15621         add_loc_descr_to_each (list_ret, lhs);
15622         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15623         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15624
15625         /* ??? Need a node to point the skip at.  Use a nop.  */
15626         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15627         add_loc_descr_to_each (list_ret, tmp);
15628         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15629         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15630       }
15631       break;
15632
15633     case FIX_TRUNC_EXPR:
15634       return 0;
15635
15636     default:
15637       /* Leave front-end specific codes as simply unknown.  This comes
15638          up, for instance, with the C STMT_EXPR.  */
15639       if ((unsigned int) TREE_CODE (loc)
15640           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15641         {
15642           expansion_failed (loc, NULL_RTX,
15643                             "language specific tree node");
15644           return 0;
15645         }
15646
15647 #ifdef ENABLE_CHECKING
15648       /* Otherwise this is a generic code; we should just lists all of
15649          these explicitly.  We forgot one.  */
15650       gcc_unreachable ();
15651 #else
15652       /* In a release build, we want to degrade gracefully: better to
15653          generate incomplete debugging information than to crash.  */
15654       return NULL;
15655 #endif
15656     }
15657
15658   if (!ret && !list_ret)
15659     return 0;
15660
15661   if (want_address == 2 && !have_address
15662       && (dwarf_version >= 4 || !dwarf_strict))
15663     {
15664       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15665         {
15666           expansion_failed (loc, NULL_RTX,
15667                             "DWARF address size mismatch");
15668           return 0;
15669         }
15670       if (ret)
15671         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15672       else
15673         add_loc_descr_to_each (list_ret,
15674                                new_loc_descr (DW_OP_stack_value, 0, 0));
15675       have_address = 1;
15676     }
15677   /* Show if we can't fill the request for an address.  */
15678   if (want_address && !have_address)
15679     {
15680       expansion_failed (loc, NULL_RTX,
15681                         "Want address and only have value");
15682       return 0;
15683     }
15684
15685   gcc_assert (!ret || !list_ret);
15686
15687   /* If we've got an address and don't want one, dereference.  */
15688   if (!want_address && have_address)
15689     {
15690       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15691
15692       if (size > DWARF2_ADDR_SIZE || size == -1)
15693         {
15694           expansion_failed (loc, NULL_RTX,
15695                             "DWARF address size mismatch");
15696           return 0;
15697         }
15698       else if (size == DWARF2_ADDR_SIZE)
15699         op = DW_OP_deref;
15700       else
15701         op = DW_OP_deref_size;
15702
15703       if (ret)
15704         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15705       else
15706         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15707     }
15708   if (ret)
15709     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15710
15711   return list_ret;
15712 }
15713
15714 /* Same as above but return only single location expression.  */
15715 static dw_loc_descr_ref
15716 loc_descriptor_from_tree (tree loc, int want_address)
15717 {
15718   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15719   if (!ret)
15720     return NULL;
15721   if (ret->dw_loc_next)
15722     {
15723       expansion_failed (loc, NULL_RTX,
15724                         "Location list where only loc descriptor needed");
15725       return NULL;
15726     }
15727   return ret->expr;
15728 }
15729
15730 /* Given a value, round it up to the lowest multiple of `boundary'
15731    which is not less than the value itself.  */
15732
15733 static inline HOST_WIDE_INT
15734 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15735 {
15736   return (((value + boundary - 1) / boundary) * boundary);
15737 }
15738
15739 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15740    pointer to the declared type for the relevant field variable, or return
15741    `integer_type_node' if the given node turns out to be an
15742    ERROR_MARK node.  */
15743
15744 static inline tree
15745 field_type (const_tree decl)
15746 {
15747   tree type;
15748
15749   if (TREE_CODE (decl) == ERROR_MARK)
15750     return integer_type_node;
15751
15752   type = DECL_BIT_FIELD_TYPE (decl);
15753   if (type == NULL_TREE)
15754     type = TREE_TYPE (decl);
15755
15756   return type;
15757 }
15758
15759 /* Given a pointer to a tree node, return the alignment in bits for
15760    it, or else return BITS_PER_WORD if the node actually turns out to
15761    be an ERROR_MARK node.  */
15762
15763 static inline unsigned
15764 simple_type_align_in_bits (const_tree type)
15765 {
15766   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15767 }
15768
15769 static inline unsigned
15770 simple_decl_align_in_bits (const_tree decl)
15771 {
15772   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15773 }
15774
15775 /* Return the result of rounding T up to ALIGN.  */
15776
15777 static inline double_int
15778 round_up_to_align (double_int t, unsigned int align)
15779 {
15780   double_int alignd = uhwi_to_double_int (align);
15781   t = double_int_add (t, alignd);
15782   t = double_int_add (t, double_int_minus_one);
15783   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15784   t = double_int_mul (t, alignd);
15785   return t;
15786 }
15787
15788 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15789    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15790    or return 0 if we are unable to determine what that offset is, either
15791    because the argument turns out to be a pointer to an ERROR_MARK node, or
15792    because the offset is actually variable.  (We can't handle the latter case
15793    just yet).  */
15794
15795 static HOST_WIDE_INT
15796 field_byte_offset (const_tree decl)
15797 {
15798   double_int object_offset_in_bits;
15799   double_int object_offset_in_bytes;
15800   double_int bitpos_int;
15801
15802   if (TREE_CODE (decl) == ERROR_MARK)
15803     return 0;
15804
15805   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15806
15807   /* We cannot yet cope with fields whose positions are variable, so
15808      for now, when we see such things, we simply return 0.  Someday, we may
15809      be able to handle such cases, but it will be damn difficult.  */
15810   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15811     return 0;
15812
15813   bitpos_int = tree_to_double_int (bit_position (decl));
15814
15815 #ifdef PCC_BITFIELD_TYPE_MATTERS
15816   if (PCC_BITFIELD_TYPE_MATTERS)
15817     {
15818       tree type;
15819       tree field_size_tree;
15820       double_int deepest_bitpos;
15821       double_int field_size_in_bits;
15822       unsigned int type_align_in_bits;
15823       unsigned int decl_align_in_bits;
15824       double_int type_size_in_bits;
15825
15826       type = field_type (decl);
15827       type_size_in_bits = double_int_type_size_in_bits (type);
15828       type_align_in_bits = simple_type_align_in_bits (type);
15829
15830       field_size_tree = DECL_SIZE (decl);
15831
15832       /* The size could be unspecified if there was an error, or for
15833          a flexible array member.  */
15834       if (!field_size_tree)
15835         field_size_tree = bitsize_zero_node;
15836
15837       /* If the size of the field is not constant, use the type size.  */
15838       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15839         field_size_in_bits = tree_to_double_int (field_size_tree);
15840       else
15841         field_size_in_bits = type_size_in_bits;
15842
15843       decl_align_in_bits = simple_decl_align_in_bits (decl);
15844
15845       /* The GCC front-end doesn't make any attempt to keep track of the
15846          starting bit offset (relative to the start of the containing
15847          structure type) of the hypothetical "containing object" for a
15848          bit-field.  Thus, when computing the byte offset value for the
15849          start of the "containing object" of a bit-field, we must deduce
15850          this information on our own. This can be rather tricky to do in
15851          some cases.  For example, handling the following structure type
15852          definition when compiling for an i386/i486 target (which only
15853          aligns long long's to 32-bit boundaries) can be very tricky:
15854
15855          struct S { int field1; long long field2:31; };
15856
15857          Fortunately, there is a simple rule-of-thumb which can be used
15858          in such cases.  When compiling for an i386/i486, GCC will
15859          allocate 8 bytes for the structure shown above.  It decides to
15860          do this based upon one simple rule for bit-field allocation.
15861          GCC allocates each "containing object" for each bit-field at
15862          the first (i.e. lowest addressed) legitimate alignment boundary
15863          (based upon the required minimum alignment for the declared
15864          type of the field) which it can possibly use, subject to the
15865          condition that there is still enough available space remaining
15866          in the containing object (when allocated at the selected point)
15867          to fully accommodate all of the bits of the bit-field itself.
15868
15869          This simple rule makes it obvious why GCC allocates 8 bytes for
15870          each object of the structure type shown above.  When looking
15871          for a place to allocate the "containing object" for `field2',
15872          the compiler simply tries to allocate a 64-bit "containing
15873          object" at each successive 32-bit boundary (starting at zero)
15874          until it finds a place to allocate that 64- bit field such that
15875          at least 31 contiguous (and previously unallocated) bits remain
15876          within that selected 64 bit field.  (As it turns out, for the
15877          example above, the compiler finds it is OK to allocate the
15878          "containing object" 64-bit field at bit-offset zero within the
15879          structure type.)
15880
15881          Here we attempt to work backwards from the limited set of facts
15882          we're given, and we try to deduce from those facts, where GCC
15883          must have believed that the containing object started (within
15884          the structure type). The value we deduce is then used (by the
15885          callers of this routine) to generate DW_AT_location and
15886          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15887          the case of DW_AT_location, regular fields as well).  */
15888
15889       /* Figure out the bit-distance from the start of the structure to
15890          the "deepest" bit of the bit-field.  */
15891       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15892
15893       /* This is the tricky part.  Use some fancy footwork to deduce
15894          where the lowest addressed bit of the containing object must
15895          be.  */
15896       object_offset_in_bits
15897         = double_int_sub (deepest_bitpos, type_size_in_bits);
15898
15899       /* Round up to type_align by default.  This works best for
15900          bitfields.  */
15901       object_offset_in_bits
15902         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15903
15904       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15905         {
15906           object_offset_in_bits
15907             = double_int_sub (deepest_bitpos, type_size_in_bits);
15908
15909           /* Round up to decl_align instead.  */
15910           object_offset_in_bits
15911             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15912         }
15913     }
15914   else
15915 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15916     object_offset_in_bits = bitpos_int;
15917
15918   object_offset_in_bytes
15919     = double_int_div (object_offset_in_bits,
15920                       uhwi_to_double_int (BITS_PER_UNIT), true,
15921                       TRUNC_DIV_EXPR);
15922   return double_int_to_shwi (object_offset_in_bytes);
15923 }
15924 \f
15925 /* The following routines define various Dwarf attributes and any data
15926    associated with them.  */
15927
15928 /* Add a location description attribute value to a DIE.
15929
15930    This emits location attributes suitable for whole variables and
15931    whole parameters.  Note that the location attributes for struct fields are
15932    generated by the routine `data_member_location_attribute' below.  */
15933
15934 static inline void
15935 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15936                              dw_loc_list_ref descr)
15937 {
15938   if (descr == 0)
15939     return;
15940   if (single_element_loc_list_p (descr))
15941     add_AT_loc (die, attr_kind, descr->expr);
15942   else
15943     add_AT_loc_list (die, attr_kind, descr);
15944 }
15945
15946 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15947
15948 static void
15949 add_accessibility_attribute (dw_die_ref die, tree decl)
15950 {
15951   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15952      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15953      the default has always been DW_ACCESS_public.  */
15954   if (TREE_PROTECTED (decl))
15955     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15956   else if (TREE_PRIVATE (decl))
15957     {
15958       if (dwarf_version == 2
15959           || die->die_parent == NULL
15960           || die->die_parent->die_tag != DW_TAG_class_type)
15961         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15962     }
15963   else if (dwarf_version > 2
15964            && die->die_parent
15965            && die->die_parent->die_tag == DW_TAG_class_type)
15966     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15967 }
15968
15969 /* Attach the specialized form of location attribute used for data members of
15970    struct and union types.  In the special case of a FIELD_DECL node which
15971    represents a bit-field, the "offset" part of this special location
15972    descriptor must indicate the distance in bytes from the lowest-addressed
15973    byte of the containing struct or union type to the lowest-addressed byte of
15974    the "containing object" for the bit-field.  (See the `field_byte_offset'
15975    function above).
15976
15977    For any given bit-field, the "containing object" is a hypothetical object
15978    (of some integral or enum type) within which the given bit-field lives.  The
15979    type of this hypothetical "containing object" is always the same as the
15980    declared type of the individual bit-field itself (for GCC anyway... the
15981    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15982    bytes) of the hypothetical "containing object" which will be given in the
15983    DW_AT_byte_size attribute for this bit-field.  (See the
15984    `byte_size_attribute' function below.)  It is also used when calculating the
15985    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15986    function below.)  */
15987
15988 static void
15989 add_data_member_location_attribute (dw_die_ref die, tree decl)
15990 {
15991   HOST_WIDE_INT offset;
15992   dw_loc_descr_ref loc_descr = 0;
15993
15994   if (TREE_CODE (decl) == TREE_BINFO)
15995     {
15996       /* We're working on the TAG_inheritance for a base class.  */
15997       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15998         {
15999           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16000              aren't at a fixed offset from all (sub)objects of the same
16001              type.  We need to extract the appropriate offset from our
16002              vtable.  The following dwarf expression means
16003
16004                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16005
16006              This is specific to the V3 ABI, of course.  */
16007
16008           dw_loc_descr_ref tmp;
16009
16010           /* Make a copy of the object address.  */
16011           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16012           add_loc_descr (&loc_descr, tmp);
16013
16014           /* Extract the vtable address.  */
16015           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16016           add_loc_descr (&loc_descr, tmp);
16017
16018           /* Calculate the address of the offset.  */
16019           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16020           gcc_assert (offset < 0);
16021
16022           tmp = int_loc_descriptor (-offset);
16023           add_loc_descr (&loc_descr, tmp);
16024           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16025           add_loc_descr (&loc_descr, tmp);
16026
16027           /* Extract the offset.  */
16028           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16029           add_loc_descr (&loc_descr, tmp);
16030
16031           /* Add it to the object address.  */
16032           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16033           add_loc_descr (&loc_descr, tmp);
16034         }
16035       else
16036         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16037     }
16038   else
16039     offset = field_byte_offset (decl);
16040
16041   if (! loc_descr)
16042     {
16043       if (dwarf_version > 2)
16044         {
16045           /* Don't need to output a location expression, just the constant. */
16046           if (offset < 0)
16047             add_AT_int (die, DW_AT_data_member_location, offset);
16048           else
16049             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16050           return;
16051         }
16052       else
16053         {
16054           enum dwarf_location_atom op;
16055
16056           /* The DWARF2 standard says that we should assume that the structure
16057              address is already on the stack, so we can specify a structure
16058              field address by using DW_OP_plus_uconst.  */
16059
16060 #ifdef MIPS_DEBUGGING_INFO
16061           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16062              operator correctly.  It works only if we leave the offset on the
16063              stack.  */
16064           op = DW_OP_constu;
16065 #else
16066           op = DW_OP_plus_uconst;
16067 #endif
16068
16069           loc_descr = new_loc_descr (op, offset, 0);
16070         }
16071     }
16072
16073   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16074 }
16075
16076 /* Writes integer values to dw_vec_const array.  */
16077
16078 static void
16079 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16080 {
16081   while (size != 0)
16082     {
16083       *dest++ = val & 0xff;
16084       val >>= 8;
16085       --size;
16086     }
16087 }
16088
16089 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16090
16091 static HOST_WIDE_INT
16092 extract_int (const unsigned char *src, unsigned int size)
16093 {
16094   HOST_WIDE_INT val = 0;
16095
16096   src += size;
16097   while (size != 0)
16098     {
16099       val <<= 8;
16100       val |= *--src & 0xff;
16101       --size;
16102     }
16103   return val;
16104 }
16105
16106 /* Writes double_int values to dw_vec_const array.  */
16107
16108 static void
16109 insert_double (double_int val, unsigned char *dest)
16110 {
16111   unsigned char *p0 = dest;
16112   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16113
16114   if (WORDS_BIG_ENDIAN)
16115     {
16116       p0 = p1;
16117       p1 = dest;
16118     }
16119
16120   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16121   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16122 }
16123
16124 /* Writes floating point values to dw_vec_const array.  */
16125
16126 static void
16127 insert_float (const_rtx rtl, unsigned char *array)
16128 {
16129   REAL_VALUE_TYPE rv;
16130   long val[4];
16131   int i;
16132
16133   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16134   real_to_target (val, &rv, GET_MODE (rtl));
16135
16136   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16137   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16138     {
16139       insert_int (val[i], 4, array);
16140       array += 4;
16141     }
16142 }
16143
16144 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16145    does not have a "location" either in memory or in a register.  These
16146    things can arise in GNU C when a constant is passed as an actual parameter
16147    to an inlined function.  They can also arise in C++ where declared
16148    constants do not necessarily get memory "homes".  */
16149
16150 static bool
16151 add_const_value_attribute (dw_die_ref die, rtx rtl)
16152 {
16153   switch (GET_CODE (rtl))
16154     {
16155     case CONST_INT:
16156       {
16157         HOST_WIDE_INT val = INTVAL (rtl);
16158
16159         if (val < 0)
16160           add_AT_int (die, DW_AT_const_value, val);
16161         else
16162           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16163       }
16164       return true;
16165
16166     case CONST_DOUBLE:
16167       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16168          floating-point constant.  A CONST_DOUBLE is used whenever the
16169          constant requires more than one word in order to be adequately
16170          represented.  */
16171       {
16172         enum machine_mode mode = GET_MODE (rtl);
16173
16174         if (SCALAR_FLOAT_MODE_P (mode))
16175           {
16176             unsigned int length = GET_MODE_SIZE (mode);
16177             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16178
16179             insert_float (rtl, array);
16180             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16181           }
16182         else
16183           add_AT_double (die, DW_AT_const_value,
16184                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16185       }
16186       return true;
16187
16188     case CONST_VECTOR:
16189       {
16190         enum machine_mode mode = GET_MODE (rtl);
16191         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16192         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16193         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16194           (length * elt_size);
16195         unsigned int i;
16196         unsigned char *p;
16197
16198         switch (GET_MODE_CLASS (mode))
16199           {
16200           case MODE_VECTOR_INT:
16201             for (i = 0, p = array; i < length; i++, p += elt_size)
16202               {
16203                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16204                 double_int val = rtx_to_double_int (elt);
16205
16206                 if (elt_size <= sizeof (HOST_WIDE_INT))
16207                   insert_int (double_int_to_shwi (val), elt_size, p);
16208                 else
16209                   {
16210                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16211                     insert_double (val, p);
16212                   }
16213               }
16214             break;
16215
16216           case MODE_VECTOR_FLOAT:
16217             for (i = 0, p = array; i < length; i++, p += elt_size)
16218               {
16219                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16220                 insert_float (elt, p);
16221               }
16222             break;
16223
16224           default:
16225             gcc_unreachable ();
16226           }
16227
16228         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16229       }
16230       return true;
16231
16232     case CONST_STRING:
16233       if (dwarf_version >= 4 || !dwarf_strict)
16234         {
16235           dw_loc_descr_ref loc_result;
16236           resolve_one_addr (&rtl, NULL);
16237         rtl_addr:
16238           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16239           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16240           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16241           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16242           add_AT_loc (die, DW_AT_location, loc_result);
16243           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16244           return true;
16245         }
16246       return false;
16247
16248     case CONST:
16249       if (CONSTANT_P (XEXP (rtl, 0)))
16250         return add_const_value_attribute (die, XEXP (rtl, 0));
16251       /* FALLTHROUGH */
16252     case SYMBOL_REF:
16253       if (!const_ok_for_output (rtl))
16254         return false;
16255     case LABEL_REF:
16256       if (dwarf_version >= 4 || !dwarf_strict)
16257         goto rtl_addr;
16258       return false;
16259
16260     case PLUS:
16261       /* In cases where an inlined instance of an inline function is passed
16262          the address of an `auto' variable (which is local to the caller) we
16263          can get a situation where the DECL_RTL of the artificial local
16264          variable (for the inlining) which acts as a stand-in for the
16265          corresponding formal parameter (of the inline function) will look
16266          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16267          exactly a compile-time constant expression, but it isn't the address
16268          of the (artificial) local variable either.  Rather, it represents the
16269          *value* which the artificial local variable always has during its
16270          lifetime.  We currently have no way to represent such quasi-constant
16271          values in Dwarf, so for now we just punt and generate nothing.  */
16272       return false;
16273
16274     case HIGH:
16275     case CONST_FIXED:
16276       return false;
16277
16278     case MEM:
16279       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16280           && MEM_READONLY_P (rtl)
16281           && GET_MODE (rtl) == BLKmode)
16282         {
16283           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16284           return true;
16285         }
16286       return false;
16287
16288     default:
16289       /* No other kinds of rtx should be possible here.  */
16290       gcc_unreachable ();
16291     }
16292   return false;
16293 }
16294
16295 /* Determine whether the evaluation of EXPR references any variables
16296    or functions which aren't otherwise used (and therefore may not be
16297    output).  */
16298 static tree
16299 reference_to_unused (tree * tp, int * walk_subtrees,
16300                      void * data ATTRIBUTE_UNUSED)
16301 {
16302   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16303     *walk_subtrees = 0;
16304
16305   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16306       && ! TREE_ASM_WRITTEN (*tp))
16307     return *tp;
16308   /* ???  The C++ FE emits debug information for using decls, so
16309      putting gcc_unreachable here falls over.  See PR31899.  For now
16310      be conservative.  */
16311   else if (!cgraph_global_info_ready
16312            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16313     return *tp;
16314   else if (TREE_CODE (*tp) == VAR_DECL)
16315     {
16316       struct varpool_node *node = varpool_get_node (*tp);
16317       if (!node || !node->needed)
16318         return *tp;
16319     }
16320   else if (TREE_CODE (*tp) == FUNCTION_DECL
16321            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16322     {
16323       /* The call graph machinery must have finished analyzing,
16324          optimizing and gimplifying the CU by now.
16325          So if *TP has no call graph node associated
16326          to it, it means *TP will not be emitted.  */
16327       if (!cgraph_get_node (*tp))
16328         return *tp;
16329     }
16330   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16331     return *tp;
16332
16333   return NULL_TREE;
16334 }
16335
16336 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16337    for use in a later add_const_value_attribute call.  */
16338
16339 static rtx
16340 rtl_for_decl_init (tree init, tree type)
16341 {
16342   rtx rtl = NULL_RTX;
16343
16344   /* If a variable is initialized with a string constant without embedded
16345      zeros, build CONST_STRING.  */
16346   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16347     {
16348       tree enttype = TREE_TYPE (type);
16349       tree domain = TYPE_DOMAIN (type);
16350       enum machine_mode mode = TYPE_MODE (enttype);
16351
16352       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16353           && domain
16354           && integer_zerop (TYPE_MIN_VALUE (domain))
16355           && compare_tree_int (TYPE_MAX_VALUE (domain),
16356                                TREE_STRING_LENGTH (init) - 1) == 0
16357           && ((size_t) TREE_STRING_LENGTH (init)
16358               == strlen (TREE_STRING_POINTER (init)) + 1))
16359         {
16360           rtl = gen_rtx_CONST_STRING (VOIDmode,
16361                                       ggc_strdup (TREE_STRING_POINTER (init)));
16362           rtl = gen_rtx_MEM (BLKmode, rtl);
16363           MEM_READONLY_P (rtl) = 1;
16364         }
16365     }
16366   /* Other aggregates, and complex values, could be represented using
16367      CONCAT: FIXME!  */
16368   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16369     ;
16370   /* Vectors only work if their mode is supported by the target.
16371      FIXME: generic vectors ought to work too.  */
16372   else if (TREE_CODE (type) == VECTOR_TYPE
16373            && !VECTOR_MODE_P (TYPE_MODE (type)))
16374     ;
16375   /* If the initializer is something that we know will expand into an
16376      immediate RTL constant, expand it now.  We must be careful not to
16377      reference variables which won't be output.  */
16378   else if (initializer_constant_valid_p (init, type)
16379            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16380     {
16381       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16382          possible.  */
16383       if (TREE_CODE (type) == VECTOR_TYPE)
16384         switch (TREE_CODE (init))
16385           {
16386           case VECTOR_CST:
16387             break;
16388           case CONSTRUCTOR:
16389             if (TREE_CONSTANT (init))
16390               {
16391                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16392                 bool constant_p = true;
16393                 tree value;
16394                 unsigned HOST_WIDE_INT ix;
16395
16396                 /* Even when ctor is constant, it might contain non-*_CST
16397                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16398                    belong into VECTOR_CST nodes.  */
16399                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16400                   if (!CONSTANT_CLASS_P (value))
16401                     {
16402                       constant_p = false;
16403                       break;
16404                     }
16405
16406                 if (constant_p)
16407                   {
16408                     init = build_vector_from_ctor (type, elts);
16409                     break;
16410                   }
16411               }
16412             /* FALLTHRU */
16413
16414           default:
16415             return NULL;
16416           }
16417
16418       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16419
16420       /* If expand_expr returns a MEM, it wasn't immediate.  */
16421       gcc_assert (!rtl || !MEM_P (rtl));
16422     }
16423
16424   return rtl;
16425 }
16426
16427 /* Generate RTL for the variable DECL to represent its location.  */
16428
16429 static rtx
16430 rtl_for_decl_location (tree decl)
16431 {
16432   rtx rtl;
16433
16434   /* Here we have to decide where we are going to say the parameter "lives"
16435      (as far as the debugger is concerned).  We only have a couple of
16436      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16437
16438      DECL_RTL normally indicates where the parameter lives during most of the
16439      activation of the function.  If optimization is enabled however, this
16440      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16441      that the parameter doesn't really live anywhere (as far as the code
16442      generation parts of GCC are concerned) during most of the function's
16443      activation.  That will happen (for example) if the parameter is never
16444      referenced within the function.
16445
16446      We could just generate a location descriptor here for all non-NULL
16447      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16448      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16449      where DECL_RTL is NULL or is a pseudo-reg.
16450
16451      Note however that we can only get away with using DECL_INCOMING_RTL as
16452      a backup substitute for DECL_RTL in certain limited cases.  In cases
16453      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16454      we can be sure that the parameter was passed using the same type as it is
16455      declared to have within the function, and that its DECL_INCOMING_RTL
16456      points us to a place where a value of that type is passed.
16457
16458      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16459      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16460      because in these cases DECL_INCOMING_RTL points us to a value of some
16461      type which is *different* from the type of the parameter itself.  Thus,
16462      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16463      such cases, the debugger would end up (for example) trying to fetch a
16464      `float' from a place which actually contains the first part of a
16465      `double'.  That would lead to really incorrect and confusing
16466      output at debug-time.
16467
16468      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16469      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16470      are a couple of exceptions however.  On little-endian machines we can
16471      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16472      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16473      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16474      when (on a little-endian machine) a non-prototyped function has a
16475      parameter declared to be of type `short' or `char'.  In such cases,
16476      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16477      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16478      passed `int' value.  If the debugger then uses that address to fetch
16479      a `short' or a `char' (on a little-endian machine) the result will be
16480      the correct data, so we allow for such exceptional cases below.
16481
16482      Note that our goal here is to describe the place where the given formal
16483      parameter lives during most of the function's activation (i.e. between the
16484      end of the prologue and the start of the epilogue).  We'll do that as best
16485      as we can. Note however that if the given formal parameter is modified
16486      sometime during the execution of the function, then a stack backtrace (at
16487      debug-time) will show the function as having been called with the *new*
16488      value rather than the value which was originally passed in.  This happens
16489      rarely enough that it is not a major problem, but it *is* a problem, and
16490      I'd like to fix it.
16491
16492      A future version of dwarf2out.c may generate two additional attributes for
16493      any given DW_TAG_formal_parameter DIE which will describe the "passed
16494      type" and the "passed location" for the given formal parameter in addition
16495      to the attributes we now generate to indicate the "declared type" and the
16496      "active location" for each parameter.  This additional set of attributes
16497      could be used by debuggers for stack backtraces. Separately, note that
16498      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16499      This happens (for example) for inlined-instances of inline function formal
16500      parameters which are never referenced.  This really shouldn't be
16501      happening.  All PARM_DECL nodes should get valid non-NULL
16502      DECL_INCOMING_RTL values.  FIXME.  */
16503
16504   /* Use DECL_RTL as the "location" unless we find something better.  */
16505   rtl = DECL_RTL_IF_SET (decl);
16506
16507   /* When generating abstract instances, ignore everything except
16508      constants, symbols living in memory, and symbols living in
16509      fixed registers.  */
16510   if (! reload_completed)
16511     {
16512       if (rtl
16513           && (CONSTANT_P (rtl)
16514               || (MEM_P (rtl)
16515                   && CONSTANT_P (XEXP (rtl, 0)))
16516               || (REG_P (rtl)
16517                   && TREE_CODE (decl) == VAR_DECL
16518                   && TREE_STATIC (decl))))
16519         {
16520           rtl = targetm.delegitimize_address (rtl);
16521           return rtl;
16522         }
16523       rtl = NULL_RTX;
16524     }
16525   else if (TREE_CODE (decl) == PARM_DECL)
16526     {
16527       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16528         {
16529           tree declared_type = TREE_TYPE (decl);
16530           tree passed_type = DECL_ARG_TYPE (decl);
16531           enum machine_mode dmode = TYPE_MODE (declared_type);
16532           enum machine_mode pmode = TYPE_MODE (passed_type);
16533
16534           /* This decl represents a formal parameter which was optimized out.
16535              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16536              all cases where (rtl == NULL_RTX) just below.  */
16537           if (dmode == pmode)
16538             rtl = DECL_INCOMING_RTL (decl);
16539           else if (SCALAR_INT_MODE_P (dmode)
16540                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16541                    && DECL_INCOMING_RTL (decl))
16542             {
16543               rtx inc = DECL_INCOMING_RTL (decl);
16544               if (REG_P (inc))
16545                 rtl = inc;
16546               else if (MEM_P (inc))
16547                 {
16548                   if (BYTES_BIG_ENDIAN)
16549                     rtl = adjust_address_nv (inc, dmode,
16550                                              GET_MODE_SIZE (pmode)
16551                                              - GET_MODE_SIZE (dmode));
16552                   else
16553                     rtl = inc;
16554                 }
16555             }
16556         }
16557
16558       /* If the parm was passed in registers, but lives on the stack, then
16559          make a big endian correction if the mode of the type of the
16560          parameter is not the same as the mode of the rtl.  */
16561       /* ??? This is the same series of checks that are made in dbxout.c before
16562          we reach the big endian correction code there.  It isn't clear if all
16563          of these checks are necessary here, but keeping them all is the safe
16564          thing to do.  */
16565       else if (MEM_P (rtl)
16566                && XEXP (rtl, 0) != const0_rtx
16567                && ! CONSTANT_P (XEXP (rtl, 0))
16568                /* Not passed in memory.  */
16569                && !MEM_P (DECL_INCOMING_RTL (decl))
16570                /* Not passed by invisible reference.  */
16571                && (!REG_P (XEXP (rtl, 0))
16572                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16573                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16574 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16575                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16576 #endif
16577                      )
16578                /* Big endian correction check.  */
16579                && BYTES_BIG_ENDIAN
16580                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16581                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16582                    < UNITS_PER_WORD))
16583         {
16584           int offset = (UNITS_PER_WORD
16585                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16586
16587           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16588                              plus_constant (XEXP (rtl, 0), offset));
16589         }
16590     }
16591   else if (TREE_CODE (decl) == VAR_DECL
16592            && rtl
16593            && MEM_P (rtl)
16594            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16595            && BYTES_BIG_ENDIAN)
16596     {
16597       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16598       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16599
16600       /* If a variable is declared "register" yet is smaller than
16601          a register, then if we store the variable to memory, it
16602          looks like we're storing a register-sized value, when in
16603          fact we are not.  We need to adjust the offset of the
16604          storage location to reflect the actual value's bytes,
16605          else gdb will not be able to display it.  */
16606       if (rsize > dsize)
16607         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16608                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16609     }
16610
16611   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16612      and will have been substituted directly into all expressions that use it.
16613      C does not have such a concept, but C++ and other languages do.  */
16614   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16615     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16616
16617   if (rtl)
16618     rtl = targetm.delegitimize_address (rtl);
16619
16620   /* If we don't look past the constant pool, we risk emitting a
16621      reference to a constant pool entry that isn't referenced from
16622      code, and thus is not emitted.  */
16623   if (rtl)
16624     rtl = avoid_constant_pool_reference (rtl);
16625
16626   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16627      in the current CU, resolve_addr will remove the expression referencing
16628      it.  */
16629   if (rtl == NULL_RTX
16630       && TREE_CODE (decl) == VAR_DECL
16631       && !DECL_EXTERNAL (decl)
16632       && TREE_STATIC (decl)
16633       && DECL_NAME (decl)
16634       && !DECL_HARD_REGISTER (decl)
16635       && DECL_MODE (decl) != VOIDmode)
16636     {
16637       rtl = make_decl_rtl_for_debug (decl);
16638       if (!MEM_P (rtl)
16639           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16640           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16641         rtl = NULL_RTX;
16642     }
16643
16644   return rtl;
16645 }
16646
16647 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16648    returned.  If so, the decl for the COMMON block is returned, and the
16649    value is the offset into the common block for the symbol.  */
16650
16651 static tree
16652 fortran_common (tree decl, HOST_WIDE_INT *value)
16653 {
16654   tree val_expr, cvar;
16655   enum machine_mode mode;
16656   HOST_WIDE_INT bitsize, bitpos;
16657   tree offset;
16658   int volatilep = 0, unsignedp = 0;
16659
16660   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16661      it does not have a value (the offset into the common area), or if it
16662      is thread local (as opposed to global) then it isn't common, and shouldn't
16663      be handled as such.  */
16664   if (TREE_CODE (decl) != VAR_DECL
16665       || !TREE_STATIC (decl)
16666       || !DECL_HAS_VALUE_EXPR_P (decl)
16667       || !is_fortran ())
16668     return NULL_TREE;
16669
16670   val_expr = DECL_VALUE_EXPR (decl);
16671   if (TREE_CODE (val_expr) != COMPONENT_REF)
16672     return NULL_TREE;
16673
16674   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16675                               &mode, &unsignedp, &volatilep, true);
16676
16677   if (cvar == NULL_TREE
16678       || TREE_CODE (cvar) != VAR_DECL
16679       || DECL_ARTIFICIAL (cvar)
16680       || !TREE_PUBLIC (cvar))
16681     return NULL_TREE;
16682
16683   *value = 0;
16684   if (offset != NULL)
16685     {
16686       if (!host_integerp (offset, 0))
16687         return NULL_TREE;
16688       *value = tree_low_cst (offset, 0);
16689     }
16690   if (bitpos != 0)
16691     *value += bitpos / BITS_PER_UNIT;
16692
16693   return cvar;
16694 }
16695
16696 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16697    data attribute for a variable or a parameter.  We generate the
16698    DW_AT_const_value attribute only in those cases where the given variable
16699    or parameter does not have a true "location" either in memory or in a
16700    register.  This can happen (for example) when a constant is passed as an
16701    actual argument in a call to an inline function.  (It's possible that
16702    these things can crop up in other ways also.)  Note that one type of
16703    constant value which can be passed into an inlined function is a constant
16704    pointer.  This can happen for example if an actual argument in an inlined
16705    function call evaluates to a compile-time constant address.  */
16706
16707 static bool
16708 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16709                                        enum dwarf_attribute attr)
16710 {
16711   rtx rtl;
16712   dw_loc_list_ref list;
16713   var_loc_list *loc_list;
16714
16715   if (TREE_CODE (decl) == ERROR_MARK)
16716     return false;
16717
16718   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16719               || TREE_CODE (decl) == RESULT_DECL);
16720
16721   /* Try to get some constant RTL for this decl, and use that as the value of
16722      the location.  */
16723
16724   rtl = rtl_for_decl_location (decl);
16725   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16726       && add_const_value_attribute (die, rtl))
16727     return true;
16728
16729   /* See if we have single element location list that is equivalent to
16730      a constant value.  That way we are better to use add_const_value_attribute
16731      rather than expanding constant value equivalent.  */
16732   loc_list = lookup_decl_loc (decl);
16733   if (loc_list
16734       && loc_list->first
16735       && loc_list->first->next == NULL
16736       && NOTE_P (loc_list->first->loc)
16737       && NOTE_VAR_LOCATION (loc_list->first->loc)
16738       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16739     {
16740       struct var_loc_node *node;
16741
16742       node = loc_list->first;
16743       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16744       if (GET_CODE (rtl) == EXPR_LIST)
16745         rtl = XEXP (rtl, 0);
16746       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16747           && add_const_value_attribute (die, rtl))
16748          return true;
16749     }
16750   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16751   if (list)
16752     {
16753       add_AT_location_description (die, attr, list);
16754       return true;
16755     }
16756   /* None of that worked, so it must not really have a location;
16757      try adding a constant value attribute from the DECL_INITIAL.  */
16758   return tree_add_const_value_attribute_for_decl (die, decl);
16759 }
16760
16761 /* Add VARIABLE and DIE into deferred locations list.  */
16762
16763 static void
16764 defer_location (tree variable, dw_die_ref die)
16765 {
16766   deferred_locations entry;
16767   entry.variable = variable;
16768   entry.die = die;
16769   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16770 }
16771
16772 /* Helper function for tree_add_const_value_attribute.  Natively encode
16773    initializer INIT into an array.  Return true if successful.  */
16774
16775 static bool
16776 native_encode_initializer (tree init, unsigned char *array, int size)
16777 {
16778   tree type;
16779
16780   if (init == NULL_TREE)
16781     return false;
16782
16783   STRIP_NOPS (init);
16784   switch (TREE_CODE (init))
16785     {
16786     case STRING_CST:
16787       type = TREE_TYPE (init);
16788       if (TREE_CODE (type) == ARRAY_TYPE)
16789         {
16790           tree enttype = TREE_TYPE (type);
16791           enum machine_mode mode = TYPE_MODE (enttype);
16792
16793           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16794             return false;
16795           if (int_size_in_bytes (type) != size)
16796             return false;
16797           if (size > TREE_STRING_LENGTH (init))
16798             {
16799               memcpy (array, TREE_STRING_POINTER (init),
16800                       TREE_STRING_LENGTH (init));
16801               memset (array + TREE_STRING_LENGTH (init),
16802                       '\0', size - TREE_STRING_LENGTH (init));
16803             }
16804           else
16805             memcpy (array, TREE_STRING_POINTER (init), size);
16806           return true;
16807         }
16808       return false;
16809     case CONSTRUCTOR:
16810       type = TREE_TYPE (init);
16811       if (int_size_in_bytes (type) != size)
16812         return false;
16813       if (TREE_CODE (type) == ARRAY_TYPE)
16814         {
16815           HOST_WIDE_INT min_index;
16816           unsigned HOST_WIDE_INT cnt;
16817           int curpos = 0, fieldsize;
16818           constructor_elt *ce;
16819
16820           if (TYPE_DOMAIN (type) == NULL_TREE
16821               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16822             return false;
16823
16824           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16825           if (fieldsize <= 0)
16826             return false;
16827
16828           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16829           memset (array, '\0', size);
16830           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16831             {
16832               tree val = ce->value;
16833               tree index = ce->index;
16834               int pos = curpos;
16835               if (index && TREE_CODE (index) == RANGE_EXPR)
16836                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16837                       * fieldsize;
16838               else if (index)
16839                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16840
16841               if (val)
16842                 {
16843                   STRIP_NOPS (val);
16844                   if (!native_encode_initializer (val, array + pos, fieldsize))
16845                     return false;
16846                 }
16847               curpos = pos + fieldsize;
16848               if (index && TREE_CODE (index) == RANGE_EXPR)
16849                 {
16850                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16851                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16852                   while (count > 0)
16853                     {
16854                       if (val)
16855                         memcpy (array + curpos, array + pos, fieldsize);
16856                       curpos += fieldsize;
16857                     }
16858                 }
16859               gcc_assert (curpos <= size);
16860             }
16861           return true;
16862         }
16863       else if (TREE_CODE (type) == RECORD_TYPE
16864                || TREE_CODE (type) == UNION_TYPE)
16865         {
16866           tree field = NULL_TREE;
16867           unsigned HOST_WIDE_INT cnt;
16868           constructor_elt *ce;
16869
16870           if (int_size_in_bytes (type) != size)
16871             return false;
16872
16873           if (TREE_CODE (type) == RECORD_TYPE)
16874             field = TYPE_FIELDS (type);
16875
16876           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16877             {
16878               tree val = ce->value;
16879               int pos, fieldsize;
16880
16881               if (ce->index != 0)
16882                 field = ce->index;
16883
16884               if (val)
16885                 STRIP_NOPS (val);
16886
16887               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16888                 return false;
16889
16890               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16891                   && TYPE_DOMAIN (TREE_TYPE (field))
16892                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16893                 return false;
16894               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16895                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16896                 return false;
16897               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16898               pos = int_byte_position (field);
16899               gcc_assert (pos + fieldsize <= size);
16900               if (val
16901                   && !native_encode_initializer (val, array + pos, fieldsize))
16902                 return false;
16903             }
16904           return true;
16905         }
16906       return false;
16907     case VIEW_CONVERT_EXPR:
16908     case NON_LVALUE_EXPR:
16909       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16910     default:
16911       return native_encode_expr (init, array, size) == size;
16912     }
16913 }
16914
16915 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16916    attribute is the const value T.  */
16917
16918 static bool
16919 tree_add_const_value_attribute (dw_die_ref die, tree t)
16920 {
16921   tree init;
16922   tree type = TREE_TYPE (t);
16923   rtx rtl;
16924
16925   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16926     return false;
16927
16928   init = t;
16929   gcc_assert (!DECL_P (init));
16930
16931   rtl = rtl_for_decl_init (init, type);
16932   if (rtl)
16933     return add_const_value_attribute (die, rtl);
16934   /* If the host and target are sane, try harder.  */
16935   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16936            && initializer_constant_valid_p (init, type))
16937     {
16938       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16939       if (size > 0 && (int) size == size)
16940         {
16941           unsigned char *array = (unsigned char *)
16942             ggc_alloc_cleared_atomic (size);
16943
16944           if (native_encode_initializer (init, array, size))
16945             {
16946               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16947               return true;
16948             }
16949         }
16950     }
16951   return false;
16952 }
16953
16954 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16955    attribute is the const value of T, where T is an integral constant
16956    variable with static storage duration
16957    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16958
16959 static bool
16960 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16961 {
16962
16963   if (!decl
16964       || (TREE_CODE (decl) != VAR_DECL
16965           && TREE_CODE (decl) != CONST_DECL))
16966     return false;
16967
16968     if (TREE_READONLY (decl)
16969         && ! TREE_THIS_VOLATILE (decl)
16970         && DECL_INITIAL (decl))
16971       /* OK */;
16972     else
16973       return false;
16974
16975   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16976   if (get_AT (var_die, DW_AT_const_value))
16977     return false;
16978
16979   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16980 }
16981
16982 /* Convert the CFI instructions for the current function into a
16983    location list.  This is used for DW_AT_frame_base when we targeting
16984    a dwarf2 consumer that does not support the dwarf3
16985    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16986    expressions.  */
16987
16988 static dw_loc_list_ref
16989 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16990 {
16991   dw_fde_ref fde;
16992   dw_loc_list_ref list, *list_tail;
16993   dw_cfi_ref cfi;
16994   dw_cfa_location last_cfa, next_cfa;
16995   const char *start_label, *last_label, *section;
16996   dw_cfa_location remember;
16997
16998   fde = current_fde ();
16999   gcc_assert (fde != NULL);
17000
17001   section = secname_for_decl (current_function_decl);
17002   list_tail = &list;
17003   list = NULL;
17004
17005   memset (&next_cfa, 0, sizeof (next_cfa));
17006   next_cfa.reg = INVALID_REGNUM;
17007   remember = next_cfa;
17008
17009   start_label = fde->dw_fde_begin;
17010
17011   /* ??? Bald assumption that the CIE opcode list does not contain
17012      advance opcodes.  */
17013   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17014     lookup_cfa_1 (cfi, &next_cfa, &remember);
17015
17016   last_cfa = next_cfa;
17017   last_label = start_label;
17018
17019   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17020     switch (cfi->dw_cfi_opc)
17021       {
17022       case DW_CFA_set_loc:
17023       case DW_CFA_advance_loc1:
17024       case DW_CFA_advance_loc2:
17025       case DW_CFA_advance_loc4:
17026         if (!cfa_equal_p (&last_cfa, &next_cfa))
17027           {
17028             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17029                                        start_label, last_label, section);
17030
17031             list_tail = &(*list_tail)->dw_loc_next;
17032             last_cfa = next_cfa;
17033             start_label = last_label;
17034           }
17035         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17036         break;
17037
17038       case DW_CFA_advance_loc:
17039         /* The encoding is complex enough that we should never emit this.  */
17040         gcc_unreachable ();
17041
17042       default:
17043         lookup_cfa_1 (cfi, &next_cfa, &remember);
17044         break;
17045       }
17046
17047   if (!cfa_equal_p (&last_cfa, &next_cfa))
17048     {
17049       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17050                                  start_label, last_label, section);
17051       list_tail = &(*list_tail)->dw_loc_next;
17052       start_label = last_label;
17053     }
17054
17055   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17056                              start_label, fde->dw_fde_end, section);
17057
17058   if (list && list->dw_loc_next)
17059     gen_llsym (list);
17060
17061   return list;
17062 }
17063
17064 /* Compute a displacement from the "steady-state frame pointer" to the
17065    frame base (often the same as the CFA), and store it in
17066    frame_pointer_fb_offset.  OFFSET is added to the displacement
17067    before the latter is negated.  */
17068
17069 static void
17070 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17071 {
17072   rtx reg, elim;
17073
17074 #ifdef FRAME_POINTER_CFA_OFFSET
17075   reg = frame_pointer_rtx;
17076   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17077 #else
17078   reg = arg_pointer_rtx;
17079   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17080 #endif
17081
17082   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17083   if (GET_CODE (elim) == PLUS)
17084     {
17085       offset += INTVAL (XEXP (elim, 1));
17086       elim = XEXP (elim, 0);
17087     }
17088
17089   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17090                && (elim == hard_frame_pointer_rtx
17091                    || elim == stack_pointer_rtx))
17092               || elim == (frame_pointer_needed
17093                           ? hard_frame_pointer_rtx
17094                           : stack_pointer_rtx));
17095
17096   frame_pointer_fb_offset = -offset;
17097 }
17098
17099 /* Generate a DW_AT_name attribute given some string value to be included as
17100    the value of the attribute.  */
17101
17102 static void
17103 add_name_attribute (dw_die_ref die, const char *name_string)
17104 {
17105   if (name_string != NULL && *name_string != 0)
17106     {
17107       if (demangle_name_func)
17108         name_string = (*demangle_name_func) (name_string);
17109
17110       add_AT_string (die, DW_AT_name, name_string);
17111     }
17112 }
17113
17114 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17115
17116 static void
17117 add_comp_dir_attribute (dw_die_ref die)
17118 {
17119   const char *wd = get_src_pwd ();
17120   char *wd1;
17121
17122   if (wd == NULL)
17123     return;
17124
17125   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17126     {
17127       int wdlen;
17128
17129       wdlen = strlen (wd);
17130       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17131       strcpy (wd1, wd);
17132       wd1 [wdlen] = DIR_SEPARATOR;
17133       wd1 [wdlen + 1] = 0;
17134       wd = wd1;
17135     }
17136
17137     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17138 }
17139
17140 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17141    default.  */
17142
17143 static int
17144 lower_bound_default (void)
17145 {
17146   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17147     {
17148     case DW_LANG_C:
17149     case DW_LANG_C89:
17150     case DW_LANG_C99:
17151     case DW_LANG_C_plus_plus:
17152     case DW_LANG_ObjC:
17153     case DW_LANG_ObjC_plus_plus:
17154     case DW_LANG_Java:
17155       return 0;
17156     case DW_LANG_Fortran77:
17157     case DW_LANG_Fortran90:
17158     case DW_LANG_Fortran95:
17159       return 1;
17160     case DW_LANG_UPC:
17161     case DW_LANG_D:
17162     case DW_LANG_Python:
17163       return dwarf_version >= 4 ? 0 : -1;
17164     case DW_LANG_Ada95:
17165     case DW_LANG_Ada83:
17166     case DW_LANG_Cobol74:
17167     case DW_LANG_Cobol85:
17168     case DW_LANG_Pascal83:
17169     case DW_LANG_Modula2:
17170     case DW_LANG_PLI:
17171       return dwarf_version >= 4 ? 1 : -1;
17172     default:
17173       return -1;
17174     }
17175 }
17176
17177 /* Given a tree node describing an array bound (either lower or upper) output
17178    a representation for that bound.  */
17179
17180 static void
17181 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17182 {
17183   switch (TREE_CODE (bound))
17184     {
17185     case ERROR_MARK:
17186       return;
17187
17188     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17189     case INTEGER_CST:
17190       {
17191         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17192         int dflt;
17193
17194         /* Use the default if possible.  */
17195         if (bound_attr == DW_AT_lower_bound
17196             && host_integerp (bound, 0)
17197             && (dflt = lower_bound_default ()) != -1
17198             && tree_low_cst (bound, 0) == dflt)
17199           ;
17200
17201         /* Otherwise represent the bound as an unsigned value with the
17202            precision of its type.  The precision and signedness of the
17203            type will be necessary to re-interpret it unambiguously.  */
17204         else if (prec < HOST_BITS_PER_WIDE_INT)
17205           {
17206             unsigned HOST_WIDE_INT mask
17207               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17208             add_AT_unsigned (subrange_die, bound_attr,
17209                              TREE_INT_CST_LOW (bound) & mask);
17210           }
17211         else if (prec == HOST_BITS_PER_WIDE_INT
17212                  || TREE_INT_CST_HIGH (bound) == 0)
17213           add_AT_unsigned (subrange_die, bound_attr,
17214                            TREE_INT_CST_LOW (bound));
17215         else
17216           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17217                          TREE_INT_CST_LOW (bound));
17218       }
17219       break;
17220
17221     CASE_CONVERT:
17222     case VIEW_CONVERT_EXPR:
17223       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17224       break;
17225
17226     case SAVE_EXPR:
17227       break;
17228
17229     case VAR_DECL:
17230     case PARM_DECL:
17231     case RESULT_DECL:
17232       {
17233         dw_die_ref decl_die = lookup_decl_die (bound);
17234
17235         /* ??? Can this happen, or should the variable have been bound
17236            first?  Probably it can, since I imagine that we try to create
17237            the types of parameters in the order in which they exist in
17238            the list, and won't have created a forward reference to a
17239            later parameter.  */
17240         if (decl_die != NULL)
17241           {
17242             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17243             break;
17244           }
17245       }
17246       /* FALLTHRU */
17247
17248     default:
17249       {
17250         /* Otherwise try to create a stack operation procedure to
17251            evaluate the value of the array bound.  */
17252
17253         dw_die_ref ctx, decl_die;
17254         dw_loc_list_ref list;
17255
17256         list = loc_list_from_tree (bound, 2);
17257         if (list == NULL || single_element_loc_list_p (list))
17258           {
17259             /* If DW_AT_*bound is not a reference nor constant, it is
17260                a DWARF expression rather than location description.
17261                For that loc_list_from_tree (bound, 0) is needed.
17262                If that fails to give a single element list,
17263                fall back to outputting this as a reference anyway.  */
17264             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17265             if (list2 && single_element_loc_list_p (list2))
17266               {
17267                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17268                 break;
17269               }
17270           }
17271         if (list == NULL)
17272           break;
17273
17274         if (current_function_decl == 0)
17275           ctx = comp_unit_die ();
17276         else
17277           ctx = lookup_decl_die (current_function_decl);
17278
17279         decl_die = new_die (DW_TAG_variable, ctx, bound);
17280         add_AT_flag (decl_die, DW_AT_artificial, 1);
17281         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17282         add_AT_location_description (decl_die, DW_AT_location, list);
17283         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17284         break;
17285       }
17286     }
17287 }
17288
17289 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17290    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17291    Note that the block of subscript information for an array type also
17292    includes information about the element type of the given array type.  */
17293
17294 static void
17295 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17296 {
17297   unsigned dimension_number;
17298   tree lower, upper;
17299   dw_die_ref subrange_die;
17300
17301   for (dimension_number = 0;
17302        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17303        type = TREE_TYPE (type), dimension_number++)
17304     {
17305       tree domain = TYPE_DOMAIN (type);
17306
17307       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17308         break;
17309
17310       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17311          and (in GNU C only) variable bounds.  Handle all three forms
17312          here.  */
17313       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17314       if (domain)
17315         {
17316           /* We have an array type with specified bounds.  */
17317           lower = TYPE_MIN_VALUE (domain);
17318           upper = TYPE_MAX_VALUE (domain);
17319
17320           /* Define the index type.  */
17321           if (TREE_TYPE (domain))
17322             {
17323               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17324                  TREE_TYPE field.  We can't emit debug info for this
17325                  because it is an unnamed integral type.  */
17326               if (TREE_CODE (domain) == INTEGER_TYPE
17327                   && TYPE_NAME (domain) == NULL_TREE
17328                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17329                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17330                 ;
17331               else
17332                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17333                                     type_die);
17334             }
17335
17336           /* ??? If upper is NULL, the array has unspecified length,
17337              but it does have a lower bound.  This happens with Fortran
17338                dimension arr(N:*)
17339              Since the debugger is definitely going to need to know N
17340              to produce useful results, go ahead and output the lower
17341              bound solo, and hope the debugger can cope.  */
17342
17343           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17344           if (upper)
17345             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17346         }
17347
17348       /* Otherwise we have an array type with an unspecified length.  The
17349          DWARF-2 spec does not say how to handle this; let's just leave out the
17350          bounds.  */
17351     }
17352 }
17353
17354 static void
17355 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17356 {
17357   unsigned size;
17358
17359   switch (TREE_CODE (tree_node))
17360     {
17361     case ERROR_MARK:
17362       size = 0;
17363       break;
17364     case ENUMERAL_TYPE:
17365     case RECORD_TYPE:
17366     case UNION_TYPE:
17367     case QUAL_UNION_TYPE:
17368       size = int_size_in_bytes (tree_node);
17369       break;
17370     case FIELD_DECL:
17371       /* For a data member of a struct or union, the DW_AT_byte_size is
17372          generally given as the number of bytes normally allocated for an
17373          object of the *declared* type of the member itself.  This is true
17374          even for bit-fields.  */
17375       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17376       break;
17377     default:
17378       gcc_unreachable ();
17379     }
17380
17381   /* Note that `size' might be -1 when we get to this point.  If it is, that
17382      indicates that the byte size of the entity in question is variable.  We
17383      have no good way of expressing this fact in Dwarf at the present time,
17384      so just let the -1 pass on through.  */
17385   add_AT_unsigned (die, DW_AT_byte_size, size);
17386 }
17387
17388 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17389    which specifies the distance in bits from the highest order bit of the
17390    "containing object" for the bit-field to the highest order bit of the
17391    bit-field itself.
17392
17393    For any given bit-field, the "containing object" is a hypothetical object
17394    (of some integral or enum type) within which the given bit-field lives.  The
17395    type of this hypothetical "containing object" is always the same as the
17396    declared type of the individual bit-field itself.  The determination of the
17397    exact location of the "containing object" for a bit-field is rather
17398    complicated.  It's handled by the `field_byte_offset' function (above).
17399
17400    Note that it is the size (in bytes) of the hypothetical "containing object"
17401    which will be given in the DW_AT_byte_size attribute for this bit-field.
17402    (See `byte_size_attribute' above).  */
17403
17404 static inline void
17405 add_bit_offset_attribute (dw_die_ref die, tree decl)
17406 {
17407   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17408   tree type = DECL_BIT_FIELD_TYPE (decl);
17409   HOST_WIDE_INT bitpos_int;
17410   HOST_WIDE_INT highest_order_object_bit_offset;
17411   HOST_WIDE_INT highest_order_field_bit_offset;
17412   HOST_WIDE_INT unsigned bit_offset;
17413
17414   /* Must be a field and a bit field.  */
17415   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17416
17417   /* We can't yet handle bit-fields whose offsets are variable, so if we
17418      encounter such things, just return without generating any attribute
17419      whatsoever.  Likewise for variable or too large size.  */
17420   if (! host_integerp (bit_position (decl), 0)
17421       || ! host_integerp (DECL_SIZE (decl), 1))
17422     return;
17423
17424   bitpos_int = int_bit_position (decl);
17425
17426   /* Note that the bit offset is always the distance (in bits) from the
17427      highest-order bit of the "containing object" to the highest-order bit of
17428      the bit-field itself.  Since the "high-order end" of any object or field
17429      is different on big-endian and little-endian machines, the computation
17430      below must take account of these differences.  */
17431   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17432   highest_order_field_bit_offset = bitpos_int;
17433
17434   if (! BYTES_BIG_ENDIAN)
17435     {
17436       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17437       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17438     }
17439
17440   bit_offset
17441     = (! BYTES_BIG_ENDIAN
17442        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17443        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17444
17445   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17446 }
17447
17448 /* For a FIELD_DECL node which represents a bit field, output an attribute
17449    which specifies the length in bits of the given field.  */
17450
17451 static inline void
17452 add_bit_size_attribute (dw_die_ref die, tree decl)
17453 {
17454   /* Must be a field and a bit field.  */
17455   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17456               && DECL_BIT_FIELD_TYPE (decl));
17457
17458   if (host_integerp (DECL_SIZE (decl), 1))
17459     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17460 }
17461
17462 /* If the compiled language is ANSI C, then add a 'prototyped'
17463    attribute, if arg types are given for the parameters of a function.  */
17464
17465 static inline void
17466 add_prototyped_attribute (dw_die_ref die, tree func_type)
17467 {
17468   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17469       && TYPE_ARG_TYPES (func_type) != NULL)
17470     add_AT_flag (die, DW_AT_prototyped, 1);
17471 }
17472
17473 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17474    by looking in either the type declaration or object declaration
17475    equate table.  */
17476
17477 static inline dw_die_ref
17478 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17479 {
17480   dw_die_ref origin_die = NULL;
17481
17482   if (TREE_CODE (origin) != FUNCTION_DECL)
17483     {
17484       /* We may have gotten separated from the block for the inlined
17485          function, if we're in an exception handler or some such; make
17486          sure that the abstract function has been written out.
17487
17488          Doing this for nested functions is wrong, however; functions are
17489          distinct units, and our context might not even be inline.  */
17490       tree fn = origin;
17491
17492       if (TYPE_P (fn))
17493         fn = TYPE_STUB_DECL (fn);
17494
17495       fn = decl_function_context (fn);
17496       if (fn)
17497         dwarf2out_abstract_function (fn);
17498     }
17499
17500   if (DECL_P (origin))
17501     origin_die = lookup_decl_die (origin);
17502   else if (TYPE_P (origin))
17503     origin_die = lookup_type_die (origin);
17504
17505   /* XXX: Functions that are never lowered don't always have correct block
17506      trees (in the case of java, they simply have no block tree, in some other
17507      languages).  For these functions, there is nothing we can really do to
17508      output correct debug info for inlined functions in all cases.  Rather
17509      than die, we'll just produce deficient debug info now, in that we will
17510      have variables without a proper abstract origin.  In the future, when all
17511      functions are lowered, we should re-add a gcc_assert (origin_die)
17512      here.  */
17513
17514   if (origin_die)
17515     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17516   return origin_die;
17517 }
17518
17519 /* We do not currently support the pure_virtual attribute.  */
17520
17521 static inline void
17522 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17523 {
17524   if (DECL_VINDEX (func_decl))
17525     {
17526       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17527
17528       if (host_integerp (DECL_VINDEX (func_decl), 0))
17529         add_AT_loc (die, DW_AT_vtable_elem_location,
17530                     new_loc_descr (DW_OP_constu,
17531                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17532                                    0));
17533
17534       /* GNU extension: Record what type this method came from originally.  */
17535       if (debug_info_level > DINFO_LEVEL_TERSE
17536           && DECL_CONTEXT (func_decl))
17537         add_AT_die_ref (die, DW_AT_containing_type,
17538                         lookup_type_die (DECL_CONTEXT (func_decl)));
17539     }
17540 }
17541 \f
17542 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17543    given decl.  This used to be a vendor extension until after DWARF 4
17544    standardized it.  */
17545
17546 static void
17547 add_linkage_attr (dw_die_ref die, tree decl)
17548 {
17549   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17550
17551   /* Mimic what assemble_name_raw does with a leading '*'.  */
17552   if (name[0] == '*')
17553     name = &name[1];
17554
17555   if (dwarf_version >= 4)
17556     add_AT_string (die, DW_AT_linkage_name, name);
17557   else
17558     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17559 }
17560
17561 /* Add source coordinate attributes for the given decl.  */
17562
17563 static void
17564 add_src_coords_attributes (dw_die_ref die, tree decl)
17565 {
17566   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17567
17568   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17569   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17570 }
17571
17572 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17573
17574 static void
17575 add_linkage_name (dw_die_ref die, tree decl)
17576 {
17577   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17578        && TREE_PUBLIC (decl)
17579        && !DECL_ABSTRACT (decl)
17580        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17581        && die->die_tag != DW_TAG_member)
17582     {
17583       /* Defer until we have an assembler name set.  */
17584       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17585         {
17586           limbo_die_node *asm_name;
17587
17588           asm_name = ggc_alloc_cleared_limbo_die_node ();
17589           asm_name->die = die;
17590           asm_name->created_for = decl;
17591           asm_name->next = deferred_asm_name;
17592           deferred_asm_name = asm_name;
17593         }
17594       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17595         add_linkage_attr (die, decl);
17596     }
17597 }
17598
17599 /* Add a DW_AT_name attribute and source coordinate attribute for the
17600    given decl, but only if it actually has a name.  */
17601
17602 static void
17603 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17604 {
17605   tree decl_name;
17606
17607   decl_name = DECL_NAME (decl);
17608   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17609     {
17610       const char *name = dwarf2_name (decl, 0);
17611       if (name)
17612         add_name_attribute (die, name);
17613       if (! DECL_ARTIFICIAL (decl))
17614         add_src_coords_attributes (die, decl);
17615
17616       add_linkage_name (die, decl);
17617     }
17618
17619 #ifdef VMS_DEBUGGING_INFO
17620   /* Get the function's name, as described by its RTL.  This may be different
17621      from the DECL_NAME name used in the source file.  */
17622   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17623     {
17624       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17625                    XEXP (DECL_RTL (decl), 0));
17626       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17627     }
17628 #endif /* VMS_DEBUGGING_INFO */
17629 }
17630
17631 #ifdef VMS_DEBUGGING_INFO
17632 /* Output the debug main pointer die for VMS */
17633
17634 void
17635 dwarf2out_vms_debug_main_pointer (void)
17636 {
17637   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17638   dw_die_ref die;
17639
17640   /* Allocate the VMS debug main subprogram die.  */
17641   die = ggc_alloc_cleared_die_node ();
17642   die->die_tag = DW_TAG_subprogram;
17643   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17644   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17645                                current_function_funcdef_no);
17646   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17647
17648   /* Make it the first child of comp_unit_die ().  */
17649   die->die_parent = comp_unit_die ();
17650   if (comp_unit_die ()->die_child)
17651     {
17652       die->die_sib = comp_unit_die ()->die_child->die_sib;
17653       comp_unit_die ()->die_child->die_sib = die;
17654     }
17655   else
17656     {
17657       die->die_sib = die;
17658       comp_unit_die ()->die_child = die;
17659     }
17660 }
17661 #endif /* VMS_DEBUGGING_INFO */
17662
17663 /* Push a new declaration scope.  */
17664
17665 static void
17666 push_decl_scope (tree scope)
17667 {
17668   VEC_safe_push (tree, gc, decl_scope_table, scope);
17669 }
17670
17671 /* Pop a declaration scope.  */
17672
17673 static inline void
17674 pop_decl_scope (void)
17675 {
17676   VEC_pop (tree, decl_scope_table);
17677 }
17678
17679 /* Return the DIE for the scope that immediately contains this type.
17680    Non-named types get global scope.  Named types nested in other
17681    types get their containing scope if it's open, or global scope
17682    otherwise.  All other types (i.e. function-local named types) get
17683    the current active scope.  */
17684
17685 static dw_die_ref
17686 scope_die_for (tree t, dw_die_ref context_die)
17687 {
17688   dw_die_ref scope_die = NULL;
17689   tree containing_scope;
17690   int i;
17691
17692   /* Non-types always go in the current scope.  */
17693   gcc_assert (TYPE_P (t));
17694
17695   containing_scope = TYPE_CONTEXT (t);
17696
17697   /* Use the containing namespace if it was passed in (for a declaration).  */
17698   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17699     {
17700       if (context_die == lookup_decl_die (containing_scope))
17701         /* OK */;
17702       else
17703         containing_scope = NULL_TREE;
17704     }
17705
17706   /* Ignore function type "scopes" from the C frontend.  They mean that
17707      a tagged type is local to a parmlist of a function declarator, but
17708      that isn't useful to DWARF.  */
17709   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17710     containing_scope = NULL_TREE;
17711
17712   if (containing_scope == NULL_TREE)
17713     scope_die = comp_unit_die ();
17714   else if (TYPE_P (containing_scope))
17715     {
17716       /* For types, we can just look up the appropriate DIE.  But
17717          first we check to see if we're in the middle of emitting it
17718          so we know where the new DIE should go.  */
17719       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17720         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17721           break;
17722
17723       if (i < 0)
17724         {
17725           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17726                       || TREE_ASM_WRITTEN (containing_scope));
17727           /*We are not in the middle of emitting the type
17728             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17729           scope_die = lookup_type_die (containing_scope);
17730
17731           /* If none of the current dies are suitable, we get file scope.  */
17732           if (scope_die == NULL)
17733             scope_die = comp_unit_die ();
17734         }
17735       else
17736         scope_die = lookup_type_die (containing_scope);
17737     }
17738   else
17739     scope_die = context_die;
17740
17741   return scope_die;
17742 }
17743
17744 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17745
17746 static inline int
17747 local_scope_p (dw_die_ref context_die)
17748 {
17749   for (; context_die; context_die = context_die->die_parent)
17750     if (context_die->die_tag == DW_TAG_inlined_subroutine
17751         || context_die->die_tag == DW_TAG_subprogram)
17752       return 1;
17753
17754   return 0;
17755 }
17756
17757 /* Returns nonzero if CONTEXT_DIE is a class.  */
17758
17759 static inline int
17760 class_scope_p (dw_die_ref context_die)
17761 {
17762   return (context_die
17763           && (context_die->die_tag == DW_TAG_structure_type
17764               || context_die->die_tag == DW_TAG_class_type
17765               || context_die->die_tag == DW_TAG_interface_type
17766               || context_die->die_tag == DW_TAG_union_type));
17767 }
17768
17769 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17770    whether or not to treat a DIE in this context as a declaration.  */
17771
17772 static inline int
17773 class_or_namespace_scope_p (dw_die_ref context_die)
17774 {
17775   return (class_scope_p (context_die)
17776           || (context_die && context_die->die_tag == DW_TAG_namespace));
17777 }
17778
17779 /* Many forms of DIEs require a "type description" attribute.  This
17780    routine locates the proper "type descriptor" die for the type given
17781    by 'type', and adds a DW_AT_type attribute below the given die.  */
17782
17783 static void
17784 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17785                     int decl_volatile, dw_die_ref context_die)
17786 {
17787   enum tree_code code  = TREE_CODE (type);
17788   dw_die_ref type_die  = NULL;
17789
17790   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17791      or fixed-point type, use the inner type.  This is because we have no
17792      support for unnamed types in base_type_die.  This can happen if this is
17793      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17794   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17795       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17796     type = TREE_TYPE (type), code = TREE_CODE (type);
17797
17798   if (code == ERROR_MARK
17799       /* Handle a special case.  For functions whose return type is void, we
17800          generate *no* type attribute.  (Note that no object may have type
17801          `void', so this only applies to function return types).  */
17802       || code == VOID_TYPE)
17803     return;
17804
17805   type_die = modified_type_die (type,
17806                                 decl_const || TYPE_READONLY (type),
17807                                 decl_volatile || TYPE_VOLATILE (type),
17808                                 context_die);
17809
17810   if (type_die != NULL)
17811     add_AT_die_ref (object_die, DW_AT_type, type_die);
17812 }
17813
17814 /* Given an object die, add the calling convention attribute for the
17815    function call type.  */
17816 static void
17817 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17818 {
17819   enum dwarf_calling_convention value = DW_CC_normal;
17820
17821   value = ((enum dwarf_calling_convention)
17822            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17823
17824   /* DWARF doesn't provide a way to identify a program's source-level
17825      entry point.  DW_AT_calling_convention attributes are only meant
17826      to describe functions' calling conventions.  However, lacking a
17827      better way to signal the Fortran main program, we use this for the
17828      time being, following existing custom.  */
17829   if (is_fortran ()
17830       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17831     value = DW_CC_program;
17832
17833   /* Only add the attribute if the backend requests it, and
17834      is not DW_CC_normal.  */
17835   if (value && (value != DW_CC_normal))
17836     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17837 }
17838
17839 /* Given a tree pointer to a struct, class, union, or enum type node, return
17840    a pointer to the (string) tag name for the given type, or zero if the type
17841    was declared without a tag.  */
17842
17843 static const char *
17844 type_tag (const_tree type)
17845 {
17846   const char *name = 0;
17847
17848   if (TYPE_NAME (type) != 0)
17849     {
17850       tree t = 0;
17851
17852       /* Find the IDENTIFIER_NODE for the type name.  */
17853       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17854           && !TYPE_NAMELESS (type))
17855         t = TYPE_NAME (type);
17856
17857       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17858          a TYPE_DECL node, regardless of whether or not a `typedef' was
17859          involved.  */
17860       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17861                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17862         {
17863           /* We want to be extra verbose.  Don't call dwarf_name if
17864              DECL_NAME isn't set.  The default hook for decl_printable_name
17865              doesn't like that, and in this context it's correct to return
17866              0, instead of "<anonymous>" or the like.  */
17867           if (DECL_NAME (TYPE_NAME (type))
17868               && !DECL_NAMELESS (TYPE_NAME (type)))
17869             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17870         }
17871
17872       /* Now get the name as a string, or invent one.  */
17873       if (!name && t != 0)
17874         name = IDENTIFIER_POINTER (t);
17875     }
17876
17877   return (name == 0 || *name == '\0') ? 0 : name;
17878 }
17879
17880 /* Return the type associated with a data member, make a special check
17881    for bit field types.  */
17882
17883 static inline tree
17884 member_declared_type (const_tree member)
17885 {
17886   return (DECL_BIT_FIELD_TYPE (member)
17887           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17888 }
17889
17890 /* Get the decl's label, as described by its RTL. This may be different
17891    from the DECL_NAME name used in the source file.  */
17892
17893 #if 0
17894 static const char *
17895 decl_start_label (tree decl)
17896 {
17897   rtx x;
17898   const char *fnname;
17899
17900   x = DECL_RTL (decl);
17901   gcc_assert (MEM_P (x));
17902
17903   x = XEXP (x, 0);
17904   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17905
17906   fnname = XSTR (x, 0);
17907   return fnname;
17908 }
17909 #endif
17910 \f
17911 /* These routines generate the internal representation of the DIE's for
17912    the compilation unit.  Debugging information is collected by walking
17913    the declaration trees passed in from dwarf2out_decl().  */
17914
17915 static void
17916 gen_array_type_die (tree type, dw_die_ref context_die)
17917 {
17918   dw_die_ref scope_die = scope_die_for (type, context_die);
17919   dw_die_ref array_die;
17920
17921   /* GNU compilers represent multidimensional array types as sequences of one
17922      dimensional array types whose element types are themselves array types.
17923      We sometimes squish that down to a single array_type DIE with multiple
17924      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17925      say that we are allowed to do this kind of compression in C, because
17926      there is no difference between an array of arrays and a multidimensional
17927      array.  We don't do this for Ada to remain as close as possible to the
17928      actual representation, which is especially important against the language
17929      flexibilty wrt arrays of variable size.  */
17930
17931   bool collapse_nested_arrays = !is_ada ();
17932   tree element_type;
17933
17934   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17935      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17936   if (TYPE_STRING_FLAG (type)
17937       && TREE_CODE (type) == ARRAY_TYPE
17938       && is_fortran ()
17939       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17940     {
17941       HOST_WIDE_INT size;
17942
17943       array_die = new_die (DW_TAG_string_type, scope_die, type);
17944       add_name_attribute (array_die, type_tag (type));
17945       equate_type_number_to_die (type, array_die);
17946       size = int_size_in_bytes (type);
17947       if (size >= 0)
17948         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17949       else if (TYPE_DOMAIN (type) != NULL_TREE
17950                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17951                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17952         {
17953           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17954           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17955
17956           size = int_size_in_bytes (TREE_TYPE (szdecl));
17957           if (loc && size > 0)
17958             {
17959               add_AT_location_description (array_die, DW_AT_string_length, loc);
17960               if (size != DWARF2_ADDR_SIZE)
17961                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17962             }
17963         }
17964       return;
17965     }
17966
17967   /* ??? The SGI dwarf reader fails for array of array of enum types
17968      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17969      array type comes before the outer array type.  We thus call gen_type_die
17970      before we new_die and must prevent nested array types collapsing for this
17971      target.  */
17972
17973 #ifdef MIPS_DEBUGGING_INFO
17974   gen_type_die (TREE_TYPE (type), context_die);
17975   collapse_nested_arrays = false;
17976 #endif
17977
17978   array_die = new_die (DW_TAG_array_type, scope_die, type);
17979   add_name_attribute (array_die, type_tag (type));
17980   equate_type_number_to_die (type, array_die);
17981
17982   if (TREE_CODE (type) == VECTOR_TYPE)
17983     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17984
17985   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17986   if (is_fortran ()
17987       && TREE_CODE (type) == ARRAY_TYPE
17988       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17989       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17990     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17991
17992 #if 0
17993   /* We default the array ordering.  SDB will probably do
17994      the right things even if DW_AT_ordering is not present.  It's not even
17995      an issue until we start to get into multidimensional arrays anyway.  If
17996      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17997      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17998      and when we find out that we need to put these in, we will only do so
17999      for multidimensional arrays.  */
18000   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18001 #endif
18002
18003 #ifdef MIPS_DEBUGGING_INFO
18004   /* The SGI compilers handle arrays of unknown bound by setting
18005      AT_declaration and not emitting any subrange DIEs.  */
18006   if (TREE_CODE (type) == ARRAY_TYPE
18007       && ! TYPE_DOMAIN (type))
18008     add_AT_flag (array_die, DW_AT_declaration, 1);
18009   else
18010 #endif
18011   if (TREE_CODE (type) == VECTOR_TYPE)
18012     {
18013       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18014       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18015       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18016       add_bound_info (subrange_die, DW_AT_upper_bound,
18017                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18018     }
18019   else
18020     add_subscript_info (array_die, type, collapse_nested_arrays);
18021
18022   /* Add representation of the type of the elements of this array type and
18023      emit the corresponding DIE if we haven't done it already.  */
18024   element_type = TREE_TYPE (type);
18025   if (collapse_nested_arrays)
18026     while (TREE_CODE (element_type) == ARRAY_TYPE)
18027       {
18028         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18029           break;
18030         element_type = TREE_TYPE (element_type);
18031       }
18032
18033 #ifndef MIPS_DEBUGGING_INFO
18034   gen_type_die (element_type, context_die);
18035 #endif
18036
18037   add_type_attribute (array_die, element_type, 0, 0, context_die);
18038
18039   if (get_AT (array_die, DW_AT_name))
18040     add_pubtype (type, array_die);
18041 }
18042
18043 static dw_loc_descr_ref
18044 descr_info_loc (tree val, tree base_decl)
18045 {
18046   HOST_WIDE_INT size;
18047   dw_loc_descr_ref loc, loc2;
18048   enum dwarf_location_atom op;
18049
18050   if (val == base_decl)
18051     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18052
18053   switch (TREE_CODE (val))
18054     {
18055     CASE_CONVERT:
18056       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18057     case VAR_DECL:
18058       return loc_descriptor_from_tree (val, 0);
18059     case INTEGER_CST:
18060       if (host_integerp (val, 0))
18061         return int_loc_descriptor (tree_low_cst (val, 0));
18062       break;
18063     case INDIRECT_REF:
18064       size = int_size_in_bytes (TREE_TYPE (val));
18065       if (size < 0)
18066         break;
18067       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18068       if (!loc)
18069         break;
18070       if (size == DWARF2_ADDR_SIZE)
18071         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18072       else
18073         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18074       return loc;
18075     case POINTER_PLUS_EXPR:
18076     case PLUS_EXPR:
18077       if (host_integerp (TREE_OPERAND (val, 1), 1)
18078           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18079              < 16384)
18080         {
18081           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18082           if (!loc)
18083             break;
18084           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18085         }
18086       else
18087         {
18088           op = DW_OP_plus;
18089         do_binop:
18090           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18091           if (!loc)
18092             break;
18093           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18094           if (!loc2)
18095             break;
18096           add_loc_descr (&loc, loc2);
18097           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18098         }
18099       return loc;
18100     case MINUS_EXPR:
18101       op = DW_OP_minus;
18102       goto do_binop;
18103     case MULT_EXPR:
18104       op = DW_OP_mul;
18105       goto do_binop;
18106     case EQ_EXPR:
18107       op = DW_OP_eq;
18108       goto do_binop;
18109     case NE_EXPR:
18110       op = DW_OP_ne;
18111       goto do_binop;
18112     default:
18113       break;
18114     }
18115   return NULL;
18116 }
18117
18118 static void
18119 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18120                       tree val, tree base_decl)
18121 {
18122   dw_loc_descr_ref loc;
18123
18124   if (host_integerp (val, 0))
18125     {
18126       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18127       return;
18128     }
18129
18130   loc = descr_info_loc (val, base_decl);
18131   if (!loc)
18132     return;
18133
18134   add_AT_loc (die, attr, loc);
18135 }
18136
18137 /* This routine generates DIE for array with hidden descriptor, details
18138    are filled into *info by a langhook.  */
18139
18140 static void
18141 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18142                           dw_die_ref context_die)
18143 {
18144   dw_die_ref scope_die = scope_die_for (type, context_die);
18145   dw_die_ref array_die;
18146   int dim;
18147
18148   array_die = new_die (DW_TAG_array_type, scope_die, type);
18149   add_name_attribute (array_die, type_tag (type));
18150   equate_type_number_to_die (type, array_die);
18151
18152   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18153   if (is_fortran ()
18154       && info->ndimensions >= 2)
18155     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18156
18157   if (info->data_location)
18158     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18159                           info->base_decl);
18160   if (info->associated)
18161     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18162                           info->base_decl);
18163   if (info->allocated)
18164     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18165                           info->base_decl);
18166
18167   for (dim = 0; dim < info->ndimensions; dim++)
18168     {
18169       dw_die_ref subrange_die
18170         = new_die (DW_TAG_subrange_type, array_die, NULL);
18171
18172       if (info->dimen[dim].lower_bound)
18173         {
18174           /* If it is the default value, omit it.  */
18175           int dflt;
18176
18177           if (host_integerp (info->dimen[dim].lower_bound, 0)
18178               && (dflt = lower_bound_default ()) != -1
18179               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18180             ;
18181           else
18182             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18183                                   info->dimen[dim].lower_bound,
18184                                   info->base_decl);
18185         }
18186       if (info->dimen[dim].upper_bound)
18187         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18188                               info->dimen[dim].upper_bound,
18189                               info->base_decl);
18190       if (info->dimen[dim].stride)
18191         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18192                               info->dimen[dim].stride,
18193                               info->base_decl);
18194     }
18195
18196   gen_type_die (info->element_type, context_die);
18197   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18198
18199   if (get_AT (array_die, DW_AT_name))
18200     add_pubtype (type, array_die);
18201 }
18202
18203 #if 0
18204 static void
18205 gen_entry_point_die (tree decl, dw_die_ref context_die)
18206 {
18207   tree origin = decl_ultimate_origin (decl);
18208   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18209
18210   if (origin != NULL)
18211     add_abstract_origin_attribute (decl_die, origin);
18212   else
18213     {
18214       add_name_and_src_coords_attributes (decl_die, decl);
18215       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18216                           0, 0, context_die);
18217     }
18218
18219   if (DECL_ABSTRACT (decl))
18220     equate_decl_number_to_die (decl, decl_die);
18221   else
18222     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18223 }
18224 #endif
18225
18226 /* Walk through the list of incomplete types again, trying once more to
18227    emit full debugging info for them.  */
18228
18229 static void
18230 retry_incomplete_types (void)
18231 {
18232   int i;
18233
18234   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18235     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18236                                   DINFO_USAGE_DIR_USE))
18237       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18238 }
18239
18240 /* Determine what tag to use for a record type.  */
18241
18242 static enum dwarf_tag
18243 record_type_tag (tree type)
18244 {
18245   if (! lang_hooks.types.classify_record)
18246     return DW_TAG_structure_type;
18247
18248   switch (lang_hooks.types.classify_record (type))
18249     {
18250     case RECORD_IS_STRUCT:
18251       return DW_TAG_structure_type;
18252
18253     case RECORD_IS_CLASS:
18254       return DW_TAG_class_type;
18255
18256     case RECORD_IS_INTERFACE:
18257       if (dwarf_version >= 3 || !dwarf_strict)
18258         return DW_TAG_interface_type;
18259       return DW_TAG_structure_type;
18260
18261     default:
18262       gcc_unreachable ();
18263     }
18264 }
18265
18266 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18267    include all of the information about the enumeration values also. Each
18268    enumerated type name/value is listed as a child of the enumerated type
18269    DIE.  */
18270
18271 static dw_die_ref
18272 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18273 {
18274   dw_die_ref type_die = lookup_type_die (type);
18275
18276   if (type_die == NULL)
18277     {
18278       type_die = new_die (DW_TAG_enumeration_type,
18279                           scope_die_for (type, context_die), type);
18280       equate_type_number_to_die (type, type_die);
18281       add_name_attribute (type_die, type_tag (type));
18282       if ((dwarf_version >= 4 || !dwarf_strict)
18283           && ENUM_IS_SCOPED (type))
18284         add_AT_flag (type_die, DW_AT_enum_class, 1);
18285     }
18286   else if (! TYPE_SIZE (type))
18287     return type_die;
18288   else
18289     remove_AT (type_die, DW_AT_declaration);
18290
18291   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18292      given enum type is incomplete, do not generate the DW_AT_byte_size
18293      attribute or the DW_AT_element_list attribute.  */
18294   if (TYPE_SIZE (type))
18295     {
18296       tree link;
18297
18298       TREE_ASM_WRITTEN (type) = 1;
18299       add_byte_size_attribute (type_die, type);
18300       if (TYPE_STUB_DECL (type) != NULL_TREE)
18301         {
18302           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18303           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18304         }
18305
18306       /* If the first reference to this type was as the return type of an
18307          inline function, then it may not have a parent.  Fix this now.  */
18308       if (type_die->die_parent == NULL)
18309         add_child_die (scope_die_for (type, context_die), type_die);
18310
18311       for (link = TYPE_VALUES (type);
18312            link != NULL; link = TREE_CHAIN (link))
18313         {
18314           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18315           tree value = TREE_VALUE (link);
18316
18317           add_name_attribute (enum_die,
18318                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18319
18320           if (TREE_CODE (value) == CONST_DECL)
18321             value = DECL_INITIAL (value);
18322
18323           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18324             /* DWARF2 does not provide a way of indicating whether or
18325                not enumeration constants are signed or unsigned.  GDB
18326                always assumes the values are signed, so we output all
18327                values as if they were signed.  That means that
18328                enumeration constants with very large unsigned values
18329                will appear to have negative values in the debugger.  */
18330             add_AT_int (enum_die, DW_AT_const_value,
18331                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18332         }
18333     }
18334   else
18335     add_AT_flag (type_die, DW_AT_declaration, 1);
18336
18337   if (get_AT (type_die, DW_AT_name))
18338     add_pubtype (type, type_die);
18339
18340   return type_die;
18341 }
18342
18343 /* Generate a DIE to represent either a real live formal parameter decl or to
18344    represent just the type of some formal parameter position in some function
18345    type.
18346
18347    Note that this routine is a bit unusual because its argument may be a
18348    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18349    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18350    node.  If it's the former then this function is being called to output a
18351    DIE to represent a formal parameter object (or some inlining thereof).  If
18352    it's the latter, then this function is only being called to output a
18353    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18354    argument type of some subprogram type.
18355    If EMIT_NAME_P is true, name and source coordinate attributes
18356    are emitted.  */
18357
18358 static dw_die_ref
18359 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18360                           dw_die_ref context_die)
18361 {
18362   tree node_or_origin = node ? node : origin;
18363   tree ultimate_origin;
18364   dw_die_ref parm_die
18365     = new_die (DW_TAG_formal_parameter, context_die, node);
18366
18367   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18368     {
18369     case tcc_declaration:
18370       ultimate_origin = decl_ultimate_origin (node_or_origin);
18371       if (node || ultimate_origin)
18372         origin = ultimate_origin;
18373       if (origin != NULL)
18374         add_abstract_origin_attribute (parm_die, origin);
18375       else if (emit_name_p)
18376         add_name_and_src_coords_attributes (parm_die, node);
18377       if (origin == NULL
18378           || (! DECL_ABSTRACT (node_or_origin)
18379               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18380                                            decl_function_context
18381                                                             (node_or_origin))))
18382         {
18383           tree type = TREE_TYPE (node_or_origin);
18384           if (decl_by_reference_p (node_or_origin))
18385             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18386                                 context_die);
18387           else
18388             add_type_attribute (parm_die, type,
18389                                 TREE_READONLY (node_or_origin),
18390                                 TREE_THIS_VOLATILE (node_or_origin),
18391                                 context_die);
18392         }
18393       if (origin == NULL && DECL_ARTIFICIAL (node))
18394         add_AT_flag (parm_die, DW_AT_artificial, 1);
18395
18396       if (node && node != origin)
18397         equate_decl_number_to_die (node, parm_die);
18398       if (! DECL_ABSTRACT (node_or_origin))
18399         add_location_or_const_value_attribute (parm_die, node_or_origin,
18400                                                DW_AT_location);
18401
18402       break;
18403
18404     case tcc_type:
18405       /* We were called with some kind of a ..._TYPE node.  */
18406       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18407       break;
18408
18409     default:
18410       gcc_unreachable ();
18411     }
18412
18413   return parm_die;
18414 }
18415
18416 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18417    children DW_TAG_formal_parameter DIEs representing the arguments of the
18418    parameter pack.
18419
18420    PARM_PACK must be a function parameter pack.
18421    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18422    must point to the subsequent arguments of the function PACK_ARG belongs to.
18423    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18424    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18425    following the last one for which a DIE was generated.  */
18426
18427 static dw_die_ref
18428 gen_formal_parameter_pack_die  (tree parm_pack,
18429                                 tree pack_arg,
18430                                 dw_die_ref subr_die,
18431                                 tree *next_arg)
18432 {
18433   tree arg;
18434   dw_die_ref parm_pack_die;
18435
18436   gcc_assert (parm_pack
18437               && lang_hooks.function_parameter_pack_p (parm_pack)
18438               && subr_die);
18439
18440   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18441   add_src_coords_attributes (parm_pack_die, parm_pack);
18442
18443   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18444     {
18445       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18446                                                                  parm_pack))
18447         break;
18448       gen_formal_parameter_die (arg, NULL,
18449                                 false /* Don't emit name attribute.  */,
18450                                 parm_pack_die);
18451     }
18452   if (next_arg)
18453     *next_arg = arg;
18454   return parm_pack_die;
18455 }
18456
18457 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18458    at the end of an (ANSI prototyped) formal parameters list.  */
18459
18460 static void
18461 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18462 {
18463   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18464 }
18465
18466 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18467    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18468    parameters as specified in some function type specification (except for
18469    those which appear as part of a function *definition*).  */
18470
18471 static void
18472 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18473 {
18474   tree link;
18475   tree formal_type = NULL;
18476   tree first_parm_type;
18477   tree arg;
18478
18479   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18480     {
18481       arg = DECL_ARGUMENTS (function_or_method_type);
18482       function_or_method_type = TREE_TYPE (function_or_method_type);
18483     }
18484   else
18485     arg = NULL_TREE;
18486
18487   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18488
18489   /* Make our first pass over the list of formal parameter types and output a
18490      DW_TAG_formal_parameter DIE for each one.  */
18491   for (link = first_parm_type; link; )
18492     {
18493       dw_die_ref parm_die;
18494
18495       formal_type = TREE_VALUE (link);
18496       if (formal_type == void_type_node)
18497         break;
18498
18499       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18500       parm_die = gen_formal_parameter_die (formal_type, NULL,
18501                                            true /* Emit name attribute.  */,
18502                                            context_die);
18503       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18504           && link == first_parm_type)
18505         {
18506           add_AT_flag (parm_die, DW_AT_artificial, 1);
18507           if (dwarf_version >= 3 || !dwarf_strict)
18508             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18509         }
18510       else if (arg && DECL_ARTIFICIAL (arg))
18511         add_AT_flag (parm_die, DW_AT_artificial, 1);
18512
18513       link = TREE_CHAIN (link);
18514       if (arg)
18515         arg = DECL_CHAIN (arg);
18516     }
18517
18518   /* If this function type has an ellipsis, add a
18519      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18520   if (formal_type != void_type_node)
18521     gen_unspecified_parameters_die (function_or_method_type, context_die);
18522
18523   /* Make our second (and final) pass over the list of formal parameter types
18524      and output DIEs to represent those types (as necessary).  */
18525   for (link = TYPE_ARG_TYPES (function_or_method_type);
18526        link && TREE_VALUE (link);
18527        link = TREE_CHAIN (link))
18528     gen_type_die (TREE_VALUE (link), context_die);
18529 }
18530
18531 /* We want to generate the DIE for TYPE so that we can generate the
18532    die for MEMBER, which has been defined; we will need to refer back
18533    to the member declaration nested within TYPE.  If we're trying to
18534    generate minimal debug info for TYPE, processing TYPE won't do the
18535    trick; we need to attach the member declaration by hand.  */
18536
18537 static void
18538 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18539 {
18540   gen_type_die (type, context_die);
18541
18542   /* If we're trying to avoid duplicate debug info, we may not have
18543      emitted the member decl for this function.  Emit it now.  */
18544   if (TYPE_STUB_DECL (type)
18545       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18546       && ! lookup_decl_die (member))
18547     {
18548       dw_die_ref type_die;
18549       gcc_assert (!decl_ultimate_origin (member));
18550
18551       push_decl_scope (type);
18552       type_die = lookup_type_die (type);
18553       if (TREE_CODE (member) == FUNCTION_DECL)
18554         gen_subprogram_die (member, type_die);
18555       else if (TREE_CODE (member) == FIELD_DECL)
18556         {
18557           /* Ignore the nameless fields that are used to skip bits but handle
18558              C++ anonymous unions and structs.  */
18559           if (DECL_NAME (member) != NULL_TREE
18560               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18561               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18562             {
18563               gen_type_die (member_declared_type (member), type_die);
18564               gen_field_die (member, type_die);
18565             }
18566         }
18567       else
18568         gen_variable_die (member, NULL_TREE, type_die);
18569
18570       pop_decl_scope ();
18571     }
18572 }
18573
18574 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18575    may later generate inlined and/or out-of-line instances of.  */
18576
18577 static void
18578 dwarf2out_abstract_function (tree decl)
18579 {
18580   dw_die_ref old_die;
18581   tree save_fn;
18582   tree context;
18583   int was_abstract;
18584   htab_t old_decl_loc_table;
18585
18586   /* Make sure we have the actual abstract inline, not a clone.  */
18587   decl = DECL_ORIGIN (decl);
18588
18589   old_die = lookup_decl_die (decl);
18590   if (old_die && get_AT (old_die, DW_AT_inline))
18591     /* We've already generated the abstract instance.  */
18592     return;
18593
18594   /* We can be called while recursively when seeing block defining inlined subroutine
18595      DIE.  Be sure to not clobber the outer location table nor use it or we would
18596      get locations in abstract instantces.  */
18597   old_decl_loc_table = decl_loc_table;
18598   decl_loc_table = NULL;
18599
18600   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18601      we don't get confused by DECL_ABSTRACT.  */
18602   if (debug_info_level > DINFO_LEVEL_TERSE)
18603     {
18604       context = decl_class_context (decl);
18605       if (context)
18606         gen_type_die_for_member
18607           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18608     }
18609
18610   /* Pretend we've just finished compiling this function.  */
18611   save_fn = current_function_decl;
18612   current_function_decl = decl;
18613   push_cfun (DECL_STRUCT_FUNCTION (decl));
18614
18615   was_abstract = DECL_ABSTRACT (decl);
18616   set_decl_abstract_flags (decl, 1);
18617   dwarf2out_decl (decl);
18618   if (! was_abstract)
18619     set_decl_abstract_flags (decl, 0);
18620
18621   current_function_decl = save_fn;
18622   decl_loc_table = old_decl_loc_table;
18623   pop_cfun ();
18624 }
18625
18626 /* Helper function of premark_used_types() which gets called through
18627    htab_traverse.
18628
18629    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18630    marked as unused by prune_unused_types.  */
18631
18632 static int
18633 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18634 {
18635   tree type;
18636   dw_die_ref die;
18637
18638   type = (tree) *slot;
18639   die = lookup_type_die (type);
18640   if (die != NULL)
18641     die->die_perennial_p = 1;
18642   return 1;
18643 }
18644
18645 /* Helper function of premark_types_used_by_global_vars which gets called
18646    through htab_traverse.
18647
18648    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18649    marked as unused by prune_unused_types. The DIE of the type is marked
18650    only if the global variable using the type will actually be emitted.  */
18651
18652 static int
18653 premark_types_used_by_global_vars_helper (void **slot,
18654                                           void *data ATTRIBUTE_UNUSED)
18655 {
18656   struct types_used_by_vars_entry *entry;
18657   dw_die_ref die;
18658
18659   entry = (struct types_used_by_vars_entry *) *slot;
18660   gcc_assert (entry->type != NULL
18661               && entry->var_decl != NULL);
18662   die = lookup_type_die (entry->type);
18663   if (die)
18664     {
18665       /* Ask cgraph if the global variable really is to be emitted.
18666          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18667       struct varpool_node *node = varpool_get_node (entry->var_decl);
18668       if (node && node->needed)
18669         {
18670           die->die_perennial_p = 1;
18671           /* Keep the parent DIEs as well.  */
18672           while ((die = die->die_parent) && die->die_perennial_p == 0)
18673             die->die_perennial_p = 1;
18674         }
18675     }
18676   return 1;
18677 }
18678
18679 /* Mark all members of used_types_hash as perennial.  */
18680
18681 static void
18682 premark_used_types (void)
18683 {
18684   if (cfun && cfun->used_types_hash)
18685     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18686 }
18687
18688 /* Mark all members of types_used_by_vars_entry as perennial.  */
18689
18690 static void
18691 premark_types_used_by_global_vars (void)
18692 {
18693   if (types_used_by_vars_hash)
18694     htab_traverse (types_used_by_vars_hash,
18695                    premark_types_used_by_global_vars_helper, NULL);
18696 }
18697
18698 /* Generate a DIE to represent a declared function (either file-scope or
18699    block-local).  */
18700
18701 static void
18702 gen_subprogram_die (tree decl, dw_die_ref context_die)
18703 {
18704   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18705   tree origin = decl_ultimate_origin (decl);
18706   dw_die_ref subr_die;
18707   tree fn_arg_types;
18708   tree outer_scope;
18709   dw_die_ref old_die = lookup_decl_die (decl);
18710   int declaration = (current_function_decl != decl
18711                      || class_or_namespace_scope_p (context_die));
18712
18713   premark_used_types ();
18714
18715   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18716      started to generate the abstract instance of an inline, decided to output
18717      its containing class, and proceeded to emit the declaration of the inline
18718      from the member list for the class.  If so, DECLARATION takes priority;
18719      we'll get back to the abstract instance when done with the class.  */
18720
18721   /* The class-scope declaration DIE must be the primary DIE.  */
18722   if (origin && declaration && class_or_namespace_scope_p (context_die))
18723     {
18724       origin = NULL;
18725       gcc_assert (!old_die);
18726     }
18727
18728   /* Now that the C++ front end lazily declares artificial member fns, we
18729      might need to retrofit the declaration into its class.  */
18730   if (!declaration && !origin && !old_die
18731       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18732       && !class_or_namespace_scope_p (context_die)
18733       && debug_info_level > DINFO_LEVEL_TERSE)
18734     old_die = force_decl_die (decl);
18735
18736   if (origin != NULL)
18737     {
18738       gcc_assert (!declaration || local_scope_p (context_die));
18739
18740       /* Fixup die_parent for the abstract instance of a nested
18741          inline function.  */
18742       if (old_die && old_die->die_parent == NULL)
18743         add_child_die (context_die, old_die);
18744
18745       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18746       add_abstract_origin_attribute (subr_die, origin);
18747     }
18748   else if (old_die)
18749     {
18750       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18751       struct dwarf_file_data * file_index = lookup_filename (s.file);
18752
18753       if (!get_AT_flag (old_die, DW_AT_declaration)
18754           /* We can have a normal definition following an inline one in the
18755              case of redefinition of GNU C extern inlines.
18756              It seems reasonable to use AT_specification in this case.  */
18757           && !get_AT (old_die, DW_AT_inline))
18758         {
18759           /* Detect and ignore this case, where we are trying to output
18760              something we have already output.  */
18761           return;
18762         }
18763
18764       /* If the definition comes from the same place as the declaration,
18765          maybe use the old DIE.  We always want the DIE for this function
18766          that has the *_pc attributes to be under comp_unit_die so the
18767          debugger can find it.  We also need to do this for abstract
18768          instances of inlines, since the spec requires the out-of-line copy
18769          to have the same parent.  For local class methods, this doesn't
18770          apply; we just use the old DIE.  */
18771       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18772           && (DECL_ARTIFICIAL (decl)
18773               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18774                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18775                       == (unsigned) s.line))))
18776         {
18777           subr_die = old_die;
18778
18779           /* Clear out the declaration attribute and the formal parameters.
18780              Do not remove all children, because it is possible that this
18781              declaration die was forced using force_decl_die(). In such
18782              cases die that forced declaration die (e.g. TAG_imported_module)
18783              is one of the children that we do not want to remove.  */
18784           remove_AT (subr_die, DW_AT_declaration);
18785           remove_AT (subr_die, DW_AT_object_pointer);
18786           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18787         }
18788       else
18789         {
18790           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18791           add_AT_specification (subr_die, old_die);
18792           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18793             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18794           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18795             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18796         }
18797     }
18798   else
18799     {
18800       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18801
18802       if (TREE_PUBLIC (decl))
18803         add_AT_flag (subr_die, DW_AT_external, 1);
18804
18805       add_name_and_src_coords_attributes (subr_die, decl);
18806       if (debug_info_level > DINFO_LEVEL_TERSE)
18807         {
18808           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18809           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18810                               0, 0, context_die);
18811         }
18812
18813       add_pure_or_virtual_attribute (subr_die, decl);
18814       if (DECL_ARTIFICIAL (decl))
18815         add_AT_flag (subr_die, DW_AT_artificial, 1);
18816
18817       add_accessibility_attribute (subr_die, decl);
18818     }
18819
18820   if (declaration)
18821     {
18822       if (!old_die || !get_AT (old_die, DW_AT_inline))
18823         {
18824           add_AT_flag (subr_die, DW_AT_declaration, 1);
18825
18826           /* If this is an explicit function declaration then generate
18827              a DW_AT_explicit attribute.  */
18828           if (lang_hooks.decls.function_decl_explicit_p (decl)
18829               && (dwarf_version >= 3 || !dwarf_strict))
18830             add_AT_flag (subr_die, DW_AT_explicit, 1);
18831
18832           /* The first time we see a member function, it is in the context of
18833              the class to which it belongs.  We make sure of this by emitting
18834              the class first.  The next time is the definition, which is
18835              handled above.  The two may come from the same source text.
18836
18837              Note that force_decl_die() forces function declaration die. It is
18838              later reused to represent definition.  */
18839           equate_decl_number_to_die (decl, subr_die);
18840         }
18841     }
18842   else if (DECL_ABSTRACT (decl))
18843     {
18844       if (DECL_DECLARED_INLINE_P (decl))
18845         {
18846           if (cgraph_function_possibly_inlined_p (decl))
18847             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18848           else
18849             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18850         }
18851       else
18852         {
18853           if (cgraph_function_possibly_inlined_p (decl))
18854             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18855           else
18856             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18857         }
18858
18859       if (DECL_DECLARED_INLINE_P (decl)
18860           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18861         add_AT_flag (subr_die, DW_AT_artificial, 1);
18862
18863       equate_decl_number_to_die (decl, subr_die);
18864     }
18865   else if (!DECL_EXTERNAL (decl))
18866     {
18867       HOST_WIDE_INT cfa_fb_offset;
18868
18869       if (!old_die || !get_AT (old_die, DW_AT_inline))
18870         equate_decl_number_to_die (decl, subr_die);
18871
18872       if (!flag_reorder_blocks_and_partition)
18873         {
18874           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18875                                        current_function_funcdef_no);
18876           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18877           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18878                                        current_function_funcdef_no);
18879           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18880
18881 #if VMS_DEBUGGING_INFO
18882       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18883          Section 2.3 Prologue and Epilogue Attributes:
18884          When a breakpoint is set on entry to a function, it is generally
18885          desirable for execution to be suspended, not on the very first
18886          instruction of the function, but rather at a point after the
18887          function's frame has been set up, after any language defined local
18888          declaration processing has been completed, and before execution of
18889          the first statement of the function begins. Debuggers generally
18890          cannot properly determine where this point is.  Similarly for a
18891          breakpoint set on exit from a function. The prologue and epilogue
18892          attributes allow a compiler to communicate the location(s) to use.  */
18893
18894       {
18895         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18896
18897         if (fde->dw_fde_vms_end_prologue)
18898           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18899             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18900
18901         if (fde->dw_fde_vms_begin_epilogue)
18902           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18903             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18904       }
18905 #endif
18906
18907           add_pubname (decl, subr_die);
18908           add_arange (decl, subr_die);
18909         }
18910       else
18911         {  /* Do nothing for now; maybe need to duplicate die, one for
18912               hot section and one for cold section, then use the hot/cold
18913               section begin/end labels to generate the aranges...  */
18914           /*
18915             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18916             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18917             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18918             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18919
18920             add_pubname (decl, subr_die);
18921             add_arange (decl, subr_die);
18922             add_arange (decl, subr_die);
18923            */
18924         }
18925
18926 #ifdef MIPS_DEBUGGING_INFO
18927       /* Add a reference to the FDE for this routine.  */
18928       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18929 #endif
18930
18931       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18932
18933       /* We define the "frame base" as the function's CFA.  This is more
18934          convenient for several reasons: (1) It's stable across the prologue
18935          and epilogue, which makes it better than just a frame pointer,
18936          (2) With dwarf3, there exists a one-byte encoding that allows us
18937          to reference the .debug_frame data by proxy, but failing that,
18938          (3) We can at least reuse the code inspection and interpretation
18939          code that determines the CFA position at various points in the
18940          function.  */
18941       if (dwarf_version >= 3)
18942         {
18943           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18944           add_AT_loc (subr_die, DW_AT_frame_base, op);
18945         }
18946       else
18947         {
18948           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18949           if (list->dw_loc_next)
18950             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18951           else
18952             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18953         }
18954
18955       /* Compute a displacement from the "steady-state frame pointer" to
18956          the CFA.  The former is what all stack slots and argument slots
18957          will reference in the rtl; the later is what we've told the
18958          debugger about.  We'll need to adjust all frame_base references
18959          by this displacement.  */
18960       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18961
18962       if (cfun->static_chain_decl)
18963         add_AT_location_description (subr_die, DW_AT_static_link,
18964                  loc_list_from_tree (cfun->static_chain_decl, 2));
18965     }
18966
18967   /* Generate child dies for template paramaters.  */
18968   if (debug_info_level > DINFO_LEVEL_TERSE)
18969     gen_generic_params_dies (decl);
18970
18971   /* Now output descriptions of the arguments for this function. This gets
18972      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18973      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18974      `...' at the end of the formal parameter list.  In order to find out if
18975      there was a trailing ellipsis or not, we must instead look at the type
18976      associated with the FUNCTION_DECL.  This will be a node of type
18977      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18978      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18979      an ellipsis at the end.  */
18980
18981   /* In the case where we are describing a mere function declaration, all we
18982      need to do here (and all we *can* do here) is to describe the *types* of
18983      its formal parameters.  */
18984   if (debug_info_level <= DINFO_LEVEL_TERSE)
18985     ;
18986   else if (declaration)
18987     gen_formal_types_die (decl, subr_die);
18988   else
18989     {
18990       /* Generate DIEs to represent all known formal parameters.  */
18991       tree parm = DECL_ARGUMENTS (decl);
18992       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18993       tree generic_decl_parm = generic_decl
18994                                 ? DECL_ARGUMENTS (generic_decl)
18995                                 : NULL;
18996
18997       /* Now we want to walk the list of parameters of the function and
18998          emit their relevant DIEs.
18999
19000          We consider the case of DECL being an instance of a generic function
19001          as well as it being a normal function.
19002
19003          If DECL is an instance of a generic function we walk the
19004          parameters of the generic function declaration _and_ the parameters of
19005          DECL itself. This is useful because we want to emit specific DIEs for
19006          function parameter packs and those are declared as part of the
19007          generic function declaration. In that particular case,
19008          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19009          That DIE has children DIEs representing the set of arguments
19010          of the pack. Note that the set of pack arguments can be empty.
19011          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19012          children DIE.
19013
19014          Otherwise, we just consider the parameters of DECL.  */
19015       while (generic_decl_parm || parm)
19016         {
19017           if (generic_decl_parm
19018               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19019             gen_formal_parameter_pack_die (generic_decl_parm,
19020                                            parm, subr_die,
19021                                            &parm);
19022           else if (parm)
19023             {
19024               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19025
19026               if (parm == DECL_ARGUMENTS (decl)
19027                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19028                   && parm_die
19029                   && (dwarf_version >= 3 || !dwarf_strict))
19030                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19031
19032               parm = DECL_CHAIN (parm);
19033             }
19034
19035           if (generic_decl_parm)
19036             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19037         }
19038
19039       /* Decide whether we need an unspecified_parameters DIE at the end.
19040          There are 2 more cases to do this for: 1) the ansi ... declaration -
19041          this is detectable when the end of the arg list is not a
19042          void_type_node 2) an unprototyped function declaration (not a
19043          definition).  This just means that we have no info about the
19044          parameters at all.  */
19045       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19046       if (fn_arg_types != NULL)
19047         {
19048           /* This is the prototyped case, check for....  */
19049           if (stdarg_p (TREE_TYPE (decl)))
19050             gen_unspecified_parameters_die (decl, subr_die);
19051         }
19052       else if (DECL_INITIAL (decl) == NULL_TREE)
19053         gen_unspecified_parameters_die (decl, subr_die);
19054     }
19055
19056   /* Output Dwarf info for all of the stuff within the body of the function
19057      (if it has one - it may be just a declaration).  */
19058   outer_scope = DECL_INITIAL (decl);
19059
19060   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19061      a function.  This BLOCK actually represents the outermost binding contour
19062      for the function, i.e. the contour in which the function's formal
19063      parameters and labels get declared. Curiously, it appears that the front
19064      end doesn't actually put the PARM_DECL nodes for the current function onto
19065      the BLOCK_VARS list for this outer scope, but are strung off of the
19066      DECL_ARGUMENTS list for the function instead.
19067
19068      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19069      the LABEL_DECL nodes for the function however, and we output DWARF info
19070      for those in decls_for_scope.  Just within the `outer_scope' there will be
19071      a BLOCK node representing the function's outermost pair of curly braces,
19072      and any blocks used for the base and member initializers of a C++
19073      constructor function.  */
19074   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19075     {
19076       /* Emit a DW_TAG_variable DIE for a named return value.  */
19077       if (DECL_NAME (DECL_RESULT (decl)))
19078         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19079
19080       current_function_has_inlines = 0;
19081       decls_for_scope (outer_scope, subr_die, 0);
19082     }
19083   /* Add the calling convention attribute if requested.  */
19084   add_calling_convention_attribute (subr_die, decl);
19085
19086 }
19087
19088 /* Returns a hash value for X (which really is a die_struct).  */
19089
19090 static hashval_t
19091 common_block_die_table_hash (const void *x)
19092 {
19093   const_dw_die_ref d = (const_dw_die_ref) x;
19094   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19095 }
19096
19097 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19098    as decl_id and die_parent of die_struct Y.  */
19099
19100 static int
19101 common_block_die_table_eq (const void *x, const void *y)
19102 {
19103   const_dw_die_ref d = (const_dw_die_ref) x;
19104   const_dw_die_ref e = (const_dw_die_ref) y;
19105   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19106 }
19107
19108 /* Generate a DIE to represent a declared data object.
19109    Either DECL or ORIGIN must be non-null.  */
19110
19111 static void
19112 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19113 {
19114   HOST_WIDE_INT off;
19115   tree com_decl;
19116   tree decl_or_origin = decl ? decl : origin;
19117   tree ultimate_origin;
19118   dw_die_ref var_die;
19119   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19120   dw_die_ref origin_die;
19121   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19122                       || class_or_namespace_scope_p (context_die));
19123   bool specialization_p = false;
19124
19125   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19126   if (decl || ultimate_origin)
19127     origin = ultimate_origin;
19128   com_decl = fortran_common (decl_or_origin, &off);
19129
19130   /* Symbol in common gets emitted as a child of the common block, in the form
19131      of a data member.  */
19132   if (com_decl)
19133     {
19134       dw_die_ref com_die;
19135       dw_loc_list_ref loc;
19136       die_node com_die_arg;
19137
19138       var_die = lookup_decl_die (decl_or_origin);
19139       if (var_die)
19140         {
19141           if (get_AT (var_die, DW_AT_location) == NULL)
19142             {
19143               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19144               if (loc)
19145                 {
19146                   if (off)
19147                     {
19148                       /* Optimize the common case.  */
19149                       if (single_element_loc_list_p (loc)
19150                           && loc->expr->dw_loc_opc == DW_OP_addr
19151                           && loc->expr->dw_loc_next == NULL
19152                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19153                              == SYMBOL_REF)
19154                         loc->expr->dw_loc_oprnd1.v.val_addr
19155                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19156                         else
19157                           loc_list_plus_const (loc, off);
19158                     }
19159                   add_AT_location_description (var_die, DW_AT_location, loc);
19160                   remove_AT (var_die, DW_AT_declaration);
19161                 }
19162             }
19163           return;
19164         }
19165
19166       if (common_block_die_table == NULL)
19167         common_block_die_table
19168           = htab_create_ggc (10, common_block_die_table_hash,
19169                              common_block_die_table_eq, NULL);
19170
19171       com_die_arg.decl_id = DECL_UID (com_decl);
19172       com_die_arg.die_parent = context_die;
19173       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19174       loc = loc_list_from_tree (com_decl, 2);
19175       if (com_die == NULL)
19176         {
19177           const char *cnam
19178             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19179           void **slot;
19180
19181           com_die = new_die (DW_TAG_common_block, context_die, decl);
19182           add_name_and_src_coords_attributes (com_die, com_decl);
19183           if (loc)
19184             {
19185               add_AT_location_description (com_die, DW_AT_location, loc);
19186               /* Avoid sharing the same loc descriptor between
19187                  DW_TAG_common_block and DW_TAG_variable.  */
19188               loc = loc_list_from_tree (com_decl, 2);
19189             }
19190           else if (DECL_EXTERNAL (decl))
19191             add_AT_flag (com_die, DW_AT_declaration, 1);
19192           add_pubname_string (cnam, com_die); /* ??? needed? */
19193           com_die->decl_id = DECL_UID (com_decl);
19194           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19195           *slot = (void *) com_die;
19196         }
19197       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19198         {
19199           add_AT_location_description (com_die, DW_AT_location, loc);
19200           loc = loc_list_from_tree (com_decl, 2);
19201           remove_AT (com_die, DW_AT_declaration);
19202         }
19203       var_die = new_die (DW_TAG_variable, com_die, decl);
19204       add_name_and_src_coords_attributes (var_die, decl);
19205       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19206                           TREE_THIS_VOLATILE (decl), context_die);
19207       add_AT_flag (var_die, DW_AT_external, 1);
19208       if (loc)
19209         {
19210           if (off)
19211             {
19212               /* Optimize the common case.  */
19213               if (single_element_loc_list_p (loc)
19214                   && loc->expr->dw_loc_opc == DW_OP_addr
19215                   && loc->expr->dw_loc_next == NULL
19216                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19217                 loc->expr->dw_loc_oprnd1.v.val_addr
19218                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19219               else
19220                 loc_list_plus_const (loc, off);
19221             }
19222           add_AT_location_description (var_die, DW_AT_location, loc);
19223         }
19224       else if (DECL_EXTERNAL (decl))
19225         add_AT_flag (var_die, DW_AT_declaration, 1);
19226       equate_decl_number_to_die (decl, var_die);
19227       return;
19228     }
19229
19230   /* If the compiler emitted a definition for the DECL declaration
19231      and if we already emitted a DIE for it, don't emit a second
19232      DIE for it again. Allow re-declarations of DECLs that are
19233      inside functions, though.  */
19234   if (old_die && declaration && !local_scope_p (context_die))
19235     return;
19236
19237   /* For static data members, the declaration in the class is supposed
19238      to have DW_TAG_member tag; the specification should still be
19239      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19240   if (declaration && class_scope_p (context_die))
19241     var_die = new_die (DW_TAG_member, context_die, decl);
19242   else
19243     var_die = new_die (DW_TAG_variable, context_die, decl);
19244
19245   origin_die = NULL;
19246   if (origin != NULL)
19247     origin_die = add_abstract_origin_attribute (var_die, origin);
19248
19249   /* Loop unrolling can create multiple blocks that refer to the same
19250      static variable, so we must test for the DW_AT_declaration flag.
19251
19252      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19253      copy decls and set the DECL_ABSTRACT flag on them instead of
19254      sharing them.
19255
19256      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19257
19258      ??? The declare_in_namespace support causes us to get two DIEs for one
19259      variable, both of which are declarations.  We want to avoid considering
19260      one to be a specification, so we must test that this DIE is not a
19261      declaration.  */
19262   else if (old_die && TREE_STATIC (decl) && ! declaration
19263            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19264     {
19265       /* This is a definition of a C++ class level static.  */
19266       add_AT_specification (var_die, old_die);
19267       specialization_p = true;
19268       if (DECL_NAME (decl))
19269         {
19270           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19271           struct dwarf_file_data * file_index = lookup_filename (s.file);
19272
19273           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19274             add_AT_file (var_die, DW_AT_decl_file, file_index);
19275
19276           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19277             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19278
19279           if (old_die->die_tag == DW_TAG_member)
19280             add_linkage_name (var_die, decl);
19281         }
19282     }
19283   else
19284     add_name_and_src_coords_attributes (var_die, decl);
19285
19286   if ((origin == NULL && !specialization_p)
19287       || (origin != NULL
19288           && !DECL_ABSTRACT (decl_or_origin)
19289           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19290                                        decl_function_context
19291                                                         (decl_or_origin))))
19292     {
19293       tree type = TREE_TYPE (decl_or_origin);
19294
19295       if (decl_by_reference_p (decl_or_origin))
19296         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19297       else
19298         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19299                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19300     }
19301
19302   if (origin == NULL && !specialization_p)
19303     {
19304       if (TREE_PUBLIC (decl))
19305         add_AT_flag (var_die, DW_AT_external, 1);
19306
19307       if (DECL_ARTIFICIAL (decl))
19308         add_AT_flag (var_die, DW_AT_artificial, 1);
19309
19310       add_accessibility_attribute (var_die, decl);
19311     }
19312
19313   if (declaration)
19314     add_AT_flag (var_die, DW_AT_declaration, 1);
19315
19316   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19317     equate_decl_number_to_die (decl, var_die);
19318
19319   if (! declaration
19320       && (! DECL_ABSTRACT (decl_or_origin)
19321           /* Local static vars are shared between all clones/inlines,
19322              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19323              already set.  */
19324           || (TREE_CODE (decl_or_origin) == VAR_DECL
19325               && TREE_STATIC (decl_or_origin)
19326               && DECL_RTL_SET_P (decl_or_origin)))
19327       /* When abstract origin already has DW_AT_location attribute, no need
19328          to add it again.  */
19329       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19330     {
19331       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19332           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19333         defer_location (decl_or_origin, var_die);
19334       else
19335         add_location_or_const_value_attribute (var_die,
19336                                                decl_or_origin,
19337                                                DW_AT_location);
19338       add_pubname (decl_or_origin, var_die);
19339     }
19340   else
19341     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19342 }
19343
19344 /* Generate a DIE to represent a named constant.  */
19345
19346 static void
19347 gen_const_die (tree decl, dw_die_ref context_die)
19348 {
19349   dw_die_ref const_die;
19350   tree type = TREE_TYPE (decl);
19351
19352   const_die = new_die (DW_TAG_constant, context_die, decl);
19353   add_name_and_src_coords_attributes (const_die, decl);
19354   add_type_attribute (const_die, type, 1, 0, context_die);
19355   if (TREE_PUBLIC (decl))
19356     add_AT_flag (const_die, DW_AT_external, 1);
19357   if (DECL_ARTIFICIAL (decl))
19358     add_AT_flag (const_die, DW_AT_artificial, 1);
19359   tree_add_const_value_attribute_for_decl (const_die, decl);
19360 }
19361
19362 /* Generate a DIE to represent a label identifier.  */
19363
19364 static void
19365 gen_label_die (tree decl, dw_die_ref context_die)
19366 {
19367   tree origin = decl_ultimate_origin (decl);
19368   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19369   rtx insn;
19370   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19371
19372   if (origin != NULL)
19373     add_abstract_origin_attribute (lbl_die, origin);
19374   else
19375     add_name_and_src_coords_attributes (lbl_die, decl);
19376
19377   if (DECL_ABSTRACT (decl))
19378     equate_decl_number_to_die (decl, lbl_die);
19379   else
19380     {
19381       insn = DECL_RTL_IF_SET (decl);
19382
19383       /* Deleted labels are programmer specified labels which have been
19384          eliminated because of various optimizations.  We still emit them
19385          here so that it is possible to put breakpoints on them.  */
19386       if (insn
19387           && (LABEL_P (insn)
19388               || ((NOTE_P (insn)
19389                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19390         {
19391           /* When optimization is enabled (via -O) some parts of the compiler
19392              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19393              represent source-level labels which were explicitly declared by
19394              the user.  This really shouldn't be happening though, so catch
19395              it if it ever does happen.  */
19396           gcc_assert (!INSN_DELETED_P (insn));
19397
19398           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19399           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19400         }
19401     }
19402 }
19403
19404 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19405    attributes to the DIE for a block STMT, to describe where the inlined
19406    function was called from.  This is similar to add_src_coords_attributes.  */
19407
19408 static inline void
19409 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19410 {
19411   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19412
19413   if (dwarf_version >= 3 || !dwarf_strict)
19414     {
19415       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19416       add_AT_unsigned (die, DW_AT_call_line, s.line);
19417     }
19418 }
19419
19420
19421 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19422    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19423
19424 static inline void
19425 add_high_low_attributes (tree stmt, dw_die_ref die)
19426 {
19427   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19428
19429   if (BLOCK_FRAGMENT_CHAIN (stmt)
19430       && (dwarf_version >= 3 || !dwarf_strict))
19431     {
19432       tree chain;
19433
19434       if (inlined_function_outer_scope_p (stmt))
19435         {
19436           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19437                                        BLOCK_NUMBER (stmt));
19438           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19439         }
19440
19441       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19442
19443       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19444       do
19445         {
19446           add_ranges (chain);
19447           chain = BLOCK_FRAGMENT_CHAIN (chain);
19448         }
19449       while (chain);
19450       add_ranges (NULL);
19451     }
19452   else
19453     {
19454       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19455                                    BLOCK_NUMBER (stmt));
19456       add_AT_lbl_id (die, DW_AT_low_pc, label);
19457       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19458                                    BLOCK_NUMBER (stmt));
19459       add_AT_lbl_id (die, DW_AT_high_pc, label);
19460     }
19461 }
19462
19463 /* Generate a DIE for a lexical block.  */
19464
19465 static void
19466 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19467 {
19468   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19469
19470   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19471     add_high_low_attributes (stmt, stmt_die);
19472
19473   decls_for_scope (stmt, stmt_die, depth);
19474 }
19475
19476 /* Generate a DIE for an inlined subprogram.  */
19477
19478 static void
19479 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19480 {
19481   tree decl;
19482
19483   /* The instance of function that is effectively being inlined shall not
19484      be abstract.  */
19485   gcc_assert (! BLOCK_ABSTRACT (stmt));
19486
19487   decl = block_ultimate_origin (stmt);
19488
19489   /* Emit info for the abstract instance first, if we haven't yet.  We
19490      must emit this even if the block is abstract, otherwise when we
19491      emit the block below (or elsewhere), we may end up trying to emit
19492      a die whose origin die hasn't been emitted, and crashing.  */
19493   dwarf2out_abstract_function (decl);
19494
19495   if (! BLOCK_ABSTRACT (stmt))
19496     {
19497       dw_die_ref subr_die
19498         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19499
19500       add_abstract_origin_attribute (subr_die, decl);
19501       if (TREE_ASM_WRITTEN (stmt))
19502         add_high_low_attributes (stmt, subr_die);
19503       add_call_src_coords_attributes (stmt, subr_die);
19504
19505       decls_for_scope (stmt, subr_die, depth);
19506       current_function_has_inlines = 1;
19507     }
19508 }
19509
19510 /* Generate a DIE for a field in a record, or structure.  */
19511
19512 static void
19513 gen_field_die (tree decl, dw_die_ref context_die)
19514 {
19515   dw_die_ref decl_die;
19516
19517   if (TREE_TYPE (decl) == error_mark_node)
19518     return;
19519
19520   decl_die = new_die (DW_TAG_member, context_die, decl);
19521   add_name_and_src_coords_attributes (decl_die, decl);
19522   add_type_attribute (decl_die, member_declared_type (decl),
19523                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19524                       context_die);
19525
19526   if (DECL_BIT_FIELD_TYPE (decl))
19527     {
19528       add_byte_size_attribute (decl_die, decl);
19529       add_bit_size_attribute (decl_die, decl);
19530       add_bit_offset_attribute (decl_die, decl);
19531     }
19532
19533   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19534     add_data_member_location_attribute (decl_die, decl);
19535
19536   if (DECL_ARTIFICIAL (decl))
19537     add_AT_flag (decl_die, DW_AT_artificial, 1);
19538
19539   add_accessibility_attribute (decl_die, decl);
19540
19541   /* Equate decl number to die, so that we can look up this decl later on.  */
19542   equate_decl_number_to_die (decl, decl_die);
19543 }
19544
19545 #if 0
19546 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19547    Use modified_type_die instead.
19548    We keep this code here just in case these types of DIEs may be needed to
19549    represent certain things in other languages (e.g. Pascal) someday.  */
19550
19551 static void
19552 gen_pointer_type_die (tree type, dw_die_ref context_die)
19553 {
19554   dw_die_ref ptr_die
19555     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19556
19557   equate_type_number_to_die (type, ptr_die);
19558   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19559   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19560 }
19561
19562 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19563    Use modified_type_die instead.
19564    We keep this code here just in case these types of DIEs may be needed to
19565    represent certain things in other languages (e.g. Pascal) someday.  */
19566
19567 static void
19568 gen_reference_type_die (tree type, dw_die_ref context_die)
19569 {
19570   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19571
19572   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19573     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19574   else
19575     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19576
19577   equate_type_number_to_die (type, ref_die);
19578   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19579   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19580 }
19581 #endif
19582
19583 /* Generate a DIE for a pointer to a member type.  */
19584
19585 static void
19586 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19587 {
19588   dw_die_ref ptr_die
19589     = new_die (DW_TAG_ptr_to_member_type,
19590                scope_die_for (type, context_die), type);
19591
19592   equate_type_number_to_die (type, ptr_die);
19593   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19594                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19595   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19596 }
19597
19598 /* Generate the DIE for the compilation unit.  */
19599
19600 static dw_die_ref
19601 gen_compile_unit_die (const char *filename)
19602 {
19603   dw_die_ref die;
19604   char producer[250];
19605   const char *language_string = lang_hooks.name;
19606   int language;
19607
19608   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19609
19610   if (filename)
19611     {
19612       add_name_attribute (die, filename);
19613       /* Don't add cwd for <built-in>.  */
19614       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19615         add_comp_dir_attribute (die);
19616     }
19617
19618   sprintf (producer, "%s %s", language_string, version_string);
19619
19620 #ifdef MIPS_DEBUGGING_INFO
19621   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19622      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19623      not appear in the producer string, the debugger reaches the conclusion
19624      that the object file is stripped and has no debugging information.
19625      To get the MIPS/SGI debugger to believe that there is debugging
19626      information in the object file, we add a -g to the producer string.  */
19627   if (debug_info_level > DINFO_LEVEL_TERSE)
19628     strcat (producer, " -g");
19629 #endif
19630
19631   add_AT_string (die, DW_AT_producer, producer);
19632
19633   /* If our producer is LTO try to figure out a common language to use
19634      from the global list of translation units.  */
19635   if (strcmp (language_string, "GNU GIMPLE") == 0)
19636     {
19637       unsigned i;
19638       tree t;
19639       const char *common_lang = NULL;
19640
19641       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19642         {
19643           if (!TRANSLATION_UNIT_LANGUAGE (t))
19644             continue;
19645           if (!common_lang)
19646             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19647           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19648             ;
19649           else if (strncmp (common_lang, "GNU C", 5) == 0
19650                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19651             /* Mixing C and C++ is ok, use C++ in that case.  */
19652             common_lang = "GNU C++";
19653           else
19654             {
19655               /* Fall back to C.  */
19656               common_lang = NULL;
19657               break;
19658             }
19659         }
19660
19661       if (common_lang)
19662         language_string = common_lang;
19663     }
19664
19665   language = DW_LANG_C89;
19666   if (strcmp (language_string, "GNU C++") == 0)
19667     language = DW_LANG_C_plus_plus;
19668   else if (strcmp (language_string, "GNU F77") == 0)
19669     language = DW_LANG_Fortran77;
19670   else if (strcmp (language_string, "GNU Pascal") == 0)
19671     language = DW_LANG_Pascal83;
19672   else if (dwarf_version >= 3 || !dwarf_strict)
19673     {
19674       if (strcmp (language_string, "GNU Ada") == 0)
19675         language = DW_LANG_Ada95;
19676       else if (strcmp (language_string, "GNU Fortran") == 0)
19677         language = DW_LANG_Fortran95;
19678       else if (strcmp (language_string, "GNU Java") == 0)
19679         language = DW_LANG_Java;
19680       else if (strcmp (language_string, "GNU Objective-C") == 0)
19681         language = DW_LANG_ObjC;
19682       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19683         language = DW_LANG_ObjC_plus_plus;
19684     }
19685
19686   add_AT_unsigned (die, DW_AT_language, language);
19687
19688   switch (language)
19689     {
19690     case DW_LANG_Fortran77:
19691     case DW_LANG_Fortran90:
19692     case DW_LANG_Fortran95:
19693       /* Fortran has case insensitive identifiers and the front-end
19694          lowercases everything.  */
19695       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19696       break;
19697     default:
19698       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19699       break;
19700     }
19701   return die;
19702 }
19703
19704 /* Generate the DIE for a base class.  */
19705
19706 static void
19707 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19708 {
19709   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19710
19711   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19712   add_data_member_location_attribute (die, binfo);
19713
19714   if (BINFO_VIRTUAL_P (binfo))
19715     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19716
19717   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19718      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19719      the default has always been DW_ACCESS_private.  */
19720   if (access == access_public_node)
19721     {
19722       if (dwarf_version == 2
19723           || context_die->die_tag == DW_TAG_class_type)
19724       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19725     }
19726   else if (access == access_protected_node)
19727     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19728   else if (dwarf_version > 2
19729            && context_die->die_tag != DW_TAG_class_type)
19730     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19731 }
19732
19733 /* Generate a DIE for a class member.  */
19734
19735 static void
19736 gen_member_die (tree type, dw_die_ref context_die)
19737 {
19738   tree member;
19739   tree binfo = TYPE_BINFO (type);
19740   dw_die_ref child;
19741
19742   /* If this is not an incomplete type, output descriptions of each of its
19743      members. Note that as we output the DIEs necessary to represent the
19744      members of this record or union type, we will also be trying to output
19745      DIEs to represent the *types* of those members. However the `type'
19746      function (above) will specifically avoid generating type DIEs for member
19747      types *within* the list of member DIEs for this (containing) type except
19748      for those types (of members) which are explicitly marked as also being
19749      members of this (containing) type themselves.  The g++ front- end can
19750      force any given type to be treated as a member of some other (containing)
19751      type by setting the TYPE_CONTEXT of the given (member) type to point to
19752      the TREE node representing the appropriate (containing) type.  */
19753
19754   /* First output info about the base classes.  */
19755   if (binfo)
19756     {
19757       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19758       int i;
19759       tree base;
19760
19761       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19762         gen_inheritance_die (base,
19763                              (accesses ? VEC_index (tree, accesses, i)
19764                               : access_public_node), context_die);
19765     }
19766
19767   /* Now output info about the data members and type members.  */
19768   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19769     {
19770       /* If we thought we were generating minimal debug info for TYPE
19771          and then changed our minds, some of the member declarations
19772          may have already been defined.  Don't define them again, but
19773          do put them in the right order.  */
19774
19775       child = lookup_decl_die (member);
19776       if (child)
19777         splice_child_die (context_die, child);
19778       else
19779         gen_decl_die (member, NULL, context_die);
19780     }
19781
19782   /* Now output info about the function members (if any).  */
19783   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19784     {
19785       /* Don't include clones in the member list.  */
19786       if (DECL_ABSTRACT_ORIGIN (member))
19787         continue;
19788
19789       child = lookup_decl_die (member);
19790       if (child)
19791         splice_child_die (context_die, child);
19792       else
19793         gen_decl_die (member, NULL, context_die);
19794     }
19795 }
19796
19797 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19798    is set, we pretend that the type was never defined, so we only get the
19799    member DIEs needed by later specification DIEs.  */
19800
19801 static void
19802 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19803                                 enum debug_info_usage usage)
19804 {
19805   dw_die_ref type_die = lookup_type_die (type);
19806   dw_die_ref scope_die = 0;
19807   int nested = 0;
19808   int complete = (TYPE_SIZE (type)
19809                   && (! TYPE_STUB_DECL (type)
19810                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19811   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19812   complete = complete && should_emit_struct_debug (type, usage);
19813
19814   if (type_die && ! complete)
19815     return;
19816
19817   if (TYPE_CONTEXT (type) != NULL_TREE
19818       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19819           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19820     nested = 1;
19821
19822   scope_die = scope_die_for (type, context_die);
19823
19824   if (! type_die || (nested && is_cu_die (scope_die)))
19825     /* First occurrence of type or toplevel definition of nested class.  */
19826     {
19827       dw_die_ref old_die = type_die;
19828
19829       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19830                           ? record_type_tag (type) : DW_TAG_union_type,
19831                           scope_die, type);
19832       equate_type_number_to_die (type, type_die);
19833       if (old_die)
19834         add_AT_specification (type_die, old_die);
19835       else
19836         add_name_attribute (type_die, type_tag (type));
19837     }
19838   else
19839     remove_AT (type_die, DW_AT_declaration);
19840
19841   /* Generate child dies for template paramaters.  */
19842   if (debug_info_level > DINFO_LEVEL_TERSE
19843       && COMPLETE_TYPE_P (type))
19844     gen_generic_params_dies (type);
19845
19846   /* If this type has been completed, then give it a byte_size attribute and
19847      then give a list of members.  */
19848   if (complete && !ns_decl)
19849     {
19850       /* Prevent infinite recursion in cases where the type of some member of
19851          this type is expressed in terms of this type itself.  */
19852       TREE_ASM_WRITTEN (type) = 1;
19853       add_byte_size_attribute (type_die, type);
19854       if (TYPE_STUB_DECL (type) != NULL_TREE)
19855         {
19856           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19857           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19858         }
19859
19860       /* If the first reference to this type was as the return type of an
19861          inline function, then it may not have a parent.  Fix this now.  */
19862       if (type_die->die_parent == NULL)
19863         add_child_die (scope_die, type_die);
19864
19865       push_decl_scope (type);
19866       gen_member_die (type, type_die);
19867       pop_decl_scope ();
19868
19869       /* GNU extension: Record what type our vtable lives in.  */
19870       if (TYPE_VFIELD (type))
19871         {
19872           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19873
19874           gen_type_die (vtype, context_die);
19875           add_AT_die_ref (type_die, DW_AT_containing_type,
19876                           lookup_type_die (vtype));
19877         }
19878     }
19879   else
19880     {
19881       add_AT_flag (type_die, DW_AT_declaration, 1);
19882
19883       /* We don't need to do this for function-local types.  */
19884       if (TYPE_STUB_DECL (type)
19885           && ! decl_function_context (TYPE_STUB_DECL (type)))
19886         VEC_safe_push (tree, gc, incomplete_types, type);
19887     }
19888
19889   if (get_AT (type_die, DW_AT_name))
19890     add_pubtype (type, type_die);
19891 }
19892
19893 /* Generate a DIE for a subroutine _type_.  */
19894
19895 static void
19896 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19897 {
19898   tree return_type = TREE_TYPE (type);
19899   dw_die_ref subr_die
19900     = new_die (DW_TAG_subroutine_type,
19901                scope_die_for (type, context_die), type);
19902
19903   equate_type_number_to_die (type, subr_die);
19904   add_prototyped_attribute (subr_die, type);
19905   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19906   gen_formal_types_die (type, subr_die);
19907
19908   if (get_AT (subr_die, DW_AT_name))
19909     add_pubtype (type, subr_die);
19910 }
19911
19912 /* Generate a DIE for a type definition.  */
19913
19914 static void
19915 gen_typedef_die (tree decl, dw_die_ref context_die)
19916 {
19917   dw_die_ref type_die;
19918   tree origin;
19919
19920   if (TREE_ASM_WRITTEN (decl))
19921     return;
19922
19923   TREE_ASM_WRITTEN (decl) = 1;
19924   type_die = new_die (DW_TAG_typedef, context_die, decl);
19925   origin = decl_ultimate_origin (decl);
19926   if (origin != NULL)
19927     add_abstract_origin_attribute (type_die, origin);
19928   else
19929     {
19930       tree type;
19931
19932       add_name_and_src_coords_attributes (type_die, decl);
19933       if (DECL_ORIGINAL_TYPE (decl))
19934         {
19935           type = DECL_ORIGINAL_TYPE (decl);
19936
19937           gcc_assert (type != TREE_TYPE (decl));
19938           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19939         }
19940       else
19941         {
19942           type = TREE_TYPE (decl);
19943
19944           if (is_naming_typedef_decl (TYPE_NAME (type)))
19945             {
19946               /* Here, we are in the case of decl being a typedef naming
19947                  an anonymous type, e.g:
19948                      typedef struct {...} foo;
19949                  In that case TREE_TYPE (decl) is not a typedef variant
19950                  type and TYPE_NAME of the anonymous type is set to the
19951                  TYPE_DECL of the typedef. This construct is emitted by
19952                  the C++ FE.
19953
19954                  TYPE is the anonymous struct named by the typedef
19955                  DECL. As we need the DW_AT_type attribute of the
19956                  DW_TAG_typedef to point to the DIE of TYPE, let's
19957                  generate that DIE right away. add_type_attribute
19958                  called below will then pick (via lookup_type_die) that
19959                  anonymous struct DIE.  */
19960               if (!TREE_ASM_WRITTEN (type))
19961                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19962             }
19963         }
19964
19965       add_type_attribute (type_die, type, TREE_READONLY (decl),
19966                           TREE_THIS_VOLATILE (decl), context_die);
19967
19968       if (is_naming_typedef_decl (decl))
19969         /* We want that all subsequent calls to lookup_type_die with
19970            TYPE in argument yield the DW_TAG_typedef we have just
19971            created.  */
19972         equate_type_number_to_die (type, type_die);
19973
19974       add_accessibility_attribute (type_die, decl);
19975     }
19976
19977   if (DECL_ABSTRACT (decl))
19978     equate_decl_number_to_die (decl, type_die);
19979
19980   if (get_AT (type_die, DW_AT_name))
19981     add_pubtype (decl, type_die);
19982 }
19983
19984 /* Generate a DIE for a struct, class, enum or union type.  */
19985
19986 static void
19987 gen_tagged_type_die (tree type,
19988                      dw_die_ref context_die,
19989                      enum debug_info_usage usage)
19990 {
19991   int need_pop;
19992
19993   if (type == NULL_TREE
19994       || !is_tagged_type (type))
19995     return;
19996
19997   /* If this is a nested type whose containing class hasn't been written
19998      out yet, writing it out will cover this one, too.  This does not apply
19999      to instantiations of member class templates; they need to be added to
20000      the containing class as they are generated.  FIXME: This hurts the
20001      idea of combining type decls from multiple TUs, since we can't predict
20002      what set of template instantiations we'll get.  */
20003   if (TYPE_CONTEXT (type)
20004       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20005       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20006     {
20007       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20008
20009       if (TREE_ASM_WRITTEN (type))
20010         return;
20011
20012       /* If that failed, attach ourselves to the stub.  */
20013       push_decl_scope (TYPE_CONTEXT (type));
20014       context_die = lookup_type_die (TYPE_CONTEXT (type));
20015       need_pop = 1;
20016     }
20017   else if (TYPE_CONTEXT (type) != NULL_TREE
20018            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20019     {
20020       /* If this type is local to a function that hasn't been written
20021          out yet, use a NULL context for now; it will be fixed up in
20022          decls_for_scope.  */
20023       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20024       need_pop = 0;
20025     }
20026   else
20027     {
20028       context_die = declare_in_namespace (type, context_die);
20029       need_pop = 0;
20030     }
20031
20032   if (TREE_CODE (type) == ENUMERAL_TYPE)
20033     {
20034       /* This might have been written out by the call to
20035          declare_in_namespace.  */
20036       if (!TREE_ASM_WRITTEN (type))
20037         gen_enumeration_type_die (type, context_die);
20038     }
20039   else
20040     gen_struct_or_union_type_die (type, context_die, usage);
20041
20042   if (need_pop)
20043     pop_decl_scope ();
20044
20045   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20046      it up if it is ever completed.  gen_*_type_die will set it for us
20047      when appropriate.  */
20048 }
20049
20050 /* Generate a type description DIE.  */
20051
20052 static void
20053 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20054                                 enum debug_info_usage usage)
20055 {
20056   struct array_descr_info info;
20057
20058   if (type == NULL_TREE || type == error_mark_node)
20059     return;
20060
20061   /* If TYPE is a typedef type variant, let's generate debug info
20062      for the parent typedef which TYPE is a type of.  */
20063   if (typedef_variant_p (type))
20064     {
20065       if (TREE_ASM_WRITTEN (type))
20066         return;
20067
20068       /* Prevent broken recursion; we can't hand off to the same type.  */
20069       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20070
20071       /* Use the DIE of the containing namespace as the parent DIE of
20072          the type description DIE we want to generate.  */
20073       if (DECL_CONTEXT (TYPE_NAME (type))
20074           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20075         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20076
20077       TREE_ASM_WRITTEN (type) = 1;
20078
20079       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20080       return;
20081     }
20082
20083   /* If type is an anonymous tagged type named by a typedef, let's
20084      generate debug info for the typedef.  */
20085   if (is_naming_typedef_decl (TYPE_NAME (type)))
20086     {
20087       /* Use the DIE of the containing namespace as the parent DIE of
20088          the type description DIE we want to generate.  */
20089       if (DECL_CONTEXT (TYPE_NAME (type))
20090           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20091         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20092       
20093       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20094       return;
20095     }
20096
20097   /* If this is an array type with hidden descriptor, handle it first.  */
20098   if (!TREE_ASM_WRITTEN (type)
20099       && lang_hooks.types.get_array_descr_info
20100       && lang_hooks.types.get_array_descr_info (type, &info)
20101       && (dwarf_version >= 3 || !dwarf_strict))
20102     {
20103       gen_descr_array_type_die (type, &info, context_die);
20104       TREE_ASM_WRITTEN (type) = 1;
20105       return;
20106     }
20107
20108   /* We are going to output a DIE to represent the unqualified version
20109      of this type (i.e. without any const or volatile qualifiers) so
20110      get the main variant (i.e. the unqualified version) of this type
20111      now.  (Vectors are special because the debugging info is in the
20112      cloned type itself).  */
20113   if (TREE_CODE (type) != VECTOR_TYPE)
20114     type = type_main_variant (type);
20115
20116   if (TREE_ASM_WRITTEN (type))
20117     return;
20118
20119   switch (TREE_CODE (type))
20120     {
20121     case ERROR_MARK:
20122       break;
20123
20124     case POINTER_TYPE:
20125     case REFERENCE_TYPE:
20126       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20127          ensures that the gen_type_die recursion will terminate even if the
20128          type is recursive.  Recursive types are possible in Ada.  */
20129       /* ??? We could perhaps do this for all types before the switch
20130          statement.  */
20131       TREE_ASM_WRITTEN (type) = 1;
20132
20133       /* For these types, all that is required is that we output a DIE (or a
20134          set of DIEs) to represent the "basis" type.  */
20135       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20136                                 DINFO_USAGE_IND_USE);
20137       break;
20138
20139     case OFFSET_TYPE:
20140       /* This code is used for C++ pointer-to-data-member types.
20141          Output a description of the relevant class type.  */
20142       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20143                                         DINFO_USAGE_IND_USE);
20144
20145       /* Output a description of the type of the object pointed to.  */
20146       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20147                                         DINFO_USAGE_IND_USE);
20148
20149       /* Now output a DIE to represent this pointer-to-data-member type
20150          itself.  */
20151       gen_ptr_to_mbr_type_die (type, context_die);
20152       break;
20153
20154     case FUNCTION_TYPE:
20155       /* Force out return type (in case it wasn't forced out already).  */
20156       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20157                                         DINFO_USAGE_DIR_USE);
20158       gen_subroutine_type_die (type, context_die);
20159       break;
20160
20161     case METHOD_TYPE:
20162       /* Force out return type (in case it wasn't forced out already).  */
20163       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20164                                         DINFO_USAGE_DIR_USE);
20165       gen_subroutine_type_die (type, context_die);
20166       break;
20167
20168     case ARRAY_TYPE:
20169       gen_array_type_die (type, context_die);
20170       break;
20171
20172     case VECTOR_TYPE:
20173       gen_array_type_die (type, context_die);
20174       break;
20175
20176     case ENUMERAL_TYPE:
20177     case RECORD_TYPE:
20178     case UNION_TYPE:
20179     case QUAL_UNION_TYPE:
20180       gen_tagged_type_die (type, context_die, usage);
20181       return;
20182
20183     case VOID_TYPE:
20184     case INTEGER_TYPE:
20185     case REAL_TYPE:
20186     case FIXED_POINT_TYPE:
20187     case COMPLEX_TYPE:
20188     case BOOLEAN_TYPE:
20189       /* No DIEs needed for fundamental types.  */
20190       break;
20191
20192     case LANG_TYPE:
20193       /* Just use DW_TAG_unspecified_type.  */
20194       {
20195         dw_die_ref type_die = lookup_type_die (type);
20196         if (type_die == NULL)
20197           {
20198             tree name = TYPE_NAME (type);
20199             if (TREE_CODE (name) == TYPE_DECL)
20200               name = DECL_NAME (name);
20201             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20202             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20203             equate_type_number_to_die (type, type_die);
20204           }
20205       }
20206       break;
20207
20208     default:
20209       gcc_unreachable ();
20210     }
20211
20212   TREE_ASM_WRITTEN (type) = 1;
20213 }
20214
20215 static void
20216 gen_type_die (tree type, dw_die_ref context_die)
20217 {
20218   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20219 }
20220
20221 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20222    things which are local to the given block.  */
20223
20224 static void
20225 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20226 {
20227   int must_output_die = 0;
20228   bool inlined_func;
20229
20230   /* Ignore blocks that are NULL.  */
20231   if (stmt == NULL_TREE)
20232     return;
20233
20234   inlined_func = inlined_function_outer_scope_p (stmt);
20235
20236   /* If the block is one fragment of a non-contiguous block, do not
20237      process the variables, since they will have been done by the
20238      origin block.  Do process subblocks.  */
20239   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20240     {
20241       tree sub;
20242
20243       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20244         gen_block_die (sub, context_die, depth + 1);
20245
20246       return;
20247     }
20248
20249   /* Determine if we need to output any Dwarf DIEs at all to represent this
20250      block.  */
20251   if (inlined_func)
20252     /* The outer scopes for inlinings *must* always be represented.  We
20253        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20254     must_output_die = 1;
20255   else
20256     {
20257       /* Determine if this block directly contains any "significant"
20258          local declarations which we will need to output DIEs for.  */
20259       if (debug_info_level > DINFO_LEVEL_TERSE)
20260         /* We are not in terse mode so *any* local declaration counts
20261            as being a "significant" one.  */
20262         must_output_die = ((BLOCK_VARS (stmt) != NULL
20263                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20264                            && (TREE_USED (stmt)
20265                                || TREE_ASM_WRITTEN (stmt)
20266                                || BLOCK_ABSTRACT (stmt)));
20267       else if ((TREE_USED (stmt)
20268                 || TREE_ASM_WRITTEN (stmt)
20269                 || BLOCK_ABSTRACT (stmt))
20270                && !dwarf2out_ignore_block (stmt))
20271         must_output_die = 1;
20272     }
20273
20274   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20275      DIE for any block which contains no significant local declarations at
20276      all.  Rather, in such cases we just call `decls_for_scope' so that any
20277      needed Dwarf info for any sub-blocks will get properly generated. Note
20278      that in terse mode, our definition of what constitutes a "significant"
20279      local declaration gets restricted to include only inlined function
20280      instances and local (nested) function definitions.  */
20281   if (must_output_die)
20282     {
20283       if (inlined_func)
20284         {
20285           /* If STMT block is abstract, that means we have been called
20286              indirectly from dwarf2out_abstract_function.
20287              That function rightfully marks the descendent blocks (of
20288              the abstract function it is dealing with) as being abstract,
20289              precisely to prevent us from emitting any
20290              DW_TAG_inlined_subroutine DIE as a descendent
20291              of an abstract function instance. So in that case, we should
20292              not call gen_inlined_subroutine_die.
20293
20294              Later though, when cgraph asks dwarf2out to emit info
20295              for the concrete instance of the function decl into which
20296              the concrete instance of STMT got inlined, the later will lead
20297              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20298           if (! BLOCK_ABSTRACT (stmt))
20299             gen_inlined_subroutine_die (stmt, context_die, depth);
20300         }
20301       else
20302         gen_lexical_block_die (stmt, context_die, depth);
20303     }
20304   else
20305     decls_for_scope (stmt, context_die, depth);
20306 }
20307
20308 /* Process variable DECL (or variable with origin ORIGIN) within
20309    block STMT and add it to CONTEXT_DIE.  */
20310 static void
20311 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20312 {
20313   dw_die_ref die;
20314   tree decl_or_origin = decl ? decl : origin;
20315
20316   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20317     die = lookup_decl_die (decl_or_origin);
20318   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20319            && TYPE_DECL_IS_STUB (decl_or_origin))
20320     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20321   else
20322     die = NULL;
20323
20324   if (die != NULL && die->die_parent == NULL)
20325     add_child_die (context_die, die);
20326   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20327     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20328                                          stmt, context_die);
20329   else
20330     gen_decl_die (decl, origin, context_die);
20331 }
20332
20333 /* Generate all of the decls declared within a given scope and (recursively)
20334    all of its sub-blocks.  */
20335
20336 static void
20337 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20338 {
20339   tree decl;
20340   unsigned int i;
20341   tree subblocks;
20342
20343   /* Ignore NULL blocks.  */
20344   if (stmt == NULL_TREE)
20345     return;
20346
20347   /* Output the DIEs to represent all of the data objects and typedefs
20348      declared directly within this block but not within any nested
20349      sub-blocks.  Also, nested function and tag DIEs have been
20350      generated with a parent of NULL; fix that up now.  */
20351   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20352     process_scope_var (stmt, decl, NULL_TREE, context_die);
20353   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20354     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20355                        context_die);
20356
20357   /* If we're at -g1, we're not interested in subblocks.  */
20358   if (debug_info_level <= DINFO_LEVEL_TERSE)
20359     return;
20360
20361   /* Output the DIEs to represent all sub-blocks (and the items declared
20362      therein) of this block.  */
20363   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20364        subblocks != NULL;
20365        subblocks = BLOCK_CHAIN (subblocks))
20366     gen_block_die (subblocks, context_die, depth + 1);
20367 }
20368
20369 /* Is this a typedef we can avoid emitting?  */
20370
20371 static inline int
20372 is_redundant_typedef (const_tree decl)
20373 {
20374   if (TYPE_DECL_IS_STUB (decl))
20375     return 1;
20376
20377   if (DECL_ARTIFICIAL (decl)
20378       && DECL_CONTEXT (decl)
20379       && is_tagged_type (DECL_CONTEXT (decl))
20380       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20381       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20382     /* Also ignore the artificial member typedef for the class name.  */
20383     return 1;
20384
20385   return 0;
20386 }
20387
20388 /* Return TRUE if TYPE is a typedef that names a type for linkage
20389    purposes. This kind of typedefs is produced by the C++ FE for
20390    constructs like:
20391
20392    typedef struct {...} foo;
20393
20394    In that case, there is no typedef variant type produced for foo.
20395    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20396    struct type.  */
20397
20398 static bool
20399 is_naming_typedef_decl (const_tree decl)
20400 {
20401   if (decl == NULL_TREE
20402       || TREE_CODE (decl) != TYPE_DECL
20403       || !is_tagged_type (TREE_TYPE (decl))
20404       || DECL_IS_BUILTIN (decl)
20405       || is_redundant_typedef (decl)
20406       /* It looks like Ada produces TYPE_DECLs that are very similar
20407          to C++ naming typedefs but that have different
20408          semantics. Let's be specific to c++ for now.  */
20409       || !is_cxx ())
20410     return FALSE;
20411
20412   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20413           && TYPE_NAME (TREE_TYPE (decl)) == decl
20414           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20415               != TYPE_NAME (TREE_TYPE (decl))));
20416 }
20417
20418 /* Returns the DIE for a context.  */
20419
20420 static inline dw_die_ref
20421 get_context_die (tree context)
20422 {
20423   if (context)
20424     {
20425       /* Find die that represents this context.  */
20426       if (TYPE_P (context))
20427         return force_type_die (TYPE_MAIN_VARIANT (context));
20428       else
20429         return force_decl_die (context);
20430     }
20431   return comp_unit_die ();
20432 }
20433
20434 /* Returns the DIE for decl.  A DIE will always be returned.  */
20435
20436 static dw_die_ref
20437 force_decl_die (tree decl)
20438 {
20439   dw_die_ref decl_die;
20440   unsigned saved_external_flag;
20441   tree save_fn = NULL_TREE;
20442   decl_die = lookup_decl_die (decl);
20443   if (!decl_die)
20444     {
20445       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20446
20447       decl_die = lookup_decl_die (decl);
20448       if (decl_die)
20449         return decl_die;
20450
20451       switch (TREE_CODE (decl))
20452         {
20453         case FUNCTION_DECL:
20454           /* Clear current_function_decl, so that gen_subprogram_die thinks
20455              that this is a declaration. At this point, we just want to force
20456              declaration die.  */
20457           save_fn = current_function_decl;
20458           current_function_decl = NULL_TREE;
20459           gen_subprogram_die (decl, context_die);
20460           current_function_decl = save_fn;
20461           break;
20462
20463         case VAR_DECL:
20464           /* Set external flag to force declaration die. Restore it after
20465            gen_decl_die() call.  */
20466           saved_external_flag = DECL_EXTERNAL (decl);
20467           DECL_EXTERNAL (decl) = 1;
20468           gen_decl_die (decl, NULL, context_die);
20469           DECL_EXTERNAL (decl) = saved_external_flag;
20470           break;
20471
20472         case NAMESPACE_DECL:
20473           if (dwarf_version >= 3 || !dwarf_strict)
20474             dwarf2out_decl (decl);
20475           else
20476             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20477             decl_die = comp_unit_die ();
20478           break;
20479
20480         case TRANSLATION_UNIT_DECL:
20481           decl_die = comp_unit_die ();
20482           break;
20483
20484         default:
20485           gcc_unreachable ();
20486         }
20487
20488       /* We should be able to find the DIE now.  */
20489       if (!decl_die)
20490         decl_die = lookup_decl_die (decl);
20491       gcc_assert (decl_die);
20492     }
20493
20494   return decl_die;
20495 }
20496
20497 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20498    always returned.  */
20499
20500 static dw_die_ref
20501 force_type_die (tree type)
20502 {
20503   dw_die_ref type_die;
20504
20505   type_die = lookup_type_die (type);
20506   if (!type_die)
20507     {
20508       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20509
20510       type_die = modified_type_die (type, TYPE_READONLY (type),
20511                                     TYPE_VOLATILE (type), context_die);
20512       gcc_assert (type_die);
20513     }
20514   return type_die;
20515 }
20516
20517 /* Force out any required namespaces to be able to output DECL,
20518    and return the new context_die for it, if it's changed.  */
20519
20520 static dw_die_ref
20521 setup_namespace_context (tree thing, dw_die_ref context_die)
20522 {
20523   tree context = (DECL_P (thing)
20524                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20525   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20526     /* Force out the namespace.  */
20527     context_die = force_decl_die (context);
20528
20529   return context_die;
20530 }
20531
20532 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20533    type) within its namespace, if appropriate.
20534
20535    For compatibility with older debuggers, namespace DIEs only contain
20536    declarations; all definitions are emitted at CU scope.  */
20537
20538 static dw_die_ref
20539 declare_in_namespace (tree thing, dw_die_ref context_die)
20540 {
20541   dw_die_ref ns_context;
20542
20543   if (debug_info_level <= DINFO_LEVEL_TERSE)
20544     return context_die;
20545
20546   /* If this decl is from an inlined function, then don't try to emit it in its
20547      namespace, as we will get confused.  It would have already been emitted
20548      when the abstract instance of the inline function was emitted anyways.  */
20549   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20550     return context_die;
20551
20552   ns_context = setup_namespace_context (thing, context_die);
20553
20554   if (ns_context != context_die)
20555     {
20556       if (is_fortran ())
20557         return ns_context;
20558       if (DECL_P (thing))
20559         gen_decl_die (thing, NULL, ns_context);
20560       else
20561         gen_type_die (thing, ns_context);
20562     }
20563   return context_die;
20564 }
20565
20566 /* Generate a DIE for a namespace or namespace alias.  */
20567
20568 static void
20569 gen_namespace_die (tree decl, dw_die_ref context_die)
20570 {
20571   dw_die_ref namespace_die;
20572
20573   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20574      they are an alias of.  */
20575   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20576     {
20577       /* Output a real namespace or module.  */
20578       context_die = setup_namespace_context (decl, comp_unit_die ());
20579       namespace_die = new_die (is_fortran ()
20580                                ? DW_TAG_module : DW_TAG_namespace,
20581                                context_die, decl);
20582       /* For Fortran modules defined in different CU don't add src coords.  */
20583       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20584         {
20585           const char *name = dwarf2_name (decl, 0);
20586           if (name)
20587             add_name_attribute (namespace_die, name);
20588         }
20589       else
20590         add_name_and_src_coords_attributes (namespace_die, decl);
20591       if (DECL_EXTERNAL (decl))
20592         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20593       equate_decl_number_to_die (decl, namespace_die);
20594     }
20595   else
20596     {
20597       /* Output a namespace alias.  */
20598
20599       /* Force out the namespace we are an alias of, if necessary.  */
20600       dw_die_ref origin_die
20601         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20602
20603       if (DECL_CONTEXT (decl) == NULL_TREE
20604           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20605         context_die = setup_namespace_context (decl, comp_unit_die ());
20606       /* Now create the namespace alias DIE.  */
20607       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20608       add_name_and_src_coords_attributes (namespace_die, decl);
20609       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20610       equate_decl_number_to_die (decl, namespace_die);
20611     }
20612 }
20613
20614 /* Generate Dwarf debug information for a decl described by DECL.
20615    The return value is currently only meaningful for PARM_DECLs,
20616    for all other decls it returns NULL.  */
20617
20618 static dw_die_ref
20619 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20620 {
20621   tree decl_or_origin = decl ? decl : origin;
20622   tree class_origin = NULL, ultimate_origin;
20623
20624   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20625     return NULL;
20626
20627   switch (TREE_CODE (decl_or_origin))
20628     {
20629     case ERROR_MARK:
20630       break;
20631
20632     case CONST_DECL:
20633       if (!is_fortran () && !is_ada ())
20634         {
20635           /* The individual enumerators of an enum type get output when we output
20636              the Dwarf representation of the relevant enum type itself.  */
20637           break;
20638         }
20639
20640       /* Emit its type.  */
20641       gen_type_die (TREE_TYPE (decl), context_die);
20642
20643       /* And its containing namespace.  */
20644       context_die = declare_in_namespace (decl, context_die);
20645
20646       gen_const_die (decl, context_die);
20647       break;
20648
20649     case FUNCTION_DECL:
20650       /* Don't output any DIEs to represent mere function declarations,
20651          unless they are class members or explicit block externs.  */
20652       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20653           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20654           && (current_function_decl == NULL_TREE
20655               || DECL_ARTIFICIAL (decl_or_origin)))
20656         break;
20657
20658 #if 0
20659       /* FIXME */
20660       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20661          on local redeclarations of global functions.  That seems broken.  */
20662       if (current_function_decl != decl)
20663         /* This is only a declaration.  */;
20664 #endif
20665
20666       /* If we're emitting a clone, emit info for the abstract instance.  */
20667       if (origin || DECL_ORIGIN (decl) != decl)
20668         dwarf2out_abstract_function (origin
20669                                      ? DECL_ORIGIN (origin)
20670                                      : DECL_ABSTRACT_ORIGIN (decl));
20671
20672       /* If we're emitting an out-of-line copy of an inline function,
20673          emit info for the abstract instance and set up to refer to it.  */
20674       else if (cgraph_function_possibly_inlined_p (decl)
20675                && ! DECL_ABSTRACT (decl)
20676                && ! class_or_namespace_scope_p (context_die)
20677                /* dwarf2out_abstract_function won't emit a die if this is just
20678                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20679                   that case, because that works only if we have a die.  */
20680                && DECL_INITIAL (decl) != NULL_TREE)
20681         {
20682           dwarf2out_abstract_function (decl);
20683           set_decl_origin_self (decl);
20684         }
20685
20686       /* Otherwise we're emitting the primary DIE for this decl.  */
20687       else if (debug_info_level > DINFO_LEVEL_TERSE)
20688         {
20689           /* Before we describe the FUNCTION_DECL itself, make sure that we
20690              have its containing type.  */
20691           if (!origin)
20692             origin = decl_class_context (decl);
20693           if (origin != NULL_TREE)
20694             gen_type_die (origin, context_die);
20695
20696           /* And its return type.  */
20697           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20698
20699           /* And its virtual context.  */
20700           if (DECL_VINDEX (decl) != NULL_TREE)
20701             gen_type_die (DECL_CONTEXT (decl), context_die);
20702
20703           /* Make sure we have a member DIE for decl.  */
20704           if (origin != NULL_TREE)
20705             gen_type_die_for_member (origin, decl, context_die);
20706
20707           /* And its containing namespace.  */
20708           context_die = declare_in_namespace (decl, context_die);
20709         }
20710
20711       /* Now output a DIE to represent the function itself.  */
20712       if (decl)
20713         gen_subprogram_die (decl, context_die);
20714       break;
20715
20716     case TYPE_DECL:
20717       /* If we are in terse mode, don't generate any DIEs to represent any
20718          actual typedefs.  */
20719       if (debug_info_level <= DINFO_LEVEL_TERSE)
20720         break;
20721
20722       /* In the special case of a TYPE_DECL node representing the declaration
20723          of some type tag, if the given TYPE_DECL is marked as having been
20724          instantiated from some other (original) TYPE_DECL node (e.g. one which
20725          was generated within the original definition of an inline function) we
20726          used to generate a special (abbreviated) DW_TAG_structure_type,
20727          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20728          should be actually referencing those DIEs, as variable DIEs with that
20729          type would be emitted already in the abstract origin, so it was always
20730          removed during unused type prunning.  Don't add anything in this
20731          case.  */
20732       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20733         break;
20734
20735       if (is_redundant_typedef (decl))
20736         gen_type_die (TREE_TYPE (decl), context_die);
20737       else
20738         /* Output a DIE to represent the typedef itself.  */
20739         gen_typedef_die (decl, context_die);
20740       break;
20741
20742     case LABEL_DECL:
20743       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20744         gen_label_die (decl, context_die);
20745       break;
20746
20747     case VAR_DECL:
20748     case RESULT_DECL:
20749       /* If we are in terse mode, don't generate any DIEs to represent any
20750          variable declarations or definitions.  */
20751       if (debug_info_level <= DINFO_LEVEL_TERSE)
20752         break;
20753
20754       /* Output any DIEs that are needed to specify the type of this data
20755          object.  */
20756       if (decl_by_reference_p (decl_or_origin))
20757         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20758       else
20759         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20760
20761       /* And its containing type.  */
20762       class_origin = decl_class_context (decl_or_origin);
20763       if (class_origin != NULL_TREE)
20764         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20765
20766       /* And its containing namespace.  */
20767       context_die = declare_in_namespace (decl_or_origin, context_die);
20768
20769       /* Now output the DIE to represent the data object itself.  This gets
20770          complicated because of the possibility that the VAR_DECL really
20771          represents an inlined instance of a formal parameter for an inline
20772          function.  */
20773       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20774       if (ultimate_origin != NULL_TREE
20775           && TREE_CODE (ultimate_origin) == PARM_DECL)
20776         gen_formal_parameter_die (decl, origin,
20777                                   true /* Emit name attribute.  */,
20778                                   context_die);
20779       else
20780         gen_variable_die (decl, origin, context_die);
20781       break;
20782
20783     case FIELD_DECL:
20784       /* Ignore the nameless fields that are used to skip bits but handle C++
20785          anonymous unions and structs.  */
20786       if (DECL_NAME (decl) != NULL_TREE
20787           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20788           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20789         {
20790           gen_type_die (member_declared_type (decl), context_die);
20791           gen_field_die (decl, context_die);
20792         }
20793       break;
20794
20795     case PARM_DECL:
20796       if (DECL_BY_REFERENCE (decl_or_origin))
20797         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20798       else
20799         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20800       return gen_formal_parameter_die (decl, origin,
20801                                        true /* Emit name attribute.  */,
20802                                        context_die);
20803
20804     case NAMESPACE_DECL:
20805     case IMPORTED_DECL:
20806       if (dwarf_version >= 3 || !dwarf_strict)
20807         gen_namespace_die (decl, context_die);
20808       break;
20809
20810     default:
20811       /* Probably some frontend-internal decl.  Assume we don't care.  */
20812       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20813       break;
20814     }
20815
20816   return NULL;
20817 }
20818 \f
20819 /* Output debug information for global decl DECL.  Called from toplev.c after
20820    compilation proper has finished.  */
20821
20822 static void
20823 dwarf2out_global_decl (tree decl)
20824 {
20825   /* Output DWARF2 information for file-scope tentative data object
20826      declarations, file-scope (extern) function declarations (which
20827      had no corresponding body) and file-scope tagged type declarations
20828      and definitions which have not yet been forced out.  */
20829   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20830     dwarf2out_decl (decl);
20831 }
20832
20833 /* Output debug information for type decl DECL.  Called from toplev.c
20834    and from language front ends (to record built-in types).  */
20835 static void
20836 dwarf2out_type_decl (tree decl, int local)
20837 {
20838   if (!local)
20839     dwarf2out_decl (decl);
20840 }
20841
20842 /* Output debug information for imported module or decl DECL.
20843    NAME is non-NULL name in the lexical block if the decl has been renamed.
20844    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20845    that DECL belongs to.
20846    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20847 static void
20848 dwarf2out_imported_module_or_decl_1 (tree decl,
20849                                      tree name,
20850                                      tree lexical_block,
20851                                      dw_die_ref lexical_block_die)
20852 {
20853   expanded_location xloc;
20854   dw_die_ref imported_die = NULL;
20855   dw_die_ref at_import_die;
20856
20857   if (TREE_CODE (decl) == IMPORTED_DECL)
20858     {
20859       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20860       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20861       gcc_assert (decl);
20862     }
20863   else
20864     xloc = expand_location (input_location);
20865
20866   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20867     {
20868       at_import_die = force_type_die (TREE_TYPE (decl));
20869       /* For namespace N { typedef void T; } using N::T; base_type_die
20870          returns NULL, but DW_TAG_imported_declaration requires
20871          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20872       if (!at_import_die)
20873         {
20874           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20875           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20876           at_import_die = lookup_type_die (TREE_TYPE (decl));
20877           gcc_assert (at_import_die);
20878         }
20879     }
20880   else
20881     {
20882       at_import_die = lookup_decl_die (decl);
20883       if (!at_import_die)
20884         {
20885           /* If we're trying to avoid duplicate debug info, we may not have
20886              emitted the member decl for this field.  Emit it now.  */
20887           if (TREE_CODE (decl) == FIELD_DECL)
20888             {
20889               tree type = DECL_CONTEXT (decl);
20890
20891               if (TYPE_CONTEXT (type)
20892                   && TYPE_P (TYPE_CONTEXT (type))
20893                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20894                                                 DINFO_USAGE_DIR_USE))
20895                 return;
20896               gen_type_die_for_member (type, decl,
20897                                        get_context_die (TYPE_CONTEXT (type)));
20898             }
20899           at_import_die = force_decl_die (decl);
20900         }
20901     }
20902
20903   if (TREE_CODE (decl) == NAMESPACE_DECL)
20904     {
20905       if (dwarf_version >= 3 || !dwarf_strict)
20906         imported_die = new_die (DW_TAG_imported_module,
20907                                 lexical_block_die,
20908                                 lexical_block);
20909       else
20910         return;
20911     }
20912   else
20913     imported_die = new_die (DW_TAG_imported_declaration,
20914                             lexical_block_die,
20915                             lexical_block);
20916
20917   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20918   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20919   if (name)
20920     add_AT_string (imported_die, DW_AT_name,
20921                    IDENTIFIER_POINTER (name));
20922   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20923 }
20924
20925 /* Output debug information for imported module or decl DECL.
20926    NAME is non-NULL name in context if the decl has been renamed.
20927    CHILD is true if decl is one of the renamed decls as part of
20928    importing whole module.  */
20929
20930 static void
20931 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20932                                    bool child)
20933 {
20934   /* dw_die_ref at_import_die;  */
20935   dw_die_ref scope_die;
20936
20937   if (debug_info_level <= DINFO_LEVEL_TERSE)
20938     return;
20939
20940   gcc_assert (decl);
20941
20942   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20943      We need decl DIE for reference and scope die. First, get DIE for the decl
20944      itself.  */
20945
20946   /* Get the scope die for decl context. Use comp_unit_die for global module
20947      or decl. If die is not found for non globals, force new die.  */
20948   if (context
20949       && TYPE_P (context)
20950       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20951     return;
20952
20953   if (!(dwarf_version >= 3 || !dwarf_strict))
20954     return;
20955
20956   scope_die = get_context_die (context);
20957
20958   if (child)
20959     {
20960       gcc_assert (scope_die->die_child);
20961       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20962       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20963       scope_die = scope_die->die_child;
20964     }
20965
20966   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20967   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20968
20969 }
20970
20971 /* Write the debugging output for DECL.  */
20972
20973 void
20974 dwarf2out_decl (tree decl)
20975 {
20976   dw_die_ref context_die = comp_unit_die ();
20977
20978   switch (TREE_CODE (decl))
20979     {
20980     case ERROR_MARK:
20981       return;
20982
20983     case FUNCTION_DECL:
20984       /* What we would really like to do here is to filter out all mere
20985          file-scope declarations of file-scope functions which are never
20986          referenced later within this translation unit (and keep all of ones
20987          that *are* referenced later on) but we aren't clairvoyant, so we have
20988          no idea which functions will be referenced in the future (i.e. later
20989          on within the current translation unit). So here we just ignore all
20990          file-scope function declarations which are not also definitions.  If
20991          and when the debugger needs to know something about these functions,
20992          it will have to hunt around and find the DWARF information associated
20993          with the definition of the function.
20994
20995          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20996          nodes represent definitions and which ones represent mere
20997          declarations.  We have to check DECL_INITIAL instead. That's because
20998          the C front-end supports some weird semantics for "extern inline"
20999          function definitions.  These can get inlined within the current
21000          translation unit (and thus, we need to generate Dwarf info for their
21001          abstract instances so that the Dwarf info for the concrete inlined
21002          instances can have something to refer to) but the compiler never
21003          generates any out-of-lines instances of such things (despite the fact
21004          that they *are* definitions).
21005
21006          The important point is that the C front-end marks these "extern
21007          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21008          them anyway. Note that the C++ front-end also plays some similar games
21009          for inline function definitions appearing within include files which
21010          also contain `#pragma interface' pragmas.  */
21011       if (DECL_INITIAL (decl) == NULL_TREE)
21012         return;
21013
21014       /* If we're a nested function, initially use a parent of NULL; if we're
21015          a plain function, this will be fixed up in decls_for_scope.  If
21016          we're a method, it will be ignored, since we already have a DIE.  */
21017       if (decl_function_context (decl)
21018           /* But if we're in terse mode, we don't care about scope.  */
21019           && debug_info_level > DINFO_LEVEL_TERSE)
21020         context_die = NULL;
21021       break;
21022
21023     case VAR_DECL:
21024       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21025          declaration and if the declaration was never even referenced from
21026          within this entire compilation unit.  We suppress these DIEs in
21027          order to save space in the .debug section (by eliminating entries
21028          which are probably useless).  Note that we must not suppress
21029          block-local extern declarations (whether used or not) because that
21030          would screw-up the debugger's name lookup mechanism and cause it to
21031          miss things which really ought to be in scope at a given point.  */
21032       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21033         return;
21034
21035       /* For local statics lookup proper context die.  */
21036       if (TREE_STATIC (decl) && decl_function_context (decl))
21037         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21038
21039       /* If we are in terse mode, don't generate any DIEs to represent any
21040          variable declarations or definitions.  */
21041       if (debug_info_level <= DINFO_LEVEL_TERSE)
21042         return;
21043       break;
21044
21045     case CONST_DECL:
21046       if (debug_info_level <= DINFO_LEVEL_TERSE)
21047         return;
21048       if (!is_fortran () && !is_ada ())
21049         return;
21050       if (TREE_STATIC (decl) && decl_function_context (decl))
21051         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21052       break;
21053
21054     case NAMESPACE_DECL:
21055     case IMPORTED_DECL:
21056       if (debug_info_level <= DINFO_LEVEL_TERSE)
21057         return;
21058       if (lookup_decl_die (decl) != NULL)
21059         return;
21060       break;
21061
21062     case TYPE_DECL:
21063       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21064       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21065         return;
21066
21067       /* Don't bother trying to generate any DIEs to represent any of the
21068          normal built-in types for the language we are compiling.  */
21069       if (DECL_IS_BUILTIN (decl))
21070         return;
21071
21072       /* If we are in terse mode, don't generate any DIEs for types.  */
21073       if (debug_info_level <= DINFO_LEVEL_TERSE)
21074         return;
21075
21076       /* If we're a function-scope tag, initially use a parent of NULL;
21077          this will be fixed up in decls_for_scope.  */
21078       if (decl_function_context (decl))
21079         context_die = NULL;
21080
21081       break;
21082
21083     default:
21084       return;
21085     }
21086
21087   gen_decl_die (decl, NULL, context_die);
21088 }
21089
21090 /* Write the debugging output for DECL.  */
21091
21092 static void
21093 dwarf2out_function_decl (tree decl)
21094 {
21095   dwarf2out_decl (decl);
21096
21097   htab_empty (decl_loc_table);
21098 }
21099
21100 /* Output a marker (i.e. a label) for the beginning of the generated code for
21101    a lexical block.  */
21102
21103 static void
21104 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21105                        unsigned int blocknum)
21106 {
21107   switch_to_section (current_function_section ());
21108   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21109 }
21110
21111 /* Output a marker (i.e. a label) for the end of the generated code for a
21112    lexical block.  */
21113
21114 static void
21115 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21116 {
21117   switch_to_section (current_function_section ());
21118   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21119 }
21120
21121 /* Returns nonzero if it is appropriate not to emit any debugging
21122    information for BLOCK, because it doesn't contain any instructions.
21123
21124    Don't allow this for blocks with nested functions or local classes
21125    as we would end up with orphans, and in the presence of scheduling
21126    we may end up calling them anyway.  */
21127
21128 static bool
21129 dwarf2out_ignore_block (const_tree block)
21130 {
21131   tree decl;
21132   unsigned int i;
21133
21134   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21135     if (TREE_CODE (decl) == FUNCTION_DECL
21136         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21137       return 0;
21138   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21139     {
21140       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21141       if (TREE_CODE (decl) == FUNCTION_DECL
21142           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21143       return 0;
21144     }
21145
21146   return 1;
21147 }
21148
21149 /* Hash table routines for file_hash.  */
21150
21151 static int
21152 file_table_eq (const void *p1_p, const void *p2_p)
21153 {
21154   const struct dwarf_file_data *const p1 =
21155     (const struct dwarf_file_data *) p1_p;
21156   const char *const p2 = (const char *) p2_p;
21157   return strcmp (p1->filename, p2) == 0;
21158 }
21159
21160 static hashval_t
21161 file_table_hash (const void *p_p)
21162 {
21163   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21164   return htab_hash_string (p->filename);
21165 }
21166
21167 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21168    dwarf2out.c) and return its "index".  The index of each (known) filename is
21169    just a unique number which is associated with only that one filename.  We
21170    need such numbers for the sake of generating labels (in the .debug_sfnames
21171    section) and references to those files numbers (in the .debug_srcinfo
21172    and.debug_macinfo sections).  If the filename given as an argument is not
21173    found in our current list, add it to the list and assign it the next
21174    available unique index number.  In order to speed up searches, we remember
21175    the index of the filename was looked up last.  This handles the majority of
21176    all searches.  */
21177
21178 static struct dwarf_file_data *
21179 lookup_filename (const char *file_name)
21180 {
21181   void ** slot;
21182   struct dwarf_file_data * created;
21183
21184   /* Check to see if the file name that was searched on the previous
21185      call matches this file name.  If so, return the index.  */
21186   if (file_table_last_lookup
21187       && (file_name == file_table_last_lookup->filename
21188           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21189     return file_table_last_lookup;
21190
21191   /* Didn't match the previous lookup, search the table.  */
21192   slot = htab_find_slot_with_hash (file_table, file_name,
21193                                    htab_hash_string (file_name), INSERT);
21194   if (*slot)
21195     return (struct dwarf_file_data *) *slot;
21196
21197   created = ggc_alloc_dwarf_file_data ();
21198   created->filename = file_name;
21199   created->emitted_number = 0;
21200   *slot = created;
21201   return created;
21202 }
21203
21204 /* If the assembler will construct the file table, then translate the compiler
21205    internal file table number into the assembler file table number, and emit
21206    a .file directive if we haven't already emitted one yet.  The file table
21207    numbers are different because we prune debug info for unused variables and
21208    types, which may include filenames.  */
21209
21210 static int
21211 maybe_emit_file (struct dwarf_file_data * fd)
21212 {
21213   if (! fd->emitted_number)
21214     {
21215       if (last_emitted_file)
21216         fd->emitted_number = last_emitted_file->emitted_number + 1;
21217       else
21218         fd->emitted_number = 1;
21219       last_emitted_file = fd;
21220
21221       if (DWARF2_ASM_LINE_DEBUG_INFO)
21222         {
21223           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21224           output_quoted_string (asm_out_file,
21225                                 remap_debug_filename (fd->filename));
21226           fputc ('\n', asm_out_file);
21227         }
21228     }
21229
21230   return fd->emitted_number;
21231 }
21232
21233 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21234    That generation should happen after function debug info has been
21235    generated. The value of the attribute is the constant value of ARG.  */
21236
21237 static void
21238 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21239 {
21240   die_arg_entry entry;
21241
21242   if (!die || !arg)
21243     return;
21244
21245   if (!tmpl_value_parm_die_table)
21246     tmpl_value_parm_die_table
21247       = VEC_alloc (die_arg_entry, gc, 32);
21248
21249   entry.die = die;
21250   entry.arg = arg;
21251   VEC_safe_push (die_arg_entry, gc,
21252                  tmpl_value_parm_die_table,
21253                  &entry);
21254 }
21255
21256 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21257    by append_entry_to_tmpl_value_parm_die_table. This function must
21258    be called after function DIEs have been generated.  */
21259
21260 static void
21261 gen_remaining_tmpl_value_param_die_attribute (void)
21262 {
21263   if (tmpl_value_parm_die_table)
21264     {
21265       unsigned i;
21266       die_arg_entry *e;
21267
21268       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21269         tree_add_const_value_attribute (e->die, e->arg);
21270     }
21271 }
21272
21273
21274 /* Replace DW_AT_name for the decl with name.  */
21275
21276 static void
21277 dwarf2out_set_name (tree decl, tree name)
21278 {
21279   dw_die_ref die;
21280   dw_attr_ref attr;
21281   const char *dname;
21282
21283   die = TYPE_SYMTAB_DIE (decl);
21284   if (!die)
21285     return;
21286
21287   dname = dwarf2_name (name, 0);
21288   if (!dname)
21289     return;
21290
21291   attr = get_AT (die, DW_AT_name);
21292   if (attr)
21293     {
21294       struct indirect_string_node *node;
21295
21296       node = find_AT_string (dname);
21297       /* replace the string.  */
21298       attr->dw_attr_val.v.val_str = node;
21299     }
21300
21301   else
21302     add_name_attribute (die, dname);
21303 }
21304
21305 /* Called by the final INSN scan whenever we see a direct function call.
21306    Make an entry into the direct call table, recording the point of call
21307    and a reference to the target function's debug entry.  */
21308
21309 static void
21310 dwarf2out_direct_call (tree targ)
21311 {
21312   dcall_entry e;
21313   tree origin = decl_ultimate_origin (targ);
21314
21315   /* If this is a clone, use the abstract origin as the target.  */
21316   if (origin)
21317     targ = origin;
21318
21319   e.poc_label_num = poc_label_num++;
21320   e.poc_decl = current_function_decl;
21321   e.targ_die = force_decl_die (targ);
21322   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21323
21324   /* Drop a label at the return point to mark the point of call.  */
21325   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21326 }
21327
21328 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21329
21330 static hashval_t
21331 vcall_insn_table_hash (const void *x)
21332 {
21333   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21334 }
21335
21336 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21337    insnd_uid of *Y.  */
21338
21339 static int
21340 vcall_insn_table_eq (const void *x, const void *y)
21341 {
21342   return (((const struct vcall_insn *) x)->insn_uid
21343           == ((const struct vcall_insn *) y)->insn_uid);
21344 }
21345
21346 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21347
21348 static void
21349 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21350 {
21351   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21352   struct vcall_insn **slot;
21353
21354   gcc_assert (item);
21355   item->insn_uid = insn_uid;
21356   item->vtable_slot = vtable_slot;
21357   slot = (struct vcall_insn **)
21358       htab_find_slot_with_hash (vcall_insn_table, &item,
21359                                 (hashval_t) insn_uid, INSERT);
21360   *slot = item;
21361 }
21362
21363 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21364
21365 static unsigned int
21366 lookup_vcall_insn (unsigned int insn_uid)
21367 {
21368   struct vcall_insn item;
21369   struct vcall_insn *p;
21370
21371   item.insn_uid = insn_uid;
21372   item.vtable_slot = 0;
21373   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21374                                                  (void *) &item,
21375                                                  (hashval_t) insn_uid);
21376   if (p == NULL)
21377     return (unsigned int) -1;
21378   return p->vtable_slot;
21379 }
21380
21381
21382 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21383    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21384    is the vtable slot index that we will need to put in the virtual call
21385    table later.  */
21386
21387 static void
21388 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21389 {
21390   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21391     {
21392       tree token = OBJ_TYPE_REF_TOKEN (addr);
21393       if (TREE_CODE (token) == INTEGER_CST)
21394         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21395     }
21396 }
21397
21398 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21399    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21400    with NEW_INSN.  */
21401
21402 static void
21403 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21404 {
21405   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21406
21407   if (vtable_slot != (unsigned int) -1)
21408     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21409 }
21410
21411 /* Called by the final INSN scan whenever we see a virtual function call.
21412    Make an entry into the virtual call table, recording the point of call
21413    and the slot index of the vtable entry used to call the virtual member
21414    function.  The slot index was associated with the INSN_UID during the
21415    lowering to RTL.  */
21416
21417 static void
21418 dwarf2out_virtual_call (int insn_uid)
21419 {
21420   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21421   vcall_entry e;
21422
21423   if (vtable_slot == (unsigned int) -1)
21424     return;
21425
21426   e.poc_label_num = poc_label_num++;
21427   e.vtable_slot = vtable_slot;
21428   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21429
21430   /* Drop a label at the return point to mark the point of call.  */
21431   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21432 }
21433
21434 /* Called by the final INSN scan whenever we see a var location.  We
21435    use it to drop labels in the right places, and throw the location in
21436    our lookup table.  */
21437
21438 static void
21439 dwarf2out_var_location (rtx loc_note)
21440 {
21441   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21442   struct var_loc_node *newloc;
21443   rtx next_real;
21444   static const char *last_label;
21445   static const char *last_postcall_label;
21446   static bool last_in_cold_section_p;
21447   tree decl;
21448
21449   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21450     return;
21451
21452   next_real = next_real_insn (loc_note);
21453   /* If there are no instructions which would be affected by this note,
21454      don't do anything.  */
21455   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21456     return;
21457
21458   /* If there were any real insns between note we processed last time
21459      and this note (or if it is the first note), clear
21460      last_{,postcall_}label so that they are not reused this time.  */
21461   if (last_var_location_insn == NULL_RTX
21462       || last_var_location_insn != next_real
21463       || last_in_cold_section_p != in_cold_section_p)
21464     {
21465       last_label = NULL;
21466       last_postcall_label = NULL;
21467     }
21468
21469   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21470   newloc = add_var_loc_to_decl (decl, loc_note,
21471                                 NOTE_DURING_CALL_P (loc_note)
21472                                 ? last_postcall_label : last_label);
21473   if (newloc == NULL)
21474     return;
21475
21476   /* If there were no real insns between note we processed last time
21477      and this note, use the label we emitted last time.  Otherwise
21478      create a new label and emit it.  */
21479   if (last_label == NULL)
21480     {
21481       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21482       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21483       loclabel_num++;
21484       last_label = ggc_strdup (loclabel);
21485     }
21486
21487   if (!NOTE_DURING_CALL_P (loc_note))
21488     newloc->label = last_label;
21489   else
21490     {
21491       if (!last_postcall_label)
21492         {
21493           sprintf (loclabel, "%s-1", last_label);
21494           last_postcall_label = ggc_strdup (loclabel);
21495         }
21496       newloc->label = last_postcall_label;
21497     }
21498
21499   last_var_location_insn = next_real;
21500   last_in_cold_section_p = in_cold_section_p;
21501 }
21502
21503 /* We need to reset the locations at the beginning of each
21504    function. We can't do this in the end_function hook, because the
21505    declarations that use the locations won't have been output when
21506    that hook is called.  Also compute have_multiple_function_sections here.  */
21507
21508 static void
21509 dwarf2out_begin_function (tree fun)
21510 {
21511   if (function_section (fun) != text_section)
21512     have_multiple_function_sections = true;
21513
21514   dwarf2out_note_section_used ();
21515 }
21516
21517 /* Output a label to mark the beginning of a source code line entry
21518    and record information relating to this source line, in
21519    'line_info_table' for later output of the .debug_line section.  */
21520
21521 static void
21522 dwarf2out_source_line (unsigned int line, const char *filename,
21523                        int discriminator, bool is_stmt)
21524 {
21525   static bool last_is_stmt = true;
21526
21527   if (debug_info_level >= DINFO_LEVEL_NORMAL
21528       && line != 0)
21529     {
21530       int file_num = maybe_emit_file (lookup_filename (filename));
21531
21532       switch_to_section (current_function_section ());
21533
21534       /* If requested, emit something human-readable.  */
21535       if (flag_debug_asm)
21536         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21537                  filename, line);
21538
21539       if (DWARF2_ASM_LINE_DEBUG_INFO)
21540         {
21541           /* Emit the .loc directive understood by GNU as.  */
21542           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21543           if (is_stmt != last_is_stmt)
21544             {
21545               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21546               last_is_stmt = is_stmt;
21547             }
21548           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21549             fprintf (asm_out_file, " discriminator %d", discriminator);
21550           fputc ('\n', asm_out_file);
21551
21552           /* Indicate that line number info exists.  */
21553           line_info_table_in_use++;
21554         }
21555       else if (function_section (current_function_decl) != text_section)
21556         {
21557           dw_separate_line_info_ref line_info;
21558           targetm.asm_out.internal_label (asm_out_file,
21559                                           SEPARATE_LINE_CODE_LABEL,
21560                                           separate_line_info_table_in_use);
21561
21562           /* Expand the line info table if necessary.  */
21563           if (separate_line_info_table_in_use
21564               == separate_line_info_table_allocated)
21565             {
21566               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21567               separate_line_info_table
21568                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21569                                  separate_line_info_table,
21570                                  separate_line_info_table_allocated);
21571               memset (separate_line_info_table
21572                        + separate_line_info_table_in_use,
21573                       0,
21574                       (LINE_INFO_TABLE_INCREMENT
21575                        * sizeof (dw_separate_line_info_entry)));
21576             }
21577
21578           /* Add the new entry at the end of the line_info_table.  */
21579           line_info
21580             = &separate_line_info_table[separate_line_info_table_in_use++];
21581           line_info->dw_file_num = file_num;
21582           line_info->dw_line_num = line;
21583           line_info->function = current_function_funcdef_no;
21584         }
21585       else
21586         {
21587           dw_line_info_ref line_info;
21588
21589           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21590                                      line_info_table_in_use);
21591
21592           /* Expand the line info table if necessary.  */
21593           if (line_info_table_in_use == line_info_table_allocated)
21594             {
21595               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21596               line_info_table
21597                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21598                                  line_info_table_allocated);
21599               memset (line_info_table + line_info_table_in_use, 0,
21600                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21601             }
21602
21603           /* Add the new entry at the end of the line_info_table.  */
21604           line_info = &line_info_table[line_info_table_in_use++];
21605           line_info->dw_file_num = file_num;
21606           line_info->dw_line_num = line;
21607         }
21608     }
21609 }
21610
21611 /* Record the beginning of a new source file.  */
21612
21613 static void
21614 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21615 {
21616   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21617     {
21618       /* Record the beginning of the file for break_out_includes.  */
21619       dw_die_ref bincl_die;
21620
21621       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21622       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21623     }
21624
21625   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21626     {
21627       int file_num = maybe_emit_file (lookup_filename (filename));
21628
21629       switch_to_section (debug_macinfo_section);
21630       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21631       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21632                                    lineno);
21633
21634       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21635     }
21636 }
21637
21638 /* Record the end of a source file.  */
21639
21640 static void
21641 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21642 {
21643   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21644     /* Record the end of the file for break_out_includes.  */
21645     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21646
21647   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21648     {
21649       switch_to_section (debug_macinfo_section);
21650       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21651     }
21652 }
21653
21654 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21655    the tail part of the directive line, i.e. the part which is past the
21656    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21657
21658 static void
21659 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21660                   const char *buffer ATTRIBUTE_UNUSED)
21661 {
21662   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21663     {
21664       switch_to_section (debug_macinfo_section);
21665       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21666       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21667       dw2_asm_output_nstring (buffer, -1, "The macro");
21668     }
21669 }
21670
21671 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21672    the tail part of the directive line, i.e. the part which is past the
21673    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21674
21675 static void
21676 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21677                  const char *buffer ATTRIBUTE_UNUSED)
21678 {
21679   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21680     {
21681       switch_to_section (debug_macinfo_section);
21682       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21683       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21684       dw2_asm_output_nstring (buffer, -1, "The macro");
21685     }
21686 }
21687
21688 /* Set up for Dwarf output at the start of compilation.  */
21689
21690 static void
21691 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21692 {
21693   /* Allocate the file_table.  */
21694   file_table = htab_create_ggc (50, file_table_hash,
21695                                 file_table_eq, NULL);
21696
21697   /* Allocate the decl_die_table.  */
21698   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21699                                     decl_die_table_eq, NULL);
21700
21701   /* Allocate the decl_loc_table.  */
21702   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21703                                     decl_loc_table_eq, NULL);
21704
21705   /* Allocate the initial hunk of the decl_scope_table.  */
21706   decl_scope_table = VEC_alloc (tree, gc, 256);
21707
21708   /* Allocate the initial hunk of the abbrev_die_table.  */
21709   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21710     (ABBREV_DIE_TABLE_INCREMENT);
21711   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21712   /* Zero-th entry is allocated, but unused.  */
21713   abbrev_die_table_in_use = 1;
21714
21715   /* Allocate the initial hunk of the line_info_table.  */
21716   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21717     (LINE_INFO_TABLE_INCREMENT);
21718   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21719
21720   /* Zero-th entry is allocated, but unused.  */
21721   line_info_table_in_use = 1;
21722
21723   /* Allocate the pubtypes and pubnames vectors.  */
21724   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21725   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21726
21727   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21728   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21729                                       vcall_insn_table_eq, NULL);
21730
21731   incomplete_types = VEC_alloc (tree, gc, 64);
21732
21733   used_rtx_array = VEC_alloc (rtx, gc, 32);
21734
21735   debug_info_section = get_section (DEBUG_INFO_SECTION,
21736                                     SECTION_DEBUG, NULL);
21737   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21738                                       SECTION_DEBUG, NULL);
21739   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21740                                        SECTION_DEBUG, NULL);
21741   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21742                                        SECTION_DEBUG, NULL);
21743   debug_line_section = get_section (DEBUG_LINE_SECTION,
21744                                     SECTION_DEBUG, NULL);
21745   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21746                                    SECTION_DEBUG, NULL);
21747   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21748                                         SECTION_DEBUG, NULL);
21749   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21750                                         SECTION_DEBUG, NULL);
21751   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21752                                      SECTION_DEBUG, NULL);
21753   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21754                                      SECTION_DEBUG, NULL);
21755   debug_str_section = get_section (DEBUG_STR_SECTION,
21756                                    DEBUG_STR_SECTION_FLAGS, NULL);
21757   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21758                                       SECTION_DEBUG, NULL);
21759   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21760                                      SECTION_DEBUG, NULL);
21761
21762   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21763   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21764                                DEBUG_ABBREV_SECTION_LABEL, 0);
21765   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21766   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21767                                COLD_TEXT_SECTION_LABEL, 0);
21768   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21769
21770   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21771                                DEBUG_INFO_SECTION_LABEL, 0);
21772   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21773                                DEBUG_LINE_SECTION_LABEL, 0);
21774   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21775                                DEBUG_RANGES_SECTION_LABEL, 0);
21776   switch_to_section (debug_abbrev_section);
21777   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21778   switch_to_section (debug_info_section);
21779   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21780   switch_to_section (debug_line_section);
21781   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21782
21783   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21784     {
21785       switch_to_section (debug_macinfo_section);
21786       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21787                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21788       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21789     }
21790
21791   switch_to_section (text_section);
21792   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21793   if (flag_reorder_blocks_and_partition)
21794     {
21795       cold_text_section = unlikely_text_section ();
21796       switch_to_section (cold_text_section);
21797       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21798     }
21799
21800 }
21801
21802 /* Called before cgraph_optimize starts outputtting functions, variables
21803    and toplevel asms into assembly.  */
21804
21805 static void
21806 dwarf2out_assembly_start (void)
21807 {
21808   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21809     {
21810 #ifndef TARGET_UNWIND_INFO
21811       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21812 #endif
21813         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21814     }
21815 }
21816
21817 /* A helper function for dwarf2out_finish called through
21818    htab_traverse.  Emit one queued .debug_str string.  */
21819
21820 static int
21821 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21822 {
21823   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21824
21825   if (node->label && node->refcount)
21826     {
21827       switch_to_section (debug_str_section);
21828       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21829       assemble_string (node->str, strlen (node->str) + 1);
21830     }
21831
21832   return 1;
21833 }
21834
21835 #if ENABLE_ASSERT_CHECKING
21836 /* Verify that all marks are clear.  */
21837
21838 static void
21839 verify_marks_clear (dw_die_ref die)
21840 {
21841   dw_die_ref c;
21842
21843   gcc_assert (! die->die_mark);
21844   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21845 }
21846 #endif /* ENABLE_ASSERT_CHECKING */
21847
21848 /* Clear the marks for a die and its children.
21849    Be cool if the mark isn't set.  */
21850
21851 static void
21852 prune_unmark_dies (dw_die_ref die)
21853 {
21854   dw_die_ref c;
21855
21856   if (die->die_mark)
21857     die->die_mark = 0;
21858   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21859 }
21860
21861 /* Given DIE that we're marking as used, find any other dies
21862    it references as attributes and mark them as used.  */
21863
21864 static void
21865 prune_unused_types_walk_attribs (dw_die_ref die)
21866 {
21867   dw_attr_ref a;
21868   unsigned ix;
21869
21870   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21871     {
21872       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21873         {
21874           /* A reference to another DIE.
21875              Make sure that it will get emitted.
21876              If it was broken out into a comdat group, don't follow it.  */
21877           if (dwarf_version < 4
21878               || a->dw_attr == DW_AT_specification
21879               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21880             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21881         }
21882       /* Set the string's refcount to 0 so that prune_unused_types_mark
21883          accounts properly for it.  */
21884       if (AT_class (a) == dw_val_class_str)
21885         a->dw_attr_val.v.val_str->refcount = 0;
21886     }
21887 }
21888
21889
21890 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21891    to DIE's children.  */
21892
21893 static void
21894 prune_unused_types_mark (dw_die_ref die, int dokids)
21895 {
21896   dw_die_ref c;
21897
21898   if (die->die_mark == 0)
21899     {
21900       /* We haven't done this node yet.  Mark it as used.  */
21901       die->die_mark = 1;
21902
21903       /* We also have to mark its parents as used.
21904          (But we don't want to mark our parents' kids due to this.)  */
21905       if (die->die_parent)
21906         prune_unused_types_mark (die->die_parent, 0);
21907
21908       /* Mark any referenced nodes.  */
21909       prune_unused_types_walk_attribs (die);
21910
21911       /* If this node is a specification,
21912          also mark the definition, if it exists.  */
21913       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21914         prune_unused_types_mark (die->die_definition, 1);
21915     }
21916
21917   if (dokids && die->die_mark != 2)
21918     {
21919       /* We need to walk the children, but haven't done so yet.
21920          Remember that we've walked the kids.  */
21921       die->die_mark = 2;
21922
21923       /* If this is an array type, we need to make sure our
21924          kids get marked, even if they're types.  If we're
21925          breaking out types into comdat sections, do this
21926          for all type definitions.  */
21927       if (die->die_tag == DW_TAG_array_type
21928           || (dwarf_version >= 4
21929               && is_type_die (die) && ! is_declaration_die (die)))
21930         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21931       else
21932         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21933     }
21934 }
21935
21936 /* For local classes, look if any static member functions were emitted
21937    and if so, mark them.  */
21938
21939 static void
21940 prune_unused_types_walk_local_classes (dw_die_ref die)
21941 {
21942   dw_die_ref c;
21943
21944   if (die->die_mark == 2)
21945     return;
21946
21947   switch (die->die_tag)
21948     {
21949     case DW_TAG_structure_type:
21950     case DW_TAG_union_type:
21951     case DW_TAG_class_type:
21952       break;
21953
21954     case DW_TAG_subprogram:
21955       if (!get_AT_flag (die, DW_AT_declaration)
21956           || die->die_definition != NULL)
21957         prune_unused_types_mark (die, 1);
21958       return;
21959
21960     default:
21961       return;
21962     }
21963
21964   /* Mark children.  */
21965   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21966 }
21967
21968 /* Walk the tree DIE and mark types that we actually use.  */
21969
21970 static void
21971 prune_unused_types_walk (dw_die_ref die)
21972 {
21973   dw_die_ref c;
21974
21975   /* Don't do anything if this node is already marked and
21976      children have been marked as well.  */
21977   if (die->die_mark == 2)
21978     return;
21979
21980   switch (die->die_tag)
21981     {
21982     case DW_TAG_structure_type:
21983     case DW_TAG_union_type:
21984     case DW_TAG_class_type:
21985       if (die->die_perennial_p)
21986         break;
21987
21988       for (c = die->die_parent; c; c = c->die_parent)
21989         if (c->die_tag == DW_TAG_subprogram)
21990           break;
21991
21992       /* Finding used static member functions inside of classes
21993          is needed just for local classes, because for other classes
21994          static member function DIEs with DW_AT_specification
21995          are emitted outside of the DW_TAG_*_type.  If we ever change
21996          it, we'd need to call this even for non-local classes.  */
21997       if (c)
21998         prune_unused_types_walk_local_classes (die);
21999
22000       /* It's a type node --- don't mark it.  */
22001       return;
22002
22003     case DW_TAG_const_type:
22004     case DW_TAG_packed_type:
22005     case DW_TAG_pointer_type:
22006     case DW_TAG_reference_type:
22007     case DW_TAG_rvalue_reference_type:
22008     case DW_TAG_volatile_type:
22009     case DW_TAG_typedef:
22010     case DW_TAG_array_type:
22011     case DW_TAG_interface_type:
22012     case DW_TAG_friend:
22013     case DW_TAG_variant_part:
22014     case DW_TAG_enumeration_type:
22015     case DW_TAG_subroutine_type:
22016     case DW_TAG_string_type:
22017     case DW_TAG_set_type:
22018     case DW_TAG_subrange_type:
22019     case DW_TAG_ptr_to_member_type:
22020     case DW_TAG_file_type:
22021       if (die->die_perennial_p)
22022         break;
22023
22024       /* It's a type node --- don't mark it.  */
22025       return;
22026
22027     default:
22028       /* Mark everything else.  */
22029       break;
22030   }
22031
22032   if (die->die_mark == 0)
22033     {
22034       die->die_mark = 1;
22035
22036       /* Now, mark any dies referenced from here.  */
22037       prune_unused_types_walk_attribs (die);
22038     }
22039
22040   die->die_mark = 2;
22041
22042   /* Mark children.  */
22043   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22044 }
22045
22046 /* Increment the string counts on strings referred to from DIE's
22047    attributes.  */
22048
22049 static void
22050 prune_unused_types_update_strings (dw_die_ref die)
22051 {
22052   dw_attr_ref a;
22053   unsigned ix;
22054
22055   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22056     if (AT_class (a) == dw_val_class_str)
22057       {
22058         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22059         s->refcount++;
22060         /* Avoid unnecessarily putting strings that are used less than
22061            twice in the hash table.  */
22062         if (s->refcount
22063             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22064           {
22065             void ** slot;
22066             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22067                                              htab_hash_string (s->str),
22068                                              INSERT);
22069             gcc_assert (*slot == NULL);
22070             *slot = s;
22071           }
22072       }
22073 }
22074
22075 /* Remove from the tree DIE any dies that aren't marked.  */
22076
22077 static void
22078 prune_unused_types_prune (dw_die_ref die)
22079 {
22080   dw_die_ref c;
22081
22082   gcc_assert (die->die_mark);
22083   prune_unused_types_update_strings (die);
22084
22085   if (! die->die_child)
22086     return;
22087
22088   c = die->die_child;
22089   do {
22090     dw_die_ref prev = c;
22091     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22092       if (c == die->die_child)
22093         {
22094           /* No marked children between 'prev' and the end of the list.  */
22095           if (prev == c)
22096             /* No marked children at all.  */
22097             die->die_child = NULL;
22098           else
22099             {
22100               prev->die_sib = c->die_sib;
22101               die->die_child = prev;
22102             }
22103           return;
22104         }
22105
22106     if (c != prev->die_sib)
22107       prev->die_sib = c;
22108     prune_unused_types_prune (c);
22109   } while (c != die->die_child);
22110 }
22111
22112 /* A helper function for dwarf2out_finish called through
22113    htab_traverse.  Clear .debug_str strings that we haven't already
22114    decided to emit.  */
22115
22116 static int
22117 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22118 {
22119   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22120
22121   if (!node->label || !node->refcount)
22122     htab_clear_slot (debug_str_hash, h);
22123
22124   return 1;
22125 }
22126
22127 /* Remove dies representing declarations that we never use.  */
22128
22129 static void
22130 prune_unused_types (void)
22131 {
22132   unsigned int i;
22133   limbo_die_node *node;
22134   comdat_type_node *ctnode;
22135   pubname_ref pub;
22136   dcall_entry *dcall;
22137
22138 #if ENABLE_ASSERT_CHECKING
22139   /* All the marks should already be clear.  */
22140   verify_marks_clear (comp_unit_die ());
22141   for (node = limbo_die_list; node; node = node->next)
22142     verify_marks_clear (node->die);
22143   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22144     verify_marks_clear (ctnode->root_die);
22145 #endif /* ENABLE_ASSERT_CHECKING */
22146
22147   /* Mark types that are used in global variables.  */
22148   premark_types_used_by_global_vars ();
22149
22150   /* Set the mark on nodes that are actually used.  */
22151   prune_unused_types_walk (comp_unit_die ());
22152   for (node = limbo_die_list; node; node = node->next)
22153     prune_unused_types_walk (node->die);
22154   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22155     {
22156       prune_unused_types_walk (ctnode->root_die);
22157       prune_unused_types_mark (ctnode->type_die, 1);
22158     }
22159
22160   /* Also set the mark on nodes referenced from the
22161      pubname_table or arange_table.  */
22162   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22163     prune_unused_types_mark (pub->die, 1);
22164   for (i = 0; i < arange_table_in_use; i++)
22165     prune_unused_types_mark (arange_table[i], 1);
22166
22167   /* Mark nodes referenced from the direct call table.  */
22168   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22169     prune_unused_types_mark (dcall->targ_die, 1);
22170
22171   /* Get rid of nodes that aren't marked; and update the string counts.  */
22172   if (debug_str_hash && debug_str_hash_forced)
22173     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22174   else if (debug_str_hash)
22175     htab_empty (debug_str_hash);
22176   prune_unused_types_prune (comp_unit_die ());
22177   for (node = limbo_die_list; node; node = node->next)
22178     prune_unused_types_prune (node->die);
22179   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22180     prune_unused_types_prune (ctnode->root_die);
22181
22182   /* Leave the marks clear.  */
22183   prune_unmark_dies (comp_unit_die ());
22184   for (node = limbo_die_list; node; node = node->next)
22185     prune_unmark_dies (node->die);
22186   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22187     prune_unmark_dies (ctnode->root_die);
22188 }
22189
22190 /* Set the parameter to true if there are any relative pathnames in
22191    the file table.  */
22192 static int
22193 file_table_relative_p (void ** slot, void *param)
22194 {
22195   bool *p = (bool *) param;
22196   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22197   if (!IS_ABSOLUTE_PATH (d->filename))
22198     {
22199       *p = true;
22200       return 0;
22201     }
22202   return 1;
22203 }
22204
22205 /* Routines to manipulate hash table of comdat type units.  */
22206
22207 static hashval_t
22208 htab_ct_hash (const void *of)
22209 {
22210   hashval_t h;
22211   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22212
22213   memcpy (&h, type_node->signature, sizeof (h));
22214   return h;
22215 }
22216
22217 static int
22218 htab_ct_eq (const void *of1, const void *of2)
22219 {
22220   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22221   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22222
22223   return (! memcmp (type_node_1->signature, type_node_2->signature,
22224                     DWARF_TYPE_SIGNATURE_SIZE));
22225 }
22226
22227 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22228    to the location it would have been added, should we know its
22229    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22230    probably improve compactness of debug info, removing equivalent
22231    abbrevs, and hide any differences caused by deferring the
22232    computation of the assembler name, triggered by e.g. PCH.  */
22233
22234 static inline void
22235 move_linkage_attr (dw_die_ref die)
22236 {
22237   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22238   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22239
22240   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22241               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22242
22243   while (--ix > 0)
22244     {
22245       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22246
22247       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22248         break;
22249     }
22250
22251   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22252     {
22253       VEC_pop (dw_attr_node, die->die_attr);
22254       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22255     }
22256 }
22257
22258 /* Helper function for resolve_addr, attempt to resolve
22259    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22260    SYMBOL_REFs refer to variables emitted in the current CU.  */
22261
22262 static int
22263 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22264 {
22265   rtx rtl = *addr;
22266
22267   if (GET_CODE (rtl) == CONST_STRING)
22268     {
22269       size_t len = strlen (XSTR (rtl, 0)) + 1;
22270       tree t = build_string (len, XSTR (rtl, 0));
22271       tree tlen = build_int_cst (NULL_TREE, len - 1);
22272       TREE_TYPE (t)
22273         = build_array_type (char_type_node, build_index_type (tlen));
22274       rtl = lookup_constant_def (t);
22275       if (!rtl || !MEM_P (rtl))
22276         return 1;
22277       rtl = XEXP (rtl, 0);
22278       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22279       *addr = rtl;
22280       return 0;
22281     }
22282
22283   if (GET_CODE (rtl) == SYMBOL_REF
22284       && SYMBOL_REF_DECL (rtl)
22285       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22286     return 1;
22287
22288   if (GET_CODE (rtl) == CONST
22289       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22290     return 1;
22291
22292   return 0;
22293 }
22294
22295 /* Helper function for resolve_addr, handle one location
22296    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22297    the location list couldn't be resolved.  */
22298
22299 static bool
22300 resolve_addr_in_expr (dw_loc_descr_ref loc)
22301 {
22302   for (; loc; loc = loc->dw_loc_next)
22303     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22304          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22305         || (loc->dw_loc_opc == DW_OP_implicit_value
22306             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22307             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22308       return false;
22309     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22310              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22311       {
22312         dw_die_ref ref
22313           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22314         if (ref == NULL)
22315           return false;
22316         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22317         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22318         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22319       }
22320   return true;
22321 }
22322
22323 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22324    an address in .rodata section if the string literal is emitted there,
22325    or remove the containing location list or replace DW_AT_const_value
22326    with DW_AT_location and empty location expression, if it isn't found
22327    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22328    to something that has been emitted in the current CU.  */
22329
22330 static void
22331 resolve_addr (dw_die_ref die)
22332 {
22333   dw_die_ref c;
22334   dw_attr_ref a;
22335   dw_loc_list_ref *curr;
22336   unsigned ix;
22337
22338   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22339     switch (AT_class (a))
22340       {
22341       case dw_val_class_loc_list:
22342         curr = AT_loc_list_ptr (a);
22343         while (*curr)
22344           {
22345             if (!resolve_addr_in_expr ((*curr)->expr))
22346               {
22347                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22348                 if (next && (*curr)->ll_symbol)
22349                   {
22350                     gcc_assert (!next->ll_symbol);
22351                     next->ll_symbol = (*curr)->ll_symbol;
22352                   }
22353                 *curr = next;
22354               }
22355             else
22356               curr = &(*curr)->dw_loc_next;
22357           }
22358         if (!AT_loc_list (a))
22359           {
22360             remove_AT (die, a->dw_attr);
22361             ix--;
22362           }
22363         break;
22364       case dw_val_class_loc:
22365         if (!resolve_addr_in_expr (AT_loc (a)))
22366           {
22367             remove_AT (die, a->dw_attr);
22368             ix--;
22369           }
22370         break;
22371       case dw_val_class_addr:
22372         if (a->dw_attr == DW_AT_const_value
22373             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22374           {
22375             remove_AT (die, a->dw_attr);
22376             ix--;
22377           }
22378         break;
22379       default:
22380         break;
22381       }
22382
22383   FOR_EACH_CHILD (die, c, resolve_addr (c));
22384 }
22385
22386 /* Output stuff that dwarf requires at the end of every file,
22387    and generate the DWARF-2 debugging info.  */
22388
22389 static void
22390 dwarf2out_finish (const char *filename)
22391 {
22392   limbo_die_node *node, *next_node;
22393   comdat_type_node *ctnode;
22394   htab_t comdat_type_table;
22395   dw_die_ref die = 0;
22396   unsigned int i;
22397
22398   gen_remaining_tmpl_value_param_die_attribute ();
22399
22400   /* Add the name for the main input file now.  We delayed this from
22401      dwarf2out_init to avoid complications with PCH.  */
22402   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22403   if (!IS_ABSOLUTE_PATH (filename))
22404     add_comp_dir_attribute (comp_unit_die ());
22405   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22406     {
22407       bool p = false;
22408       htab_traverse (file_table, file_table_relative_p, &p);
22409       if (p)
22410         add_comp_dir_attribute (comp_unit_die ());
22411     }
22412
22413   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22414     {
22415       add_location_or_const_value_attribute (
22416         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22417         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22418         DW_AT_location);
22419     }
22420
22421   /* Traverse the limbo die list, and add parent/child links.  The only
22422      dies without parents that should be here are concrete instances of
22423      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22424      For concrete instances, we can get the parent die from the abstract
22425      instance.  */
22426   for (node = limbo_die_list; node; node = next_node)
22427     {
22428       next_node = node->next;
22429       die = node->die;
22430
22431       if (die->die_parent == NULL)
22432         {
22433           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22434
22435           if (origin)
22436             add_child_die (origin->die_parent, die);
22437           else if (is_cu_die (die))
22438             ;
22439           else if (seen_error ())
22440             /* It's OK to be confused by errors in the input.  */
22441             add_child_die (comp_unit_die (), die);
22442           else
22443             {
22444               /* In certain situations, the lexical block containing a
22445                  nested function can be optimized away, which results
22446                  in the nested function die being orphaned.  Likewise
22447                  with the return type of that nested function.  Force
22448                  this to be a child of the containing function.
22449
22450                  It may happen that even the containing function got fully
22451                  inlined and optimized out.  In that case we are lost and
22452                  assign the empty child.  This should not be big issue as
22453                  the function is likely unreachable too.  */
22454               tree context = NULL_TREE;
22455
22456               gcc_assert (node->created_for);
22457
22458               if (DECL_P (node->created_for))
22459                 context = DECL_CONTEXT (node->created_for);
22460               else if (TYPE_P (node->created_for))
22461                 context = TYPE_CONTEXT (node->created_for);
22462
22463               gcc_assert (context
22464                           && (TREE_CODE (context) == FUNCTION_DECL
22465                               || TREE_CODE (context) == NAMESPACE_DECL));
22466
22467               origin = lookup_decl_die (context);
22468               if (origin)
22469                 add_child_die (origin, die);
22470               else
22471                 add_child_die (comp_unit_die (), die);
22472             }
22473         }
22474     }
22475
22476   limbo_die_list = NULL;
22477
22478   resolve_addr (comp_unit_die ());
22479
22480   for (node = deferred_asm_name; node; node = node->next)
22481     {
22482       tree decl = node->created_for;
22483       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22484         {
22485           add_linkage_attr (node->die, decl);
22486           move_linkage_attr (node->die);
22487         }
22488     }
22489
22490   deferred_asm_name = NULL;
22491
22492   /* Walk through the list of incomplete types again, trying once more to
22493      emit full debugging info for them.  */
22494   retry_incomplete_types ();
22495
22496   if (flag_eliminate_unused_debug_types)
22497     prune_unused_types ();
22498
22499   /* Generate separate CUs for each of the include files we've seen.
22500      They will go into limbo_die_list.  */
22501   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22502     break_out_includes (comp_unit_die ());
22503
22504   /* Generate separate COMDAT sections for type DIEs. */
22505   if (dwarf_version >= 4)
22506     {
22507       break_out_comdat_types (comp_unit_die ());
22508
22509       /* Each new type_unit DIE was added to the limbo die list when created.
22510          Since these have all been added to comdat_type_list, clear the
22511          limbo die list.  */
22512       limbo_die_list = NULL;
22513
22514       /* For each new comdat type unit, copy declarations for incomplete
22515          types to make the new unit self-contained (i.e., no direct
22516          references to the main compile unit).  */
22517       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22518         copy_decls_for_unworthy_types (ctnode->root_die);
22519       copy_decls_for_unworthy_types (comp_unit_die ());
22520
22521       /* In the process of copying declarations from one unit to another,
22522          we may have left some declarations behind that are no longer
22523          referenced.  Prune them.  */
22524       prune_unused_types ();
22525     }
22526
22527   /* Traverse the DIE's and add add sibling attributes to those DIE's
22528      that have children.  */
22529   add_sibling_attributes (comp_unit_die ());
22530   for (node = limbo_die_list; node; node = node->next)
22531     add_sibling_attributes (node->die);
22532   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22533     add_sibling_attributes (ctnode->root_die);
22534
22535   /* Output a terminator label for the .text section.  */
22536   switch_to_section (text_section);
22537   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22538   if (flag_reorder_blocks_and_partition)
22539     {
22540       switch_to_section (unlikely_text_section ());
22541       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22542     }
22543
22544   /* We can only use the low/high_pc attributes if all of the code was
22545      in .text.  */
22546   if (!have_multiple_function_sections
22547       || !(dwarf_version >= 3 || !dwarf_strict))
22548     {
22549       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22550       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22551     }
22552
22553   else
22554     {
22555       unsigned fde_idx = 0;
22556       bool range_list_added = false;
22557
22558       /* We need to give .debug_loc and .debug_ranges an appropriate
22559          "base address".  Use zero so that these addresses become
22560          absolute.  Historically, we've emitted the unexpected
22561          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22562          Emit both to give time for other tools to adapt.  */
22563       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22564       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22565
22566       if (text_section_used)
22567         add_ranges_by_labels (comp_unit_die (), text_section_label,
22568                               text_end_label, &range_list_added);
22569       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22570         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22571                               cold_end_label, &range_list_added);
22572
22573       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22574         {
22575           dw_fde_ref fde = &fde_table[fde_idx];
22576
22577           if (fde->dw_fde_switched_sections)
22578             {
22579               if (!fde->in_std_section)
22580                 add_ranges_by_labels (comp_unit_die (),
22581                                       fde->dw_fde_hot_section_label,
22582                                       fde->dw_fde_hot_section_end_label,
22583                                       &range_list_added);
22584               if (!fde->cold_in_std_section)
22585                 add_ranges_by_labels (comp_unit_die (),
22586                                       fde->dw_fde_unlikely_section_label,
22587                                       fde->dw_fde_unlikely_section_end_label,
22588                                       &range_list_added);
22589             }
22590           else if (!fde->in_std_section)
22591             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22592                                   fde->dw_fde_end, &range_list_added);
22593         }
22594
22595       if (range_list_added)
22596         add_ranges (NULL);
22597     }
22598
22599   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22600     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22601                     debug_line_section_label);
22602
22603   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22604     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
22605
22606   /* Output all of the compilation units.  We put the main one last so that
22607      the offsets are available to output_pubnames.  */
22608   for (node = limbo_die_list; node; node = node->next)
22609     output_comp_unit (node->die, 0);
22610
22611   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22612   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22613     {
22614       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22615
22616       /* Don't output duplicate types.  */
22617       if (*slot != HTAB_EMPTY_ENTRY)
22618         continue;
22619
22620       /* Add a pointer to the line table for the main compilation unit
22621          so that the debugger can make sense of DW_AT_decl_file
22622          attributes.  */
22623       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22624         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22625                         debug_line_section_label);
22626
22627       output_comdat_type_unit (ctnode);
22628       *slot = ctnode;
22629     }
22630   htab_delete (comdat_type_table);
22631
22632   /* Output the main compilation unit if non-empty or if .debug_macinfo
22633      has been emitted.  */
22634   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22635
22636   /* Output the abbreviation table.  */
22637   switch_to_section (debug_abbrev_section);
22638   output_abbrev_section ();
22639
22640   /* Output location list section if necessary.  */
22641   if (have_location_lists)
22642     {
22643       /* Output the location lists info.  */
22644       switch_to_section (debug_loc_section);
22645       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22646                                    DEBUG_LOC_SECTION_LABEL, 0);
22647       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22648       output_location_lists (die);
22649     }
22650
22651   /* Output public names table if necessary.  */
22652   if (!VEC_empty (pubname_entry, pubname_table))
22653     {
22654       switch_to_section (debug_pubnames_section);
22655       output_pubnames (pubname_table);
22656     }
22657
22658   /* Output public types table if necessary.  */
22659   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22660      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22661      simply won't look for the section.  */
22662   if (!VEC_empty (pubname_entry, pubtype_table))
22663     {
22664       switch_to_section (debug_pubtypes_section);
22665       output_pubnames (pubtype_table);
22666     }
22667
22668   /* Output direct and virtual call tables if necessary.  */
22669   if (!VEC_empty (dcall_entry, dcall_table))
22670     {
22671       switch_to_section (debug_dcall_section);
22672       output_dcall_table ();
22673     }
22674   if (!VEC_empty (vcall_entry, vcall_table))
22675     {
22676       switch_to_section (debug_vcall_section);
22677       output_vcall_table ();
22678     }
22679
22680   /* Output the address range information.  We only put functions in the arange
22681      table, so don't write it out if we don't have any.  */
22682   if (fde_table_in_use)
22683     {
22684       switch_to_section (debug_aranges_section);
22685       output_aranges ();
22686     }
22687
22688   /* Output ranges section if necessary.  */
22689   if (ranges_table_in_use)
22690     {
22691       switch_to_section (debug_ranges_section);
22692       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22693       output_ranges ();
22694     }
22695
22696   /* Output the source line correspondence table.  We must do this
22697      even if there is no line information.  Otherwise, on an empty
22698      translation unit, we will generate a present, but empty,
22699      .debug_info section.  IRIX 6.5 `nm' will then complain when
22700      examining the file.  This is done late so that any filenames
22701      used by the debug_info section are marked as 'used'.  */
22702   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22703     {
22704       switch_to_section (debug_line_section);
22705       output_line_info ();
22706     }
22707
22708   /* Have to end the macro section.  */
22709   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22710     {
22711       switch_to_section (debug_macinfo_section);
22712       dw2_asm_output_data (1, 0, "End compilation unit");
22713     }
22714
22715   /* If we emitted any DW_FORM_strp form attribute, output the string
22716      table too.  */
22717   if (debug_str_hash)
22718     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22719 }
22720
22721 #include "gt-dwarf2out.h"