OSDN Git Service

2010-08-27 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_UNWIND_INFO
116 #define DWARF2_UNWIND_INFO 0
117 #endif
118
119 #ifndef INCOMING_RETURN_ADDR_RTX
120 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
121 #endif
122
123 #ifndef DWARF2_FRAME_INFO
124 # ifdef DWARF2_DEBUGGING_INFO
125 #  define DWARF2_FRAME_INFO \
126   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
127 # else
128 #  define DWARF2_FRAME_INFO 0
129 # endif
130 #endif
131
132 /* Map register numbers held in the call frame info that gcc has
133    collected using DWARF_FRAME_REGNUM to those that should be output in
134    .debug_frame and .eh_frame.  */
135 #ifndef DWARF2_FRAME_REG_OUT
136 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
137 #endif
138
139 /* Save the result of dwarf2out_do_frame across PCH.  */
140 static GTY(()) bool saved_do_cfi_asm = 0;
141
142 /* Decide whether we want to emit frame unwind information for the current
143    translation unit.  */
144
145 int
146 dwarf2out_do_frame (void)
147 {
148   /* We want to emit correct CFA location expressions or lists, so we
149      have to return true if we're going to output debug info, even if
150      we're not going to output frame or unwind info.  */
151   return (write_symbols == DWARF2_DEBUG
152           || write_symbols == VMS_AND_DWARF2_DEBUG
153           || DWARF2_FRAME_INFO || saved_do_cfi_asm
154           || (DWARF2_UNWIND_INFO
155               && (flag_unwind_tables
156                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
157           );
158 }
159
160 /* Decide whether to emit frame unwind via assembler directives.  */
161
162 int
163 dwarf2out_do_cfi_asm (void)
164 {
165   int enc;
166
167 #ifdef MIPS_DEBUGGING_INFO
168   return false;
169 #endif
170   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
171     return false;
172   if (saved_do_cfi_asm)
173     return true;
174   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
175     return false;
176
177   /* Make sure the personality encoding is one the assembler can support.
178      In particular, aligned addresses can't be handled.  */
179   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
180   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181     return false;
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185
186   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
187     {
188 #ifdef TARGET_UNWIND_INFO
189       return false;
190 #else
191       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
192         return false;
193 #endif
194     }
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_dcall_section;
232 static GTY(()) section *debug_vcall_section;
233 static GTY(()) section *debug_str_section;
234 static GTY(()) section *debug_ranges_section;
235 static GTY(()) section *debug_frame_section;
236
237 /* Personality decl of current unit.  Used only when assembler does not support
238    personality CFI.  */
239 static GTY(()) rtx current_unit_personality;
240
241 /* How to start an assembler comment.  */
242 #ifndef ASM_COMMENT_START
243 #define ASM_COMMENT_START ";#"
244 #endif
245
246 typedef struct dw_cfi_struct *dw_cfi_ref;
247 typedef struct dw_fde_struct *dw_fde_ref;
248 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
249
250 /* Call frames are described using a sequence of Call Frame
251    Information instructions.  The register number, offset
252    and address fields are provided as possible operands;
253    their use is selected by the opcode field.  */
254
255 enum dw_cfi_oprnd_type {
256   dw_cfi_oprnd_unused,
257   dw_cfi_oprnd_reg_num,
258   dw_cfi_oprnd_offset,
259   dw_cfi_oprnd_addr,
260   dw_cfi_oprnd_loc
261 };
262
263 typedef union GTY(()) dw_cfi_oprnd_struct {
264   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
265   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
266   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
267   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
268 }
269 dw_cfi_oprnd;
270
271 typedef struct GTY(()) dw_cfi_struct {
272   dw_cfi_ref dw_cfi_next;
273   enum dwarf_call_frame_info dw_cfi_opc;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd1;
276   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
277     dw_cfi_oprnd2;
278 }
279 dw_cfi_node;
280
281 /* This is how we define the location of the CFA. We use to handle it
282    as REG + OFFSET all the time,  but now it can be more complex.
283    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
284    Instead of passing around REG and OFFSET, we pass a copy
285    of this structure.  */
286 typedef struct GTY(()) cfa_loc {
287   HOST_WIDE_INT offset;
288   HOST_WIDE_INT base_offset;
289   unsigned int reg;
290   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
291   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
292 } dw_cfa_location;
293
294 /* All call frame descriptions (FDE's) in the GCC generated DWARF
295    refer to a single Common Information Entry (CIE), defined at
296    the beginning of the .debug_frame section.  This use of a single
297    CIE obviates the need to keep track of multiple CIE's
298    in the DWARF generation routines below.  */
299
300 typedef struct GTY(()) dw_fde_struct {
301   tree decl;
302   const char *dw_fde_begin;
303   const char *dw_fde_current_label;
304   const char *dw_fde_end;
305   const char *dw_fde_vms_end_prologue;
306   const char *dw_fde_vms_begin_epilogue;
307   const char *dw_fde_hot_section_label;
308   const char *dw_fde_hot_section_end_label;
309   const char *dw_fde_unlikely_section_label;
310   const char *dw_fde_unlikely_section_end_label;
311   dw_cfi_ref dw_fde_cfi;
312   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
313   HOST_WIDE_INT stack_realignment;
314   unsigned funcdef_number;
315   /* Dynamic realign argument pointer register.  */
316   unsigned int drap_reg;
317   /* Virtual dynamic realign argument pointer register.  */
318   unsigned int vdrap_reg;
319   /* These 3 flags are copied from rtl_data in function.h.  */
320   unsigned all_throwers_are_sibcalls : 1;
321   unsigned uses_eh_lsda : 1;
322   unsigned nothrow : 1;
323   /* Whether we did stack realign in this call frame.  */
324   unsigned stack_realign : 1;
325   /* Whether dynamic realign argument pointer register has been saved.  */
326   unsigned drap_reg_saved: 1;
327   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
328   unsigned in_std_section : 1;
329   /* True iff dw_fde_unlikely_section_label is in text_section or
330      cold_text_section.  */
331   unsigned cold_in_std_section : 1;
332   /* True iff switched sections.  */
333   unsigned dw_fde_switched_sections : 1;
334   /* True iff switching from cold to hot section.  */
335   unsigned dw_fde_switched_cold_to_hot : 1;
336 }
337 dw_fde_node;
338
339 /* Maximum size (in bytes) of an artificially generated label.  */
340 #define MAX_ARTIFICIAL_LABEL_BYTES      30
341
342 /* The size of addresses as they appear in the Dwarf 2 data.
343    Some architectures use word addresses to refer to code locations,
344    but Dwarf 2 info always uses byte addresses.  On such machines,
345    Dwarf 2 addresses need to be larger than the architecture's
346    pointers.  */
347 #ifndef DWARF2_ADDR_SIZE
348 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
349 #endif
350
351 /* The size in bytes of a DWARF field indicating an offset or length
352    relative to a debug info section, specified to be 4 bytes in the
353    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
354    as PTR_SIZE.  */
355
356 #ifndef DWARF_OFFSET_SIZE
357 #define DWARF_OFFSET_SIZE 4
358 #endif
359
360 /* The size in bytes of a DWARF 4 type signature.  */
361
362 #ifndef DWARF_TYPE_SIGNATURE_SIZE
363 #define DWARF_TYPE_SIGNATURE_SIZE 8
364 #endif
365
366 /* According to the (draft) DWARF 3 specification, the initial length
367    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
368    bytes are 0xffffffff, followed by the length stored in the next 8
369    bytes.
370
371    However, the SGI/MIPS ABI uses an initial length which is equal to
372    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
373
374 #ifndef DWARF_INITIAL_LENGTH_SIZE
375 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
376 #endif
377
378 /* Round SIZE up to the nearest BOUNDARY.  */
379 #define DWARF_ROUND(SIZE,BOUNDARY) \
380   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
381
382 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
383 #ifndef DWARF_CIE_DATA_ALIGNMENT
384 #ifdef STACK_GROWS_DOWNWARD
385 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
386 #else
387 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
388 #endif
389 #endif
390
391 /* CIE identifier.  */
392 #if HOST_BITS_PER_WIDE_INT >= 64
393 #define DWARF_CIE_ID \
394   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
395 #else
396 #define DWARF_CIE_ID DW_CIE_ID
397 #endif
398
399 /* A pointer to the base of a table that contains frame description
400    information for each routine.  */
401 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
402
403 /* Number of elements currently allocated for fde_table.  */
404 static GTY(()) unsigned fde_table_allocated;
405
406 /* Number of elements in fde_table currently in use.  */
407 static GTY(()) unsigned fde_table_in_use;
408
409 /* Size (in elements) of increments by which we may expand the
410    fde_table.  */
411 #define FDE_TABLE_INCREMENT 256
412
413 /* Get the current fde_table entry we should use.  */
414
415 static inline dw_fde_ref
416 current_fde (void)
417 {
418   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
419 }
420
421 /* A list of call frame insns for the CIE.  */
422 static GTY(()) dw_cfi_ref cie_cfi_head;
423
424 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
425    attribute that accelerates the lookup of the FDE associated
426    with the subprogram.  This variable holds the table index of the FDE
427    associated with the current function (body) definition.  */
428 static unsigned current_funcdef_fde;
429
430 struct GTY(()) indirect_string_node {
431   const char *str;
432   unsigned int refcount;
433   enum dwarf_form form;
434   char *label;
435 };
436
437 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438
439 /* True if the compilation unit has location entries that reference
440    debug strings.  */
441 static GTY(()) bool debug_str_hash_forced = false;
442
443 static GTY(()) int dw2_string_counter;
444 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445
446 /* True if the compilation unit places functions in more than one section.  */
447 static GTY(()) bool have_multiple_function_sections = false;
448
449 /* Whether the default text and cold text sections have been used at all.  */
450
451 static GTY(()) bool text_section_used = false;
452 static GTY(()) bool cold_text_section_used = false;
453
454 /* The default cold text section.  */
455 static GTY(()) section *cold_text_section;
456
457 /* Forward declarations for functions defined in this file.  */
458
459 static char *stripattributes (const char *);
460 static const char *dwarf_cfi_name (unsigned);
461 static dw_cfi_ref new_cfi (void);
462 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
463 static void add_fde_cfi (const char *, dw_cfi_ref);
464 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
465 static void lookup_cfa (dw_cfa_location *);
466 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
467 static void initial_return_save (rtx);
468 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
469                                           HOST_WIDE_INT);
470 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
471 static void output_cfi_directive (dw_cfi_ref);
472 static void output_call_frame_info (int);
473 static void dwarf2out_note_section_used (void);
474 static void flush_queued_reg_saves (void);
475 static bool clobbers_queued_reg_save (const_rtx);
476 static void dwarf2out_frame_debug_expr (rtx, const char *);
477
478 /* Support for complex CFA locations.  */
479 static void output_cfa_loc (dw_cfi_ref);
480 static void output_cfa_loc_raw (dw_cfi_ref);
481 static void get_cfa_from_loc_descr (dw_cfa_location *,
482                                     struct dw_loc_descr_struct *);
483 static struct dw_loc_descr_struct *build_cfa_loc
484   (dw_cfa_location *, HOST_WIDE_INT);
485 static struct dw_loc_descr_struct *build_cfa_aligned_loc
486   (HOST_WIDE_INT, HOST_WIDE_INT);
487 static void def_cfa_1 (const char *, dw_cfa_location *);
488 static struct dw_loc_descr_struct *mem_loc_descriptor
489   (rtx, enum machine_mode mode, enum var_init_status);
490
491 /* How to start an assembler comment.  */
492 #ifndef ASM_COMMENT_START
493 #define ASM_COMMENT_START ";#"
494 #endif
495
496 /* Data and reference forms for relocatable data.  */
497 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
498 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
499
500 #ifndef DEBUG_FRAME_SECTION
501 #define DEBUG_FRAME_SECTION     ".debug_frame"
502 #endif
503
504 #ifndef FUNC_BEGIN_LABEL
505 #define FUNC_BEGIN_LABEL        "LFB"
506 #endif
507
508 #ifndef FUNC_END_LABEL
509 #define FUNC_END_LABEL          "LFE"
510 #endif
511
512 #ifndef PROLOGUE_END_LABEL
513 #define PROLOGUE_END_LABEL      "LPE"
514 #endif
515
516 #ifndef EPILOGUE_BEGIN_LABEL
517 #define EPILOGUE_BEGIN_LABEL    "LEB"
518 #endif
519
520 #ifndef FRAME_BEGIN_LABEL
521 #define FRAME_BEGIN_LABEL       "Lframe"
522 #endif
523 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
524 #define CIE_END_LABEL           "LECIE"
525 #define FDE_LABEL               "LSFDE"
526 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
527 #define FDE_END_LABEL           "LEFDE"
528 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
529 #define LINE_NUMBER_END_LABEL   "LELT"
530 #define LN_PROLOG_AS_LABEL      "LASLTP"
531 #define LN_PROLOG_END_LABEL     "LELTP"
532 #define DIE_LABEL_PREFIX        "DW"
533
534 /* The DWARF 2 CFA column which tracks the return address.  Normally this
535    is the column for PC, or the first column after all of the hard
536    registers.  */
537 #ifndef DWARF_FRAME_RETURN_COLUMN
538 #ifdef PC_REGNUM
539 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
540 #else
541 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
542 #endif
543 #endif
544
545 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
546    default, we just provide columns for all registers.  */
547 #ifndef DWARF_FRAME_REGNUM
548 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
549 #endif
550 \f
551 /* Hook used by __throw.  */
552
553 rtx
554 expand_builtin_dwarf_sp_column (void)
555 {
556   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
557   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
558 }
559
560 /* Return a pointer to a copy of the section string name S with all
561    attributes stripped off, and an asterisk prepended (for assemble_name).  */
562
563 static inline char *
564 stripattributes (const char *s)
565 {
566   char *stripped = XNEWVEC (char, strlen (s) + 2);
567   char *p = stripped;
568
569   *p++ = '*';
570
571   while (*s && *s != ',')
572     *p++ = *s++;
573
574   *p = '\0';
575   return stripped;
576 }
577
578 /* MEM is a memory reference for the register size table, each element of
579    which has mode MODE.  Initialize column C as a return address column.  */
580
581 static void
582 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
583 {
584   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
585   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
586   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
587 }
588
589 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
590
591 static inline HOST_WIDE_INT
592 div_data_align (HOST_WIDE_INT off)
593 {
594   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
595   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
596   return r;
597 }
598
599 /* Return true if we need a signed version of a given opcode
600    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
601
602 static inline bool
603 need_data_align_sf_opcode (HOST_WIDE_INT off)
604 {
605   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
606 }
607
608 /* Generate code to initialize the register size table.  */
609
610 void
611 expand_builtin_init_dwarf_reg_sizes (tree address)
612 {
613   unsigned int i;
614   enum machine_mode mode = TYPE_MODE (char_type_node);
615   rtx addr = expand_normal (address);
616   rtx mem = gen_rtx_MEM (BLKmode, addr);
617   bool wrote_return_column = false;
618
619   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
620     {
621       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
622
623       if (rnum < DWARF_FRAME_REGISTERS)
624         {
625           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
626           enum machine_mode save_mode = reg_raw_mode[i];
627           HOST_WIDE_INT size;
628
629           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
630             save_mode = choose_hard_reg_mode (i, 1, true);
631           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
632             {
633               if (save_mode == VOIDmode)
634                 continue;
635               wrote_return_column = true;
636             }
637           size = GET_MODE_SIZE (save_mode);
638           if (offset < 0)
639             continue;
640
641           emit_move_insn (adjust_address (mem, mode, offset),
642                           gen_int_mode (size, mode));
643         }
644     }
645
646   if (!wrote_return_column)
647     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
648
649 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
650   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
651 #endif
652
653   targetm.init_dwarf_reg_sizes_extra (address);
654 }
655
656 /* Convert a DWARF call frame info. operation to its string name */
657
658 static const char *
659 dwarf_cfi_name (unsigned int cfi_opc)
660 {
661   switch (cfi_opc)
662     {
663     case DW_CFA_advance_loc:
664       return "DW_CFA_advance_loc";
665     case DW_CFA_offset:
666       return "DW_CFA_offset";
667     case DW_CFA_restore:
668       return "DW_CFA_restore";
669     case DW_CFA_nop:
670       return "DW_CFA_nop";
671     case DW_CFA_set_loc:
672       return "DW_CFA_set_loc";
673     case DW_CFA_advance_loc1:
674       return "DW_CFA_advance_loc1";
675     case DW_CFA_advance_loc2:
676       return "DW_CFA_advance_loc2";
677     case DW_CFA_advance_loc4:
678       return "DW_CFA_advance_loc4";
679     case DW_CFA_offset_extended:
680       return "DW_CFA_offset_extended";
681     case DW_CFA_restore_extended:
682       return "DW_CFA_restore_extended";
683     case DW_CFA_undefined:
684       return "DW_CFA_undefined";
685     case DW_CFA_same_value:
686       return "DW_CFA_same_value";
687     case DW_CFA_register:
688       return "DW_CFA_register";
689     case DW_CFA_remember_state:
690       return "DW_CFA_remember_state";
691     case DW_CFA_restore_state:
692       return "DW_CFA_restore_state";
693     case DW_CFA_def_cfa:
694       return "DW_CFA_def_cfa";
695     case DW_CFA_def_cfa_register:
696       return "DW_CFA_def_cfa_register";
697     case DW_CFA_def_cfa_offset:
698       return "DW_CFA_def_cfa_offset";
699
700     /* DWARF 3 */
701     case DW_CFA_def_cfa_expression:
702       return "DW_CFA_def_cfa_expression";
703     case DW_CFA_expression:
704       return "DW_CFA_expression";
705     case DW_CFA_offset_extended_sf:
706       return "DW_CFA_offset_extended_sf";
707     case DW_CFA_def_cfa_sf:
708       return "DW_CFA_def_cfa_sf";
709     case DW_CFA_def_cfa_offset_sf:
710       return "DW_CFA_def_cfa_offset_sf";
711
712     /* SGI/MIPS specific */
713     case DW_CFA_MIPS_advance_loc8:
714       return "DW_CFA_MIPS_advance_loc8";
715
716     /* GNU extensions */
717     case DW_CFA_GNU_window_save:
718       return "DW_CFA_GNU_window_save";
719     case DW_CFA_GNU_args_size:
720       return "DW_CFA_GNU_args_size";
721     case DW_CFA_GNU_negative_offset_extended:
722       return "DW_CFA_GNU_negative_offset_extended";
723
724     default:
725       return "DW_CFA_<unknown>";
726     }
727 }
728
729 /* Return a pointer to a newly allocated Call Frame Instruction.  */
730
731 static inline dw_cfi_ref
732 new_cfi (void)
733 {
734   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
735
736   cfi->dw_cfi_next = NULL;
737   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
738   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
739
740   return cfi;
741 }
742
743 /* Add a Call Frame Instruction to list of instructions.  */
744
745 static inline void
746 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
747 {
748   dw_cfi_ref *p;
749   dw_fde_ref fde = current_fde ();
750
751   /* When DRAP is used, CFA is defined with an expression.  Redefine
752      CFA may lead to a different CFA value.   */
753   /* ??? Of course, this heuristic fails when we're annotating epilogues,
754      because of course we'll always want to redefine the CFA back to the
755      stack pointer on the way out.  Where should we move this check?  */
756   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
757     switch (cfi->dw_cfi_opc)
758       {
759         case DW_CFA_def_cfa_register:
760         case DW_CFA_def_cfa_offset:
761         case DW_CFA_def_cfa_offset_sf:
762         case DW_CFA_def_cfa:
763         case DW_CFA_def_cfa_sf:
764           gcc_unreachable ();
765
766         default:
767           break;
768       }
769
770   /* Find the end of the chain.  */
771   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
772     ;
773
774   *p = cfi;
775 }
776
777 /* Generate a new label for the CFI info to refer to.  FORCE is true
778    if a label needs to be output even when using .cfi_* directives.  */
779
780 char *
781 dwarf2out_cfi_label (bool force)
782 {
783   static char label[20];
784
785   if (!force && dwarf2out_do_cfi_asm ())
786     {
787       /* In this case, we will be emitting the asm directive instead of
788          the label, so just return a placeholder to keep the rest of the
789          interfaces happy.  */
790       strcpy (label, "<do not output>");
791     }
792   else
793     {
794       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
795       ASM_OUTPUT_LABEL (asm_out_file, label);
796     }
797
798   return label;
799 }
800
801 /* True if remember_state should be emitted before following CFI directive.  */
802 static bool emit_cfa_remember;
803
804 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
805    or to the CIE if LABEL is NULL.  */
806
807 static void
808 add_fde_cfi (const char *label, dw_cfi_ref cfi)
809 {
810   dw_cfi_ref *list_head;
811
812   if (emit_cfa_remember)
813     {
814       dw_cfi_ref cfi_remember;
815
816       /* Emit the state save.  */
817       emit_cfa_remember = false;
818       cfi_remember = new_cfi ();
819       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
820       add_fde_cfi (label, cfi_remember);
821     }
822
823   list_head = &cie_cfi_head;
824
825   if (dwarf2out_do_cfi_asm ())
826     {
827       if (label)
828         {
829           dw_fde_ref fde = current_fde ();
830
831           gcc_assert (fde != NULL);
832
833           /* We still have to add the cfi to the list so that lookup_cfa
834              works later on.  When -g2 and above we even need to force
835              emitting of CFI labels and add to list a DW_CFA_set_loc for
836              convert_cfa_to_fb_loc_list purposes.  If we're generating
837              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
838              convert_cfa_to_fb_loc_list.  */
839           if (dwarf_version == 2
840               && debug_info_level > DINFO_LEVEL_TERSE
841               && (write_symbols == DWARF2_DEBUG
842                   || write_symbols == VMS_AND_DWARF2_DEBUG))
843             {
844               switch (cfi->dw_cfi_opc)
845                 {
846                 case DW_CFA_def_cfa_offset:
847                 case DW_CFA_def_cfa_offset_sf:
848                 case DW_CFA_def_cfa_register:
849                 case DW_CFA_def_cfa:
850                 case DW_CFA_def_cfa_sf:
851                 case DW_CFA_def_cfa_expression:
852                 case DW_CFA_restore_state:
853                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
854                     label = dwarf2out_cfi_label (true);
855
856                   if (fde->dw_fde_current_label == NULL
857                       || strcmp (label, fde->dw_fde_current_label) != 0)
858                     {
859                       dw_cfi_ref xcfi;
860
861                       label = xstrdup (label);
862
863                       /* Set the location counter to the new label.  */
864                       xcfi = new_cfi ();
865                       /* It doesn't metter whether DW_CFA_set_loc
866                          or DW_CFA_advance_loc4 is added here, those aren't
867                          emitted into assembly, only looked up by
868                          convert_cfa_to_fb_loc_list.  */
869                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
870                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
871                       add_cfi (&fde->dw_fde_cfi, xcfi);
872                       fde->dw_fde_current_label = label;
873                     }
874                   break;
875                 default:
876                   break;
877                 }
878             }
879
880           output_cfi_directive (cfi);
881
882           list_head = &fde->dw_fde_cfi;
883         }
884       /* ??? If this is a CFI for the CIE, we don't emit.  This
885          assumes that the standard CIE contents that the assembler
886          uses matches the standard CIE contents that the compiler
887          uses.  This is probably a bad assumption.  I'm not quite
888          sure how to address this for now.  */
889     }
890   else if (label)
891     {
892       dw_fde_ref fde = current_fde ();
893
894       gcc_assert (fde != NULL);
895
896       if (*label == 0)
897         label = dwarf2out_cfi_label (false);
898
899       if (fde->dw_fde_current_label == NULL
900           || strcmp (label, fde->dw_fde_current_label) != 0)
901         {
902           dw_cfi_ref xcfi;
903
904           label = xstrdup (label);
905
906           /* Set the location counter to the new label.  */
907           xcfi = new_cfi ();
908           /* If we have a current label, advance from there, otherwise
909              set the location directly using set_loc.  */
910           xcfi->dw_cfi_opc = fde->dw_fde_current_label
911                              ? DW_CFA_advance_loc4
912                              : DW_CFA_set_loc;
913           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
914           add_cfi (&fde->dw_fde_cfi, xcfi);
915
916           fde->dw_fde_current_label = label;
917         }
918
919       list_head = &fde->dw_fde_cfi;
920     }
921
922   add_cfi (list_head, cfi);
923 }
924
925 /* Subroutine of lookup_cfa.  */
926
927 static void
928 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
929 {
930   switch (cfi->dw_cfi_opc)
931     {
932     case DW_CFA_def_cfa_offset:
933     case DW_CFA_def_cfa_offset_sf:
934       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
935       break;
936     case DW_CFA_def_cfa_register:
937       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
938       break;
939     case DW_CFA_def_cfa:
940     case DW_CFA_def_cfa_sf:
941       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
943       break;
944     case DW_CFA_def_cfa_expression:
945       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
946       break;
947
948     case DW_CFA_remember_state:
949       gcc_assert (!remember->in_use);
950       *remember = *loc;
951       remember->in_use = 1;
952       break;
953     case DW_CFA_restore_state:
954       gcc_assert (remember->in_use);
955       *loc = *remember;
956       remember->in_use = 0;
957       break;
958
959     default:
960       break;
961     }
962 }
963
964 /* Find the previous value for the CFA.  */
965
966 static void
967 lookup_cfa (dw_cfa_location *loc)
968 {
969   dw_cfi_ref cfi;
970   dw_fde_ref fde;
971   dw_cfa_location remember;
972
973   memset (loc, 0, sizeof (*loc));
974   loc->reg = INVALID_REGNUM;
975   remember = *loc;
976
977   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
978     lookup_cfa_1 (cfi, loc, &remember);
979
980   fde = current_fde ();
981   if (fde)
982     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
983       lookup_cfa_1 (cfi, loc, &remember);
984 }
985
986 /* The current rule for calculating the DWARF2 canonical frame address.  */
987 static dw_cfa_location cfa;
988
989 /* The register used for saving registers to the stack, and its offset
990    from the CFA.  */
991 static dw_cfa_location cfa_store;
992
993 /* The current save location around an epilogue.  */
994 static dw_cfa_location cfa_remember;
995
996 /* The running total of the size of arguments pushed onto the stack.  */
997 static HOST_WIDE_INT args_size;
998
999 /* The last args_size we actually output.  */
1000 static HOST_WIDE_INT old_args_size;
1001
1002 /* Entry point to update the canonical frame address (CFA).
1003    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1004    calculated from REG+OFFSET.  */
1005
1006 void
1007 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1008 {
1009   dw_cfa_location loc;
1010   loc.indirect = 0;
1011   loc.base_offset = 0;
1012   loc.reg = reg;
1013   loc.offset = offset;
1014   def_cfa_1 (label, &loc);
1015 }
1016
1017 /* Determine if two dw_cfa_location structures define the same data.  */
1018
1019 static bool
1020 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1021 {
1022   return (loc1->reg == loc2->reg
1023           && loc1->offset == loc2->offset
1024           && loc1->indirect == loc2->indirect
1025           && (loc1->indirect == 0
1026               || loc1->base_offset == loc2->base_offset));
1027 }
1028
1029 /* This routine does the actual work.  The CFA is now calculated from
1030    the dw_cfa_location structure.  */
1031
1032 static void
1033 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1034 {
1035   dw_cfi_ref cfi;
1036   dw_cfa_location old_cfa, loc;
1037
1038   cfa = *loc_p;
1039   loc = *loc_p;
1040
1041   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1042     cfa_store.offset = loc.offset;
1043
1044   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1045   lookup_cfa (&old_cfa);
1046
1047   /* If nothing changed, no need to issue any call frame instructions.  */
1048   if (cfa_equal_p (&loc, &old_cfa))
1049     return;
1050
1051   cfi = new_cfi ();
1052
1053   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1054     {
1055       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1056          the CFA register did not change but the offset did.  The data
1057          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1058          in the assembler via the .cfi_def_cfa_offset directive.  */
1059       if (loc.offset < 0)
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1061       else
1062         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1063       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1064     }
1065
1066 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1067   else if (loc.offset == old_cfa.offset
1068            && old_cfa.reg != INVALID_REGNUM
1069            && !loc.indirect
1070            && !old_cfa.indirect)
1071     {
1072       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1073          indicating the CFA register has changed to <register> but the
1074          offset has not changed.  */
1075       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1076       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1077     }
1078 #endif
1079
1080   else if (loc.indirect == 0)
1081     {
1082       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1083          indicating the CFA register has changed to <register> with
1084          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1085          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1086          directive.  */
1087       if (loc.offset < 0)
1088         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1089       else
1090         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1091       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1092       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1093     }
1094   else
1095     {
1096       /* Construct a DW_CFA_def_cfa_expression instruction to
1097          calculate the CFA using a full location expression since no
1098          register-offset pair is available.  */
1099       struct dw_loc_descr_struct *loc_list;
1100
1101       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1102       loc_list = build_cfa_loc (&loc, 0);
1103       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1104     }
1105
1106   add_fde_cfi (label, cfi);
1107 }
1108
1109 /* Add the CFI for saving a register.  REG is the CFA column number.
1110    LABEL is passed to add_fde_cfi.
1111    If SREG is -1, the register is saved at OFFSET from the CFA;
1112    otherwise it is saved in SREG.  */
1113
1114 static void
1115 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1116 {
1117   dw_cfi_ref cfi = new_cfi ();
1118   dw_fde_ref fde = current_fde ();
1119
1120   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1121
1122   /* When stack is aligned, store REG using DW_CFA_expression with
1123      FP.  */
1124   if (fde
1125       && fde->stack_realign
1126       && sreg == INVALID_REGNUM)
1127     {
1128       cfi->dw_cfi_opc = DW_CFA_expression;
1129       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1130       cfi->dw_cfi_oprnd2.dw_cfi_loc
1131         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1132     }
1133   else if (sreg == INVALID_REGNUM)
1134     {
1135       if (need_data_align_sf_opcode (offset))
1136         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1137       else if (reg & ~0x3f)
1138         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1139       else
1140         cfi->dw_cfi_opc = DW_CFA_offset;
1141       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1142     }
1143   else if (sreg == reg)
1144     cfi->dw_cfi_opc = DW_CFA_same_value;
1145   else
1146     {
1147       cfi->dw_cfi_opc = DW_CFA_register;
1148       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1149     }
1150
1151   add_fde_cfi (label, cfi);
1152 }
1153
1154 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1155    This CFI tells the unwinder that it needs to restore the window registers
1156    from the previous frame's window save area.
1157
1158    ??? Perhaps we should note in the CIE where windows are saved (instead of
1159    assuming 0(cfa)) and what registers are in the window.  */
1160
1161 void
1162 dwarf2out_window_save (const char *label)
1163 {
1164   dw_cfi_ref cfi = new_cfi ();
1165
1166   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1167   add_fde_cfi (label, cfi);
1168 }
1169
1170 /* Entry point for saving a register to the stack.  REG is the GCC register
1171    number.  LABEL and OFFSET are passed to reg_save.  */
1172
1173 void
1174 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1175 {
1176   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1177 }
1178
1179 /* Entry point for saving the return address in the stack.
1180    LABEL and OFFSET are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1184 {
1185   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1186 }
1187
1188 /* Entry point for saving the return address in a register.
1189    LABEL and SREG are passed to reg_save.  */
1190
1191 void
1192 dwarf2out_return_reg (const char *label, unsigned int sreg)
1193 {
1194   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1195 }
1196
1197 /* Record the initial position of the return address.  RTL is
1198    INCOMING_RETURN_ADDR_RTX.  */
1199
1200 static void
1201 initial_return_save (rtx rtl)
1202 {
1203   unsigned int reg = INVALID_REGNUM;
1204   HOST_WIDE_INT offset = 0;
1205
1206   switch (GET_CODE (rtl))
1207     {
1208     case REG:
1209       /* RA is in a register.  */
1210       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1211       break;
1212
1213     case MEM:
1214       /* RA is on the stack.  */
1215       rtl = XEXP (rtl, 0);
1216       switch (GET_CODE (rtl))
1217         {
1218         case REG:
1219           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1220           offset = 0;
1221           break;
1222
1223         case PLUS:
1224           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1225           offset = INTVAL (XEXP (rtl, 1));
1226           break;
1227
1228         case MINUS:
1229           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1230           offset = -INTVAL (XEXP (rtl, 1));
1231           break;
1232
1233         default:
1234           gcc_unreachable ();
1235         }
1236
1237       break;
1238
1239     case PLUS:
1240       /* The return address is at some offset from any value we can
1241          actually load.  For instance, on the SPARC it is in %i7+8. Just
1242          ignore the offset for now; it doesn't matter for unwinding frames.  */
1243       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1244       initial_return_save (XEXP (rtl, 0));
1245       return;
1246
1247     default:
1248       gcc_unreachable ();
1249     }
1250
1251   if (reg != DWARF_FRAME_RETURN_COLUMN)
1252     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1253 }
1254
1255 /* Given a SET, calculate the amount of stack adjustment it
1256    contains.  */
1257
1258 static HOST_WIDE_INT
1259 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1260                      HOST_WIDE_INT cur_offset)
1261 {
1262   const_rtx src = SET_SRC (pattern);
1263   const_rtx dest = SET_DEST (pattern);
1264   HOST_WIDE_INT offset = 0;
1265   enum rtx_code code;
1266
1267   if (dest == stack_pointer_rtx)
1268     {
1269       code = GET_CODE (src);
1270
1271       /* Assume (set (reg sp) (reg whatever)) sets args_size
1272          level to 0.  */
1273       if (code == REG && src != stack_pointer_rtx)
1274         {
1275           offset = -cur_args_size;
1276 #ifndef STACK_GROWS_DOWNWARD
1277           offset = -offset;
1278 #endif
1279           return offset - cur_offset;
1280         }
1281
1282       if (! (code == PLUS || code == MINUS)
1283           || XEXP (src, 0) != stack_pointer_rtx
1284           || !CONST_INT_P (XEXP (src, 1)))
1285         return 0;
1286
1287       /* (set (reg sp) (plus (reg sp) (const_int))) */
1288       offset = INTVAL (XEXP (src, 1));
1289       if (code == PLUS)
1290         offset = -offset;
1291       return offset;
1292     }
1293
1294   if (MEM_P (src) && !MEM_P (dest))
1295     dest = src;
1296   if (MEM_P (dest))
1297     {
1298       /* (set (mem (pre_dec (reg sp))) (foo)) */
1299       src = XEXP (dest, 0);
1300       code = GET_CODE (src);
1301
1302       switch (code)
1303         {
1304         case PRE_MODIFY:
1305         case POST_MODIFY:
1306           if (XEXP (src, 0) == stack_pointer_rtx)
1307             {
1308               rtx val = XEXP (XEXP (src, 1), 1);
1309               /* We handle only adjustments by constant amount.  */
1310               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1311                           && CONST_INT_P (val));
1312               offset = -INTVAL (val);
1313               break;
1314             }
1315           return 0;
1316
1317         case PRE_DEC:
1318         case POST_DEC:
1319           if (XEXP (src, 0) == stack_pointer_rtx)
1320             {
1321               offset = GET_MODE_SIZE (GET_MODE (dest));
1322               break;
1323             }
1324           return 0;
1325
1326         case PRE_INC:
1327         case POST_INC:
1328           if (XEXP (src, 0) == stack_pointer_rtx)
1329             {
1330               offset = -GET_MODE_SIZE (GET_MODE (dest));
1331               break;
1332             }
1333           return 0;
1334
1335         default:
1336           return 0;
1337         }
1338     }
1339   else
1340     return 0;
1341
1342   return offset;
1343 }
1344
1345 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1346    indexed by INSN_UID.  */
1347
1348 static HOST_WIDE_INT *barrier_args_size;
1349
1350 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1351
1352 static HOST_WIDE_INT
1353 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1354                              VEC (rtx, heap) **next)
1355 {
1356   HOST_WIDE_INT offset = 0;
1357   int i;
1358
1359   if (! RTX_FRAME_RELATED_P (insn))
1360     {
1361       if (prologue_epilogue_contains (insn))
1362         /* Nothing */;
1363       else if (GET_CODE (PATTERN (insn)) == SET)
1364         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1365       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1366                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1367         {
1368           /* There may be stack adjustments inside compound insns.  Search
1369              for them.  */
1370           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1371             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1372               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1373                                              cur_args_size, offset);
1374         }
1375     }
1376   else
1377     {
1378       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1379
1380       if (expr)
1381         {
1382           expr = XEXP (expr, 0);
1383           if (GET_CODE (expr) == PARALLEL
1384               || GET_CODE (expr) == SEQUENCE)
1385             for (i = 1; i < XVECLEN (expr, 0); i++)
1386               {
1387                 rtx elem = XVECEXP (expr, 0, i);
1388
1389                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1390                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1391               }
1392         }
1393     }
1394
1395 #ifndef STACK_GROWS_DOWNWARD
1396   offset = -offset;
1397 #endif
1398
1399   cur_args_size += offset;
1400   if (cur_args_size < 0)
1401     cur_args_size = 0;
1402
1403   if (JUMP_P (insn))
1404     {
1405       rtx dest = JUMP_LABEL (insn);
1406
1407       if (dest)
1408         {
1409           if (barrier_args_size [INSN_UID (dest)] < 0)
1410             {
1411               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1412               VEC_safe_push (rtx, heap, *next, dest);
1413             }
1414         }
1415     }
1416
1417   return cur_args_size;
1418 }
1419
1420 /* Walk the whole function and compute args_size on BARRIERs.  */
1421
1422 static void
1423 compute_barrier_args_size (void)
1424 {
1425   int max_uid = get_max_uid (), i;
1426   rtx insn;
1427   VEC (rtx, heap) *worklist, *next, *tmp;
1428
1429   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1430   for (i = 0; i < max_uid; i++)
1431     barrier_args_size[i] = -1;
1432
1433   worklist = VEC_alloc (rtx, heap, 20);
1434   next = VEC_alloc (rtx, heap, 20);
1435   insn = get_insns ();
1436   barrier_args_size[INSN_UID (insn)] = 0;
1437   VEC_quick_push (rtx, worklist, insn);
1438   for (;;)
1439     {
1440       while (!VEC_empty (rtx, worklist))
1441         {
1442           rtx prev, body, first_insn;
1443           HOST_WIDE_INT cur_args_size;
1444
1445           first_insn = insn = VEC_pop (rtx, worklist);
1446           cur_args_size = barrier_args_size[INSN_UID (insn)];
1447           prev = prev_nonnote_insn (insn);
1448           if (prev && BARRIER_P (prev))
1449             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1450
1451           for (; insn; insn = NEXT_INSN (insn))
1452             {
1453               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1454                 continue;
1455               if (BARRIER_P (insn))
1456                 break;
1457
1458               if (LABEL_P (insn))
1459                 {
1460                   if (insn == first_insn)
1461                     continue;
1462                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1463                     {
1464                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1465                       continue;
1466                     }
1467                   else
1468                     {
1469                       /* The insns starting with this label have been
1470                          already scanned or are in the worklist.  */
1471                       break;
1472                     }
1473                 }
1474
1475               body = PATTERN (insn);
1476               if (GET_CODE (body) == SEQUENCE)
1477                 {
1478                   HOST_WIDE_INT dest_args_size = cur_args_size;
1479                   for (i = 1; i < XVECLEN (body, 0); i++)
1480                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1481                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1482                       dest_args_size
1483                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1484                                                        dest_args_size, &next);
1485                     else
1486                       cur_args_size
1487                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488                                                        cur_args_size, &next);
1489
1490                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1491                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1492                                                  dest_args_size, &next);
1493                   else
1494                     cur_args_size
1495                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496                                                      cur_args_size, &next);
1497                 }
1498               else
1499                 cur_args_size
1500                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1501             }
1502         }
1503
1504       if (VEC_empty (rtx, next))
1505         break;
1506
1507       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1508       tmp = next;
1509       next = worklist;
1510       worklist = tmp;
1511       VEC_truncate (rtx, next, 0);
1512     }
1513
1514   VEC_free (rtx, heap, worklist);
1515   VEC_free (rtx, heap, next);
1516 }
1517
1518 /* Add a CFI to update the running total of the size of arguments
1519    pushed onto the stack.  */
1520
1521 static void
1522 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1523 {
1524   dw_cfi_ref cfi;
1525
1526   if (size == old_args_size)
1527     return;
1528
1529   old_args_size = size;
1530
1531   cfi = new_cfi ();
1532   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1533   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1534   add_fde_cfi (label, cfi);
1535 }
1536
1537 /* Record a stack adjustment of OFFSET bytes.  */
1538
1539 static void
1540 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1541 {
1542   if (cfa.reg == STACK_POINTER_REGNUM)
1543     cfa.offset += offset;
1544
1545   if (cfa_store.reg == STACK_POINTER_REGNUM)
1546     cfa_store.offset += offset;
1547
1548   if (ACCUMULATE_OUTGOING_ARGS)
1549     return;
1550
1551 #ifndef STACK_GROWS_DOWNWARD
1552   offset = -offset;
1553 #endif
1554
1555   args_size += offset;
1556   if (args_size < 0)
1557     args_size = 0;
1558
1559   def_cfa_1 (label, &cfa);
1560   if (flag_asynchronous_unwind_tables)
1561     dwarf2out_args_size (label, args_size);
1562 }
1563
1564 /* Check INSN to see if it looks like a push or a stack adjustment, and
1565    make a note of it if it does.  EH uses this information to find out
1566    how much extra space it needs to pop off the stack.  */
1567
1568 static void
1569 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1570 {
1571   HOST_WIDE_INT offset;
1572   const char *label;
1573   int i;
1574
1575   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1576      with this function.  Proper support would require all frame-related
1577      insns to be marked, and to be able to handle saving state around
1578      epilogues textually in the middle of the function.  */
1579   if (prologue_epilogue_contains (insn))
1580     return;
1581
1582   /* If INSN is an instruction from target of an annulled branch, the
1583      effects are for the target only and so current argument size
1584      shouldn't change at all.  */
1585   if (final_sequence
1586       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1587       && INSN_FROM_TARGET_P (insn))
1588     return;
1589
1590   /* If only calls can throw, and we have a frame pointer,
1591      save up adjustments until we see the CALL_INSN.  */
1592   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1593     {
1594       if (CALL_P (insn) && !after_p)
1595         {
1596           /* Extract the size of the args from the CALL rtx itself.  */
1597           insn = PATTERN (insn);
1598           if (GET_CODE (insn) == PARALLEL)
1599             insn = XVECEXP (insn, 0, 0);
1600           if (GET_CODE (insn) == SET)
1601             insn = SET_SRC (insn);
1602           gcc_assert (GET_CODE (insn) == CALL);
1603           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1604         }
1605       return;
1606     }
1607
1608   if (CALL_P (insn) && !after_p)
1609     {
1610       if (!flag_asynchronous_unwind_tables)
1611         dwarf2out_args_size ("", args_size);
1612       return;
1613     }
1614   else if (BARRIER_P (insn))
1615     {
1616       /* Don't call compute_barrier_args_size () if the only
1617          BARRIER is at the end of function.  */
1618       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1619         compute_barrier_args_size ();
1620       if (barrier_args_size == NULL)
1621         offset = 0;
1622       else
1623         {
1624           offset = barrier_args_size[INSN_UID (insn)];
1625           if (offset < 0)
1626             offset = 0;
1627         }
1628
1629       offset -= args_size;
1630 #ifndef STACK_GROWS_DOWNWARD
1631       offset = -offset;
1632 #endif
1633     }
1634   else if (GET_CODE (PATTERN (insn)) == SET)
1635     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1636   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1637            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1638     {
1639       /* There may be stack adjustments inside compound insns.  Search
1640          for them.  */
1641       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1642         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1643           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1644                                          args_size, offset);
1645     }
1646   else
1647     return;
1648
1649   if (offset == 0)
1650     return;
1651
1652   label = dwarf2out_cfi_label (false);
1653   dwarf2out_stack_adjust (offset, label);
1654 }
1655
1656 /* We delay emitting a register save until either (a) we reach the end
1657    of the prologue or (b) the register is clobbered.  This clusters
1658    register saves so that there are fewer pc advances.  */
1659
1660 struct GTY(()) queued_reg_save {
1661   struct queued_reg_save *next;
1662   rtx reg;
1663   HOST_WIDE_INT cfa_offset;
1664   rtx saved_reg;
1665 };
1666
1667 static GTY(()) struct queued_reg_save *queued_reg_saves;
1668
1669 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1670 struct GTY(()) reg_saved_in_data {
1671   rtx orig_reg;
1672   rtx saved_in_reg;
1673 };
1674
1675 /* A list of registers saved in other registers.
1676    The list intentionally has a small maximum capacity of 4; if your
1677    port needs more than that, you might consider implementing a
1678    more efficient data structure.  */
1679 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1680 static GTY(()) size_t num_regs_saved_in_regs;
1681
1682 static const char *last_reg_save_label;
1683
1684 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1685    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1686
1687 static void
1688 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1689 {
1690   struct queued_reg_save *q;
1691
1692   /* Duplicates waste space, but it's also necessary to remove them
1693      for correctness, since the queue gets output in reverse
1694      order.  */
1695   for (q = queued_reg_saves; q != NULL; q = q->next)
1696     if (REGNO (q->reg) == REGNO (reg))
1697       break;
1698
1699   if (q == NULL)
1700     {
1701       q = ggc_alloc_queued_reg_save ();
1702       q->next = queued_reg_saves;
1703       queued_reg_saves = q;
1704     }
1705
1706   q->reg = reg;
1707   q->cfa_offset = offset;
1708   q->saved_reg = sreg;
1709
1710   last_reg_save_label = label;
1711 }
1712
1713 /* Output all the entries in QUEUED_REG_SAVES.  */
1714
1715 static void
1716 flush_queued_reg_saves (void)
1717 {
1718   struct queued_reg_save *q;
1719
1720   for (q = queued_reg_saves; q; q = q->next)
1721     {
1722       size_t i;
1723       unsigned int reg, sreg;
1724
1725       for (i = 0; i < num_regs_saved_in_regs; i++)
1726         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1727           break;
1728       if (q->saved_reg && i == num_regs_saved_in_regs)
1729         {
1730           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1731           num_regs_saved_in_regs++;
1732         }
1733       if (i != num_regs_saved_in_regs)
1734         {
1735           regs_saved_in_regs[i].orig_reg = q->reg;
1736           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1737         }
1738
1739       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1740       if (q->saved_reg)
1741         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1742       else
1743         sreg = INVALID_REGNUM;
1744       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1745     }
1746
1747   queued_reg_saves = NULL;
1748   last_reg_save_label = NULL;
1749 }
1750
1751 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1752    location for?  Or, does it clobber a register which we've previously
1753    said that some other register is saved in, and for which we now
1754    have a new location for?  */
1755
1756 static bool
1757 clobbers_queued_reg_save (const_rtx insn)
1758 {
1759   struct queued_reg_save *q;
1760
1761   for (q = queued_reg_saves; q; q = q->next)
1762     {
1763       size_t i;
1764       if (modified_in_p (q->reg, insn))
1765         return true;
1766       for (i = 0; i < num_regs_saved_in_regs; i++)
1767         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1768             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1769           return true;
1770     }
1771
1772   return false;
1773 }
1774
1775 /* Entry point for saving the first register into the second.  */
1776
1777 void
1778 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1779 {
1780   size_t i;
1781   unsigned int regno, sregno;
1782
1783   for (i = 0; i < num_regs_saved_in_regs; i++)
1784     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1785       break;
1786   if (i == num_regs_saved_in_regs)
1787     {
1788       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1789       num_regs_saved_in_regs++;
1790     }
1791   regs_saved_in_regs[i].orig_reg = reg;
1792   regs_saved_in_regs[i].saved_in_reg = sreg;
1793
1794   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1795   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1796   reg_save (label, regno, sregno, 0);
1797 }
1798
1799 /* What register, if any, is currently saved in REG?  */
1800
1801 static rtx
1802 reg_saved_in (rtx reg)
1803 {
1804   unsigned int regn = REGNO (reg);
1805   size_t i;
1806   struct queued_reg_save *q;
1807
1808   for (q = queued_reg_saves; q; q = q->next)
1809     if (q->saved_reg && regn == REGNO (q->saved_reg))
1810       return q->reg;
1811
1812   for (i = 0; i < num_regs_saved_in_regs; i++)
1813     if (regs_saved_in_regs[i].saved_in_reg
1814         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1815       return regs_saved_in_regs[i].orig_reg;
1816
1817   return NULL_RTX;
1818 }
1819
1820
1821 /* A temporary register holding an integral value used in adjusting SP
1822    or setting up the store_reg.  The "offset" field holds the integer
1823    value, not an offset.  */
1824 static dw_cfa_location cfa_temp;
1825
1826 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1827
1828 static void
1829 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1830 {
1831   memset (&cfa, 0, sizeof (cfa));
1832
1833   switch (GET_CODE (pat))
1834     {
1835     case PLUS:
1836       cfa.reg = REGNO (XEXP (pat, 0));
1837       cfa.offset = INTVAL (XEXP (pat, 1));
1838       break;
1839
1840     case REG:
1841       cfa.reg = REGNO (pat);
1842       break;
1843
1844     case MEM:
1845       cfa.indirect = 1;
1846       pat = XEXP (pat, 0);
1847       if (GET_CODE (pat) == PLUS)
1848         {
1849           cfa.base_offset = INTVAL (XEXP (pat, 1));
1850           pat = XEXP (pat, 0);
1851         }
1852       cfa.reg = REGNO (pat);
1853       break;
1854
1855     default:
1856       /* Recurse and define an expression.  */
1857       gcc_unreachable ();
1858     }
1859
1860   def_cfa_1 (label, &cfa);
1861 }
1862
1863 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1864
1865 static void
1866 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1867 {
1868   rtx src, dest;
1869
1870   gcc_assert (GET_CODE (pat) == SET);
1871   dest = XEXP (pat, 0);
1872   src = XEXP (pat, 1);
1873
1874   switch (GET_CODE (src))
1875     {
1876     case PLUS:
1877       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1878       cfa.offset -= INTVAL (XEXP (src, 1));
1879       break;
1880
1881     case REG:
1882         break;
1883
1884     default:
1885         gcc_unreachable ();
1886     }
1887
1888   cfa.reg = REGNO (dest);
1889   gcc_assert (cfa.indirect == 0);
1890
1891   def_cfa_1 (label, &cfa);
1892 }
1893
1894 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1895
1896 static void
1897 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1898 {
1899   HOST_WIDE_INT offset;
1900   rtx src, addr, span;
1901
1902   src = XEXP (set, 1);
1903   addr = XEXP (set, 0);
1904   gcc_assert (MEM_P (addr));
1905   addr = XEXP (addr, 0);
1906
1907   /* As documented, only consider extremely simple addresses.  */
1908   switch (GET_CODE (addr))
1909     {
1910     case REG:
1911       gcc_assert (REGNO (addr) == cfa.reg);
1912       offset = -cfa.offset;
1913       break;
1914     case PLUS:
1915       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1916       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1917       break;
1918     default:
1919       gcc_unreachable ();
1920     }
1921
1922   span = targetm.dwarf_register_span (src);
1923
1924   /* ??? We'd like to use queue_reg_save, but we need to come up with
1925      a different flushing heuristic for epilogues.  */
1926   if (!span)
1927     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1928   else
1929     {
1930       /* We have a PARALLEL describing where the contents of SRC live.
1931          Queue register saves for each piece of the PARALLEL.  */
1932       int par_index;
1933       int limit;
1934       HOST_WIDE_INT span_offset = offset;
1935
1936       gcc_assert (GET_CODE (span) == PARALLEL);
1937
1938       limit = XVECLEN (span, 0);
1939       for (par_index = 0; par_index < limit; par_index++)
1940         {
1941           rtx elem = XVECEXP (span, 0, par_index);
1942
1943           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1944                     INVALID_REGNUM, span_offset);
1945           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1946         }
1947     }
1948 }
1949
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1951
1952 static void
1953 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1954 {
1955   rtx src, dest;
1956   unsigned sregno, dregno;
1957
1958   src = XEXP (set, 1);
1959   dest = XEXP (set, 0);
1960
1961   if (src == pc_rtx)
1962     sregno = DWARF_FRAME_RETURN_COLUMN;
1963   else
1964     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1965
1966   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1967
1968   /* ??? We'd like to use queue_reg_save, but we need to come up with
1969      a different flushing heuristic for epilogues.  */
1970   reg_save (label, sregno, dregno, 0);
1971 }
1972
1973 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1974
1975 static void
1976 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1977 {
1978   rtx src, dest, span;
1979   dw_cfi_ref cfi = new_cfi ();
1980
1981   dest = SET_DEST (set);
1982   src = SET_SRC (set);
1983
1984   gcc_assert (REG_P (src));
1985   gcc_assert (MEM_P (dest));
1986
1987   span = targetm.dwarf_register_span (src);
1988   gcc_assert (!span);
1989
1990   cfi->dw_cfi_opc = DW_CFA_expression;
1991   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1992   cfi->dw_cfi_oprnd2.dw_cfi_loc
1993     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1994                           VAR_INIT_STATUS_INITIALIZED);
1995
1996   /* ??? We'd like to use queue_reg_save, were the interface different,
1997      and, as above, we could manage flushing for epilogues.  */
1998   add_fde_cfi (label, cfi);
1999 }
2000
2001 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2002
2003 static void
2004 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2005 {
2006   dw_cfi_ref cfi = new_cfi ();
2007   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2008
2009   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2010   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2011
2012   add_fde_cfi (label, cfi);
2013 }
2014
2015 /* Record call frame debugging information for an expression EXPR,
2016    which either sets SP or FP (adjusting how we calculate the frame
2017    address) or saves a register to the stack or another register.
2018    LABEL indicates the address of EXPR.
2019
2020    This function encodes a state machine mapping rtxes to actions on
2021    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2022    users need not read the source code.
2023
2024   The High-Level Picture
2025
2026   Changes in the register we use to calculate the CFA: Currently we
2027   assume that if you copy the CFA register into another register, we
2028   should take the other one as the new CFA register; this seems to
2029   work pretty well.  If it's wrong for some target, it's simple
2030   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2031
2032   Changes in the register we use for saving registers to the stack:
2033   This is usually SP, but not always.  Again, we deduce that if you
2034   copy SP into another register (and SP is not the CFA register),
2035   then the new register is the one we will be using for register
2036   saves.  This also seems to work.
2037
2038   Register saves: There's not much guesswork about this one; if
2039   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2040   register save, and the register used to calculate the destination
2041   had better be the one we think we're using for this purpose.
2042   It's also assumed that a copy from a call-saved register to another
2043   register is saving that register if RTX_FRAME_RELATED_P is set on
2044   that instruction.  If the copy is from a call-saved register to
2045   the *same* register, that means that the register is now the same
2046   value as in the caller.
2047
2048   Except: If the register being saved is the CFA register, and the
2049   offset is nonzero, we are saving the CFA, so we assume we have to
2050   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2051   the intent is to save the value of SP from the previous frame.
2052
2053   In addition, if a register has previously been saved to a different
2054   register,
2055
2056   Invariants / Summaries of Rules
2057
2058   cfa          current rule for calculating the CFA.  It usually
2059                consists of a register and an offset.
2060   cfa_store    register used by prologue code to save things to the stack
2061                cfa_store.offset is the offset from the value of
2062                cfa_store.reg to the actual CFA
2063   cfa_temp     register holding an integral value.  cfa_temp.offset
2064                stores the value, which will be used to adjust the
2065                stack pointer.  cfa_temp is also used like cfa_store,
2066                to track stores to the stack via fp or a temp reg.
2067
2068   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2069                with cfa.reg as the first operand changes the cfa.reg and its
2070                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2071                cfa_temp.offset.
2072
2073   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2074                expression yielding a constant.  This sets cfa_temp.reg
2075                and cfa_temp.offset.
2076
2077   Rule 5:      Create a new register cfa_store used to save items to the
2078                stack.
2079
2080   Rules 10-14: Save a register to the stack.  Define offset as the
2081                difference of the original location and cfa_store's
2082                location (or cfa_temp's location if cfa_temp is used).
2083
2084   Rules 16-20: If AND operation happens on sp in prologue, we assume
2085                stack is realigned.  We will use a group of DW_OP_XXX
2086                expressions to represent the location of the stored
2087                register instead of CFA+offset.
2088
2089   The Rules
2090
2091   "{a,b}" indicates a choice of a xor b.
2092   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2093
2094   Rule 1:
2095   (set <reg1> <reg2>:cfa.reg)
2096   effects: cfa.reg = <reg1>
2097            cfa.offset unchanged
2098            cfa_temp.reg = <reg1>
2099            cfa_temp.offset = cfa.offset
2100
2101   Rule 2:
2102   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2103                               {<const_int>,<reg>:cfa_temp.reg}))
2104   effects: cfa.reg = sp if fp used
2105            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2106            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2107              if cfa_store.reg==sp
2108
2109   Rule 3:
2110   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2111   effects: cfa.reg = fp
2112            cfa_offset += +/- <const_int>
2113
2114   Rule 4:
2115   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2116   constraints: <reg1> != fp
2117                <reg1> != sp
2118   effects: cfa.reg = <reg1>
2119            cfa_temp.reg = <reg1>
2120            cfa_temp.offset = cfa.offset
2121
2122   Rule 5:
2123   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2124   constraints: <reg1> != fp
2125                <reg1> != sp
2126   effects: cfa_store.reg = <reg1>
2127            cfa_store.offset = cfa.offset - cfa_temp.offset
2128
2129   Rule 6:
2130   (set <reg> <const_int>)
2131   effects: cfa_temp.reg = <reg>
2132            cfa_temp.offset = <const_int>
2133
2134   Rule 7:
2135   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2136   effects: cfa_temp.reg = <reg1>
2137            cfa_temp.offset |= <const_int>
2138
2139   Rule 8:
2140   (set <reg> (high <exp>))
2141   effects: none
2142
2143   Rule 9:
2144   (set <reg> (lo_sum <exp> <const_int>))
2145   effects: cfa_temp.reg = <reg>
2146            cfa_temp.offset = <const_int>
2147
2148   Rule 10:
2149   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2150   effects: cfa_store.offset -= <const_int>
2151            cfa.offset = cfa_store.offset if cfa.reg == sp
2152            cfa.reg = sp
2153            cfa.base_offset = -cfa_store.offset
2154
2155   Rule 11:
2156   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2157   effects: cfa_store.offset += -/+ mode_size(mem)
2158            cfa.offset = cfa_store.offset if cfa.reg == sp
2159            cfa.reg = sp
2160            cfa.base_offset = -cfa_store.offset
2161
2162   Rule 12:
2163   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2164
2165        <reg2>)
2166   effects: cfa.reg = <reg1>
2167            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2168
2169   Rule 13:
2170   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2171   effects: cfa.reg = <reg1>
2172            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2173
2174   Rule 14:
2175   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2176   effects: cfa.reg = <reg1>
2177            cfa.base_offset = -cfa_temp.offset
2178            cfa_temp.offset -= mode_size(mem)
2179
2180   Rule 15:
2181   (set <reg> {unspec, unspec_volatile})
2182   effects: target-dependent
2183
2184   Rule 16:
2185   (set sp (and: sp <const_int>))
2186   constraints: cfa_store.reg == sp
2187   effects: current_fde.stack_realign = 1
2188            cfa_store.offset = 0
2189            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2190
2191   Rule 17:
2192   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2193   effects: cfa_store.offset += -/+ mode_size(mem)
2194
2195   Rule 18:
2196   (set (mem ({pre_inc, pre_dec} sp)) fp)
2197   constraints: fde->stack_realign == 1
2198   effects: cfa_store.offset = 0
2199            cfa.reg != HARD_FRAME_POINTER_REGNUM
2200
2201   Rule 19:
2202   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2203   constraints: fde->stack_realign == 1
2204                && cfa.offset == 0
2205                && cfa.indirect == 0
2206                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2207   effects: Use DW_CFA_def_cfa_expression to define cfa
2208            cfa.reg == fde->drap_reg  */
2209
2210 static void
2211 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2212 {
2213   rtx src, dest, span;
2214   HOST_WIDE_INT offset;
2215   dw_fde_ref fde;
2216
2217   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2218      the PARALLEL independently. The first element is always processed if
2219      it is a SET. This is for backward compatibility.   Other elements
2220      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2221      flag is set in them.  */
2222   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2223     {
2224       int par_index;
2225       int limit = XVECLEN (expr, 0);
2226       rtx elem;
2227
2228       /* PARALLELs have strict read-modify-write semantics, so we
2229          ought to evaluate every rvalue before changing any lvalue.
2230          It's cumbersome to do that in general, but there's an
2231          easy approximation that is enough for all current users:
2232          handle register saves before register assignments.  */
2233       if (GET_CODE (expr) == PARALLEL)
2234         for (par_index = 0; par_index < limit; par_index++)
2235           {
2236             elem = XVECEXP (expr, 0, par_index);
2237             if (GET_CODE (elem) == SET
2238                 && MEM_P (SET_DEST (elem))
2239                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2240               dwarf2out_frame_debug_expr (elem, label);
2241           }
2242
2243       for (par_index = 0; par_index < limit; par_index++)
2244         {
2245           elem = XVECEXP (expr, 0, par_index);
2246           if (GET_CODE (elem) == SET
2247               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2248               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2249             dwarf2out_frame_debug_expr (elem, label);
2250           else if (GET_CODE (elem) == SET
2251                    && par_index != 0
2252                    && !RTX_FRAME_RELATED_P (elem))
2253             {
2254               /* Stack adjustment combining might combine some post-prologue
2255                  stack adjustment into a prologue stack adjustment.  */
2256               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2257
2258               if (offset != 0)
2259                 dwarf2out_stack_adjust (offset, label);
2260             }
2261         }
2262       return;
2263     }
2264
2265   gcc_assert (GET_CODE (expr) == SET);
2266
2267   src = SET_SRC (expr);
2268   dest = SET_DEST (expr);
2269
2270   if (REG_P (src))
2271     {
2272       rtx rsi = reg_saved_in (src);
2273       if (rsi)
2274         src = rsi;
2275     }
2276
2277   fde = current_fde ();
2278
2279   switch (GET_CODE (dest))
2280     {
2281     case REG:
2282       switch (GET_CODE (src))
2283         {
2284           /* Setting FP from SP.  */
2285         case REG:
2286           if (cfa.reg == (unsigned) REGNO (src))
2287             {
2288               /* Rule 1 */
2289               /* Update the CFA rule wrt SP or FP.  Make sure src is
2290                  relative to the current CFA register.
2291
2292                  We used to require that dest be either SP or FP, but the
2293                  ARM copies SP to a temporary register, and from there to
2294                  FP.  So we just rely on the backends to only set
2295                  RTX_FRAME_RELATED_P on appropriate insns.  */
2296               cfa.reg = REGNO (dest);
2297               cfa_temp.reg = cfa.reg;
2298               cfa_temp.offset = cfa.offset;
2299             }
2300           else
2301             {
2302               /* Saving a register in a register.  */
2303               gcc_assert (!fixed_regs [REGNO (dest)]
2304                           /* For the SPARC and its register window.  */
2305                           || (DWARF_FRAME_REGNUM (REGNO (src))
2306                               == DWARF_FRAME_RETURN_COLUMN));
2307
2308               /* After stack is aligned, we can only save SP in FP
2309                  if drap register is used.  In this case, we have
2310                  to restore stack pointer with the CFA value and we
2311                  don't generate this DWARF information.  */
2312               if (fde
2313                   && fde->stack_realign
2314                   && REGNO (src) == STACK_POINTER_REGNUM)
2315                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2316                             && fde->drap_reg != INVALID_REGNUM
2317                             && cfa.reg != REGNO (src));
2318               else
2319                 queue_reg_save (label, src, dest, 0);
2320             }
2321           break;
2322
2323         case PLUS:
2324         case MINUS:
2325         case LO_SUM:
2326           if (dest == stack_pointer_rtx)
2327             {
2328               /* Rule 2 */
2329               /* Adjusting SP.  */
2330               switch (GET_CODE (XEXP (src, 1)))
2331                 {
2332                 case CONST_INT:
2333                   offset = INTVAL (XEXP (src, 1));
2334                   break;
2335                 case REG:
2336                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2337                               == cfa_temp.reg);
2338                   offset = cfa_temp.offset;
2339                   break;
2340                 default:
2341                   gcc_unreachable ();
2342                 }
2343
2344               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2345                 {
2346                   /* Restoring SP from FP in the epilogue.  */
2347                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2348                   cfa.reg = STACK_POINTER_REGNUM;
2349                 }
2350               else if (GET_CODE (src) == LO_SUM)
2351                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2352                 ;
2353               else
2354                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2355
2356               if (GET_CODE (src) != MINUS)
2357                 offset = -offset;
2358               if (cfa.reg == STACK_POINTER_REGNUM)
2359                 cfa.offset += offset;
2360               if (cfa_store.reg == STACK_POINTER_REGNUM)
2361                 cfa_store.offset += offset;
2362             }
2363           else if (dest == hard_frame_pointer_rtx)
2364             {
2365               /* Rule 3 */
2366               /* Either setting the FP from an offset of the SP,
2367                  or adjusting the FP */
2368               gcc_assert (frame_pointer_needed);
2369
2370               gcc_assert (REG_P (XEXP (src, 0))
2371                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2372                           && CONST_INT_P (XEXP (src, 1)));
2373               offset = INTVAL (XEXP (src, 1));
2374               if (GET_CODE (src) != MINUS)
2375                 offset = -offset;
2376               cfa.offset += offset;
2377               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2378             }
2379           else
2380             {
2381               gcc_assert (GET_CODE (src) != MINUS);
2382
2383               /* Rule 4 */
2384               if (REG_P (XEXP (src, 0))
2385                   && REGNO (XEXP (src, 0)) == cfa.reg
2386                   && CONST_INT_P (XEXP (src, 1)))
2387                 {
2388                   /* Setting a temporary CFA register that will be copied
2389                      into the FP later on.  */
2390                   offset = - INTVAL (XEXP (src, 1));
2391                   cfa.offset += offset;
2392                   cfa.reg = REGNO (dest);
2393                   /* Or used to save regs to the stack.  */
2394                   cfa_temp.reg = cfa.reg;
2395                   cfa_temp.offset = cfa.offset;
2396                 }
2397
2398               /* Rule 5 */
2399               else if (REG_P (XEXP (src, 0))
2400                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2401                        && XEXP (src, 1) == stack_pointer_rtx)
2402                 {
2403                   /* Setting a scratch register that we will use instead
2404                      of SP for saving registers to the stack.  */
2405                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2406                   cfa_store.reg = REGNO (dest);
2407                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2408                 }
2409
2410               /* Rule 9 */
2411               else if (GET_CODE (src) == LO_SUM
2412                        && CONST_INT_P (XEXP (src, 1)))
2413                 {
2414                   cfa_temp.reg = REGNO (dest);
2415                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2416                 }
2417               else
2418                 gcc_unreachable ();
2419             }
2420           break;
2421
2422           /* Rule 6 */
2423         case CONST_INT:
2424           cfa_temp.reg = REGNO (dest);
2425           cfa_temp.offset = INTVAL (src);
2426           break;
2427
2428           /* Rule 7 */
2429         case IOR:
2430           gcc_assert (REG_P (XEXP (src, 0))
2431                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2432                       && CONST_INT_P (XEXP (src, 1)));
2433
2434           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2435             cfa_temp.reg = REGNO (dest);
2436           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2437           break;
2438
2439           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2440              which will fill in all of the bits.  */
2441           /* Rule 8 */
2442         case HIGH:
2443           break;
2444
2445           /* Rule 15 */
2446         case UNSPEC:
2447         case UNSPEC_VOLATILE:
2448           gcc_assert (targetm.dwarf_handle_frame_unspec);
2449           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2450           return;
2451
2452           /* Rule 16 */
2453         case AND:
2454           /* If this AND operation happens on stack pointer in prologue,
2455              we assume the stack is realigned and we extract the
2456              alignment.  */
2457           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2458             {
2459               /* We interpret reg_save differently with stack_realign set.
2460                  Thus we must flush whatever we have queued first.  */
2461               flush_queued_reg_saves ();
2462
2463               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2464               fde->stack_realign = 1;
2465               fde->stack_realignment = INTVAL (XEXP (src, 1));
2466               cfa_store.offset = 0;
2467
2468               if (cfa.reg != STACK_POINTER_REGNUM
2469                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2470                 fde->drap_reg = cfa.reg;
2471             }
2472           return;
2473
2474         default:
2475           gcc_unreachable ();
2476         }
2477
2478       def_cfa_1 (label, &cfa);
2479       break;
2480
2481     case MEM:
2482
2483       /* Saving a register to the stack.  Make sure dest is relative to the
2484          CFA register.  */
2485       switch (GET_CODE (XEXP (dest, 0)))
2486         {
2487           /* Rule 10 */
2488           /* With a push.  */
2489         case PRE_MODIFY:
2490           /* We can't handle variable size modifications.  */
2491           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2492                       == CONST_INT);
2493           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2494
2495           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2496                       && cfa_store.reg == STACK_POINTER_REGNUM);
2497
2498           cfa_store.offset += offset;
2499           if (cfa.reg == STACK_POINTER_REGNUM)
2500             cfa.offset = cfa_store.offset;
2501
2502           offset = -cfa_store.offset;
2503           break;
2504
2505           /* Rule 11 */
2506         case PRE_INC:
2507         case PRE_DEC:
2508           offset = GET_MODE_SIZE (GET_MODE (dest));
2509           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2510             offset = -offset;
2511
2512           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2513                        == STACK_POINTER_REGNUM)
2514                       && cfa_store.reg == STACK_POINTER_REGNUM);
2515
2516           cfa_store.offset += offset;
2517
2518           /* Rule 18: If stack is aligned, we will use FP as a
2519              reference to represent the address of the stored
2520              regiser.  */
2521           if (fde
2522               && fde->stack_realign
2523               && src == hard_frame_pointer_rtx)
2524             {
2525               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2526               cfa_store.offset = 0;
2527             }
2528
2529           if (cfa.reg == STACK_POINTER_REGNUM)
2530             cfa.offset = cfa_store.offset;
2531
2532           offset = -cfa_store.offset;
2533           break;
2534
2535           /* Rule 12 */
2536           /* With an offset.  */
2537         case PLUS:
2538         case MINUS:
2539         case LO_SUM:
2540           {
2541             int regno;
2542
2543             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2544                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2545             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2546             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2547               offset = -offset;
2548
2549             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2550
2551             if (cfa_store.reg == (unsigned) regno)
2552               offset -= cfa_store.offset;
2553             else
2554               {
2555                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2556                 offset -= cfa_temp.offset;
2557               }
2558           }
2559           break;
2560
2561           /* Rule 13 */
2562           /* Without an offset.  */
2563         case REG:
2564           {
2565             int regno = REGNO (XEXP (dest, 0));
2566
2567             if (cfa_store.reg == (unsigned) regno)
2568               offset = -cfa_store.offset;
2569             else
2570               {
2571                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2572                 offset = -cfa_temp.offset;
2573               }
2574           }
2575           break;
2576
2577           /* Rule 14 */
2578         case POST_INC:
2579           gcc_assert (cfa_temp.reg
2580                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2581           offset = -cfa_temp.offset;
2582           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2583           break;
2584
2585         default:
2586           gcc_unreachable ();
2587         }
2588
2589         /* Rule 17 */
2590         /* If the source operand of this MEM operation is not a
2591            register, basically the source is return address.  Here
2592            we only care how much stack grew and we don't save it.  */
2593       if (!REG_P (src))
2594         break;
2595
2596       if (REGNO (src) != STACK_POINTER_REGNUM
2597           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2598           && (unsigned) REGNO (src) == cfa.reg)
2599         {
2600           /* We're storing the current CFA reg into the stack.  */
2601
2602           if (cfa.offset == 0)
2603             {
2604               /* Rule 19 */
2605               /* If stack is aligned, putting CFA reg into stack means
2606                  we can no longer use reg + offset to represent CFA.
2607                  Here we use DW_CFA_def_cfa_expression instead.  The
2608                  result of this expression equals to the original CFA
2609                  value.  */
2610               if (fde
2611                   && fde->stack_realign
2612                   && cfa.indirect == 0
2613                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2614                 {
2615                   dw_cfa_location cfa_exp;
2616
2617                   gcc_assert (fde->drap_reg == cfa.reg);
2618
2619                   cfa_exp.indirect = 1;
2620                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2621                   cfa_exp.base_offset = offset;
2622                   cfa_exp.offset = 0;
2623
2624                   fde->drap_reg_saved = 1;
2625
2626                   def_cfa_1 (label, &cfa_exp);
2627                   break;
2628                 }
2629
2630               /* If the source register is exactly the CFA, assume
2631                  we're saving SP like any other register; this happens
2632                  on the ARM.  */
2633               def_cfa_1 (label, &cfa);
2634               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2635               break;
2636             }
2637           else
2638             {
2639               /* Otherwise, we'll need to look in the stack to
2640                  calculate the CFA.  */
2641               rtx x = XEXP (dest, 0);
2642
2643               if (!REG_P (x))
2644                 x = XEXP (x, 0);
2645               gcc_assert (REG_P (x));
2646
2647               cfa.reg = REGNO (x);
2648               cfa.base_offset = offset;
2649               cfa.indirect = 1;
2650               def_cfa_1 (label, &cfa);
2651               break;
2652             }
2653         }
2654
2655       def_cfa_1 (label, &cfa);
2656       {
2657         span = targetm.dwarf_register_span (src);
2658
2659         if (!span)
2660           queue_reg_save (label, src, NULL_RTX, offset);
2661         else
2662           {
2663             /* We have a PARALLEL describing where the contents of SRC
2664                live.  Queue register saves for each piece of the
2665                PARALLEL.  */
2666             int par_index;
2667             int limit;
2668             HOST_WIDE_INT span_offset = offset;
2669
2670             gcc_assert (GET_CODE (span) == PARALLEL);
2671
2672             limit = XVECLEN (span, 0);
2673             for (par_index = 0; par_index < limit; par_index++)
2674               {
2675                 rtx elem = XVECEXP (span, 0, par_index);
2676
2677                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2678                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2679               }
2680           }
2681       }
2682       break;
2683
2684     default:
2685       gcc_unreachable ();
2686     }
2687 }
2688
2689 /* Record call frame debugging information for INSN, which either
2690    sets SP or FP (adjusting how we calculate the frame address) or saves a
2691    register to the stack.  If INSN is NULL_RTX, initialize our state.
2692
2693    If AFTER_P is false, we're being called before the insn is emitted,
2694    otherwise after.  Call instructions get invoked twice.  */
2695
2696 void
2697 dwarf2out_frame_debug (rtx insn, bool after_p)
2698 {
2699   const char *label;
2700   rtx note, n;
2701   bool handled_one = false;
2702
2703   if (insn == NULL_RTX)
2704     {
2705       size_t i;
2706
2707       /* Flush any queued register saves.  */
2708       flush_queued_reg_saves ();
2709
2710       /* Set up state for generating call frame debug info.  */
2711       lookup_cfa (&cfa);
2712       gcc_assert (cfa.reg
2713                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2714
2715       cfa.reg = STACK_POINTER_REGNUM;
2716       cfa_store = cfa;
2717       cfa_temp.reg = -1;
2718       cfa_temp.offset = 0;
2719
2720       for (i = 0; i < num_regs_saved_in_regs; i++)
2721         {
2722           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2723           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2724         }
2725       num_regs_saved_in_regs = 0;
2726
2727       if (barrier_args_size)
2728         {
2729           XDELETEVEC (barrier_args_size);
2730           barrier_args_size = NULL;
2731         }
2732       return;
2733     }
2734
2735   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2736     flush_queued_reg_saves ();
2737
2738   if (!RTX_FRAME_RELATED_P (insn))
2739     {
2740       /* ??? This should be done unconditionally since stack adjustments
2741          matter if the stack pointer is not the CFA register anymore but
2742          is still used to save registers.  */
2743       if (!ACCUMULATE_OUTGOING_ARGS)
2744         dwarf2out_notice_stack_adjust (insn, after_p);
2745       return;
2746     }
2747
2748   label = dwarf2out_cfi_label (false);
2749
2750   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2751     switch (REG_NOTE_KIND (note))
2752       {
2753       case REG_FRAME_RELATED_EXPR:
2754         insn = XEXP (note, 0);
2755         goto found;
2756
2757       case REG_CFA_DEF_CFA:
2758         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2759         handled_one = true;
2760         break;
2761
2762       case REG_CFA_ADJUST_CFA:
2763         n = XEXP (note, 0);
2764         if (n == NULL)
2765           {
2766             n = PATTERN (insn);
2767             if (GET_CODE (n) == PARALLEL)
2768               n = XVECEXP (n, 0, 0);
2769           }
2770         dwarf2out_frame_debug_adjust_cfa (n, label);
2771         handled_one = true;
2772         break;
2773
2774       case REG_CFA_OFFSET:
2775         n = XEXP (note, 0);
2776         if (n == NULL)
2777           n = single_set (insn);
2778         dwarf2out_frame_debug_cfa_offset (n, label);
2779         handled_one = true;
2780         break;
2781
2782       case REG_CFA_REGISTER:
2783         n = XEXP (note, 0);
2784         if (n == NULL)
2785           {
2786             n = PATTERN (insn);
2787             if (GET_CODE (n) == PARALLEL)
2788               n = XVECEXP (n, 0, 0);
2789           }
2790         dwarf2out_frame_debug_cfa_register (n, label);
2791         handled_one = true;
2792         break;
2793
2794       case REG_CFA_EXPRESSION:
2795         n = XEXP (note, 0);
2796         if (n == NULL)
2797           n = single_set (insn);
2798         dwarf2out_frame_debug_cfa_expression (n, label);
2799         handled_one = true;
2800         break;
2801
2802       case REG_CFA_RESTORE:
2803         n = XEXP (note, 0);
2804         if (n == NULL)
2805           {
2806             n = PATTERN (insn);
2807             if (GET_CODE (n) == PARALLEL)
2808               n = XVECEXP (n, 0, 0);
2809             n = XEXP (n, 0);
2810           }
2811         dwarf2out_frame_debug_cfa_restore (n, label);
2812         handled_one = true;
2813         break;
2814
2815       case REG_CFA_SET_VDRAP:
2816         n = XEXP (note, 0);
2817         if (REG_P (n))
2818           {
2819             dw_fde_ref fde = current_fde ();
2820             if (fde)
2821               {
2822                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2823                 if (REG_P (n))
2824                   fde->vdrap_reg = REGNO (n);
2825               }
2826           }
2827         handled_one = true;
2828         break;
2829
2830       default:
2831         break;
2832       }
2833   if (handled_one)
2834     return;
2835
2836   insn = PATTERN (insn);
2837  found:
2838   dwarf2out_frame_debug_expr (insn, label);
2839
2840   /* Check again.  A parallel can save and update the same register.
2841      We could probably check just once, here, but this is safer than
2842      removing the check above.  */
2843   if (clobbers_queued_reg_save (insn))
2844     flush_queued_reg_saves ();
2845 }
2846
2847 /* Determine if we need to save and restore CFI information around this
2848    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2849    we do need to save/restore, then emit the save now, and insert a
2850    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2851
2852 void
2853 dwarf2out_cfi_begin_epilogue (rtx insn)
2854 {
2855   bool saw_frp = false;
2856   rtx i;
2857
2858   /* Scan forward to the return insn, noticing if there are possible
2859      frame related insns.  */
2860   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2861     {
2862       if (!INSN_P (i))
2863         continue;
2864
2865       /* Look for both regular and sibcalls to end the block.  */
2866       if (returnjump_p (i))
2867         break;
2868       if (CALL_P (i) && SIBLING_CALL_P (i))
2869         break;
2870
2871       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2872         {
2873           int idx;
2874           rtx seq = PATTERN (i);
2875
2876           if (returnjump_p (XVECEXP (seq, 0, 0)))
2877             break;
2878           if (CALL_P (XVECEXP (seq, 0, 0))
2879               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2880             break;
2881
2882           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2883             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2884               saw_frp = true;
2885         }
2886
2887       if (RTX_FRAME_RELATED_P (i))
2888         saw_frp = true;
2889     }
2890
2891   /* If the port doesn't emit epilogue unwind info, we don't need a
2892      save/restore pair.  */
2893   if (!saw_frp)
2894     return;
2895
2896   /* Otherwise, search forward to see if the return insn was the last
2897      basic block of the function.  If so, we don't need save/restore.  */
2898   gcc_assert (i != NULL);
2899   i = next_real_insn (i);
2900   if (i == NULL)
2901     return;
2902
2903   /* Insert the restore before that next real insn in the stream, and before
2904      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2905      properly nested.  This should be after any label or alignment.  This
2906      will be pushed into the CFI stream by the function below.  */
2907   while (1)
2908     {
2909       rtx p = PREV_INSN (i);
2910       if (!NOTE_P (p))
2911         break;
2912       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2913         break;
2914       i = p;
2915     }
2916   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2917
2918   emit_cfa_remember = true;
2919
2920   /* And emulate the state save.  */
2921   gcc_assert (!cfa_remember.in_use);
2922   cfa_remember = cfa;
2923   cfa_remember.in_use = 1;
2924 }
2925
2926 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2927    required.  */
2928
2929 void
2930 dwarf2out_frame_debug_restore_state (void)
2931 {
2932   dw_cfi_ref cfi = new_cfi ();
2933   const char *label = dwarf2out_cfi_label (false);
2934
2935   cfi->dw_cfi_opc = DW_CFA_restore_state;
2936   add_fde_cfi (label, cfi);
2937
2938   gcc_assert (cfa_remember.in_use);
2939   cfa = cfa_remember;
2940   cfa_remember.in_use = 0;
2941 }
2942
2943 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2944 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2945  (enum dwarf_call_frame_info cfi);
2946
2947 static enum dw_cfi_oprnd_type
2948 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2949 {
2950   switch (cfi)
2951     {
2952     case DW_CFA_nop:
2953     case DW_CFA_GNU_window_save:
2954     case DW_CFA_remember_state:
2955     case DW_CFA_restore_state:
2956       return dw_cfi_oprnd_unused;
2957
2958     case DW_CFA_set_loc:
2959     case DW_CFA_advance_loc1:
2960     case DW_CFA_advance_loc2:
2961     case DW_CFA_advance_loc4:
2962     case DW_CFA_MIPS_advance_loc8:
2963       return dw_cfi_oprnd_addr;
2964
2965     case DW_CFA_offset:
2966     case DW_CFA_offset_extended:
2967     case DW_CFA_def_cfa:
2968     case DW_CFA_offset_extended_sf:
2969     case DW_CFA_def_cfa_sf:
2970     case DW_CFA_restore:
2971     case DW_CFA_restore_extended:
2972     case DW_CFA_undefined:
2973     case DW_CFA_same_value:
2974     case DW_CFA_def_cfa_register:
2975     case DW_CFA_register:
2976     case DW_CFA_expression:
2977       return dw_cfi_oprnd_reg_num;
2978
2979     case DW_CFA_def_cfa_offset:
2980     case DW_CFA_GNU_args_size:
2981     case DW_CFA_def_cfa_offset_sf:
2982       return dw_cfi_oprnd_offset;
2983
2984     case DW_CFA_def_cfa_expression:
2985       return dw_cfi_oprnd_loc;
2986
2987     default:
2988       gcc_unreachable ();
2989     }
2990 }
2991
2992 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2993 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2994  (enum dwarf_call_frame_info cfi);
2995
2996 static enum dw_cfi_oprnd_type
2997 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2998 {
2999   switch (cfi)
3000     {
3001     case DW_CFA_def_cfa:
3002     case DW_CFA_def_cfa_sf:
3003     case DW_CFA_offset:
3004     case DW_CFA_offset_extended_sf:
3005     case DW_CFA_offset_extended:
3006       return dw_cfi_oprnd_offset;
3007
3008     case DW_CFA_register:
3009       return dw_cfi_oprnd_reg_num;
3010
3011     case DW_CFA_expression:
3012       return dw_cfi_oprnd_loc;
3013
3014     default:
3015       return dw_cfi_oprnd_unused;
3016     }
3017 }
3018
3019 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3020    switch to the data section instead, and write out a synthetic start label
3021    for collect2 the first time around.  */
3022
3023 static void
3024 switch_to_eh_frame_section (bool back)
3025 {
3026   tree label;
3027
3028 #ifdef EH_FRAME_SECTION_NAME
3029   if (eh_frame_section == 0)
3030     {
3031       int flags;
3032
3033       if (EH_TABLES_CAN_BE_READ_ONLY)
3034         {
3035           int fde_encoding;
3036           int per_encoding;
3037           int lsda_encoding;
3038
3039           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3040                                                        /*global=*/0);
3041           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3042                                                        /*global=*/1);
3043           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3044                                                         /*global=*/0);
3045           flags = ((! flag_pic
3046                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3047                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3048                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3049                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3050                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3051                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3052                    ? 0 : SECTION_WRITE);
3053         }
3054       else
3055         flags = SECTION_WRITE;
3056       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3057     }
3058 #endif /* EH_FRAME_SECTION_NAME */
3059
3060   if (eh_frame_section)
3061     switch_to_section (eh_frame_section);
3062   else
3063     {
3064       /* We have no special eh_frame section.  Put the information in
3065          the data section and emit special labels to guide collect2.  */
3066       switch_to_section (data_section);
3067
3068       if (!back)
3069         {
3070           label = get_file_function_name ("F");
3071           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3072           targetm.asm_out.globalize_label (asm_out_file,
3073                                            IDENTIFIER_POINTER (label));
3074           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3075         }
3076     }
3077 }
3078
3079 /* Switch [BACK] to the eh or debug frame table section, depending on
3080    FOR_EH.  */
3081
3082 static void
3083 switch_to_frame_table_section (int for_eh, bool back)
3084 {
3085   if (for_eh)
3086     switch_to_eh_frame_section (back);
3087   else
3088     {
3089       if (!debug_frame_section)
3090         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3091                                            SECTION_DEBUG, NULL);
3092       switch_to_section (debug_frame_section);
3093     }
3094 }
3095
3096 /* Output a Call Frame Information opcode and its operand(s).  */
3097
3098 static void
3099 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3100 {
3101   unsigned long r;
3102   HOST_WIDE_INT off;
3103
3104   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3105     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3106                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3107                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3108                          ((unsigned HOST_WIDE_INT)
3109                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3110   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3111     {
3112       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3113       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3114                            "DW_CFA_offset, column %#lx", r);
3115       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3116       dw2_asm_output_data_uleb128 (off, NULL);
3117     }
3118   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3119     {
3120       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3121       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3122                            "DW_CFA_restore, column %#lx", r);
3123     }
3124   else
3125     {
3126       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3127                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3128
3129       switch (cfi->dw_cfi_opc)
3130         {
3131         case DW_CFA_set_loc:
3132           if (for_eh)
3133             dw2_asm_output_encoded_addr_rtx (
3134                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3135                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3136                 false, NULL);
3137           else
3138             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3139                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3140           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3141           break;
3142
3143         case DW_CFA_advance_loc1:
3144           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3145                                 fde->dw_fde_current_label, NULL);
3146           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3147           break;
3148
3149         case DW_CFA_advance_loc2:
3150           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3151                                 fde->dw_fde_current_label, NULL);
3152           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3153           break;
3154
3155         case DW_CFA_advance_loc4:
3156           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3157                                 fde->dw_fde_current_label, NULL);
3158           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3159           break;
3160
3161         case DW_CFA_MIPS_advance_loc8:
3162           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3163                                 fde->dw_fde_current_label, NULL);
3164           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3165           break;
3166
3167         case DW_CFA_offset_extended:
3168           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3169           dw2_asm_output_data_uleb128 (r, NULL);
3170           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3171           dw2_asm_output_data_uleb128 (off, NULL);
3172           break;
3173
3174         case DW_CFA_def_cfa:
3175           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3176           dw2_asm_output_data_uleb128 (r, NULL);
3177           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3178           break;
3179
3180         case DW_CFA_offset_extended_sf:
3181           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3182           dw2_asm_output_data_uleb128 (r, NULL);
3183           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3184           dw2_asm_output_data_sleb128 (off, NULL);
3185           break;
3186
3187         case DW_CFA_def_cfa_sf:
3188           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3189           dw2_asm_output_data_uleb128 (r, NULL);
3190           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3191           dw2_asm_output_data_sleb128 (off, NULL);
3192           break;
3193
3194         case DW_CFA_restore_extended:
3195         case DW_CFA_undefined:
3196         case DW_CFA_same_value:
3197         case DW_CFA_def_cfa_register:
3198           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3199           dw2_asm_output_data_uleb128 (r, NULL);
3200           break;
3201
3202         case DW_CFA_register:
3203           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3204           dw2_asm_output_data_uleb128 (r, NULL);
3205           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3206           dw2_asm_output_data_uleb128 (r, NULL);
3207           break;
3208
3209         case DW_CFA_def_cfa_offset:
3210         case DW_CFA_GNU_args_size:
3211           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3212           break;
3213
3214         case DW_CFA_def_cfa_offset_sf:
3215           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3216           dw2_asm_output_data_sleb128 (off, NULL);
3217           break;
3218
3219         case DW_CFA_GNU_window_save:
3220           break;
3221
3222         case DW_CFA_def_cfa_expression:
3223         case DW_CFA_expression:
3224           output_cfa_loc (cfi);
3225           break;
3226
3227         case DW_CFA_GNU_negative_offset_extended:
3228           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3229           gcc_unreachable ();
3230
3231         default:
3232           break;
3233         }
3234     }
3235 }
3236
3237 /* Similar, but do it via assembler directives instead.  */
3238
3239 static void
3240 output_cfi_directive (dw_cfi_ref cfi)
3241 {
3242   unsigned long r, r2;
3243
3244   switch (cfi->dw_cfi_opc)
3245     {
3246     case DW_CFA_advance_loc:
3247     case DW_CFA_advance_loc1:
3248     case DW_CFA_advance_loc2:
3249     case DW_CFA_advance_loc4:
3250     case DW_CFA_MIPS_advance_loc8:
3251     case DW_CFA_set_loc:
3252       /* Should only be created by add_fde_cfi in a code path not
3253          followed when emitting via directives.  The assembler is
3254          going to take care of this for us.  */
3255       gcc_unreachable ();
3256
3257     case DW_CFA_offset:
3258     case DW_CFA_offset_extended:
3259     case DW_CFA_offset_extended_sf:
3260       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3261       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3262                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3263       break;
3264
3265     case DW_CFA_restore:
3266     case DW_CFA_restore_extended:
3267       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3268       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3269       break;
3270
3271     case DW_CFA_undefined:
3272       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3273       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3274       break;
3275
3276     case DW_CFA_same_value:
3277       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3278       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3279       break;
3280
3281     case DW_CFA_def_cfa:
3282     case DW_CFA_def_cfa_sf:
3283       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3284       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3285                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3286       break;
3287
3288     case DW_CFA_def_cfa_register:
3289       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3290       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3291       break;
3292
3293     case DW_CFA_register:
3294       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3295       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3296       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3297       break;
3298
3299     case DW_CFA_def_cfa_offset:
3300     case DW_CFA_def_cfa_offset_sf:
3301       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3302                HOST_WIDE_INT_PRINT_DEC"\n",
3303                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3304       break;
3305
3306     case DW_CFA_remember_state:
3307       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3308       break;
3309     case DW_CFA_restore_state:
3310       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3311       break;
3312
3313     case DW_CFA_GNU_args_size:
3314       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3315       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3316       if (flag_debug_asm)
3317         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3318                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3319       fputc ('\n', asm_out_file);
3320       break;
3321
3322     case DW_CFA_GNU_window_save:
3323       fprintf (asm_out_file, "\t.cfi_window_save\n");
3324       break;
3325
3326     case DW_CFA_def_cfa_expression:
3327     case DW_CFA_expression:
3328       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3329       output_cfa_loc_raw (cfi);
3330       fputc ('\n', asm_out_file);
3331       break;
3332
3333     default:
3334       gcc_unreachable ();
3335     }
3336 }
3337
3338 DEF_VEC_P (dw_cfi_ref);
3339 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3340
3341 /* Output CFIs to bring current FDE to the same state as after executing
3342    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3343    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3344    other arguments to pass to output_cfi.  */
3345
3346 static void
3347 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3348 {
3349   struct dw_cfi_struct cfi_buf;
3350   dw_cfi_ref cfi2;
3351   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3352   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3353   unsigned int len, idx;
3354
3355   for (;; cfi = cfi->dw_cfi_next)
3356     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3357       {
3358       case DW_CFA_advance_loc:
3359       case DW_CFA_advance_loc1:
3360       case DW_CFA_advance_loc2:
3361       case DW_CFA_advance_loc4:
3362       case DW_CFA_MIPS_advance_loc8:
3363       case DW_CFA_set_loc:
3364         /* All advances should be ignored.  */
3365         break;
3366       case DW_CFA_remember_state:
3367         {
3368           dw_cfi_ref args_size = cfi_args_size;
3369
3370           /* Skip everything between .cfi_remember_state and
3371              .cfi_restore_state.  */
3372           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3373             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3374               break;
3375             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3376               args_size = cfi2;
3377             else
3378               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3379
3380           if (cfi2 == NULL)
3381             goto flush_all;
3382           else
3383             {
3384               cfi = cfi2;
3385               cfi_args_size = args_size;
3386             }
3387           break;
3388         }
3389       case DW_CFA_GNU_args_size:
3390         cfi_args_size = cfi;
3391         break;
3392       case DW_CFA_GNU_window_save:
3393         goto flush_all;
3394       case DW_CFA_offset:
3395       case DW_CFA_offset_extended:
3396       case DW_CFA_offset_extended_sf:
3397       case DW_CFA_restore:
3398       case DW_CFA_restore_extended:
3399       case DW_CFA_undefined:
3400       case DW_CFA_same_value:
3401       case DW_CFA_register:
3402       case DW_CFA_val_offset:
3403       case DW_CFA_val_offset_sf:
3404       case DW_CFA_expression:
3405       case DW_CFA_val_expression:
3406       case DW_CFA_GNU_negative_offset_extended:
3407         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3408           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3409                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3410         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3411         break;
3412       case DW_CFA_def_cfa:
3413       case DW_CFA_def_cfa_sf:
3414       case DW_CFA_def_cfa_expression:
3415         cfi_cfa = cfi;
3416         cfi_cfa_offset = cfi;
3417         break;
3418       case DW_CFA_def_cfa_register:
3419         cfi_cfa = cfi;
3420         break;
3421       case DW_CFA_def_cfa_offset:
3422       case DW_CFA_def_cfa_offset_sf:
3423         cfi_cfa_offset = cfi;
3424         break;
3425       case DW_CFA_nop:
3426         gcc_assert (cfi == NULL);
3427       flush_all:
3428         len = VEC_length (dw_cfi_ref, regs);
3429         for (idx = 0; idx < len; idx++)
3430           {
3431             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3432             if (cfi2 != NULL
3433                 && cfi2->dw_cfi_opc != DW_CFA_restore
3434                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3435               {
3436                 if (do_cfi_asm)
3437                   output_cfi_directive (cfi2);
3438                 else
3439                   output_cfi (cfi2, fde, for_eh);
3440               }
3441           }
3442         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3443           {
3444             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3445             cfi_buf = *cfi_cfa;
3446             switch (cfi_cfa_offset->dw_cfi_opc)
3447               {
3448               case DW_CFA_def_cfa_offset:
3449                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3450                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3451                 break;
3452               case DW_CFA_def_cfa_offset_sf:
3453                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3454                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3455                 break;
3456               case DW_CFA_def_cfa:
3457               case DW_CFA_def_cfa_sf:
3458                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3459                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3460                 break;
3461               default:
3462                 gcc_unreachable ();
3463               }
3464             cfi_cfa = &cfi_buf;
3465           }
3466         else if (cfi_cfa_offset)
3467           cfi_cfa = cfi_cfa_offset;
3468         if (cfi_cfa)
3469           {
3470             if (do_cfi_asm)
3471               output_cfi_directive (cfi_cfa);
3472             else
3473               output_cfi (cfi_cfa, fde, for_eh);
3474           }
3475         cfi_cfa = NULL;
3476         cfi_cfa_offset = NULL;
3477         if (cfi_args_size
3478             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3479           {
3480             if (do_cfi_asm)
3481               output_cfi_directive (cfi_args_size);
3482             else
3483               output_cfi (cfi_args_size, fde, for_eh);
3484           }
3485         cfi_args_size = NULL;
3486         if (cfi == NULL)
3487           {
3488             VEC_free (dw_cfi_ref, heap, regs);
3489             return;
3490           }
3491         else if (do_cfi_asm)
3492           output_cfi_directive (cfi);
3493         else
3494           output_cfi (cfi, fde, for_eh);
3495         break;
3496       default:
3497         gcc_unreachable ();
3498     }
3499 }
3500
3501 /* Output one FDE.  */
3502
3503 static void
3504 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3505             char *section_start_label, int fde_encoding, char *augmentation,
3506             bool any_lsda_needed, int lsda_encoding)
3507 {
3508   const char *begin, *end;
3509   static unsigned int j;
3510   char l1[20], l2[20];
3511   dw_cfi_ref cfi;
3512
3513   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3514                                      /* empty */ 0);
3515   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3516                                   for_eh + j);
3517   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3518   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3519   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3520     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3521                          " indicating 64-bit DWARF extension");
3522   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3523                         "FDE Length");
3524   ASM_OUTPUT_LABEL (asm_out_file, l1);
3525
3526   if (for_eh)
3527     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3528   else
3529     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3530                            debug_frame_section, "FDE CIE offset");
3531
3532   if (!fde->dw_fde_switched_sections)
3533     {
3534       begin = fde->dw_fde_begin;
3535       end = fde->dw_fde_end;
3536     }
3537   else
3538     {
3539       /* For the first section, prefer dw_fde_begin over
3540          dw_fde_{hot,cold}_section_label, as the latter
3541          might be separated from the real start of the
3542          function by alignment padding.  */
3543       if (!second)
3544         begin = fde->dw_fde_begin;
3545       else if (fde->dw_fde_switched_cold_to_hot)
3546         begin = fde->dw_fde_hot_section_label;
3547       else
3548         begin = fde->dw_fde_unlikely_section_label;
3549       if (second ^ fde->dw_fde_switched_cold_to_hot)
3550         end = fde->dw_fde_unlikely_section_end_label;
3551       else
3552         end = fde->dw_fde_hot_section_end_label;
3553     }
3554
3555   if (for_eh)
3556     {
3557       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3558       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3559       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3560                                        "FDE initial location");
3561       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3562                             end, begin, "FDE address range");
3563     }
3564   else
3565     {
3566       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3567       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3568     }
3569
3570   if (augmentation[0])
3571     {
3572       if (any_lsda_needed)
3573         {
3574           int size = size_of_encoded_value (lsda_encoding);
3575
3576           if (lsda_encoding == DW_EH_PE_aligned)
3577             {
3578               int offset = (  4         /* Length */
3579                             + 4         /* CIE offset */
3580                             + 2 * size_of_encoded_value (fde_encoding)
3581                             + 1         /* Augmentation size */ );
3582               int pad = -offset & (PTR_SIZE - 1);
3583
3584               size += pad;
3585               gcc_assert (size_of_uleb128 (size) == 1);
3586             }
3587
3588           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3589
3590           if (fde->uses_eh_lsda)
3591             {
3592               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3593                                            fde->funcdef_number);
3594               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3595                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3596                                                false,
3597                                                "Language Specific Data Area");
3598             }
3599           else
3600             {
3601               if (lsda_encoding == DW_EH_PE_aligned)
3602                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3603               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3604                                    "Language Specific Data Area (none)");
3605             }
3606         }
3607       else
3608         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3609     }
3610
3611   /* Loop through the Call Frame Instructions associated with
3612      this FDE.  */
3613   fde->dw_fde_current_label = begin;
3614   if (!fde->dw_fde_switched_sections)
3615     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3616       output_cfi (cfi, fde, for_eh);
3617   else if (!second)
3618     {
3619       if (fde->dw_fde_switch_cfi)
3620         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3621           {
3622             output_cfi (cfi, fde, for_eh);
3623             if (cfi == fde->dw_fde_switch_cfi)
3624               break;
3625           }
3626     }
3627   else
3628     {
3629       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3630
3631       if (fde->dw_fde_switch_cfi)
3632         {
3633           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3634           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3635           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3636           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3637         }
3638       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3639         output_cfi (cfi, fde, for_eh);
3640     }
3641
3642   /* If we are to emit a ref/link from function bodies to their frame tables,
3643      do it now.  This is typically performed to make sure that tables
3644      associated with functions are dragged with them and not discarded in
3645      garbage collecting links. We need to do this on a per function basis to
3646      cope with -ffunction-sections.  */
3647
3648 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3649   /* Switch to the function section, emit the ref to the tables, and
3650      switch *back* into the table section.  */
3651   switch_to_section (function_section (fde->decl));
3652   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3653   switch_to_frame_table_section (for_eh, true);
3654 #endif
3655
3656   /* Pad the FDE out to an address sized boundary.  */
3657   ASM_OUTPUT_ALIGN (asm_out_file,
3658                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3659   ASM_OUTPUT_LABEL (asm_out_file, l2);
3660
3661   j += 2;
3662 }
3663
3664 /* Return true if frame description entry FDE is needed for EH.  */
3665
3666 static bool
3667 fde_needed_for_eh_p (dw_fde_ref fde)
3668 {
3669   if (flag_asynchronous_unwind_tables)
3670     return true;
3671
3672   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3673     return true;
3674
3675   if (fde->uses_eh_lsda)
3676     return true;
3677
3678   /* If exceptions are enabled, we have collected nothrow info.  */
3679   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3680     return false;
3681
3682   return true;
3683 }
3684
3685 /* Output the call frame information used to record information
3686    that relates to calculating the frame pointer, and records the
3687    location of saved registers.  */
3688
3689 static void
3690 output_call_frame_info (int for_eh)
3691 {
3692   unsigned int i;
3693   dw_fde_ref fde;
3694   dw_cfi_ref cfi;
3695   char l1[20], l2[20], section_start_label[20];
3696   bool any_lsda_needed = false;
3697   char augmentation[6];
3698   int augmentation_size;
3699   int fde_encoding = DW_EH_PE_absptr;
3700   int per_encoding = DW_EH_PE_absptr;
3701   int lsda_encoding = DW_EH_PE_absptr;
3702   int return_reg;
3703   rtx personality = NULL;
3704   int dw_cie_version;
3705
3706   /* Don't emit a CIE if there won't be any FDEs.  */
3707   if (fde_table_in_use == 0)
3708     return;
3709
3710   /* Nothing to do if the assembler's doing it all.  */
3711   if (dwarf2out_do_cfi_asm ())
3712     return;
3713
3714   /* If we don't have any functions we'll want to unwind out of, don't emit
3715      any EH unwind information.  If we make FDEs linkonce, we may have to
3716      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3717      want to avoid having an FDE kept around when the function it refers to
3718      is discarded.  Example where this matters: a primary function template
3719      in C++ requires EH information, an explicit specialization doesn't.  */
3720   if (for_eh)
3721     {
3722       bool any_eh_needed = false;
3723
3724       for (i = 0; i < fde_table_in_use; i++)
3725         if (fde_table[i].uses_eh_lsda)
3726           any_eh_needed = any_lsda_needed = true;
3727         else if (fde_needed_for_eh_p (&fde_table[i]))
3728           any_eh_needed = true;
3729         else if (TARGET_USES_WEAK_UNWIND_INFO)
3730           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3731                                              1, 1);
3732
3733       if (!any_eh_needed)
3734         return;
3735     }
3736
3737   /* We're going to be generating comments, so turn on app.  */
3738   if (flag_debug_asm)
3739     app_enable ();
3740
3741   /* Switch to the proper frame section, first time.  */
3742   switch_to_frame_table_section (for_eh, false);
3743
3744   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3745   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3746
3747   /* Output the CIE.  */
3748   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3749   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3750   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3751     dw2_asm_output_data (4, 0xffffffff,
3752       "Initial length escape value indicating 64-bit DWARF extension");
3753   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3754                         "Length of Common Information Entry");
3755   ASM_OUTPUT_LABEL (asm_out_file, l1);
3756
3757   /* Now that the CIE pointer is PC-relative for EH,
3758      use 0 to identify the CIE.  */
3759   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3760                        (for_eh ? 0 : DWARF_CIE_ID),
3761                        "CIE Identifier Tag");
3762
3763   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3764      use CIE version 1, unless that would produce incorrect results
3765      due to overflowing the return register column.  */
3766   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3767   dw_cie_version = 1;
3768   if (return_reg >= 256 || dwarf_version > 2)
3769     dw_cie_version = 3;
3770   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3771
3772   augmentation[0] = 0;
3773   augmentation_size = 0;
3774
3775   personality = current_unit_personality;
3776   if (for_eh)
3777     {
3778       char *p;
3779
3780       /* Augmentation:
3781          z      Indicates that a uleb128 is present to size the
3782                 augmentation section.
3783          L      Indicates the encoding (and thus presence) of
3784                 an LSDA pointer in the FDE augmentation.
3785          R      Indicates a non-default pointer encoding for
3786                 FDE code pointers.
3787          P      Indicates the presence of an encoding + language
3788                 personality routine in the CIE augmentation.  */
3789
3790       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3791       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3792       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3793
3794       p = augmentation + 1;
3795       if (personality)
3796         {
3797           *p++ = 'P';
3798           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3799           assemble_external_libcall (personality);
3800         }
3801       if (any_lsda_needed)
3802         {
3803           *p++ = 'L';
3804           augmentation_size += 1;
3805         }
3806       if (fde_encoding != DW_EH_PE_absptr)
3807         {
3808           *p++ = 'R';
3809           augmentation_size += 1;
3810         }
3811       if (p > augmentation + 1)
3812         {
3813           augmentation[0] = 'z';
3814           *p = '\0';
3815         }
3816
3817       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3818       if (personality && per_encoding == DW_EH_PE_aligned)
3819         {
3820           int offset = (  4             /* Length */
3821                         + 4             /* CIE Id */
3822                         + 1             /* CIE version */
3823                         + strlen (augmentation) + 1     /* Augmentation */
3824                         + size_of_uleb128 (1)           /* Code alignment */
3825                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3826                         + 1             /* RA column */
3827                         + 1             /* Augmentation size */
3828                         + 1             /* Personality encoding */ );
3829           int pad = -offset & (PTR_SIZE - 1);
3830
3831           augmentation_size += pad;
3832
3833           /* Augmentations should be small, so there's scarce need to
3834              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3835           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3836         }
3837     }
3838
3839   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3840   if (dw_cie_version >= 4)
3841     {
3842       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3843       dw2_asm_output_data (1, 0, "CIE Segment Size");
3844     }
3845   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3846   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3847                                "CIE Data Alignment Factor");
3848
3849   if (dw_cie_version == 1)
3850     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3851   else
3852     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3853
3854   if (augmentation[0])
3855     {
3856       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3857       if (personality)
3858         {
3859           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3860                                eh_data_format_name (per_encoding));
3861           dw2_asm_output_encoded_addr_rtx (per_encoding,
3862                                            personality,
3863                                            true, NULL);
3864         }
3865
3866       if (any_lsda_needed)
3867         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3868                              eh_data_format_name (lsda_encoding));
3869
3870       if (fde_encoding != DW_EH_PE_absptr)
3871         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3872                              eh_data_format_name (fde_encoding));
3873     }
3874
3875   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3876     output_cfi (cfi, NULL, for_eh);
3877
3878   /* Pad the CIE out to an address sized boundary.  */
3879   ASM_OUTPUT_ALIGN (asm_out_file,
3880                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3881   ASM_OUTPUT_LABEL (asm_out_file, l2);
3882
3883   /* Loop through all of the FDE's.  */
3884   for (i = 0; i < fde_table_in_use; i++)
3885     {
3886       unsigned int k;
3887       fde = &fde_table[i];
3888
3889       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3890       if (for_eh && !fde_needed_for_eh_p (fde))
3891         continue;
3892
3893       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3894         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3895                     augmentation, any_lsda_needed, lsda_encoding);
3896     }
3897
3898   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3899     dw2_asm_output_data (4, 0, "End of Table");
3900 #ifdef MIPS_DEBUGGING_INFO
3901   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3902      get a value of 0.  Putting .align 0 after the label fixes it.  */
3903   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3904 #endif
3905
3906   /* Turn off app to make assembly quicker.  */
3907   if (flag_debug_asm)
3908     app_disable ();
3909 }
3910
3911 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3912
3913 static void
3914 dwarf2out_do_cfi_startproc (bool second)
3915 {
3916   int enc;
3917   rtx ref;
3918   rtx personality = get_personality_function (current_function_decl);
3919
3920   fprintf (asm_out_file, "\t.cfi_startproc\n");
3921
3922   if (personality)
3923     {
3924       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3925       ref = personality;
3926
3927       /* ??? The GAS support isn't entirely consistent.  We have to
3928          handle indirect support ourselves, but PC-relative is done
3929          in the assembler.  Further, the assembler can't handle any
3930          of the weirder relocation types.  */
3931       if (enc & DW_EH_PE_indirect)
3932         ref = dw2_force_const_mem (ref, true);
3933
3934       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3935       output_addr_const (asm_out_file, ref);
3936       fputc ('\n', asm_out_file);
3937     }
3938
3939   if (crtl->uses_eh_lsda)
3940     {
3941       char lab[20];
3942
3943       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3944       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3945                                    current_function_funcdef_no);
3946       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3947       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3948
3949       if (enc & DW_EH_PE_indirect)
3950         ref = dw2_force_const_mem (ref, true);
3951
3952       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3953       output_addr_const (asm_out_file, ref);
3954       fputc ('\n', asm_out_file);
3955     }
3956 }
3957
3958 /* Output a marker (i.e. a label) for the beginning of a function, before
3959    the prologue.  */
3960
3961 void
3962 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3963                           const char *file ATTRIBUTE_UNUSED)
3964 {
3965   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3966   char * dup_label;
3967   dw_fde_ref fde;
3968   section *fnsec;
3969
3970   current_function_func_begin_label = NULL;
3971
3972 #ifdef TARGET_UNWIND_INFO
3973   /* ??? current_function_func_begin_label is also used by except.c
3974      for call-site information.  We must emit this label if it might
3975      be used.  */
3976   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3977       && ! dwarf2out_do_frame ())
3978     return;
3979 #else
3980   if (! dwarf2out_do_frame ())
3981     return;
3982 #endif
3983
3984   fnsec = function_section (current_function_decl);
3985   switch_to_section (fnsec);
3986   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3987                                current_function_funcdef_no);
3988   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3989                           current_function_funcdef_no);
3990   dup_label = xstrdup (label);
3991   current_function_func_begin_label = dup_label;
3992
3993 #ifdef TARGET_UNWIND_INFO
3994   /* We can elide the fde allocation if we're not emitting debug info.  */
3995   if (! dwarf2out_do_frame ())
3996     return;
3997 #endif
3998
3999   /* Expand the fde table if necessary.  */
4000   if (fde_table_in_use == fde_table_allocated)
4001     {
4002       fde_table_allocated += FDE_TABLE_INCREMENT;
4003       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4004       memset (fde_table + fde_table_in_use, 0,
4005               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4006     }
4007
4008   /* Record the FDE associated with this function.  */
4009   current_funcdef_fde = fde_table_in_use;
4010
4011   /* Add the new FDE at the end of the fde_table.  */
4012   fde = &fde_table[fde_table_in_use++];
4013   fde->decl = current_function_decl;
4014   fde->dw_fde_begin = dup_label;
4015   fde->dw_fde_current_label = dup_label;
4016   fde->dw_fde_hot_section_label = NULL;
4017   fde->dw_fde_hot_section_end_label = NULL;
4018   fde->dw_fde_unlikely_section_label = NULL;
4019   fde->dw_fde_unlikely_section_end_label = NULL;
4020   fde->dw_fde_switched_sections = 0;
4021   fde->dw_fde_switched_cold_to_hot = 0;
4022   fde->dw_fde_end = NULL;
4023   fde->dw_fde_vms_end_prologue = NULL;
4024   fde->dw_fde_vms_begin_epilogue = NULL;
4025   fde->dw_fde_cfi = NULL;
4026   fde->dw_fde_switch_cfi = NULL;
4027   fde->funcdef_number = current_function_funcdef_no;
4028   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4029   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4030   fde->nothrow = crtl->nothrow;
4031   fde->drap_reg = INVALID_REGNUM;
4032   fde->vdrap_reg = INVALID_REGNUM;
4033   if (flag_reorder_blocks_and_partition)
4034     {
4035       section *unlikelysec;
4036       if (first_function_block_is_cold)
4037         fde->in_std_section = 1;
4038       else
4039         fde->in_std_section
4040           = (fnsec == text_section
4041              || (cold_text_section && fnsec == cold_text_section));
4042       unlikelysec = unlikely_text_section ();
4043       fde->cold_in_std_section
4044         = (unlikelysec == text_section
4045            || (cold_text_section && unlikelysec == cold_text_section));
4046     }
4047   else
4048     {
4049       fde->in_std_section
4050         = (fnsec == text_section
4051            || (cold_text_section && fnsec == cold_text_section));
4052       fde->cold_in_std_section = 0;
4053     }
4054
4055   args_size = old_args_size = 0;
4056
4057   /* We only want to output line number information for the genuine dwarf2
4058      prologue case, not the eh frame case.  */
4059 #ifdef DWARF2_DEBUGGING_INFO
4060   if (file)
4061     dwarf2out_source_line (line, file, 0, true);
4062 #endif
4063
4064   if (dwarf2out_do_cfi_asm ())
4065     dwarf2out_do_cfi_startproc (false);
4066   else
4067     {
4068       rtx personality = get_personality_function (current_function_decl);
4069       if (!current_unit_personality)
4070         current_unit_personality = personality;
4071
4072       /* We cannot keep a current personality per function as without CFI
4073          asm, at the point where we emit the CFI data, there is no current
4074          function anymore.  */
4075       if (personality && current_unit_personality != personality)
4076         sorry ("multiple EH personalities are supported only with assemblers "
4077                "supporting .cfi_personality directive");
4078     }
4079 }
4080
4081 /* Output a marker (i.e. a label) for the end of the generated code
4082    for a function prologue.  This gets called *after* the prologue code has
4083    been generated.  */
4084
4085 void
4086 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4087                         const char *file ATTRIBUTE_UNUSED)
4088 {
4089   dw_fde_ref fde;
4090   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4091
4092   /* Output a label to mark the endpoint of the code generated for this
4093      function.  */
4094   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4095                                current_function_funcdef_no);
4096   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4097                           current_function_funcdef_no);
4098   fde = &fde_table[fde_table_in_use - 1];
4099   fde->dw_fde_vms_end_prologue = xstrdup (label);
4100 }
4101
4102 /* Output a marker (i.e. a label) for the beginning of the generated code
4103    for a function epilogue.  This gets called *before* the prologue code has
4104    been generated.  */
4105
4106 void
4107 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4108                           const char *file ATTRIBUTE_UNUSED)
4109 {
4110   dw_fde_ref fde;
4111   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4112
4113   fde = &fde_table[fde_table_in_use - 1];
4114   if (fde->dw_fde_vms_begin_epilogue)
4115     return;
4116
4117   /* Output a label to mark the endpoint of the code generated for this
4118      function.  */
4119   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4120                                current_function_funcdef_no);
4121   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4122                           current_function_funcdef_no);
4123   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4124 }
4125
4126 /* Output a marker (i.e. a label) for the absolute end of the generated code
4127    for a function definition.  This gets called *after* the epilogue code has
4128    been generated.  */
4129
4130 void
4131 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4132                         const char *file ATTRIBUTE_UNUSED)
4133 {
4134   dw_fde_ref fde;
4135   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4136
4137   last_var_location_insn = NULL_RTX;
4138
4139   if (dwarf2out_do_cfi_asm ())
4140     fprintf (asm_out_file, "\t.cfi_endproc\n");
4141
4142   /* Output a label to mark the endpoint of the code generated for this
4143      function.  */
4144   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4145                                current_function_funcdef_no);
4146   ASM_OUTPUT_LABEL (asm_out_file, label);
4147   fde = current_fde ();
4148   gcc_assert (fde != NULL);
4149   fde->dw_fde_end = xstrdup (label);
4150 }
4151
4152 void
4153 dwarf2out_frame_init (void)
4154 {
4155   /* Allocate the initial hunk of the fde_table.  */
4156   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4157   fde_table_allocated = FDE_TABLE_INCREMENT;
4158   fde_table_in_use = 0;
4159
4160   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4161      sake of lookup_cfa.  */
4162
4163   /* On entry, the Canonical Frame Address is at SP.  */
4164   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4165
4166   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4167     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4168 }
4169
4170 void
4171 dwarf2out_frame_finish (void)
4172 {
4173   /* Output call frame information.  */
4174   if (DWARF2_FRAME_INFO)
4175     output_call_frame_info (0);
4176
4177 #ifndef TARGET_UNWIND_INFO
4178   /* Output another copy for the unwinder.  */
4179   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4180     output_call_frame_info (1);
4181 #endif
4182 }
4183
4184 /* Note that the current function section is being used for code.  */
4185
4186 static void
4187 dwarf2out_note_section_used (void)
4188 {
4189   section *sec = current_function_section ();
4190   if (sec == text_section)
4191     text_section_used = true;
4192   else if (sec == cold_text_section)
4193     cold_text_section_used = true;
4194 }
4195
4196 void
4197 dwarf2out_switch_text_section (void)
4198 {
4199   dw_fde_ref fde = current_fde ();
4200
4201   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4202
4203   fde->dw_fde_switched_sections = 1;
4204   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4205
4206   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4207   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4208   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4209   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4210   have_multiple_function_sections = true;
4211
4212   /* Reset the current label on switching text sections, so that we
4213      don't attempt to advance_loc4 between labels in different sections.  */
4214   fde->dw_fde_current_label = NULL;
4215
4216   /* There is no need to mark used sections when not debugging.  */
4217   if (cold_text_section != NULL)
4218     dwarf2out_note_section_used ();
4219
4220   if (dwarf2out_do_cfi_asm ())
4221     fprintf (asm_out_file, "\t.cfi_endproc\n");
4222
4223   /* Now do the real section switch.  */
4224   switch_to_section (current_function_section ());
4225
4226   if (dwarf2out_do_cfi_asm ())
4227     {
4228       dwarf2out_do_cfi_startproc (true);
4229       /* As this is a different FDE, insert all current CFI instructions
4230          again.  */
4231       output_cfis (fde->dw_fde_cfi, true, fde, true);
4232     }
4233   else
4234     {
4235       dw_cfi_ref cfi = fde->dw_fde_cfi;
4236
4237       cfi = fde->dw_fde_cfi;
4238       if (cfi)
4239         while (cfi->dw_cfi_next != NULL)
4240           cfi = cfi->dw_cfi_next;
4241       fde->dw_fde_switch_cfi = cfi;
4242     }
4243 }
4244 \f
4245 /* And now, the subset of the debugging information support code necessary
4246    for emitting location expressions.  */
4247
4248 /* Data about a single source file.  */
4249 struct GTY(()) dwarf_file_data {
4250   const char * filename;
4251   int emitted_number;
4252 };
4253
4254 typedef struct dw_val_struct *dw_val_ref;
4255 typedef struct die_struct *dw_die_ref;
4256 typedef const struct die_struct *const_dw_die_ref;
4257 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4258 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4259
4260 typedef struct GTY(()) deferred_locations_struct
4261 {
4262   tree variable;
4263   dw_die_ref die;
4264 } deferred_locations;
4265
4266 DEF_VEC_O(deferred_locations);
4267 DEF_VEC_ALLOC_O(deferred_locations,gc);
4268
4269 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4270
4271 DEF_VEC_P(dw_die_ref);
4272 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4273
4274 /* Each DIE may have a series of attribute/value pairs.  Values
4275    can take on several forms.  The forms that are used in this
4276    implementation are listed below.  */
4277
4278 enum dw_val_class
4279 {
4280   dw_val_class_addr,
4281   dw_val_class_offset,
4282   dw_val_class_loc,
4283   dw_val_class_loc_list,
4284   dw_val_class_range_list,
4285   dw_val_class_const,
4286   dw_val_class_unsigned_const,
4287   dw_val_class_const_double,
4288   dw_val_class_vec,
4289   dw_val_class_flag,
4290   dw_val_class_die_ref,
4291   dw_val_class_fde_ref,
4292   dw_val_class_lbl_id,
4293   dw_val_class_lineptr,
4294   dw_val_class_str,
4295   dw_val_class_macptr,
4296   dw_val_class_file,
4297   dw_val_class_data8,
4298   dw_val_class_vms_delta
4299 };
4300
4301 /* Describe a floating point constant value, or a vector constant value.  */
4302
4303 typedef struct GTY(()) dw_vec_struct {
4304   unsigned char * GTY((length ("%h.length"))) array;
4305   unsigned length;
4306   unsigned elt_size;
4307 }
4308 dw_vec_const;
4309
4310 /* The dw_val_node describes an attribute's value, as it is
4311    represented internally.  */
4312
4313 typedef struct GTY(()) dw_val_struct {
4314   enum dw_val_class val_class;
4315   union dw_val_struct_union
4316     {
4317       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4318       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4319       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4320       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4321       HOST_WIDE_INT GTY ((default)) val_int;
4322       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4323       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4324       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4325       struct dw_val_die_union
4326         {
4327           dw_die_ref die;
4328           int external;
4329         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4330       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4331       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4332       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4333       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4334       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4335       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4336       struct dw_val_vms_delta_union
4337         {
4338           char * lbl1;
4339           char * lbl2;
4340         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4341     }
4342   GTY ((desc ("%1.val_class"))) v;
4343 }
4344 dw_val_node;
4345
4346 /* Locations in memory are described using a sequence of stack machine
4347    operations.  */
4348
4349 typedef struct GTY(()) dw_loc_descr_struct {
4350   dw_loc_descr_ref dw_loc_next;
4351   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4352   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4353      from DW_OP_addr with a dtp-relative symbol relocation.  */
4354   unsigned int dtprel : 1;
4355   int dw_loc_addr;
4356   dw_val_node dw_loc_oprnd1;
4357   dw_val_node dw_loc_oprnd2;
4358 }
4359 dw_loc_descr_node;
4360
4361 /* Location lists are ranges + location descriptions for that range,
4362    so you can track variables that are in different places over
4363    their entire life.  */
4364 typedef struct GTY(()) dw_loc_list_struct {
4365   dw_loc_list_ref dw_loc_next;
4366   const char *begin; /* Label for begin address of range */
4367   const char *end;  /* Label for end address of range */
4368   char *ll_symbol; /* Label for beginning of location list.
4369                       Only on head of list */
4370   const char *section; /* Section this loclist is relative to */
4371   dw_loc_descr_ref expr;
4372 } dw_loc_list_node;
4373
4374 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4375
4376 /* Convert a DWARF stack opcode into its string name.  */
4377
4378 static const char *
4379 dwarf_stack_op_name (unsigned int op)
4380 {
4381   switch (op)
4382     {
4383     case DW_OP_addr:
4384       return "DW_OP_addr";
4385     case DW_OP_deref:
4386       return "DW_OP_deref";
4387     case DW_OP_const1u:
4388       return "DW_OP_const1u";
4389     case DW_OP_const1s:
4390       return "DW_OP_const1s";
4391     case DW_OP_const2u:
4392       return "DW_OP_const2u";
4393     case DW_OP_const2s:
4394       return "DW_OP_const2s";
4395     case DW_OP_const4u:
4396       return "DW_OP_const4u";
4397     case DW_OP_const4s:
4398       return "DW_OP_const4s";
4399     case DW_OP_const8u:
4400       return "DW_OP_const8u";
4401     case DW_OP_const8s:
4402       return "DW_OP_const8s";
4403     case DW_OP_constu:
4404       return "DW_OP_constu";
4405     case DW_OP_consts:
4406       return "DW_OP_consts";
4407     case DW_OP_dup:
4408       return "DW_OP_dup";
4409     case DW_OP_drop:
4410       return "DW_OP_drop";
4411     case DW_OP_over:
4412       return "DW_OP_over";
4413     case DW_OP_pick:
4414       return "DW_OP_pick";
4415     case DW_OP_swap:
4416       return "DW_OP_swap";
4417     case DW_OP_rot:
4418       return "DW_OP_rot";
4419     case DW_OP_xderef:
4420       return "DW_OP_xderef";
4421     case DW_OP_abs:
4422       return "DW_OP_abs";
4423     case DW_OP_and:
4424       return "DW_OP_and";
4425     case DW_OP_div:
4426       return "DW_OP_div";
4427     case DW_OP_minus:
4428       return "DW_OP_minus";
4429     case DW_OP_mod:
4430       return "DW_OP_mod";
4431     case DW_OP_mul:
4432       return "DW_OP_mul";
4433     case DW_OP_neg:
4434       return "DW_OP_neg";
4435     case DW_OP_not:
4436       return "DW_OP_not";
4437     case DW_OP_or:
4438       return "DW_OP_or";
4439     case DW_OP_plus:
4440       return "DW_OP_plus";
4441     case DW_OP_plus_uconst:
4442       return "DW_OP_plus_uconst";
4443     case DW_OP_shl:
4444       return "DW_OP_shl";
4445     case DW_OP_shr:
4446       return "DW_OP_shr";
4447     case DW_OP_shra:
4448       return "DW_OP_shra";
4449     case DW_OP_xor:
4450       return "DW_OP_xor";
4451     case DW_OP_bra:
4452       return "DW_OP_bra";
4453     case DW_OP_eq:
4454       return "DW_OP_eq";
4455     case DW_OP_ge:
4456       return "DW_OP_ge";
4457     case DW_OP_gt:
4458       return "DW_OP_gt";
4459     case DW_OP_le:
4460       return "DW_OP_le";
4461     case DW_OP_lt:
4462       return "DW_OP_lt";
4463     case DW_OP_ne:
4464       return "DW_OP_ne";
4465     case DW_OP_skip:
4466       return "DW_OP_skip";
4467     case DW_OP_lit0:
4468       return "DW_OP_lit0";
4469     case DW_OP_lit1:
4470       return "DW_OP_lit1";
4471     case DW_OP_lit2:
4472       return "DW_OP_lit2";
4473     case DW_OP_lit3:
4474       return "DW_OP_lit3";
4475     case DW_OP_lit4:
4476       return "DW_OP_lit4";
4477     case DW_OP_lit5:
4478       return "DW_OP_lit5";
4479     case DW_OP_lit6:
4480       return "DW_OP_lit6";
4481     case DW_OP_lit7:
4482       return "DW_OP_lit7";
4483     case DW_OP_lit8:
4484       return "DW_OP_lit8";
4485     case DW_OP_lit9:
4486       return "DW_OP_lit9";
4487     case DW_OP_lit10:
4488       return "DW_OP_lit10";
4489     case DW_OP_lit11:
4490       return "DW_OP_lit11";
4491     case DW_OP_lit12:
4492       return "DW_OP_lit12";
4493     case DW_OP_lit13:
4494       return "DW_OP_lit13";
4495     case DW_OP_lit14:
4496       return "DW_OP_lit14";
4497     case DW_OP_lit15:
4498       return "DW_OP_lit15";
4499     case DW_OP_lit16:
4500       return "DW_OP_lit16";
4501     case DW_OP_lit17:
4502       return "DW_OP_lit17";
4503     case DW_OP_lit18:
4504       return "DW_OP_lit18";
4505     case DW_OP_lit19:
4506       return "DW_OP_lit19";
4507     case DW_OP_lit20:
4508       return "DW_OP_lit20";
4509     case DW_OP_lit21:
4510       return "DW_OP_lit21";
4511     case DW_OP_lit22:
4512       return "DW_OP_lit22";
4513     case DW_OP_lit23:
4514       return "DW_OP_lit23";
4515     case DW_OP_lit24:
4516       return "DW_OP_lit24";
4517     case DW_OP_lit25:
4518       return "DW_OP_lit25";
4519     case DW_OP_lit26:
4520       return "DW_OP_lit26";
4521     case DW_OP_lit27:
4522       return "DW_OP_lit27";
4523     case DW_OP_lit28:
4524       return "DW_OP_lit28";
4525     case DW_OP_lit29:
4526       return "DW_OP_lit29";
4527     case DW_OP_lit30:
4528       return "DW_OP_lit30";
4529     case DW_OP_lit31:
4530       return "DW_OP_lit31";
4531     case DW_OP_reg0:
4532       return "DW_OP_reg0";
4533     case DW_OP_reg1:
4534       return "DW_OP_reg1";
4535     case DW_OP_reg2:
4536       return "DW_OP_reg2";
4537     case DW_OP_reg3:
4538       return "DW_OP_reg3";
4539     case DW_OP_reg4:
4540       return "DW_OP_reg4";
4541     case DW_OP_reg5:
4542       return "DW_OP_reg5";
4543     case DW_OP_reg6:
4544       return "DW_OP_reg6";
4545     case DW_OP_reg7:
4546       return "DW_OP_reg7";
4547     case DW_OP_reg8:
4548       return "DW_OP_reg8";
4549     case DW_OP_reg9:
4550       return "DW_OP_reg9";
4551     case DW_OP_reg10:
4552       return "DW_OP_reg10";
4553     case DW_OP_reg11:
4554       return "DW_OP_reg11";
4555     case DW_OP_reg12:
4556       return "DW_OP_reg12";
4557     case DW_OP_reg13:
4558       return "DW_OP_reg13";
4559     case DW_OP_reg14:
4560       return "DW_OP_reg14";
4561     case DW_OP_reg15:
4562       return "DW_OP_reg15";
4563     case DW_OP_reg16:
4564       return "DW_OP_reg16";
4565     case DW_OP_reg17:
4566       return "DW_OP_reg17";
4567     case DW_OP_reg18:
4568       return "DW_OP_reg18";
4569     case DW_OP_reg19:
4570       return "DW_OP_reg19";
4571     case DW_OP_reg20:
4572       return "DW_OP_reg20";
4573     case DW_OP_reg21:
4574       return "DW_OP_reg21";
4575     case DW_OP_reg22:
4576       return "DW_OP_reg22";
4577     case DW_OP_reg23:
4578       return "DW_OP_reg23";
4579     case DW_OP_reg24:
4580       return "DW_OP_reg24";
4581     case DW_OP_reg25:
4582       return "DW_OP_reg25";
4583     case DW_OP_reg26:
4584       return "DW_OP_reg26";
4585     case DW_OP_reg27:
4586       return "DW_OP_reg27";
4587     case DW_OP_reg28:
4588       return "DW_OP_reg28";
4589     case DW_OP_reg29:
4590       return "DW_OP_reg29";
4591     case DW_OP_reg30:
4592       return "DW_OP_reg30";
4593     case DW_OP_reg31:
4594       return "DW_OP_reg31";
4595     case DW_OP_breg0:
4596       return "DW_OP_breg0";
4597     case DW_OP_breg1:
4598       return "DW_OP_breg1";
4599     case DW_OP_breg2:
4600       return "DW_OP_breg2";
4601     case DW_OP_breg3:
4602       return "DW_OP_breg3";
4603     case DW_OP_breg4:
4604       return "DW_OP_breg4";
4605     case DW_OP_breg5:
4606       return "DW_OP_breg5";
4607     case DW_OP_breg6:
4608       return "DW_OP_breg6";
4609     case DW_OP_breg7:
4610       return "DW_OP_breg7";
4611     case DW_OP_breg8:
4612       return "DW_OP_breg8";
4613     case DW_OP_breg9:
4614       return "DW_OP_breg9";
4615     case DW_OP_breg10:
4616       return "DW_OP_breg10";
4617     case DW_OP_breg11:
4618       return "DW_OP_breg11";
4619     case DW_OP_breg12:
4620       return "DW_OP_breg12";
4621     case DW_OP_breg13:
4622       return "DW_OP_breg13";
4623     case DW_OP_breg14:
4624       return "DW_OP_breg14";
4625     case DW_OP_breg15:
4626       return "DW_OP_breg15";
4627     case DW_OP_breg16:
4628       return "DW_OP_breg16";
4629     case DW_OP_breg17:
4630       return "DW_OP_breg17";
4631     case DW_OP_breg18:
4632       return "DW_OP_breg18";
4633     case DW_OP_breg19:
4634       return "DW_OP_breg19";
4635     case DW_OP_breg20:
4636       return "DW_OP_breg20";
4637     case DW_OP_breg21:
4638       return "DW_OP_breg21";
4639     case DW_OP_breg22:
4640       return "DW_OP_breg22";
4641     case DW_OP_breg23:
4642       return "DW_OP_breg23";
4643     case DW_OP_breg24:
4644       return "DW_OP_breg24";
4645     case DW_OP_breg25:
4646       return "DW_OP_breg25";
4647     case DW_OP_breg26:
4648       return "DW_OP_breg26";
4649     case DW_OP_breg27:
4650       return "DW_OP_breg27";
4651     case DW_OP_breg28:
4652       return "DW_OP_breg28";
4653     case DW_OP_breg29:
4654       return "DW_OP_breg29";
4655     case DW_OP_breg30:
4656       return "DW_OP_breg30";
4657     case DW_OP_breg31:
4658       return "DW_OP_breg31";
4659     case DW_OP_regx:
4660       return "DW_OP_regx";
4661     case DW_OP_fbreg:
4662       return "DW_OP_fbreg";
4663     case DW_OP_bregx:
4664       return "DW_OP_bregx";
4665     case DW_OP_piece:
4666       return "DW_OP_piece";
4667     case DW_OP_deref_size:
4668       return "DW_OP_deref_size";
4669     case DW_OP_xderef_size:
4670       return "DW_OP_xderef_size";
4671     case DW_OP_nop:
4672       return "DW_OP_nop";
4673
4674     case DW_OP_push_object_address:
4675       return "DW_OP_push_object_address";
4676     case DW_OP_call2:
4677       return "DW_OP_call2";
4678     case DW_OP_call4:
4679       return "DW_OP_call4";
4680     case DW_OP_call_ref:
4681       return "DW_OP_call_ref";
4682     case DW_OP_implicit_value:
4683       return "DW_OP_implicit_value";
4684     case DW_OP_stack_value:
4685       return "DW_OP_stack_value";
4686     case DW_OP_form_tls_address:
4687       return "DW_OP_form_tls_address";
4688     case DW_OP_call_frame_cfa:
4689       return "DW_OP_call_frame_cfa";
4690     case DW_OP_bit_piece:
4691       return "DW_OP_bit_piece";
4692
4693     case DW_OP_GNU_push_tls_address:
4694       return "DW_OP_GNU_push_tls_address";
4695     case DW_OP_GNU_uninit:
4696       return "DW_OP_GNU_uninit";
4697     case DW_OP_GNU_encoded_addr:
4698       return "DW_OP_GNU_encoded_addr";
4699
4700     default:
4701       return "OP_<unknown>";
4702     }
4703 }
4704
4705 /* Return a pointer to a newly allocated location description.  Location
4706    descriptions are simple expression terms that can be strung
4707    together to form more complicated location (address) descriptions.  */
4708
4709 static inline dw_loc_descr_ref
4710 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4711                unsigned HOST_WIDE_INT oprnd2)
4712 {
4713   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4714
4715   descr->dw_loc_opc = op;
4716   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4717   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4718   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4719   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4720
4721   return descr;
4722 }
4723
4724 /* Return a pointer to a newly allocated location description for
4725    REG and OFFSET.  */
4726
4727 static inline dw_loc_descr_ref
4728 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4729 {
4730   if (reg <= 31)
4731     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4732                           offset, 0);
4733   else
4734     return new_loc_descr (DW_OP_bregx, reg, offset);
4735 }
4736
4737 /* Add a location description term to a location description expression.  */
4738
4739 static inline void
4740 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4741 {
4742   dw_loc_descr_ref *d;
4743
4744   /* Find the end of the chain.  */
4745   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4746     ;
4747
4748   *d = descr;
4749 }
4750
4751 /* Add a constant OFFSET to a location expression.  */
4752
4753 static void
4754 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4755 {
4756   dw_loc_descr_ref loc;
4757   HOST_WIDE_INT *p;
4758
4759   gcc_assert (*list_head != NULL);
4760
4761   if (!offset)
4762     return;
4763
4764   /* Find the end of the chain.  */
4765   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4766     ;
4767
4768   p = NULL;
4769   if (loc->dw_loc_opc == DW_OP_fbreg
4770       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4771     p = &loc->dw_loc_oprnd1.v.val_int;
4772   else if (loc->dw_loc_opc == DW_OP_bregx)
4773     p = &loc->dw_loc_oprnd2.v.val_int;
4774
4775   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4776      offset.  Don't optimize if an signed integer overflow would happen.  */
4777   if (p != NULL
4778       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4779           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4780     *p += offset;
4781
4782   else if (offset > 0)
4783     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4784
4785   else
4786     {
4787       loc->dw_loc_next = int_loc_descriptor (-offset);
4788       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4789     }
4790 }
4791
4792 /* Add a constant OFFSET to a location list.  */
4793
4794 static void
4795 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4796 {
4797   dw_loc_list_ref d;
4798   for (d = list_head; d != NULL; d = d->dw_loc_next)
4799     loc_descr_plus_const (&d->expr, offset);
4800 }
4801
4802 /* Return the size of a location descriptor.  */
4803
4804 static unsigned long
4805 size_of_loc_descr (dw_loc_descr_ref loc)
4806 {
4807   unsigned long size = 1;
4808
4809   switch (loc->dw_loc_opc)
4810     {
4811     case DW_OP_addr:
4812       size += DWARF2_ADDR_SIZE;
4813       break;
4814     case DW_OP_const1u:
4815     case DW_OP_const1s:
4816       size += 1;
4817       break;
4818     case DW_OP_const2u:
4819     case DW_OP_const2s:
4820       size += 2;
4821       break;
4822     case DW_OP_const4u:
4823     case DW_OP_const4s:
4824       size += 4;
4825       break;
4826     case DW_OP_const8u:
4827     case DW_OP_const8s:
4828       size += 8;
4829       break;
4830     case DW_OP_constu:
4831       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4832       break;
4833     case DW_OP_consts:
4834       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4835       break;
4836     case DW_OP_pick:
4837       size += 1;
4838       break;
4839     case DW_OP_plus_uconst:
4840       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4841       break;
4842     case DW_OP_skip:
4843     case DW_OP_bra:
4844       size += 2;
4845       break;
4846     case DW_OP_breg0:
4847     case DW_OP_breg1:
4848     case DW_OP_breg2:
4849     case DW_OP_breg3:
4850     case DW_OP_breg4:
4851     case DW_OP_breg5:
4852     case DW_OP_breg6:
4853     case DW_OP_breg7:
4854     case DW_OP_breg8:
4855     case DW_OP_breg9:
4856     case DW_OP_breg10:
4857     case DW_OP_breg11:
4858     case DW_OP_breg12:
4859     case DW_OP_breg13:
4860     case DW_OP_breg14:
4861     case DW_OP_breg15:
4862     case DW_OP_breg16:
4863     case DW_OP_breg17:
4864     case DW_OP_breg18:
4865     case DW_OP_breg19:
4866     case DW_OP_breg20:
4867     case DW_OP_breg21:
4868     case DW_OP_breg22:
4869     case DW_OP_breg23:
4870     case DW_OP_breg24:
4871     case DW_OP_breg25:
4872     case DW_OP_breg26:
4873     case DW_OP_breg27:
4874     case DW_OP_breg28:
4875     case DW_OP_breg29:
4876     case DW_OP_breg30:
4877     case DW_OP_breg31:
4878       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4879       break;
4880     case DW_OP_regx:
4881       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4882       break;
4883     case DW_OP_fbreg:
4884       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4885       break;
4886     case DW_OP_bregx:
4887       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4888       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4889       break;
4890     case DW_OP_piece:
4891       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4892       break;
4893     case DW_OP_bit_piece:
4894       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4895       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4896       break;
4897     case DW_OP_deref_size:
4898     case DW_OP_xderef_size:
4899       size += 1;
4900       break;
4901     case DW_OP_call2:
4902       size += 2;
4903       break;
4904     case DW_OP_call4:
4905       size += 4;
4906       break;
4907     case DW_OP_call_ref:
4908       size += DWARF2_ADDR_SIZE;
4909       break;
4910     case DW_OP_implicit_value:
4911       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4912               + loc->dw_loc_oprnd1.v.val_unsigned;
4913       break;
4914     default:
4915       break;
4916     }
4917
4918   return size;
4919 }
4920
4921 /* Return the size of a series of location descriptors.  */
4922
4923 static unsigned long
4924 size_of_locs (dw_loc_descr_ref loc)
4925 {
4926   dw_loc_descr_ref l;
4927   unsigned long size;
4928
4929   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4930      field, to avoid writing to a PCH file.  */
4931   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4932     {
4933       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4934         break;
4935       size += size_of_loc_descr (l);
4936     }
4937   if (! l)
4938     return size;
4939
4940   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4941     {
4942       l->dw_loc_addr = size;
4943       size += size_of_loc_descr (l);
4944     }
4945
4946   return size;
4947 }
4948
4949 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4950
4951 /* Output location description stack opcode's operands (if any).  */
4952
4953 static void
4954 output_loc_operands (dw_loc_descr_ref loc)
4955 {
4956   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4957   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4958
4959   switch (loc->dw_loc_opc)
4960     {
4961 #ifdef DWARF2_DEBUGGING_INFO
4962     case DW_OP_const2u:
4963     case DW_OP_const2s:
4964       dw2_asm_output_data (2, val1->v.val_int, NULL);
4965       break;
4966     case DW_OP_const4u:
4967       if (loc->dtprel)
4968         {
4969           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4970           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4971                                                val1->v.val_addr);
4972           fputc ('\n', asm_out_file);
4973           break;
4974         }
4975       /* FALLTHRU */
4976     case DW_OP_const4s:
4977       dw2_asm_output_data (4, val1->v.val_int, NULL);
4978       break;
4979     case DW_OP_const8u:
4980       if (loc->dtprel)
4981         {
4982           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4983           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4984                                                val1->v.val_addr);
4985           fputc ('\n', asm_out_file);
4986           break;
4987         }
4988       /* FALLTHRU */
4989     case DW_OP_const8s:
4990       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4991       dw2_asm_output_data (8, val1->v.val_int, NULL);
4992       break;
4993     case DW_OP_skip:
4994     case DW_OP_bra:
4995       {
4996         int offset;
4997
4998         gcc_assert (val1->val_class == dw_val_class_loc);
4999         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5000
5001         dw2_asm_output_data (2, offset, NULL);
5002       }
5003       break;
5004     case DW_OP_implicit_value:
5005       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5006       switch (val2->val_class)
5007         {
5008         case dw_val_class_const:
5009           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5010           break;
5011         case dw_val_class_vec:
5012           {
5013             unsigned int elt_size = val2->v.val_vec.elt_size;
5014             unsigned int len = val2->v.val_vec.length;
5015             unsigned int i;
5016             unsigned char *p;
5017
5018             if (elt_size > sizeof (HOST_WIDE_INT))
5019               {
5020                 elt_size /= 2;
5021                 len *= 2;
5022               }
5023             for (i = 0, p = val2->v.val_vec.array;
5024                  i < len;
5025                  i++, p += elt_size)
5026               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5027                                    "fp or vector constant word %u", i);
5028           }
5029           break;
5030         case dw_val_class_const_double:
5031           {
5032             unsigned HOST_WIDE_INT first, second;
5033
5034             if (WORDS_BIG_ENDIAN)
5035               {
5036                 first = val2->v.val_double.high;
5037                 second = val2->v.val_double.low;
5038               }
5039             else
5040               {
5041                 first = val2->v.val_double.low;
5042                 second = val2->v.val_double.high;
5043               }
5044             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5045                                  first, NULL);
5046             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5047                                  second, NULL);
5048           }
5049           break;
5050         case dw_val_class_addr:
5051           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5052           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5053           break;
5054         default:
5055           gcc_unreachable ();
5056         }
5057       break;
5058 #else
5059     case DW_OP_const2u:
5060     case DW_OP_const2s:
5061     case DW_OP_const4u:
5062     case DW_OP_const4s:
5063     case DW_OP_const8u:
5064     case DW_OP_const8s:
5065     case DW_OP_skip:
5066     case DW_OP_bra:
5067     case DW_OP_implicit_value:
5068       /* We currently don't make any attempt to make sure these are
5069          aligned properly like we do for the main unwind info, so
5070          don't support emitting things larger than a byte if we're
5071          only doing unwinding.  */
5072       gcc_unreachable ();
5073 #endif
5074     case DW_OP_const1u:
5075     case DW_OP_const1s:
5076       dw2_asm_output_data (1, val1->v.val_int, NULL);
5077       break;
5078     case DW_OP_constu:
5079       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5080       break;
5081     case DW_OP_consts:
5082       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5083       break;
5084     case DW_OP_pick:
5085       dw2_asm_output_data (1, val1->v.val_int, NULL);
5086       break;
5087     case DW_OP_plus_uconst:
5088       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5089       break;
5090     case DW_OP_breg0:
5091     case DW_OP_breg1:
5092     case DW_OP_breg2:
5093     case DW_OP_breg3:
5094     case DW_OP_breg4:
5095     case DW_OP_breg5:
5096     case DW_OP_breg6:
5097     case DW_OP_breg7:
5098     case DW_OP_breg8:
5099     case DW_OP_breg9:
5100     case DW_OP_breg10:
5101     case DW_OP_breg11:
5102     case DW_OP_breg12:
5103     case DW_OP_breg13:
5104     case DW_OP_breg14:
5105     case DW_OP_breg15:
5106     case DW_OP_breg16:
5107     case DW_OP_breg17:
5108     case DW_OP_breg18:
5109     case DW_OP_breg19:
5110     case DW_OP_breg20:
5111     case DW_OP_breg21:
5112     case DW_OP_breg22:
5113     case DW_OP_breg23:
5114     case DW_OP_breg24:
5115     case DW_OP_breg25:
5116     case DW_OP_breg26:
5117     case DW_OP_breg27:
5118     case DW_OP_breg28:
5119     case DW_OP_breg29:
5120     case DW_OP_breg30:
5121     case DW_OP_breg31:
5122       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5123       break;
5124     case DW_OP_regx:
5125       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5126       break;
5127     case DW_OP_fbreg:
5128       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5129       break;
5130     case DW_OP_bregx:
5131       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5132       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5133       break;
5134     case DW_OP_piece:
5135       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5136       break;
5137     case DW_OP_bit_piece:
5138       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5139       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5140       break;
5141     case DW_OP_deref_size:
5142     case DW_OP_xderef_size:
5143       dw2_asm_output_data (1, val1->v.val_int, NULL);
5144       break;
5145
5146     case DW_OP_addr:
5147       if (loc->dtprel)
5148         {
5149           if (targetm.asm_out.output_dwarf_dtprel)
5150             {
5151               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5152                                                    DWARF2_ADDR_SIZE,
5153                                                    val1->v.val_addr);
5154               fputc ('\n', asm_out_file);
5155             }
5156           else
5157             gcc_unreachable ();
5158         }
5159       else
5160         {
5161 #ifdef DWARF2_DEBUGGING_INFO
5162           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5163 #else
5164           gcc_unreachable ();
5165 #endif
5166         }
5167       break;
5168
5169     default:
5170       /* Other codes have no operands.  */
5171       break;
5172     }
5173 }
5174
5175 /* Output a sequence of location operations.  */
5176
5177 static void
5178 output_loc_sequence (dw_loc_descr_ref loc)
5179 {
5180   for (; loc != NULL; loc = loc->dw_loc_next)
5181     {
5182       /* Output the opcode.  */
5183       dw2_asm_output_data (1, loc->dw_loc_opc,
5184                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5185
5186       /* Output the operand(s) (if any).  */
5187       output_loc_operands (loc);
5188     }
5189 }
5190
5191 /* Output location description stack opcode's operands (if any).
5192    The output is single bytes on a line, suitable for .cfi_escape.  */
5193
5194 static void
5195 output_loc_operands_raw (dw_loc_descr_ref loc)
5196 {
5197   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5198   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5199
5200   switch (loc->dw_loc_opc)
5201     {
5202     case DW_OP_addr:
5203     case DW_OP_implicit_value:
5204       /* We cannot output addresses in .cfi_escape, only bytes.  */
5205       gcc_unreachable ();
5206
5207     case DW_OP_const1u:
5208     case DW_OP_const1s:
5209     case DW_OP_pick:
5210     case DW_OP_deref_size:
5211     case DW_OP_xderef_size:
5212       fputc (',', asm_out_file);
5213       dw2_asm_output_data_raw (1, val1->v.val_int);
5214       break;
5215
5216     case DW_OP_const2u:
5217     case DW_OP_const2s:
5218       fputc (',', asm_out_file);
5219       dw2_asm_output_data_raw (2, val1->v.val_int);
5220       break;
5221
5222     case DW_OP_const4u:
5223     case DW_OP_const4s:
5224       fputc (',', asm_out_file);
5225       dw2_asm_output_data_raw (4, val1->v.val_int);
5226       break;
5227
5228     case DW_OP_const8u:
5229     case DW_OP_const8s:
5230       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5231       fputc (',', asm_out_file);
5232       dw2_asm_output_data_raw (8, val1->v.val_int);
5233       break;
5234
5235     case DW_OP_skip:
5236     case DW_OP_bra:
5237       {
5238         int offset;
5239
5240         gcc_assert (val1->val_class == dw_val_class_loc);
5241         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5242
5243         fputc (',', asm_out_file);
5244         dw2_asm_output_data_raw (2, offset);
5245       }
5246       break;
5247
5248     case DW_OP_constu:
5249     case DW_OP_plus_uconst:
5250     case DW_OP_regx:
5251     case DW_OP_piece:
5252       fputc (',', asm_out_file);
5253       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5254       break;
5255
5256     case DW_OP_bit_piece:
5257       fputc (',', asm_out_file);
5258       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5259       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5260       break;
5261
5262     case DW_OP_consts:
5263     case DW_OP_breg0:
5264     case DW_OP_breg1:
5265     case DW_OP_breg2:
5266     case DW_OP_breg3:
5267     case DW_OP_breg4:
5268     case DW_OP_breg5:
5269     case DW_OP_breg6:
5270     case DW_OP_breg7:
5271     case DW_OP_breg8:
5272     case DW_OP_breg9:
5273     case DW_OP_breg10:
5274     case DW_OP_breg11:
5275     case DW_OP_breg12:
5276     case DW_OP_breg13:
5277     case DW_OP_breg14:
5278     case DW_OP_breg15:
5279     case DW_OP_breg16:
5280     case DW_OP_breg17:
5281     case DW_OP_breg18:
5282     case DW_OP_breg19:
5283     case DW_OP_breg20:
5284     case DW_OP_breg21:
5285     case DW_OP_breg22:
5286     case DW_OP_breg23:
5287     case DW_OP_breg24:
5288     case DW_OP_breg25:
5289     case DW_OP_breg26:
5290     case DW_OP_breg27:
5291     case DW_OP_breg28:
5292     case DW_OP_breg29:
5293     case DW_OP_breg30:
5294     case DW_OP_breg31:
5295     case DW_OP_fbreg:
5296       fputc (',', asm_out_file);
5297       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5298       break;
5299
5300     case DW_OP_bregx:
5301       fputc (',', asm_out_file);
5302       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5303       fputc (',', asm_out_file);
5304       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5305       break;
5306
5307     default:
5308       /* Other codes have no operands.  */
5309       break;
5310     }
5311 }
5312
5313 static void
5314 output_loc_sequence_raw (dw_loc_descr_ref loc)
5315 {
5316   while (1)
5317     {
5318       /* Output the opcode.  */
5319       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5320       output_loc_operands_raw (loc);
5321
5322       if (!loc->dw_loc_next)
5323         break;
5324       loc = loc->dw_loc_next;
5325
5326       fputc (',', asm_out_file);
5327     }
5328 }
5329
5330 /* This routine will generate the correct assembly data for a location
5331    description based on a cfi entry with a complex address.  */
5332
5333 static void
5334 output_cfa_loc (dw_cfi_ref cfi)
5335 {
5336   dw_loc_descr_ref loc;
5337   unsigned long size;
5338
5339   if (cfi->dw_cfi_opc == DW_CFA_expression)
5340     {
5341       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5342       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5343     }
5344   else
5345     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5346
5347   /* Output the size of the block.  */
5348   size = size_of_locs (loc);
5349   dw2_asm_output_data_uleb128 (size, NULL);
5350
5351   /* Now output the operations themselves.  */
5352   output_loc_sequence (loc);
5353 }
5354
5355 /* Similar, but used for .cfi_escape.  */
5356
5357 static void
5358 output_cfa_loc_raw (dw_cfi_ref cfi)
5359 {
5360   dw_loc_descr_ref loc;
5361   unsigned long size;
5362
5363   if (cfi->dw_cfi_opc == DW_CFA_expression)
5364     {
5365       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5366       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5367     }
5368   else
5369     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5370
5371   /* Output the size of the block.  */
5372   size = size_of_locs (loc);
5373   dw2_asm_output_data_uleb128_raw (size);
5374   fputc (',', asm_out_file);
5375
5376   /* Now output the operations themselves.  */
5377   output_loc_sequence_raw (loc);
5378 }
5379
5380 /* This function builds a dwarf location descriptor sequence from a
5381    dw_cfa_location, adding the given OFFSET to the result of the
5382    expression.  */
5383
5384 static struct dw_loc_descr_struct *
5385 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5386 {
5387   struct dw_loc_descr_struct *head, *tmp;
5388
5389   offset += cfa->offset;
5390
5391   if (cfa->indirect)
5392     {
5393       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5394       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5395       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5396       add_loc_descr (&head, tmp);
5397       if (offset != 0)
5398         {
5399           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5400           add_loc_descr (&head, tmp);
5401         }
5402     }
5403   else
5404     head = new_reg_loc_descr (cfa->reg, offset);
5405
5406   return head;
5407 }
5408
5409 /* This function builds a dwarf location descriptor sequence for
5410    the address at OFFSET from the CFA when stack is aligned to
5411    ALIGNMENT byte.  */
5412
5413 static struct dw_loc_descr_struct *
5414 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5415 {
5416   struct dw_loc_descr_struct *head;
5417   unsigned int dwarf_fp
5418     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5419
5420  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5421   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5422     {
5423       head = new_reg_loc_descr (dwarf_fp, 0);
5424       add_loc_descr (&head, int_loc_descriptor (alignment));
5425       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5426       loc_descr_plus_const (&head, offset);
5427     }
5428   else
5429     head = new_reg_loc_descr (dwarf_fp, offset);
5430   return head;
5431 }
5432
5433 /* This function fills in aa dw_cfa_location structure from a dwarf location
5434    descriptor sequence.  */
5435
5436 static void
5437 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5438 {
5439   struct dw_loc_descr_struct *ptr;
5440   cfa->offset = 0;
5441   cfa->base_offset = 0;
5442   cfa->indirect = 0;
5443   cfa->reg = -1;
5444
5445   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5446     {
5447       enum dwarf_location_atom op = ptr->dw_loc_opc;
5448
5449       switch (op)
5450         {
5451         case DW_OP_reg0:
5452         case DW_OP_reg1:
5453         case DW_OP_reg2:
5454         case DW_OP_reg3:
5455         case DW_OP_reg4:
5456         case DW_OP_reg5:
5457         case DW_OP_reg6:
5458         case DW_OP_reg7:
5459         case DW_OP_reg8:
5460         case DW_OP_reg9:
5461         case DW_OP_reg10:
5462         case DW_OP_reg11:
5463         case DW_OP_reg12:
5464         case DW_OP_reg13:
5465         case DW_OP_reg14:
5466         case DW_OP_reg15:
5467         case DW_OP_reg16:
5468         case DW_OP_reg17:
5469         case DW_OP_reg18:
5470         case DW_OP_reg19:
5471         case DW_OP_reg20:
5472         case DW_OP_reg21:
5473         case DW_OP_reg22:
5474         case DW_OP_reg23:
5475         case DW_OP_reg24:
5476         case DW_OP_reg25:
5477         case DW_OP_reg26:
5478         case DW_OP_reg27:
5479         case DW_OP_reg28:
5480         case DW_OP_reg29:
5481         case DW_OP_reg30:
5482         case DW_OP_reg31:
5483           cfa->reg = op - DW_OP_reg0;
5484           break;
5485         case DW_OP_regx:
5486           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5487           break;
5488         case DW_OP_breg0:
5489         case DW_OP_breg1:
5490         case DW_OP_breg2:
5491         case DW_OP_breg3:
5492         case DW_OP_breg4:
5493         case DW_OP_breg5:
5494         case DW_OP_breg6:
5495         case DW_OP_breg7:
5496         case DW_OP_breg8:
5497         case DW_OP_breg9:
5498         case DW_OP_breg10:
5499         case DW_OP_breg11:
5500         case DW_OP_breg12:
5501         case DW_OP_breg13:
5502         case DW_OP_breg14:
5503         case DW_OP_breg15:
5504         case DW_OP_breg16:
5505         case DW_OP_breg17:
5506         case DW_OP_breg18:
5507         case DW_OP_breg19:
5508         case DW_OP_breg20:
5509         case DW_OP_breg21:
5510         case DW_OP_breg22:
5511         case DW_OP_breg23:
5512         case DW_OP_breg24:
5513         case DW_OP_breg25:
5514         case DW_OP_breg26:
5515         case DW_OP_breg27:
5516         case DW_OP_breg28:
5517         case DW_OP_breg29:
5518         case DW_OP_breg30:
5519         case DW_OP_breg31:
5520           cfa->reg = op - DW_OP_breg0;
5521           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5522           break;
5523         case DW_OP_bregx:
5524           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5525           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5526           break;
5527         case DW_OP_deref:
5528           cfa->indirect = 1;
5529           break;
5530         case DW_OP_plus_uconst:
5531           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5532           break;
5533         default:
5534           internal_error ("DW_LOC_OP %s not implemented",
5535                           dwarf_stack_op_name (ptr->dw_loc_opc));
5536         }
5537     }
5538 }
5539 \f
5540 /* And now, the support for symbolic debugging information.  */
5541
5542 /* .debug_str support.  */
5543 static int output_indirect_string (void **, void *);
5544
5545 static void dwarf2out_init (const char *);
5546 static void dwarf2out_finish (const char *);
5547 static void dwarf2out_assembly_start (void);
5548 static void dwarf2out_define (unsigned int, const char *);
5549 static void dwarf2out_undef (unsigned int, const char *);
5550 static void dwarf2out_start_source_file (unsigned, const char *);
5551 static void dwarf2out_end_source_file (unsigned);
5552 static void dwarf2out_function_decl (tree);
5553 static void dwarf2out_begin_block (unsigned, unsigned);
5554 static void dwarf2out_end_block (unsigned, unsigned);
5555 static bool dwarf2out_ignore_block (const_tree);
5556 static void dwarf2out_global_decl (tree);
5557 static void dwarf2out_type_decl (tree, int);
5558 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5559 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5560                                                  dw_die_ref);
5561 static void dwarf2out_abstract_function (tree);
5562 static void dwarf2out_var_location (rtx);
5563 static void dwarf2out_direct_call (tree);
5564 static void dwarf2out_virtual_call_token (tree, int);
5565 static void dwarf2out_copy_call_info (rtx, rtx);
5566 static void dwarf2out_virtual_call (int);
5567 static void dwarf2out_begin_function (tree);
5568 static void dwarf2out_set_name (tree, tree);
5569
5570 /* The debug hooks structure.  */
5571
5572 const struct gcc_debug_hooks dwarf2_debug_hooks =
5573 {
5574   dwarf2out_init,
5575   dwarf2out_finish,
5576   dwarf2out_assembly_start,
5577   dwarf2out_define,
5578   dwarf2out_undef,
5579   dwarf2out_start_source_file,
5580   dwarf2out_end_source_file,
5581   dwarf2out_begin_block,
5582   dwarf2out_end_block,
5583   dwarf2out_ignore_block,
5584   dwarf2out_source_line,
5585   dwarf2out_begin_prologue,
5586 #if VMS_DEBUGGING_INFO
5587   dwarf2out_vms_end_prologue,
5588   dwarf2out_vms_begin_epilogue,
5589 #else
5590   debug_nothing_int_charstar,
5591   debug_nothing_int_charstar,
5592 #endif
5593   dwarf2out_end_epilogue,
5594   dwarf2out_begin_function,
5595   debug_nothing_int,            /* end_function */
5596   dwarf2out_function_decl,      /* function_decl */
5597   dwarf2out_global_decl,
5598   dwarf2out_type_decl,          /* type_decl */
5599   dwarf2out_imported_module_or_decl,
5600   debug_nothing_tree,           /* deferred_inline_function */
5601   /* The DWARF 2 backend tries to reduce debugging bloat by not
5602      emitting the abstract description of inline functions until
5603      something tries to reference them.  */
5604   dwarf2out_abstract_function,  /* outlining_inline_function */
5605   debug_nothing_rtx,            /* label */
5606   debug_nothing_int,            /* handle_pch */
5607   dwarf2out_var_location,
5608   dwarf2out_switch_text_section,
5609   dwarf2out_direct_call,
5610   dwarf2out_virtual_call_token,
5611   dwarf2out_copy_call_info,
5612   dwarf2out_virtual_call,
5613   dwarf2out_set_name,
5614   1                             /* start_end_main_source_file */
5615 };
5616 \f
5617 /* NOTE: In the comments in this file, many references are made to
5618    "Debugging Information Entries".  This term is abbreviated as `DIE'
5619    throughout the remainder of this file.  */
5620
5621 /* An internal representation of the DWARF output is built, and then
5622    walked to generate the DWARF debugging info.  The walk of the internal
5623    representation is done after the entire program has been compiled.
5624    The types below are used to describe the internal representation.  */
5625
5626 /* Various DIE's use offsets relative to the beginning of the
5627    .debug_info section to refer to each other.  */
5628
5629 typedef long int dw_offset;
5630
5631 /* Define typedefs here to avoid circular dependencies.  */
5632
5633 typedef struct dw_attr_struct *dw_attr_ref;
5634 typedef struct dw_line_info_struct *dw_line_info_ref;
5635 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5636 typedef struct pubname_struct *pubname_ref;
5637 typedef struct dw_ranges_struct *dw_ranges_ref;
5638 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5639 typedef struct comdat_type_struct *comdat_type_node_ref;
5640
5641 /* Each entry in the line_info_table maintains the file and
5642    line number associated with the label generated for that
5643    entry.  The label gives the PC value associated with
5644    the line number entry.  */
5645
5646 typedef struct GTY(()) dw_line_info_struct {
5647   unsigned long dw_file_num;
5648   unsigned long dw_line_num;
5649 }
5650 dw_line_info_entry;
5651
5652 /* Line information for functions in separate sections; each one gets its
5653    own sequence.  */
5654 typedef struct GTY(()) dw_separate_line_info_struct {
5655   unsigned long dw_file_num;
5656   unsigned long dw_line_num;
5657   unsigned long function;
5658 }
5659 dw_separate_line_info_entry;
5660
5661 /* Each DIE attribute has a field specifying the attribute kind,
5662    a link to the next attribute in the chain, and an attribute value.
5663    Attributes are typically linked below the DIE they modify.  */
5664
5665 typedef struct GTY(()) dw_attr_struct {
5666   enum dwarf_attribute dw_attr;
5667   dw_val_node dw_attr_val;
5668 }
5669 dw_attr_node;
5670
5671 DEF_VEC_O(dw_attr_node);
5672 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5673
5674 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5675    The children of each node form a circular list linked by
5676    die_sib.  die_child points to the node *before* the "first" child node.  */
5677
5678 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5679   union die_symbol_or_type_node
5680     {
5681       char * GTY ((tag ("0"))) die_symbol;
5682       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5683     }
5684   GTY ((desc ("dwarf_version >= 4"))) die_id;
5685   VEC(dw_attr_node,gc) * die_attr;
5686   dw_die_ref die_parent;
5687   dw_die_ref die_child;
5688   dw_die_ref die_sib;
5689   dw_die_ref die_definition; /* ref from a specification to its definition */
5690   dw_offset die_offset;
5691   unsigned long die_abbrev;
5692   int die_mark;
5693   /* Die is used and must not be pruned as unused.  */
5694   int die_perennial_p;
5695   unsigned int decl_id;
5696   enum dwarf_tag die_tag;
5697 }
5698 die_node;
5699
5700 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5701 #define FOR_EACH_CHILD(die, c, expr) do {       \
5702   c = die->die_child;                           \
5703   if (c) do {                                   \
5704     c = c->die_sib;                             \
5705     expr;                                       \
5706   } while (c != die->die_child);                \
5707 } while (0)
5708
5709 /* The pubname structure */
5710
5711 typedef struct GTY(()) pubname_struct {
5712   dw_die_ref die;
5713   const char *name;
5714 }
5715 pubname_entry;
5716
5717 DEF_VEC_O(pubname_entry);
5718 DEF_VEC_ALLOC_O(pubname_entry, gc);
5719
5720 struct GTY(()) dw_ranges_struct {
5721   /* If this is positive, it's a block number, otherwise it's a
5722      bitwise-negated index into dw_ranges_by_label.  */
5723   int num;
5724 };
5725
5726 struct GTY(()) dw_ranges_by_label_struct {
5727   const char *begin;
5728   const char *end;
5729 };
5730
5731 /* The comdat type node structure.  */
5732 typedef struct GTY(()) comdat_type_struct
5733 {
5734   dw_die_ref root_die;
5735   dw_die_ref type_die;
5736   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5737   struct comdat_type_struct *next;
5738 }
5739 comdat_type_node;
5740
5741 /* The limbo die list structure.  */
5742 typedef struct GTY(()) limbo_die_struct {
5743   dw_die_ref die;
5744   tree created_for;
5745   struct limbo_die_struct *next;
5746 }
5747 limbo_die_node;
5748
5749 typedef struct GTY(()) skeleton_chain_struct
5750 {
5751   dw_die_ref old_die;
5752   dw_die_ref new_die;
5753   struct skeleton_chain_struct *parent;
5754 }
5755 skeleton_chain_node;
5756
5757 /* How to start an assembler comment.  */
5758 #ifndef ASM_COMMENT_START
5759 #define ASM_COMMENT_START ";#"
5760 #endif
5761
5762 /* Define a macro which returns nonzero for a TYPE_DECL which was
5763    implicitly generated for a tagged type.
5764
5765    Note that unlike the gcc front end (which generates a NULL named
5766    TYPE_DECL node for each complete tagged type, each array type, and
5767    each function type node created) the g++ front end generates a
5768    _named_ TYPE_DECL node for each tagged type node created.
5769    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5770    generate a DW_TAG_typedef DIE for them.  */
5771
5772 #define TYPE_DECL_IS_STUB(decl)                         \
5773   (DECL_NAME (decl) == NULL_TREE                        \
5774    || (DECL_ARTIFICIAL (decl)                           \
5775        && is_tagged_type (TREE_TYPE (decl))             \
5776        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5777            /* This is necessary for stub decls that     \
5778               appear in nested inline functions.  */    \
5779            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5780                && (decl_ultimate_origin (decl)          \
5781                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5782
5783 /* Information concerning the compilation unit's programming
5784    language, and compiler version.  */
5785
5786 /* Fixed size portion of the DWARF compilation unit header.  */
5787 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5788   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5789
5790 /* Fixed size portion of the DWARF comdat type unit header.  */
5791 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5792   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5793    + DWARF_OFFSET_SIZE)
5794
5795 /* Fixed size portion of public names info.  */
5796 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5797
5798 /* Fixed size portion of the address range info.  */
5799 #define DWARF_ARANGES_HEADER_SIZE                                       \
5800   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5801                 DWARF2_ADDR_SIZE * 2)                                   \
5802    - DWARF_INITIAL_LENGTH_SIZE)
5803
5804 /* Size of padding portion in the address range info.  It must be
5805    aligned to twice the pointer size.  */
5806 #define DWARF_ARANGES_PAD_SIZE \
5807   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5808                 DWARF2_ADDR_SIZE * 2)                              \
5809    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5810
5811 /* Use assembler line directives if available.  */
5812 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5813 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5814 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5815 #else
5816 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5817 #endif
5818 #endif
5819
5820 /* Minimum line offset in a special line info. opcode.
5821    This value was chosen to give a reasonable range of values.  */
5822 #define DWARF_LINE_BASE  -10
5823
5824 /* First special line opcode - leave room for the standard opcodes.  */
5825 #define DWARF_LINE_OPCODE_BASE  10
5826
5827 /* Range of line offsets in a special line info. opcode.  */
5828 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5829
5830 /* Flag that indicates the initial value of the is_stmt_start flag.
5831    In the present implementation, we do not mark any lines as
5832    the beginning of a source statement, because that information
5833    is not made available by the GCC front-end.  */
5834 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5835
5836 /* Maximum number of operations per instruction bundle.  */
5837 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5838 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5839 #endif
5840
5841 /* This location is used by calc_die_sizes() to keep track
5842    the offset of each DIE within the .debug_info section.  */
5843 static unsigned long next_die_offset;
5844
5845 /* Record the root of the DIE's built for the current compilation unit.  */
5846 static GTY(()) dw_die_ref comp_unit_die;
5847
5848 /* A list of type DIEs that have been separated into comdat sections.  */
5849 static GTY(()) comdat_type_node *comdat_type_list;
5850
5851 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5852 static GTY(()) limbo_die_node *limbo_die_list;
5853
5854 /* A list of DIEs for which we may have to generate
5855    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5856 static GTY(()) limbo_die_node *deferred_asm_name;
5857
5858 /* Filenames referenced by this compilation unit.  */
5859 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5860
5861 /* A hash table of references to DIE's that describe declarations.
5862    The key is a DECL_UID() which is a unique number identifying each decl.  */
5863 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5864
5865 /* A hash table of references to DIE's that describe COMMON blocks.
5866    The key is DECL_UID() ^ die_parent.  */
5867 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5868
5869 typedef struct GTY(()) die_arg_entry_struct {
5870     dw_die_ref die;
5871     tree arg;
5872 } die_arg_entry;
5873
5874 DEF_VEC_O(die_arg_entry);
5875 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5876
5877 /* Node of the variable location list.  */
5878 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5879   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5880      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5881      in mode of the EXPR_LIST node and first EXPR_LIST operand
5882      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5883      location or NULL for padding.  For larger bitsizes,
5884      mode is 0 and first operand is a CONCAT with bitsize
5885      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5886      NULL as second operand.  */
5887   rtx GTY (()) loc;
5888   const char * GTY (()) label;
5889   struct var_loc_node * GTY (()) next;
5890 };
5891
5892 /* Variable location list.  */
5893 struct GTY (()) var_loc_list_def {
5894   struct var_loc_node * GTY (()) first;
5895
5896   /* Pointer to the last but one or last element of the
5897      chained list.  If the list is empty, both first and
5898      last are NULL, if the list contains just one node
5899      or the last node certainly is not redundant, it points
5900      to the last node, otherwise points to the last but one.
5901      Do not mark it for GC because it is marked through the chain.  */
5902   struct var_loc_node * GTY ((skip ("%h"))) last;
5903
5904   /* DECL_UID of the variable decl.  */
5905   unsigned int decl_id;
5906 };
5907 typedef struct var_loc_list_def var_loc_list;
5908
5909
5910 /* Table of decl location linked lists.  */
5911 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5912
5913 /* A pointer to the base of a list of references to DIE's that
5914    are uniquely identified by their tag, presence/absence of
5915    children DIE's, and list of attribute/value pairs.  */
5916 static GTY((length ("abbrev_die_table_allocated")))
5917   dw_die_ref *abbrev_die_table;
5918
5919 /* Number of elements currently allocated for abbrev_die_table.  */
5920 static GTY(()) unsigned abbrev_die_table_allocated;
5921
5922 /* Number of elements in type_die_table currently in use.  */
5923 static GTY(()) unsigned abbrev_die_table_in_use;
5924
5925 /* Size (in elements) of increments by which we may expand the
5926    abbrev_die_table.  */
5927 #define ABBREV_DIE_TABLE_INCREMENT 256
5928
5929 /* A pointer to the base of a table that contains line information
5930    for each source code line in .text in the compilation unit.  */
5931 static GTY((length ("line_info_table_allocated")))
5932      dw_line_info_ref line_info_table;
5933
5934 /* Number of elements currently allocated for line_info_table.  */
5935 static GTY(()) unsigned line_info_table_allocated;
5936
5937 /* Number of elements in line_info_table currently in use.  */
5938 static GTY(()) unsigned line_info_table_in_use;
5939
5940 /* A pointer to the base of a table that contains line information
5941    for each source code line outside of .text in the compilation unit.  */
5942 static GTY ((length ("separate_line_info_table_allocated")))
5943      dw_separate_line_info_ref separate_line_info_table;
5944
5945 /* Number of elements currently allocated for separate_line_info_table.  */
5946 static GTY(()) unsigned separate_line_info_table_allocated;
5947
5948 /* Number of elements in separate_line_info_table currently in use.  */
5949 static GTY(()) unsigned separate_line_info_table_in_use;
5950
5951 /* Size (in elements) of increments by which we may expand the
5952    line_info_table.  */
5953 #define LINE_INFO_TABLE_INCREMENT 1024
5954
5955 /* A pointer to the base of a table that contains a list of publicly
5956    accessible names.  */
5957 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5958
5959 /* A pointer to the base of a table that contains a list of publicly
5960    accessible types.  */
5961 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5962
5963 /* Array of dies for which we should generate .debug_arange info.  */
5964 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5965
5966 /* Number of elements currently allocated for arange_table.  */
5967 static GTY(()) unsigned arange_table_allocated;
5968
5969 /* Number of elements in arange_table currently in use.  */
5970 static GTY(()) unsigned arange_table_in_use;
5971
5972 /* Size (in elements) of increments by which we may expand the
5973    arange_table.  */
5974 #define ARANGE_TABLE_INCREMENT 64
5975
5976 /* Array of dies for which we should generate .debug_ranges info.  */
5977 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5978
5979 /* Number of elements currently allocated for ranges_table.  */
5980 static GTY(()) unsigned ranges_table_allocated;
5981
5982 /* Number of elements in ranges_table currently in use.  */
5983 static GTY(()) unsigned ranges_table_in_use;
5984
5985 /* Array of pairs of labels referenced in ranges_table.  */
5986 static GTY ((length ("ranges_by_label_allocated")))
5987      dw_ranges_by_label_ref ranges_by_label;
5988
5989 /* Number of elements currently allocated for ranges_by_label.  */
5990 static GTY(()) unsigned ranges_by_label_allocated;
5991
5992 /* Number of elements in ranges_by_label currently in use.  */
5993 static GTY(()) unsigned ranges_by_label_in_use;
5994
5995 /* Size (in elements) of increments by which we may expand the
5996    ranges_table.  */
5997 #define RANGES_TABLE_INCREMENT 64
5998
5999 /* Whether we have location lists that need outputting */
6000 static GTY(()) bool have_location_lists;
6001
6002 /* Unique label counter.  */
6003 static GTY(()) unsigned int loclabel_num;
6004
6005 /* Unique label counter for point-of-call tables.  */
6006 static GTY(()) unsigned int poc_label_num;
6007
6008 /* The direct call table structure.  */
6009
6010 typedef struct GTY(()) dcall_struct {
6011   unsigned int poc_label_num;
6012   tree poc_decl;
6013   dw_die_ref targ_die;
6014 }
6015 dcall_entry;
6016
6017 DEF_VEC_O(dcall_entry);
6018 DEF_VEC_ALLOC_O(dcall_entry, gc);
6019
6020 /* The virtual call table structure.  */
6021
6022 typedef struct GTY(()) vcall_struct {
6023   unsigned int poc_label_num;
6024   unsigned int vtable_slot;
6025 }
6026 vcall_entry;
6027
6028 DEF_VEC_O(vcall_entry);
6029 DEF_VEC_ALLOC_O(vcall_entry, gc);
6030
6031 /* Pointers to the direct and virtual call tables.  */
6032 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6033 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6034
6035 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6036
6037 struct GTY (()) vcall_insn {
6038   int insn_uid;
6039   unsigned int vtable_slot;
6040 };
6041
6042 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6043
6044 /* Record whether the function being analyzed contains inlined functions.  */
6045 static int current_function_has_inlines;
6046 #if 0 && defined (MIPS_DEBUGGING_INFO)
6047 static int comp_unit_has_inlines;
6048 #endif
6049
6050 /* The last file entry emitted by maybe_emit_file().  */
6051 static GTY(()) struct dwarf_file_data * last_emitted_file;
6052
6053 /* Number of internal labels generated by gen_internal_sym().  */
6054 static GTY(()) int label_num;
6055
6056 /* Cached result of previous call to lookup_filename.  */
6057 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6058
6059 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6060
6061 /* Offset from the "steady-state frame pointer" to the frame base,
6062    within the current function.  */
6063 static HOST_WIDE_INT frame_pointer_fb_offset;
6064
6065 /* Forward declarations for functions defined in this file.  */
6066
6067 static int is_pseudo_reg (const_rtx);
6068 static tree type_main_variant (tree);
6069 static int is_tagged_type (const_tree);
6070 static const char *dwarf_tag_name (unsigned);
6071 static const char *dwarf_attr_name (unsigned);
6072 static const char *dwarf_form_name (unsigned);
6073 static tree decl_ultimate_origin (const_tree);
6074 static tree decl_class_context (tree);
6075 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6076 static inline enum dw_val_class AT_class (dw_attr_ref);
6077 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6078 static inline unsigned AT_flag (dw_attr_ref);
6079 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6080 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6081 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6082 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6083 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6084                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6085 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6086                                unsigned int, unsigned char *);
6087 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6088 static hashval_t debug_str_do_hash (const void *);
6089 static int debug_str_eq (const void *, const void *);
6090 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6091 static inline const char *AT_string (dw_attr_ref);
6092 static enum dwarf_form AT_string_form (dw_attr_ref);
6093 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6094 static void add_AT_specification (dw_die_ref, dw_die_ref);
6095 static inline dw_die_ref AT_ref (dw_attr_ref);
6096 static inline int AT_ref_external (dw_attr_ref);
6097 static inline void set_AT_ref_external (dw_attr_ref, int);
6098 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6099 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6100 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6101 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6102                              dw_loc_list_ref);
6103 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6104 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6105 static inline rtx AT_addr (dw_attr_ref);
6106 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6107 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6108 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6109 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6110                            unsigned HOST_WIDE_INT);
6111 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6112                                unsigned long);
6113 static inline const char *AT_lbl (dw_attr_ref);
6114 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6115 static const char *get_AT_low_pc (dw_die_ref);
6116 static const char *get_AT_hi_pc (dw_die_ref);
6117 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6118 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6119 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6120 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6121 static bool is_cxx (void);
6122 static bool is_fortran (void);
6123 static bool is_ada (void);
6124 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6125 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6126 static void add_child_die (dw_die_ref, dw_die_ref);
6127 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6128 static dw_die_ref lookup_type_die (tree);
6129 static void equate_type_number_to_die (tree, dw_die_ref);
6130 static hashval_t decl_die_table_hash (const void *);
6131 static int decl_die_table_eq (const void *, const void *);
6132 static dw_die_ref lookup_decl_die (tree);
6133 static hashval_t common_block_die_table_hash (const void *);
6134 static int common_block_die_table_eq (const void *, const void *);
6135 static hashval_t decl_loc_table_hash (const void *);
6136 static int decl_loc_table_eq (const void *, const void *);
6137 static var_loc_list *lookup_decl_loc (const_tree);
6138 static void equate_decl_number_to_die (tree, dw_die_ref);
6139 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6140 static void print_spaces (FILE *);
6141 static void print_die (dw_die_ref, FILE *);
6142 static void print_dwarf_line_table (FILE *);
6143 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6144 static dw_die_ref pop_compile_unit (dw_die_ref);
6145 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6146 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6147 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6148 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6149 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6150 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6151 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6152                                    struct md5_ctx *, int *);
6153 struct checksum_attributes;
6154 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6155 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6156 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6157 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6158 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6159 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6160 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6161 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6162 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6163 static void compute_section_prefix (dw_die_ref);
6164 static int is_type_die (dw_die_ref);
6165 static int is_comdat_die (dw_die_ref);
6166 static int is_symbol_die (dw_die_ref);
6167 static void assign_symbol_names (dw_die_ref);
6168 static void break_out_includes (dw_die_ref);
6169 static int is_declaration_die (dw_die_ref);
6170 static int should_move_die_to_comdat (dw_die_ref);
6171 static dw_die_ref clone_as_declaration (dw_die_ref);
6172 static dw_die_ref clone_die (dw_die_ref);
6173 static dw_die_ref clone_tree (dw_die_ref);
6174 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6175 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6176 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6177 static dw_die_ref generate_skeleton (dw_die_ref);
6178 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6179                                                          dw_die_ref);
6180 static void break_out_comdat_types (dw_die_ref);
6181 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6182 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6183 static void copy_decls_for_unworthy_types (dw_die_ref);
6184
6185 static hashval_t htab_cu_hash (const void *);
6186 static int htab_cu_eq (const void *, const void *);
6187 static void htab_cu_del (void *);
6188 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6189 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6190 static void add_sibling_attributes (dw_die_ref);
6191 static void build_abbrev_table (dw_die_ref);
6192 static void output_location_lists (dw_die_ref);
6193 static int constant_size (unsigned HOST_WIDE_INT);
6194 static unsigned long size_of_die (dw_die_ref);
6195 static void calc_die_sizes (dw_die_ref);
6196 static void mark_dies (dw_die_ref);
6197 static void unmark_dies (dw_die_ref);
6198 static void unmark_all_dies (dw_die_ref);
6199 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6200 static unsigned long size_of_aranges (void);
6201 static enum dwarf_form value_format (dw_attr_ref);
6202 static void output_value_format (dw_attr_ref);
6203 static void output_abbrev_section (void);
6204 static void output_die_symbol (dw_die_ref);
6205 static void output_die (dw_die_ref);
6206 static void output_compilation_unit_header (void);
6207 static void output_comp_unit (dw_die_ref, int);
6208 static void output_comdat_type_unit (comdat_type_node *);
6209 static const char *dwarf2_name (tree, int);
6210 static void add_pubname (tree, dw_die_ref);
6211 static void add_pubname_string (const char *, dw_die_ref);
6212 static void add_pubtype (tree, dw_die_ref);
6213 static void output_pubnames (VEC (pubname_entry,gc) *);
6214 static void add_arange (tree, dw_die_ref);
6215 static void output_aranges (void);
6216 static unsigned int add_ranges_num (int);
6217 static unsigned int add_ranges (const_tree);
6218 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6219                                   bool *);
6220 static void output_ranges (void);
6221 static void output_line_info (void);
6222 static void output_file_names (void);
6223 static dw_die_ref base_type_die (tree);
6224 static int is_base_type (tree);
6225 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6226 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6227 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6228 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6229 static int type_is_enum (const_tree);
6230 static unsigned int dbx_reg_number (const_rtx);
6231 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6232 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6233 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6234                                                 enum var_init_status);
6235 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6236                                                      enum var_init_status);
6237 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6238                                          enum var_init_status);
6239 static int is_based_loc (const_rtx);
6240 static int resolve_one_addr (rtx *, void *);
6241 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6242                                                enum var_init_status);
6243 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6244                                         enum var_init_status);
6245 static dw_loc_list_ref loc_list_from_tree (tree, int);
6246 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6247 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6248 static tree field_type (const_tree);
6249 static unsigned int simple_type_align_in_bits (const_tree);
6250 static unsigned int simple_decl_align_in_bits (const_tree);
6251 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6252 static HOST_WIDE_INT field_byte_offset (const_tree);
6253 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6254                                          dw_loc_list_ref);
6255 static void add_data_member_location_attribute (dw_die_ref, tree);
6256 static bool add_const_value_attribute (dw_die_ref, rtx);
6257 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6258 static void insert_double (double_int, unsigned char *);
6259 static void insert_float (const_rtx, unsigned char *);
6260 static rtx rtl_for_decl_location (tree);
6261 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6262                                                    enum dwarf_attribute);
6263 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6264 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6265 static void add_name_attribute (dw_die_ref, const char *);
6266 static void add_comp_dir_attribute (dw_die_ref);
6267 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6268 static void add_subscript_info (dw_die_ref, tree, bool);
6269 static void add_byte_size_attribute (dw_die_ref, tree);
6270 static void add_bit_offset_attribute (dw_die_ref, tree);
6271 static void add_bit_size_attribute (dw_die_ref, tree);
6272 static void add_prototyped_attribute (dw_die_ref, tree);
6273 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6274 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6275 static void add_src_coords_attributes (dw_die_ref, tree);
6276 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6277 static void push_decl_scope (tree);
6278 static void pop_decl_scope (void);
6279 static dw_die_ref scope_die_for (tree, dw_die_ref);
6280 static inline int local_scope_p (dw_die_ref);
6281 static inline int class_scope_p (dw_die_ref);
6282 static inline int class_or_namespace_scope_p (dw_die_ref);
6283 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6284 static void add_calling_convention_attribute (dw_die_ref, tree);
6285 static const char *type_tag (const_tree);
6286 static tree member_declared_type (const_tree);
6287 #if 0
6288 static const char *decl_start_label (tree);
6289 #endif
6290 static void gen_array_type_die (tree, dw_die_ref);
6291 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6292 #if 0
6293 static void gen_entry_point_die (tree, dw_die_ref);
6294 #endif
6295 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6296 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6297 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6298 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6299 static void gen_formal_types_die (tree, dw_die_ref);
6300 static void gen_subprogram_die (tree, dw_die_ref);
6301 static void gen_variable_die (tree, tree, dw_die_ref);
6302 static void gen_const_die (tree, dw_die_ref);
6303 static void gen_label_die (tree, dw_die_ref);
6304 static void gen_lexical_block_die (tree, dw_die_ref, int);
6305 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6306 static void gen_field_die (tree, dw_die_ref);
6307 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6308 static dw_die_ref gen_compile_unit_die (const char *);
6309 static void gen_inheritance_die (tree, tree, dw_die_ref);
6310 static void gen_member_die (tree, dw_die_ref);
6311 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6312                                                 enum debug_info_usage);
6313 static void gen_subroutine_type_die (tree, dw_die_ref);
6314 static void gen_typedef_die (tree, dw_die_ref);
6315 static void gen_type_die (tree, dw_die_ref);
6316 static void gen_block_die (tree, dw_die_ref, int);
6317 static void decls_for_scope (tree, dw_die_ref, int);
6318 static int is_redundant_typedef (const_tree);
6319 static bool is_naming_typedef_decl (const_tree);
6320 static inline dw_die_ref get_context_die (tree);
6321 static void gen_namespace_die (tree, dw_die_ref);
6322 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6323 static dw_die_ref force_decl_die (tree);
6324 static dw_die_ref force_type_die (tree);
6325 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6326 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6327 static struct dwarf_file_data * lookup_filename (const char *);
6328 static void retry_incomplete_types (void);
6329 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6330 static void gen_generic_params_dies (tree);
6331 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6332 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6333 static void splice_child_die (dw_die_ref, dw_die_ref);
6334 static int file_info_cmp (const void *, const void *);
6335 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6336                                      const char *, const char *);
6337 static void output_loc_list (dw_loc_list_ref);
6338 static char *gen_internal_sym (const char *);
6339
6340 static void prune_unmark_dies (dw_die_ref);
6341 static void prune_unused_types_mark (dw_die_ref, int);
6342 static void prune_unused_types_walk (dw_die_ref);
6343 static void prune_unused_types_walk_attribs (dw_die_ref);
6344 static void prune_unused_types_prune (dw_die_ref);
6345 static void prune_unused_types (void);
6346 static int maybe_emit_file (struct dwarf_file_data *fd);
6347 static inline const char *AT_vms_delta1 (dw_attr_ref);
6348 static inline const char *AT_vms_delta2 (dw_attr_ref);
6349 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6350                                      const char *, const char *);
6351 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6352 static void gen_remaining_tmpl_value_param_die_attribute (void);
6353
6354 /* Section names used to hold DWARF debugging information.  */
6355 #ifndef DEBUG_INFO_SECTION
6356 #define DEBUG_INFO_SECTION      ".debug_info"
6357 #endif
6358 #ifndef DEBUG_ABBREV_SECTION
6359 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6360 #endif
6361 #ifndef DEBUG_ARANGES_SECTION
6362 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6363 #endif
6364 #ifndef DEBUG_MACINFO_SECTION
6365 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6366 #endif
6367 #ifndef DEBUG_LINE_SECTION
6368 #define DEBUG_LINE_SECTION      ".debug_line"
6369 #endif
6370 #ifndef DEBUG_LOC_SECTION
6371 #define DEBUG_LOC_SECTION       ".debug_loc"
6372 #endif
6373 #ifndef DEBUG_PUBNAMES_SECTION
6374 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6375 #endif
6376 #ifndef DEBUG_PUBTYPES_SECTION
6377 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6378 #endif
6379 #ifndef DEBUG_DCALL_SECTION
6380 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6381 #endif
6382 #ifndef DEBUG_VCALL_SECTION
6383 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6384 #endif
6385 #ifndef DEBUG_STR_SECTION
6386 #define DEBUG_STR_SECTION       ".debug_str"
6387 #endif
6388 #ifndef DEBUG_RANGES_SECTION
6389 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6390 #endif
6391
6392 /* Standard ELF section names for compiled code and data.  */
6393 #ifndef TEXT_SECTION_NAME
6394 #define TEXT_SECTION_NAME       ".text"
6395 #endif
6396
6397 /* Section flags for .debug_str section.  */
6398 #define DEBUG_STR_SECTION_FLAGS \
6399   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6400    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6401    : SECTION_DEBUG)
6402
6403 /* Labels we insert at beginning sections we can reference instead of
6404    the section names themselves.  */
6405
6406 #ifndef TEXT_SECTION_LABEL
6407 #define TEXT_SECTION_LABEL              "Ltext"
6408 #endif
6409 #ifndef COLD_TEXT_SECTION_LABEL
6410 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6411 #endif
6412 #ifndef DEBUG_LINE_SECTION_LABEL
6413 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6414 #endif
6415 #ifndef DEBUG_INFO_SECTION_LABEL
6416 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6417 #endif
6418 #ifndef DEBUG_ABBREV_SECTION_LABEL
6419 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6420 #endif
6421 #ifndef DEBUG_LOC_SECTION_LABEL
6422 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6423 #endif
6424 #ifndef DEBUG_RANGES_SECTION_LABEL
6425 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6426 #endif
6427 #ifndef DEBUG_MACINFO_SECTION_LABEL
6428 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6429 #endif
6430
6431
6432 /* Definitions of defaults for formats and names of various special
6433    (artificial) labels which may be generated within this file (when the -g
6434    options is used and DWARF2_DEBUGGING_INFO is in effect.
6435    If necessary, these may be overridden from within the tm.h file, but
6436    typically, overriding these defaults is unnecessary.  */
6437
6438 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6439 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6440 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6441 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6442 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6443 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6444 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6445 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6446 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6447 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6448
6449 #ifndef TEXT_END_LABEL
6450 #define TEXT_END_LABEL          "Letext"
6451 #endif
6452 #ifndef COLD_END_LABEL
6453 #define COLD_END_LABEL          "Letext_cold"
6454 #endif
6455 #ifndef BLOCK_BEGIN_LABEL
6456 #define BLOCK_BEGIN_LABEL       "LBB"
6457 #endif
6458 #ifndef BLOCK_END_LABEL
6459 #define BLOCK_END_LABEL         "LBE"
6460 #endif
6461 #ifndef LINE_CODE_LABEL
6462 #define LINE_CODE_LABEL         "LM"
6463 #endif
6464 #ifndef SEPARATE_LINE_CODE_LABEL
6465 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6466 #endif
6467
6468 \f
6469 /* We allow a language front-end to designate a function that is to be
6470    called to "demangle" any name before it is put into a DIE.  */
6471
6472 static const char *(*demangle_name_func) (const char *);
6473
6474 void
6475 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6476 {
6477   demangle_name_func = func;
6478 }
6479
6480 /* Test if rtl node points to a pseudo register.  */
6481
6482 static inline int
6483 is_pseudo_reg (const_rtx rtl)
6484 {
6485   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6486           || (GET_CODE (rtl) == SUBREG
6487               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6488 }
6489
6490 /* Return a reference to a type, with its const and volatile qualifiers
6491    removed.  */
6492
6493 static inline tree
6494 type_main_variant (tree type)
6495 {
6496   type = TYPE_MAIN_VARIANT (type);
6497
6498   /* ??? There really should be only one main variant among any group of
6499      variants of a given type (and all of the MAIN_VARIANT values for all
6500      members of the group should point to that one type) but sometimes the C
6501      front-end messes this up for array types, so we work around that bug
6502      here.  */
6503   if (TREE_CODE (type) == ARRAY_TYPE)
6504     while (type != TYPE_MAIN_VARIANT (type))
6505       type = TYPE_MAIN_VARIANT (type);
6506
6507   return type;
6508 }
6509
6510 /* Return nonzero if the given type node represents a tagged type.  */
6511
6512 static inline int
6513 is_tagged_type (const_tree type)
6514 {
6515   enum tree_code code = TREE_CODE (type);
6516
6517   return (code == RECORD_TYPE || code == UNION_TYPE
6518           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6519 }
6520
6521 /* Convert a DIE tag into its string name.  */
6522
6523 static const char *
6524 dwarf_tag_name (unsigned int tag)
6525 {
6526   switch (tag)
6527     {
6528     case DW_TAG_padding:
6529       return "DW_TAG_padding";
6530     case DW_TAG_array_type:
6531       return "DW_TAG_array_type";
6532     case DW_TAG_class_type:
6533       return "DW_TAG_class_type";
6534     case DW_TAG_entry_point:
6535       return "DW_TAG_entry_point";
6536     case DW_TAG_enumeration_type:
6537       return "DW_TAG_enumeration_type";
6538     case DW_TAG_formal_parameter:
6539       return "DW_TAG_formal_parameter";
6540     case DW_TAG_imported_declaration:
6541       return "DW_TAG_imported_declaration";
6542     case DW_TAG_label:
6543       return "DW_TAG_label";
6544     case DW_TAG_lexical_block:
6545       return "DW_TAG_lexical_block";
6546     case DW_TAG_member:
6547       return "DW_TAG_member";
6548     case DW_TAG_pointer_type:
6549       return "DW_TAG_pointer_type";
6550     case DW_TAG_reference_type:
6551       return "DW_TAG_reference_type";
6552     case DW_TAG_compile_unit:
6553       return "DW_TAG_compile_unit";
6554     case DW_TAG_string_type:
6555       return "DW_TAG_string_type";
6556     case DW_TAG_structure_type:
6557       return "DW_TAG_structure_type";
6558     case DW_TAG_subroutine_type:
6559       return "DW_TAG_subroutine_type";
6560     case DW_TAG_typedef:
6561       return "DW_TAG_typedef";
6562     case DW_TAG_union_type:
6563       return "DW_TAG_union_type";
6564     case DW_TAG_unspecified_parameters:
6565       return "DW_TAG_unspecified_parameters";
6566     case DW_TAG_variant:
6567       return "DW_TAG_variant";
6568     case DW_TAG_common_block:
6569       return "DW_TAG_common_block";
6570     case DW_TAG_common_inclusion:
6571       return "DW_TAG_common_inclusion";
6572     case DW_TAG_inheritance:
6573       return "DW_TAG_inheritance";
6574     case DW_TAG_inlined_subroutine:
6575       return "DW_TAG_inlined_subroutine";
6576     case DW_TAG_module:
6577       return "DW_TAG_module";
6578     case DW_TAG_ptr_to_member_type:
6579       return "DW_TAG_ptr_to_member_type";
6580     case DW_TAG_set_type:
6581       return "DW_TAG_set_type";
6582     case DW_TAG_subrange_type:
6583       return "DW_TAG_subrange_type";
6584     case DW_TAG_with_stmt:
6585       return "DW_TAG_with_stmt";
6586     case DW_TAG_access_declaration:
6587       return "DW_TAG_access_declaration";
6588     case DW_TAG_base_type:
6589       return "DW_TAG_base_type";
6590     case DW_TAG_catch_block:
6591       return "DW_TAG_catch_block";
6592     case DW_TAG_const_type:
6593       return "DW_TAG_const_type";
6594     case DW_TAG_constant:
6595       return "DW_TAG_constant";
6596     case DW_TAG_enumerator:
6597       return "DW_TAG_enumerator";
6598     case DW_TAG_file_type:
6599       return "DW_TAG_file_type";
6600     case DW_TAG_friend:
6601       return "DW_TAG_friend";
6602     case DW_TAG_namelist:
6603       return "DW_TAG_namelist";
6604     case DW_TAG_namelist_item:
6605       return "DW_TAG_namelist_item";
6606     case DW_TAG_packed_type:
6607       return "DW_TAG_packed_type";
6608     case DW_TAG_subprogram:
6609       return "DW_TAG_subprogram";
6610     case DW_TAG_template_type_param:
6611       return "DW_TAG_template_type_param";
6612     case DW_TAG_template_value_param:
6613       return "DW_TAG_template_value_param";
6614     case DW_TAG_thrown_type:
6615       return "DW_TAG_thrown_type";
6616     case DW_TAG_try_block:
6617       return "DW_TAG_try_block";
6618     case DW_TAG_variant_part:
6619       return "DW_TAG_variant_part";
6620     case DW_TAG_variable:
6621       return "DW_TAG_variable";
6622     case DW_TAG_volatile_type:
6623       return "DW_TAG_volatile_type";
6624     case DW_TAG_dwarf_procedure:
6625       return "DW_TAG_dwarf_procedure";
6626     case DW_TAG_restrict_type:
6627       return "DW_TAG_restrict_type";
6628     case DW_TAG_interface_type:
6629       return "DW_TAG_interface_type";
6630     case DW_TAG_namespace:
6631       return "DW_TAG_namespace";
6632     case DW_TAG_imported_module:
6633       return "DW_TAG_imported_module";
6634     case DW_TAG_unspecified_type:
6635       return "DW_TAG_unspecified_type";
6636     case DW_TAG_partial_unit:
6637       return "DW_TAG_partial_unit";
6638     case DW_TAG_imported_unit:
6639       return "DW_TAG_imported_unit";
6640     case DW_TAG_condition:
6641       return "DW_TAG_condition";
6642     case DW_TAG_shared_type:
6643       return "DW_TAG_shared_type";
6644     case DW_TAG_type_unit:
6645       return "DW_TAG_type_unit";
6646     case DW_TAG_rvalue_reference_type:
6647       return "DW_TAG_rvalue_reference_type";
6648     case DW_TAG_template_alias:
6649       return "DW_TAG_template_alias";
6650     case DW_TAG_GNU_template_parameter_pack:
6651       return "DW_TAG_GNU_template_parameter_pack";
6652     case DW_TAG_GNU_formal_parameter_pack:
6653       return "DW_TAG_GNU_formal_parameter_pack";
6654     case DW_TAG_MIPS_loop:
6655       return "DW_TAG_MIPS_loop";
6656     case DW_TAG_format_label:
6657       return "DW_TAG_format_label";
6658     case DW_TAG_function_template:
6659       return "DW_TAG_function_template";
6660     case DW_TAG_class_template:
6661       return "DW_TAG_class_template";
6662     case DW_TAG_GNU_BINCL:
6663       return "DW_TAG_GNU_BINCL";
6664     case DW_TAG_GNU_EINCL:
6665       return "DW_TAG_GNU_EINCL";
6666     case DW_TAG_GNU_template_template_param:
6667       return "DW_TAG_GNU_template_template_param";
6668     default:
6669       return "DW_TAG_<unknown>";
6670     }
6671 }
6672
6673 /* Convert a DWARF attribute code into its string name.  */
6674
6675 static const char *
6676 dwarf_attr_name (unsigned int attr)
6677 {
6678   switch (attr)
6679     {
6680     case DW_AT_sibling:
6681       return "DW_AT_sibling";
6682     case DW_AT_location:
6683       return "DW_AT_location";
6684     case DW_AT_name:
6685       return "DW_AT_name";
6686     case DW_AT_ordering:
6687       return "DW_AT_ordering";
6688     case DW_AT_subscr_data:
6689       return "DW_AT_subscr_data";
6690     case DW_AT_byte_size:
6691       return "DW_AT_byte_size";
6692     case DW_AT_bit_offset:
6693       return "DW_AT_bit_offset";
6694     case DW_AT_bit_size:
6695       return "DW_AT_bit_size";
6696     case DW_AT_element_list:
6697       return "DW_AT_element_list";
6698     case DW_AT_stmt_list:
6699       return "DW_AT_stmt_list";
6700     case DW_AT_low_pc:
6701       return "DW_AT_low_pc";
6702     case DW_AT_high_pc:
6703       return "DW_AT_high_pc";
6704     case DW_AT_language:
6705       return "DW_AT_language";
6706     case DW_AT_member:
6707       return "DW_AT_member";
6708     case DW_AT_discr:
6709       return "DW_AT_discr";
6710     case DW_AT_discr_value:
6711       return "DW_AT_discr_value";
6712     case DW_AT_visibility:
6713       return "DW_AT_visibility";
6714     case DW_AT_import:
6715       return "DW_AT_import";
6716     case DW_AT_string_length:
6717       return "DW_AT_string_length";
6718     case DW_AT_common_reference:
6719       return "DW_AT_common_reference";
6720     case DW_AT_comp_dir:
6721       return "DW_AT_comp_dir";
6722     case DW_AT_const_value:
6723       return "DW_AT_const_value";
6724     case DW_AT_containing_type:
6725       return "DW_AT_containing_type";
6726     case DW_AT_default_value:
6727       return "DW_AT_default_value";
6728     case DW_AT_inline:
6729       return "DW_AT_inline";
6730     case DW_AT_is_optional:
6731       return "DW_AT_is_optional";
6732     case DW_AT_lower_bound:
6733       return "DW_AT_lower_bound";
6734     case DW_AT_producer:
6735       return "DW_AT_producer";
6736     case DW_AT_prototyped:
6737       return "DW_AT_prototyped";
6738     case DW_AT_return_addr:
6739       return "DW_AT_return_addr";
6740     case DW_AT_start_scope:
6741       return "DW_AT_start_scope";
6742     case DW_AT_bit_stride:
6743       return "DW_AT_bit_stride";
6744     case DW_AT_upper_bound:
6745       return "DW_AT_upper_bound";
6746     case DW_AT_abstract_origin:
6747       return "DW_AT_abstract_origin";
6748     case DW_AT_accessibility:
6749       return "DW_AT_accessibility";
6750     case DW_AT_address_class:
6751       return "DW_AT_address_class";
6752     case DW_AT_artificial:
6753       return "DW_AT_artificial";
6754     case DW_AT_base_types:
6755       return "DW_AT_base_types";
6756     case DW_AT_calling_convention:
6757       return "DW_AT_calling_convention";
6758     case DW_AT_count:
6759       return "DW_AT_count";
6760     case DW_AT_data_member_location:
6761       return "DW_AT_data_member_location";
6762     case DW_AT_decl_column:
6763       return "DW_AT_decl_column";
6764     case DW_AT_decl_file:
6765       return "DW_AT_decl_file";
6766     case DW_AT_decl_line:
6767       return "DW_AT_decl_line";
6768     case DW_AT_declaration:
6769       return "DW_AT_declaration";
6770     case DW_AT_discr_list:
6771       return "DW_AT_discr_list";
6772     case DW_AT_encoding:
6773       return "DW_AT_encoding";
6774     case DW_AT_external:
6775       return "DW_AT_external";
6776     case DW_AT_explicit:
6777       return "DW_AT_explicit";
6778     case DW_AT_frame_base:
6779       return "DW_AT_frame_base";
6780     case DW_AT_friend:
6781       return "DW_AT_friend";
6782     case DW_AT_identifier_case:
6783       return "DW_AT_identifier_case";
6784     case DW_AT_macro_info:
6785       return "DW_AT_macro_info";
6786     case DW_AT_namelist_items:
6787       return "DW_AT_namelist_items";
6788     case DW_AT_priority:
6789       return "DW_AT_priority";
6790     case DW_AT_segment:
6791       return "DW_AT_segment";
6792     case DW_AT_specification:
6793       return "DW_AT_specification";
6794     case DW_AT_static_link:
6795       return "DW_AT_static_link";
6796     case DW_AT_type:
6797       return "DW_AT_type";
6798     case DW_AT_use_location:
6799       return "DW_AT_use_location";
6800     case DW_AT_variable_parameter:
6801       return "DW_AT_variable_parameter";
6802     case DW_AT_virtuality:
6803       return "DW_AT_virtuality";
6804     case DW_AT_vtable_elem_location:
6805       return "DW_AT_vtable_elem_location";
6806
6807     case DW_AT_allocated:
6808       return "DW_AT_allocated";
6809     case DW_AT_associated:
6810       return "DW_AT_associated";
6811     case DW_AT_data_location:
6812       return "DW_AT_data_location";
6813     case DW_AT_byte_stride:
6814       return "DW_AT_byte_stride";
6815     case DW_AT_entry_pc:
6816       return "DW_AT_entry_pc";
6817     case DW_AT_use_UTF8:
6818       return "DW_AT_use_UTF8";
6819     case DW_AT_extension:
6820       return "DW_AT_extension";
6821     case DW_AT_ranges:
6822       return "DW_AT_ranges";
6823     case DW_AT_trampoline:
6824       return "DW_AT_trampoline";
6825     case DW_AT_call_column:
6826       return "DW_AT_call_column";
6827     case DW_AT_call_file:
6828       return "DW_AT_call_file";
6829     case DW_AT_call_line:
6830       return "DW_AT_call_line";
6831     case DW_AT_object_pointer:
6832       return "DW_AT_object_pointer";
6833
6834     case DW_AT_signature:
6835       return "DW_AT_signature";
6836     case DW_AT_main_subprogram:
6837       return "DW_AT_main_subprogram";
6838     case DW_AT_data_bit_offset:
6839       return "DW_AT_data_bit_offset";
6840     case DW_AT_const_expr:
6841       return "DW_AT_const_expr";
6842     case DW_AT_enum_class:
6843       return "DW_AT_enum_class";
6844     case DW_AT_linkage_name:
6845       return "DW_AT_linkage_name";
6846
6847     case DW_AT_MIPS_fde:
6848       return "DW_AT_MIPS_fde";
6849     case DW_AT_MIPS_loop_begin:
6850       return "DW_AT_MIPS_loop_begin";
6851     case DW_AT_MIPS_tail_loop_begin:
6852       return "DW_AT_MIPS_tail_loop_begin";
6853     case DW_AT_MIPS_epilog_begin:
6854       return "DW_AT_MIPS_epilog_begin";
6855 #if VMS_DEBUGGING_INFO
6856     case DW_AT_HP_prologue:
6857       return "DW_AT_HP_prologue";
6858 #else
6859     case DW_AT_MIPS_loop_unroll_factor:
6860       return "DW_AT_MIPS_loop_unroll_factor";
6861 #endif
6862     case DW_AT_MIPS_software_pipeline_depth:
6863       return "DW_AT_MIPS_software_pipeline_depth";
6864     case DW_AT_MIPS_linkage_name:
6865       return "DW_AT_MIPS_linkage_name";
6866 #if VMS_DEBUGGING_INFO
6867     case DW_AT_HP_epilogue:
6868       return "DW_AT_HP_epilogue";
6869 #else
6870     case DW_AT_MIPS_stride:
6871       return "DW_AT_MIPS_stride";
6872 #endif
6873     case DW_AT_MIPS_abstract_name:
6874       return "DW_AT_MIPS_abstract_name";
6875     case DW_AT_MIPS_clone_origin:
6876       return "DW_AT_MIPS_clone_origin";
6877     case DW_AT_MIPS_has_inlines:
6878       return "DW_AT_MIPS_has_inlines";
6879
6880     case DW_AT_sf_names:
6881       return "DW_AT_sf_names";
6882     case DW_AT_src_info:
6883       return "DW_AT_src_info";
6884     case DW_AT_mac_info:
6885       return "DW_AT_mac_info";
6886     case DW_AT_src_coords:
6887       return "DW_AT_src_coords";
6888     case DW_AT_body_begin:
6889       return "DW_AT_body_begin";
6890     case DW_AT_body_end:
6891       return "DW_AT_body_end";
6892     case DW_AT_GNU_vector:
6893       return "DW_AT_GNU_vector";
6894     case DW_AT_GNU_guarded_by:
6895       return "DW_AT_GNU_guarded_by";
6896     case DW_AT_GNU_pt_guarded_by:
6897       return "DW_AT_GNU_pt_guarded_by";
6898     case DW_AT_GNU_guarded:
6899       return "DW_AT_GNU_guarded";
6900     case DW_AT_GNU_pt_guarded:
6901       return "DW_AT_GNU_pt_guarded";
6902     case DW_AT_GNU_locks_excluded:
6903       return "DW_AT_GNU_locks_excluded";
6904     case DW_AT_GNU_exclusive_locks_required:
6905       return "DW_AT_GNU_exclusive_locks_required";
6906     case DW_AT_GNU_shared_locks_required:
6907       return "DW_AT_GNU_shared_locks_required";
6908     case DW_AT_GNU_odr_signature:
6909       return "DW_AT_GNU_odr_signature";
6910     case DW_AT_GNU_template_name:
6911       return "DW_AT_GNU_template_name";
6912
6913     case DW_AT_VMS_rtnbeg_pd_address:
6914       return "DW_AT_VMS_rtnbeg_pd_address";
6915
6916     default:
6917       return "DW_AT_<unknown>";
6918     }
6919 }
6920
6921 /* Convert a DWARF value form code into its string name.  */
6922
6923 static const char *
6924 dwarf_form_name (unsigned int form)
6925 {
6926   switch (form)
6927     {
6928     case DW_FORM_addr:
6929       return "DW_FORM_addr";
6930     case DW_FORM_block2:
6931       return "DW_FORM_block2";
6932     case DW_FORM_block4:
6933       return "DW_FORM_block4";
6934     case DW_FORM_data2:
6935       return "DW_FORM_data2";
6936     case DW_FORM_data4:
6937       return "DW_FORM_data4";
6938     case DW_FORM_data8:
6939       return "DW_FORM_data8";
6940     case DW_FORM_string:
6941       return "DW_FORM_string";
6942     case DW_FORM_block:
6943       return "DW_FORM_block";
6944     case DW_FORM_block1:
6945       return "DW_FORM_block1";
6946     case DW_FORM_data1:
6947       return "DW_FORM_data1";
6948     case DW_FORM_flag:
6949       return "DW_FORM_flag";
6950     case DW_FORM_sdata:
6951       return "DW_FORM_sdata";
6952     case DW_FORM_strp:
6953       return "DW_FORM_strp";
6954     case DW_FORM_udata:
6955       return "DW_FORM_udata";
6956     case DW_FORM_ref_addr:
6957       return "DW_FORM_ref_addr";
6958     case DW_FORM_ref1:
6959       return "DW_FORM_ref1";
6960     case DW_FORM_ref2:
6961       return "DW_FORM_ref2";
6962     case DW_FORM_ref4:
6963       return "DW_FORM_ref4";
6964     case DW_FORM_ref8:
6965       return "DW_FORM_ref8";
6966     case DW_FORM_ref_udata:
6967       return "DW_FORM_ref_udata";
6968     case DW_FORM_indirect:
6969       return "DW_FORM_indirect";
6970     case DW_FORM_sec_offset:
6971       return "DW_FORM_sec_offset";
6972     case DW_FORM_exprloc:
6973       return "DW_FORM_exprloc";
6974     case DW_FORM_flag_present:
6975       return "DW_FORM_flag_present";
6976     case DW_FORM_ref_sig8:
6977       return "DW_FORM_ref_sig8";
6978     default:
6979       return "DW_FORM_<unknown>";
6980     }
6981 }
6982 \f
6983 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6984    instance of an inlined instance of a decl which is local to an inline
6985    function, so we have to trace all of the way back through the origin chain
6986    to find out what sort of node actually served as the original seed for the
6987    given block.  */
6988
6989 static tree
6990 decl_ultimate_origin (const_tree decl)
6991 {
6992   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6993     return NULL_TREE;
6994
6995   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6996      nodes in the function to point to themselves; ignore that if
6997      we're trying to output the abstract instance of this function.  */
6998   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6999     return NULL_TREE;
7000
7001   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7002      most distant ancestor, this should never happen.  */
7003   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7004
7005   return DECL_ABSTRACT_ORIGIN (decl);
7006 }
7007
7008 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7009    of a virtual function may refer to a base class, so we check the 'this'
7010    parameter.  */
7011
7012 static tree
7013 decl_class_context (tree decl)
7014 {
7015   tree context = NULL_TREE;
7016
7017   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7018     context = DECL_CONTEXT (decl);
7019   else
7020     context = TYPE_MAIN_VARIANT
7021       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7022
7023   if (context && !TYPE_P (context))
7024     context = NULL_TREE;
7025
7026   return context;
7027 }
7028 \f
7029 /* Add an attribute/value pair to a DIE.  */
7030
7031 static inline void
7032 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7033 {
7034   /* Maybe this should be an assert?  */
7035   if (die == NULL)
7036     return;
7037
7038   if (die->die_attr == NULL)
7039     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7040   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7041 }
7042
7043 static inline enum dw_val_class
7044 AT_class (dw_attr_ref a)
7045 {
7046   return a->dw_attr_val.val_class;
7047 }
7048
7049 /* Add a flag value attribute to a DIE.  */
7050
7051 static inline void
7052 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7053 {
7054   dw_attr_node attr;
7055
7056   attr.dw_attr = attr_kind;
7057   attr.dw_attr_val.val_class = dw_val_class_flag;
7058   attr.dw_attr_val.v.val_flag = flag;
7059   add_dwarf_attr (die, &attr);
7060 }
7061
7062 static inline unsigned
7063 AT_flag (dw_attr_ref a)
7064 {
7065   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7066   return a->dw_attr_val.v.val_flag;
7067 }
7068
7069 /* Add a signed integer attribute value to a DIE.  */
7070
7071 static inline void
7072 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7073 {
7074   dw_attr_node attr;
7075
7076   attr.dw_attr = attr_kind;
7077   attr.dw_attr_val.val_class = dw_val_class_const;
7078   attr.dw_attr_val.v.val_int = int_val;
7079   add_dwarf_attr (die, &attr);
7080 }
7081
7082 static inline HOST_WIDE_INT
7083 AT_int (dw_attr_ref a)
7084 {
7085   gcc_assert (a && AT_class (a) == dw_val_class_const);
7086   return a->dw_attr_val.v.val_int;
7087 }
7088
7089 /* Add an unsigned integer attribute value to a DIE.  */
7090
7091 static inline void
7092 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7093                  unsigned HOST_WIDE_INT unsigned_val)
7094 {
7095   dw_attr_node attr;
7096
7097   attr.dw_attr = attr_kind;
7098   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7099   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7100   add_dwarf_attr (die, &attr);
7101 }
7102
7103 static inline unsigned HOST_WIDE_INT
7104 AT_unsigned (dw_attr_ref a)
7105 {
7106   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7107   return a->dw_attr_val.v.val_unsigned;
7108 }
7109
7110 /* Add an unsigned double integer attribute value to a DIE.  */
7111
7112 static inline void
7113 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7114                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7115 {
7116   dw_attr_node attr;
7117
7118   attr.dw_attr = attr_kind;
7119   attr.dw_attr_val.val_class = dw_val_class_const_double;
7120   attr.dw_attr_val.v.val_double.high = high;
7121   attr.dw_attr_val.v.val_double.low = low;
7122   add_dwarf_attr (die, &attr);
7123 }
7124
7125 /* Add a floating point attribute value to a DIE and return it.  */
7126
7127 static inline void
7128 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7129             unsigned int length, unsigned int elt_size, unsigned char *array)
7130 {
7131   dw_attr_node attr;
7132
7133   attr.dw_attr = attr_kind;
7134   attr.dw_attr_val.val_class = dw_val_class_vec;
7135   attr.dw_attr_val.v.val_vec.length = length;
7136   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7137   attr.dw_attr_val.v.val_vec.array = array;
7138   add_dwarf_attr (die, &attr);
7139 }
7140
7141 /* Add an 8-byte data attribute value to a DIE.  */
7142
7143 static inline void
7144 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7145               unsigned char data8[8])
7146 {
7147   dw_attr_node attr;
7148
7149   attr.dw_attr = attr_kind;
7150   attr.dw_attr_val.val_class = dw_val_class_data8;
7151   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7152   add_dwarf_attr (die, &attr);
7153 }
7154
7155 /* Hash and equality functions for debug_str_hash.  */
7156
7157 static hashval_t
7158 debug_str_do_hash (const void *x)
7159 {
7160   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7161 }
7162
7163 static int
7164 debug_str_eq (const void *x1, const void *x2)
7165 {
7166   return strcmp ((((const struct indirect_string_node *)x1)->str),
7167                  (const char *)x2) == 0;
7168 }
7169
7170 /* Add STR to the indirect string hash table.  */
7171
7172 static struct indirect_string_node *
7173 find_AT_string (const char *str)
7174 {
7175   struct indirect_string_node *node;
7176   void **slot;
7177
7178   if (! debug_str_hash)
7179     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7180                                       debug_str_eq, NULL);
7181
7182   slot = htab_find_slot_with_hash (debug_str_hash, str,
7183                                    htab_hash_string (str), INSERT);
7184   if (*slot == NULL)
7185     {
7186       node = ggc_alloc_cleared_indirect_string_node ();
7187       node->str = ggc_strdup (str);
7188       *slot = node;
7189     }
7190   else
7191     node = (struct indirect_string_node *) *slot;
7192
7193   node->refcount++;
7194   return node;
7195 }
7196
7197 /* Add a string attribute value to a DIE.  */
7198
7199 static inline void
7200 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7201 {
7202   dw_attr_node attr;
7203   struct indirect_string_node *node;
7204
7205   node = find_AT_string (str);
7206
7207   attr.dw_attr = attr_kind;
7208   attr.dw_attr_val.val_class = dw_val_class_str;
7209   attr.dw_attr_val.v.val_str = node;
7210   add_dwarf_attr (die, &attr);
7211 }
7212
7213 /* Create a label for an indirect string node, ensuring it is going to
7214    be output, unless its reference count goes down to zero.  */
7215
7216 static inline void
7217 gen_label_for_indirect_string (struct indirect_string_node *node)
7218 {
7219   char label[32];
7220
7221   if (node->label)
7222     return;
7223
7224   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7225   ++dw2_string_counter;
7226   node->label = xstrdup (label);
7227 }
7228
7229 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7230    debug string STR.  */
7231
7232 static inline rtx
7233 get_debug_string_label (const char *str)
7234 {
7235   struct indirect_string_node *node = find_AT_string (str);
7236
7237   debug_str_hash_forced = true;
7238
7239   gen_label_for_indirect_string (node);
7240
7241   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7242 }
7243
7244 static inline const char *
7245 AT_string (dw_attr_ref a)
7246 {
7247   gcc_assert (a && AT_class (a) == dw_val_class_str);
7248   return a->dw_attr_val.v.val_str->str;
7249 }
7250
7251 /* Find out whether a string should be output inline in DIE
7252    or out-of-line in .debug_str section.  */
7253
7254 static enum dwarf_form
7255 AT_string_form (dw_attr_ref a)
7256 {
7257   struct indirect_string_node *node;
7258   unsigned int len;
7259
7260   gcc_assert (a && AT_class (a) == dw_val_class_str);
7261
7262   node = a->dw_attr_val.v.val_str;
7263   if (node->form)
7264     return node->form;
7265
7266   len = strlen (node->str) + 1;
7267
7268   /* If the string is shorter or equal to the size of the reference, it is
7269      always better to put it inline.  */
7270   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7271     return node->form = DW_FORM_string;
7272
7273   /* If we cannot expect the linker to merge strings in .debug_str
7274      section, only put it into .debug_str if it is worth even in this
7275      single module.  */
7276   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7277       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7278       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7279     return node->form = DW_FORM_string;
7280
7281   gen_label_for_indirect_string (node);
7282
7283   return node->form = DW_FORM_strp;
7284 }
7285
7286 /* Add a DIE reference attribute value to a DIE.  */
7287
7288 static inline void
7289 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7290 {
7291   dw_attr_node attr;
7292
7293   attr.dw_attr = attr_kind;
7294   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7295   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7296   attr.dw_attr_val.v.val_die_ref.external = 0;
7297   add_dwarf_attr (die, &attr);
7298 }
7299
7300 /* Add an AT_specification attribute to a DIE, and also make the back
7301    pointer from the specification to the definition.  */
7302
7303 static inline void
7304 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7305 {
7306   add_AT_die_ref (die, DW_AT_specification, targ_die);
7307   gcc_assert (!targ_die->die_definition);
7308   targ_die->die_definition = die;
7309 }
7310
7311 static inline dw_die_ref
7312 AT_ref (dw_attr_ref a)
7313 {
7314   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7315   return a->dw_attr_val.v.val_die_ref.die;
7316 }
7317
7318 static inline int
7319 AT_ref_external (dw_attr_ref a)
7320 {
7321   if (a && AT_class (a) == dw_val_class_die_ref)
7322     return a->dw_attr_val.v.val_die_ref.external;
7323
7324   return 0;
7325 }
7326
7327 static inline void
7328 set_AT_ref_external (dw_attr_ref a, int i)
7329 {
7330   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7331   a->dw_attr_val.v.val_die_ref.external = i;
7332 }
7333
7334 /* Add an FDE reference attribute value to a DIE.  */
7335
7336 static inline void
7337 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7338 {
7339   dw_attr_node attr;
7340
7341   attr.dw_attr = attr_kind;
7342   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7343   attr.dw_attr_val.v.val_fde_index = targ_fde;
7344   add_dwarf_attr (die, &attr);
7345 }
7346
7347 /* Add a location description attribute value to a DIE.  */
7348
7349 static inline void
7350 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7351 {
7352   dw_attr_node attr;
7353
7354   attr.dw_attr = attr_kind;
7355   attr.dw_attr_val.val_class = dw_val_class_loc;
7356   attr.dw_attr_val.v.val_loc = loc;
7357   add_dwarf_attr (die, &attr);
7358 }
7359
7360 static inline dw_loc_descr_ref
7361 AT_loc (dw_attr_ref a)
7362 {
7363   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7364   return a->dw_attr_val.v.val_loc;
7365 }
7366
7367 static inline void
7368 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7369 {
7370   dw_attr_node attr;
7371
7372   attr.dw_attr = attr_kind;
7373   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7374   attr.dw_attr_val.v.val_loc_list = loc_list;
7375   add_dwarf_attr (die, &attr);
7376   have_location_lists = true;
7377 }
7378
7379 static inline dw_loc_list_ref
7380 AT_loc_list (dw_attr_ref a)
7381 {
7382   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7383   return a->dw_attr_val.v.val_loc_list;
7384 }
7385
7386 static inline dw_loc_list_ref *
7387 AT_loc_list_ptr (dw_attr_ref a)
7388 {
7389   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7390   return &a->dw_attr_val.v.val_loc_list;
7391 }
7392
7393 /* Add an address constant attribute value to a DIE.  */
7394
7395 static inline void
7396 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7397 {
7398   dw_attr_node attr;
7399
7400   attr.dw_attr = attr_kind;
7401   attr.dw_attr_val.val_class = dw_val_class_addr;
7402   attr.dw_attr_val.v.val_addr = addr;
7403   add_dwarf_attr (die, &attr);
7404 }
7405
7406 /* Get the RTX from to an address DIE attribute.  */
7407
7408 static inline rtx
7409 AT_addr (dw_attr_ref a)
7410 {
7411   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7412   return a->dw_attr_val.v.val_addr;
7413 }
7414
7415 /* Add a file attribute value to a DIE.  */
7416
7417 static inline void
7418 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7419              struct dwarf_file_data *fd)
7420 {
7421   dw_attr_node attr;
7422
7423   attr.dw_attr = attr_kind;
7424   attr.dw_attr_val.val_class = dw_val_class_file;
7425   attr.dw_attr_val.v.val_file = fd;
7426   add_dwarf_attr (die, &attr);
7427 }
7428
7429 /* Get the dwarf_file_data from a file DIE attribute.  */
7430
7431 static inline struct dwarf_file_data *
7432 AT_file (dw_attr_ref a)
7433 {
7434   gcc_assert (a && AT_class (a) == dw_val_class_file);
7435   return a->dw_attr_val.v.val_file;
7436 }
7437
7438 /* Add a vms delta attribute value to a DIE.  */
7439
7440 static inline void
7441 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7442                   const char *lbl1, const char *lbl2)
7443 {
7444   dw_attr_node attr;
7445
7446   attr.dw_attr = attr_kind;
7447   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7448   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7449   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7450   add_dwarf_attr (die, &attr);
7451 }
7452
7453 /* Add a label identifier attribute value to a DIE.  */
7454
7455 static inline void
7456 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7457 {
7458   dw_attr_node attr;
7459
7460   attr.dw_attr = attr_kind;
7461   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7462   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7463   add_dwarf_attr (die, &attr);
7464 }
7465
7466 /* Add a section offset attribute value to a DIE, an offset into the
7467    debug_line section.  */
7468
7469 static inline void
7470 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7471                 const char *label)
7472 {
7473   dw_attr_node attr;
7474
7475   attr.dw_attr = attr_kind;
7476   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7477   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7478   add_dwarf_attr (die, &attr);
7479 }
7480
7481 /* Add a section offset attribute value to a DIE, an offset into the
7482    debug_macinfo section.  */
7483
7484 static inline void
7485 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7486                const char *label)
7487 {
7488   dw_attr_node attr;
7489
7490   attr.dw_attr = attr_kind;
7491   attr.dw_attr_val.val_class = dw_val_class_macptr;
7492   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7493   add_dwarf_attr (die, &attr);
7494 }
7495
7496 /* Add an offset attribute value to a DIE.  */
7497
7498 static inline void
7499 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7500                unsigned HOST_WIDE_INT offset)
7501 {
7502   dw_attr_node attr;
7503
7504   attr.dw_attr = attr_kind;
7505   attr.dw_attr_val.val_class = dw_val_class_offset;
7506   attr.dw_attr_val.v.val_offset = offset;
7507   add_dwarf_attr (die, &attr);
7508 }
7509
7510 /* Add an range_list attribute value to a DIE.  */
7511
7512 static void
7513 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7514                    long unsigned int offset)
7515 {
7516   dw_attr_node attr;
7517
7518   attr.dw_attr = attr_kind;
7519   attr.dw_attr_val.val_class = dw_val_class_range_list;
7520   attr.dw_attr_val.v.val_offset = offset;
7521   add_dwarf_attr (die, &attr);
7522 }
7523
7524 /* Return the start label of a delta attribute.  */
7525
7526 static inline const char *
7527 AT_vms_delta1 (dw_attr_ref a)
7528 {
7529   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7530   return a->dw_attr_val.v.val_vms_delta.lbl1;
7531 }
7532
7533 /* Return the end label of a delta attribute.  */
7534
7535 static inline const char *
7536 AT_vms_delta2 (dw_attr_ref a)
7537 {
7538   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7539   return a->dw_attr_val.v.val_vms_delta.lbl2;
7540 }
7541
7542 static inline const char *
7543 AT_lbl (dw_attr_ref a)
7544 {
7545   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7546                     || AT_class (a) == dw_val_class_lineptr
7547                     || AT_class (a) == dw_val_class_macptr));
7548   return a->dw_attr_val.v.val_lbl_id;
7549 }
7550
7551 /* Get the attribute of type attr_kind.  */
7552
7553 static dw_attr_ref
7554 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7555 {
7556   dw_attr_ref a;
7557   unsigned ix;
7558   dw_die_ref spec = NULL;
7559
7560   if (! die)
7561     return NULL;
7562
7563   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7564     if (a->dw_attr == attr_kind)
7565       return a;
7566     else if (a->dw_attr == DW_AT_specification
7567              || a->dw_attr == DW_AT_abstract_origin)
7568       spec = AT_ref (a);
7569
7570   if (spec)
7571     return get_AT (spec, attr_kind);
7572
7573   return NULL;
7574 }
7575
7576 /* Return the "low pc" attribute value, typically associated with a subprogram
7577    DIE.  Return null if the "low pc" attribute is either not present, or if it
7578    cannot be represented as an assembler label identifier.  */
7579
7580 static inline const char *
7581 get_AT_low_pc (dw_die_ref die)
7582 {
7583   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7584
7585   return a ? AT_lbl (a) : NULL;
7586 }
7587
7588 /* Return the "high pc" attribute value, typically associated with a subprogram
7589    DIE.  Return null if the "high pc" attribute is either not present, or if it
7590    cannot be represented as an assembler label identifier.  */
7591
7592 static inline const char *
7593 get_AT_hi_pc (dw_die_ref die)
7594 {
7595   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7596
7597   return a ? AT_lbl (a) : NULL;
7598 }
7599
7600 /* Return the value of the string attribute designated by ATTR_KIND, or
7601    NULL if it is not present.  */
7602
7603 static inline const char *
7604 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7605 {
7606   dw_attr_ref a = get_AT (die, attr_kind);
7607
7608   return a ? AT_string (a) : NULL;
7609 }
7610
7611 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7612    if it is not present.  */
7613
7614 static inline int
7615 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7616 {
7617   dw_attr_ref a = get_AT (die, attr_kind);
7618
7619   return a ? AT_flag (a) : 0;
7620 }
7621
7622 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7623    if it is not present.  */
7624
7625 static inline unsigned
7626 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7627 {
7628   dw_attr_ref a = get_AT (die, attr_kind);
7629
7630   return a ? AT_unsigned (a) : 0;
7631 }
7632
7633 static inline dw_die_ref
7634 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7635 {
7636   dw_attr_ref a = get_AT (die, attr_kind);
7637
7638   return a ? AT_ref (a) : NULL;
7639 }
7640
7641 static inline struct dwarf_file_data *
7642 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7643 {
7644   dw_attr_ref a = get_AT (die, attr_kind);
7645
7646   return a ? AT_file (a) : NULL;
7647 }
7648
7649 /* Return TRUE if the language is C++.  */
7650
7651 static inline bool
7652 is_cxx (void)
7653 {
7654   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7655
7656   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7657 }
7658
7659 /* Return TRUE if the language is Fortran.  */
7660
7661 static inline bool
7662 is_fortran (void)
7663 {
7664   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7665
7666   return (lang == DW_LANG_Fortran77
7667           || lang == DW_LANG_Fortran90
7668           || lang == DW_LANG_Fortran95);
7669 }
7670
7671 /* Return TRUE if the language is Ada.  */
7672
7673 static inline bool
7674 is_ada (void)
7675 {
7676   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7677
7678   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7679 }
7680
7681 /* Remove the specified attribute if present.  */
7682
7683 static void
7684 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7685 {
7686   dw_attr_ref a;
7687   unsigned ix;
7688
7689   if (! die)
7690     return;
7691
7692   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7693     if (a->dw_attr == attr_kind)
7694       {
7695         if (AT_class (a) == dw_val_class_str)
7696           if (a->dw_attr_val.v.val_str->refcount)
7697             a->dw_attr_val.v.val_str->refcount--;
7698
7699         /* VEC_ordered_remove should help reduce the number of abbrevs
7700            that are needed.  */
7701         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7702         return;
7703       }
7704 }
7705
7706 /* Remove CHILD from its parent.  PREV must have the property that
7707    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7708
7709 static void
7710 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7711 {
7712   gcc_assert (child->die_parent == prev->die_parent);
7713   gcc_assert (prev->die_sib == child);
7714   if (prev == child)
7715     {
7716       gcc_assert (child->die_parent->die_child == child);
7717       prev = NULL;
7718     }
7719   else
7720     prev->die_sib = child->die_sib;
7721   if (child->die_parent->die_child == child)
7722     child->die_parent->die_child = prev;
7723 }
7724
7725 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7726    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7727
7728 static void
7729 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7730 {
7731   dw_die_ref parent = old_child->die_parent;
7732
7733   gcc_assert (parent == prev->die_parent);
7734   gcc_assert (prev->die_sib == old_child);
7735
7736   new_child->die_parent = parent;
7737   if (prev == old_child)
7738     {
7739       gcc_assert (parent->die_child == old_child);
7740       new_child->die_sib = new_child;
7741     }
7742   else
7743     {
7744       prev->die_sib = new_child;
7745       new_child->die_sib = old_child->die_sib;
7746     }
7747   if (old_child->die_parent->die_child == old_child)
7748     old_child->die_parent->die_child = new_child;
7749 }
7750
7751 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7752
7753 static void
7754 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7755 {
7756   dw_die_ref c;
7757   new_parent->die_child = old_parent->die_child;
7758   old_parent->die_child = NULL;
7759   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7760 }
7761
7762 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7763    matches TAG.  */
7764
7765 static void
7766 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7767 {
7768   dw_die_ref c;
7769
7770   c = die->die_child;
7771   if (c) do {
7772     dw_die_ref prev = c;
7773     c = c->die_sib;
7774     while (c->die_tag == tag)
7775       {
7776         remove_child_with_prev (c, prev);
7777         /* Might have removed every child.  */
7778         if (c == c->die_sib)
7779           return;
7780         c = c->die_sib;
7781       }
7782   } while (c != die->die_child);
7783 }
7784
7785 /* Add a CHILD_DIE as the last child of DIE.  */
7786
7787 static void
7788 add_child_die (dw_die_ref die, dw_die_ref child_die)
7789 {
7790   /* FIXME this should probably be an assert.  */
7791   if (! die || ! child_die)
7792     return;
7793   gcc_assert (die != child_die);
7794
7795   child_die->die_parent = die;
7796   if (die->die_child)
7797     {
7798       child_die->die_sib = die->die_child->die_sib;
7799       die->die_child->die_sib = child_die;
7800     }
7801   else
7802     child_die->die_sib = child_die;
7803   die->die_child = child_die;
7804 }
7805
7806 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7807    is the specification, to the end of PARENT's list of children.
7808    This is done by removing and re-adding it.  */
7809
7810 static void
7811 splice_child_die (dw_die_ref parent, dw_die_ref child)
7812 {
7813   dw_die_ref p;
7814
7815   /* We want the declaration DIE from inside the class, not the
7816      specification DIE at toplevel.  */
7817   if (child->die_parent != parent)
7818     {
7819       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7820
7821       if (tmp)
7822         child = tmp;
7823     }
7824
7825   gcc_assert (child->die_parent == parent
7826               || (child->die_parent
7827                   == get_AT_ref (parent, DW_AT_specification)));
7828
7829   for (p = child->die_parent->die_child; ; p = p->die_sib)
7830     if (p->die_sib == child)
7831       {
7832         remove_child_with_prev (child, p);
7833         break;
7834       }
7835
7836   add_child_die (parent, child);
7837 }
7838
7839 /* Return a pointer to a newly created DIE node.  */
7840
7841 static inline dw_die_ref
7842 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7843 {
7844   dw_die_ref die = ggc_alloc_cleared_die_node ();
7845
7846   die->die_tag = tag_value;
7847
7848   if (parent_die != NULL)
7849     add_child_die (parent_die, die);
7850   else
7851     {
7852       limbo_die_node *limbo_node;
7853
7854       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7855       limbo_node->die = die;
7856       limbo_node->created_for = t;
7857       limbo_node->next = limbo_die_list;
7858       limbo_die_list = limbo_node;
7859     }
7860
7861   return die;
7862 }
7863
7864 /* Return the DIE associated with the given type specifier.  */
7865
7866 static inline dw_die_ref
7867 lookup_type_die (tree type)
7868 {
7869   return TYPE_SYMTAB_DIE (type);
7870 }
7871
7872 /* Equate a DIE to a given type specifier.  */
7873
7874 static inline void
7875 equate_type_number_to_die (tree type, dw_die_ref type_die)
7876 {
7877   TYPE_SYMTAB_DIE (type) = type_die;
7878 }
7879
7880 /* Returns a hash value for X (which really is a die_struct).  */
7881
7882 static hashval_t
7883 decl_die_table_hash (const void *x)
7884 {
7885   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7886 }
7887
7888 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7889
7890 static int
7891 decl_die_table_eq (const void *x, const void *y)
7892 {
7893   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7894 }
7895
7896 /* Return the DIE associated with a given declaration.  */
7897
7898 static inline dw_die_ref
7899 lookup_decl_die (tree decl)
7900 {
7901   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7902 }
7903
7904 /* Returns a hash value for X (which really is a var_loc_list).  */
7905
7906 static hashval_t
7907 decl_loc_table_hash (const void *x)
7908 {
7909   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7910 }
7911
7912 /* Return nonzero if decl_id of var_loc_list X is the same as
7913    UID of decl *Y.  */
7914
7915 static int
7916 decl_loc_table_eq (const void *x, const void *y)
7917 {
7918   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7919 }
7920
7921 /* Return the var_loc list associated with a given declaration.  */
7922
7923 static inline var_loc_list *
7924 lookup_decl_loc (const_tree decl)
7925 {
7926   if (!decl_loc_table)
7927     return NULL;
7928   return (var_loc_list *)
7929     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7930 }
7931
7932 /* Equate a DIE to a particular declaration.  */
7933
7934 static void
7935 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7936 {
7937   unsigned int decl_id = DECL_UID (decl);
7938   void **slot;
7939
7940   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7941   *slot = decl_die;
7942   decl_die->decl_id = decl_id;
7943 }
7944
7945 /* Return how many bits covers PIECE EXPR_LIST.  */
7946
7947 static int
7948 decl_piece_bitsize (rtx piece)
7949 {
7950   int ret = (int) GET_MODE (piece);
7951   if (ret)
7952     return ret;
7953   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7954               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7955   return INTVAL (XEXP (XEXP (piece, 0), 0));
7956 }
7957
7958 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7959
7960 static rtx *
7961 decl_piece_varloc_ptr (rtx piece)
7962 {
7963   if ((int) GET_MODE (piece))
7964     return &XEXP (piece, 0);
7965   else
7966     return &XEXP (XEXP (piece, 0), 1);
7967 }
7968
7969 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7970    Next is the chain of following piece nodes.  */
7971
7972 static rtx
7973 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7974 {
7975   if (bitsize <= (int) MAX_MACHINE_MODE)
7976     return alloc_EXPR_LIST (bitsize, loc_note, next);
7977   else
7978     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7979                                                GEN_INT (bitsize),
7980                                                loc_note), next);
7981 }
7982
7983 /* Return rtx that should be stored into loc field for
7984    LOC_NOTE and BITPOS/BITSIZE.  */
7985
7986 static rtx
7987 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7988                       HOST_WIDE_INT bitsize)
7989 {
7990   if (bitsize != -1)
7991     {
7992       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7993       if (bitpos != 0)
7994         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7995     }
7996   return loc_note;
7997 }
7998
7999 /* This function either modifies location piece list *DEST in
8000    place (if SRC and INNER is NULL), or copies location piece list
8001    *SRC to *DEST while modifying it.  Location BITPOS is modified
8002    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8003    not copied and if needed some padding around it is added.
8004    When modifying in place, DEST should point to EXPR_LIST where
8005    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8006    to the start of the whole list and INNER points to the EXPR_LIST
8007    where earlier pieces cover PIECE_BITPOS bits.  */
8008
8009 static void
8010 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8011                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8012                    HOST_WIDE_INT bitsize, rtx loc_note)
8013 {
8014   int diff;
8015   bool copy = inner != NULL;
8016
8017   if (copy)
8018     {
8019       /* First copy all nodes preceeding the current bitpos.  */
8020       while (src != inner)
8021         {
8022           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8023                                    decl_piece_bitsize (*src), NULL_RTX);
8024           dest = &XEXP (*dest, 1);
8025           src = &XEXP (*src, 1);
8026         }
8027     }
8028   /* Add padding if needed.  */
8029   if (bitpos != piece_bitpos)
8030     {
8031       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8032                                copy ? NULL_RTX : *dest);
8033       dest = &XEXP (*dest, 1);
8034     }
8035   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8036     {
8037       gcc_assert (!copy);
8038       /* A piece with correct bitpos and bitsize already exist,
8039          just update the location for it and return.  */
8040       *decl_piece_varloc_ptr (*dest) = loc_note;
8041       return;
8042     }
8043   /* Add the piece that changed.  */
8044   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8045   dest = &XEXP (*dest, 1);
8046   /* Skip over pieces that overlap it.  */
8047   diff = bitpos - piece_bitpos + bitsize;
8048   if (!copy)
8049     src = dest;
8050   while (diff > 0 && *src)
8051     {
8052       rtx piece = *src;
8053       diff -= decl_piece_bitsize (piece);
8054       if (copy)
8055         src = &XEXP (piece, 1);
8056       else
8057         {
8058           *src = XEXP (piece, 1);
8059           free_EXPR_LIST_node (piece);
8060         }
8061     }
8062   /* Add padding if needed.  */
8063   if (diff < 0 && *src)
8064     {
8065       if (!copy)
8066         dest = src;
8067       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8068       dest = &XEXP (*dest, 1);
8069     }
8070   if (!copy)
8071     return;
8072   /* Finally copy all nodes following it.  */
8073   while (*src)
8074     {
8075       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8076                                decl_piece_bitsize (*src), NULL_RTX);
8077       dest = &XEXP (*dest, 1);
8078       src = &XEXP (*src, 1);
8079     }
8080 }
8081
8082 /* Add a variable location node to the linked list for DECL.  */
8083
8084 static struct var_loc_node *
8085 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8086 {
8087   unsigned int decl_id;
8088   var_loc_list *temp;
8089   void **slot;
8090   struct var_loc_node *loc = NULL;
8091   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8092
8093   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8094     {
8095       tree realdecl = DECL_DEBUG_EXPR (decl);
8096       if (realdecl && handled_component_p (realdecl))
8097         {
8098           HOST_WIDE_INT maxsize;
8099           tree innerdecl;
8100           innerdecl
8101             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8102           if (!DECL_P (innerdecl)
8103               || DECL_IGNORED_P (innerdecl)
8104               || TREE_STATIC (innerdecl)
8105               || bitsize <= 0
8106               || bitpos + bitsize > 256
8107               || bitsize != maxsize)
8108             return NULL;
8109           decl = innerdecl;
8110         }
8111     }
8112
8113   decl_id = DECL_UID (decl);
8114   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8115   if (*slot == NULL)
8116     {
8117       temp = ggc_alloc_cleared_var_loc_list ();
8118       temp->decl_id = decl_id;
8119       *slot = temp;
8120     }
8121   else
8122     temp = (var_loc_list *) *slot;
8123
8124   if (temp->last)
8125     {
8126       struct var_loc_node *last = temp->last, *unused = NULL;
8127       rtx *piece_loc = NULL, last_loc_note;
8128       int piece_bitpos = 0;
8129       if (last->next)
8130         {
8131           last = last->next;
8132           gcc_assert (last->next == NULL);
8133         }
8134       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8135         {
8136           piece_loc = &last->loc;
8137           do
8138             {
8139               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8140               if (piece_bitpos + cur_bitsize > bitpos)
8141                 break;
8142               piece_bitpos += cur_bitsize;
8143               piece_loc = &XEXP (*piece_loc, 1);
8144             }
8145           while (*piece_loc);
8146         }
8147       /* TEMP->LAST here is either pointer to the last but one or
8148          last element in the chained list, LAST is pointer to the
8149          last element.  */
8150       if (label && strcmp (last->label, label) == 0)
8151         {
8152           /* For SRA optimized variables if there weren't any real
8153              insns since last note, just modify the last node.  */
8154           if (piece_loc != NULL)
8155             {
8156               adjust_piece_list (piece_loc, NULL, NULL,
8157                                  bitpos, piece_bitpos, bitsize, loc_note);
8158               return NULL;
8159             }
8160           /* If the last note doesn't cover any instructions, remove it.  */
8161           if (temp->last != last)
8162             {
8163               temp->last->next = NULL;
8164               unused = last;
8165               last = temp->last;
8166               gcc_assert (strcmp (last->label, label) != 0);
8167             }
8168           else
8169             {
8170               gcc_assert (temp->first == temp->last);
8171               memset (temp->last, '\0', sizeof (*temp->last));
8172               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8173               return temp->last;
8174             }
8175         }
8176       if (bitsize == -1 && NOTE_P (last->loc))
8177         last_loc_note = last->loc;
8178       else if (piece_loc != NULL
8179                && *piece_loc != NULL_RTX
8180                && piece_bitpos == bitpos
8181                && decl_piece_bitsize (*piece_loc) == bitsize)
8182         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8183       else
8184         last_loc_note = NULL_RTX;
8185       /* If the current location is the same as the end of the list,
8186          and either both or neither of the locations is uninitialized,
8187          we have nothing to do.  */
8188       if (last_loc_note == NULL_RTX
8189           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8190                             NOTE_VAR_LOCATION_LOC (loc_note)))
8191           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8192                != NOTE_VAR_LOCATION_STATUS (loc_note))
8193               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8194                    == VAR_INIT_STATUS_UNINITIALIZED)
8195                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8196                       == VAR_INIT_STATUS_UNINITIALIZED))))
8197         {
8198           /* Add LOC to the end of list and update LAST.  If the last
8199              element of the list has been removed above, reuse its
8200              memory for the new node, otherwise allocate a new one.  */
8201           if (unused)
8202             {
8203               loc = unused;
8204               memset (loc, '\0', sizeof (*loc));
8205             }
8206           else
8207             loc = ggc_alloc_cleared_var_loc_node ();
8208           if (bitsize == -1 || piece_loc == NULL)
8209             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8210           else
8211             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8212                                bitpos, piece_bitpos, bitsize, loc_note);
8213           last->next = loc;
8214           /* Ensure TEMP->LAST will point either to the new last but one
8215              element of the chain, or to the last element in it.  */
8216           if (last != temp->last)
8217             temp->last = last;
8218         }
8219       else if (unused)
8220         ggc_free (unused);
8221     }
8222   else
8223     {
8224       loc = ggc_alloc_cleared_var_loc_node ();
8225       temp->first = loc;
8226       temp->last = loc;
8227       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8228     }
8229   return loc;
8230 }
8231 \f
8232 /* Keep track of the number of spaces used to indent the
8233    output of the debugging routines that print the structure of
8234    the DIE internal representation.  */
8235 static int print_indent;
8236
8237 /* Indent the line the number of spaces given by print_indent.  */
8238
8239 static inline void
8240 print_spaces (FILE *outfile)
8241 {
8242   fprintf (outfile, "%*s", print_indent, "");
8243 }
8244
8245 /* Print a type signature in hex.  */
8246
8247 static inline void
8248 print_signature (FILE *outfile, char *sig)
8249 {
8250   int i;
8251
8252   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8253     fprintf (outfile, "%02x", sig[i] & 0xff);
8254 }
8255
8256 /* Print the information associated with a given DIE, and its children.
8257    This routine is a debugging aid only.  */
8258
8259 static void
8260 print_die (dw_die_ref die, FILE *outfile)
8261 {
8262   dw_attr_ref a;
8263   dw_die_ref c;
8264   unsigned ix;
8265
8266   print_spaces (outfile);
8267   fprintf (outfile, "DIE %4ld: %s\n",
8268            die->die_offset, dwarf_tag_name (die->die_tag));
8269   print_spaces (outfile);
8270   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8271   fprintf (outfile, " offset: %ld\n", die->die_offset);
8272   if (dwarf_version >= 4 && die->die_id.die_type_node)
8273     {
8274       print_spaces (outfile);
8275       fprintf (outfile, "  signature: ");
8276       print_signature (outfile, die->die_id.die_type_node->signature);
8277       fprintf (outfile, "\n");
8278     }
8279
8280   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8281     {
8282       print_spaces (outfile);
8283       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8284
8285       switch (AT_class (a))
8286         {
8287         case dw_val_class_addr:
8288           fprintf (outfile, "address");
8289           break;
8290         case dw_val_class_offset:
8291           fprintf (outfile, "offset");
8292           break;
8293         case dw_val_class_loc:
8294           fprintf (outfile, "location descriptor");
8295           break;
8296         case dw_val_class_loc_list:
8297           fprintf (outfile, "location list -> label:%s",
8298                    AT_loc_list (a)->ll_symbol);
8299           break;
8300         case dw_val_class_range_list:
8301           fprintf (outfile, "range list");
8302           break;
8303         case dw_val_class_const:
8304           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8305           break;
8306         case dw_val_class_unsigned_const:
8307           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8308           break;
8309         case dw_val_class_const_double:
8310           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8311                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8312                    a->dw_attr_val.v.val_double.high,
8313                    a->dw_attr_val.v.val_double.low);
8314           break;
8315         case dw_val_class_vec:
8316           fprintf (outfile, "floating-point or vector constant");
8317           break;
8318         case dw_val_class_flag:
8319           fprintf (outfile, "%u", AT_flag (a));
8320           break;
8321         case dw_val_class_die_ref:
8322           if (AT_ref (a) != NULL)
8323             {
8324               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8325                 {
8326                   fprintf (outfile, "die -> signature: ");
8327                   print_signature (outfile,
8328                                    AT_ref (a)->die_id.die_type_node->signature);
8329                 }
8330               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8331                 fprintf (outfile, "die -> label: %s",
8332                          AT_ref (a)->die_id.die_symbol);
8333               else
8334                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8335             }
8336           else
8337             fprintf (outfile, "die -> <null>");
8338           break;
8339         case dw_val_class_vms_delta:
8340           fprintf (outfile, "delta: @slotcount(%s-%s)",
8341                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8342           break;
8343         case dw_val_class_lbl_id:
8344         case dw_val_class_lineptr:
8345         case dw_val_class_macptr:
8346           fprintf (outfile, "label: %s", AT_lbl (a));
8347           break;
8348         case dw_val_class_str:
8349           if (AT_string (a) != NULL)
8350             fprintf (outfile, "\"%s\"", AT_string (a));
8351           else
8352             fprintf (outfile, "<null>");
8353           break;
8354         case dw_val_class_file:
8355           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8356                    AT_file (a)->emitted_number);
8357           break;
8358         case dw_val_class_data8:
8359           {
8360             int i;
8361
8362             for (i = 0; i < 8; i++)
8363               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8364             break;
8365           }
8366         default:
8367           break;
8368         }
8369
8370       fprintf (outfile, "\n");
8371     }
8372
8373   if (die->die_child != NULL)
8374     {
8375       print_indent += 4;
8376       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8377       print_indent -= 4;
8378     }
8379   if (print_indent == 0)
8380     fprintf (outfile, "\n");
8381 }
8382
8383 /* Print the contents of the source code line number correspondence table.
8384    This routine is a debugging aid only.  */
8385
8386 static void
8387 print_dwarf_line_table (FILE *outfile)
8388 {
8389   unsigned i;
8390   dw_line_info_ref line_info;
8391
8392   fprintf (outfile, "\n\nDWARF source line information\n");
8393   for (i = 1; i < line_info_table_in_use; i++)
8394     {
8395       line_info = &line_info_table[i];
8396       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8397                line_info->dw_file_num,
8398                line_info->dw_line_num);
8399     }
8400
8401   fprintf (outfile, "\n\n");
8402 }
8403
8404 /* Print the information collected for a given DIE.  */
8405
8406 DEBUG_FUNCTION void
8407 debug_dwarf_die (dw_die_ref die)
8408 {
8409   print_die (die, stderr);
8410 }
8411
8412 /* Print all DWARF information collected for the compilation unit.
8413    This routine is a debugging aid only.  */
8414
8415 DEBUG_FUNCTION void
8416 debug_dwarf (void)
8417 {
8418   print_indent = 0;
8419   print_die (comp_unit_die, stderr);
8420   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8421     print_dwarf_line_table (stderr);
8422 }
8423 \f
8424 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8425    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8426    DIE that marks the start of the DIEs for this include file.  */
8427
8428 static dw_die_ref
8429 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8430 {
8431   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8432   dw_die_ref new_unit = gen_compile_unit_die (filename);
8433
8434   new_unit->die_sib = old_unit;
8435   return new_unit;
8436 }
8437
8438 /* Close an include-file CU and reopen the enclosing one.  */
8439
8440 static dw_die_ref
8441 pop_compile_unit (dw_die_ref old_unit)
8442 {
8443   dw_die_ref new_unit = old_unit->die_sib;
8444
8445   old_unit->die_sib = NULL;
8446   return new_unit;
8447 }
8448
8449 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8450 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8451
8452 /* Calculate the checksum of a location expression.  */
8453
8454 static inline void
8455 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8456 {
8457   int tem;
8458
8459   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8460   CHECKSUM (tem);
8461   CHECKSUM (loc->dw_loc_oprnd1);
8462   CHECKSUM (loc->dw_loc_oprnd2);
8463 }
8464
8465 /* Calculate the checksum of an attribute.  */
8466
8467 static void
8468 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8469 {
8470   dw_loc_descr_ref loc;
8471   rtx r;
8472
8473   CHECKSUM (at->dw_attr);
8474
8475   /* We don't care that this was compiled with a different compiler
8476      snapshot; if the output is the same, that's what matters.  */
8477   if (at->dw_attr == DW_AT_producer)
8478     return;
8479
8480   switch (AT_class (at))
8481     {
8482     case dw_val_class_const:
8483       CHECKSUM (at->dw_attr_val.v.val_int);
8484       break;
8485     case dw_val_class_unsigned_const:
8486       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8487       break;
8488     case dw_val_class_const_double:
8489       CHECKSUM (at->dw_attr_val.v.val_double);
8490       break;
8491     case dw_val_class_vec:
8492       CHECKSUM (at->dw_attr_val.v.val_vec);
8493       break;
8494     case dw_val_class_flag:
8495       CHECKSUM (at->dw_attr_val.v.val_flag);
8496       break;
8497     case dw_val_class_str:
8498       CHECKSUM_STRING (AT_string (at));
8499       break;
8500
8501     case dw_val_class_addr:
8502       r = AT_addr (at);
8503       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8504       CHECKSUM_STRING (XSTR (r, 0));
8505       break;
8506
8507     case dw_val_class_offset:
8508       CHECKSUM (at->dw_attr_val.v.val_offset);
8509       break;
8510
8511     case dw_val_class_loc:
8512       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8513         loc_checksum (loc, ctx);
8514       break;
8515
8516     case dw_val_class_die_ref:
8517       die_checksum (AT_ref (at), ctx, mark);
8518       break;
8519
8520     case dw_val_class_fde_ref:
8521     case dw_val_class_vms_delta:
8522     case dw_val_class_lbl_id:
8523     case dw_val_class_lineptr:
8524     case dw_val_class_macptr:
8525       break;
8526
8527     case dw_val_class_file:
8528       CHECKSUM_STRING (AT_file (at)->filename);
8529       break;
8530
8531     case dw_val_class_data8:
8532       CHECKSUM (at->dw_attr_val.v.val_data8);
8533       break;
8534
8535     default:
8536       break;
8537     }
8538 }
8539
8540 /* Calculate the checksum of a DIE.  */
8541
8542 static void
8543 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8544 {
8545   dw_die_ref c;
8546   dw_attr_ref a;
8547   unsigned ix;
8548
8549   /* To avoid infinite recursion.  */
8550   if (die->die_mark)
8551     {
8552       CHECKSUM (die->die_mark);
8553       return;
8554     }
8555   die->die_mark = ++(*mark);
8556
8557   CHECKSUM (die->die_tag);
8558
8559   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8560     attr_checksum (a, ctx, mark);
8561
8562   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8563 }
8564
8565 #undef CHECKSUM
8566 #undef CHECKSUM_STRING
8567
8568 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8569 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8570 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8571 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8572 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8573 #define CHECKSUM_ATTR(FOO) \
8574   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8575
8576 /* Calculate the checksum of a number in signed LEB128 format.  */
8577
8578 static void
8579 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8580 {
8581   unsigned char byte;
8582   bool more;
8583
8584   while (1)
8585     {
8586       byte = (value & 0x7f);
8587       value >>= 7;
8588       more = !((value == 0 && (byte & 0x40) == 0)
8589                 || (value == -1 && (byte & 0x40) != 0));
8590       if (more)
8591         byte |= 0x80;
8592       CHECKSUM (byte);
8593       if (!more)
8594         break;
8595     }
8596 }
8597
8598 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8599
8600 static void
8601 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8602 {
8603   while (1)
8604     {
8605       unsigned char byte = (value & 0x7f);
8606       value >>= 7;
8607       if (value != 0)
8608         /* More bytes to follow.  */
8609         byte |= 0x80;
8610       CHECKSUM (byte);
8611       if (value == 0)
8612         break;
8613     }
8614 }
8615
8616 /* Checksum the context of the DIE.  This adds the names of any
8617    surrounding namespaces or structures to the checksum.  */
8618
8619 static void
8620 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8621 {
8622   const char *name;
8623   dw_die_ref spec;
8624   int tag = die->die_tag;
8625
8626   if (tag != DW_TAG_namespace
8627       && tag != DW_TAG_structure_type
8628       && tag != DW_TAG_class_type)
8629     return;
8630
8631   name = get_AT_string (die, DW_AT_name);
8632
8633   spec = get_AT_ref (die, DW_AT_specification);
8634   if (spec != NULL)
8635     die = spec;
8636
8637   if (die->die_parent != NULL)
8638     checksum_die_context (die->die_parent, ctx);
8639
8640   CHECKSUM_ULEB128 ('C');
8641   CHECKSUM_ULEB128 (tag);
8642   if (name != NULL)
8643     CHECKSUM_STRING (name);
8644 }
8645
8646 /* Calculate the checksum of a location expression.  */
8647
8648 static inline void
8649 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8650 {
8651   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8652      were emitted as a DW_FORM_sdata instead of a location expression.  */
8653   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8654     {
8655       CHECKSUM_ULEB128 (DW_FORM_sdata);
8656       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8657       return;
8658     }
8659
8660   /* Otherwise, just checksum the raw location expression.  */
8661   while (loc != NULL)
8662     {
8663       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8664       CHECKSUM (loc->dw_loc_oprnd1);
8665       CHECKSUM (loc->dw_loc_oprnd2);
8666       loc = loc->dw_loc_next;
8667     }
8668 }
8669
8670 /* Calculate the checksum of an attribute.  */
8671
8672 static void
8673 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8674                        struct md5_ctx *ctx, int *mark)
8675 {
8676   dw_loc_descr_ref loc;
8677   rtx r;
8678
8679   if (AT_class (at) == dw_val_class_die_ref)
8680     {
8681       dw_die_ref target_die = AT_ref (at);
8682
8683       /* For pointer and reference types, we checksum only the (qualified)
8684          name of the target type (if there is a name).  For friend entries,
8685          we checksum only the (qualified) name of the target type or function.
8686          This allows the checksum to remain the same whether the target type
8687          is complete or not.  */
8688       if ((at->dw_attr == DW_AT_type
8689            && (tag == DW_TAG_pointer_type
8690                || tag == DW_TAG_reference_type
8691                || tag == DW_TAG_rvalue_reference_type
8692                || tag == DW_TAG_ptr_to_member_type))
8693           || (at->dw_attr == DW_AT_friend
8694               && tag == DW_TAG_friend))
8695         {
8696           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8697
8698           if (name_attr != NULL)
8699             {
8700               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8701
8702               if (decl == NULL)
8703                 decl = target_die;
8704               CHECKSUM_ULEB128 ('N');
8705               CHECKSUM_ULEB128 (at->dw_attr);
8706               if (decl->die_parent != NULL)
8707                 checksum_die_context (decl->die_parent, ctx);
8708               CHECKSUM_ULEB128 ('E');
8709               CHECKSUM_STRING (AT_string (name_attr));
8710               return;
8711             }
8712         }
8713
8714       /* For all other references to another DIE, we check to see if the
8715          target DIE has already been visited.  If it has, we emit a
8716          backward reference; if not, we descend recursively.  */
8717       if (target_die->die_mark > 0)
8718         {
8719           CHECKSUM_ULEB128 ('R');
8720           CHECKSUM_ULEB128 (at->dw_attr);
8721           CHECKSUM_ULEB128 (target_die->die_mark);
8722         }
8723       else
8724         {
8725           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8726
8727           if (decl == NULL)
8728             decl = target_die;
8729           target_die->die_mark = ++(*mark);
8730           CHECKSUM_ULEB128 ('T');
8731           CHECKSUM_ULEB128 (at->dw_attr);
8732           if (decl->die_parent != NULL)
8733             checksum_die_context (decl->die_parent, ctx);
8734           die_checksum_ordered (target_die, ctx, mark);
8735         }
8736       return;
8737     }
8738
8739   CHECKSUM_ULEB128 ('A');
8740   CHECKSUM_ULEB128 (at->dw_attr);
8741
8742   switch (AT_class (at))
8743     {
8744     case dw_val_class_const:
8745       CHECKSUM_ULEB128 (DW_FORM_sdata);
8746       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8747       break;
8748
8749     case dw_val_class_unsigned_const:
8750       CHECKSUM_ULEB128 (DW_FORM_sdata);
8751       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8752       break;
8753
8754     case dw_val_class_const_double:
8755       CHECKSUM_ULEB128 (DW_FORM_block);
8756       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8757       CHECKSUM (at->dw_attr_val.v.val_double);
8758       break;
8759
8760     case dw_val_class_vec:
8761       CHECKSUM_ULEB128 (DW_FORM_block);
8762       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8763       CHECKSUM (at->dw_attr_val.v.val_vec);
8764       break;
8765
8766     case dw_val_class_flag:
8767       CHECKSUM_ULEB128 (DW_FORM_flag);
8768       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8769       break;
8770
8771     case dw_val_class_str:
8772       CHECKSUM_ULEB128 (DW_FORM_string);
8773       CHECKSUM_STRING (AT_string (at));
8774       break;
8775
8776     case dw_val_class_addr:
8777       r = AT_addr (at);
8778       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8779       CHECKSUM_ULEB128 (DW_FORM_string);
8780       CHECKSUM_STRING (XSTR (r, 0));
8781       break;
8782
8783     case dw_val_class_offset:
8784       CHECKSUM_ULEB128 (DW_FORM_sdata);
8785       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8786       break;
8787
8788     case dw_val_class_loc:
8789       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8790         loc_checksum_ordered (loc, ctx);
8791       break;
8792
8793     case dw_val_class_fde_ref:
8794     case dw_val_class_lbl_id:
8795     case dw_val_class_lineptr:
8796     case dw_val_class_macptr:
8797       break;
8798
8799     case dw_val_class_file:
8800       CHECKSUM_ULEB128 (DW_FORM_string);
8801       CHECKSUM_STRING (AT_file (at)->filename);
8802       break;
8803
8804     case dw_val_class_data8:
8805       CHECKSUM (at->dw_attr_val.v.val_data8);
8806       break;
8807
8808     default:
8809       break;
8810     }
8811 }
8812
8813 struct checksum_attributes
8814 {
8815   dw_attr_ref at_name;
8816   dw_attr_ref at_type;
8817   dw_attr_ref at_friend;
8818   dw_attr_ref at_accessibility;
8819   dw_attr_ref at_address_class;
8820   dw_attr_ref at_allocated;
8821   dw_attr_ref at_artificial;
8822   dw_attr_ref at_associated;
8823   dw_attr_ref at_binary_scale;
8824   dw_attr_ref at_bit_offset;
8825   dw_attr_ref at_bit_size;
8826   dw_attr_ref at_bit_stride;
8827   dw_attr_ref at_byte_size;
8828   dw_attr_ref at_byte_stride;
8829   dw_attr_ref at_const_value;
8830   dw_attr_ref at_containing_type;
8831   dw_attr_ref at_count;
8832   dw_attr_ref at_data_location;
8833   dw_attr_ref at_data_member_location;
8834   dw_attr_ref at_decimal_scale;
8835   dw_attr_ref at_decimal_sign;
8836   dw_attr_ref at_default_value;
8837   dw_attr_ref at_digit_count;
8838   dw_attr_ref at_discr;
8839   dw_attr_ref at_discr_list;
8840   dw_attr_ref at_discr_value;
8841   dw_attr_ref at_encoding;
8842   dw_attr_ref at_endianity;
8843   dw_attr_ref at_explicit;
8844   dw_attr_ref at_is_optional;
8845   dw_attr_ref at_location;
8846   dw_attr_ref at_lower_bound;
8847   dw_attr_ref at_mutable;
8848   dw_attr_ref at_ordering;
8849   dw_attr_ref at_picture_string;
8850   dw_attr_ref at_prototyped;
8851   dw_attr_ref at_small;
8852   dw_attr_ref at_segment;
8853   dw_attr_ref at_string_length;
8854   dw_attr_ref at_threads_scaled;
8855   dw_attr_ref at_upper_bound;
8856   dw_attr_ref at_use_location;
8857   dw_attr_ref at_use_UTF8;
8858   dw_attr_ref at_variable_parameter;
8859   dw_attr_ref at_virtuality;
8860   dw_attr_ref at_visibility;
8861   dw_attr_ref at_vtable_elem_location;
8862 };
8863
8864 /* Collect the attributes that we will want to use for the checksum.  */
8865
8866 static void
8867 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8868 {
8869   dw_attr_ref a;
8870   unsigned ix;
8871
8872   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8873     {
8874       switch (a->dw_attr)
8875         {
8876         case DW_AT_name:
8877           attrs->at_name = a;
8878           break;
8879         case DW_AT_type:
8880           attrs->at_type = a;
8881           break;
8882         case DW_AT_friend:
8883           attrs->at_friend = a;
8884           break;
8885         case DW_AT_accessibility:
8886           attrs->at_accessibility = a;
8887           break;
8888         case DW_AT_address_class:
8889           attrs->at_address_class = a;
8890           break;
8891         case DW_AT_allocated:
8892           attrs->at_allocated = a;
8893           break;
8894         case DW_AT_artificial:
8895           attrs->at_artificial = a;
8896           break;
8897         case DW_AT_associated:
8898           attrs->at_associated = a;
8899           break;
8900         case DW_AT_binary_scale:
8901           attrs->at_binary_scale = a;
8902           break;
8903         case DW_AT_bit_offset:
8904           attrs->at_bit_offset = a;
8905           break;
8906         case DW_AT_bit_size:
8907           attrs->at_bit_size = a;
8908           break;
8909         case DW_AT_bit_stride:
8910           attrs->at_bit_stride = a;
8911           break;
8912         case DW_AT_byte_size:
8913           attrs->at_byte_size = a;
8914           break;
8915         case DW_AT_byte_stride:
8916           attrs->at_byte_stride = a;
8917           break;
8918         case DW_AT_const_value:
8919           attrs->at_const_value = a;
8920           break;
8921         case DW_AT_containing_type:
8922           attrs->at_containing_type = a;
8923           break;
8924         case DW_AT_count:
8925           attrs->at_count = a;
8926           break;
8927         case DW_AT_data_location:
8928           attrs->at_data_location = a;
8929           break;
8930         case DW_AT_data_member_location:
8931           attrs->at_data_member_location = a;
8932           break;
8933         case DW_AT_decimal_scale:
8934           attrs->at_decimal_scale = a;
8935           break;
8936         case DW_AT_decimal_sign:
8937           attrs->at_decimal_sign = a;
8938           break;
8939         case DW_AT_default_value:
8940           attrs->at_default_value = a;
8941           break;
8942         case DW_AT_digit_count:
8943           attrs->at_digit_count = a;
8944           break;
8945         case DW_AT_discr:
8946           attrs->at_discr = a;
8947           break;
8948         case DW_AT_discr_list:
8949           attrs->at_discr_list = a;
8950           break;
8951         case DW_AT_discr_value:
8952           attrs->at_discr_value = a;
8953           break;
8954         case DW_AT_encoding:
8955           attrs->at_encoding = a;
8956           break;
8957         case DW_AT_endianity:
8958           attrs->at_endianity = a;
8959           break;
8960         case DW_AT_explicit:
8961           attrs->at_explicit = a;
8962           break;
8963         case DW_AT_is_optional:
8964           attrs->at_is_optional = a;
8965           break;
8966         case DW_AT_location:
8967           attrs->at_location = a;
8968           break;
8969         case DW_AT_lower_bound:
8970           attrs->at_lower_bound = a;
8971           break;
8972         case DW_AT_mutable:
8973           attrs->at_mutable = a;
8974           break;
8975         case DW_AT_ordering:
8976           attrs->at_ordering = a;
8977           break;
8978         case DW_AT_picture_string:
8979           attrs->at_picture_string = a;
8980           break;
8981         case DW_AT_prototyped:
8982           attrs->at_prototyped = a;
8983           break;
8984         case DW_AT_small:
8985           attrs->at_small = a;
8986           break;
8987         case DW_AT_segment:
8988           attrs->at_segment = a;
8989           break;
8990         case DW_AT_string_length:
8991           attrs->at_string_length = a;
8992           break;
8993         case DW_AT_threads_scaled:
8994           attrs->at_threads_scaled = a;
8995           break;
8996         case DW_AT_upper_bound:
8997           attrs->at_upper_bound = a;
8998           break;
8999         case DW_AT_use_location:
9000           attrs->at_use_location = a;
9001           break;
9002         case DW_AT_use_UTF8:
9003           attrs->at_use_UTF8 = a;
9004           break;
9005         case DW_AT_variable_parameter:
9006           attrs->at_variable_parameter = a;
9007           break;
9008         case DW_AT_virtuality:
9009           attrs->at_virtuality = a;
9010           break;
9011         case DW_AT_visibility:
9012           attrs->at_visibility = a;
9013           break;
9014         case DW_AT_vtable_elem_location:
9015           attrs->at_vtable_elem_location = a;
9016           break;
9017         default:
9018           break;
9019         }
9020     }
9021 }
9022
9023 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9024
9025 static void
9026 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9027 {
9028   dw_die_ref c;
9029   dw_die_ref decl;
9030   struct checksum_attributes attrs;
9031
9032   CHECKSUM_ULEB128 ('D');
9033   CHECKSUM_ULEB128 (die->die_tag);
9034
9035   memset (&attrs, 0, sizeof (attrs));
9036
9037   decl = get_AT_ref (die, DW_AT_specification);
9038   if (decl != NULL)
9039     collect_checksum_attributes (&attrs, decl);
9040   collect_checksum_attributes (&attrs, die);
9041
9042   CHECKSUM_ATTR (attrs.at_name);
9043   CHECKSUM_ATTR (attrs.at_accessibility);
9044   CHECKSUM_ATTR (attrs.at_address_class);
9045   CHECKSUM_ATTR (attrs.at_allocated);
9046   CHECKSUM_ATTR (attrs.at_artificial);
9047   CHECKSUM_ATTR (attrs.at_associated);
9048   CHECKSUM_ATTR (attrs.at_binary_scale);
9049   CHECKSUM_ATTR (attrs.at_bit_offset);
9050   CHECKSUM_ATTR (attrs.at_bit_size);
9051   CHECKSUM_ATTR (attrs.at_bit_stride);
9052   CHECKSUM_ATTR (attrs.at_byte_size);
9053   CHECKSUM_ATTR (attrs.at_byte_stride);
9054   CHECKSUM_ATTR (attrs.at_const_value);
9055   CHECKSUM_ATTR (attrs.at_containing_type);
9056   CHECKSUM_ATTR (attrs.at_count);
9057   CHECKSUM_ATTR (attrs.at_data_location);
9058   CHECKSUM_ATTR (attrs.at_data_member_location);
9059   CHECKSUM_ATTR (attrs.at_decimal_scale);
9060   CHECKSUM_ATTR (attrs.at_decimal_sign);
9061   CHECKSUM_ATTR (attrs.at_default_value);
9062   CHECKSUM_ATTR (attrs.at_digit_count);
9063   CHECKSUM_ATTR (attrs.at_discr);
9064   CHECKSUM_ATTR (attrs.at_discr_list);
9065   CHECKSUM_ATTR (attrs.at_discr_value);
9066   CHECKSUM_ATTR (attrs.at_encoding);
9067   CHECKSUM_ATTR (attrs.at_endianity);
9068   CHECKSUM_ATTR (attrs.at_explicit);
9069   CHECKSUM_ATTR (attrs.at_is_optional);
9070   CHECKSUM_ATTR (attrs.at_location);
9071   CHECKSUM_ATTR (attrs.at_lower_bound);
9072   CHECKSUM_ATTR (attrs.at_mutable);
9073   CHECKSUM_ATTR (attrs.at_ordering);
9074   CHECKSUM_ATTR (attrs.at_picture_string);
9075   CHECKSUM_ATTR (attrs.at_prototyped);
9076   CHECKSUM_ATTR (attrs.at_small);
9077   CHECKSUM_ATTR (attrs.at_segment);
9078   CHECKSUM_ATTR (attrs.at_string_length);
9079   CHECKSUM_ATTR (attrs.at_threads_scaled);
9080   CHECKSUM_ATTR (attrs.at_upper_bound);
9081   CHECKSUM_ATTR (attrs.at_use_location);
9082   CHECKSUM_ATTR (attrs.at_use_UTF8);
9083   CHECKSUM_ATTR (attrs.at_variable_parameter);
9084   CHECKSUM_ATTR (attrs.at_virtuality);
9085   CHECKSUM_ATTR (attrs.at_visibility);
9086   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9087   CHECKSUM_ATTR (attrs.at_type);
9088   CHECKSUM_ATTR (attrs.at_friend);
9089
9090   /* Checksum the child DIEs, except for nested types and member functions.  */
9091   c = die->die_child;
9092   if (c) do {
9093     dw_attr_ref name_attr;
9094
9095     c = c->die_sib;
9096     name_attr = get_AT (c, DW_AT_name);
9097     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9098         && name_attr != NULL)
9099       {
9100         CHECKSUM_ULEB128 ('S');
9101         CHECKSUM_ULEB128 (c->die_tag);
9102         CHECKSUM_STRING (AT_string (name_attr));
9103       }
9104     else
9105       {
9106         /* Mark this DIE so it gets processed when unmarking.  */
9107         if (c->die_mark == 0)
9108           c->die_mark = -1;
9109         die_checksum_ordered (c, ctx, mark);
9110       }
9111   } while (c != die->die_child);
9112
9113   CHECKSUM_ULEB128 (0);
9114 }
9115
9116 #undef CHECKSUM
9117 #undef CHECKSUM_STRING
9118 #undef CHECKSUM_ATTR
9119 #undef CHECKSUM_LEB128
9120 #undef CHECKSUM_ULEB128
9121
9122 /* Generate the type signature for DIE.  This is computed by generating an
9123    MD5 checksum over the DIE's tag, its relevant attributes, and its
9124    children.  Attributes that are references to other DIEs are processed
9125    by recursion, using the MARK field to prevent infinite recursion.
9126    If the DIE is nested inside a namespace or another type, we also
9127    need to include that context in the signature.  The lower 64 bits
9128    of the resulting MD5 checksum comprise the signature.  */
9129
9130 static void
9131 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9132 {
9133   int mark;
9134   const char *name;
9135   unsigned char checksum[16];
9136   struct md5_ctx ctx;
9137   dw_die_ref decl;
9138
9139   name = get_AT_string (die, DW_AT_name);
9140   decl = get_AT_ref (die, DW_AT_specification);
9141
9142   /* First, compute a signature for just the type name (and its surrounding
9143      context, if any.  This is stored in the type unit DIE for link-time
9144      ODR (one-definition rule) checking.  */
9145
9146   if (is_cxx() && name != NULL)
9147     {
9148       md5_init_ctx (&ctx);
9149
9150       /* Checksum the names of surrounding namespaces and structures.  */
9151       if (decl != NULL && decl->die_parent != NULL)
9152         checksum_die_context (decl->die_parent, &ctx);
9153
9154       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9155       md5_process_bytes (name, strlen (name) + 1, &ctx);
9156       md5_finish_ctx (&ctx, checksum);
9157
9158       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9159     }
9160
9161   /* Next, compute the complete type signature.  */
9162
9163   md5_init_ctx (&ctx);
9164   mark = 1;
9165   die->die_mark = mark;
9166
9167   /* Checksum the names of surrounding namespaces and structures.  */
9168   if (decl != NULL && decl->die_parent != NULL)
9169     checksum_die_context (decl->die_parent, &ctx);
9170
9171   /* Checksum the DIE and its children.  */
9172   die_checksum_ordered (die, &ctx, &mark);
9173   unmark_all_dies (die);
9174   md5_finish_ctx (&ctx, checksum);
9175
9176   /* Store the signature in the type node and link the type DIE and the
9177      type node together.  */
9178   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9179           DWARF_TYPE_SIGNATURE_SIZE);
9180   die->die_id.die_type_node = type_node;
9181   type_node->type_die = die;
9182
9183   /* If the DIE is a specification, link its declaration to the type node
9184      as well.  */
9185   if (decl != NULL)
9186     decl->die_id.die_type_node = type_node;
9187 }
9188
9189 /* Do the location expressions look same?  */
9190 static inline int
9191 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9192 {
9193   return loc1->dw_loc_opc == loc2->dw_loc_opc
9194          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9195          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9196 }
9197
9198 /* Do the values look the same?  */
9199 static int
9200 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9201 {
9202   dw_loc_descr_ref loc1, loc2;
9203   rtx r1, r2;
9204
9205   if (v1->val_class != v2->val_class)
9206     return 0;
9207
9208   switch (v1->val_class)
9209     {
9210     case dw_val_class_const:
9211       return v1->v.val_int == v2->v.val_int;
9212     case dw_val_class_unsigned_const:
9213       return v1->v.val_unsigned == v2->v.val_unsigned;
9214     case dw_val_class_const_double:
9215       return v1->v.val_double.high == v2->v.val_double.high
9216              && v1->v.val_double.low == v2->v.val_double.low;
9217     case dw_val_class_vec:
9218       if (v1->v.val_vec.length != v2->v.val_vec.length
9219           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9220         return 0;
9221       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9222                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9223         return 0;
9224       return 1;
9225     case dw_val_class_flag:
9226       return v1->v.val_flag == v2->v.val_flag;
9227     case dw_val_class_str:
9228       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9229
9230     case dw_val_class_addr:
9231       r1 = v1->v.val_addr;
9232       r2 = v2->v.val_addr;
9233       if (GET_CODE (r1) != GET_CODE (r2))
9234         return 0;
9235       return !rtx_equal_p (r1, r2);
9236
9237     case dw_val_class_offset:
9238       return v1->v.val_offset == v2->v.val_offset;
9239
9240     case dw_val_class_loc:
9241       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9242            loc1 && loc2;
9243            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9244         if (!same_loc_p (loc1, loc2, mark))
9245           return 0;
9246       return !loc1 && !loc2;
9247
9248     case dw_val_class_die_ref:
9249       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9250
9251     case dw_val_class_fde_ref:
9252     case dw_val_class_vms_delta:
9253     case dw_val_class_lbl_id:
9254     case dw_val_class_lineptr:
9255     case dw_val_class_macptr:
9256       return 1;
9257
9258     case dw_val_class_file:
9259       return v1->v.val_file == v2->v.val_file;
9260
9261     case dw_val_class_data8:
9262       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9263
9264     default:
9265       return 1;
9266     }
9267 }
9268
9269 /* Do the attributes look the same?  */
9270
9271 static int
9272 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9273 {
9274   if (at1->dw_attr != at2->dw_attr)
9275     return 0;
9276
9277   /* We don't care that this was compiled with a different compiler
9278      snapshot; if the output is the same, that's what matters. */
9279   if (at1->dw_attr == DW_AT_producer)
9280     return 1;
9281
9282   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9283 }
9284
9285 /* Do the dies look the same?  */
9286
9287 static int
9288 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9289 {
9290   dw_die_ref c1, c2;
9291   dw_attr_ref a1;
9292   unsigned ix;
9293
9294   /* To avoid infinite recursion.  */
9295   if (die1->die_mark)
9296     return die1->die_mark == die2->die_mark;
9297   die1->die_mark = die2->die_mark = ++(*mark);
9298
9299   if (die1->die_tag != die2->die_tag)
9300     return 0;
9301
9302   if (VEC_length (dw_attr_node, die1->die_attr)
9303       != VEC_length (dw_attr_node, die2->die_attr))
9304     return 0;
9305
9306   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9307     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9308       return 0;
9309
9310   c1 = die1->die_child;
9311   c2 = die2->die_child;
9312   if (! c1)
9313     {
9314       if (c2)
9315         return 0;
9316     }
9317   else
9318     for (;;)
9319       {
9320         if (!same_die_p (c1, c2, mark))
9321           return 0;
9322         c1 = c1->die_sib;
9323         c2 = c2->die_sib;
9324         if (c1 == die1->die_child)
9325           {
9326             if (c2 == die2->die_child)
9327               break;
9328             else
9329               return 0;
9330           }
9331     }
9332
9333   return 1;
9334 }
9335
9336 /* Do the dies look the same?  Wrapper around same_die_p.  */
9337
9338 static int
9339 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9340 {
9341   int mark = 0;
9342   int ret = same_die_p (die1, die2, &mark);
9343
9344   unmark_all_dies (die1);
9345   unmark_all_dies (die2);
9346
9347   return ret;
9348 }
9349
9350 /* The prefix to attach to symbols on DIEs in the current comdat debug
9351    info section.  */
9352 static char *comdat_symbol_id;
9353
9354 /* The index of the current symbol within the current comdat CU.  */
9355 static unsigned int comdat_symbol_number;
9356
9357 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9358    children, and set comdat_symbol_id accordingly.  */
9359
9360 static void
9361 compute_section_prefix (dw_die_ref unit_die)
9362 {
9363   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9364   const char *base = die_name ? lbasename (die_name) : "anonymous";
9365   char *name = XALLOCAVEC (char, strlen (base) + 64);
9366   char *p;
9367   int i, mark;
9368   unsigned char checksum[16];
9369   struct md5_ctx ctx;
9370
9371   /* Compute the checksum of the DIE, then append part of it as hex digits to
9372      the name filename of the unit.  */
9373
9374   md5_init_ctx (&ctx);
9375   mark = 0;
9376   die_checksum (unit_die, &ctx, &mark);
9377   unmark_all_dies (unit_die);
9378   md5_finish_ctx (&ctx, checksum);
9379
9380   sprintf (name, "%s.", base);
9381   clean_symbol_name (name);
9382
9383   p = name + strlen (name);
9384   for (i = 0; i < 4; i++)
9385     {
9386       sprintf (p, "%.2x", checksum[i]);
9387       p += 2;
9388     }
9389
9390   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9391   comdat_symbol_number = 0;
9392 }
9393
9394 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9395
9396 static int
9397 is_type_die (dw_die_ref die)
9398 {
9399   switch (die->die_tag)
9400     {
9401     case DW_TAG_array_type:
9402     case DW_TAG_class_type:
9403     case DW_TAG_interface_type:
9404     case DW_TAG_enumeration_type:
9405     case DW_TAG_pointer_type:
9406     case DW_TAG_reference_type:
9407     case DW_TAG_rvalue_reference_type:
9408     case DW_TAG_string_type:
9409     case DW_TAG_structure_type:
9410     case DW_TAG_subroutine_type:
9411     case DW_TAG_union_type:
9412     case DW_TAG_ptr_to_member_type:
9413     case DW_TAG_set_type:
9414     case DW_TAG_subrange_type:
9415     case DW_TAG_base_type:
9416     case DW_TAG_const_type:
9417     case DW_TAG_file_type:
9418     case DW_TAG_packed_type:
9419     case DW_TAG_volatile_type:
9420     case DW_TAG_typedef:
9421       return 1;
9422     default:
9423       return 0;
9424     }
9425 }
9426
9427 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9428    Basically, we want to choose the bits that are likely to be shared between
9429    compilations (types) and leave out the bits that are specific to individual
9430    compilations (functions).  */
9431
9432 static int
9433 is_comdat_die (dw_die_ref c)
9434 {
9435   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9436      we do for stabs.  The advantage is a greater likelihood of sharing between
9437      objects that don't include headers in the same order (and therefore would
9438      put the base types in a different comdat).  jason 8/28/00 */
9439
9440   if (c->die_tag == DW_TAG_base_type)
9441     return 0;
9442
9443   if (c->die_tag == DW_TAG_pointer_type
9444       || c->die_tag == DW_TAG_reference_type
9445       || c->die_tag == DW_TAG_rvalue_reference_type
9446       || c->die_tag == DW_TAG_const_type
9447       || c->die_tag == DW_TAG_volatile_type)
9448     {
9449       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9450
9451       return t ? is_comdat_die (t) : 0;
9452     }
9453
9454   return is_type_die (c);
9455 }
9456
9457 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9458    compilation unit.  */
9459
9460 static int
9461 is_symbol_die (dw_die_ref c)
9462 {
9463   return (is_type_die (c)
9464           || is_declaration_die (c)
9465           || c->die_tag == DW_TAG_namespace
9466           || c->die_tag == DW_TAG_module);
9467 }
9468
9469 static char *
9470 gen_internal_sym (const char *prefix)
9471 {
9472   char buf[256];
9473
9474   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9475   return xstrdup (buf);
9476 }
9477
9478 /* Assign symbols to all worthy DIEs under DIE.  */
9479
9480 static void
9481 assign_symbol_names (dw_die_ref die)
9482 {
9483   dw_die_ref c;
9484
9485   if (is_symbol_die (die))
9486     {
9487       if (comdat_symbol_id)
9488         {
9489           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9490
9491           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9492                    comdat_symbol_id, comdat_symbol_number++);
9493           die->die_id.die_symbol = xstrdup (p);
9494         }
9495       else
9496         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9497     }
9498
9499   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9500 }
9501
9502 struct cu_hash_table_entry
9503 {
9504   dw_die_ref cu;
9505   unsigned min_comdat_num, max_comdat_num;
9506   struct cu_hash_table_entry *next;
9507 };
9508
9509 /* Routines to manipulate hash table of CUs.  */
9510 static hashval_t
9511 htab_cu_hash (const void *of)
9512 {
9513   const struct cu_hash_table_entry *const entry =
9514     (const struct cu_hash_table_entry *) of;
9515
9516   return htab_hash_string (entry->cu->die_id.die_symbol);
9517 }
9518
9519 static int
9520 htab_cu_eq (const void *of1, const void *of2)
9521 {
9522   const struct cu_hash_table_entry *const entry1 =
9523     (const struct cu_hash_table_entry *) of1;
9524   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9525
9526   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9527 }
9528
9529 static void
9530 htab_cu_del (void *what)
9531 {
9532   struct cu_hash_table_entry *next,
9533     *entry = (struct cu_hash_table_entry *) what;
9534
9535   while (entry)
9536     {
9537       next = entry->next;
9538       free (entry);
9539       entry = next;
9540     }
9541 }
9542
9543 /* Check whether we have already seen this CU and set up SYM_NUM
9544    accordingly.  */
9545 static int
9546 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9547 {
9548   struct cu_hash_table_entry dummy;
9549   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9550
9551   dummy.max_comdat_num = 0;
9552
9553   slot = (struct cu_hash_table_entry **)
9554     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9555         INSERT);
9556   entry = *slot;
9557
9558   for (; entry; last = entry, entry = entry->next)
9559     {
9560       if (same_die_p_wrap (cu, entry->cu))
9561         break;
9562     }
9563
9564   if (entry)
9565     {
9566       *sym_num = entry->min_comdat_num;
9567       return 1;
9568     }
9569
9570   entry = XCNEW (struct cu_hash_table_entry);
9571   entry->cu = cu;
9572   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9573   entry->next = *slot;
9574   *slot = entry;
9575
9576   return 0;
9577 }
9578
9579 /* Record SYM_NUM to record of CU in HTABLE.  */
9580 static void
9581 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9582 {
9583   struct cu_hash_table_entry **slot, *entry;
9584
9585   slot = (struct cu_hash_table_entry **)
9586     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9587         NO_INSERT);
9588   entry = *slot;
9589
9590   entry->max_comdat_num = sym_num;
9591 }
9592
9593 /* Traverse the DIE (which is always comp_unit_die), and set up
9594    additional compilation units for each of the include files we see
9595    bracketed by BINCL/EINCL.  */
9596
9597 static void
9598 break_out_includes (dw_die_ref die)
9599 {
9600   dw_die_ref c;
9601   dw_die_ref unit = NULL;
9602   limbo_die_node *node, **pnode;
9603   htab_t cu_hash_table;
9604
9605   c = die->die_child;
9606   if (c) do {
9607     dw_die_ref prev = c;
9608     c = c->die_sib;
9609     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9610            || (unit && is_comdat_die (c)))
9611       {
9612         dw_die_ref next = c->die_sib;
9613
9614         /* This DIE is for a secondary CU; remove it from the main one.  */
9615         remove_child_with_prev (c, prev);
9616
9617         if (c->die_tag == DW_TAG_GNU_BINCL)
9618           unit = push_new_compile_unit (unit, c);
9619         else if (c->die_tag == DW_TAG_GNU_EINCL)
9620           unit = pop_compile_unit (unit);
9621         else
9622           add_child_die (unit, c);
9623         c = next;
9624         if (c == die->die_child)
9625           break;
9626       }
9627   } while (c != die->die_child);
9628
9629 #if 0
9630   /* We can only use this in debugging, since the frontend doesn't check
9631      to make sure that we leave every include file we enter.  */
9632   gcc_assert (!unit);
9633 #endif
9634
9635   assign_symbol_names (die);
9636   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9637   for (node = limbo_die_list, pnode = &limbo_die_list;
9638        node;
9639        node = node->next)
9640     {
9641       int is_dupl;
9642
9643       compute_section_prefix (node->die);
9644       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9645                         &comdat_symbol_number);
9646       assign_symbol_names (node->die);
9647       if (is_dupl)
9648         *pnode = node->next;
9649       else
9650         {
9651           pnode = &node->next;
9652           record_comdat_symbol_number (node->die, cu_hash_table,
9653                 comdat_symbol_number);
9654         }
9655     }
9656   htab_delete (cu_hash_table);
9657 }
9658
9659 /* Return non-zero if this DIE is a declaration.  */
9660
9661 static int
9662 is_declaration_die (dw_die_ref die)
9663 {
9664   dw_attr_ref a;
9665   unsigned ix;
9666
9667   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9668     if (a->dw_attr == DW_AT_declaration)
9669       return 1;
9670
9671   return 0;
9672 }
9673
9674 /* Return non-zero if this DIE is nested inside a subprogram.  */
9675
9676 static int
9677 is_nested_in_subprogram (dw_die_ref die)
9678 {
9679   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9680
9681   if (decl == NULL)
9682     decl = die;
9683   return local_scope_p (decl);
9684 }
9685
9686 /* Return non-zero if this is a type DIE that should be moved to a
9687    COMDAT .debug_types section.  */
9688
9689 static int
9690 should_move_die_to_comdat (dw_die_ref die)
9691 {
9692   switch (die->die_tag)
9693     {
9694     case DW_TAG_class_type:
9695     case DW_TAG_structure_type:
9696     case DW_TAG_enumeration_type:
9697     case DW_TAG_union_type:
9698       /* Don't move declarations, inlined instances, or types nested in a
9699          subprogram.  */
9700       if (is_declaration_die (die)
9701           || get_AT (die, DW_AT_abstract_origin)
9702           || is_nested_in_subprogram (die))
9703         return 0;
9704       return 1;
9705     case DW_TAG_array_type:
9706     case DW_TAG_interface_type:
9707     case DW_TAG_pointer_type:
9708     case DW_TAG_reference_type:
9709     case DW_TAG_rvalue_reference_type:
9710     case DW_TAG_string_type:
9711     case DW_TAG_subroutine_type:
9712     case DW_TAG_ptr_to_member_type:
9713     case DW_TAG_set_type:
9714     case DW_TAG_subrange_type:
9715     case DW_TAG_base_type:
9716     case DW_TAG_const_type:
9717     case DW_TAG_file_type:
9718     case DW_TAG_packed_type:
9719     case DW_TAG_volatile_type:
9720     case DW_TAG_typedef:
9721     default:
9722       return 0;
9723     }
9724 }
9725
9726 /* Make a clone of DIE.  */
9727
9728 static dw_die_ref
9729 clone_die (dw_die_ref die)
9730 {
9731   dw_die_ref clone;
9732   dw_attr_ref a;
9733   unsigned ix;
9734
9735   clone = ggc_alloc_cleared_die_node ();
9736   clone->die_tag = die->die_tag;
9737
9738   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9739     add_dwarf_attr (clone, a);
9740
9741   return clone;
9742 }
9743
9744 /* Make a clone of the tree rooted at DIE.  */
9745
9746 static dw_die_ref
9747 clone_tree (dw_die_ref die)
9748 {
9749   dw_die_ref c;
9750   dw_die_ref clone = clone_die (die);
9751
9752   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9753
9754   return clone;
9755 }
9756
9757 /* Make a clone of DIE as a declaration.  */
9758
9759 static dw_die_ref
9760 clone_as_declaration (dw_die_ref die)
9761 {
9762   dw_die_ref clone;
9763   dw_die_ref decl;
9764   dw_attr_ref a;
9765   unsigned ix;
9766
9767   /* If the DIE is already a declaration, just clone it.  */
9768   if (is_declaration_die (die))
9769     return clone_die (die);
9770
9771   /* If the DIE is a specification, just clone its declaration DIE.  */
9772   decl = get_AT_ref (die, DW_AT_specification);
9773   if (decl != NULL)
9774     return clone_die (decl);
9775
9776   clone = ggc_alloc_cleared_die_node ();
9777   clone->die_tag = die->die_tag;
9778
9779   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9780     {
9781       /* We don't want to copy over all attributes.
9782          For example we don't want DW_AT_byte_size because otherwise we will no
9783          longer have a declaration and GDB will treat it as a definition.  */
9784
9785       switch (a->dw_attr)
9786         {
9787         case DW_AT_artificial:
9788         case DW_AT_containing_type:
9789         case DW_AT_external:
9790         case DW_AT_name:
9791         case DW_AT_type:
9792         case DW_AT_virtuality:
9793         case DW_AT_linkage_name:
9794         case DW_AT_MIPS_linkage_name:
9795           add_dwarf_attr (clone, a);
9796           break;
9797         case DW_AT_byte_size:
9798         default:
9799           break;
9800         }
9801     }
9802
9803   if (die->die_id.die_type_node)
9804     add_AT_die_ref (clone, DW_AT_signature, die);
9805
9806   add_AT_flag (clone, DW_AT_declaration, 1);
9807   return clone;
9808 }
9809
9810 /* Copy the declaration context to the new compile unit DIE.  This includes
9811    any surrounding namespace or type declarations.  If the DIE has an
9812    AT_specification attribute, it also includes attributes and children
9813    attached to the specification.  */
9814
9815 static void
9816 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9817 {
9818   dw_die_ref decl;
9819   dw_die_ref new_decl;
9820
9821   decl = get_AT_ref (die, DW_AT_specification);
9822   if (decl == NULL)
9823     decl = die;
9824   else
9825     {
9826       unsigned ix;
9827       dw_die_ref c;
9828       dw_attr_ref a;
9829
9830       /* Copy the type node pointer from the new DIE to the original
9831          declaration DIE so we can forward references later.  */
9832       decl->die_id.die_type_node = die->die_id.die_type_node;
9833
9834       remove_AT (die, DW_AT_specification);
9835
9836       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9837         {
9838           if (a->dw_attr != DW_AT_name
9839               && a->dw_attr != DW_AT_declaration
9840               && a->dw_attr != DW_AT_external)
9841             add_dwarf_attr (die, a);
9842         }
9843
9844       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9845     }
9846
9847   if (decl->die_parent != NULL
9848       && decl->die_parent->die_tag != DW_TAG_compile_unit
9849       && decl->die_parent->die_tag != DW_TAG_type_unit)
9850     {
9851       new_decl = copy_ancestor_tree (unit, decl, NULL);
9852       if (new_decl != NULL)
9853         {
9854           remove_AT (new_decl, DW_AT_signature);
9855           add_AT_specification (die, new_decl);
9856         }
9857     }
9858 }
9859
9860 /* Generate the skeleton ancestor tree for the given NODE, then clone
9861    the DIE and add the clone into the tree.  */
9862
9863 static void
9864 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9865 {
9866   if (node->new_die != NULL)
9867     return;
9868
9869   node->new_die = clone_as_declaration (node->old_die);
9870
9871   if (node->parent != NULL)
9872     {
9873       generate_skeleton_ancestor_tree (node->parent);
9874       add_child_die (node->parent->new_die, node->new_die);
9875     }
9876 }
9877
9878 /* Generate a skeleton tree of DIEs containing any declarations that are
9879    found in the original tree.  We traverse the tree looking for declaration
9880    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9881
9882 static void
9883 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9884 {
9885   skeleton_chain_node node;
9886   dw_die_ref c;
9887   dw_die_ref first;
9888   dw_die_ref prev = NULL;
9889   dw_die_ref next = NULL;
9890
9891   node.parent = parent;
9892
9893   first = c = parent->old_die->die_child;
9894   if (c)
9895     next = c->die_sib;
9896   if (c) do {
9897     if (prev == NULL || prev->die_sib == c)
9898       prev = c;
9899     c = next;
9900     next = (c == first ? NULL : c->die_sib);
9901     node.old_die = c;
9902     node.new_die = NULL;
9903     if (is_declaration_die (c))
9904       {
9905         /* Clone the existing DIE, move the original to the skeleton
9906            tree (which is in the main CU), and put the clone, with
9907            all the original's children, where the original came from.  */
9908         dw_die_ref clone = clone_die (c);
9909         move_all_children (c, clone);
9910
9911         replace_child (c, clone, prev);
9912         generate_skeleton_ancestor_tree (parent);
9913         add_child_die (parent->new_die, c);
9914         node.new_die = c;
9915         c = clone;
9916       }
9917     generate_skeleton_bottom_up (&node);
9918   } while (next != NULL);
9919 }
9920
9921 /* Wrapper function for generate_skeleton_bottom_up.  */
9922
9923 static dw_die_ref
9924 generate_skeleton (dw_die_ref die)
9925 {
9926   skeleton_chain_node node;
9927
9928   node.old_die = die;
9929   node.new_die = NULL;
9930   node.parent = NULL;
9931
9932   /* If this type definition is nested inside another type,
9933      always leave at least a declaration in its place.  */
9934   if (die->die_parent != NULL && is_type_die (die->die_parent))
9935     node.new_die = clone_as_declaration (die);
9936
9937   generate_skeleton_bottom_up (&node);
9938   return node.new_die;
9939 }
9940
9941 /* Remove the DIE from its parent, possibly replacing it with a cloned
9942    declaration.  The original DIE will be moved to a new compile unit
9943    so that existing references to it follow it to the new location.  If
9944    any of the original DIE's descendants is a declaration, we need to
9945    replace the original DIE with a skeleton tree and move the
9946    declarations back into the skeleton tree.  */
9947
9948 static dw_die_ref
9949 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9950 {
9951   dw_die_ref skeleton;
9952
9953   skeleton = generate_skeleton (child);
9954   if (skeleton == NULL)
9955     remove_child_with_prev (child, prev);
9956   else
9957     {
9958       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9959       replace_child (child, skeleton, prev);
9960     }
9961
9962   return skeleton;
9963 }
9964
9965 /* Traverse the DIE and set up additional .debug_types sections for each
9966    type worthy of being placed in a COMDAT section.  */
9967
9968 static void
9969 break_out_comdat_types (dw_die_ref die)
9970 {
9971   dw_die_ref c;
9972   dw_die_ref first;
9973   dw_die_ref prev = NULL;
9974   dw_die_ref next = NULL;
9975   dw_die_ref unit = NULL;
9976
9977   first = c = die->die_child;
9978   if (c)
9979     next = c->die_sib;
9980   if (c) do {
9981     if (prev == NULL || prev->die_sib == c)
9982       prev = c;
9983     c = next;
9984     next = (c == first ? NULL : c->die_sib);
9985     if (should_move_die_to_comdat (c))
9986       {
9987         dw_die_ref replacement;
9988         comdat_type_node_ref type_node;
9989
9990         /* Create a new type unit DIE as the root for the new tree, and
9991            add it to the list of comdat types.  */
9992         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9993         add_AT_unsigned (unit, DW_AT_language,
9994                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9995         type_node = ggc_alloc_cleared_comdat_type_node ();
9996         type_node->root_die = unit;
9997         type_node->next = comdat_type_list;
9998         comdat_type_list = type_node;
9999
10000         /* Generate the type signature.  */
10001         generate_type_signature (c, type_node);
10002
10003         /* Copy the declaration context, attributes, and children of the
10004            declaration into the new compile unit DIE.  */
10005         copy_declaration_context (unit, c);
10006
10007         /* Remove this DIE from the main CU.  */
10008         replacement = remove_child_or_replace_with_skeleton (c, prev);
10009
10010         /* Break out nested types into their own type units.  */
10011         break_out_comdat_types (c);
10012
10013         /* Add the DIE to the new compunit.  */
10014         add_child_die (unit, c);
10015
10016         if (replacement != NULL)
10017           c = replacement;
10018       }
10019     else if (c->die_tag == DW_TAG_namespace
10020              || c->die_tag == DW_TAG_class_type
10021              || c->die_tag == DW_TAG_structure_type
10022              || c->die_tag == DW_TAG_union_type)
10023       {
10024         /* Look for nested types that can be broken out.  */
10025         break_out_comdat_types (c);
10026       }
10027   } while (next != NULL);
10028 }
10029
10030 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10031
10032 struct decl_table_entry
10033 {
10034   dw_die_ref orig;
10035   dw_die_ref copy;
10036 };
10037
10038 /* Routines to manipulate hash table of copied declarations.  */
10039
10040 static hashval_t
10041 htab_decl_hash (const void *of)
10042 {
10043   const struct decl_table_entry *const entry =
10044     (const struct decl_table_entry *) of;
10045
10046   return htab_hash_pointer (entry->orig);
10047 }
10048
10049 static int
10050 htab_decl_eq (const void *of1, const void *of2)
10051 {
10052   const struct decl_table_entry *const entry1 =
10053     (const struct decl_table_entry *) of1;
10054   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10055
10056   return entry1->orig == entry2;
10057 }
10058
10059 static void
10060 htab_decl_del (void *what)
10061 {
10062   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10063
10064   free (entry);
10065 }
10066
10067 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10068    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10069    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10070    to check if the ancestor has already been copied into UNIT.  */
10071
10072 static dw_die_ref
10073 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10074 {
10075   dw_die_ref parent = die->die_parent;
10076   dw_die_ref new_parent = unit;
10077   dw_die_ref copy;
10078   void **slot = NULL;
10079   struct decl_table_entry *entry = NULL;
10080
10081   if (decl_table)
10082     {
10083       /* Check if the entry has already been copied to UNIT.  */
10084       slot = htab_find_slot_with_hash (decl_table, die,
10085                                        htab_hash_pointer (die), INSERT);
10086       if (*slot != HTAB_EMPTY_ENTRY)
10087         {
10088           entry = (struct decl_table_entry *) *slot;
10089           return entry->copy;
10090         }
10091
10092       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10093       entry = XCNEW (struct decl_table_entry);
10094       entry->orig = die;
10095       entry->copy = NULL;
10096       *slot = entry;
10097     }
10098
10099   if (parent != NULL)
10100     {
10101       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10102       if (spec != NULL)
10103         parent = spec;
10104       if (parent->die_tag != DW_TAG_compile_unit
10105           && parent->die_tag != DW_TAG_type_unit)
10106         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10107     }
10108
10109   copy = clone_as_declaration (die);
10110   add_child_die (new_parent, copy);
10111
10112   if (decl_table != NULL)
10113     {
10114       /* Record the pointer to the copy.  */
10115       entry->copy = copy;
10116     }
10117
10118   return copy;
10119 }
10120
10121 /* Walk the DIE and its children, looking for references to incomplete
10122    or trivial types that are unmarked (i.e., that are not in the current
10123    type_unit).  */
10124
10125 static void
10126 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10127 {
10128   dw_die_ref c;
10129   dw_attr_ref a;
10130   unsigned ix;
10131
10132   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10133     {
10134       if (AT_class (a) == dw_val_class_die_ref)
10135         {
10136           dw_die_ref targ = AT_ref (a);
10137           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10138           void **slot;
10139           struct decl_table_entry *entry;
10140
10141           if (targ->die_mark != 0 || type_node != NULL)
10142             continue;
10143
10144           slot = htab_find_slot_with_hash (decl_table, targ,
10145                                            htab_hash_pointer (targ), INSERT);
10146
10147           if (*slot != HTAB_EMPTY_ENTRY)
10148             {
10149               /* TARG has already been copied, so we just need to
10150                  modify the reference to point to the copy.  */
10151               entry = (struct decl_table_entry *) *slot;
10152               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10153             }
10154           else
10155             {
10156               dw_die_ref parent = unit;
10157               dw_die_ref copy = clone_tree (targ);
10158
10159               /* Make sure the cloned tree is marked as part of the
10160                  type unit.  */
10161               mark_dies (copy);
10162
10163               /* Record in DECL_TABLE that TARG has been copied.
10164                  Need to do this now, before the recursive call,
10165                  because DECL_TABLE may be expanded and SLOT
10166                  would no longer be a valid pointer.  */
10167               entry = XCNEW (struct decl_table_entry);
10168               entry->orig = targ;
10169               entry->copy = copy;
10170               *slot = entry;
10171
10172               /* If TARG has surrounding context, copy its ancestor tree
10173                  into the new type unit.  */
10174               if (targ->die_parent != NULL
10175                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10176                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10177                 parent = copy_ancestor_tree (unit, targ->die_parent,
10178                                              decl_table);
10179
10180               add_child_die (parent, copy);
10181               a->dw_attr_val.v.val_die_ref.die = copy;
10182
10183               /* Make sure the newly-copied DIE is walked.  If it was
10184                  installed in a previously-added context, it won't
10185                  get visited otherwise.  */
10186               if (parent != unit)
10187                 {
10188                   /* Find the highest point of the newly-added tree,
10189                      mark each node along the way, and walk from there.  */
10190                   parent->die_mark = 1;
10191                   while (parent->die_parent
10192                          && parent->die_parent->die_mark == 0)
10193                     {
10194                       parent = parent->die_parent;
10195                       parent->die_mark = 1;
10196                     }
10197                   copy_decls_walk (unit, parent, decl_table);
10198                 }
10199             }
10200         }
10201     }
10202
10203   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10204 }
10205
10206 /* Copy declarations for "unworthy" types into the new comdat section.
10207    Incomplete types, modified types, and certain other types aren't broken
10208    out into comdat sections of their own, so they don't have a signature,
10209    and we need to copy the declaration into the same section so that we
10210    don't have an external reference.  */
10211
10212 static void
10213 copy_decls_for_unworthy_types (dw_die_ref unit)
10214 {
10215   htab_t decl_table;
10216
10217   mark_dies (unit);
10218   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10219   copy_decls_walk (unit, unit, decl_table);
10220   htab_delete (decl_table);
10221   unmark_dies (unit);
10222 }
10223
10224 /* Traverse the DIE and add a sibling attribute if it may have the
10225    effect of speeding up access to siblings.  To save some space,
10226    avoid generating sibling attributes for DIE's without children.  */
10227
10228 static void
10229 add_sibling_attributes (dw_die_ref die)
10230 {
10231   dw_die_ref c;
10232
10233   if (! die->die_child)
10234     return;
10235
10236   if (die->die_parent && die != die->die_parent->die_child)
10237     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10238
10239   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10240 }
10241
10242 /* Output all location lists for the DIE and its children.  */
10243
10244 static void
10245 output_location_lists (dw_die_ref die)
10246 {
10247   dw_die_ref c;
10248   dw_attr_ref a;
10249   unsigned ix;
10250
10251   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10252     if (AT_class (a) == dw_val_class_loc_list)
10253       output_loc_list (AT_loc_list (a));
10254
10255   FOR_EACH_CHILD (die, c, output_location_lists (c));
10256 }
10257
10258 /* The format of each DIE (and its attribute value pairs) is encoded in an
10259    abbreviation table.  This routine builds the abbreviation table and assigns
10260    a unique abbreviation id for each abbreviation entry.  The children of each
10261    die are visited recursively.  */
10262
10263 static void
10264 build_abbrev_table (dw_die_ref die)
10265 {
10266   unsigned long abbrev_id;
10267   unsigned int n_alloc;
10268   dw_die_ref c;
10269   dw_attr_ref a;
10270   unsigned ix;
10271
10272   /* Scan the DIE references, and mark as external any that refer to
10273      DIEs from other CUs (i.e. those which are not marked).  */
10274   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10275     if (AT_class (a) == dw_val_class_die_ref
10276         && AT_ref (a)->die_mark == 0)
10277       {
10278         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10279         set_AT_ref_external (a, 1);
10280       }
10281
10282   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10283     {
10284       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10285       dw_attr_ref die_a, abbrev_a;
10286       unsigned ix;
10287       bool ok = true;
10288
10289       if (abbrev->die_tag != die->die_tag)
10290         continue;
10291       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10292         continue;
10293
10294       if (VEC_length (dw_attr_node, abbrev->die_attr)
10295           != VEC_length (dw_attr_node, die->die_attr))
10296         continue;
10297
10298       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10299         {
10300           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10301           if ((abbrev_a->dw_attr != die_a->dw_attr)
10302               || (value_format (abbrev_a) != value_format (die_a)))
10303             {
10304               ok = false;
10305               break;
10306             }
10307         }
10308       if (ok)
10309         break;
10310     }
10311
10312   if (abbrev_id >= abbrev_die_table_in_use)
10313     {
10314       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10315         {
10316           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10317           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10318                                             n_alloc);
10319
10320           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10321                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10322           abbrev_die_table_allocated = n_alloc;
10323         }
10324
10325       ++abbrev_die_table_in_use;
10326       abbrev_die_table[abbrev_id] = die;
10327     }
10328
10329   die->die_abbrev = abbrev_id;
10330   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10331 }
10332 \f
10333 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10334
10335 static int
10336 constant_size (unsigned HOST_WIDE_INT value)
10337 {
10338   int log;
10339
10340   if (value == 0)
10341     log = 0;
10342   else
10343     log = floor_log2 (value);
10344
10345   log = log / 8;
10346   log = 1 << (floor_log2 (log) + 1);
10347
10348   return log;
10349 }
10350
10351 /* Return the size of a DIE as it is represented in the
10352    .debug_info section.  */
10353
10354 static unsigned long
10355 size_of_die (dw_die_ref die)
10356 {
10357   unsigned long size = 0;
10358   dw_attr_ref a;
10359   unsigned ix;
10360
10361   size += size_of_uleb128 (die->die_abbrev);
10362   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10363     {
10364       switch (AT_class (a))
10365         {
10366         case dw_val_class_addr:
10367           size += DWARF2_ADDR_SIZE;
10368           break;
10369         case dw_val_class_offset:
10370           size += DWARF_OFFSET_SIZE;
10371           break;
10372         case dw_val_class_loc:
10373           {
10374             unsigned long lsize = size_of_locs (AT_loc (a));
10375
10376             /* Block length.  */
10377             if (dwarf_version >= 4)
10378               size += size_of_uleb128 (lsize);
10379             else
10380               size += constant_size (lsize);
10381             size += lsize;
10382           }
10383           break;
10384         case dw_val_class_loc_list:
10385           size += DWARF_OFFSET_SIZE;
10386           break;
10387         case dw_val_class_range_list:
10388           size += DWARF_OFFSET_SIZE;
10389           break;
10390         case dw_val_class_const:
10391           size += size_of_sleb128 (AT_int (a));
10392           break;
10393         case dw_val_class_unsigned_const:
10394           size += constant_size (AT_unsigned (a));
10395           break;
10396         case dw_val_class_const_double:
10397           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10398           if (HOST_BITS_PER_WIDE_INT >= 64)
10399             size++; /* block */
10400           break;
10401         case dw_val_class_vec:
10402           size += constant_size (a->dw_attr_val.v.val_vec.length
10403                                  * a->dw_attr_val.v.val_vec.elt_size)
10404                   + a->dw_attr_val.v.val_vec.length
10405                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10406           break;
10407         case dw_val_class_flag:
10408           if (dwarf_version >= 4)
10409             /* Currently all add_AT_flag calls pass in 1 as last argument,
10410                so DW_FORM_flag_present can be used.  If that ever changes,
10411                we'll need to use DW_FORM_flag and have some optimization
10412                in build_abbrev_table that will change those to
10413                DW_FORM_flag_present if it is set to 1 in all DIEs using
10414                the same abbrev entry.  */
10415             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10416           else
10417             size += 1;
10418           break;
10419         case dw_val_class_die_ref:
10420           if (AT_ref_external (a))
10421             {
10422               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10423                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10424                  is sized by target address length, whereas in DWARF3
10425                  it's always sized as an offset.  */
10426               if (dwarf_version >= 4)
10427                 size += DWARF_TYPE_SIGNATURE_SIZE;
10428               else if (dwarf_version == 2)
10429                 size += DWARF2_ADDR_SIZE;
10430               else
10431                 size += DWARF_OFFSET_SIZE;
10432             }
10433           else
10434             size += DWARF_OFFSET_SIZE;
10435           break;
10436         case dw_val_class_fde_ref:
10437           size += DWARF_OFFSET_SIZE;
10438           break;
10439         case dw_val_class_lbl_id:
10440           size += DWARF2_ADDR_SIZE;
10441           break;
10442         case dw_val_class_lineptr:
10443         case dw_val_class_macptr:
10444           size += DWARF_OFFSET_SIZE;
10445           break;
10446         case dw_val_class_str:
10447           if (AT_string_form (a) == DW_FORM_strp)
10448             size += DWARF_OFFSET_SIZE;
10449           else
10450             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10451           break;
10452         case dw_val_class_file:
10453           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10454           break;
10455         case dw_val_class_data8:
10456           size += 8;
10457           break;
10458         case dw_val_class_vms_delta:
10459           size += DWARF_OFFSET_SIZE;
10460           break;
10461         default:
10462           gcc_unreachable ();
10463         }
10464     }
10465
10466   return size;
10467 }
10468
10469 /* Size the debugging information associated with a given DIE.  Visits the
10470    DIE's children recursively.  Updates the global variable next_die_offset, on
10471    each time through.  Uses the current value of next_die_offset to update the
10472    die_offset field in each DIE.  */
10473
10474 static void
10475 calc_die_sizes (dw_die_ref die)
10476 {
10477   dw_die_ref c;
10478
10479   die->die_offset = next_die_offset;
10480   next_die_offset += size_of_die (die);
10481
10482   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10483
10484   if (die->die_child != NULL)
10485     /* Count the null byte used to terminate sibling lists.  */
10486     next_die_offset += 1;
10487 }
10488
10489 /* Set the marks for a die and its children.  We do this so
10490    that we know whether or not a reference needs to use FORM_ref_addr; only
10491    DIEs in the same CU will be marked.  We used to clear out the offset
10492    and use that as the flag, but ran into ordering problems.  */
10493
10494 static void
10495 mark_dies (dw_die_ref die)
10496 {
10497   dw_die_ref c;
10498
10499   gcc_assert (!die->die_mark);
10500
10501   die->die_mark = 1;
10502   FOR_EACH_CHILD (die, c, mark_dies (c));
10503 }
10504
10505 /* Clear the marks for a die and its children.  */
10506
10507 static void
10508 unmark_dies (dw_die_ref die)
10509 {
10510   dw_die_ref c;
10511
10512   if (dwarf_version < 4)
10513     gcc_assert (die->die_mark);
10514
10515   die->die_mark = 0;
10516   FOR_EACH_CHILD (die, c, unmark_dies (c));
10517 }
10518
10519 /* Clear the marks for a die, its children and referred dies.  */
10520
10521 static void
10522 unmark_all_dies (dw_die_ref die)
10523 {
10524   dw_die_ref c;
10525   dw_attr_ref a;
10526   unsigned ix;
10527
10528   if (!die->die_mark)
10529     return;
10530   die->die_mark = 0;
10531
10532   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10533
10534   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10535     if (AT_class (a) == dw_val_class_die_ref)
10536       unmark_all_dies (AT_ref (a));
10537 }
10538
10539 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10540    generated for the compilation unit.  */
10541
10542 static unsigned long
10543 size_of_pubnames (VEC (pubname_entry, gc) * names)
10544 {
10545   unsigned long size;
10546   unsigned i;
10547   pubname_ref p;
10548
10549   size = DWARF_PUBNAMES_HEADER_SIZE;
10550   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10551     if (names != pubtype_table
10552         || p->die->die_offset != 0
10553         || !flag_eliminate_unused_debug_types)
10554       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10555
10556   size += DWARF_OFFSET_SIZE;
10557   return size;
10558 }
10559
10560 /* Return the size of the information in the .debug_aranges section.  */
10561
10562 static unsigned long
10563 size_of_aranges (void)
10564 {
10565   unsigned long size;
10566
10567   size = DWARF_ARANGES_HEADER_SIZE;
10568
10569   /* Count the address/length pair for this compilation unit.  */
10570   if (text_section_used)
10571     size += 2 * DWARF2_ADDR_SIZE;
10572   if (cold_text_section_used)
10573     size += 2 * DWARF2_ADDR_SIZE;
10574   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10575
10576   /* Count the two zero words used to terminated the address range table.  */
10577   size += 2 * DWARF2_ADDR_SIZE;
10578   return size;
10579 }
10580 \f
10581 /* Select the encoding of an attribute value.  */
10582
10583 static enum dwarf_form
10584 value_format (dw_attr_ref a)
10585 {
10586   switch (a->dw_attr_val.val_class)
10587     {
10588     case dw_val_class_addr:
10589       /* Only very few attributes allow DW_FORM_addr.  */
10590       switch (a->dw_attr)
10591         {
10592         case DW_AT_low_pc:
10593         case DW_AT_high_pc:
10594         case DW_AT_entry_pc:
10595         case DW_AT_trampoline:
10596           return DW_FORM_addr;
10597         default:
10598           break;
10599         }
10600       switch (DWARF2_ADDR_SIZE)
10601         {
10602         case 1:
10603           return DW_FORM_data1;
10604         case 2:
10605           return DW_FORM_data2;
10606         case 4:
10607           return DW_FORM_data4;
10608         case 8:
10609           return DW_FORM_data8;
10610         default:
10611           gcc_unreachable ();
10612         }
10613     case dw_val_class_range_list:
10614     case dw_val_class_loc_list:
10615       if (dwarf_version >= 4)
10616         return DW_FORM_sec_offset;
10617       /* FALLTHRU */
10618     case dw_val_class_vms_delta:
10619     case dw_val_class_offset:
10620       switch (DWARF_OFFSET_SIZE)
10621         {
10622         case 4:
10623           return DW_FORM_data4;
10624         case 8:
10625           return DW_FORM_data8;
10626         default:
10627           gcc_unreachable ();
10628         }
10629     case dw_val_class_loc:
10630       if (dwarf_version >= 4)
10631         return DW_FORM_exprloc;
10632       switch (constant_size (size_of_locs (AT_loc (a))))
10633         {
10634         case 1:
10635           return DW_FORM_block1;
10636         case 2:
10637           return DW_FORM_block2;
10638         default:
10639           gcc_unreachable ();
10640         }
10641     case dw_val_class_const:
10642       return DW_FORM_sdata;
10643     case dw_val_class_unsigned_const:
10644       switch (constant_size (AT_unsigned (a)))
10645         {
10646         case 1:
10647           return DW_FORM_data1;
10648         case 2:
10649           return DW_FORM_data2;
10650         case 4:
10651           return DW_FORM_data4;
10652         case 8:
10653           return DW_FORM_data8;
10654         default:
10655           gcc_unreachable ();
10656         }
10657     case dw_val_class_const_double:
10658       switch (HOST_BITS_PER_WIDE_INT)
10659         {
10660         case 8:
10661           return DW_FORM_data2;
10662         case 16:
10663           return DW_FORM_data4;
10664         case 32:
10665           return DW_FORM_data8;
10666         case 64:
10667         default:
10668           return DW_FORM_block1;
10669         }
10670     case dw_val_class_vec:
10671       switch (constant_size (a->dw_attr_val.v.val_vec.length
10672                              * a->dw_attr_val.v.val_vec.elt_size))
10673         {
10674         case 1:
10675           return DW_FORM_block1;
10676         case 2:
10677           return DW_FORM_block2;
10678         case 4:
10679           return DW_FORM_block4;
10680         default:
10681           gcc_unreachable ();
10682         }
10683     case dw_val_class_flag:
10684       if (dwarf_version >= 4)
10685         {
10686           /* Currently all add_AT_flag calls pass in 1 as last argument,
10687              so DW_FORM_flag_present can be used.  If that ever changes,
10688              we'll need to use DW_FORM_flag and have some optimization
10689              in build_abbrev_table that will change those to
10690              DW_FORM_flag_present if it is set to 1 in all DIEs using
10691              the same abbrev entry.  */
10692           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10693           return DW_FORM_flag_present;
10694         }
10695       return DW_FORM_flag;
10696     case dw_val_class_die_ref:
10697       if (AT_ref_external (a))
10698         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10699       else
10700         return DW_FORM_ref;
10701     case dw_val_class_fde_ref:
10702       return DW_FORM_data;
10703     case dw_val_class_lbl_id:
10704       return DW_FORM_addr;
10705     case dw_val_class_lineptr:
10706     case dw_val_class_macptr:
10707       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10708     case dw_val_class_str:
10709       return AT_string_form (a);
10710     case dw_val_class_file:
10711       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10712         {
10713         case 1:
10714           return DW_FORM_data1;
10715         case 2:
10716           return DW_FORM_data2;
10717         case 4:
10718           return DW_FORM_data4;
10719         default:
10720           gcc_unreachable ();
10721         }
10722
10723     case dw_val_class_data8:
10724       return DW_FORM_data8;
10725
10726     default:
10727       gcc_unreachable ();
10728     }
10729 }
10730
10731 /* Output the encoding of an attribute value.  */
10732
10733 static void
10734 output_value_format (dw_attr_ref a)
10735 {
10736   enum dwarf_form form = value_format (a);
10737
10738   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10739 }
10740
10741 /* Output the .debug_abbrev section which defines the DIE abbreviation
10742    table.  */
10743
10744 static void
10745 output_abbrev_section (void)
10746 {
10747   unsigned long abbrev_id;
10748
10749   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10750     {
10751       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10752       unsigned ix;
10753       dw_attr_ref a_attr;
10754
10755       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10756       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10757                                    dwarf_tag_name (abbrev->die_tag));
10758
10759       if (abbrev->die_child != NULL)
10760         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10761       else
10762         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10763
10764       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10765            ix++)
10766         {
10767           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10768                                        dwarf_attr_name (a_attr->dw_attr));
10769           output_value_format (a_attr);
10770         }
10771
10772       dw2_asm_output_data (1, 0, NULL);
10773       dw2_asm_output_data (1, 0, NULL);
10774     }
10775
10776   /* Terminate the table.  */
10777   dw2_asm_output_data (1, 0, NULL);
10778 }
10779
10780 /* Output a symbol we can use to refer to this DIE from another CU.  */
10781
10782 static inline void
10783 output_die_symbol (dw_die_ref die)
10784 {
10785   char *sym = die->die_id.die_symbol;
10786
10787   if (sym == 0)
10788     return;
10789
10790   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10791     /* We make these global, not weak; if the target doesn't support
10792        .linkonce, it doesn't support combining the sections, so debugging
10793        will break.  */
10794     targetm.asm_out.globalize_label (asm_out_file, sym);
10795
10796   ASM_OUTPUT_LABEL (asm_out_file, sym);
10797 }
10798
10799 /* Return a new location list, given the begin and end range, and the
10800    expression.  */
10801
10802 static inline dw_loc_list_ref
10803 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10804               const char *section)
10805 {
10806   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10807
10808   retlist->begin = begin;
10809   retlist->end = end;
10810   retlist->expr = expr;
10811   retlist->section = section;
10812
10813   return retlist;
10814 }
10815
10816 /* Generate a new internal symbol for this location list node, if it
10817    hasn't got one yet.  */
10818
10819 static inline void
10820 gen_llsym (dw_loc_list_ref list)
10821 {
10822   gcc_assert (!list->ll_symbol);
10823   list->ll_symbol = gen_internal_sym ("LLST");
10824 }
10825
10826 /* Output the location list given to us.  */
10827
10828 static void
10829 output_loc_list (dw_loc_list_ref list_head)
10830 {
10831   dw_loc_list_ref curr = list_head;
10832
10833   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10834
10835   /* Walk the location list, and output each range + expression.  */
10836   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10837     {
10838       unsigned long size;
10839       /* Don't output an entry that starts and ends at the same address.  */
10840       if (strcmp (curr->begin, curr->end) == 0)
10841         continue;
10842       if (!have_multiple_function_sections)
10843         {
10844           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10845                                 "Location list begin address (%s)",
10846                                 list_head->ll_symbol);
10847           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10848                                 "Location list end address (%s)",
10849                                 list_head->ll_symbol);
10850         }
10851       else
10852         {
10853           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10854                                "Location list begin address (%s)",
10855                                list_head->ll_symbol);
10856           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10857                                "Location list end address (%s)",
10858                                list_head->ll_symbol);
10859         }
10860       size = size_of_locs (curr->expr);
10861
10862       /* Output the block length for this list of location operations.  */
10863       gcc_assert (size <= 0xffff);
10864       dw2_asm_output_data (2, size, "%s", "Location expression size");
10865
10866       output_loc_sequence (curr->expr);
10867     }
10868
10869   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10870                        "Location list terminator begin (%s)",
10871                        list_head->ll_symbol);
10872   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10873                        "Location list terminator end (%s)",
10874                        list_head->ll_symbol);
10875 }
10876
10877 /* Output a type signature.  */
10878
10879 static inline void
10880 output_signature (const char *sig, const char *name)
10881 {
10882   int i;
10883
10884   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10885     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10886 }
10887
10888 /* Output the DIE and its attributes.  Called recursively to generate
10889    the definitions of each child DIE.  */
10890
10891 static void
10892 output_die (dw_die_ref die)
10893 {
10894   dw_attr_ref a;
10895   dw_die_ref c;
10896   unsigned long size;
10897   unsigned ix;
10898
10899   /* If someone in another CU might refer to us, set up a symbol for
10900      them to point to.  */
10901   if (dwarf_version < 4 && die->die_id.die_symbol)
10902     output_die_symbol (die);
10903
10904   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10905                                (unsigned long)die->die_offset,
10906                                dwarf_tag_name (die->die_tag));
10907
10908   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10909     {
10910       const char *name = dwarf_attr_name (a->dw_attr);
10911
10912       switch (AT_class (a))
10913         {
10914         case dw_val_class_addr:
10915           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10916           break;
10917
10918         case dw_val_class_offset:
10919           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10920                                "%s", name);
10921           break;
10922
10923         case dw_val_class_range_list:
10924           {
10925             char *p = strchr (ranges_section_label, '\0');
10926
10927             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10928                      a->dw_attr_val.v.val_offset);
10929             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10930                                    debug_ranges_section, "%s", name);
10931             *p = '\0';
10932           }
10933           break;
10934
10935         case dw_val_class_loc:
10936           size = size_of_locs (AT_loc (a));
10937
10938           /* Output the block length for this list of location operations.  */
10939           if (dwarf_version >= 4)
10940             dw2_asm_output_data_uleb128 (size, "%s", name);
10941           else
10942             dw2_asm_output_data (constant_size (size), size, "%s", name);
10943
10944           output_loc_sequence (AT_loc (a));
10945           break;
10946
10947         case dw_val_class_const:
10948           /* ??? It would be slightly more efficient to use a scheme like is
10949              used for unsigned constants below, but gdb 4.x does not sign
10950              extend.  Gdb 5.x does sign extend.  */
10951           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10952           break;
10953
10954         case dw_val_class_unsigned_const:
10955           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10956                                AT_unsigned (a), "%s", name);
10957           break;
10958
10959         case dw_val_class_const_double:
10960           {
10961             unsigned HOST_WIDE_INT first, second;
10962
10963             if (HOST_BITS_PER_WIDE_INT >= 64)
10964               dw2_asm_output_data (1,
10965                                    2 * HOST_BITS_PER_WIDE_INT
10966                                    / HOST_BITS_PER_CHAR,
10967                                    NULL);
10968
10969             if (WORDS_BIG_ENDIAN)
10970               {
10971                 first = a->dw_attr_val.v.val_double.high;
10972                 second = a->dw_attr_val.v.val_double.low;
10973               }
10974             else
10975               {
10976                 first = a->dw_attr_val.v.val_double.low;
10977                 second = a->dw_attr_val.v.val_double.high;
10978               }
10979
10980             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10981                                  first, name);
10982             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10983                                  second, NULL);
10984           }
10985           break;
10986
10987         case dw_val_class_vec:
10988           {
10989             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10990             unsigned int len = a->dw_attr_val.v.val_vec.length;
10991             unsigned int i;
10992             unsigned char *p;
10993
10994             dw2_asm_output_data (constant_size (len * elt_size),
10995                                  len * elt_size, "%s", name);
10996             if (elt_size > sizeof (HOST_WIDE_INT))
10997               {
10998                 elt_size /= 2;
10999                 len *= 2;
11000               }
11001             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11002                  i < len;
11003                  i++, p += elt_size)
11004               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11005                                    "fp or vector constant word %u", i);
11006             break;
11007           }
11008
11009         case dw_val_class_flag:
11010           if (dwarf_version >= 4)
11011             {
11012               /* Currently all add_AT_flag calls pass in 1 as last argument,
11013                  so DW_FORM_flag_present can be used.  If that ever changes,
11014                  we'll need to use DW_FORM_flag and have some optimization
11015                  in build_abbrev_table that will change those to
11016                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11017                  the same abbrev entry.  */
11018               gcc_assert (AT_flag (a) == 1);
11019               if (flag_debug_asm)
11020                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11021                          ASM_COMMENT_START, name);
11022               break;
11023             }
11024           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11025           break;
11026
11027         case dw_val_class_loc_list:
11028           {
11029             char *sym = AT_loc_list (a)->ll_symbol;
11030
11031             gcc_assert (sym);
11032             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11033                                    "%s", name);
11034           }
11035           break;
11036
11037         case dw_val_class_die_ref:
11038           if (AT_ref_external (a))
11039             {
11040               if (dwarf_version >= 4)
11041                 {
11042                   comdat_type_node_ref type_node =
11043                     AT_ref (a)->die_id.die_type_node;
11044
11045                   gcc_assert (type_node);
11046                   output_signature (type_node->signature, name);
11047                 }
11048               else
11049                 {
11050                   char *sym = AT_ref (a)->die_id.die_symbol;
11051                   int size;
11052
11053                   gcc_assert (sym);
11054                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11055                      length, whereas in DWARF3 it's always sized as an
11056                      offset.  */
11057                   if (dwarf_version == 2)
11058                     size = DWARF2_ADDR_SIZE;
11059                   else
11060                     size = DWARF_OFFSET_SIZE;
11061                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11062                                          name);
11063                 }
11064             }
11065           else
11066             {
11067               gcc_assert (AT_ref (a)->die_offset);
11068               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11069                                    "%s", name);
11070             }
11071           break;
11072
11073         case dw_val_class_fde_ref:
11074           {
11075             char l1[20];
11076
11077             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11078                                          a->dw_attr_val.v.val_fde_index * 2);
11079             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11080                                    "%s", name);
11081           }
11082           break;
11083
11084         case dw_val_class_vms_delta:
11085           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11086                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11087                                     "%s", name);
11088           break;
11089
11090         case dw_val_class_lbl_id:
11091           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11092           break;
11093
11094         case dw_val_class_lineptr:
11095           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11096                                  debug_line_section, "%s", name);
11097           break;
11098
11099         case dw_val_class_macptr:
11100           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11101                                  debug_macinfo_section, "%s", name);
11102           break;
11103
11104         case dw_val_class_str:
11105           if (AT_string_form (a) == DW_FORM_strp)
11106             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11107                                    a->dw_attr_val.v.val_str->label,
11108                                    debug_str_section,
11109                                    "%s: \"%s\"", name, AT_string (a));
11110           else
11111             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11112           break;
11113
11114         case dw_val_class_file:
11115           {
11116             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11117
11118             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11119                                  a->dw_attr_val.v.val_file->filename);
11120             break;
11121           }
11122
11123         case dw_val_class_data8:
11124           {
11125             int i;
11126
11127             for (i = 0; i < 8; i++)
11128               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11129                                    i == 0 ? "%s" : NULL, name);
11130             break;
11131           }
11132
11133         default:
11134           gcc_unreachable ();
11135         }
11136     }
11137
11138   FOR_EACH_CHILD (die, c, output_die (c));
11139
11140   /* Add null byte to terminate sibling list.  */
11141   if (die->die_child != NULL)
11142     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11143                          (unsigned long) die->die_offset);
11144 }
11145
11146 /* Output the compilation unit that appears at the beginning of the
11147    .debug_info section, and precedes the DIE descriptions.  */
11148
11149 static void
11150 output_compilation_unit_header (void)
11151 {
11152   int ver = dwarf_version;
11153
11154   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11155     dw2_asm_output_data (4, 0xffffffff,
11156       "Initial length escape value indicating 64-bit DWARF extension");
11157   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11158                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11159                        "Length of Compilation Unit Info");
11160   dw2_asm_output_data (2, ver, "DWARF version number");
11161   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11162                          debug_abbrev_section,
11163                          "Offset Into Abbrev. Section");
11164   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11165 }
11166
11167 /* Output the compilation unit DIE and its children.  */
11168
11169 static void
11170 output_comp_unit (dw_die_ref die, int output_if_empty)
11171 {
11172   const char *secname;
11173   char *oldsym, *tmp;
11174
11175   /* Unless we are outputting main CU, we may throw away empty ones.  */
11176   if (!output_if_empty && die->die_child == NULL)
11177     return;
11178
11179   /* Even if there are no children of this DIE, we must output the information
11180      about the compilation unit.  Otherwise, on an empty translation unit, we
11181      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11182      will then complain when examining the file.  First mark all the DIEs in
11183      this CU so we know which get local refs.  */
11184   mark_dies (die);
11185
11186   build_abbrev_table (die);
11187
11188   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11189   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11190   calc_die_sizes (die);
11191
11192   oldsym = die->die_id.die_symbol;
11193   if (oldsym)
11194     {
11195       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11196
11197       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11198       secname = tmp;
11199       die->die_id.die_symbol = NULL;
11200       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11201     }
11202   else
11203     switch_to_section (debug_info_section);
11204
11205   /* Output debugging information.  */
11206   output_compilation_unit_header ();
11207   output_die (die);
11208
11209   /* Leave the marks on the main CU, so we can check them in
11210      output_pubnames.  */
11211   if (oldsym)
11212     {
11213       unmark_dies (die);
11214       die->die_id.die_symbol = oldsym;
11215     }
11216 }
11217
11218 /* Output a comdat type unit DIE and its children.  */
11219
11220 static void
11221 output_comdat_type_unit (comdat_type_node *node)
11222 {
11223   const char *secname;
11224   char *tmp;
11225   int i;
11226 #if defined (OBJECT_FORMAT_ELF)
11227   tree comdat_key;
11228 #endif
11229
11230   /* First mark all the DIEs in this CU so we know which get local refs.  */
11231   mark_dies (node->root_die);
11232
11233   build_abbrev_table (node->root_die);
11234
11235   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11236   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11237   calc_die_sizes (node->root_die);
11238
11239 #if defined (OBJECT_FORMAT_ELF)
11240   secname = ".debug_types";
11241   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11242   sprintf (tmp, "wt.");
11243   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11244     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11245   comdat_key = get_identifier (tmp);
11246   targetm.asm_out.named_section (secname,
11247                                  SECTION_DEBUG | SECTION_LINKONCE,
11248                                  comdat_key);
11249 #else
11250   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11251   sprintf (tmp, ".gnu.linkonce.wt.");
11252   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11253     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11254   secname = tmp;
11255   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11256 #endif
11257
11258   /* Output debugging information.  */
11259   output_compilation_unit_header ();
11260   output_signature (node->signature, "Type Signature");
11261   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11262                        "Offset to Type DIE");
11263   output_die (node->root_die);
11264
11265   unmark_dies (node->root_die);
11266 }
11267
11268 /* Return the DWARF2/3 pubname associated with a decl.  */
11269
11270 static const char *
11271 dwarf2_name (tree decl, int scope)
11272 {
11273   if (DECL_NAMELESS (decl))
11274     return NULL;
11275   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11276 }
11277
11278 /* Add a new entry to .debug_pubnames if appropriate.  */
11279
11280 static void
11281 add_pubname_string (const char *str, dw_die_ref die)
11282 {
11283   if (targetm.want_debug_pub_sections)
11284     {
11285       pubname_entry e;
11286
11287       e.die = die;
11288       e.name = xstrdup (str);
11289       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11290     }
11291 }
11292
11293 static void
11294 add_pubname (tree decl, dw_die_ref die)
11295 {
11296   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11297     {
11298       const char *name = dwarf2_name (decl, 1);
11299       if (name)
11300         add_pubname_string (name, die);
11301     }
11302 }
11303
11304 /* Add a new entry to .debug_pubtypes if appropriate.  */
11305
11306 static void
11307 add_pubtype (tree decl, dw_die_ref die)
11308 {
11309   pubname_entry e;
11310
11311   if (!targetm.want_debug_pub_sections)
11312     return;
11313
11314   e.name = NULL;
11315   if ((TREE_PUBLIC (decl)
11316        || die->die_parent == comp_unit_die)
11317       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11318     {
11319       e.die = die;
11320       if (TYPE_P (decl))
11321         {
11322           if (TYPE_NAME (decl))
11323             {
11324               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11325                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11326               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11327                        && DECL_NAME (TYPE_NAME (decl)))
11328                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11329               else
11330                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11331             }
11332         }
11333       else
11334         {
11335           e.name = dwarf2_name (decl, 1);
11336           if (e.name)
11337             e.name = xstrdup (e.name);
11338         }
11339
11340       /* If we don't have a name for the type, there's no point in adding
11341          it to the table.  */
11342       if (e.name && e.name[0] != '\0')
11343         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11344     }
11345 }
11346
11347 /* Output the public names table used to speed up access to externally
11348    visible names; or the public types table used to find type definitions.  */
11349
11350 static void
11351 output_pubnames (VEC (pubname_entry, gc) * names)
11352 {
11353   unsigned i;
11354   unsigned long pubnames_length = size_of_pubnames (names);
11355   pubname_ref pub;
11356
11357   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11358     dw2_asm_output_data (4, 0xffffffff,
11359       "Initial length escape value indicating 64-bit DWARF extension");
11360   if (names == pubname_table)
11361     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11362                          "Length of Public Names Info");
11363   else
11364     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11365                          "Length of Public Type Names Info");
11366   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11367   dw2_asm_output_data (2, 2, "DWARF Version");
11368   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11369                          debug_info_section,
11370                          "Offset of Compilation Unit Info");
11371   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11372                        "Compilation Unit Length");
11373
11374   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11375     {
11376       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11377       if (names == pubname_table)
11378         gcc_assert (pub->die->die_mark);
11379
11380       if (names != pubtype_table
11381           || pub->die->die_offset != 0
11382           || !flag_eliminate_unused_debug_types)
11383         {
11384           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11385                                "DIE offset");
11386
11387           dw2_asm_output_nstring (pub->name, -1, "external name");
11388         }
11389     }
11390
11391   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11392 }
11393
11394 /* Add a new entry to .debug_aranges if appropriate.  */
11395
11396 static void
11397 add_arange (tree decl, dw_die_ref die)
11398 {
11399   if (! DECL_SECTION_NAME (decl))
11400     return;
11401
11402   if (arange_table_in_use == arange_table_allocated)
11403     {
11404       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11405       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11406                                     arange_table_allocated);
11407       memset (arange_table + arange_table_in_use, 0,
11408               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11409     }
11410
11411   arange_table[arange_table_in_use++] = die;
11412 }
11413
11414 /* Output the information that goes into the .debug_aranges table.
11415    Namely, define the beginning and ending address range of the
11416    text section generated for this compilation unit.  */
11417
11418 static void
11419 output_aranges (void)
11420 {
11421   unsigned i;
11422   unsigned long aranges_length = size_of_aranges ();
11423
11424   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11425     dw2_asm_output_data (4, 0xffffffff,
11426       "Initial length escape value indicating 64-bit DWARF extension");
11427   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11428                        "Length of Address Ranges Info");
11429   /* Version number for aranges is still 2, even in DWARF3.  */
11430   dw2_asm_output_data (2, 2, "DWARF Version");
11431   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11432                          debug_info_section,
11433                          "Offset of Compilation Unit Info");
11434   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11435   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11436
11437   /* We need to align to twice the pointer size here.  */
11438   if (DWARF_ARANGES_PAD_SIZE)
11439     {
11440       /* Pad using a 2 byte words so that padding is correct for any
11441          pointer size.  */
11442       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11443                            2 * DWARF2_ADDR_SIZE);
11444       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11445         dw2_asm_output_data (2, 0, NULL);
11446     }
11447
11448   /* It is necessary not to output these entries if the sections were
11449      not used; if the sections were not used, the length will be 0 and
11450      the address may end up as 0 if the section is discarded by ld
11451      --gc-sections, leaving an invalid (0, 0) entry that can be
11452      confused with the terminator.  */
11453   if (text_section_used)
11454     {
11455       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11456       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11457                             text_section_label, "Length");
11458     }
11459   if (cold_text_section_used)
11460     {
11461       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11462                            "Address");
11463       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11464                             cold_text_section_label, "Length");
11465     }
11466
11467   for (i = 0; i < arange_table_in_use; i++)
11468     {
11469       dw_die_ref die = arange_table[i];
11470
11471       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11472       gcc_assert (die->die_mark);
11473
11474       if (die->die_tag == DW_TAG_subprogram)
11475         {
11476           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11477                                "Address");
11478           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11479                                 get_AT_low_pc (die), "Length");
11480         }
11481       else
11482         {
11483           /* A static variable; extract the symbol from DW_AT_location.
11484              Note that this code isn't currently hit, as we only emit
11485              aranges for functions (jason 9/23/99).  */
11486           dw_attr_ref a = get_AT (die, DW_AT_location);
11487           dw_loc_descr_ref loc;
11488
11489           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11490
11491           loc = AT_loc (a);
11492           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11493
11494           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11495                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11496           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11497                                get_AT_unsigned (die, DW_AT_byte_size),
11498                                "Length");
11499         }
11500     }
11501
11502   /* Output the terminator words.  */
11503   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11504   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11505 }
11506
11507 /* Add a new entry to .debug_ranges.  Return the offset at which it
11508    was placed.  */
11509
11510 static unsigned int
11511 add_ranges_num (int num)
11512 {
11513   unsigned int in_use = ranges_table_in_use;
11514
11515   if (in_use == ranges_table_allocated)
11516     {
11517       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11518       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11519                                     ranges_table_allocated);
11520       memset (ranges_table + ranges_table_in_use, 0,
11521               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11522     }
11523
11524   ranges_table[in_use].num = num;
11525   ranges_table_in_use = in_use + 1;
11526
11527   return in_use * 2 * DWARF2_ADDR_SIZE;
11528 }
11529
11530 /* Add a new entry to .debug_ranges corresponding to a block, or a
11531    range terminator if BLOCK is NULL.  */
11532
11533 static unsigned int
11534 add_ranges (const_tree block)
11535 {
11536   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11537 }
11538
11539 /* Add a new entry to .debug_ranges corresponding to a pair of
11540    labels.  */
11541
11542 static void
11543 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11544                       bool *added)
11545 {
11546   unsigned int in_use = ranges_by_label_in_use;
11547   unsigned int offset;
11548
11549   if (in_use == ranges_by_label_allocated)
11550     {
11551       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11552       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11553                                        ranges_by_label,
11554                                        ranges_by_label_allocated);
11555       memset (ranges_by_label + ranges_by_label_in_use, 0,
11556               RANGES_TABLE_INCREMENT
11557               * sizeof (struct dw_ranges_by_label_struct));
11558     }
11559
11560   ranges_by_label[in_use].begin = begin;
11561   ranges_by_label[in_use].end = end;
11562   ranges_by_label_in_use = in_use + 1;
11563
11564   offset = add_ranges_num (-(int)in_use - 1);
11565   if (!*added)
11566     {
11567       add_AT_range_list (die, DW_AT_ranges, offset);
11568       *added = true;
11569     }
11570 }
11571
11572 static void
11573 output_ranges (void)
11574 {
11575   unsigned i;
11576   static const char *const start_fmt = "Offset %#x";
11577   const char *fmt = start_fmt;
11578
11579   for (i = 0; i < ranges_table_in_use; i++)
11580     {
11581       int block_num = ranges_table[i].num;
11582
11583       if (block_num > 0)
11584         {
11585           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11586           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11587
11588           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11589           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11590
11591           /* If all code is in the text section, then the compilation
11592              unit base address defaults to DW_AT_low_pc, which is the
11593              base of the text section.  */
11594           if (!have_multiple_function_sections)
11595             {
11596               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11597                                     text_section_label,
11598                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11599               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11600                                     text_section_label, NULL);
11601             }
11602
11603           /* Otherwise, the compilation unit base address is zero,
11604              which allows us to use absolute addresses, and not worry
11605              about whether the target supports cross-section
11606              arithmetic.  */
11607           else
11608             {
11609               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11610                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11611               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11612             }
11613
11614           fmt = NULL;
11615         }
11616
11617       /* Negative block_num stands for an index into ranges_by_label.  */
11618       else if (block_num < 0)
11619         {
11620           int lab_idx = - block_num - 1;
11621
11622           if (!have_multiple_function_sections)
11623             {
11624               gcc_unreachable ();
11625 #if 0
11626               /* If we ever use add_ranges_by_labels () for a single
11627                  function section, all we have to do is to take out
11628                  the #if 0 above.  */
11629               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11630                                     ranges_by_label[lab_idx].begin,
11631                                     text_section_label,
11632                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11633               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11634                                     ranges_by_label[lab_idx].end,
11635                                     text_section_label, NULL);
11636 #endif
11637             }
11638           else
11639             {
11640               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11641                                    ranges_by_label[lab_idx].begin,
11642                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11643               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11644                                    ranges_by_label[lab_idx].end,
11645                                    NULL);
11646             }
11647         }
11648       else
11649         {
11650           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11651           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11652           fmt = start_fmt;
11653         }
11654     }
11655 }
11656
11657 /* Data structure containing information about input files.  */
11658 struct file_info
11659 {
11660   const char *path;     /* Complete file name.  */
11661   const char *fname;    /* File name part.  */
11662   int length;           /* Length of entire string.  */
11663   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11664   int dir_idx;          /* Index in directory table.  */
11665 };
11666
11667 /* Data structure containing information about directories with source
11668    files.  */
11669 struct dir_info
11670 {
11671   const char *path;     /* Path including directory name.  */
11672   int length;           /* Path length.  */
11673   int prefix;           /* Index of directory entry which is a prefix.  */
11674   int count;            /* Number of files in this directory.  */
11675   int dir_idx;          /* Index of directory used as base.  */
11676 };
11677
11678 /* Callback function for file_info comparison.  We sort by looking at
11679    the directories in the path.  */
11680
11681 static int
11682 file_info_cmp (const void *p1, const void *p2)
11683 {
11684   const struct file_info *const s1 = (const struct file_info *) p1;
11685   const struct file_info *const s2 = (const struct file_info *) p2;
11686   const unsigned char *cp1;
11687   const unsigned char *cp2;
11688
11689   /* Take care of file names without directories.  We need to make sure that
11690      we return consistent values to qsort since some will get confused if
11691      we return the same value when identical operands are passed in opposite
11692      orders.  So if neither has a directory, return 0 and otherwise return
11693      1 or -1 depending on which one has the directory.  */
11694   if ((s1->path == s1->fname || s2->path == s2->fname))
11695     return (s2->path == s2->fname) - (s1->path == s1->fname);
11696
11697   cp1 = (const unsigned char *) s1->path;
11698   cp2 = (const unsigned char *) s2->path;
11699
11700   while (1)
11701     {
11702       ++cp1;
11703       ++cp2;
11704       /* Reached the end of the first path?  If so, handle like above.  */
11705       if ((cp1 == (const unsigned char *) s1->fname)
11706           || (cp2 == (const unsigned char *) s2->fname))
11707         return ((cp2 == (const unsigned char *) s2->fname)
11708                 - (cp1 == (const unsigned char *) s1->fname));
11709
11710       /* Character of current path component the same?  */
11711       else if (*cp1 != *cp2)
11712         return *cp1 - *cp2;
11713     }
11714 }
11715
11716 struct file_name_acquire_data
11717 {
11718   struct file_info *files;
11719   int used_files;
11720   int max_files;
11721 };
11722
11723 /* Traversal function for the hash table.  */
11724
11725 static int
11726 file_name_acquire (void ** slot, void *data)
11727 {
11728   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11729   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11730   struct file_info *fi;
11731   const char *f;
11732
11733   gcc_assert (fnad->max_files >= d->emitted_number);
11734
11735   if (! d->emitted_number)
11736     return 1;
11737
11738   gcc_assert (fnad->max_files != fnad->used_files);
11739
11740   fi = fnad->files + fnad->used_files++;
11741
11742   /* Skip all leading "./".  */
11743   f = d->filename;
11744   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11745     f += 2;
11746
11747   /* Create a new array entry.  */
11748   fi->path = f;
11749   fi->length = strlen (f);
11750   fi->file_idx = d;
11751
11752   /* Search for the file name part.  */
11753   f = strrchr (f, DIR_SEPARATOR);
11754 #if defined (DIR_SEPARATOR_2)
11755   {
11756     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11757
11758     if (g != NULL)
11759       {
11760         if (f == NULL || f < g)
11761           f = g;
11762       }
11763   }
11764 #endif
11765
11766   fi->fname = f == NULL ? fi->path : f + 1;
11767   return 1;
11768 }
11769
11770 /* Output the directory table and the file name table.  We try to minimize
11771    the total amount of memory needed.  A heuristic is used to avoid large
11772    slowdowns with many input files.  */
11773
11774 static void
11775 output_file_names (void)
11776 {
11777   struct file_name_acquire_data fnad;
11778   int numfiles;
11779   struct file_info *files;
11780   struct dir_info *dirs;
11781   int *saved;
11782   int *savehere;
11783   int *backmap;
11784   int ndirs;
11785   int idx_offset;
11786   int i;
11787
11788   if (!last_emitted_file)
11789     {
11790       dw2_asm_output_data (1, 0, "End directory table");
11791       dw2_asm_output_data (1, 0, "End file name table");
11792       return;
11793     }
11794
11795   numfiles = last_emitted_file->emitted_number;
11796
11797   /* Allocate the various arrays we need.  */
11798   files = XALLOCAVEC (struct file_info, numfiles);
11799   dirs = XALLOCAVEC (struct dir_info, numfiles);
11800
11801   fnad.files = files;
11802   fnad.used_files = 0;
11803   fnad.max_files = numfiles;
11804   htab_traverse (file_table, file_name_acquire, &fnad);
11805   gcc_assert (fnad.used_files == fnad.max_files);
11806
11807   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11808
11809   /* Find all the different directories used.  */
11810   dirs[0].path = files[0].path;
11811   dirs[0].length = files[0].fname - files[0].path;
11812   dirs[0].prefix = -1;
11813   dirs[0].count = 1;
11814   dirs[0].dir_idx = 0;
11815   files[0].dir_idx = 0;
11816   ndirs = 1;
11817
11818   for (i = 1; i < numfiles; i++)
11819     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11820         && memcmp (dirs[ndirs - 1].path, files[i].path,
11821                    dirs[ndirs - 1].length) == 0)
11822       {
11823         /* Same directory as last entry.  */
11824         files[i].dir_idx = ndirs - 1;
11825         ++dirs[ndirs - 1].count;
11826       }
11827     else
11828       {
11829         int j;
11830
11831         /* This is a new directory.  */
11832         dirs[ndirs].path = files[i].path;
11833         dirs[ndirs].length = files[i].fname - files[i].path;
11834         dirs[ndirs].count = 1;
11835         dirs[ndirs].dir_idx = ndirs;
11836         files[i].dir_idx = ndirs;
11837
11838         /* Search for a prefix.  */
11839         dirs[ndirs].prefix = -1;
11840         for (j = 0; j < ndirs; j++)
11841           if (dirs[j].length < dirs[ndirs].length
11842               && dirs[j].length > 1
11843               && (dirs[ndirs].prefix == -1
11844                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11845               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11846             dirs[ndirs].prefix = j;
11847
11848         ++ndirs;
11849       }
11850
11851   /* Now to the actual work.  We have to find a subset of the directories which
11852      allow expressing the file name using references to the directory table
11853      with the least amount of characters.  We do not do an exhaustive search
11854      where we would have to check out every combination of every single
11855      possible prefix.  Instead we use a heuristic which provides nearly optimal
11856      results in most cases and never is much off.  */
11857   saved = XALLOCAVEC (int, ndirs);
11858   savehere = XALLOCAVEC (int, ndirs);
11859
11860   memset (saved, '\0', ndirs * sizeof (saved[0]));
11861   for (i = 0; i < ndirs; i++)
11862     {
11863       int j;
11864       int total;
11865
11866       /* We can always save some space for the current directory.  But this
11867          does not mean it will be enough to justify adding the directory.  */
11868       savehere[i] = dirs[i].length;
11869       total = (savehere[i] - saved[i]) * dirs[i].count;
11870
11871       for (j = i + 1; j < ndirs; j++)
11872         {
11873           savehere[j] = 0;
11874           if (saved[j] < dirs[i].length)
11875             {
11876               /* Determine whether the dirs[i] path is a prefix of the
11877                  dirs[j] path.  */
11878               int k;
11879
11880               k = dirs[j].prefix;
11881               while (k != -1 && k != (int) i)
11882                 k = dirs[k].prefix;
11883
11884               if (k == (int) i)
11885                 {
11886                   /* Yes it is.  We can possibly save some memory by
11887                      writing the filenames in dirs[j] relative to
11888                      dirs[i].  */
11889                   savehere[j] = dirs[i].length;
11890                   total += (savehere[j] - saved[j]) * dirs[j].count;
11891                 }
11892             }
11893         }
11894
11895       /* Check whether we can save enough to justify adding the dirs[i]
11896          directory.  */
11897       if (total > dirs[i].length + 1)
11898         {
11899           /* It's worthwhile adding.  */
11900           for (j = i; j < ndirs; j++)
11901             if (savehere[j] > 0)
11902               {
11903                 /* Remember how much we saved for this directory so far.  */
11904                 saved[j] = savehere[j];
11905
11906                 /* Remember the prefix directory.  */
11907                 dirs[j].dir_idx = i;
11908               }
11909         }
11910     }
11911
11912   /* Emit the directory name table.  */
11913   idx_offset = dirs[0].length > 0 ? 1 : 0;
11914   for (i = 1 - idx_offset; i < ndirs; i++)
11915     dw2_asm_output_nstring (dirs[i].path,
11916                             dirs[i].length
11917                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11918                             "Directory Entry: %#x", i + idx_offset);
11919
11920   dw2_asm_output_data (1, 0, "End directory table");
11921
11922   /* We have to emit them in the order of emitted_number since that's
11923      used in the debug info generation.  To do this efficiently we
11924      generate a back-mapping of the indices first.  */
11925   backmap = XALLOCAVEC (int, numfiles);
11926   for (i = 0; i < numfiles; i++)
11927     backmap[files[i].file_idx->emitted_number - 1] = i;
11928
11929   /* Now write all the file names.  */
11930   for (i = 0; i < numfiles; i++)
11931     {
11932       int file_idx = backmap[i];
11933       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11934
11935 #ifdef VMS_DEBUGGING_INFO
11936 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11937
11938       /* Setting these fields can lead to debugger miscomparisons,
11939          but VMS Debug requires them to be set correctly.  */
11940
11941       int ver;
11942       long long cdt;
11943       long siz;
11944       int maxfilelen = strlen (files[file_idx].path)
11945                                + dirs[dir_idx].length
11946                                + MAX_VMS_VERSION_LEN + 1;
11947       char *filebuf = XALLOCAVEC (char, maxfilelen);
11948
11949       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11950       snprintf (filebuf, maxfilelen, "%s;%d",
11951                 files[file_idx].path + dirs[dir_idx].length, ver);
11952
11953       dw2_asm_output_nstring
11954         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11955
11956       /* Include directory index.  */
11957       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11958
11959       /* Modification time.  */
11960       dw2_asm_output_data_uleb128
11961         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11962           ? cdt : 0,
11963          NULL);
11964
11965       /* File length in bytes.  */
11966       dw2_asm_output_data_uleb128
11967         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11968           ? siz : 0,
11969          NULL);
11970 #else
11971       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11972                               "File Entry: %#x", (unsigned) i + 1);
11973
11974       /* Include directory index.  */
11975       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11976
11977       /* Modification time.  */
11978       dw2_asm_output_data_uleb128 (0, NULL);
11979
11980       /* File length in bytes.  */
11981       dw2_asm_output_data_uleb128 (0, NULL);
11982 #endif /* VMS_DEBUGGING_INFO */
11983     }
11984
11985   dw2_asm_output_data (1, 0, "End file name table");
11986 }
11987
11988
11989 /* Output the source line number correspondence information.  This
11990    information goes into the .debug_line section.  */
11991
11992 static void
11993 output_line_info (void)
11994 {
11995   char l1[20], l2[20], p1[20], p2[20];
11996   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11997   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11998   unsigned opc;
11999   unsigned n_op_args;
12000   unsigned long lt_index;
12001   unsigned long current_line;
12002   long line_offset;
12003   long line_delta;
12004   unsigned long current_file;
12005   unsigned long function;
12006   int ver = dwarf_version;
12007
12008   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12009   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12010   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12011   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12012
12013   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12014     dw2_asm_output_data (4, 0xffffffff,
12015       "Initial length escape value indicating 64-bit DWARF extension");
12016   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12017                         "Length of Source Line Info");
12018   ASM_OUTPUT_LABEL (asm_out_file, l1);
12019
12020   dw2_asm_output_data (2, ver, "DWARF Version");
12021   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12022   ASM_OUTPUT_LABEL (asm_out_file, p1);
12023
12024   /* Define the architecture-dependent minimum instruction length (in
12025    bytes).  In this implementation of DWARF, this field is used for
12026    information purposes only.  Since GCC generates assembly language,
12027    we have no a priori knowledge of how many instruction bytes are
12028    generated for each source line, and therefore can use only the
12029    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12030    commands.  Accordingly, we fix this as `1', which is "correct
12031    enough" for all architectures, and don't let the target override.  */
12032   dw2_asm_output_data (1, 1,
12033                        "Minimum Instruction Length");
12034
12035   if (ver >= 4)
12036     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12037                          "Maximum Operations Per Instruction");
12038   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12039                        "Default is_stmt_start flag");
12040   dw2_asm_output_data (1, DWARF_LINE_BASE,
12041                        "Line Base Value (Special Opcodes)");
12042   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12043                        "Line Range Value (Special Opcodes)");
12044   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12045                        "Special Opcode Base");
12046
12047   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12048     {
12049       switch (opc)
12050         {
12051         case DW_LNS_advance_pc:
12052         case DW_LNS_advance_line:
12053         case DW_LNS_set_file:
12054         case DW_LNS_set_column:
12055         case DW_LNS_fixed_advance_pc:
12056           n_op_args = 1;
12057           break;
12058         default:
12059           n_op_args = 0;
12060           break;
12061         }
12062
12063       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12064                            opc, n_op_args);
12065     }
12066
12067   /* Write out the information about the files we use.  */
12068   output_file_names ();
12069   ASM_OUTPUT_LABEL (asm_out_file, p2);
12070
12071   /* We used to set the address register to the first location in the text
12072      section here, but that didn't accomplish anything since we already
12073      have a line note for the opening brace of the first function.  */
12074
12075   /* Generate the line number to PC correspondence table, encoded as
12076      a series of state machine operations.  */
12077   current_file = 1;
12078   current_line = 1;
12079
12080   if (cfun && in_cold_section_p)
12081     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12082   else
12083     strcpy (prev_line_label, text_section_label);
12084   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12085     {
12086       dw_line_info_ref line_info = &line_info_table[lt_index];
12087
12088 #if 0
12089       /* Disable this optimization for now; GDB wants to see two line notes
12090          at the beginning of a function so it can find the end of the
12091          prologue.  */
12092
12093       /* Don't emit anything for redundant notes.  Just updating the
12094          address doesn't accomplish anything, because we already assume
12095          that anything after the last address is this line.  */
12096       if (line_info->dw_line_num == current_line
12097           && line_info->dw_file_num == current_file)
12098         continue;
12099 #endif
12100
12101       /* Emit debug info for the address of the current line.
12102
12103          Unfortunately, we have little choice here currently, and must always
12104          use the most general form.  GCC does not know the address delta
12105          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12106          attributes which will give an upper bound on the address range.  We
12107          could perhaps use length attributes to determine when it is safe to
12108          use DW_LNS_fixed_advance_pc.  */
12109
12110       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12111       if (0)
12112         {
12113           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12114           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12115                                "DW_LNS_fixed_advance_pc");
12116           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12117         }
12118       else
12119         {
12120           /* This can handle any delta.  This takes
12121              4+DWARF2_ADDR_SIZE bytes.  */
12122           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12123           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12124           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12125           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12126         }
12127
12128       strcpy (prev_line_label, line_label);
12129
12130       /* Emit debug info for the source file of the current line, if
12131          different from the previous line.  */
12132       if (line_info->dw_file_num != current_file)
12133         {
12134           current_file = line_info->dw_file_num;
12135           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12136           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12137         }
12138
12139       /* Emit debug info for the current line number, choosing the encoding
12140          that uses the least amount of space.  */
12141       if (line_info->dw_line_num != current_line)
12142         {
12143           line_offset = line_info->dw_line_num - current_line;
12144           line_delta = line_offset - DWARF_LINE_BASE;
12145           current_line = line_info->dw_line_num;
12146           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12147             /* This can handle deltas from -10 to 234, using the current
12148                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12149                takes 1 byte.  */
12150             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12151                                  "line %lu", current_line);
12152           else
12153             {
12154               /* This can handle any delta.  This takes at least 4 bytes,
12155                  depending on the value being encoded.  */
12156               dw2_asm_output_data (1, DW_LNS_advance_line,
12157                                    "advance to line %lu", current_line);
12158               dw2_asm_output_data_sleb128 (line_offset, NULL);
12159               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12160             }
12161         }
12162       else
12163         /* We still need to start a new row, so output a copy insn.  */
12164         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12165     }
12166
12167   /* Emit debug info for the address of the end of the function.  */
12168   if (0)
12169     {
12170       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12171                            "DW_LNS_fixed_advance_pc");
12172       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12173     }
12174   else
12175     {
12176       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12177       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12178       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12179       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12180     }
12181
12182   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12183   dw2_asm_output_data_uleb128 (1, NULL);
12184   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12185
12186   function = 0;
12187   current_file = 1;
12188   current_line = 1;
12189   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12190     {
12191       dw_separate_line_info_ref line_info
12192         = &separate_line_info_table[lt_index];
12193
12194 #if 0
12195       /* Don't emit anything for redundant notes.  */
12196       if (line_info->dw_line_num == current_line
12197           && line_info->dw_file_num == current_file
12198           && line_info->function == function)
12199         goto cont;
12200 #endif
12201
12202       /* Emit debug info for the address of the current line.  If this is
12203          a new function, or the first line of a function, then we need
12204          to handle it differently.  */
12205       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12206                                    lt_index);
12207       if (function != line_info->function)
12208         {
12209           function = line_info->function;
12210
12211           /* Set the address register to the first line in the function.  */
12212           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12213           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12214           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12215           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12216         }
12217       else
12218         {
12219           /* ??? See the DW_LNS_advance_pc comment above.  */
12220           if (0)
12221             {
12222               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12223                                    "DW_LNS_fixed_advance_pc");
12224               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12225             }
12226           else
12227             {
12228               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12229               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12230               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12231               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12232             }
12233         }
12234
12235       strcpy (prev_line_label, line_label);
12236
12237       /* Emit debug info for the source file of the current line, if
12238          different from the previous line.  */
12239       if (line_info->dw_file_num != current_file)
12240         {
12241           current_file = line_info->dw_file_num;
12242           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12243           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12244         }
12245
12246       /* Emit debug info for the current line number, choosing the encoding
12247          that uses the least amount of space.  */
12248       if (line_info->dw_line_num != current_line)
12249         {
12250           line_offset = line_info->dw_line_num - current_line;
12251           line_delta = line_offset - DWARF_LINE_BASE;
12252           current_line = line_info->dw_line_num;
12253           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12254             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12255                                  "line %lu", current_line);
12256           else
12257             {
12258               dw2_asm_output_data (1, DW_LNS_advance_line,
12259                                    "advance to line %lu", current_line);
12260               dw2_asm_output_data_sleb128 (line_offset, NULL);
12261               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12262             }
12263         }
12264       else
12265         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12266
12267 #if 0
12268     cont:
12269 #endif
12270
12271       lt_index++;
12272
12273       /* If we're done with a function, end its sequence.  */
12274       if (lt_index == separate_line_info_table_in_use
12275           || separate_line_info_table[lt_index].function != function)
12276         {
12277           current_file = 1;
12278           current_line = 1;
12279
12280           /* Emit debug info for the address of the end of the function.  */
12281           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12282           if (0)
12283             {
12284               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12285                                    "DW_LNS_fixed_advance_pc");
12286               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12287             }
12288           else
12289             {
12290               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12291               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12292               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12293               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12294             }
12295
12296           /* Output the marker for the end of this sequence.  */
12297           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12298           dw2_asm_output_data_uleb128 (1, NULL);
12299           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12300         }
12301     }
12302
12303   /* Output the marker for the end of the line number info.  */
12304   ASM_OUTPUT_LABEL (asm_out_file, l2);
12305 }
12306
12307 /* Return the size of the .debug_dcall table for the compilation unit.  */
12308
12309 static unsigned long
12310 size_of_dcall_table (void)
12311 {
12312   unsigned long size;
12313   unsigned int i;
12314   dcall_entry *p;
12315   tree last_poc_decl = NULL;
12316
12317   /* Header:  version + debug info section pointer + pointer size.  */
12318   size = 2 + DWARF_OFFSET_SIZE + 1;
12319
12320   /* Each entry:  code label + DIE offset.  */
12321   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12322     {
12323       gcc_assert (p->targ_die != NULL);
12324       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12325       if (p->poc_decl != last_poc_decl)
12326         {
12327           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12328           gcc_assert (poc_die);
12329           last_poc_decl = p->poc_decl;
12330           if (poc_die)
12331             size += (DWARF_OFFSET_SIZE
12332                      + size_of_uleb128 (poc_die->die_offset));
12333         }
12334       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12335     }
12336
12337   return size;
12338 }
12339
12340 /* Output the direct call table used to disambiguate PC values when
12341    identical function have been merged.  */
12342
12343 static void
12344 output_dcall_table (void)
12345 {
12346   unsigned i;
12347   unsigned long dcall_length = size_of_dcall_table ();
12348   dcall_entry *p;
12349   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12350   tree last_poc_decl = NULL;
12351
12352   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12353     dw2_asm_output_data (4, 0xffffffff,
12354       "Initial length escape value indicating 64-bit DWARF extension");
12355   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12356                        "Length of Direct Call Table");
12357   dw2_asm_output_data (2, 4, "Version number");
12358   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12359                          debug_info_section,
12360                          "Offset of Compilation Unit Info");
12361   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12362
12363   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12364     {
12365       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12366       if (p->poc_decl != last_poc_decl)
12367         {
12368           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12369           last_poc_decl = p->poc_decl;
12370           if (poc_die)
12371             {
12372               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12373               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12374                                            "Caller DIE offset");
12375             }
12376         }
12377       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12378       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12379       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12380                                    "Callee DIE offset");
12381     }
12382 }
12383 \f
12384 /* Return the size of the .debug_vcall table for the compilation unit.  */
12385
12386 static unsigned long
12387 size_of_vcall_table (void)
12388 {
12389   unsigned long size;
12390   unsigned int i;
12391   vcall_entry *p;
12392
12393   /* Header:  version + pointer size.  */
12394   size = 2 + 1;
12395
12396   /* Each entry:  code label + vtable slot index.  */
12397   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12398     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12399
12400   return size;
12401 }
12402
12403 /* Output the virtual call table used to disambiguate PC values when
12404    identical function have been merged.  */
12405
12406 static void
12407 output_vcall_table (void)
12408 {
12409   unsigned i;
12410   unsigned long vcall_length = size_of_vcall_table ();
12411   vcall_entry *p;
12412   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12413
12414   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12415     dw2_asm_output_data (4, 0xffffffff,
12416       "Initial length escape value indicating 64-bit DWARF extension");
12417   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12418                        "Length of Virtual Call Table");
12419   dw2_asm_output_data (2, 4, "Version number");
12420   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12421
12422   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12423     {
12424       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12425       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12426       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12427     }
12428 }
12429 \f
12430 /* Given a pointer to a tree node for some base type, return a pointer to
12431    a DIE that describes the given type.
12432
12433    This routine must only be called for GCC type nodes that correspond to
12434    Dwarf base (fundamental) types.  */
12435
12436 static dw_die_ref
12437 base_type_die (tree type)
12438 {
12439   dw_die_ref base_type_result;
12440   enum dwarf_type encoding;
12441
12442   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12443     return 0;
12444
12445   /* If this is a subtype that should not be emitted as a subrange type,
12446      use the base type.  See subrange_type_for_debug_p.  */
12447   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12448     type = TREE_TYPE (type);
12449
12450   switch (TREE_CODE (type))
12451     {
12452     case INTEGER_TYPE:
12453       if ((dwarf_version >= 4 || !dwarf_strict)
12454           && TYPE_NAME (type)
12455           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12456           && DECL_IS_BUILTIN (TYPE_NAME (type))
12457           && DECL_NAME (TYPE_NAME (type)))
12458         {
12459           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12460           if (strcmp (name, "char16_t") == 0
12461               || strcmp (name, "char32_t") == 0)
12462             {
12463               encoding = DW_ATE_UTF;
12464               break;
12465             }
12466         }
12467       if (TYPE_STRING_FLAG (type))
12468         {
12469           if (TYPE_UNSIGNED (type))
12470             encoding = DW_ATE_unsigned_char;
12471           else
12472             encoding = DW_ATE_signed_char;
12473         }
12474       else if (TYPE_UNSIGNED (type))
12475         encoding = DW_ATE_unsigned;
12476       else
12477         encoding = DW_ATE_signed;
12478       break;
12479
12480     case REAL_TYPE:
12481       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12482         {
12483           if (dwarf_version >= 3 || !dwarf_strict)
12484             encoding = DW_ATE_decimal_float;
12485           else
12486             encoding = DW_ATE_lo_user;
12487         }
12488       else
12489         encoding = DW_ATE_float;
12490       break;
12491
12492     case FIXED_POINT_TYPE:
12493       if (!(dwarf_version >= 3 || !dwarf_strict))
12494         encoding = DW_ATE_lo_user;
12495       else if (TYPE_UNSIGNED (type))
12496         encoding = DW_ATE_unsigned_fixed;
12497       else
12498         encoding = DW_ATE_signed_fixed;
12499       break;
12500
12501       /* Dwarf2 doesn't know anything about complex ints, so use
12502          a user defined type for it.  */
12503     case COMPLEX_TYPE:
12504       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12505         encoding = DW_ATE_complex_float;
12506       else
12507         encoding = DW_ATE_lo_user;
12508       break;
12509
12510     case BOOLEAN_TYPE:
12511       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12512       encoding = DW_ATE_boolean;
12513       break;
12514
12515     default:
12516       /* No other TREE_CODEs are Dwarf fundamental types.  */
12517       gcc_unreachable ();
12518     }
12519
12520   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12521
12522   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12523                    int_size_in_bytes (type));
12524   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12525
12526   return base_type_result;
12527 }
12528
12529 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12530    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12531
12532 static inline int
12533 is_base_type (tree type)
12534 {
12535   switch (TREE_CODE (type))
12536     {
12537     case ERROR_MARK:
12538     case VOID_TYPE:
12539     case INTEGER_TYPE:
12540     case REAL_TYPE:
12541     case FIXED_POINT_TYPE:
12542     case COMPLEX_TYPE:
12543     case BOOLEAN_TYPE:
12544       return 1;
12545
12546     case ARRAY_TYPE:
12547     case RECORD_TYPE:
12548     case UNION_TYPE:
12549     case QUAL_UNION_TYPE:
12550     case ENUMERAL_TYPE:
12551     case FUNCTION_TYPE:
12552     case METHOD_TYPE:
12553     case POINTER_TYPE:
12554     case REFERENCE_TYPE:
12555     case OFFSET_TYPE:
12556     case LANG_TYPE:
12557     case VECTOR_TYPE:
12558       return 0;
12559
12560     default:
12561       gcc_unreachable ();
12562     }
12563
12564   return 0;
12565 }
12566
12567 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12568    node, return the size in bits for the type if it is a constant, or else
12569    return the alignment for the type if the type's size is not constant, or
12570    else return BITS_PER_WORD if the type actually turns out to be an
12571    ERROR_MARK node.  */
12572
12573 static inline unsigned HOST_WIDE_INT
12574 simple_type_size_in_bits (const_tree type)
12575 {
12576   if (TREE_CODE (type) == ERROR_MARK)
12577     return BITS_PER_WORD;
12578   else if (TYPE_SIZE (type) == NULL_TREE)
12579     return 0;
12580   else if (host_integerp (TYPE_SIZE (type), 1))
12581     return tree_low_cst (TYPE_SIZE (type), 1);
12582   else
12583     return TYPE_ALIGN (type);
12584 }
12585
12586 /* Similarly, but return a double_int instead of UHWI.  */
12587
12588 static inline double_int
12589 double_int_type_size_in_bits (const_tree type)
12590 {
12591   if (TREE_CODE (type) == ERROR_MARK)
12592     return uhwi_to_double_int (BITS_PER_WORD);
12593   else if (TYPE_SIZE (type) == NULL_TREE)
12594     return double_int_zero;
12595   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12596     return tree_to_double_int (TYPE_SIZE (type));
12597   else
12598     return uhwi_to_double_int (TYPE_ALIGN (type));
12599 }
12600
12601 /*  Given a pointer to a tree node for a subrange type, return a pointer
12602     to a DIE that describes the given type.  */
12603
12604 static dw_die_ref
12605 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12606 {
12607   dw_die_ref subrange_die;
12608   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12609
12610   if (context_die == NULL)
12611     context_die = comp_unit_die;
12612
12613   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12614
12615   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12616     {
12617       /* The size of the subrange type and its base type do not match,
12618          so we need to generate a size attribute for the subrange type.  */
12619       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12620     }
12621
12622   if (low)
12623     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12624   if (high)
12625     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12626
12627   return subrange_die;
12628 }
12629
12630 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12631    entry that chains various modifiers in front of the given type.  */
12632
12633 static dw_die_ref
12634 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12635                    dw_die_ref context_die)
12636 {
12637   enum tree_code code = TREE_CODE (type);
12638   dw_die_ref mod_type_die;
12639   dw_die_ref sub_die = NULL;
12640   tree item_type = NULL;
12641   tree qualified_type;
12642   tree name, low, high;
12643
12644   if (code == ERROR_MARK)
12645     return NULL;
12646
12647   /* See if we already have the appropriately qualified variant of
12648      this type.  */
12649   qualified_type
12650     = get_qualified_type (type,
12651                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12652                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12653
12654   if (qualified_type == sizetype
12655       && TYPE_NAME (qualified_type)
12656       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12657     {
12658 #ifdef ENABLE_CHECKING
12659       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12660                   == INTEGER_TYPE
12661                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12662                      == TYPE_PRECISION (qualified_type)
12663                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12664                      == TYPE_UNSIGNED (qualified_type));
12665 #endif
12666       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12667     }
12668
12669   /* If we do, then we can just use its DIE, if it exists.  */
12670   if (qualified_type)
12671     {
12672       mod_type_die = lookup_type_die (qualified_type);
12673       if (mod_type_die)
12674         return mod_type_die;
12675     }
12676
12677   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12678
12679   /* Handle C typedef types.  */
12680   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12681       && !DECL_ARTIFICIAL (name))
12682     {
12683       tree dtype = TREE_TYPE (name);
12684
12685       if (qualified_type == dtype)
12686         {
12687           /* For a named type, use the typedef.  */
12688           gen_type_die (qualified_type, context_die);
12689           return lookup_type_die (qualified_type);
12690         }
12691       else if (is_const_type < TYPE_READONLY (dtype)
12692                || is_volatile_type < TYPE_VOLATILE (dtype)
12693                || (is_const_type <= TYPE_READONLY (dtype)
12694                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12695                    && DECL_ORIGINAL_TYPE (name) != type))
12696         /* cv-unqualified version of named type.  Just use the unnamed
12697            type to which it refers.  */
12698         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12699                                   is_const_type, is_volatile_type,
12700                                   context_die);
12701       /* Else cv-qualified version of named type; fall through.  */
12702     }
12703
12704   if (is_const_type)
12705     {
12706       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12707       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12708     }
12709   else if (is_volatile_type)
12710     {
12711       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12712       sub_die = modified_type_die (type, 0, 0, context_die);
12713     }
12714   else if (code == POINTER_TYPE)
12715     {
12716       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12717       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12718                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12719       item_type = TREE_TYPE (type);
12720       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12721         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12722                          TYPE_ADDR_SPACE (item_type));
12723     }
12724   else if (code == REFERENCE_TYPE)
12725     {
12726       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12727         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12728                                 type);
12729       else
12730         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12731       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12732                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12733       item_type = TREE_TYPE (type);
12734       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12735         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12736                          TYPE_ADDR_SPACE (item_type));
12737     }
12738   else if (code == INTEGER_TYPE
12739            && TREE_TYPE (type) != NULL_TREE
12740            && subrange_type_for_debug_p (type, &low, &high))
12741     {
12742       mod_type_die = subrange_type_die (type, low, high, context_die);
12743       item_type = TREE_TYPE (type);
12744     }
12745   else if (is_base_type (type))
12746     mod_type_die = base_type_die (type);
12747   else
12748     {
12749       gen_type_die (type, context_die);
12750
12751       /* We have to get the type_main_variant here (and pass that to the
12752          `lookup_type_die' routine) because the ..._TYPE node we have
12753          might simply be a *copy* of some original type node (where the
12754          copy was created to help us keep track of typedef names) and
12755          that copy might have a different TYPE_UID from the original
12756          ..._TYPE node.  */
12757       if (TREE_CODE (type) != VECTOR_TYPE)
12758         return lookup_type_die (type_main_variant (type));
12759       else
12760         /* Vectors have the debugging information in the type,
12761            not the main variant.  */
12762         return lookup_type_die (type);
12763     }
12764
12765   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12766      don't output a DW_TAG_typedef, since there isn't one in the
12767      user's program; just attach a DW_AT_name to the type.
12768      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12769      if the base type already has the same name.  */
12770   if (name
12771       && ((TREE_CODE (name) != TYPE_DECL
12772            && (qualified_type == TYPE_MAIN_VARIANT (type)
12773                || (!is_const_type && !is_volatile_type)))
12774           || (TREE_CODE (name) == TYPE_DECL
12775               && TREE_TYPE (name) == qualified_type
12776               && DECL_NAME (name))))
12777     {
12778       if (TREE_CODE (name) == TYPE_DECL)
12779         /* Could just call add_name_and_src_coords_attributes here,
12780            but since this is a builtin type it doesn't have any
12781            useful source coordinates anyway.  */
12782         name = DECL_NAME (name);
12783       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12784     }
12785   /* This probably indicates a bug.  */
12786   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12787     add_name_attribute (mod_type_die, "__unknown__");
12788
12789   if (qualified_type)
12790     equate_type_number_to_die (qualified_type, mod_type_die);
12791
12792   if (item_type)
12793     /* We must do this after the equate_type_number_to_die call, in case
12794        this is a recursive type.  This ensures that the modified_type_die
12795        recursion will terminate even if the type is recursive.  Recursive
12796        types are possible in Ada.  */
12797     sub_die = modified_type_die (item_type,
12798                                  TYPE_READONLY (item_type),
12799                                  TYPE_VOLATILE (item_type),
12800                                  context_die);
12801
12802   if (sub_die != NULL)
12803     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12804
12805   return mod_type_die;
12806 }
12807
12808 /* Generate DIEs for the generic parameters of T.
12809    T must be either a generic type or a generic function.
12810    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12811
12812 static void
12813 gen_generic_params_dies (tree t)
12814 {
12815   tree parms, args;
12816   int parms_num, i;
12817   dw_die_ref die = NULL;
12818
12819   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12820     return;
12821
12822   if (TYPE_P (t))
12823     die = lookup_type_die (t);
12824   else if (DECL_P (t))
12825     die = lookup_decl_die (t);
12826
12827   gcc_assert (die);
12828
12829   parms = lang_hooks.get_innermost_generic_parms (t);
12830   if (!parms)
12831     /* T has no generic parameter. It means T is neither a generic type
12832        or function. End of story.  */
12833     return;
12834
12835   parms_num = TREE_VEC_LENGTH (parms);
12836   args = lang_hooks.get_innermost_generic_args (t);
12837   for (i = 0; i < parms_num; i++)
12838     {
12839       tree parm, arg, arg_pack_elems;
12840
12841       parm = TREE_VEC_ELT (parms, i);
12842       arg = TREE_VEC_ELT (args, i);
12843       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12844       gcc_assert (parm && TREE_VALUE (parm) && arg);
12845
12846       if (parm && TREE_VALUE (parm) && arg)
12847         {
12848           /* If PARM represents a template parameter pack,
12849              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12850              by DW_TAG_template_*_parameter DIEs for the argument
12851              pack elements of ARG. Note that ARG would then be
12852              an argument pack.  */
12853           if (arg_pack_elems)
12854             template_parameter_pack_die (TREE_VALUE (parm),
12855                                          arg_pack_elems,
12856                                          die);
12857           else
12858             generic_parameter_die (TREE_VALUE (parm), arg,
12859                                    true /* Emit DW_AT_name */, die);
12860         }
12861     }
12862 }
12863
12864 /* Create and return a DIE for PARM which should be
12865    the representation of a generic type parameter.
12866    For instance, in the C++ front end, PARM would be a template parameter.
12867    ARG is the argument to PARM.
12868    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12869    name of the PARM.
12870    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12871    as a child node.  */
12872
12873 static dw_die_ref
12874 generic_parameter_die (tree parm, tree arg,
12875                        bool emit_name_p,
12876                        dw_die_ref parent_die)
12877 {
12878   dw_die_ref tmpl_die = NULL;
12879   const char *name = NULL;
12880
12881   if (!parm || !DECL_NAME (parm) || !arg)
12882     return NULL;
12883
12884   /* We support non-type generic parameters and arguments,
12885      type generic parameters and arguments, as well as
12886      generic generic parameters (a.k.a. template template parameters in C++)
12887      and arguments.  */
12888   if (TREE_CODE (parm) == PARM_DECL)
12889     /* PARM is a nontype generic parameter  */
12890     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12891   else if (TREE_CODE (parm) == TYPE_DECL)
12892     /* PARM is a type generic parameter.  */
12893     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12894   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12895     /* PARM is a generic generic parameter.
12896        Its DIE is a GNU extension. It shall have a
12897        DW_AT_name attribute to represent the name of the template template
12898        parameter, and a DW_AT_GNU_template_name attribute to represent the
12899        name of the template template argument.  */
12900     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12901                         parent_die, parm);
12902   else
12903     gcc_unreachable ();
12904
12905   if (tmpl_die)
12906     {
12907       tree tmpl_type;
12908
12909       /* If PARM is a generic parameter pack, it means we are
12910          emitting debug info for a template argument pack element.
12911          In other terms, ARG is a template argument pack element.
12912          In that case, we don't emit any DW_AT_name attribute for
12913          the die.  */
12914       if (emit_name_p)
12915         {
12916           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12917           gcc_assert (name);
12918           add_AT_string (tmpl_die, DW_AT_name, name);
12919         }
12920
12921       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12922         {
12923           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12924              TMPL_DIE should have a child DW_AT_type attribute that is set
12925              to the type of the argument to PARM, which is ARG.
12926              If PARM is a type generic parameter, TMPL_DIE should have a
12927              child DW_AT_type that is set to ARG.  */
12928           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12929           add_type_attribute (tmpl_die, tmpl_type, 0,
12930                               TREE_THIS_VOLATILE (tmpl_type),
12931                               parent_die);
12932         }
12933       else
12934         {
12935           /* So TMPL_DIE is a DIE representing a
12936              a generic generic template parameter, a.k.a template template
12937              parameter in C++ and arg is a template.  */
12938
12939           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12940              to the name of the argument.  */
12941           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12942           if (name)
12943             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12944         }
12945
12946       if (TREE_CODE (parm) == PARM_DECL)
12947         /* So PARM is a non-type generic parameter.
12948            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12949            attribute of TMPL_DIE which value represents the value
12950            of ARG.
12951            We must be careful here:
12952            The value of ARG might reference some function decls.
12953            We might currently be emitting debug info for a generic
12954            type and types are emitted before function decls, we don't
12955            know if the function decls referenced by ARG will actually be
12956            emitted after cgraph computations.
12957            So must defer the generation of the DW_AT_const_value to
12958            after cgraph is ready.  */
12959         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12960     }
12961
12962   return tmpl_die;
12963 }
12964
12965 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12966    PARM_PACK must be a template parameter pack. The returned DIE
12967    will be child DIE of PARENT_DIE.  */
12968
12969 static dw_die_ref
12970 template_parameter_pack_die (tree parm_pack,
12971                              tree parm_pack_args,
12972                              dw_die_ref parent_die)
12973 {
12974   dw_die_ref die;
12975   int j;
12976
12977   gcc_assert (parent_die && parm_pack);
12978
12979   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12980   add_name_and_src_coords_attributes (die, parm_pack);
12981   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12982     generic_parameter_die (parm_pack,
12983                            TREE_VEC_ELT (parm_pack_args, j),
12984                            false /* Don't emit DW_AT_name */,
12985                            die);
12986   return die;
12987 }
12988
12989 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12990    an enumerated type.  */
12991
12992 static inline int
12993 type_is_enum (const_tree type)
12994 {
12995   return TREE_CODE (type) == ENUMERAL_TYPE;
12996 }
12997
12998 /* Return the DBX register number described by a given RTL node.  */
12999
13000 static unsigned int
13001 dbx_reg_number (const_rtx rtl)
13002 {
13003   unsigned regno = REGNO (rtl);
13004
13005   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13006
13007 #ifdef LEAF_REG_REMAP
13008   if (current_function_uses_only_leaf_regs)
13009     {
13010       int leaf_reg = LEAF_REG_REMAP (regno);
13011       if (leaf_reg != -1)
13012         regno = (unsigned) leaf_reg;
13013     }
13014 #endif
13015
13016   return DBX_REGISTER_NUMBER (regno);
13017 }
13018
13019 /* Optionally add a DW_OP_piece term to a location description expression.
13020    DW_OP_piece is only added if the location description expression already
13021    doesn't end with DW_OP_piece.  */
13022
13023 static void
13024 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13025 {
13026   dw_loc_descr_ref loc;
13027
13028   if (*list_head != NULL)
13029     {
13030       /* Find the end of the chain.  */
13031       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13032         ;
13033
13034       if (loc->dw_loc_opc != DW_OP_piece)
13035         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13036     }
13037 }
13038
13039 /* Return a location descriptor that designates a machine register or
13040    zero if there is none.  */
13041
13042 static dw_loc_descr_ref
13043 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13044 {
13045   rtx regs;
13046
13047   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13048     return 0;
13049
13050   /* We only use "frame base" when we're sure we're talking about the
13051      post-prologue local stack frame.  We do this by *not* running
13052      register elimination until this point, and recognizing the special
13053      argument pointer and soft frame pointer rtx's.
13054      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13055   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13056       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13057     {
13058       dw_loc_descr_ref result = NULL;
13059
13060       if (dwarf_version >= 4 || !dwarf_strict)
13061         {
13062           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13063           if (result)
13064             add_loc_descr (&result,
13065                            new_loc_descr (DW_OP_stack_value, 0, 0));
13066         }
13067       return result;
13068     }
13069
13070   regs = targetm.dwarf_register_span (rtl);
13071
13072   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13073     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13074   else
13075     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13076 }
13077
13078 /* Return a location descriptor that designates a machine register for
13079    a given hard register number.  */
13080
13081 static dw_loc_descr_ref
13082 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13083 {
13084   dw_loc_descr_ref reg_loc_descr;
13085
13086   if (regno <= 31)
13087     reg_loc_descr
13088       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13089   else
13090     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13091
13092   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13093     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13094
13095   return reg_loc_descr;
13096 }
13097
13098 /* Given an RTL of a register, return a location descriptor that
13099    designates a value that spans more than one register.  */
13100
13101 static dw_loc_descr_ref
13102 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13103                              enum var_init_status initialized)
13104 {
13105   int nregs, size, i;
13106   unsigned reg;
13107   dw_loc_descr_ref loc_result = NULL;
13108
13109   reg = REGNO (rtl);
13110 #ifdef LEAF_REG_REMAP
13111   if (current_function_uses_only_leaf_regs)
13112     {
13113       int leaf_reg = LEAF_REG_REMAP (reg);
13114       if (leaf_reg != -1)
13115         reg = (unsigned) leaf_reg;
13116     }
13117 #endif
13118   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13119   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13120
13121   /* Simple, contiguous registers.  */
13122   if (regs == NULL_RTX)
13123     {
13124       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13125
13126       loc_result = NULL;
13127       while (nregs--)
13128         {
13129           dw_loc_descr_ref t;
13130
13131           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13132                                       VAR_INIT_STATUS_INITIALIZED);
13133           add_loc_descr (&loc_result, t);
13134           add_loc_descr_op_piece (&loc_result, size);
13135           ++reg;
13136         }
13137       return loc_result;
13138     }
13139
13140   /* Now onto stupid register sets in non contiguous locations.  */
13141
13142   gcc_assert (GET_CODE (regs) == PARALLEL);
13143
13144   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13145   loc_result = NULL;
13146
13147   for (i = 0; i < XVECLEN (regs, 0); ++i)
13148     {
13149       dw_loc_descr_ref t;
13150
13151       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13152                                   VAR_INIT_STATUS_INITIALIZED);
13153       add_loc_descr (&loc_result, t);
13154       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13155       add_loc_descr_op_piece (&loc_result, size);
13156     }
13157
13158   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13159     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13160   return loc_result;
13161 }
13162
13163 /* Return a location descriptor that designates a constant.  */
13164
13165 static dw_loc_descr_ref
13166 int_loc_descriptor (HOST_WIDE_INT i)
13167 {
13168   enum dwarf_location_atom op;
13169
13170   /* Pick the smallest representation of a constant, rather than just
13171      defaulting to the LEB encoding.  */
13172   if (i >= 0)
13173     {
13174       if (i <= 31)
13175         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13176       else if (i <= 0xff)
13177         op = DW_OP_const1u;
13178       else if (i <= 0xffff)
13179         op = DW_OP_const2u;
13180       else if (HOST_BITS_PER_WIDE_INT == 32
13181                || i <= 0xffffffff)
13182         op = DW_OP_const4u;
13183       else
13184         op = DW_OP_constu;
13185     }
13186   else
13187     {
13188       if (i >= -0x80)
13189         op = DW_OP_const1s;
13190       else if (i >= -0x8000)
13191         op = DW_OP_const2s;
13192       else if (HOST_BITS_PER_WIDE_INT == 32
13193                || i >= -0x80000000)
13194         op = DW_OP_const4s;
13195       else
13196         op = DW_OP_consts;
13197     }
13198
13199   return new_loc_descr (op, i, 0);
13200 }
13201
13202 /* Return loc description representing "address" of integer value.
13203    This can appear only as toplevel expression.  */
13204
13205 static dw_loc_descr_ref
13206 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13207 {
13208   int litsize;
13209   dw_loc_descr_ref loc_result = NULL;
13210
13211   if (!(dwarf_version >= 4 || !dwarf_strict))
13212     return NULL;
13213
13214   if (i >= 0)
13215     {
13216       if (i <= 31)
13217         litsize = 1;
13218       else if (i <= 0xff)
13219         litsize = 2;
13220       else if (i <= 0xffff)
13221         litsize = 3;
13222       else if (HOST_BITS_PER_WIDE_INT == 32
13223                || i <= 0xffffffff)
13224         litsize = 5;
13225       else
13226         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13227     }
13228   else
13229     {
13230       if (i >= -0x80)
13231         litsize = 2;
13232       else if (i >= -0x8000)
13233         litsize = 3;
13234       else if (HOST_BITS_PER_WIDE_INT == 32
13235                || i >= -0x80000000)
13236         litsize = 5;
13237       else
13238         litsize = 1 + size_of_sleb128 (i);
13239     }
13240   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13241      is more compact.  For DW_OP_stack_value we need:
13242      litsize + 1 (DW_OP_stack_value)
13243      and for DW_OP_implicit_value:
13244      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13245   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13246     {
13247       loc_result = int_loc_descriptor (i);
13248       add_loc_descr (&loc_result,
13249                      new_loc_descr (DW_OP_stack_value, 0, 0));
13250       return loc_result;
13251     }
13252
13253   loc_result = new_loc_descr (DW_OP_implicit_value,
13254                               size, 0);
13255   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13256   loc_result->dw_loc_oprnd2.v.val_int = i;
13257   return loc_result;
13258 }
13259
13260 /* Return a location descriptor that designates a base+offset location.  */
13261
13262 static dw_loc_descr_ref
13263 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13264                  enum var_init_status initialized)
13265 {
13266   unsigned int regno;
13267   dw_loc_descr_ref result;
13268   dw_fde_ref fde = current_fde ();
13269
13270   /* We only use "frame base" when we're sure we're talking about the
13271      post-prologue local stack frame.  We do this by *not* running
13272      register elimination until this point, and recognizing the special
13273      argument pointer and soft frame pointer rtx's.  */
13274   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13275     {
13276       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13277
13278       if (elim != reg)
13279         {
13280           if (GET_CODE (elim) == PLUS)
13281             {
13282               offset += INTVAL (XEXP (elim, 1));
13283               elim = XEXP (elim, 0);
13284             }
13285           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13286                        && (elim == hard_frame_pointer_rtx
13287                            || elim == stack_pointer_rtx))
13288                       || elim == (frame_pointer_needed
13289                                   ? hard_frame_pointer_rtx
13290                                   : stack_pointer_rtx));
13291
13292           /* If drap register is used to align stack, use frame
13293              pointer + offset to access stack variables.  If stack
13294              is aligned without drap, use stack pointer + offset to
13295              access stack variables.  */
13296           if (crtl->stack_realign_tried
13297               && reg == frame_pointer_rtx)
13298             {
13299               int base_reg
13300                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13301                                       ? HARD_FRAME_POINTER_REGNUM
13302                                       : STACK_POINTER_REGNUM);
13303               return new_reg_loc_descr (base_reg, offset);
13304             }
13305
13306           offset += frame_pointer_fb_offset;
13307           return new_loc_descr (DW_OP_fbreg, offset, 0);
13308         }
13309     }
13310   else if (!optimize
13311            && fde
13312            && (fde->drap_reg == REGNO (reg)
13313                || fde->vdrap_reg == REGNO (reg)))
13314     {
13315       /* Use cfa+offset to represent the location of arguments passed
13316          on the stack when drap is used to align stack.
13317          Only do this when not optimizing, for optimized code var-tracking
13318          is supposed to track where the arguments live and the register
13319          used as vdrap or drap in some spot might be used for something
13320          else in other part of the routine.  */
13321       return new_loc_descr (DW_OP_fbreg, offset, 0);
13322     }
13323
13324   regno = dbx_reg_number (reg);
13325   if (regno <= 31)
13326     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13327                             offset, 0);
13328   else
13329     result = new_loc_descr (DW_OP_bregx, regno, offset);
13330
13331   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13332     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13333
13334   return result;
13335 }
13336
13337 /* Return true if this RTL expression describes a base+offset calculation.  */
13338
13339 static inline int
13340 is_based_loc (const_rtx rtl)
13341 {
13342   return (GET_CODE (rtl) == PLUS
13343           && ((REG_P (XEXP (rtl, 0))
13344                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13345                && CONST_INT_P (XEXP (rtl, 1)))));
13346 }
13347
13348 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13349    failed.  */
13350
13351 static dw_loc_descr_ref
13352 tls_mem_loc_descriptor (rtx mem)
13353 {
13354   tree base;
13355   dw_loc_descr_ref loc_result;
13356
13357   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13358     return NULL;
13359
13360   base = get_base_address (MEM_EXPR (mem));
13361   if (base == NULL
13362       || TREE_CODE (base) != VAR_DECL
13363       || !DECL_THREAD_LOCAL_P (base))
13364     return NULL;
13365
13366   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13367   if (loc_result == NULL)
13368     return NULL;
13369
13370   if (INTVAL (MEM_OFFSET (mem)))
13371     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13372
13373   return loc_result;
13374 }
13375
13376 /* Output debug info about reason why we failed to expand expression as dwarf
13377    expression.  */
13378
13379 static void
13380 expansion_failed (tree expr, rtx rtl, char const *reason)
13381 {
13382   if (dump_file && (dump_flags & TDF_DETAILS))
13383     {
13384       fprintf (dump_file, "Failed to expand as dwarf: ");
13385       if (expr)
13386         print_generic_expr (dump_file, expr, dump_flags);
13387       if (rtl)
13388         {
13389           fprintf (dump_file, "\n");
13390           print_rtl (dump_file, rtl);
13391         }
13392       fprintf (dump_file, "\nReason: %s\n", reason);
13393     }
13394 }
13395
13396 /* Helper function for const_ok_for_output, called either directly
13397    or via for_each_rtx.  */
13398
13399 static int
13400 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13401 {
13402   rtx rtl = *rtlp;
13403
13404   if (GET_CODE (rtl) == UNSPEC)
13405     {
13406       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13407          we can't express it in the debug info.  */
13408 #ifdef ENABLE_CHECKING
13409       inform (current_function_decl
13410               ? DECL_SOURCE_LOCATION (current_function_decl)
13411               : UNKNOWN_LOCATION,
13412               "non-delegitimized UNSPEC %d found in variable location",
13413               XINT (rtl, 1));
13414 #endif
13415       expansion_failed (NULL_TREE, rtl,
13416                         "UNSPEC hasn't been delegitimized.\n");
13417       return 1;
13418     }
13419
13420   if (GET_CODE (rtl) != SYMBOL_REF)
13421     return 0;
13422
13423   if (CONSTANT_POOL_ADDRESS_P (rtl))
13424     {
13425       bool marked;
13426       get_pool_constant_mark (rtl, &marked);
13427       /* If all references to this pool constant were optimized away,
13428          it was not output and thus we can't represent it.  */
13429       if (!marked)
13430         {
13431           expansion_failed (NULL_TREE, rtl,
13432                             "Constant was removed from constant pool.\n");
13433           return 1;
13434         }
13435     }
13436
13437   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13438     return 1;
13439
13440   /* Avoid references to external symbols in debug info, on several targets
13441      the linker might even refuse to link when linking a shared library,
13442      and in many other cases the relocations for .debug_info/.debug_loc are
13443      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13444      to be defined within the same shared library or executable are fine.  */
13445   if (SYMBOL_REF_EXTERNAL_P (rtl))
13446     {
13447       tree decl = SYMBOL_REF_DECL (rtl);
13448
13449       if (decl == NULL || !targetm.binds_local_p (decl))
13450         {
13451           expansion_failed (NULL_TREE, rtl,
13452                             "Symbol not defined in current TU.\n");
13453           return 1;
13454         }
13455     }
13456
13457   return 0;
13458 }
13459
13460 /* Return true if constant RTL can be emitted in DW_OP_addr or
13461    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13462    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13463
13464 static bool
13465 const_ok_for_output (rtx rtl)
13466 {
13467   if (GET_CODE (rtl) == SYMBOL_REF)
13468     return const_ok_for_output_1 (&rtl, NULL) == 0;
13469
13470   if (GET_CODE (rtl) == CONST)
13471     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13472
13473   return true;
13474 }
13475
13476 /* The following routine converts the RTL for a variable or parameter
13477    (resident in memory) into an equivalent Dwarf representation of a
13478    mechanism for getting the address of that same variable onto the top of a
13479    hypothetical "address evaluation" stack.
13480
13481    When creating memory location descriptors, we are effectively transforming
13482    the RTL for a memory-resident object into its Dwarf postfix expression
13483    equivalent.  This routine recursively descends an RTL tree, turning
13484    it into Dwarf postfix code as it goes.
13485
13486    MODE is the mode of the memory reference, needed to handle some
13487    autoincrement addressing modes.
13488
13489    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13490    location list for RTL.
13491
13492    Return 0 if we can't represent the location.  */
13493
13494 static dw_loc_descr_ref
13495 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13496                     enum var_init_status initialized)
13497 {
13498   dw_loc_descr_ref mem_loc_result = NULL;
13499   enum dwarf_location_atom op;
13500   dw_loc_descr_ref op0, op1;
13501
13502   /* Note that for a dynamically sized array, the location we will generate a
13503      description of here will be the lowest numbered location which is
13504      actually within the array.  That's *not* necessarily the same as the
13505      zeroth element of the array.  */
13506
13507   rtl = targetm.delegitimize_address (rtl);
13508
13509   switch (GET_CODE (rtl))
13510     {
13511     case POST_INC:
13512     case POST_DEC:
13513     case POST_MODIFY:
13514       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13515
13516     case SUBREG:
13517       /* The case of a subreg may arise when we have a local (register)
13518          variable or a formal (register) parameter which doesn't quite fill
13519          up an entire register.  For now, just assume that it is
13520          legitimate to make the Dwarf info refer to the whole register which
13521          contains the given subreg.  */
13522       if (!subreg_lowpart_p (rtl))
13523         break;
13524       rtl = SUBREG_REG (rtl);
13525       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13526         break;
13527       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13528         break;
13529       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13530       break;
13531
13532     case REG:
13533       /* Whenever a register number forms a part of the description of the
13534          method for calculating the (dynamic) address of a memory resident
13535          object, DWARF rules require the register number be referred to as
13536          a "base register".  This distinction is not based in any way upon
13537          what category of register the hardware believes the given register
13538          belongs to.  This is strictly DWARF terminology we're dealing with
13539          here. Note that in cases where the location of a memory-resident
13540          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13541          OP_CONST (0)) the actual DWARF location descriptor that we generate
13542          may just be OP_BASEREG (basereg).  This may look deceptively like
13543          the object in question was allocated to a register (rather than in
13544          memory) so DWARF consumers need to be aware of the subtle
13545          distinction between OP_REG and OP_BASEREG.  */
13546       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13547         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13548       else if (stack_realign_drap
13549                && crtl->drap_reg
13550                && crtl->args.internal_arg_pointer == rtl
13551                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13552         {
13553           /* If RTL is internal_arg_pointer, which has been optimized
13554              out, use DRAP instead.  */
13555           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13556                                             VAR_INIT_STATUS_INITIALIZED);
13557         }
13558       break;
13559
13560     case SIGN_EXTEND:
13561     case ZERO_EXTEND:
13562       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13563                                 VAR_INIT_STATUS_INITIALIZED);
13564       if (op0 == 0)
13565         break;
13566       else
13567         {
13568           int shift = DWARF2_ADDR_SIZE
13569                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13570           shift *= BITS_PER_UNIT;
13571           if (GET_CODE (rtl) == SIGN_EXTEND)
13572             op = DW_OP_shra;
13573           else
13574             op = DW_OP_shr;
13575           mem_loc_result = op0;
13576           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13577           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13578           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13579           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13580         }
13581       break;
13582
13583     case MEM:
13584       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13585                                            VAR_INIT_STATUS_INITIALIZED);
13586       if (mem_loc_result == NULL)
13587         mem_loc_result = tls_mem_loc_descriptor (rtl);
13588       if (mem_loc_result != 0)
13589         {
13590           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13591             {
13592               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13593               return 0;
13594             }
13595           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13596             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13597           else
13598             add_loc_descr (&mem_loc_result,
13599                            new_loc_descr (DW_OP_deref_size,
13600                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13601         }
13602       else
13603         {
13604           rtx new_rtl = avoid_constant_pool_reference (rtl);
13605           if (new_rtl != rtl)
13606             return mem_loc_descriptor (new_rtl, mode, initialized);
13607         }
13608       break;
13609
13610     case LO_SUM:
13611          rtl = XEXP (rtl, 1);
13612
13613       /* ... fall through ...  */
13614
13615     case LABEL_REF:
13616       /* Some ports can transform a symbol ref into a label ref, because
13617          the symbol ref is too far away and has to be dumped into a constant
13618          pool.  */
13619     case CONST:
13620     case SYMBOL_REF:
13621       if (GET_CODE (rtl) == SYMBOL_REF
13622           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13623         {
13624           dw_loc_descr_ref temp;
13625
13626           /* If this is not defined, we have no way to emit the data.  */
13627           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13628             break;
13629
13630           /* We used to emit DW_OP_addr here, but that's wrong, since
13631              DW_OP_addr should be relocated by the debug info consumer,
13632              while DW_OP_GNU_push_tls_address operand should not.  */
13633           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13634                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13635           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13636           temp->dw_loc_oprnd1.v.val_addr = rtl;
13637           temp->dtprel = true;
13638
13639           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13640           add_loc_descr (&mem_loc_result, temp);
13641
13642           break;
13643         }
13644
13645       if (!const_ok_for_output (rtl))
13646         break;
13647
13648     symref:
13649       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13650       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13651       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13652       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13653       break;
13654
13655     case CONCAT:
13656     case CONCATN:
13657     case VAR_LOCATION:
13658       expansion_failed (NULL_TREE, rtl,
13659                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13660       return 0;
13661
13662     case PRE_MODIFY:
13663       /* Extract the PLUS expression nested inside and fall into
13664          PLUS code below.  */
13665       rtl = XEXP (rtl, 1);
13666       goto plus;
13667
13668     case PRE_INC:
13669     case PRE_DEC:
13670       /* Turn these into a PLUS expression and fall into the PLUS code
13671          below.  */
13672       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13673                           GEN_INT (GET_CODE (rtl) == PRE_INC
13674                                    ? GET_MODE_UNIT_SIZE (mode)
13675                                    : -GET_MODE_UNIT_SIZE (mode)));
13676
13677       /* ... fall through ...  */
13678
13679     case PLUS:
13680     plus:
13681       if (is_based_loc (rtl))
13682         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13683                                           INTVAL (XEXP (rtl, 1)),
13684                                           VAR_INIT_STATUS_INITIALIZED);
13685       else
13686         {
13687           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13688                                                VAR_INIT_STATUS_INITIALIZED);
13689           if (mem_loc_result == 0)
13690             break;
13691
13692           if (CONST_INT_P (XEXP (rtl, 1)))
13693             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13694           else
13695             {
13696               dw_loc_descr_ref mem_loc_result2
13697                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13698                                       VAR_INIT_STATUS_INITIALIZED);
13699               if (mem_loc_result2 == 0)
13700                 break;
13701               add_loc_descr (&mem_loc_result, mem_loc_result2);
13702               add_loc_descr (&mem_loc_result,
13703                              new_loc_descr (DW_OP_plus, 0, 0));
13704             }
13705         }
13706       break;
13707
13708     /* If a pseudo-reg is optimized away, it is possible for it to
13709        be replaced with a MEM containing a multiply or shift.  */
13710     case MINUS:
13711       op = DW_OP_minus;
13712       goto do_binop;
13713
13714     case MULT:
13715       op = DW_OP_mul;
13716       goto do_binop;
13717
13718     case DIV:
13719       op = DW_OP_div;
13720       goto do_binop;
13721
13722     case UMOD:
13723       op = DW_OP_mod;
13724       goto do_binop;
13725
13726     case ASHIFT:
13727       op = DW_OP_shl;
13728       goto do_binop;
13729
13730     case ASHIFTRT:
13731       op = DW_OP_shra;
13732       goto do_binop;
13733
13734     case LSHIFTRT:
13735       op = DW_OP_shr;
13736       goto do_binop;
13737
13738     case AND:
13739       op = DW_OP_and;
13740       goto do_binop;
13741
13742     case IOR:
13743       op = DW_OP_or;
13744       goto do_binop;
13745
13746     case XOR:
13747       op = DW_OP_xor;
13748       goto do_binop;
13749
13750     do_binop:
13751       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13752                                 VAR_INIT_STATUS_INITIALIZED);
13753       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13754                                 VAR_INIT_STATUS_INITIALIZED);
13755
13756       if (op0 == 0 || op1 == 0)
13757         break;
13758
13759       mem_loc_result = op0;
13760       add_loc_descr (&mem_loc_result, op1);
13761       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13762       break;
13763
13764     case MOD:
13765       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13766                                 VAR_INIT_STATUS_INITIALIZED);
13767       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13768                                 VAR_INIT_STATUS_INITIALIZED);
13769
13770       if (op0 == 0 || op1 == 0)
13771         break;
13772
13773       mem_loc_result = op0;
13774       add_loc_descr (&mem_loc_result, op1);
13775       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13776       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13777       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13778       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13779       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13780       break;
13781
13782     case NOT:
13783       op = DW_OP_not;
13784       goto do_unop;
13785
13786     case ABS:
13787       op = DW_OP_abs;
13788       goto do_unop;
13789
13790     case NEG:
13791       op = DW_OP_neg;
13792       goto do_unop;
13793
13794     do_unop:
13795       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13796                                 VAR_INIT_STATUS_INITIALIZED);
13797
13798       if (op0 == 0)
13799         break;
13800
13801       mem_loc_result = op0;
13802       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13803       break;
13804
13805     case CONST_INT:
13806       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13807       break;
13808
13809     case EQ:
13810       op = DW_OP_eq;
13811       goto do_scompare;
13812
13813     case GE:
13814       op = DW_OP_ge;
13815       goto do_scompare;
13816
13817     case GT:
13818       op = DW_OP_gt;
13819       goto do_scompare;
13820
13821     case LE:
13822       op = DW_OP_le;
13823       goto do_scompare;
13824
13825     case LT:
13826       op = DW_OP_lt;
13827       goto do_scompare;
13828
13829     case NE:
13830       op = DW_OP_ne;
13831       goto do_scompare;
13832
13833     do_scompare:
13834       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13835           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13836         break;
13837       else
13838         {
13839           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13840
13841           if (op_mode == VOIDmode)
13842             op_mode = GET_MODE (XEXP (rtl, 1));
13843           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13844             break;
13845
13846           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13847                                     VAR_INIT_STATUS_INITIALIZED);
13848           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13849                                     VAR_INIT_STATUS_INITIALIZED);
13850
13851           if (op0 == 0 || op1 == 0)
13852             break;
13853
13854           if (op_mode != VOIDmode
13855               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13856             {
13857               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13858               shift *= BITS_PER_UNIT;
13859               /* For eq/ne, if the operands are known to be zero-extended,
13860                  there is no need to do the fancy shifting up.  */
13861               if (op == DW_OP_eq || op == DW_OP_ne)
13862                 {
13863                   dw_loc_descr_ref last0, last1;
13864                   for (last0 = op0;
13865                        last0->dw_loc_next != NULL;
13866                        last0 = last0->dw_loc_next)
13867                     ;
13868                   for (last1 = op1;
13869                        last1->dw_loc_next != NULL;
13870                        last1 = last1->dw_loc_next)
13871                     ;
13872                   /* deref_size zero extends, and for constants we can check
13873                      whether they are zero extended or not.  */
13874                   if (((last0->dw_loc_opc == DW_OP_deref_size
13875                         && last0->dw_loc_oprnd1.v.val_int
13876                            <= GET_MODE_SIZE (op_mode))
13877                        || (CONST_INT_P (XEXP (rtl, 0))
13878                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13879                                == (INTVAL (XEXP (rtl, 0))
13880                                    & GET_MODE_MASK (op_mode))))
13881                       && ((last1->dw_loc_opc == DW_OP_deref_size
13882                            && last1->dw_loc_oprnd1.v.val_int
13883                               <= GET_MODE_SIZE (op_mode))
13884                           || (CONST_INT_P (XEXP (rtl, 1))
13885                               && (unsigned HOST_WIDE_INT)
13886                                  INTVAL (XEXP (rtl, 1))
13887                                  == (INTVAL (XEXP (rtl, 1))
13888                                      & GET_MODE_MASK (op_mode)))))
13889                     goto do_compare;
13890                 }
13891               add_loc_descr (&op0, int_loc_descriptor (shift));
13892               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13893               if (CONST_INT_P (XEXP (rtl, 1)))
13894                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13895               else
13896                 {
13897                   add_loc_descr (&op1, int_loc_descriptor (shift));
13898                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13899                 }
13900             }
13901         }
13902
13903     do_compare:
13904       mem_loc_result = op0;
13905       add_loc_descr (&mem_loc_result, op1);
13906       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13907       if (STORE_FLAG_VALUE != 1)
13908         {
13909           add_loc_descr (&mem_loc_result,
13910                          int_loc_descriptor (STORE_FLAG_VALUE));
13911           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13912         }
13913       break;
13914
13915     case GEU:
13916       op = DW_OP_ge;
13917       goto do_ucompare;
13918
13919     case GTU:
13920       op = DW_OP_gt;
13921       goto do_ucompare;
13922
13923     case LEU:
13924       op = DW_OP_le;
13925       goto do_ucompare;
13926
13927     case LTU:
13928       op = DW_OP_lt;
13929       goto do_ucompare;
13930
13931     do_ucompare:
13932       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13933           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13934         break;
13935       else
13936         {
13937           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13938
13939           if (op_mode == VOIDmode)
13940             op_mode = GET_MODE (XEXP (rtl, 1));
13941           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13942             break;
13943
13944           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13945                                     VAR_INIT_STATUS_INITIALIZED);
13946           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13947                                     VAR_INIT_STATUS_INITIALIZED);
13948
13949           if (op0 == 0 || op1 == 0)
13950             break;
13951
13952           if (op_mode != VOIDmode
13953               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13954             {
13955               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13956               dw_loc_descr_ref last0, last1;
13957               for (last0 = op0;
13958                    last0->dw_loc_next != NULL;
13959                    last0 = last0->dw_loc_next)
13960                 ;
13961               for (last1 = op1;
13962                    last1->dw_loc_next != NULL;
13963                    last1 = last1->dw_loc_next)
13964                 ;
13965               if (CONST_INT_P (XEXP (rtl, 0)))
13966                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13967               /* deref_size zero extends, so no need to mask it again.  */
13968               else if (last0->dw_loc_opc != DW_OP_deref_size
13969                        || last0->dw_loc_oprnd1.v.val_int
13970                           > GET_MODE_SIZE (op_mode))
13971                 {
13972                   add_loc_descr (&op0, int_loc_descriptor (mask));
13973                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13974                 }
13975               if (CONST_INT_P (XEXP (rtl, 1)))
13976                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13977               /* deref_size zero extends, so no need to mask it again.  */
13978               else if (last1->dw_loc_opc != DW_OP_deref_size
13979                        || last1->dw_loc_oprnd1.v.val_int
13980                           > GET_MODE_SIZE (op_mode))
13981                 {
13982                   add_loc_descr (&op1, int_loc_descriptor (mask));
13983                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13984                 }
13985             }
13986           else
13987             {
13988               HOST_WIDE_INT bias = 1;
13989               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13990               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13991               if (CONST_INT_P (XEXP (rtl, 1)))
13992                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13993                                           + INTVAL (XEXP (rtl, 1)));
13994               else
13995                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13996                                                     bias, 0));
13997             }
13998         }
13999       goto do_compare;
14000
14001     case SMIN:
14002     case SMAX:
14003     case UMIN:
14004     case UMAX:
14005       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14006           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14007           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14008         break;
14009
14010       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14011                                 VAR_INIT_STATUS_INITIALIZED);
14012       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14013                                 VAR_INIT_STATUS_INITIALIZED);
14014
14015       if (op0 == 0 || op1 == 0)
14016         break;
14017
14018       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14019       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14020       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14021       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14022         {
14023           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14024             {
14025               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14026               add_loc_descr (&op0, int_loc_descriptor (mask));
14027               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14028               add_loc_descr (&op1, int_loc_descriptor (mask));
14029               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14030             }
14031           else
14032             {
14033               HOST_WIDE_INT bias = 1;
14034               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14035               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14036               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14037             }
14038         }
14039       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14040         {
14041           int shift = DWARF2_ADDR_SIZE
14042                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14043           shift *= BITS_PER_UNIT;
14044           add_loc_descr (&op0, int_loc_descriptor (shift));
14045           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14046           add_loc_descr (&op1, int_loc_descriptor (shift));
14047           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14048         }
14049
14050       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14051         op = DW_OP_lt;
14052       else
14053         op = DW_OP_gt;
14054       mem_loc_result = op0;
14055       add_loc_descr (&mem_loc_result, op1);
14056       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14057       {
14058         dw_loc_descr_ref bra_node, drop_node;
14059
14060         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14061         add_loc_descr (&mem_loc_result, bra_node);
14062         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14063         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14064         add_loc_descr (&mem_loc_result, drop_node);
14065         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14066         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14067       }
14068       break;
14069
14070     case ZERO_EXTRACT:
14071     case SIGN_EXTRACT:
14072       if (CONST_INT_P (XEXP (rtl, 1))
14073           && CONST_INT_P (XEXP (rtl, 2))
14074           && ((unsigned) INTVAL (XEXP (rtl, 1))
14075               + (unsigned) INTVAL (XEXP (rtl, 2))
14076               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14077           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14078           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14079         {
14080           int shift, size;
14081           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14082                                     VAR_INIT_STATUS_INITIALIZED);
14083           if (op0 == 0)
14084             break;
14085           if (GET_CODE (rtl) == SIGN_EXTRACT)
14086             op = DW_OP_shra;
14087           else
14088             op = DW_OP_shr;
14089           mem_loc_result = op0;
14090           size = INTVAL (XEXP (rtl, 1));
14091           shift = INTVAL (XEXP (rtl, 2));
14092           if (BITS_BIG_ENDIAN)
14093             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14094                     - shift - size;
14095           if (shift + size != (int) DWARF2_ADDR_SIZE)
14096             {
14097               add_loc_descr (&mem_loc_result,
14098                              int_loc_descriptor (DWARF2_ADDR_SIZE
14099                                                  - shift - size));
14100               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14101             }
14102           if (size != (int) DWARF2_ADDR_SIZE)
14103             {
14104               add_loc_descr (&mem_loc_result,
14105                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14106               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14107             }
14108         }
14109       break;
14110
14111     case COMPARE:
14112     case IF_THEN_ELSE:
14113     case ROTATE:
14114     case ROTATERT:
14115     case TRUNCATE:
14116       /* In theory, we could implement the above.  */
14117       /* DWARF cannot represent the unsigned compare operations
14118          natively.  */
14119     case SS_MULT:
14120     case US_MULT:
14121     case SS_DIV:
14122     case US_DIV:
14123     case SS_PLUS:
14124     case US_PLUS:
14125     case SS_MINUS:
14126     case US_MINUS:
14127     case SS_NEG:
14128     case US_NEG:
14129     case SS_ABS:
14130     case SS_ASHIFT:
14131     case US_ASHIFT:
14132     case SS_TRUNCATE:
14133     case US_TRUNCATE:
14134     case UDIV:
14135     case UNORDERED:
14136     case ORDERED:
14137     case UNEQ:
14138     case UNGE:
14139     case UNGT:
14140     case UNLE:
14141     case UNLT:
14142     case LTGT:
14143     case FLOAT_EXTEND:
14144     case FLOAT_TRUNCATE:
14145     case FLOAT:
14146     case UNSIGNED_FLOAT:
14147     case FIX:
14148     case UNSIGNED_FIX:
14149     case FRACT_CONVERT:
14150     case UNSIGNED_FRACT_CONVERT:
14151     case SAT_FRACT:
14152     case UNSIGNED_SAT_FRACT:
14153     case SQRT:
14154     case BSWAP:
14155     case FFS:
14156     case CLZ:
14157     case CTZ:
14158     case POPCOUNT:
14159     case PARITY:
14160     case ASM_OPERANDS:
14161     case VEC_MERGE:
14162     case VEC_SELECT:
14163     case VEC_CONCAT:
14164     case VEC_DUPLICATE:
14165     case UNSPEC:
14166     case HIGH:
14167       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14168          can't express it in the debug info.  This can happen e.g. with some
14169          TLS UNSPECs.  */
14170       break;
14171
14172     case CONST_STRING:
14173       resolve_one_addr (&rtl, NULL);
14174       goto symref;
14175
14176     default:
14177 #ifdef ENABLE_CHECKING
14178       print_rtl (stderr, rtl);
14179       gcc_unreachable ();
14180 #else
14181       break;
14182 #endif
14183     }
14184
14185   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14186     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14187
14188   return mem_loc_result;
14189 }
14190
14191 /* Return a descriptor that describes the concatenation of two locations.
14192    This is typically a complex variable.  */
14193
14194 static dw_loc_descr_ref
14195 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14196 {
14197   dw_loc_descr_ref cc_loc_result = NULL;
14198   dw_loc_descr_ref x0_ref
14199     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14200   dw_loc_descr_ref x1_ref
14201     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14202
14203   if (x0_ref == 0 || x1_ref == 0)
14204     return 0;
14205
14206   cc_loc_result = x0_ref;
14207   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14208
14209   add_loc_descr (&cc_loc_result, x1_ref);
14210   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14211
14212   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14213     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14214
14215   return cc_loc_result;
14216 }
14217
14218 /* Return a descriptor that describes the concatenation of N
14219    locations.  */
14220
14221 static dw_loc_descr_ref
14222 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14223 {
14224   unsigned int i;
14225   dw_loc_descr_ref cc_loc_result = NULL;
14226   unsigned int n = XVECLEN (concatn, 0);
14227
14228   for (i = 0; i < n; ++i)
14229     {
14230       dw_loc_descr_ref ref;
14231       rtx x = XVECEXP (concatn, 0, i);
14232
14233       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14234       if (ref == NULL)
14235         return NULL;
14236
14237       add_loc_descr (&cc_loc_result, ref);
14238       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14239     }
14240
14241   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14242     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14243
14244   return cc_loc_result;
14245 }
14246
14247 /* Output a proper Dwarf location descriptor for a variable or parameter
14248    which is either allocated in a register or in a memory location.  For a
14249    register, we just generate an OP_REG and the register number.  For a
14250    memory location we provide a Dwarf postfix expression describing how to
14251    generate the (dynamic) address of the object onto the address stack.
14252
14253    MODE is mode of the decl if this loc_descriptor is going to be used in
14254    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14255    allowed, VOIDmode otherwise.
14256
14257    If we don't know how to describe it, return 0.  */
14258
14259 static dw_loc_descr_ref
14260 loc_descriptor (rtx rtl, enum machine_mode mode,
14261                 enum var_init_status initialized)
14262 {
14263   dw_loc_descr_ref loc_result = NULL;
14264
14265   switch (GET_CODE (rtl))
14266     {
14267     case SUBREG:
14268       /* The case of a subreg may arise when we have a local (register)
14269          variable or a formal (register) parameter which doesn't quite fill
14270          up an entire register.  For now, just assume that it is
14271          legitimate to make the Dwarf info refer to the whole register which
14272          contains the given subreg.  */
14273       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14274       break;
14275
14276     case REG:
14277       loc_result = reg_loc_descriptor (rtl, initialized);
14278       break;
14279
14280     case MEM:
14281       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14282                                        initialized);
14283       if (loc_result == NULL)
14284         loc_result = tls_mem_loc_descriptor (rtl);
14285       if (loc_result == NULL)
14286         {
14287           rtx new_rtl = avoid_constant_pool_reference (rtl);
14288           if (new_rtl != rtl)
14289             loc_result = loc_descriptor (new_rtl, mode, initialized);
14290         }
14291       break;
14292
14293     case CONCAT:
14294       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14295                                           initialized);
14296       break;
14297
14298     case CONCATN:
14299       loc_result = concatn_loc_descriptor (rtl, initialized);
14300       break;
14301
14302     case VAR_LOCATION:
14303       /* Single part.  */
14304       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14305         {
14306           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14307           if (GET_CODE (loc) == EXPR_LIST)
14308             loc = XEXP (loc, 0);
14309           loc_result = loc_descriptor (loc, mode, initialized);
14310           break;
14311         }
14312
14313       rtl = XEXP (rtl, 1);
14314       /* FALLTHRU */
14315
14316     case PARALLEL:
14317       {
14318         rtvec par_elems = XVEC (rtl, 0);
14319         int num_elem = GET_NUM_ELEM (par_elems);
14320         enum machine_mode mode;
14321         int i;
14322
14323         /* Create the first one, so we have something to add to.  */
14324         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14325                                      VOIDmode, initialized);
14326         if (loc_result == NULL)
14327           return NULL;
14328         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14329         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14330         for (i = 1; i < num_elem; i++)
14331           {
14332             dw_loc_descr_ref temp;
14333
14334             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14335                                    VOIDmode, initialized);
14336             if (temp == NULL)
14337               return NULL;
14338             add_loc_descr (&loc_result, temp);
14339             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14340             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14341           }
14342       }
14343       break;
14344
14345     case CONST_INT:
14346       if (mode != VOIDmode && mode != BLKmode)
14347         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14348                                                     INTVAL (rtl));
14349       break;
14350
14351     case CONST_DOUBLE:
14352       if (mode == VOIDmode)
14353         mode = GET_MODE (rtl);
14354
14355       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14356         {
14357           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14358
14359           /* Note that a CONST_DOUBLE rtx could represent either an integer
14360              or a floating-point constant.  A CONST_DOUBLE is used whenever
14361              the constant requires more than one word in order to be
14362              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14363           loc_result = new_loc_descr (DW_OP_implicit_value,
14364                                       GET_MODE_SIZE (mode), 0);
14365           if (SCALAR_FLOAT_MODE_P (mode))
14366             {
14367               unsigned int length = GET_MODE_SIZE (mode);
14368               unsigned char *array
14369                   = (unsigned char*) ggc_alloc_atomic (length);
14370
14371               insert_float (rtl, array);
14372               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14373               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14374               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14375               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14376             }
14377           else
14378             {
14379               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14380               loc_result->dw_loc_oprnd2.v.val_double
14381                 = rtx_to_double_int (rtl);
14382             }
14383         }
14384       break;
14385
14386     case CONST_VECTOR:
14387       if (mode == VOIDmode)
14388         mode = GET_MODE (rtl);
14389
14390       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14391         {
14392           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14393           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14394           unsigned char *array = (unsigned char *)
14395             ggc_alloc_atomic (length * elt_size);
14396           unsigned int i;
14397           unsigned char *p;
14398
14399           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14400           switch (GET_MODE_CLASS (mode))
14401             {
14402             case MODE_VECTOR_INT:
14403               for (i = 0, p = array; i < length; i++, p += elt_size)
14404                 {
14405                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14406                   double_int val = rtx_to_double_int (elt);
14407
14408                   if (elt_size <= sizeof (HOST_WIDE_INT))
14409                     insert_int (double_int_to_shwi (val), elt_size, p);
14410                   else
14411                     {
14412                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14413                       insert_double (val, p);
14414                     }
14415                 }
14416               break;
14417
14418             case MODE_VECTOR_FLOAT:
14419               for (i = 0, p = array; i < length; i++, p += elt_size)
14420                 {
14421                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14422                   insert_float (elt, p);
14423                 }
14424               break;
14425
14426             default:
14427               gcc_unreachable ();
14428             }
14429
14430           loc_result = new_loc_descr (DW_OP_implicit_value,
14431                                       length * elt_size, 0);
14432           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14433           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14434           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14435           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14436         }
14437       break;
14438
14439     case CONST:
14440       if (mode == VOIDmode
14441           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14442           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14443           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14444         {
14445           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14446           break;
14447         }
14448       /* FALLTHROUGH */
14449     case SYMBOL_REF:
14450       if (!const_ok_for_output (rtl))
14451         break;
14452     case LABEL_REF:
14453       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14454           && (dwarf_version >= 4 || !dwarf_strict))
14455         {
14456           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14457           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14458           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14459           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14460           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14461         }
14462       break;
14463
14464     default:
14465       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14466           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14467           && (dwarf_version >= 4 || !dwarf_strict))
14468         {
14469           /* Value expression.  */
14470           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14471           if (loc_result)
14472             add_loc_descr (&loc_result,
14473                            new_loc_descr (DW_OP_stack_value, 0, 0));
14474         }
14475       break;
14476     }
14477
14478   return loc_result;
14479 }
14480
14481 /* We need to figure out what section we should use as the base for the
14482    address ranges where a given location is valid.
14483    1. If this particular DECL has a section associated with it, use that.
14484    2. If this function has a section associated with it, use that.
14485    3. Otherwise, use the text section.
14486    XXX: If you split a variable across multiple sections, we won't notice.  */
14487
14488 static const char *
14489 secname_for_decl (const_tree decl)
14490 {
14491   const char *secname;
14492
14493   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14494     {
14495       tree sectree = DECL_SECTION_NAME (decl);
14496       secname = TREE_STRING_POINTER (sectree);
14497     }
14498   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14499     {
14500       tree sectree = DECL_SECTION_NAME (current_function_decl);
14501       secname = TREE_STRING_POINTER (sectree);
14502     }
14503   else if (cfun && in_cold_section_p)
14504     secname = crtl->subsections.cold_section_label;
14505   else
14506     secname = text_section_label;
14507
14508   return secname;
14509 }
14510
14511 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14512
14513 static bool
14514 decl_by_reference_p (tree decl)
14515 {
14516   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14517            || TREE_CODE (decl) == VAR_DECL)
14518           && DECL_BY_REFERENCE (decl));
14519 }
14520
14521 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14522    for VARLOC.  */
14523
14524 static dw_loc_descr_ref
14525 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14526                enum var_init_status initialized)
14527 {
14528   int have_address = 0;
14529   dw_loc_descr_ref descr;
14530   enum machine_mode mode;
14531
14532   if (want_address != 2)
14533     {
14534       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14535       /* Single part.  */
14536       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14537         {
14538           varloc = PAT_VAR_LOCATION_LOC (varloc);
14539           if (GET_CODE (varloc) == EXPR_LIST)
14540             varloc = XEXP (varloc, 0);
14541           mode = GET_MODE (varloc);
14542           if (MEM_P (varloc))
14543             {
14544               rtx addr = XEXP (varloc, 0);
14545               descr = mem_loc_descriptor (addr, mode, initialized);
14546               if (descr)
14547                 have_address = 1;
14548               else
14549                 {
14550                   rtx x = avoid_constant_pool_reference (varloc);
14551                   if (x != varloc)
14552                     descr = mem_loc_descriptor (x, mode, initialized);
14553                 }
14554             }
14555           else
14556             descr = mem_loc_descriptor (varloc, mode, initialized);
14557         }
14558       else
14559         return 0;
14560     }
14561   else
14562     {
14563       if (GET_CODE (varloc) == VAR_LOCATION)
14564         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14565       else
14566         mode = DECL_MODE (loc);
14567       descr = loc_descriptor (varloc, mode, initialized);
14568       have_address = 1;
14569     }
14570
14571   if (!descr)
14572     return 0;
14573
14574   if (want_address == 2 && !have_address
14575       && (dwarf_version >= 4 || !dwarf_strict))
14576     {
14577       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14578         {
14579           expansion_failed (loc, NULL_RTX,
14580                             "DWARF address size mismatch");
14581           return 0;
14582         }
14583       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14584       have_address = 1;
14585     }
14586   /* Show if we can't fill the request for an address.  */
14587   if (want_address && !have_address)
14588     {
14589       expansion_failed (loc, NULL_RTX,
14590                         "Want address and only have value");
14591       return 0;
14592     }
14593
14594   /* If we've got an address and don't want one, dereference.  */
14595   if (!want_address && have_address)
14596     {
14597       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14598       enum dwarf_location_atom op;
14599
14600       if (size > DWARF2_ADDR_SIZE || size == -1)
14601         {
14602           expansion_failed (loc, NULL_RTX,
14603                             "DWARF address size mismatch");
14604           return 0;
14605         }
14606       else if (size == DWARF2_ADDR_SIZE)
14607         op = DW_OP_deref;
14608       else
14609         op = DW_OP_deref_size;
14610
14611       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14612     }
14613
14614   return descr;
14615 }
14616
14617 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14618    if it is not possible.  */
14619
14620 static dw_loc_descr_ref
14621 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14622 {
14623   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14624     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14625   else if (dwarf_version >= 3 || !dwarf_strict)
14626     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14627   else
14628     return NULL;
14629 }
14630
14631 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14632    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14633
14634 static dw_loc_descr_ref
14635 dw_sra_loc_expr (tree decl, rtx loc)
14636 {
14637   rtx p;
14638   unsigned int padsize = 0;
14639   dw_loc_descr_ref descr, *descr_tail;
14640   unsigned HOST_WIDE_INT decl_size;
14641   rtx varloc;
14642   enum var_init_status initialized;
14643
14644   if (DECL_SIZE (decl) == NULL
14645       || !host_integerp (DECL_SIZE (decl), 1))
14646     return NULL;
14647
14648   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14649   descr = NULL;
14650   descr_tail = &descr;
14651
14652   for (p = loc; p; p = XEXP (p, 1))
14653     {
14654       unsigned int bitsize = decl_piece_bitsize (p);
14655       rtx loc_note = *decl_piece_varloc_ptr (p);
14656       dw_loc_descr_ref cur_descr;
14657       dw_loc_descr_ref *tail, last = NULL;
14658       unsigned int opsize = 0;
14659
14660       if (loc_note == NULL_RTX
14661           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14662         {
14663           padsize += bitsize;
14664           continue;
14665         }
14666       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14667       varloc = NOTE_VAR_LOCATION (loc_note);
14668       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14669       if (cur_descr == NULL)
14670         {
14671           padsize += bitsize;
14672           continue;
14673         }
14674
14675       /* Check that cur_descr either doesn't use
14676          DW_OP_*piece operations, or their sum is equal
14677          to bitsize.  Otherwise we can't embed it.  */
14678       for (tail = &cur_descr; *tail != NULL;
14679            tail = &(*tail)->dw_loc_next)
14680         if ((*tail)->dw_loc_opc == DW_OP_piece)
14681           {
14682             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14683                       * BITS_PER_UNIT;
14684             last = *tail;
14685           }
14686         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14687           {
14688             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14689             last = *tail;
14690           }
14691
14692       if (last != NULL && opsize != bitsize)
14693         {
14694           padsize += bitsize;
14695           continue;
14696         }
14697
14698       /* If there is a hole, add DW_OP_*piece after empty DWARF
14699          expression, which means that those bits are optimized out.  */
14700       if (padsize)
14701         {
14702           if (padsize > decl_size)
14703             return NULL;
14704           decl_size -= padsize;
14705           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14706           if (*descr_tail == NULL)
14707             return NULL;
14708           descr_tail = &(*descr_tail)->dw_loc_next;
14709           padsize = 0;
14710         }
14711       *descr_tail = cur_descr;
14712       descr_tail = tail;
14713       if (bitsize > decl_size)
14714         return NULL;
14715       decl_size -= bitsize;
14716       if (last == NULL)
14717         {
14718           HOST_WIDE_INT offset = 0;
14719           if (GET_CODE (varloc) == VAR_LOCATION
14720               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14721             {
14722               varloc = PAT_VAR_LOCATION_LOC (varloc);
14723               if (GET_CODE (varloc) == EXPR_LIST)
14724                 varloc = XEXP (varloc, 0);
14725             }
14726           do 
14727             {
14728               if (GET_CODE (varloc) == CONST
14729                   || GET_CODE (varloc) == SIGN_EXTEND
14730                   || GET_CODE (varloc) == ZERO_EXTEND)
14731                 varloc = XEXP (varloc, 0);
14732               else if (GET_CODE (varloc) == SUBREG)
14733                 varloc = SUBREG_REG (varloc);
14734               else
14735                 break;
14736             }
14737           while (1);
14738           /* DW_OP_bit_size offset should be zero for register
14739              or implicit location descriptions and empty location
14740              descriptions, but for memory addresses needs big endian
14741              adjustment.  */
14742           if (MEM_P (varloc))
14743             {
14744               unsigned HOST_WIDE_INT memsize
14745                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14746               if (memsize != bitsize)
14747                 {
14748                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14749                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14750                     return NULL;
14751                   if (memsize < bitsize)
14752                     return NULL;
14753                   if (BITS_BIG_ENDIAN)
14754                     offset = memsize - bitsize;
14755                 }
14756             }
14757
14758           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14759           if (*descr_tail == NULL)
14760             return NULL;
14761           descr_tail = &(*descr_tail)->dw_loc_next;
14762         }
14763     }
14764
14765   /* If there were any non-empty expressions, add padding till the end of
14766      the decl.  */
14767   if (descr != NULL && decl_size != 0)
14768     {
14769       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14770       if (*descr_tail == NULL)
14771         return NULL;
14772     }
14773   return descr;
14774 }
14775
14776 /* Return the dwarf representation of the location list LOC_LIST of
14777    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14778    function.  */
14779
14780 static dw_loc_list_ref
14781 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14782 {
14783   const char *endname, *secname;
14784   rtx varloc;
14785   enum var_init_status initialized;
14786   struct var_loc_node *node;
14787   dw_loc_descr_ref descr;
14788   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14789   dw_loc_list_ref list = NULL;
14790   dw_loc_list_ref *listp = &list;
14791
14792   /* Now that we know what section we are using for a base,
14793      actually construct the list of locations.
14794      The first location information is what is passed to the
14795      function that creates the location list, and the remaining
14796      locations just get added on to that list.
14797      Note that we only know the start address for a location
14798      (IE location changes), so to build the range, we use
14799      the range [current location start, next location start].
14800      This means we have to special case the last node, and generate
14801      a range of [last location start, end of function label].  */
14802
14803   secname = secname_for_decl (decl);
14804
14805   for (node = loc_list->first; node; node = node->next)
14806     if (GET_CODE (node->loc) == EXPR_LIST
14807         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14808       {
14809         if (GET_CODE (node->loc) == EXPR_LIST)
14810           {
14811             /* This requires DW_OP_{,bit_}piece, which is not usable
14812                inside DWARF expressions.  */
14813             if (want_address != 2)
14814               continue;
14815             descr = dw_sra_loc_expr (decl, node->loc);
14816             if (descr == NULL)
14817               continue;
14818           }
14819         else
14820           {
14821             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14822             varloc = NOTE_VAR_LOCATION (node->loc);
14823             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14824           }
14825         if (descr)
14826           {
14827             /* The variable has a location between NODE->LABEL and
14828                NODE->NEXT->LABEL.  */
14829             if (node->next)
14830               endname = node->next->label;
14831             /* If the variable has a location at the last label
14832                it keeps its location until the end of function.  */
14833             else if (!current_function_decl)
14834               endname = text_end_label;
14835             else
14836               {
14837                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14838                                              current_function_funcdef_no);
14839                 endname = ggc_strdup (label_id);
14840               }
14841
14842             *listp = new_loc_list (descr, node->label, endname, secname);
14843             listp = &(*listp)->dw_loc_next;
14844           }
14845       }
14846
14847   /* Try to avoid the overhead of a location list emitting a location
14848      expression instead, but only if we didn't have more than one
14849      location entry in the first place.  If some entries were not
14850      representable, we don't want to pretend a single entry that was
14851      applies to the entire scope in which the variable is
14852      available.  */
14853   if (list && loc_list->first->next)
14854     gen_llsym (list);
14855
14856   return list;
14857 }
14858
14859 /* Return if the loc_list has only single element and thus can be represented
14860    as location description.   */
14861
14862 static bool
14863 single_element_loc_list_p (dw_loc_list_ref list)
14864 {
14865   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14866   return !list->ll_symbol;
14867 }
14868
14869 /* To each location in list LIST add loc descr REF.  */
14870
14871 static void
14872 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14873 {
14874   dw_loc_descr_ref copy;
14875   add_loc_descr (&list->expr, ref);
14876   list = list->dw_loc_next;
14877   while (list)
14878     {
14879       copy = ggc_alloc_dw_loc_descr_node ();
14880       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14881       add_loc_descr (&list->expr, copy);
14882       while (copy->dw_loc_next)
14883         {
14884           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14885           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14886           copy->dw_loc_next = new_copy;
14887           copy = new_copy;
14888         }
14889       list = list->dw_loc_next;
14890     }
14891 }
14892
14893 /* Given two lists RET and LIST
14894    produce location list that is result of adding expression in LIST
14895    to expression in RET on each possition in program.
14896    Might be destructive on both RET and LIST.
14897
14898    TODO: We handle only simple cases of RET or LIST having at most one
14899    element. General case would inolve sorting the lists in program order
14900    and merging them that will need some additional work.
14901    Adding that will improve quality of debug info especially for SRA-ed
14902    structures.  */
14903
14904 static void
14905 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14906 {
14907   if (!list)
14908     return;
14909   if (!*ret)
14910     {
14911       *ret = list;
14912       return;
14913     }
14914   if (!list->dw_loc_next)
14915     {
14916       add_loc_descr_to_each (*ret, list->expr);
14917       return;
14918     }
14919   if (!(*ret)->dw_loc_next)
14920     {
14921       add_loc_descr_to_each (list, (*ret)->expr);
14922       *ret = list;
14923       return;
14924     }
14925   expansion_failed (NULL_TREE, NULL_RTX,
14926                     "Don't know how to merge two non-trivial"
14927                     " location lists.\n");
14928   *ret = NULL;
14929   return;
14930 }
14931
14932 /* LOC is constant expression.  Try a luck, look it up in constant
14933    pool and return its loc_descr of its address.  */
14934
14935 static dw_loc_descr_ref
14936 cst_pool_loc_descr (tree loc)
14937 {
14938   /* Get an RTL for this, if something has been emitted.  */
14939   rtx rtl = lookup_constant_def (loc);
14940   enum machine_mode mode;
14941
14942   if (!rtl || !MEM_P (rtl))
14943     {
14944       gcc_assert (!rtl);
14945       return 0;
14946     }
14947   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14948
14949   /* TODO: We might get more coverage if we was actually delaying expansion
14950      of all expressions till end of compilation when constant pools are fully
14951      populated.  */
14952   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14953     {
14954       expansion_failed (loc, NULL_RTX,
14955                         "CST value in contant pool but not marked.");
14956       return 0;
14957     }
14958   mode = GET_MODE (rtl);
14959   rtl = XEXP (rtl, 0);
14960   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14961 }
14962
14963 /* Return dw_loc_list representing address of addr_expr LOC
14964    by looking for innder INDIRECT_REF expression and turing it
14965    into simple arithmetics.  */
14966
14967 static dw_loc_list_ref
14968 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14969 {
14970   tree obj, offset;
14971   HOST_WIDE_INT bitsize, bitpos, bytepos;
14972   enum machine_mode mode;
14973   int volatilep;
14974   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14975   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14976
14977   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14978                              &bitsize, &bitpos, &offset, &mode,
14979                              &unsignedp, &volatilep, false);
14980   STRIP_NOPS (obj);
14981   if (bitpos % BITS_PER_UNIT)
14982     {
14983       expansion_failed (loc, NULL_RTX, "bitfield access");
14984       return 0;
14985     }
14986   if (!INDIRECT_REF_P (obj))
14987     {
14988       expansion_failed (obj,
14989                         NULL_RTX, "no indirect ref in inner refrence");
14990       return 0;
14991     }
14992   if (!offset && !bitpos)
14993     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14994   else if (toplev
14995            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14996            && (dwarf_version >= 4 || !dwarf_strict))
14997     {
14998       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14999       if (!list_ret)
15000         return 0;
15001       if (offset)
15002         {
15003           /* Variable offset.  */
15004           list_ret1 = loc_list_from_tree (offset, 0);
15005           if (list_ret1 == 0)
15006             return 0;
15007           add_loc_list (&list_ret, list_ret1);
15008           if (!list_ret)
15009             return 0;
15010           add_loc_descr_to_each (list_ret,
15011                                  new_loc_descr (DW_OP_plus, 0, 0));
15012         }
15013       bytepos = bitpos / BITS_PER_UNIT;
15014       if (bytepos > 0)
15015         add_loc_descr_to_each (list_ret,
15016                                new_loc_descr (DW_OP_plus_uconst,
15017                                               bytepos, 0));
15018       else if (bytepos < 0)
15019         loc_list_plus_const (list_ret, bytepos);
15020       add_loc_descr_to_each (list_ret,
15021                              new_loc_descr (DW_OP_stack_value, 0, 0));
15022     }
15023   return list_ret;
15024 }
15025
15026
15027 /* Generate Dwarf location list representing LOC.
15028    If WANT_ADDRESS is false, expression computing LOC will be computed
15029    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15030    if WANT_ADDRESS is 2, expression computing address useable in location
15031      will be returned (i.e. DW_OP_reg can be used
15032      to refer to register values).  */
15033
15034 static dw_loc_list_ref
15035 loc_list_from_tree (tree loc, int want_address)
15036 {
15037   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15038   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15039   int have_address = 0;
15040   enum dwarf_location_atom op;
15041
15042   /* ??? Most of the time we do not take proper care for sign/zero
15043      extending the values properly.  Hopefully this won't be a real
15044      problem...  */
15045
15046   switch (TREE_CODE (loc))
15047     {
15048     case ERROR_MARK:
15049       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15050       return 0;
15051
15052     case PLACEHOLDER_EXPR:
15053       /* This case involves extracting fields from an object to determine the
15054          position of other fields.  We don't try to encode this here.  The
15055          only user of this is Ada, which encodes the needed information using
15056          the names of types.  */
15057       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15058       return 0;
15059
15060     case CALL_EXPR:
15061       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15062       /* There are no opcodes for these operations.  */
15063       return 0;
15064
15065     case PREINCREMENT_EXPR:
15066     case PREDECREMENT_EXPR:
15067     case POSTINCREMENT_EXPR:
15068     case POSTDECREMENT_EXPR:
15069       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15070       /* There are no opcodes for these operations.  */
15071       return 0;
15072
15073     case ADDR_EXPR:
15074       /* If we already want an address, see if there is INDIRECT_REF inside
15075          e.g. for &this->field.  */
15076       if (want_address)
15077         {
15078           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15079                        (loc, want_address == 2);
15080           if (list_ret)
15081             have_address = 1;
15082           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15083                    && (ret = cst_pool_loc_descr (loc)))
15084             have_address = 1;
15085         }
15086         /* Otherwise, process the argument and look for the address.  */
15087       if (!list_ret && !ret)
15088         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15089       else
15090         {
15091           if (want_address)
15092             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15093           return NULL;
15094         }
15095       break;
15096
15097     case VAR_DECL:
15098       if (DECL_THREAD_LOCAL_P (loc))
15099         {
15100           rtx rtl;
15101           enum dwarf_location_atom first_op;
15102           enum dwarf_location_atom second_op;
15103           bool dtprel = false;
15104
15105           if (targetm.have_tls)
15106             {
15107               /* If this is not defined, we have no way to emit the
15108                  data.  */
15109               if (!targetm.asm_out.output_dwarf_dtprel)
15110                 return 0;
15111
15112                /* The way DW_OP_GNU_push_tls_address is specified, we
15113                   can only look up addresses of objects in the current
15114                   module.  We used DW_OP_addr as first op, but that's
15115                   wrong, because DW_OP_addr is relocated by the debug
15116                   info consumer, while DW_OP_GNU_push_tls_address
15117                   operand shouldn't be.  */
15118               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15119                 return 0;
15120               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15121               dtprel = true;
15122               second_op = DW_OP_GNU_push_tls_address;
15123             }
15124           else
15125             {
15126               if (!targetm.emutls.debug_form_tls_address
15127                   || !(dwarf_version >= 3 || !dwarf_strict))
15128                 return 0;
15129               /* We stuffed the control variable into the DECL_VALUE_EXPR
15130                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15131                  no longer appear in gimple code.  We used the control
15132                  variable in specific so that we could pick it up here.  */
15133               loc = DECL_VALUE_EXPR (loc);
15134               first_op = DW_OP_addr;
15135               second_op = DW_OP_form_tls_address;
15136             }
15137
15138           rtl = rtl_for_decl_location (loc);
15139           if (rtl == NULL_RTX)
15140             return 0;
15141
15142           if (!MEM_P (rtl))
15143             return 0;
15144           rtl = XEXP (rtl, 0);
15145           if (! CONSTANT_P (rtl))
15146             return 0;
15147
15148           ret = new_loc_descr (first_op, 0, 0);
15149           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15150           ret->dw_loc_oprnd1.v.val_addr = rtl;
15151           ret->dtprel = dtprel;
15152
15153           ret1 = new_loc_descr (second_op, 0, 0);
15154           add_loc_descr (&ret, ret1);
15155
15156           have_address = 1;
15157           break;
15158         }
15159       /* FALLTHRU */
15160
15161     case PARM_DECL:
15162       if (DECL_HAS_VALUE_EXPR_P (loc))
15163         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15164                                    want_address);
15165       /* FALLTHRU */
15166
15167     case RESULT_DECL:
15168     case FUNCTION_DECL:
15169       {
15170         rtx rtl;
15171         var_loc_list *loc_list = lookup_decl_loc (loc);
15172
15173         if (loc_list && loc_list->first)
15174           {
15175             list_ret = dw_loc_list (loc_list, loc, want_address);
15176             have_address = want_address != 0;
15177             break;
15178           }
15179         rtl = rtl_for_decl_location (loc);
15180         if (rtl == NULL_RTX)
15181           {
15182             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15183             return 0;
15184           }
15185         else if (CONST_INT_P (rtl))
15186           {
15187             HOST_WIDE_INT val = INTVAL (rtl);
15188             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15189               val &= GET_MODE_MASK (DECL_MODE (loc));
15190             ret = int_loc_descriptor (val);
15191           }
15192         else if (GET_CODE (rtl) == CONST_STRING)
15193           {
15194             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15195             return 0;
15196           }
15197         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15198           {
15199             ret = new_loc_descr (DW_OP_addr, 0, 0);
15200             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15201             ret->dw_loc_oprnd1.v.val_addr = rtl;
15202           }
15203         else
15204           {
15205             enum machine_mode mode;
15206
15207             /* Certain constructs can only be represented at top-level.  */
15208             if (want_address == 2)
15209               {
15210                 ret = loc_descriptor (rtl, VOIDmode,
15211                                       VAR_INIT_STATUS_INITIALIZED);
15212                 have_address = 1;
15213               }
15214             else
15215               {
15216                 mode = GET_MODE (rtl);
15217                 if (MEM_P (rtl))
15218                   {
15219                     rtl = XEXP (rtl, 0);
15220                     have_address = 1;
15221                   }
15222                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15223               }
15224             if (!ret)
15225               expansion_failed (loc, rtl,
15226                                 "failed to produce loc descriptor for rtl");
15227           }
15228       }
15229       break;
15230
15231     case MEM_REF:
15232       /* ??? FIXME.  */
15233       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15234         return 0;
15235       /* Fallthru.  */
15236     case INDIRECT_REF:
15237     case MISALIGNED_INDIRECT_REF:
15238       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15239       have_address = 1;
15240       break;
15241
15242     case COMPOUND_EXPR:
15243       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15244
15245     CASE_CONVERT:
15246     case VIEW_CONVERT_EXPR:
15247     case SAVE_EXPR:
15248     case MODIFY_EXPR:
15249       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15250
15251     case COMPONENT_REF:
15252     case BIT_FIELD_REF:
15253     case ARRAY_REF:
15254     case ARRAY_RANGE_REF:
15255     case REALPART_EXPR:
15256     case IMAGPART_EXPR:
15257       {
15258         tree obj, offset;
15259         HOST_WIDE_INT bitsize, bitpos, bytepos;
15260         enum machine_mode mode;
15261         int volatilep;
15262         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15263
15264         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15265                                    &unsignedp, &volatilep, false);
15266
15267         gcc_assert (obj != loc);
15268
15269         list_ret = loc_list_from_tree (obj,
15270                                        want_address == 2
15271                                        && !bitpos && !offset ? 2 : 1);
15272         /* TODO: We can extract value of the small expression via shifting even
15273            for nonzero bitpos.  */
15274         if (list_ret == 0)
15275           return 0;
15276         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15277           {
15278             expansion_failed (loc, NULL_RTX,
15279                               "bitfield access");
15280             return 0;
15281           }
15282
15283         if (offset != NULL_TREE)
15284           {
15285             /* Variable offset.  */
15286             list_ret1 = loc_list_from_tree (offset, 0);
15287             if (list_ret1 == 0)
15288               return 0;
15289             add_loc_list (&list_ret, list_ret1);
15290             if (!list_ret)
15291               return 0;
15292             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15293           }
15294
15295         bytepos = bitpos / BITS_PER_UNIT;
15296         if (bytepos > 0)
15297           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15298         else if (bytepos < 0)
15299           loc_list_plus_const (list_ret, bytepos);
15300
15301         have_address = 1;
15302         break;
15303       }
15304
15305     case INTEGER_CST:
15306       if ((want_address || !host_integerp (loc, 0))
15307           && (ret = cst_pool_loc_descr (loc)))
15308         have_address = 1;
15309       else if (want_address == 2
15310                && host_integerp (loc, 0)
15311                && (ret = address_of_int_loc_descriptor
15312                            (int_size_in_bytes (TREE_TYPE (loc)),
15313                             tree_low_cst (loc, 0))))
15314         have_address = 1;
15315       else if (host_integerp (loc, 0))
15316         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15317       else
15318         {
15319           expansion_failed (loc, NULL_RTX,
15320                             "Integer operand is not host integer");
15321           return 0;
15322         }
15323       break;
15324
15325     case CONSTRUCTOR:
15326     case REAL_CST:
15327     case STRING_CST:
15328     case COMPLEX_CST:
15329       if ((ret = cst_pool_loc_descr (loc)))
15330         have_address = 1;
15331       else
15332       /* We can construct small constants here using int_loc_descriptor.  */
15333         expansion_failed (loc, NULL_RTX,
15334                           "constructor or constant not in constant pool");
15335       break;
15336
15337     case TRUTH_AND_EXPR:
15338     case TRUTH_ANDIF_EXPR:
15339     case BIT_AND_EXPR:
15340       op = DW_OP_and;
15341       goto do_binop;
15342
15343     case TRUTH_XOR_EXPR:
15344     case BIT_XOR_EXPR:
15345       op = DW_OP_xor;
15346       goto do_binop;
15347
15348     case TRUTH_OR_EXPR:
15349     case TRUTH_ORIF_EXPR:
15350     case BIT_IOR_EXPR:
15351       op = DW_OP_or;
15352       goto do_binop;
15353
15354     case FLOOR_DIV_EXPR:
15355     case CEIL_DIV_EXPR:
15356     case ROUND_DIV_EXPR:
15357     case TRUNC_DIV_EXPR:
15358       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15359         return 0;
15360       op = DW_OP_div;
15361       goto do_binop;
15362
15363     case MINUS_EXPR:
15364       op = DW_OP_minus;
15365       goto do_binop;
15366
15367     case FLOOR_MOD_EXPR:
15368     case CEIL_MOD_EXPR:
15369     case ROUND_MOD_EXPR:
15370     case TRUNC_MOD_EXPR:
15371       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15372         {
15373           op = DW_OP_mod;
15374           goto do_binop;
15375         }
15376       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15377       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15378       if (list_ret == 0 || list_ret1 == 0)
15379         return 0;
15380
15381       add_loc_list (&list_ret, list_ret1);
15382       if (list_ret == 0)
15383         return 0;
15384       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15385       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15386       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15387       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15388       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15389       break;
15390
15391     case MULT_EXPR:
15392       op = DW_OP_mul;
15393       goto do_binop;
15394
15395     case LSHIFT_EXPR:
15396       op = DW_OP_shl;
15397       goto do_binop;
15398
15399     case RSHIFT_EXPR:
15400       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15401       goto do_binop;
15402
15403     case POINTER_PLUS_EXPR:
15404     case PLUS_EXPR:
15405       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15406         {
15407           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15408           if (list_ret == 0)
15409             return 0;
15410
15411           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15412           break;
15413         }
15414
15415       op = DW_OP_plus;
15416       goto do_binop;
15417
15418     case LE_EXPR:
15419       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15420         return 0;
15421
15422       op = DW_OP_le;
15423       goto do_binop;
15424
15425     case GE_EXPR:
15426       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15427         return 0;
15428
15429       op = DW_OP_ge;
15430       goto do_binop;
15431
15432     case LT_EXPR:
15433       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15434         return 0;
15435
15436       op = DW_OP_lt;
15437       goto do_binop;
15438
15439     case GT_EXPR:
15440       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15441         return 0;
15442
15443       op = DW_OP_gt;
15444       goto do_binop;
15445
15446     case EQ_EXPR:
15447       op = DW_OP_eq;
15448       goto do_binop;
15449
15450     case NE_EXPR:
15451       op = DW_OP_ne;
15452       goto do_binop;
15453
15454     do_binop:
15455       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15456       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15457       if (list_ret == 0 || list_ret1 == 0)
15458         return 0;
15459
15460       add_loc_list (&list_ret, list_ret1);
15461       if (list_ret == 0)
15462         return 0;
15463       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15464       break;
15465
15466     case TRUTH_NOT_EXPR:
15467     case BIT_NOT_EXPR:
15468       op = DW_OP_not;
15469       goto do_unop;
15470
15471     case ABS_EXPR:
15472       op = DW_OP_abs;
15473       goto do_unop;
15474
15475     case NEGATE_EXPR:
15476       op = DW_OP_neg;
15477       goto do_unop;
15478
15479     do_unop:
15480       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15481       if (list_ret == 0)
15482         return 0;
15483
15484       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15485       break;
15486
15487     case MIN_EXPR:
15488     case MAX_EXPR:
15489       {
15490         const enum tree_code code =
15491           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15492
15493         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15494                       build2 (code, integer_type_node,
15495                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15496                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15497       }
15498
15499       /* ... fall through ...  */
15500
15501     case COND_EXPR:
15502       {
15503         dw_loc_descr_ref lhs
15504           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15505         dw_loc_list_ref rhs
15506           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15507         dw_loc_descr_ref bra_node, jump_node, tmp;
15508
15509         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15510         if (list_ret == 0 || lhs == 0 || rhs == 0)
15511           return 0;
15512
15513         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15514         add_loc_descr_to_each (list_ret, bra_node);
15515
15516         add_loc_list (&list_ret, rhs);
15517         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15518         add_loc_descr_to_each (list_ret, jump_node);
15519
15520         add_loc_descr_to_each (list_ret, lhs);
15521         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15522         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15523
15524         /* ??? Need a node to point the skip at.  Use a nop.  */
15525         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15526         add_loc_descr_to_each (list_ret, tmp);
15527         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15528         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15529       }
15530       break;
15531
15532     case FIX_TRUNC_EXPR:
15533       return 0;
15534
15535     default:
15536       /* Leave front-end specific codes as simply unknown.  This comes
15537          up, for instance, with the C STMT_EXPR.  */
15538       if ((unsigned int) TREE_CODE (loc)
15539           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15540         {
15541           expansion_failed (loc, NULL_RTX,
15542                             "language specific tree node");
15543           return 0;
15544         }
15545
15546 #ifdef ENABLE_CHECKING
15547       /* Otherwise this is a generic code; we should just lists all of
15548          these explicitly.  We forgot one.  */
15549       gcc_unreachable ();
15550 #else
15551       /* In a release build, we want to degrade gracefully: better to
15552          generate incomplete debugging information than to crash.  */
15553       return NULL;
15554 #endif
15555     }
15556
15557   if (!ret && !list_ret)
15558     return 0;
15559
15560   if (want_address == 2 && !have_address
15561       && (dwarf_version >= 4 || !dwarf_strict))
15562     {
15563       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15564         {
15565           expansion_failed (loc, NULL_RTX,
15566                             "DWARF address size mismatch");
15567           return 0;
15568         }
15569       if (ret)
15570         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15571       else
15572         add_loc_descr_to_each (list_ret,
15573                                new_loc_descr (DW_OP_stack_value, 0, 0));
15574       have_address = 1;
15575     }
15576   /* Show if we can't fill the request for an address.  */
15577   if (want_address && !have_address)
15578     {
15579       expansion_failed (loc, NULL_RTX,
15580                         "Want address and only have value");
15581       return 0;
15582     }
15583
15584   gcc_assert (!ret || !list_ret);
15585
15586   /* If we've got an address and don't want one, dereference.  */
15587   if (!want_address && have_address)
15588     {
15589       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15590
15591       if (size > DWARF2_ADDR_SIZE || size == -1)
15592         {
15593           expansion_failed (loc, NULL_RTX,
15594                             "DWARF address size mismatch");
15595           return 0;
15596         }
15597       else if (size == DWARF2_ADDR_SIZE)
15598         op = DW_OP_deref;
15599       else
15600         op = DW_OP_deref_size;
15601
15602       if (ret)
15603         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15604       else
15605         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15606     }
15607   if (ret)
15608     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15609
15610   return list_ret;
15611 }
15612
15613 /* Same as above but return only single location expression.  */
15614 static dw_loc_descr_ref
15615 loc_descriptor_from_tree (tree loc, int want_address)
15616 {
15617   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15618   if (!ret)
15619     return NULL;
15620   if (ret->dw_loc_next)
15621     {
15622       expansion_failed (loc, NULL_RTX,
15623                         "Location list where only loc descriptor needed");
15624       return NULL;
15625     }
15626   return ret->expr;
15627 }
15628
15629 /* Given a value, round it up to the lowest multiple of `boundary'
15630    which is not less than the value itself.  */
15631
15632 static inline HOST_WIDE_INT
15633 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15634 {
15635   return (((value + boundary - 1) / boundary) * boundary);
15636 }
15637
15638 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15639    pointer to the declared type for the relevant field variable, or return
15640    `integer_type_node' if the given node turns out to be an
15641    ERROR_MARK node.  */
15642
15643 static inline tree
15644 field_type (const_tree decl)
15645 {
15646   tree type;
15647
15648   if (TREE_CODE (decl) == ERROR_MARK)
15649     return integer_type_node;
15650
15651   type = DECL_BIT_FIELD_TYPE (decl);
15652   if (type == NULL_TREE)
15653     type = TREE_TYPE (decl);
15654
15655   return type;
15656 }
15657
15658 /* Given a pointer to a tree node, return the alignment in bits for
15659    it, or else return BITS_PER_WORD if the node actually turns out to
15660    be an ERROR_MARK node.  */
15661
15662 static inline unsigned
15663 simple_type_align_in_bits (const_tree type)
15664 {
15665   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15666 }
15667
15668 static inline unsigned
15669 simple_decl_align_in_bits (const_tree decl)
15670 {
15671   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15672 }
15673
15674 /* Return the result of rounding T up to ALIGN.  */
15675
15676 static inline double_int
15677 round_up_to_align (double_int t, unsigned int align)
15678 {
15679   double_int alignd = uhwi_to_double_int (align);
15680   t = double_int_add (t, alignd);
15681   t = double_int_add (t, double_int_minus_one);
15682   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15683   t = double_int_mul (t, alignd);
15684   return t;
15685 }
15686
15687 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15688    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15689    or return 0 if we are unable to determine what that offset is, either
15690    because the argument turns out to be a pointer to an ERROR_MARK node, or
15691    because the offset is actually variable.  (We can't handle the latter case
15692    just yet).  */
15693
15694 static HOST_WIDE_INT
15695 field_byte_offset (const_tree decl)
15696 {
15697   double_int object_offset_in_bits;
15698   double_int object_offset_in_bytes;
15699   double_int bitpos_int;
15700
15701   if (TREE_CODE (decl) == ERROR_MARK)
15702     return 0;
15703
15704   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15705
15706   /* We cannot yet cope with fields whose positions are variable, so
15707      for now, when we see such things, we simply return 0.  Someday, we may
15708      be able to handle such cases, but it will be damn difficult.  */
15709   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15710     return 0;
15711
15712   bitpos_int = tree_to_double_int (bit_position (decl));
15713
15714 #ifdef PCC_BITFIELD_TYPE_MATTERS
15715   if (PCC_BITFIELD_TYPE_MATTERS)
15716     {
15717       tree type;
15718       tree field_size_tree;
15719       double_int deepest_bitpos;
15720       double_int field_size_in_bits;
15721       unsigned int type_align_in_bits;
15722       unsigned int decl_align_in_bits;
15723       double_int type_size_in_bits;
15724
15725       type = field_type (decl);
15726       type_size_in_bits = double_int_type_size_in_bits (type);
15727       type_align_in_bits = simple_type_align_in_bits (type);
15728
15729       field_size_tree = DECL_SIZE (decl);
15730
15731       /* The size could be unspecified if there was an error, or for
15732          a flexible array member.  */
15733       if (!field_size_tree)
15734         field_size_tree = bitsize_zero_node;
15735
15736       /* If the size of the field is not constant, use the type size.  */
15737       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15738         field_size_in_bits = tree_to_double_int (field_size_tree);
15739       else
15740         field_size_in_bits = type_size_in_bits;
15741
15742       decl_align_in_bits = simple_decl_align_in_bits (decl);
15743
15744       /* The GCC front-end doesn't make any attempt to keep track of the
15745          starting bit offset (relative to the start of the containing
15746          structure type) of the hypothetical "containing object" for a
15747          bit-field.  Thus, when computing the byte offset value for the
15748          start of the "containing object" of a bit-field, we must deduce
15749          this information on our own. This can be rather tricky to do in
15750          some cases.  For example, handling the following structure type
15751          definition when compiling for an i386/i486 target (which only
15752          aligns long long's to 32-bit boundaries) can be very tricky:
15753
15754          struct S { int field1; long long field2:31; };
15755
15756          Fortunately, there is a simple rule-of-thumb which can be used
15757          in such cases.  When compiling for an i386/i486, GCC will
15758          allocate 8 bytes for the structure shown above.  It decides to
15759          do this based upon one simple rule for bit-field allocation.
15760          GCC allocates each "containing object" for each bit-field at
15761          the first (i.e. lowest addressed) legitimate alignment boundary
15762          (based upon the required minimum alignment for the declared
15763          type of the field) which it can possibly use, subject to the
15764          condition that there is still enough available space remaining
15765          in the containing object (when allocated at the selected point)
15766          to fully accommodate all of the bits of the bit-field itself.
15767
15768          This simple rule makes it obvious why GCC allocates 8 bytes for
15769          each object of the structure type shown above.  When looking
15770          for a place to allocate the "containing object" for `field2',
15771          the compiler simply tries to allocate a 64-bit "containing
15772          object" at each successive 32-bit boundary (starting at zero)
15773          until it finds a place to allocate that 64- bit field such that
15774          at least 31 contiguous (and previously unallocated) bits remain
15775          within that selected 64 bit field.  (As it turns out, for the
15776          example above, the compiler finds it is OK to allocate the
15777          "containing object" 64-bit field at bit-offset zero within the
15778          structure type.)
15779
15780          Here we attempt to work backwards from the limited set of facts
15781          we're given, and we try to deduce from those facts, where GCC
15782          must have believed that the containing object started (within
15783          the structure type). The value we deduce is then used (by the
15784          callers of this routine) to generate DW_AT_location and
15785          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15786          the case of DW_AT_location, regular fields as well).  */
15787
15788       /* Figure out the bit-distance from the start of the structure to
15789          the "deepest" bit of the bit-field.  */
15790       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15791
15792       /* This is the tricky part.  Use some fancy footwork to deduce
15793          where the lowest addressed bit of the containing object must
15794          be.  */
15795       object_offset_in_bits
15796         = double_int_sub (deepest_bitpos, type_size_in_bits);
15797
15798       /* Round up to type_align by default.  This works best for
15799          bitfields.  */
15800       object_offset_in_bits
15801         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15802
15803       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15804         {
15805           object_offset_in_bits
15806             = double_int_sub (deepest_bitpos, type_size_in_bits);
15807
15808           /* Round up to decl_align instead.  */
15809           object_offset_in_bits
15810             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15811         }
15812     }
15813   else
15814 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15815     object_offset_in_bits = bitpos_int;
15816
15817   object_offset_in_bytes
15818     = double_int_div (object_offset_in_bits,
15819                       uhwi_to_double_int (BITS_PER_UNIT), true,
15820                       TRUNC_DIV_EXPR);
15821   return double_int_to_shwi (object_offset_in_bytes);
15822 }
15823 \f
15824 /* The following routines define various Dwarf attributes and any data
15825    associated with them.  */
15826
15827 /* Add a location description attribute value to a DIE.
15828
15829    This emits location attributes suitable for whole variables and
15830    whole parameters.  Note that the location attributes for struct fields are
15831    generated by the routine `data_member_location_attribute' below.  */
15832
15833 static inline void
15834 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15835                              dw_loc_list_ref descr)
15836 {
15837   if (descr == 0)
15838     return;
15839   if (single_element_loc_list_p (descr))
15840     add_AT_loc (die, attr_kind, descr->expr);
15841   else
15842     add_AT_loc_list (die, attr_kind, descr);
15843 }
15844
15845 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15846
15847 static void
15848 add_accessibility_attribute (dw_die_ref die, tree decl)
15849 {
15850   if (TREE_PROTECTED (decl))
15851     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15852   else if (TREE_PRIVATE (decl))
15853     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15854 }
15855
15856 /* Attach the specialized form of location attribute used for data members of
15857    struct and union types.  In the special case of a FIELD_DECL node which
15858    represents a bit-field, the "offset" part of this special location
15859    descriptor must indicate the distance in bytes from the lowest-addressed
15860    byte of the containing struct or union type to the lowest-addressed byte of
15861    the "containing object" for the bit-field.  (See the `field_byte_offset'
15862    function above).
15863
15864    For any given bit-field, the "containing object" is a hypothetical object
15865    (of some integral or enum type) within which the given bit-field lives.  The
15866    type of this hypothetical "containing object" is always the same as the
15867    declared type of the individual bit-field itself (for GCC anyway... the
15868    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15869    bytes) of the hypothetical "containing object" which will be given in the
15870    DW_AT_byte_size attribute for this bit-field.  (See the
15871    `byte_size_attribute' function below.)  It is also used when calculating the
15872    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15873    function below.)  */
15874
15875 static void
15876 add_data_member_location_attribute (dw_die_ref die, tree decl)
15877 {
15878   HOST_WIDE_INT offset;
15879   dw_loc_descr_ref loc_descr = 0;
15880
15881   if (TREE_CODE (decl) == TREE_BINFO)
15882     {
15883       /* We're working on the TAG_inheritance for a base class.  */
15884       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15885         {
15886           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15887              aren't at a fixed offset from all (sub)objects of the same
15888              type.  We need to extract the appropriate offset from our
15889              vtable.  The following dwarf expression means
15890
15891                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15892
15893              This is specific to the V3 ABI, of course.  */
15894
15895           dw_loc_descr_ref tmp;
15896
15897           /* Make a copy of the object address.  */
15898           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15899           add_loc_descr (&loc_descr, tmp);
15900
15901           /* Extract the vtable address.  */
15902           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15903           add_loc_descr (&loc_descr, tmp);
15904
15905           /* Calculate the address of the offset.  */
15906           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15907           gcc_assert (offset < 0);
15908
15909           tmp = int_loc_descriptor (-offset);
15910           add_loc_descr (&loc_descr, tmp);
15911           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15912           add_loc_descr (&loc_descr, tmp);
15913
15914           /* Extract the offset.  */
15915           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15916           add_loc_descr (&loc_descr, tmp);
15917
15918           /* Add it to the object address.  */
15919           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15920           add_loc_descr (&loc_descr, tmp);
15921         }
15922       else
15923         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15924     }
15925   else
15926     offset = field_byte_offset (decl);
15927
15928   if (! loc_descr)
15929     {
15930       if (dwarf_version > 2)
15931         {
15932           /* Don't need to output a location expression, just the constant. */
15933           if (offset < 0)
15934             add_AT_int (die, DW_AT_data_member_location, offset);
15935           else
15936             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15937           return;
15938         }
15939       else
15940         {
15941           enum dwarf_location_atom op;
15942
15943           /* The DWARF2 standard says that we should assume that the structure
15944              address is already on the stack, so we can specify a structure
15945              field address by using DW_OP_plus_uconst.  */
15946
15947 #ifdef MIPS_DEBUGGING_INFO
15948           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15949              operator correctly.  It works only if we leave the offset on the
15950              stack.  */
15951           op = DW_OP_constu;
15952 #else
15953           op = DW_OP_plus_uconst;
15954 #endif
15955
15956           loc_descr = new_loc_descr (op, offset, 0);
15957         }
15958     }
15959
15960   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15961 }
15962
15963 /* Writes integer values to dw_vec_const array.  */
15964
15965 static void
15966 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15967 {
15968   while (size != 0)
15969     {
15970       *dest++ = val & 0xff;
15971       val >>= 8;
15972       --size;
15973     }
15974 }
15975
15976 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15977
15978 static HOST_WIDE_INT
15979 extract_int (const unsigned char *src, unsigned int size)
15980 {
15981   HOST_WIDE_INT val = 0;
15982
15983   src += size;
15984   while (size != 0)
15985     {
15986       val <<= 8;
15987       val |= *--src & 0xff;
15988       --size;
15989     }
15990   return val;
15991 }
15992
15993 /* Writes double_int values to dw_vec_const array.  */
15994
15995 static void
15996 insert_double (double_int val, unsigned char *dest)
15997 {
15998   unsigned char *p0 = dest;
15999   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16000
16001   if (WORDS_BIG_ENDIAN)
16002     {
16003       p0 = p1;
16004       p1 = dest;
16005     }
16006
16007   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16008   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16009 }
16010
16011 /* Writes floating point values to dw_vec_const array.  */
16012
16013 static void
16014 insert_float (const_rtx rtl, unsigned char *array)
16015 {
16016   REAL_VALUE_TYPE rv;
16017   long val[4];
16018   int i;
16019
16020   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16021   real_to_target (val, &rv, GET_MODE (rtl));
16022
16023   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16024   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16025     {
16026       insert_int (val[i], 4, array);
16027       array += 4;
16028     }
16029 }
16030
16031 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16032    does not have a "location" either in memory or in a register.  These
16033    things can arise in GNU C when a constant is passed as an actual parameter
16034    to an inlined function.  They can also arise in C++ where declared
16035    constants do not necessarily get memory "homes".  */
16036
16037 static bool
16038 add_const_value_attribute (dw_die_ref die, rtx rtl)
16039 {
16040   switch (GET_CODE (rtl))
16041     {
16042     case CONST_INT:
16043       {
16044         HOST_WIDE_INT val = INTVAL (rtl);
16045
16046         if (val < 0)
16047           add_AT_int (die, DW_AT_const_value, val);
16048         else
16049           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16050       }
16051       return true;
16052
16053     case CONST_DOUBLE:
16054       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16055          floating-point constant.  A CONST_DOUBLE is used whenever the
16056          constant requires more than one word in order to be adequately
16057          represented.  */
16058       {
16059         enum machine_mode mode = GET_MODE (rtl);
16060
16061         if (SCALAR_FLOAT_MODE_P (mode))
16062           {
16063             unsigned int length = GET_MODE_SIZE (mode);
16064             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16065
16066             insert_float (rtl, array);
16067             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16068           }
16069         else
16070           add_AT_double (die, DW_AT_const_value,
16071                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16072       }
16073       return true;
16074
16075     case CONST_VECTOR:
16076       {
16077         enum machine_mode mode = GET_MODE (rtl);
16078         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16079         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16080         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16081           (length * elt_size);
16082         unsigned int i;
16083         unsigned char *p;
16084
16085         switch (GET_MODE_CLASS (mode))
16086           {
16087           case MODE_VECTOR_INT:
16088             for (i = 0, p = array; i < length; i++, p += elt_size)
16089               {
16090                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16091                 double_int val = rtx_to_double_int (elt);
16092
16093                 if (elt_size <= sizeof (HOST_WIDE_INT))
16094                   insert_int (double_int_to_shwi (val), elt_size, p);
16095                 else
16096                   {
16097                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16098                     insert_double (val, p);
16099                   }
16100               }
16101             break;
16102
16103           case MODE_VECTOR_FLOAT:
16104             for (i = 0, p = array; i < length; i++, p += elt_size)
16105               {
16106                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16107                 insert_float (elt, p);
16108               }
16109             break;
16110
16111           default:
16112             gcc_unreachable ();
16113           }
16114
16115         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16116       }
16117       return true;
16118
16119     case CONST_STRING:
16120       if (dwarf_version >= 4 || !dwarf_strict)
16121         {
16122           dw_loc_descr_ref loc_result;
16123           resolve_one_addr (&rtl, NULL);
16124         rtl_addr:
16125           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16126           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16127           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16128           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16129           add_AT_loc (die, DW_AT_location, loc_result);
16130           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16131           return true;
16132         }
16133       return false;
16134
16135     case CONST:
16136       if (CONSTANT_P (XEXP (rtl, 0)))
16137         return add_const_value_attribute (die, XEXP (rtl, 0));
16138       /* FALLTHROUGH */
16139     case SYMBOL_REF:
16140       if (!const_ok_for_output (rtl))
16141         return false;
16142     case LABEL_REF:
16143       if (dwarf_version >= 4 || !dwarf_strict)
16144         goto rtl_addr;
16145       return false;
16146
16147     case PLUS:
16148       /* In cases where an inlined instance of an inline function is passed
16149          the address of an `auto' variable (which is local to the caller) we
16150          can get a situation where the DECL_RTL of the artificial local
16151          variable (for the inlining) which acts as a stand-in for the
16152          corresponding formal parameter (of the inline function) will look
16153          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16154          exactly a compile-time constant expression, but it isn't the address
16155          of the (artificial) local variable either.  Rather, it represents the
16156          *value* which the artificial local variable always has during its
16157          lifetime.  We currently have no way to represent such quasi-constant
16158          values in Dwarf, so for now we just punt and generate nothing.  */
16159       return false;
16160
16161     case HIGH:
16162     case CONST_FIXED:
16163       return false;
16164
16165     case MEM:
16166       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16167           && MEM_READONLY_P (rtl)
16168           && GET_MODE (rtl) == BLKmode)
16169         {
16170           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16171           return true;
16172         }
16173       return false;
16174
16175     default:
16176       /* No other kinds of rtx should be possible here.  */
16177       gcc_unreachable ();
16178     }
16179   return false;
16180 }
16181
16182 /* Determine whether the evaluation of EXPR references any variables
16183    or functions which aren't otherwise used (and therefore may not be
16184    output).  */
16185 static tree
16186 reference_to_unused (tree * tp, int * walk_subtrees,
16187                      void * data ATTRIBUTE_UNUSED)
16188 {
16189   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16190     *walk_subtrees = 0;
16191
16192   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16193       && ! TREE_ASM_WRITTEN (*tp))
16194     return *tp;
16195   /* ???  The C++ FE emits debug information for using decls, so
16196      putting gcc_unreachable here falls over.  See PR31899.  For now
16197      be conservative.  */
16198   else if (!cgraph_global_info_ready
16199            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16200     return *tp;
16201   else if (TREE_CODE (*tp) == VAR_DECL)
16202     {
16203       struct varpool_node *node = varpool_get_node (*tp);
16204       if (!node || !node->needed)
16205         return *tp;
16206     }
16207   else if (TREE_CODE (*tp) == FUNCTION_DECL
16208            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16209     {
16210       /* The call graph machinery must have finished analyzing,
16211          optimizing and gimplifying the CU by now.
16212          So if *TP has no call graph node associated
16213          to it, it means *TP will not be emitted.  */
16214       if (!cgraph_get_node (*tp))
16215         return *tp;
16216     }
16217   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16218     return *tp;
16219
16220   return NULL_TREE;
16221 }
16222
16223 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16224    for use in a later add_const_value_attribute call.  */
16225
16226 static rtx
16227 rtl_for_decl_init (tree init, tree type)
16228 {
16229   rtx rtl = NULL_RTX;
16230
16231   /* If a variable is initialized with a string constant without embedded
16232      zeros, build CONST_STRING.  */
16233   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16234     {
16235       tree enttype = TREE_TYPE (type);
16236       tree domain = TYPE_DOMAIN (type);
16237       enum machine_mode mode = TYPE_MODE (enttype);
16238
16239       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16240           && domain
16241           && integer_zerop (TYPE_MIN_VALUE (domain))
16242           && compare_tree_int (TYPE_MAX_VALUE (domain),
16243                                TREE_STRING_LENGTH (init) - 1) == 0
16244           && ((size_t) TREE_STRING_LENGTH (init)
16245               == strlen (TREE_STRING_POINTER (init)) + 1))
16246         {
16247           rtl = gen_rtx_CONST_STRING (VOIDmode,
16248                                       ggc_strdup (TREE_STRING_POINTER (init)));
16249           rtl = gen_rtx_MEM (BLKmode, rtl);
16250           MEM_READONLY_P (rtl) = 1;
16251         }
16252     }
16253   /* Other aggregates, and complex values, could be represented using
16254      CONCAT: FIXME!  */
16255   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16256     ;
16257   /* Vectors only work if their mode is supported by the target.
16258      FIXME: generic vectors ought to work too.  */
16259   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16260     ;
16261   /* If the initializer is something that we know will expand into an
16262      immediate RTL constant, expand it now.  We must be careful not to
16263      reference variables which won't be output.  */
16264   else if (initializer_constant_valid_p (init, type)
16265            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16266     {
16267       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16268          possible.  */
16269       if (TREE_CODE (type) == VECTOR_TYPE)
16270         switch (TREE_CODE (init))
16271           {
16272           case VECTOR_CST:
16273             break;
16274           case CONSTRUCTOR:
16275             if (TREE_CONSTANT (init))
16276               {
16277                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16278                 bool constant_p = true;
16279                 tree value;
16280                 unsigned HOST_WIDE_INT ix;
16281
16282                 /* Even when ctor is constant, it might contain non-*_CST
16283                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16284                    belong into VECTOR_CST nodes.  */
16285                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16286                   if (!CONSTANT_CLASS_P (value))
16287                     {
16288                       constant_p = false;
16289                       break;
16290                     }
16291
16292                 if (constant_p)
16293                   {
16294                     init = build_vector_from_ctor (type, elts);
16295                     break;
16296                   }
16297               }
16298             /* FALLTHRU */
16299
16300           default:
16301             return NULL;
16302           }
16303
16304       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16305
16306       /* If expand_expr returns a MEM, it wasn't immediate.  */
16307       gcc_assert (!rtl || !MEM_P (rtl));
16308     }
16309
16310   return rtl;
16311 }
16312
16313 /* Generate RTL for the variable DECL to represent its location.  */
16314
16315 static rtx
16316 rtl_for_decl_location (tree decl)
16317 {
16318   rtx rtl;
16319
16320   /* Here we have to decide where we are going to say the parameter "lives"
16321      (as far as the debugger is concerned).  We only have a couple of
16322      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16323
16324      DECL_RTL normally indicates where the parameter lives during most of the
16325      activation of the function.  If optimization is enabled however, this
16326      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16327      that the parameter doesn't really live anywhere (as far as the code
16328      generation parts of GCC are concerned) during most of the function's
16329      activation.  That will happen (for example) if the parameter is never
16330      referenced within the function.
16331
16332      We could just generate a location descriptor here for all non-NULL
16333      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16334      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16335      where DECL_RTL is NULL or is a pseudo-reg.
16336
16337      Note however that we can only get away with using DECL_INCOMING_RTL as
16338      a backup substitute for DECL_RTL in certain limited cases.  In cases
16339      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16340      we can be sure that the parameter was passed using the same type as it is
16341      declared to have within the function, and that its DECL_INCOMING_RTL
16342      points us to a place where a value of that type is passed.
16343
16344      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16345      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16346      because in these cases DECL_INCOMING_RTL points us to a value of some
16347      type which is *different* from the type of the parameter itself.  Thus,
16348      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16349      such cases, the debugger would end up (for example) trying to fetch a
16350      `float' from a place which actually contains the first part of a
16351      `double'.  That would lead to really incorrect and confusing
16352      output at debug-time.
16353
16354      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16355      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16356      are a couple of exceptions however.  On little-endian machines we can
16357      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16358      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16359      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16360      when (on a little-endian machine) a non-prototyped function has a
16361      parameter declared to be of type `short' or `char'.  In such cases,
16362      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16363      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16364      passed `int' value.  If the debugger then uses that address to fetch
16365      a `short' or a `char' (on a little-endian machine) the result will be
16366      the correct data, so we allow for such exceptional cases below.
16367
16368      Note that our goal here is to describe the place where the given formal
16369      parameter lives during most of the function's activation (i.e. between the
16370      end of the prologue and the start of the epilogue).  We'll do that as best
16371      as we can. Note however that if the given formal parameter is modified
16372      sometime during the execution of the function, then a stack backtrace (at
16373      debug-time) will show the function as having been called with the *new*
16374      value rather than the value which was originally passed in.  This happens
16375      rarely enough that it is not a major problem, but it *is* a problem, and
16376      I'd like to fix it.
16377
16378      A future version of dwarf2out.c may generate two additional attributes for
16379      any given DW_TAG_formal_parameter DIE which will describe the "passed
16380      type" and the "passed location" for the given formal parameter in addition
16381      to the attributes we now generate to indicate the "declared type" and the
16382      "active location" for each parameter.  This additional set of attributes
16383      could be used by debuggers for stack backtraces. Separately, note that
16384      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16385      This happens (for example) for inlined-instances of inline function formal
16386      parameters which are never referenced.  This really shouldn't be
16387      happening.  All PARM_DECL nodes should get valid non-NULL
16388      DECL_INCOMING_RTL values.  FIXME.  */
16389
16390   /* Use DECL_RTL as the "location" unless we find something better.  */
16391   rtl = DECL_RTL_IF_SET (decl);
16392
16393   /* When generating abstract instances, ignore everything except
16394      constants, symbols living in memory, and symbols living in
16395      fixed registers.  */
16396   if (! reload_completed)
16397     {
16398       if (rtl
16399           && (CONSTANT_P (rtl)
16400               || (MEM_P (rtl)
16401                   && CONSTANT_P (XEXP (rtl, 0)))
16402               || (REG_P (rtl)
16403                   && TREE_CODE (decl) == VAR_DECL
16404                   && TREE_STATIC (decl))))
16405         {
16406           rtl = targetm.delegitimize_address (rtl);
16407           return rtl;
16408         }
16409       rtl = NULL_RTX;
16410     }
16411   else if (TREE_CODE (decl) == PARM_DECL)
16412     {
16413       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16414         {
16415           tree declared_type = TREE_TYPE (decl);
16416           tree passed_type = DECL_ARG_TYPE (decl);
16417           enum machine_mode dmode = TYPE_MODE (declared_type);
16418           enum machine_mode pmode = TYPE_MODE (passed_type);
16419
16420           /* This decl represents a formal parameter which was optimized out.
16421              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16422              all cases where (rtl == NULL_RTX) just below.  */
16423           if (dmode == pmode)
16424             rtl = DECL_INCOMING_RTL (decl);
16425           else if (SCALAR_INT_MODE_P (dmode)
16426                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16427                    && DECL_INCOMING_RTL (decl))
16428             {
16429               rtx inc = DECL_INCOMING_RTL (decl);
16430               if (REG_P (inc))
16431                 rtl = inc;
16432               else if (MEM_P (inc))
16433                 {
16434                   if (BYTES_BIG_ENDIAN)
16435                     rtl = adjust_address_nv (inc, dmode,
16436                                              GET_MODE_SIZE (pmode)
16437                                              - GET_MODE_SIZE (dmode));
16438                   else
16439                     rtl = inc;
16440                 }
16441             }
16442         }
16443
16444       /* If the parm was passed in registers, but lives on the stack, then
16445          make a big endian correction if the mode of the type of the
16446          parameter is not the same as the mode of the rtl.  */
16447       /* ??? This is the same series of checks that are made in dbxout.c before
16448          we reach the big endian correction code there.  It isn't clear if all
16449          of these checks are necessary here, but keeping them all is the safe
16450          thing to do.  */
16451       else if (MEM_P (rtl)
16452                && XEXP (rtl, 0) != const0_rtx
16453                && ! CONSTANT_P (XEXP (rtl, 0))
16454                /* Not passed in memory.  */
16455                && !MEM_P (DECL_INCOMING_RTL (decl))
16456                /* Not passed by invisible reference.  */
16457                && (!REG_P (XEXP (rtl, 0))
16458                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16459                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16460 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16461                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16462 #endif
16463                      )
16464                /* Big endian correction check.  */
16465                && BYTES_BIG_ENDIAN
16466                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16467                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16468                    < UNITS_PER_WORD))
16469         {
16470           int offset = (UNITS_PER_WORD
16471                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16472
16473           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16474                              plus_constant (XEXP (rtl, 0), offset));
16475         }
16476     }
16477   else if (TREE_CODE (decl) == VAR_DECL
16478            && rtl
16479            && MEM_P (rtl)
16480            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16481            && BYTES_BIG_ENDIAN)
16482     {
16483       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16484       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16485
16486       /* If a variable is declared "register" yet is smaller than
16487          a register, then if we store the variable to memory, it
16488          looks like we're storing a register-sized value, when in
16489          fact we are not.  We need to adjust the offset of the
16490          storage location to reflect the actual value's bytes,
16491          else gdb will not be able to display it.  */
16492       if (rsize > dsize)
16493         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16494                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16495     }
16496
16497   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16498      and will have been substituted directly into all expressions that use it.
16499      C does not have such a concept, but C++ and other languages do.  */
16500   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16501     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16502
16503   if (rtl)
16504     rtl = targetm.delegitimize_address (rtl);
16505
16506   /* If we don't look past the constant pool, we risk emitting a
16507      reference to a constant pool entry that isn't referenced from
16508      code, and thus is not emitted.  */
16509   if (rtl)
16510     rtl = avoid_constant_pool_reference (rtl);
16511
16512   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16513      in the current CU, resolve_addr will remove the expression referencing
16514      it.  */
16515   if (rtl == NULL_RTX
16516       && TREE_CODE (decl) == VAR_DECL
16517       && !DECL_EXTERNAL (decl)
16518       && TREE_STATIC (decl)
16519       && DECL_NAME (decl)
16520       && !DECL_HARD_REGISTER (decl)
16521       && DECL_MODE (decl) != VOIDmode)
16522     {
16523       rtl = make_decl_rtl_for_debug (decl);
16524       if (!MEM_P (rtl)
16525           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16526           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16527         rtl = NULL_RTX;
16528     }
16529
16530   return rtl;
16531 }
16532
16533 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16534    returned.  If so, the decl for the COMMON block is returned, and the
16535    value is the offset into the common block for the symbol.  */
16536
16537 static tree
16538 fortran_common (tree decl, HOST_WIDE_INT *value)
16539 {
16540   tree val_expr, cvar;
16541   enum machine_mode mode;
16542   HOST_WIDE_INT bitsize, bitpos;
16543   tree offset;
16544   int volatilep = 0, unsignedp = 0;
16545
16546   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16547      it does not have a value (the offset into the common area), or if it
16548      is thread local (as opposed to global) then it isn't common, and shouldn't
16549      be handled as such.  */
16550   if (TREE_CODE (decl) != VAR_DECL
16551       || !TREE_STATIC (decl)
16552       || !DECL_HAS_VALUE_EXPR_P (decl)
16553       || !is_fortran ())
16554     return NULL_TREE;
16555
16556   val_expr = DECL_VALUE_EXPR (decl);
16557   if (TREE_CODE (val_expr) != COMPONENT_REF)
16558     return NULL_TREE;
16559
16560   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16561                               &mode, &unsignedp, &volatilep, true);
16562
16563   if (cvar == NULL_TREE
16564       || TREE_CODE (cvar) != VAR_DECL
16565       || DECL_ARTIFICIAL (cvar)
16566       || !TREE_PUBLIC (cvar))
16567     return NULL_TREE;
16568
16569   *value = 0;
16570   if (offset != NULL)
16571     {
16572       if (!host_integerp (offset, 0))
16573         return NULL_TREE;
16574       *value = tree_low_cst (offset, 0);
16575     }
16576   if (bitpos != 0)
16577     *value += bitpos / BITS_PER_UNIT;
16578
16579   return cvar;
16580 }
16581
16582 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16583    data attribute for a variable or a parameter.  We generate the
16584    DW_AT_const_value attribute only in those cases where the given variable
16585    or parameter does not have a true "location" either in memory or in a
16586    register.  This can happen (for example) when a constant is passed as an
16587    actual argument in a call to an inline function.  (It's possible that
16588    these things can crop up in other ways also.)  Note that one type of
16589    constant value which can be passed into an inlined function is a constant
16590    pointer.  This can happen for example if an actual argument in an inlined
16591    function call evaluates to a compile-time constant address.  */
16592
16593 static bool
16594 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16595                                        enum dwarf_attribute attr)
16596 {
16597   rtx rtl;
16598   dw_loc_list_ref list;
16599   var_loc_list *loc_list;
16600
16601   if (TREE_CODE (decl) == ERROR_MARK)
16602     return false;
16603
16604   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16605               || TREE_CODE (decl) == RESULT_DECL);
16606
16607   /* Try to get some constant RTL for this decl, and use that as the value of
16608      the location.  */
16609
16610   rtl = rtl_for_decl_location (decl);
16611   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16612       && add_const_value_attribute (die, rtl))
16613     return true;
16614
16615   /* See if we have single element location list that is equivalent to
16616      a constant value.  That way we are better to use add_const_value_attribute
16617      rather than expanding constant value equivalent.  */
16618   loc_list = lookup_decl_loc (decl);
16619   if (loc_list
16620       && loc_list->first
16621       && loc_list->first->next == NULL
16622       && NOTE_P (loc_list->first->loc)
16623       && NOTE_VAR_LOCATION (loc_list->first->loc)
16624       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16625     {
16626       struct var_loc_node *node;
16627
16628       node = loc_list->first;
16629       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16630       if (GET_CODE (rtl) == EXPR_LIST)
16631         rtl = XEXP (rtl, 0);
16632       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16633           && add_const_value_attribute (die, rtl))
16634          return true;
16635     }
16636   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16637   if (list)
16638     {
16639       add_AT_location_description (die, attr, list);
16640       return true;
16641     }
16642   /* None of that worked, so it must not really have a location;
16643      try adding a constant value attribute from the DECL_INITIAL.  */
16644   return tree_add_const_value_attribute_for_decl (die, decl);
16645 }
16646
16647 /* Add VARIABLE and DIE into deferred locations list.  */
16648
16649 static void
16650 defer_location (tree variable, dw_die_ref die)
16651 {
16652   deferred_locations entry;
16653   entry.variable = variable;
16654   entry.die = die;
16655   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16656 }
16657
16658 /* Helper function for tree_add_const_value_attribute.  Natively encode
16659    initializer INIT into an array.  Return true if successful.  */
16660
16661 static bool
16662 native_encode_initializer (tree init, unsigned char *array, int size)
16663 {
16664   tree type;
16665
16666   if (init == NULL_TREE)
16667     return false;
16668
16669   STRIP_NOPS (init);
16670   switch (TREE_CODE (init))
16671     {
16672     case STRING_CST:
16673       type = TREE_TYPE (init);
16674       if (TREE_CODE (type) == ARRAY_TYPE)
16675         {
16676           tree enttype = TREE_TYPE (type);
16677           enum machine_mode mode = TYPE_MODE (enttype);
16678
16679           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16680             return false;
16681           if (int_size_in_bytes (type) != size)
16682             return false;
16683           if (size > TREE_STRING_LENGTH (init))
16684             {
16685               memcpy (array, TREE_STRING_POINTER (init),
16686                       TREE_STRING_LENGTH (init));
16687               memset (array + TREE_STRING_LENGTH (init),
16688                       '\0', size - TREE_STRING_LENGTH (init));
16689             }
16690           else
16691             memcpy (array, TREE_STRING_POINTER (init), size);
16692           return true;
16693         }
16694       return false;
16695     case CONSTRUCTOR:
16696       type = TREE_TYPE (init);
16697       if (int_size_in_bytes (type) != size)
16698         return false;
16699       if (TREE_CODE (type) == ARRAY_TYPE)
16700         {
16701           HOST_WIDE_INT min_index;
16702           unsigned HOST_WIDE_INT cnt;
16703           int curpos = 0, fieldsize;
16704           constructor_elt *ce;
16705
16706           if (TYPE_DOMAIN (type) == NULL_TREE
16707               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16708             return false;
16709
16710           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16711           if (fieldsize <= 0)
16712             return false;
16713
16714           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16715           memset (array, '\0', size);
16716           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16717             {
16718               tree val = ce->value;
16719               tree index = ce->index;
16720               int pos = curpos;
16721               if (index && TREE_CODE (index) == RANGE_EXPR)
16722                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16723                       * fieldsize;
16724               else if (index)
16725                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16726
16727               if (val)
16728                 {
16729                   STRIP_NOPS (val);
16730                   if (!native_encode_initializer (val, array + pos, fieldsize))
16731                     return false;
16732                 }
16733               curpos = pos + fieldsize;
16734               if (index && TREE_CODE (index) == RANGE_EXPR)
16735                 {
16736                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16737                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16738                   while (count > 0)
16739                     {
16740                       if (val)
16741                         memcpy (array + curpos, array + pos, fieldsize);
16742                       curpos += fieldsize;
16743                     }
16744                 }
16745               gcc_assert (curpos <= size);
16746             }
16747           return true;
16748         }
16749       else if (TREE_CODE (type) == RECORD_TYPE
16750                || TREE_CODE (type) == UNION_TYPE)
16751         {
16752           tree field = NULL_TREE;
16753           unsigned HOST_WIDE_INT cnt;
16754           constructor_elt *ce;
16755
16756           if (int_size_in_bytes (type) != size)
16757             return false;
16758
16759           if (TREE_CODE (type) == RECORD_TYPE)
16760             field = TYPE_FIELDS (type);
16761
16762           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16763             {
16764               tree val = ce->value;
16765               int pos, fieldsize;
16766
16767               if (ce->index != 0)
16768                 field = ce->index;
16769
16770               if (val)
16771                 STRIP_NOPS (val);
16772
16773               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16774                 return false;
16775
16776               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16777                   && TYPE_DOMAIN (TREE_TYPE (field))
16778                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16779                 return false;
16780               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16781                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16782                 return false;
16783               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16784               pos = int_byte_position (field);
16785               gcc_assert (pos + fieldsize <= size);
16786               if (val
16787                   && !native_encode_initializer (val, array + pos, fieldsize))
16788                 return false;
16789             }
16790           return true;
16791         }
16792       return false;
16793     case VIEW_CONVERT_EXPR:
16794     case NON_LVALUE_EXPR:
16795       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16796     default:
16797       return native_encode_expr (init, array, size) == size;
16798     }
16799 }
16800
16801 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16802    attribute is the const value T.  */
16803
16804 static bool
16805 tree_add_const_value_attribute (dw_die_ref die, tree t)
16806 {
16807   tree init;
16808   tree type = TREE_TYPE (t);
16809   rtx rtl;
16810
16811   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16812     return false;
16813
16814   init = t;
16815   gcc_assert (!DECL_P (init));
16816
16817   rtl = rtl_for_decl_init (init, type);
16818   if (rtl)
16819     return add_const_value_attribute (die, rtl);
16820   /* If the host and target are sane, try harder.  */
16821   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16822            && initializer_constant_valid_p (init, type))
16823     {
16824       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16825       if (size > 0 && (int) size == size)
16826         {
16827           unsigned char *array = (unsigned char *)
16828             ggc_alloc_cleared_atomic (size);
16829
16830           if (native_encode_initializer (init, array, size))
16831             {
16832               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16833               return true;
16834             }
16835         }
16836     }
16837   return false;
16838 }
16839
16840 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16841    attribute is the const value of T, where T is an integral constant
16842    variable with static storage duration
16843    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16844
16845 static bool
16846 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16847 {
16848
16849   if (!decl
16850       || (TREE_CODE (decl) != VAR_DECL
16851           && TREE_CODE (decl) != CONST_DECL))
16852     return false;
16853
16854     if (TREE_READONLY (decl)
16855         && ! TREE_THIS_VOLATILE (decl)
16856         && DECL_INITIAL (decl))
16857       /* OK */;
16858     else
16859       return false;
16860
16861   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16862   if (get_AT (var_die, DW_AT_const_value))
16863     return false;
16864
16865   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16866 }
16867
16868 /* Convert the CFI instructions for the current function into a
16869    location list.  This is used for DW_AT_frame_base when we targeting
16870    a dwarf2 consumer that does not support the dwarf3
16871    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16872    expressions.  */
16873
16874 static dw_loc_list_ref
16875 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16876 {
16877   dw_fde_ref fde;
16878   dw_loc_list_ref list, *list_tail;
16879   dw_cfi_ref cfi;
16880   dw_cfa_location last_cfa, next_cfa;
16881   const char *start_label, *last_label, *section;
16882   dw_cfa_location remember;
16883
16884   fde = current_fde ();
16885   gcc_assert (fde != NULL);
16886
16887   section = secname_for_decl (current_function_decl);
16888   list_tail = &list;
16889   list = NULL;
16890
16891   memset (&next_cfa, 0, sizeof (next_cfa));
16892   next_cfa.reg = INVALID_REGNUM;
16893   remember = next_cfa;
16894
16895   start_label = fde->dw_fde_begin;
16896
16897   /* ??? Bald assumption that the CIE opcode list does not contain
16898      advance opcodes.  */
16899   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16900     lookup_cfa_1 (cfi, &next_cfa, &remember);
16901
16902   last_cfa = next_cfa;
16903   last_label = start_label;
16904
16905   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16906     switch (cfi->dw_cfi_opc)
16907       {
16908       case DW_CFA_set_loc:
16909       case DW_CFA_advance_loc1:
16910       case DW_CFA_advance_loc2:
16911       case DW_CFA_advance_loc4:
16912         if (!cfa_equal_p (&last_cfa, &next_cfa))
16913           {
16914             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16915                                        start_label, last_label, section);
16916
16917             list_tail = &(*list_tail)->dw_loc_next;
16918             last_cfa = next_cfa;
16919             start_label = last_label;
16920           }
16921         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16922         break;
16923
16924       case DW_CFA_advance_loc:
16925         /* The encoding is complex enough that we should never emit this.  */
16926         gcc_unreachable ();
16927
16928       default:
16929         lookup_cfa_1 (cfi, &next_cfa, &remember);
16930         break;
16931       }
16932
16933   if (!cfa_equal_p (&last_cfa, &next_cfa))
16934     {
16935       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16936                                  start_label, last_label, section);
16937       list_tail = &(*list_tail)->dw_loc_next;
16938       start_label = last_label;
16939     }
16940
16941   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16942                              start_label, fde->dw_fde_end, section);
16943
16944   if (list && list->dw_loc_next)
16945     gen_llsym (list);
16946
16947   return list;
16948 }
16949
16950 /* Compute a displacement from the "steady-state frame pointer" to the
16951    frame base (often the same as the CFA), and store it in
16952    frame_pointer_fb_offset.  OFFSET is added to the displacement
16953    before the latter is negated.  */
16954
16955 static void
16956 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16957 {
16958   rtx reg, elim;
16959
16960 #ifdef FRAME_POINTER_CFA_OFFSET
16961   reg = frame_pointer_rtx;
16962   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16963 #else
16964   reg = arg_pointer_rtx;
16965   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16966 #endif
16967
16968   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16969   if (GET_CODE (elim) == PLUS)
16970     {
16971       offset += INTVAL (XEXP (elim, 1));
16972       elim = XEXP (elim, 0);
16973     }
16974
16975   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16976                && (elim == hard_frame_pointer_rtx
16977                    || elim == stack_pointer_rtx))
16978               || elim == (frame_pointer_needed
16979                           ? hard_frame_pointer_rtx
16980                           : stack_pointer_rtx));
16981
16982   frame_pointer_fb_offset = -offset;
16983 }
16984
16985 /* Generate a DW_AT_name attribute given some string value to be included as
16986    the value of the attribute.  */
16987
16988 static void
16989 add_name_attribute (dw_die_ref die, const char *name_string)
16990 {
16991   if (name_string != NULL && *name_string != 0)
16992     {
16993       if (demangle_name_func)
16994         name_string = (*demangle_name_func) (name_string);
16995
16996       add_AT_string (die, DW_AT_name, name_string);
16997     }
16998 }
16999
17000 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17001
17002 static void
17003 add_comp_dir_attribute (dw_die_ref die)
17004 {
17005   const char *wd = get_src_pwd ();
17006   char *wd1;
17007
17008   if (wd == NULL)
17009     return;
17010
17011   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17012     {
17013       int wdlen;
17014
17015       wdlen = strlen (wd);
17016       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17017       strcpy (wd1, wd);
17018       wd1 [wdlen] = DIR_SEPARATOR;
17019       wd1 [wdlen + 1] = 0;
17020       wd = wd1;
17021     }
17022
17023     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17024 }
17025
17026 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17027    default.  */
17028
17029 static int
17030 lower_bound_default (void)
17031 {
17032   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17033     {
17034     case DW_LANG_C:
17035     case DW_LANG_C89:
17036     case DW_LANG_C99:
17037     case DW_LANG_C_plus_plus:
17038     case DW_LANG_ObjC:
17039     case DW_LANG_ObjC_plus_plus:
17040     case DW_LANG_Java:
17041       return 0;
17042     case DW_LANG_Fortran77:
17043     case DW_LANG_Fortran90:
17044     case DW_LANG_Fortran95:
17045       return 1;
17046     case DW_LANG_UPC:
17047     case DW_LANG_D:
17048     case DW_LANG_Python:
17049       return dwarf_version >= 4 ? 0 : -1;
17050     case DW_LANG_Ada95:
17051     case DW_LANG_Ada83:
17052     case DW_LANG_Cobol74:
17053     case DW_LANG_Cobol85:
17054     case DW_LANG_Pascal83:
17055     case DW_LANG_Modula2:
17056     case DW_LANG_PLI:
17057       return dwarf_version >= 4 ? 1 : -1;
17058     default:
17059       return -1;
17060     }
17061 }
17062
17063 /* Given a tree node describing an array bound (either lower or upper) output
17064    a representation for that bound.  */
17065
17066 static void
17067 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17068 {
17069   switch (TREE_CODE (bound))
17070     {
17071     case ERROR_MARK:
17072       return;
17073
17074     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17075     case INTEGER_CST:
17076       {
17077         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17078         int dflt;
17079
17080         /* Use the default if possible.  */
17081         if (bound_attr == DW_AT_lower_bound
17082             && host_integerp (bound, 0)
17083             && (dflt = lower_bound_default ()) != -1
17084             && tree_low_cst (bound, 0) == dflt)
17085           ;
17086
17087         /* Otherwise represent the bound as an unsigned value with the
17088            precision of its type.  The precision and signedness of the
17089            type will be necessary to re-interpret it unambiguously.  */
17090         else if (prec < HOST_BITS_PER_WIDE_INT)
17091           {
17092             unsigned HOST_WIDE_INT mask
17093               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17094             add_AT_unsigned (subrange_die, bound_attr,
17095                              TREE_INT_CST_LOW (bound) & mask);
17096           }
17097         else if (prec == HOST_BITS_PER_WIDE_INT
17098                  || TREE_INT_CST_HIGH (bound) == 0)
17099           add_AT_unsigned (subrange_die, bound_attr,
17100                            TREE_INT_CST_LOW (bound));
17101         else
17102           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17103                          TREE_INT_CST_LOW (bound));
17104       }
17105       break;
17106
17107     CASE_CONVERT:
17108     case VIEW_CONVERT_EXPR:
17109       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17110       break;
17111
17112     case SAVE_EXPR:
17113       break;
17114
17115     case VAR_DECL:
17116     case PARM_DECL:
17117     case RESULT_DECL:
17118       {
17119         dw_die_ref decl_die = lookup_decl_die (bound);
17120
17121         /* ??? Can this happen, or should the variable have been bound
17122            first?  Probably it can, since I imagine that we try to create
17123            the types of parameters in the order in which they exist in
17124            the list, and won't have created a forward reference to a
17125            later parameter.  */
17126         if (decl_die != NULL)
17127           {
17128             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17129             break;
17130           }
17131       }
17132       /* FALLTHRU */
17133
17134     default:
17135       {
17136         /* Otherwise try to create a stack operation procedure to
17137            evaluate the value of the array bound.  */
17138
17139         dw_die_ref ctx, decl_die;
17140         dw_loc_list_ref list;
17141
17142         list = loc_list_from_tree (bound, 2);
17143         if (list == NULL || single_element_loc_list_p (list))
17144           {
17145             /* If DW_AT_*bound is not a reference nor constant, it is
17146                a DWARF expression rather than location description.
17147                For that loc_list_from_tree (bound, 0) is needed.
17148                If that fails to give a single element list,
17149                fall back to outputting this as a reference anyway.  */
17150             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17151             if (list2 && single_element_loc_list_p (list2))
17152               {
17153                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17154                 break;
17155               }
17156           }
17157         if (list == NULL)
17158           break;
17159
17160         if (current_function_decl == 0)
17161           ctx = comp_unit_die;
17162         else
17163           ctx = lookup_decl_die (current_function_decl);
17164
17165         decl_die = new_die (DW_TAG_variable, ctx, bound);
17166         add_AT_flag (decl_die, DW_AT_artificial, 1);
17167         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17168         add_AT_location_description (decl_die, DW_AT_location, list);
17169         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17170         break;
17171       }
17172     }
17173 }
17174
17175 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17176    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17177    Note that the block of subscript information for an array type also
17178    includes information about the element type of the given array type.  */
17179
17180 static void
17181 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17182 {
17183   unsigned dimension_number;
17184   tree lower, upper;
17185   dw_die_ref subrange_die;
17186
17187   for (dimension_number = 0;
17188        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17189        type = TREE_TYPE (type), dimension_number++)
17190     {
17191       tree domain = TYPE_DOMAIN (type);
17192
17193       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17194         break;
17195
17196       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17197          and (in GNU C only) variable bounds.  Handle all three forms
17198          here.  */
17199       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17200       if (domain)
17201         {
17202           /* We have an array type with specified bounds.  */
17203           lower = TYPE_MIN_VALUE (domain);
17204           upper = TYPE_MAX_VALUE (domain);
17205
17206           /* Define the index type.  */
17207           if (TREE_TYPE (domain))
17208             {
17209               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17210                  TREE_TYPE field.  We can't emit debug info for this
17211                  because it is an unnamed integral type.  */
17212               if (TREE_CODE (domain) == INTEGER_TYPE
17213                   && TYPE_NAME (domain) == NULL_TREE
17214                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17215                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17216                 ;
17217               else
17218                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17219                                     type_die);
17220             }
17221
17222           /* ??? If upper is NULL, the array has unspecified length,
17223              but it does have a lower bound.  This happens with Fortran
17224                dimension arr(N:*)
17225              Since the debugger is definitely going to need to know N
17226              to produce useful results, go ahead and output the lower
17227              bound solo, and hope the debugger can cope.  */
17228
17229           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17230           if (upper)
17231             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17232         }
17233
17234       /* Otherwise we have an array type with an unspecified length.  The
17235          DWARF-2 spec does not say how to handle this; let's just leave out the
17236          bounds.  */
17237     }
17238 }
17239
17240 static void
17241 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17242 {
17243   unsigned size;
17244
17245   switch (TREE_CODE (tree_node))
17246     {
17247     case ERROR_MARK:
17248       size = 0;
17249       break;
17250     case ENUMERAL_TYPE:
17251     case RECORD_TYPE:
17252     case UNION_TYPE:
17253     case QUAL_UNION_TYPE:
17254       size = int_size_in_bytes (tree_node);
17255       break;
17256     case FIELD_DECL:
17257       /* For a data member of a struct or union, the DW_AT_byte_size is
17258          generally given as the number of bytes normally allocated for an
17259          object of the *declared* type of the member itself.  This is true
17260          even for bit-fields.  */
17261       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17262       break;
17263     default:
17264       gcc_unreachable ();
17265     }
17266
17267   /* Note that `size' might be -1 when we get to this point.  If it is, that
17268      indicates that the byte size of the entity in question is variable.  We
17269      have no good way of expressing this fact in Dwarf at the present time,
17270      so just let the -1 pass on through.  */
17271   add_AT_unsigned (die, DW_AT_byte_size, size);
17272 }
17273
17274 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17275    which specifies the distance in bits from the highest order bit of the
17276    "containing object" for the bit-field to the highest order bit of the
17277    bit-field itself.
17278
17279    For any given bit-field, the "containing object" is a hypothetical object
17280    (of some integral or enum type) within which the given bit-field lives.  The
17281    type of this hypothetical "containing object" is always the same as the
17282    declared type of the individual bit-field itself.  The determination of the
17283    exact location of the "containing object" for a bit-field is rather
17284    complicated.  It's handled by the `field_byte_offset' function (above).
17285
17286    Note that it is the size (in bytes) of the hypothetical "containing object"
17287    which will be given in the DW_AT_byte_size attribute for this bit-field.
17288    (See `byte_size_attribute' above).  */
17289
17290 static inline void
17291 add_bit_offset_attribute (dw_die_ref die, tree decl)
17292 {
17293   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17294   tree type = DECL_BIT_FIELD_TYPE (decl);
17295   HOST_WIDE_INT bitpos_int;
17296   HOST_WIDE_INT highest_order_object_bit_offset;
17297   HOST_WIDE_INT highest_order_field_bit_offset;
17298   HOST_WIDE_INT unsigned bit_offset;
17299
17300   /* Must be a field and a bit field.  */
17301   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17302
17303   /* We can't yet handle bit-fields whose offsets are variable, so if we
17304      encounter such things, just return without generating any attribute
17305      whatsoever.  Likewise for variable or too large size.  */
17306   if (! host_integerp (bit_position (decl), 0)
17307       || ! host_integerp (DECL_SIZE (decl), 1))
17308     return;
17309
17310   bitpos_int = int_bit_position (decl);
17311
17312   /* Note that the bit offset is always the distance (in bits) from the
17313      highest-order bit of the "containing object" to the highest-order bit of
17314      the bit-field itself.  Since the "high-order end" of any object or field
17315      is different on big-endian and little-endian machines, the computation
17316      below must take account of these differences.  */
17317   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17318   highest_order_field_bit_offset = bitpos_int;
17319
17320   if (! BYTES_BIG_ENDIAN)
17321     {
17322       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17323       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17324     }
17325
17326   bit_offset
17327     = (! BYTES_BIG_ENDIAN
17328        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17329        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17330
17331   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17332 }
17333
17334 /* For a FIELD_DECL node which represents a bit field, output an attribute
17335    which specifies the length in bits of the given field.  */
17336
17337 static inline void
17338 add_bit_size_attribute (dw_die_ref die, tree decl)
17339 {
17340   /* Must be a field and a bit field.  */
17341   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17342               && DECL_BIT_FIELD_TYPE (decl));
17343
17344   if (host_integerp (DECL_SIZE (decl), 1))
17345     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17346 }
17347
17348 /* If the compiled language is ANSI C, then add a 'prototyped'
17349    attribute, if arg types are given for the parameters of a function.  */
17350
17351 static inline void
17352 add_prototyped_attribute (dw_die_ref die, tree func_type)
17353 {
17354   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17355       && TYPE_ARG_TYPES (func_type) != NULL)
17356     add_AT_flag (die, DW_AT_prototyped, 1);
17357 }
17358
17359 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17360    by looking in either the type declaration or object declaration
17361    equate table.  */
17362
17363 static inline dw_die_ref
17364 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17365 {
17366   dw_die_ref origin_die = NULL;
17367
17368   if (TREE_CODE (origin) != FUNCTION_DECL)
17369     {
17370       /* We may have gotten separated from the block for the inlined
17371          function, if we're in an exception handler or some such; make
17372          sure that the abstract function has been written out.
17373
17374          Doing this for nested functions is wrong, however; functions are
17375          distinct units, and our context might not even be inline.  */
17376       tree fn = origin;
17377
17378       if (TYPE_P (fn))
17379         fn = TYPE_STUB_DECL (fn);
17380
17381       fn = decl_function_context (fn);
17382       if (fn)
17383         dwarf2out_abstract_function (fn);
17384     }
17385
17386   if (DECL_P (origin))
17387     origin_die = lookup_decl_die (origin);
17388   else if (TYPE_P (origin))
17389     origin_die = lookup_type_die (origin);
17390
17391   /* XXX: Functions that are never lowered don't always have correct block
17392      trees (in the case of java, they simply have no block tree, in some other
17393      languages).  For these functions, there is nothing we can really do to
17394      output correct debug info for inlined functions in all cases.  Rather
17395      than die, we'll just produce deficient debug info now, in that we will
17396      have variables without a proper abstract origin.  In the future, when all
17397      functions are lowered, we should re-add a gcc_assert (origin_die)
17398      here.  */
17399
17400   if (origin_die)
17401     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17402   return origin_die;
17403 }
17404
17405 /* We do not currently support the pure_virtual attribute.  */
17406
17407 static inline void
17408 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17409 {
17410   if (DECL_VINDEX (func_decl))
17411     {
17412       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17413
17414       if (host_integerp (DECL_VINDEX (func_decl), 0))
17415         add_AT_loc (die, DW_AT_vtable_elem_location,
17416                     new_loc_descr (DW_OP_constu,
17417                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17418                                    0));
17419
17420       /* GNU extension: Record what type this method came from originally.  */
17421       if (debug_info_level > DINFO_LEVEL_TERSE
17422           && DECL_CONTEXT (func_decl))
17423         add_AT_die_ref (die, DW_AT_containing_type,
17424                         lookup_type_die (DECL_CONTEXT (func_decl)));
17425     }
17426 }
17427 \f
17428 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17429    given decl.  This used to be a vendor extension until after DWARF 4
17430    standardized it.  */
17431
17432 static void
17433 add_linkage_attr (dw_die_ref die, tree decl)
17434 {
17435   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17436
17437   /* Mimic what assemble_name_raw does with a leading '*'.  */
17438   if (name[0] == '*')
17439     name = &name[1];
17440
17441   if (dwarf_version >= 4)
17442     add_AT_string (die, DW_AT_linkage_name, name);
17443   else
17444     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17445 }
17446
17447 /* Add source coordinate attributes for the given decl.  */
17448
17449 static void
17450 add_src_coords_attributes (dw_die_ref die, tree decl)
17451 {
17452   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17453
17454   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17455   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17456 }
17457
17458 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17459
17460 static void
17461 add_linkage_name (dw_die_ref die, tree decl)
17462 {
17463   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17464        && TREE_PUBLIC (decl)
17465        && !DECL_ABSTRACT (decl)
17466        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17467        && die->die_tag != DW_TAG_member)
17468     {
17469       /* Defer until we have an assembler name set.  */
17470       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17471         {
17472           limbo_die_node *asm_name;
17473
17474           asm_name = ggc_alloc_cleared_limbo_die_node ();
17475           asm_name->die = die;
17476           asm_name->created_for = decl;
17477           asm_name->next = deferred_asm_name;
17478           deferred_asm_name = asm_name;
17479         }
17480       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17481         add_linkage_attr (die, decl);
17482     }
17483 }
17484
17485 /* Add a DW_AT_name attribute and source coordinate attribute for the
17486    given decl, but only if it actually has a name.  */
17487
17488 static void
17489 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17490 {
17491   tree decl_name;
17492
17493   decl_name = DECL_NAME (decl);
17494   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17495     {
17496       const char *name = dwarf2_name (decl, 0);
17497       if (name)
17498         add_name_attribute (die, name);
17499       if (! DECL_ARTIFICIAL (decl))
17500         add_src_coords_attributes (die, decl);
17501
17502       add_linkage_name (die, decl);
17503     }
17504
17505 #ifdef VMS_DEBUGGING_INFO
17506   /* Get the function's name, as described by its RTL.  This may be different
17507      from the DECL_NAME name used in the source file.  */
17508   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17509     {
17510       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17511                    XEXP (DECL_RTL (decl), 0));
17512       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17513     }
17514 #endif /* VMS_DEBUGGING_INFO */
17515 }
17516
17517 #ifdef VMS_DEBUGGING_INFO
17518 /* Output the debug main pointer die for VMS */
17519
17520 void
17521 dwarf2out_vms_debug_main_pointer (void)
17522 {
17523   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17524   dw_die_ref die;
17525
17526   /* Allocate the VMS debug main subprogram die.  */
17527   die = ggc_alloc_cleared_die_node ();
17528   die->die_tag = DW_TAG_subprogram;
17529   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17530   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17531                                current_function_funcdef_no);
17532   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17533
17534   /* Make it the first child of comp_unit_die.  */
17535   die->die_parent = comp_unit_die;
17536   if (comp_unit_die->die_child)
17537     {
17538       die->die_sib = comp_unit_die->die_child->die_sib;
17539       comp_unit_die->die_child->die_sib = die;
17540     }
17541   else
17542     {
17543       die->die_sib = die;
17544       comp_unit_die->die_child = die;
17545     }
17546 }
17547 #endif /* VMS_DEBUGGING_INFO */
17548
17549 /* Push a new declaration scope.  */
17550
17551 static void
17552 push_decl_scope (tree scope)
17553 {
17554   VEC_safe_push (tree, gc, decl_scope_table, scope);
17555 }
17556
17557 /* Pop a declaration scope.  */
17558
17559 static inline void
17560 pop_decl_scope (void)
17561 {
17562   VEC_pop (tree, decl_scope_table);
17563 }
17564
17565 /* Return the DIE for the scope that immediately contains this type.
17566    Non-named types get global scope.  Named types nested in other
17567    types get their containing scope if it's open, or global scope
17568    otherwise.  All other types (i.e. function-local named types) get
17569    the current active scope.  */
17570
17571 static dw_die_ref
17572 scope_die_for (tree t, dw_die_ref context_die)
17573 {
17574   dw_die_ref scope_die = NULL;
17575   tree containing_scope;
17576   int i;
17577
17578   /* Non-types always go in the current scope.  */
17579   gcc_assert (TYPE_P (t));
17580
17581   containing_scope = TYPE_CONTEXT (t);
17582
17583   /* Use the containing namespace if it was passed in (for a declaration).  */
17584   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17585     {
17586       if (context_die == lookup_decl_die (containing_scope))
17587         /* OK */;
17588       else
17589         containing_scope = NULL_TREE;
17590     }
17591
17592   /* Ignore function type "scopes" from the C frontend.  They mean that
17593      a tagged type is local to a parmlist of a function declarator, but
17594      that isn't useful to DWARF.  */
17595   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17596     containing_scope = NULL_TREE;
17597
17598   if (containing_scope == NULL_TREE)
17599     scope_die = comp_unit_die;
17600   else if (TYPE_P (containing_scope))
17601     {
17602       /* For types, we can just look up the appropriate DIE.  But
17603          first we check to see if we're in the middle of emitting it
17604          so we know where the new DIE should go.  */
17605       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17606         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17607           break;
17608
17609       if (i < 0)
17610         {
17611           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17612                       || TREE_ASM_WRITTEN (containing_scope));
17613           /*We are not in the middle of emitting the type
17614             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17615           scope_die = lookup_type_die (containing_scope);
17616
17617           /* If none of the current dies are suitable, we get file scope.  */
17618           if (scope_die == NULL)
17619             scope_die = comp_unit_die;
17620         }
17621       else
17622         scope_die = lookup_type_die (containing_scope);
17623     }
17624   else
17625     scope_die = context_die;
17626
17627   return scope_die;
17628 }
17629
17630 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17631
17632 static inline int
17633 local_scope_p (dw_die_ref context_die)
17634 {
17635   for (; context_die; context_die = context_die->die_parent)
17636     if (context_die->die_tag == DW_TAG_inlined_subroutine
17637         || context_die->die_tag == DW_TAG_subprogram)
17638       return 1;
17639
17640   return 0;
17641 }
17642
17643 /* Returns nonzero if CONTEXT_DIE is a class.  */
17644
17645 static inline int
17646 class_scope_p (dw_die_ref context_die)
17647 {
17648   return (context_die
17649           && (context_die->die_tag == DW_TAG_structure_type
17650               || context_die->die_tag == DW_TAG_class_type
17651               || context_die->die_tag == DW_TAG_interface_type
17652               || context_die->die_tag == DW_TAG_union_type));
17653 }
17654
17655 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17656    whether or not to treat a DIE in this context as a declaration.  */
17657
17658 static inline int
17659 class_or_namespace_scope_p (dw_die_ref context_die)
17660 {
17661   return (class_scope_p (context_die)
17662           || (context_die && context_die->die_tag == DW_TAG_namespace));
17663 }
17664
17665 /* Many forms of DIEs require a "type description" attribute.  This
17666    routine locates the proper "type descriptor" die for the type given
17667    by 'type', and adds a DW_AT_type attribute below the given die.  */
17668
17669 static void
17670 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17671                     int decl_volatile, dw_die_ref context_die)
17672 {
17673   enum tree_code code  = TREE_CODE (type);
17674   dw_die_ref type_die  = NULL;
17675
17676   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17677      or fixed-point type, use the inner type.  This is because we have no
17678      support for unnamed types in base_type_die.  This can happen if this is
17679      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17680   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17681       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17682     type = TREE_TYPE (type), code = TREE_CODE (type);
17683
17684   if (code == ERROR_MARK
17685       /* Handle a special case.  For functions whose return type is void, we
17686          generate *no* type attribute.  (Note that no object may have type
17687          `void', so this only applies to function return types).  */
17688       || code == VOID_TYPE)
17689     return;
17690
17691   type_die = modified_type_die (type,
17692                                 decl_const || TYPE_READONLY (type),
17693                                 decl_volatile || TYPE_VOLATILE (type),
17694                                 context_die);
17695
17696   if (type_die != NULL)
17697     add_AT_die_ref (object_die, DW_AT_type, type_die);
17698 }
17699
17700 /* Given an object die, add the calling convention attribute for the
17701    function call type.  */
17702 static void
17703 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17704 {
17705   enum dwarf_calling_convention value = DW_CC_normal;
17706
17707   value = ((enum dwarf_calling_convention)
17708            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17709
17710   /* DWARF doesn't provide a way to identify a program's source-level
17711      entry point.  DW_AT_calling_convention attributes are only meant
17712      to describe functions' calling conventions.  However, lacking a
17713      better way to signal the Fortran main program, we use this for the
17714      time being, following existing custom.  */
17715   if (is_fortran ()
17716       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17717     value = DW_CC_program;
17718
17719   /* Only add the attribute if the backend requests it, and
17720      is not DW_CC_normal.  */
17721   if (value && (value != DW_CC_normal))
17722     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17723 }
17724
17725 /* Given a tree pointer to a struct, class, union, or enum type node, return
17726    a pointer to the (string) tag name for the given type, or zero if the type
17727    was declared without a tag.  */
17728
17729 static const char *
17730 type_tag (const_tree type)
17731 {
17732   const char *name = 0;
17733
17734   if (TYPE_NAME (type) != 0)
17735     {
17736       tree t = 0;
17737
17738       /* Find the IDENTIFIER_NODE for the type name.  */
17739       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17740           && !TYPE_NAMELESS (type))
17741         t = TYPE_NAME (type);
17742
17743       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17744          a TYPE_DECL node, regardless of whether or not a `typedef' was
17745          involved.  */
17746       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17747                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17748         {
17749           /* We want to be extra verbose.  Don't call dwarf_name if
17750              DECL_NAME isn't set.  The default hook for decl_printable_name
17751              doesn't like that, and in this context it's correct to return
17752              0, instead of "<anonymous>" or the like.  */
17753           if (DECL_NAME (TYPE_NAME (type))
17754               && !DECL_NAMELESS (TYPE_NAME (type)))
17755             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17756         }
17757
17758       /* Now get the name as a string, or invent one.  */
17759       if (!name && t != 0)
17760         name = IDENTIFIER_POINTER (t);
17761     }
17762
17763   return (name == 0 || *name == '\0') ? 0 : name;
17764 }
17765
17766 /* Return the type associated with a data member, make a special check
17767    for bit field types.  */
17768
17769 static inline tree
17770 member_declared_type (const_tree member)
17771 {
17772   return (DECL_BIT_FIELD_TYPE (member)
17773           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17774 }
17775
17776 /* Get the decl's label, as described by its RTL. This may be different
17777    from the DECL_NAME name used in the source file.  */
17778
17779 #if 0
17780 static const char *
17781 decl_start_label (tree decl)
17782 {
17783   rtx x;
17784   const char *fnname;
17785
17786   x = DECL_RTL (decl);
17787   gcc_assert (MEM_P (x));
17788
17789   x = XEXP (x, 0);
17790   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17791
17792   fnname = XSTR (x, 0);
17793   return fnname;
17794 }
17795 #endif
17796 \f
17797 /* These routines generate the internal representation of the DIE's for
17798    the compilation unit.  Debugging information is collected by walking
17799    the declaration trees passed in from dwarf2out_decl().  */
17800
17801 static void
17802 gen_array_type_die (tree type, dw_die_ref context_die)
17803 {
17804   dw_die_ref scope_die = scope_die_for (type, context_die);
17805   dw_die_ref array_die;
17806
17807   /* GNU compilers represent multidimensional array types as sequences of one
17808      dimensional array types whose element types are themselves array types.
17809      We sometimes squish that down to a single array_type DIE with multiple
17810      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17811      say that we are allowed to do this kind of compression in C, because
17812      there is no difference between an array of arrays and a multidimensional
17813      array.  We don't do this for Ada to remain as close as possible to the
17814      actual representation, which is especially important against the language
17815      flexibilty wrt arrays of variable size.  */
17816
17817   bool collapse_nested_arrays = !is_ada ();
17818   tree element_type;
17819
17820   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17821      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17822   if (TYPE_STRING_FLAG (type)
17823       && TREE_CODE (type) == ARRAY_TYPE
17824       && is_fortran ()
17825       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17826     {
17827       HOST_WIDE_INT size;
17828
17829       array_die = new_die (DW_TAG_string_type, scope_die, type);
17830       add_name_attribute (array_die, type_tag (type));
17831       equate_type_number_to_die (type, array_die);
17832       size = int_size_in_bytes (type);
17833       if (size >= 0)
17834         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17835       else if (TYPE_DOMAIN (type) != NULL_TREE
17836                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17837                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17838         {
17839           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17840           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17841
17842           size = int_size_in_bytes (TREE_TYPE (szdecl));
17843           if (loc && size > 0)
17844             {
17845               add_AT_location_description (array_die, DW_AT_string_length, loc);
17846               if (size != DWARF2_ADDR_SIZE)
17847                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17848             }
17849         }
17850       return;
17851     }
17852
17853   /* ??? The SGI dwarf reader fails for array of array of enum types
17854      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17855      array type comes before the outer array type.  We thus call gen_type_die
17856      before we new_die and must prevent nested array types collapsing for this
17857      target.  */
17858
17859 #ifdef MIPS_DEBUGGING_INFO
17860   gen_type_die (TREE_TYPE (type), context_die);
17861   collapse_nested_arrays = false;
17862 #endif
17863
17864   array_die = new_die (DW_TAG_array_type, scope_die, type);
17865   add_name_attribute (array_die, type_tag (type));
17866   equate_type_number_to_die (type, array_die);
17867
17868   if (TREE_CODE (type) == VECTOR_TYPE)
17869     {
17870       /* The frontend feeds us a representation for the vector as a struct
17871          containing an array.  Pull out the array type.  */
17872       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17873       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17874     }
17875
17876   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17877   if (is_fortran ()
17878       && TREE_CODE (type) == ARRAY_TYPE
17879       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17880       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17881     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17882
17883 #if 0
17884   /* We default the array ordering.  SDB will probably do
17885      the right things even if DW_AT_ordering is not present.  It's not even
17886      an issue until we start to get into multidimensional arrays anyway.  If
17887      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17888      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17889      and when we find out that we need to put these in, we will only do so
17890      for multidimensional arrays.  */
17891   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17892 #endif
17893
17894 #ifdef MIPS_DEBUGGING_INFO
17895   /* The SGI compilers handle arrays of unknown bound by setting
17896      AT_declaration and not emitting any subrange DIEs.  */
17897   if (! TYPE_DOMAIN (type))
17898     add_AT_flag (array_die, DW_AT_declaration, 1);
17899   else
17900 #endif
17901     add_subscript_info (array_die, type, collapse_nested_arrays);
17902
17903   /* Add representation of the type of the elements of this array type and
17904      emit the corresponding DIE if we haven't done it already.  */
17905   element_type = TREE_TYPE (type);
17906   if (collapse_nested_arrays)
17907     while (TREE_CODE (element_type) == ARRAY_TYPE)
17908       {
17909         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17910           break;
17911         element_type = TREE_TYPE (element_type);
17912       }
17913
17914 #ifndef MIPS_DEBUGGING_INFO
17915   gen_type_die (element_type, context_die);
17916 #endif
17917
17918   add_type_attribute (array_die, element_type, 0, 0, context_die);
17919
17920   if (get_AT (array_die, DW_AT_name))
17921     add_pubtype (type, array_die);
17922 }
17923
17924 static dw_loc_descr_ref
17925 descr_info_loc (tree val, tree base_decl)
17926 {
17927   HOST_WIDE_INT size;
17928   dw_loc_descr_ref loc, loc2;
17929   enum dwarf_location_atom op;
17930
17931   if (val == base_decl)
17932     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17933
17934   switch (TREE_CODE (val))
17935     {
17936     CASE_CONVERT:
17937       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17938     case VAR_DECL:
17939       return loc_descriptor_from_tree (val, 0);
17940     case INTEGER_CST:
17941       if (host_integerp (val, 0))
17942         return int_loc_descriptor (tree_low_cst (val, 0));
17943       break;
17944     case INDIRECT_REF:
17945       size = int_size_in_bytes (TREE_TYPE (val));
17946       if (size < 0)
17947         break;
17948       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17949       if (!loc)
17950         break;
17951       if (size == DWARF2_ADDR_SIZE)
17952         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17953       else
17954         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17955       return loc;
17956     case POINTER_PLUS_EXPR:
17957     case PLUS_EXPR:
17958       if (host_integerp (TREE_OPERAND (val, 1), 1)
17959           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17960              < 16384)
17961         {
17962           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17963           if (!loc)
17964             break;
17965           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17966         }
17967       else
17968         {
17969           op = DW_OP_plus;
17970         do_binop:
17971           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17972           if (!loc)
17973             break;
17974           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17975           if (!loc2)
17976             break;
17977           add_loc_descr (&loc, loc2);
17978           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17979         }
17980       return loc;
17981     case MINUS_EXPR:
17982       op = DW_OP_minus;
17983       goto do_binop;
17984     case MULT_EXPR:
17985       op = DW_OP_mul;
17986       goto do_binop;
17987     case EQ_EXPR:
17988       op = DW_OP_eq;
17989       goto do_binop;
17990     case NE_EXPR:
17991       op = DW_OP_ne;
17992       goto do_binop;
17993     default:
17994       break;
17995     }
17996   return NULL;
17997 }
17998
17999 static void
18000 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18001                       tree val, tree base_decl)
18002 {
18003   dw_loc_descr_ref loc;
18004
18005   if (host_integerp (val, 0))
18006     {
18007       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18008       return;
18009     }
18010
18011   loc = descr_info_loc (val, base_decl);
18012   if (!loc)
18013     return;
18014
18015   add_AT_loc (die, attr, loc);
18016 }
18017
18018 /* This routine generates DIE for array with hidden descriptor, details
18019    are filled into *info by a langhook.  */
18020
18021 static void
18022 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18023                           dw_die_ref context_die)
18024 {
18025   dw_die_ref scope_die = scope_die_for (type, context_die);
18026   dw_die_ref array_die;
18027   int dim;
18028
18029   array_die = new_die (DW_TAG_array_type, scope_die, type);
18030   add_name_attribute (array_die, type_tag (type));
18031   equate_type_number_to_die (type, array_die);
18032
18033   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18034   if (is_fortran ()
18035       && info->ndimensions >= 2)
18036     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18037
18038   if (info->data_location)
18039     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18040                           info->base_decl);
18041   if (info->associated)
18042     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18043                           info->base_decl);
18044   if (info->allocated)
18045     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18046                           info->base_decl);
18047
18048   for (dim = 0; dim < info->ndimensions; dim++)
18049     {
18050       dw_die_ref subrange_die
18051         = new_die (DW_TAG_subrange_type, array_die, NULL);
18052
18053       if (info->dimen[dim].lower_bound)
18054         {
18055           /* If it is the default value, omit it.  */
18056           int dflt;
18057
18058           if (host_integerp (info->dimen[dim].lower_bound, 0)
18059               && (dflt = lower_bound_default ()) != -1
18060               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18061             ;
18062           else
18063             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18064                                   info->dimen[dim].lower_bound,
18065                                   info->base_decl);
18066         }
18067       if (info->dimen[dim].upper_bound)
18068         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18069                               info->dimen[dim].upper_bound,
18070                               info->base_decl);
18071       if (info->dimen[dim].stride)
18072         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18073                               info->dimen[dim].stride,
18074                               info->base_decl);
18075     }
18076
18077   gen_type_die (info->element_type, context_die);
18078   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18079
18080   if (get_AT (array_die, DW_AT_name))
18081     add_pubtype (type, array_die);
18082 }
18083
18084 #if 0
18085 static void
18086 gen_entry_point_die (tree decl, dw_die_ref context_die)
18087 {
18088   tree origin = decl_ultimate_origin (decl);
18089   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18090
18091   if (origin != NULL)
18092     add_abstract_origin_attribute (decl_die, origin);
18093   else
18094     {
18095       add_name_and_src_coords_attributes (decl_die, decl);
18096       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18097                           0, 0, context_die);
18098     }
18099
18100   if (DECL_ABSTRACT (decl))
18101     equate_decl_number_to_die (decl, decl_die);
18102   else
18103     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18104 }
18105 #endif
18106
18107 /* Walk through the list of incomplete types again, trying once more to
18108    emit full debugging info for them.  */
18109
18110 static void
18111 retry_incomplete_types (void)
18112 {
18113   int i;
18114
18115   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18116     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18117                                   DINFO_USAGE_DIR_USE))
18118       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18119 }
18120
18121 /* Determine what tag to use for a record type.  */
18122
18123 static enum dwarf_tag
18124 record_type_tag (tree type)
18125 {
18126   if (! lang_hooks.types.classify_record)
18127     return DW_TAG_structure_type;
18128
18129   switch (lang_hooks.types.classify_record (type))
18130     {
18131     case RECORD_IS_STRUCT:
18132       return DW_TAG_structure_type;
18133
18134     case RECORD_IS_CLASS:
18135       return DW_TAG_class_type;
18136
18137     case RECORD_IS_INTERFACE:
18138       if (dwarf_version >= 3 || !dwarf_strict)
18139         return DW_TAG_interface_type;
18140       return DW_TAG_structure_type;
18141
18142     default:
18143       gcc_unreachable ();
18144     }
18145 }
18146
18147 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18148    include all of the information about the enumeration values also. Each
18149    enumerated type name/value is listed as a child of the enumerated type
18150    DIE.  */
18151
18152 static dw_die_ref
18153 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18154 {
18155   dw_die_ref type_die = lookup_type_die (type);
18156
18157   if (type_die == NULL)
18158     {
18159       type_die = new_die (DW_TAG_enumeration_type,
18160                           scope_die_for (type, context_die), type);
18161       equate_type_number_to_die (type, type_die);
18162       add_name_attribute (type_die, type_tag (type));
18163       if ((dwarf_version >= 4 || !dwarf_strict)
18164           && ENUM_IS_SCOPED (type))
18165         add_AT_flag (type_die, DW_AT_enum_class, 1);
18166     }
18167   else if (! TYPE_SIZE (type))
18168     return type_die;
18169   else
18170     remove_AT (type_die, DW_AT_declaration);
18171
18172   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18173      given enum type is incomplete, do not generate the DW_AT_byte_size
18174      attribute or the DW_AT_element_list attribute.  */
18175   if (TYPE_SIZE (type))
18176     {
18177       tree link;
18178
18179       TREE_ASM_WRITTEN (type) = 1;
18180       add_byte_size_attribute (type_die, type);
18181       if (TYPE_STUB_DECL (type) != NULL_TREE)
18182         {
18183           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18184           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18185         }
18186
18187       /* If the first reference to this type was as the return type of an
18188          inline function, then it may not have a parent.  Fix this now.  */
18189       if (type_die->die_parent == NULL)
18190         add_child_die (scope_die_for (type, context_die), type_die);
18191
18192       for (link = TYPE_VALUES (type);
18193            link != NULL; link = TREE_CHAIN (link))
18194         {
18195           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18196           tree value = TREE_VALUE (link);
18197
18198           add_name_attribute (enum_die,
18199                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18200
18201           if (TREE_CODE (value) == CONST_DECL)
18202             value = DECL_INITIAL (value);
18203
18204           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18205             /* DWARF2 does not provide a way of indicating whether or
18206                not enumeration constants are signed or unsigned.  GDB
18207                always assumes the values are signed, so we output all
18208                values as if they were signed.  That means that
18209                enumeration constants with very large unsigned values
18210                will appear to have negative values in the debugger.  */
18211             add_AT_int (enum_die, DW_AT_const_value,
18212                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18213         }
18214     }
18215   else
18216     add_AT_flag (type_die, DW_AT_declaration, 1);
18217
18218   if (get_AT (type_die, DW_AT_name))
18219     add_pubtype (type, type_die);
18220
18221   return type_die;
18222 }
18223
18224 /* Generate a DIE to represent either a real live formal parameter decl or to
18225    represent just the type of some formal parameter position in some function
18226    type.
18227
18228    Note that this routine is a bit unusual because its argument may be a
18229    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18230    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18231    node.  If it's the former then this function is being called to output a
18232    DIE to represent a formal parameter object (or some inlining thereof).  If
18233    it's the latter, then this function is only being called to output a
18234    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18235    argument type of some subprogram type.
18236    If EMIT_NAME_P is true, name and source coordinate attributes
18237    are emitted.  */
18238
18239 static dw_die_ref
18240 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18241                           dw_die_ref context_die)
18242 {
18243   tree node_or_origin = node ? node : origin;
18244   tree ultimate_origin;
18245   dw_die_ref parm_die
18246     = new_die (DW_TAG_formal_parameter, context_die, node);
18247
18248   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18249     {
18250     case tcc_declaration:
18251       ultimate_origin = decl_ultimate_origin (node_or_origin);
18252       if (node || ultimate_origin)
18253         origin = ultimate_origin;
18254       if (origin != NULL)
18255         add_abstract_origin_attribute (parm_die, origin);
18256       else if (emit_name_p)
18257         add_name_and_src_coords_attributes (parm_die, node);
18258       if (origin == NULL
18259           || (! DECL_ABSTRACT (node_or_origin)
18260               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18261                                            decl_function_context
18262                                                             (node_or_origin))))
18263         {
18264           tree type = TREE_TYPE (node_or_origin);
18265           if (decl_by_reference_p (node_or_origin))
18266             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18267                                 context_die);
18268           else
18269             add_type_attribute (parm_die, type,
18270                                 TREE_READONLY (node_or_origin),
18271                                 TREE_THIS_VOLATILE (node_or_origin),
18272                                 context_die);
18273         }
18274       if (origin == NULL && DECL_ARTIFICIAL (node))
18275         add_AT_flag (parm_die, DW_AT_artificial, 1);
18276
18277       if (node && node != origin)
18278         equate_decl_number_to_die (node, parm_die);
18279       if (! DECL_ABSTRACT (node_or_origin))
18280         add_location_or_const_value_attribute (parm_die, node_or_origin,
18281                                                DW_AT_location);
18282
18283       break;
18284
18285     case tcc_type:
18286       /* We were called with some kind of a ..._TYPE node.  */
18287       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18288       break;
18289
18290     default:
18291       gcc_unreachable ();
18292     }
18293
18294   return parm_die;
18295 }
18296
18297 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18298    children DW_TAG_formal_parameter DIEs representing the arguments of the
18299    parameter pack.
18300
18301    PARM_PACK must be a function parameter pack.
18302    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18303    must point to the subsequent arguments of the function PACK_ARG belongs to.
18304    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18305    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18306    following the last one for which a DIE was generated.  */
18307
18308 static dw_die_ref
18309 gen_formal_parameter_pack_die  (tree parm_pack,
18310                                 tree pack_arg,
18311                                 dw_die_ref subr_die,
18312                                 tree *next_arg)
18313 {
18314   tree arg;
18315   dw_die_ref parm_pack_die;
18316
18317   gcc_assert (parm_pack
18318               && lang_hooks.function_parameter_pack_p (parm_pack)
18319               && subr_die);
18320
18321   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18322   add_src_coords_attributes (parm_pack_die, parm_pack);
18323
18324   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18325     {
18326       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18327                                                                  parm_pack))
18328         break;
18329       gen_formal_parameter_die (arg, NULL,
18330                                 false /* Don't emit name attribute.  */,
18331                                 parm_pack_die);
18332     }
18333   if (next_arg)
18334     *next_arg = arg;
18335   return parm_pack_die;
18336 }
18337
18338 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18339    at the end of an (ANSI prototyped) formal parameters list.  */
18340
18341 static void
18342 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18343 {
18344   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18345 }
18346
18347 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18348    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18349    parameters as specified in some function type specification (except for
18350    those which appear as part of a function *definition*).  */
18351
18352 static void
18353 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18354 {
18355   tree link;
18356   tree formal_type = NULL;
18357   tree first_parm_type;
18358   tree arg;
18359
18360   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18361     {
18362       arg = DECL_ARGUMENTS (function_or_method_type);
18363       function_or_method_type = TREE_TYPE (function_or_method_type);
18364     }
18365   else
18366     arg = NULL_TREE;
18367
18368   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18369
18370   /* Make our first pass over the list of formal parameter types and output a
18371      DW_TAG_formal_parameter DIE for each one.  */
18372   for (link = first_parm_type; link; )
18373     {
18374       dw_die_ref parm_die;
18375
18376       formal_type = TREE_VALUE (link);
18377       if (formal_type == void_type_node)
18378         break;
18379
18380       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18381       parm_die = gen_formal_parameter_die (formal_type, NULL,
18382                                            true /* Emit name attribute.  */,
18383                                            context_die);
18384       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18385           && link == first_parm_type)
18386         {
18387           add_AT_flag (parm_die, DW_AT_artificial, 1);
18388           if (dwarf_version >= 3 || !dwarf_strict)
18389             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18390         }
18391       else if (arg && DECL_ARTIFICIAL (arg))
18392         add_AT_flag (parm_die, DW_AT_artificial, 1);
18393
18394       link = TREE_CHAIN (link);
18395       if (arg)
18396         arg = DECL_CHAIN (arg);
18397     }
18398
18399   /* If this function type has an ellipsis, add a
18400      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18401   if (formal_type != void_type_node)
18402     gen_unspecified_parameters_die (function_or_method_type, context_die);
18403
18404   /* Make our second (and final) pass over the list of formal parameter types
18405      and output DIEs to represent those types (as necessary).  */
18406   for (link = TYPE_ARG_TYPES (function_or_method_type);
18407        link && TREE_VALUE (link);
18408        link = TREE_CHAIN (link))
18409     gen_type_die (TREE_VALUE (link), context_die);
18410 }
18411
18412 /* We want to generate the DIE for TYPE so that we can generate the
18413    die for MEMBER, which has been defined; we will need to refer back
18414    to the member declaration nested within TYPE.  If we're trying to
18415    generate minimal debug info for TYPE, processing TYPE won't do the
18416    trick; we need to attach the member declaration by hand.  */
18417
18418 static void
18419 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18420 {
18421   gen_type_die (type, context_die);
18422
18423   /* If we're trying to avoid duplicate debug info, we may not have
18424      emitted the member decl for this function.  Emit it now.  */
18425   if (TYPE_STUB_DECL (type)
18426       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18427       && ! lookup_decl_die (member))
18428     {
18429       dw_die_ref type_die;
18430       gcc_assert (!decl_ultimate_origin (member));
18431
18432       push_decl_scope (type);
18433       type_die = lookup_type_die (type);
18434       if (TREE_CODE (member) == FUNCTION_DECL)
18435         gen_subprogram_die (member, type_die);
18436       else if (TREE_CODE (member) == FIELD_DECL)
18437         {
18438           /* Ignore the nameless fields that are used to skip bits but handle
18439              C++ anonymous unions and structs.  */
18440           if (DECL_NAME (member) != NULL_TREE
18441               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18442               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18443             {
18444               gen_type_die (member_declared_type (member), type_die);
18445               gen_field_die (member, type_die);
18446             }
18447         }
18448       else
18449         gen_variable_die (member, NULL_TREE, type_die);
18450
18451       pop_decl_scope ();
18452     }
18453 }
18454
18455 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18456    may later generate inlined and/or out-of-line instances of.  */
18457
18458 static void
18459 dwarf2out_abstract_function (tree decl)
18460 {
18461   dw_die_ref old_die;
18462   tree save_fn;
18463   tree context;
18464   int was_abstract;
18465   htab_t old_decl_loc_table;
18466
18467   /* Make sure we have the actual abstract inline, not a clone.  */
18468   decl = DECL_ORIGIN (decl);
18469
18470   old_die = lookup_decl_die (decl);
18471   if (old_die && get_AT (old_die, DW_AT_inline))
18472     /* We've already generated the abstract instance.  */
18473     return;
18474
18475   /* We can be called while recursively when seeing block defining inlined subroutine
18476      DIE.  Be sure to not clobber the outer location table nor use it or we would
18477      get locations in abstract instantces.  */
18478   old_decl_loc_table = decl_loc_table;
18479   decl_loc_table = NULL;
18480
18481   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18482      we don't get confused by DECL_ABSTRACT.  */
18483   if (debug_info_level > DINFO_LEVEL_TERSE)
18484     {
18485       context = decl_class_context (decl);
18486       if (context)
18487         gen_type_die_for_member
18488           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18489     }
18490
18491   /* Pretend we've just finished compiling this function.  */
18492   save_fn = current_function_decl;
18493   current_function_decl = decl;
18494   push_cfun (DECL_STRUCT_FUNCTION (decl));
18495
18496   was_abstract = DECL_ABSTRACT (decl);
18497   set_decl_abstract_flags (decl, 1);
18498   dwarf2out_decl (decl);
18499   if (! was_abstract)
18500     set_decl_abstract_flags (decl, 0);
18501
18502   current_function_decl = save_fn;
18503   decl_loc_table = old_decl_loc_table;
18504   pop_cfun ();
18505 }
18506
18507 /* Helper function of premark_used_types() which gets called through
18508    htab_traverse.
18509
18510    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18511    marked as unused by prune_unused_types.  */
18512
18513 static int
18514 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18515 {
18516   tree type;
18517   dw_die_ref die;
18518
18519   type = (tree) *slot;
18520   die = lookup_type_die (type);
18521   if (die != NULL)
18522     die->die_perennial_p = 1;
18523   return 1;
18524 }
18525
18526 /* Helper function of premark_types_used_by_global_vars which gets called
18527    through htab_traverse.
18528
18529    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18530    marked as unused by prune_unused_types. The DIE of the type is marked
18531    only if the global variable using the type will actually be emitted.  */
18532
18533 static int
18534 premark_types_used_by_global_vars_helper (void **slot,
18535                                           void *data ATTRIBUTE_UNUSED)
18536 {
18537   struct types_used_by_vars_entry *entry;
18538   dw_die_ref die;
18539
18540   entry = (struct types_used_by_vars_entry *) *slot;
18541   gcc_assert (entry->type != NULL
18542               && entry->var_decl != NULL);
18543   die = lookup_type_die (entry->type);
18544   if (die)
18545     {
18546       /* Ask cgraph if the global variable really is to be emitted.
18547          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18548       struct varpool_node *node = varpool_get_node (entry->var_decl);
18549       if (node && node->needed)
18550         {
18551           die->die_perennial_p = 1;
18552           /* Keep the parent DIEs as well.  */
18553           while ((die = die->die_parent) && die->die_perennial_p == 0)
18554             die->die_perennial_p = 1;
18555         }
18556     }
18557   return 1;
18558 }
18559
18560 /* Mark all members of used_types_hash as perennial.  */
18561
18562 static void
18563 premark_used_types (void)
18564 {
18565   if (cfun && cfun->used_types_hash)
18566     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18567 }
18568
18569 /* Mark all members of types_used_by_vars_entry as perennial.  */
18570
18571 static void
18572 premark_types_used_by_global_vars (void)
18573 {
18574   if (types_used_by_vars_hash)
18575     htab_traverse (types_used_by_vars_hash,
18576                    premark_types_used_by_global_vars_helper, NULL);
18577 }
18578
18579 /* Generate a DIE to represent a declared function (either file-scope or
18580    block-local).  */
18581
18582 static void
18583 gen_subprogram_die (tree decl, dw_die_ref context_die)
18584 {
18585   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18586   tree origin = decl_ultimate_origin (decl);
18587   dw_die_ref subr_die;
18588   tree fn_arg_types;
18589   tree outer_scope;
18590   dw_die_ref old_die = lookup_decl_die (decl);
18591   int declaration = (current_function_decl != decl
18592                      || class_or_namespace_scope_p (context_die));
18593
18594   premark_used_types ();
18595
18596   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18597      started to generate the abstract instance of an inline, decided to output
18598      its containing class, and proceeded to emit the declaration of the inline
18599      from the member list for the class.  If so, DECLARATION takes priority;
18600      we'll get back to the abstract instance when done with the class.  */
18601
18602   /* The class-scope declaration DIE must be the primary DIE.  */
18603   if (origin && declaration && class_or_namespace_scope_p (context_die))
18604     {
18605       origin = NULL;
18606       gcc_assert (!old_die);
18607     }
18608
18609   /* Now that the C++ front end lazily declares artificial member fns, we
18610      might need to retrofit the declaration into its class.  */
18611   if (!declaration && !origin && !old_die
18612       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18613       && !class_or_namespace_scope_p (context_die)
18614       && debug_info_level > DINFO_LEVEL_TERSE)
18615     old_die = force_decl_die (decl);
18616
18617   if (origin != NULL)
18618     {
18619       gcc_assert (!declaration || local_scope_p (context_die));
18620
18621       /* Fixup die_parent for the abstract instance of a nested
18622          inline function.  */
18623       if (old_die && old_die->die_parent == NULL)
18624         add_child_die (context_die, old_die);
18625
18626       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18627       add_abstract_origin_attribute (subr_die, origin);
18628     }
18629   else if (old_die)
18630     {
18631       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18632       struct dwarf_file_data * file_index = lookup_filename (s.file);
18633
18634       if (!get_AT_flag (old_die, DW_AT_declaration)
18635           /* We can have a normal definition following an inline one in the
18636              case of redefinition of GNU C extern inlines.
18637              It seems reasonable to use AT_specification in this case.  */
18638           && !get_AT (old_die, DW_AT_inline))
18639         {
18640           /* Detect and ignore this case, where we are trying to output
18641              something we have already output.  */
18642           return;
18643         }
18644
18645       /* If the definition comes from the same place as the declaration,
18646          maybe use the old DIE.  We always want the DIE for this function
18647          that has the *_pc attributes to be under comp_unit_die so the
18648          debugger can find it.  We also need to do this for abstract
18649          instances of inlines, since the spec requires the out-of-line copy
18650          to have the same parent.  For local class methods, this doesn't
18651          apply; we just use the old DIE.  */
18652       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18653           && (DECL_ARTIFICIAL (decl)
18654               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18655                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18656                       == (unsigned) s.line))))
18657         {
18658           subr_die = old_die;
18659
18660           /* Clear out the declaration attribute and the formal parameters.
18661              Do not remove all children, because it is possible that this
18662              declaration die was forced using force_decl_die(). In such
18663              cases die that forced declaration die (e.g. TAG_imported_module)
18664              is one of the children that we do not want to remove.  */
18665           remove_AT (subr_die, DW_AT_declaration);
18666           remove_AT (subr_die, DW_AT_object_pointer);
18667           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18668         }
18669       else
18670         {
18671           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18672           add_AT_specification (subr_die, old_die);
18673           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18674             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18675           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18676             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18677         }
18678     }
18679   else
18680     {
18681       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18682
18683       if (TREE_PUBLIC (decl))
18684         add_AT_flag (subr_die, DW_AT_external, 1);
18685
18686       add_name_and_src_coords_attributes (subr_die, decl);
18687       if (debug_info_level > DINFO_LEVEL_TERSE)
18688         {
18689           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18690           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18691                               0, 0, context_die);
18692         }
18693
18694       add_pure_or_virtual_attribute (subr_die, decl);
18695       if (DECL_ARTIFICIAL (decl))
18696         add_AT_flag (subr_die, DW_AT_artificial, 1);
18697
18698       add_accessibility_attribute (subr_die, decl);
18699     }
18700
18701   if (declaration)
18702     {
18703       if (!old_die || !get_AT (old_die, DW_AT_inline))
18704         {
18705           add_AT_flag (subr_die, DW_AT_declaration, 1);
18706
18707           /* If this is an explicit function declaration then generate
18708              a DW_AT_explicit attribute.  */
18709           if (lang_hooks.decls.function_decl_explicit_p (decl)
18710               && (dwarf_version >= 3 || !dwarf_strict))
18711             add_AT_flag (subr_die, DW_AT_explicit, 1);
18712
18713           /* The first time we see a member function, it is in the context of
18714              the class to which it belongs.  We make sure of this by emitting
18715              the class first.  The next time is the definition, which is
18716              handled above.  The two may come from the same source text.
18717
18718              Note that force_decl_die() forces function declaration die. It is
18719              later reused to represent definition.  */
18720           equate_decl_number_to_die (decl, subr_die);
18721         }
18722     }
18723   else if (DECL_ABSTRACT (decl))
18724     {
18725       if (DECL_DECLARED_INLINE_P (decl))
18726         {
18727           if (cgraph_function_possibly_inlined_p (decl))
18728             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18729           else
18730             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18731         }
18732       else
18733         {
18734           if (cgraph_function_possibly_inlined_p (decl))
18735             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18736           else
18737             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18738         }
18739
18740       if (DECL_DECLARED_INLINE_P (decl)
18741           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18742         add_AT_flag (subr_die, DW_AT_artificial, 1);
18743
18744       equate_decl_number_to_die (decl, subr_die);
18745     }
18746   else if (!DECL_EXTERNAL (decl))
18747     {
18748       HOST_WIDE_INT cfa_fb_offset;
18749
18750       if (!old_die || !get_AT (old_die, DW_AT_inline))
18751         equate_decl_number_to_die (decl, subr_die);
18752
18753       if (!flag_reorder_blocks_and_partition)
18754         {
18755           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18756                                        current_function_funcdef_no);
18757           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18758           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18759                                        current_function_funcdef_no);
18760           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18761
18762 #if VMS_DEBUGGING_INFO
18763       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18764          Section 2.3 Prologue and Epilogue Attributes:
18765          When a breakpoint is set on entry to a function, it is generally
18766          desirable for execution to be suspended, not on the very first
18767          instruction of the function, but rather at a point after the
18768          function's frame has been set up, after any language defined local
18769          declaration processing has been completed, and before execution of
18770          the first statement of the function begins. Debuggers generally
18771          cannot properly determine where this point is.  Similarly for a
18772          breakpoint set on exit from a function. The prologue and epilogue
18773          attributes allow a compiler to communicate the location(s) to use.  */
18774
18775       {
18776         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18777
18778         if (fde->dw_fde_vms_end_prologue)
18779           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18780             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18781
18782         if (fde->dw_fde_vms_begin_epilogue)
18783           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18784             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18785       }
18786 #endif
18787
18788           add_pubname (decl, subr_die);
18789           add_arange (decl, subr_die);
18790         }
18791       else
18792         {  /* Do nothing for now; maybe need to duplicate die, one for
18793               hot section and one for cold section, then use the hot/cold
18794               section begin/end labels to generate the aranges...  */
18795           /*
18796             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18797             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18798             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18799             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18800
18801             add_pubname (decl, subr_die);
18802             add_arange (decl, subr_die);
18803             add_arange (decl, subr_die);
18804            */
18805         }
18806
18807 #ifdef MIPS_DEBUGGING_INFO
18808       /* Add a reference to the FDE for this routine.  */
18809       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18810 #endif
18811
18812       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18813
18814       /* We define the "frame base" as the function's CFA.  This is more
18815          convenient for several reasons: (1) It's stable across the prologue
18816          and epilogue, which makes it better than just a frame pointer,
18817          (2) With dwarf3, there exists a one-byte encoding that allows us
18818          to reference the .debug_frame data by proxy, but failing that,
18819          (3) We can at least reuse the code inspection and interpretation
18820          code that determines the CFA position at various points in the
18821          function.  */
18822       if (dwarf_version >= 3)
18823         {
18824           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18825           add_AT_loc (subr_die, DW_AT_frame_base, op);
18826         }
18827       else
18828         {
18829           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18830           if (list->dw_loc_next)
18831             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18832           else
18833             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18834         }
18835
18836       /* Compute a displacement from the "steady-state frame pointer" to
18837          the CFA.  The former is what all stack slots and argument slots
18838          will reference in the rtl; the later is what we've told the
18839          debugger about.  We'll need to adjust all frame_base references
18840          by this displacement.  */
18841       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18842
18843       if (cfun->static_chain_decl)
18844         add_AT_location_description (subr_die, DW_AT_static_link,
18845                  loc_list_from_tree (cfun->static_chain_decl, 2));
18846     }
18847
18848   /* Generate child dies for template paramaters.  */
18849   if (debug_info_level > DINFO_LEVEL_TERSE)
18850     gen_generic_params_dies (decl);
18851
18852   /* Now output descriptions of the arguments for this function. This gets
18853      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18854      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18855      `...' at the end of the formal parameter list.  In order to find out if
18856      there was a trailing ellipsis or not, we must instead look at the type
18857      associated with the FUNCTION_DECL.  This will be a node of type
18858      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18859      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18860      an ellipsis at the end.  */
18861
18862   /* In the case where we are describing a mere function declaration, all we
18863      need to do here (and all we *can* do here) is to describe the *types* of
18864      its formal parameters.  */
18865   if (debug_info_level <= DINFO_LEVEL_TERSE)
18866     ;
18867   else if (declaration)
18868     gen_formal_types_die (decl, subr_die);
18869   else
18870     {
18871       /* Generate DIEs to represent all known formal parameters.  */
18872       tree parm = DECL_ARGUMENTS (decl);
18873       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18874       tree generic_decl_parm = generic_decl
18875                                 ? DECL_ARGUMENTS (generic_decl)
18876                                 : NULL;
18877
18878       /* Now we want to walk the list of parameters of the function and
18879          emit their relevant DIEs.
18880
18881          We consider the case of DECL being an instance of a generic function
18882          as well as it being a normal function.
18883
18884          If DECL is an instance of a generic function we walk the
18885          parameters of the generic function declaration _and_ the parameters of
18886          DECL itself. This is useful because we want to emit specific DIEs for
18887          function parameter packs and those are declared as part of the
18888          generic function declaration. In that particular case,
18889          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18890          That DIE has children DIEs representing the set of arguments
18891          of the pack. Note that the set of pack arguments can be empty.
18892          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18893          children DIE.
18894
18895          Otherwise, we just consider the parameters of DECL.  */
18896       while (generic_decl_parm || parm)
18897         {
18898           if (generic_decl_parm
18899               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18900             gen_formal_parameter_pack_die (generic_decl_parm,
18901                                            parm, subr_die,
18902                                            &parm);
18903           else if (parm)
18904             {
18905               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18906
18907               if (parm == DECL_ARGUMENTS (decl)
18908                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18909                   && parm_die
18910                   && (dwarf_version >= 3 || !dwarf_strict))
18911                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18912
18913               parm = DECL_CHAIN (parm);
18914             }
18915
18916           if (generic_decl_parm)
18917             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18918         }
18919
18920       /* Decide whether we need an unspecified_parameters DIE at the end.
18921          There are 2 more cases to do this for: 1) the ansi ... declaration -
18922          this is detectable when the end of the arg list is not a
18923          void_type_node 2) an unprototyped function declaration (not a
18924          definition).  This just means that we have no info about the
18925          parameters at all.  */
18926       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18927       if (fn_arg_types != NULL)
18928         {
18929           /* This is the prototyped case, check for....  */
18930           if (stdarg_p (TREE_TYPE (decl)))
18931             gen_unspecified_parameters_die (decl, subr_die);
18932         }
18933       else if (DECL_INITIAL (decl) == NULL_TREE)
18934         gen_unspecified_parameters_die (decl, subr_die);
18935     }
18936
18937   /* Output Dwarf info for all of the stuff within the body of the function
18938      (if it has one - it may be just a declaration).  */
18939   outer_scope = DECL_INITIAL (decl);
18940
18941   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18942      a function.  This BLOCK actually represents the outermost binding contour
18943      for the function, i.e. the contour in which the function's formal
18944      parameters and labels get declared. Curiously, it appears that the front
18945      end doesn't actually put the PARM_DECL nodes for the current function onto
18946      the BLOCK_VARS list for this outer scope, but are strung off of the
18947      DECL_ARGUMENTS list for the function instead.
18948
18949      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18950      the LABEL_DECL nodes for the function however, and we output DWARF info
18951      for those in decls_for_scope.  Just within the `outer_scope' there will be
18952      a BLOCK node representing the function's outermost pair of curly braces,
18953      and any blocks used for the base and member initializers of a C++
18954      constructor function.  */
18955   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18956     {
18957       /* Emit a DW_TAG_variable DIE for a named return value.  */
18958       if (DECL_NAME (DECL_RESULT (decl)))
18959         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18960
18961       current_function_has_inlines = 0;
18962       decls_for_scope (outer_scope, subr_die, 0);
18963
18964 #if 0 && defined (MIPS_DEBUGGING_INFO)
18965       if (current_function_has_inlines)
18966         {
18967           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18968           if (! comp_unit_has_inlines)
18969             {
18970               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18971               comp_unit_has_inlines = 1;
18972             }
18973         }
18974 #endif
18975     }
18976   /* Add the calling convention attribute if requested.  */
18977   add_calling_convention_attribute (subr_die, decl);
18978
18979 }
18980
18981 /* Returns a hash value for X (which really is a die_struct).  */
18982
18983 static hashval_t
18984 common_block_die_table_hash (const void *x)
18985 {
18986   const_dw_die_ref d = (const_dw_die_ref) x;
18987   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18988 }
18989
18990 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18991    as decl_id and die_parent of die_struct Y.  */
18992
18993 static int
18994 common_block_die_table_eq (const void *x, const void *y)
18995 {
18996   const_dw_die_ref d = (const_dw_die_ref) x;
18997   const_dw_die_ref e = (const_dw_die_ref) y;
18998   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18999 }
19000
19001 /* Generate a DIE to represent a declared data object.
19002    Either DECL or ORIGIN must be non-null.  */
19003
19004 static void
19005 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19006 {
19007   HOST_WIDE_INT off;
19008   tree com_decl;
19009   tree decl_or_origin = decl ? decl : origin;
19010   tree ultimate_origin;
19011   dw_die_ref var_die;
19012   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19013   dw_die_ref origin_die;
19014   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19015                       || class_or_namespace_scope_p (context_die));
19016   bool specialization_p = false;
19017
19018   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19019   if (decl || ultimate_origin)
19020     origin = ultimate_origin;
19021   com_decl = fortran_common (decl_or_origin, &off);
19022
19023   /* Symbol in common gets emitted as a child of the common block, in the form
19024      of a data member.  */
19025   if (com_decl)
19026     {
19027       dw_die_ref com_die;
19028       dw_loc_list_ref loc;
19029       die_node com_die_arg;
19030
19031       var_die = lookup_decl_die (decl_or_origin);
19032       if (var_die)
19033         {
19034           if (get_AT (var_die, DW_AT_location) == NULL)
19035             {
19036               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19037               if (loc)
19038                 {
19039                   if (off)
19040                     {
19041                       /* Optimize the common case.  */
19042                       if (single_element_loc_list_p (loc)
19043                           && loc->expr->dw_loc_opc == DW_OP_addr
19044                           && loc->expr->dw_loc_next == NULL
19045                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19046                              == SYMBOL_REF)
19047                         loc->expr->dw_loc_oprnd1.v.val_addr
19048                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19049                         else
19050                           loc_list_plus_const (loc, off);
19051                     }
19052                   add_AT_location_description (var_die, DW_AT_location, loc);
19053                   remove_AT (var_die, DW_AT_declaration);
19054                 }
19055             }
19056           return;
19057         }
19058
19059       if (common_block_die_table == NULL)
19060         common_block_die_table
19061           = htab_create_ggc (10, common_block_die_table_hash,
19062                              common_block_die_table_eq, NULL);
19063
19064       com_die_arg.decl_id = DECL_UID (com_decl);
19065       com_die_arg.die_parent = context_die;
19066       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19067       loc = loc_list_from_tree (com_decl, 2);
19068       if (com_die == NULL)
19069         {
19070           const char *cnam
19071             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19072           void **slot;
19073
19074           com_die = new_die (DW_TAG_common_block, context_die, decl);
19075           add_name_and_src_coords_attributes (com_die, com_decl);
19076           if (loc)
19077             {
19078               add_AT_location_description (com_die, DW_AT_location, loc);
19079               /* Avoid sharing the same loc descriptor between
19080                  DW_TAG_common_block and DW_TAG_variable.  */
19081               loc = loc_list_from_tree (com_decl, 2);
19082             }
19083           else if (DECL_EXTERNAL (decl))
19084             add_AT_flag (com_die, DW_AT_declaration, 1);
19085           add_pubname_string (cnam, com_die); /* ??? needed? */
19086           com_die->decl_id = DECL_UID (com_decl);
19087           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19088           *slot = (void *) com_die;
19089         }
19090       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19091         {
19092           add_AT_location_description (com_die, DW_AT_location, loc);
19093           loc = loc_list_from_tree (com_decl, 2);
19094           remove_AT (com_die, DW_AT_declaration);
19095         }
19096       var_die = new_die (DW_TAG_variable, com_die, decl);
19097       add_name_and_src_coords_attributes (var_die, decl);
19098       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19099                           TREE_THIS_VOLATILE (decl), context_die);
19100       add_AT_flag (var_die, DW_AT_external, 1);
19101       if (loc)
19102         {
19103           if (off)
19104             {
19105               /* Optimize the common case.  */
19106               if (single_element_loc_list_p (loc)
19107                   && loc->expr->dw_loc_opc == DW_OP_addr
19108                   && loc->expr->dw_loc_next == NULL
19109                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19110                 loc->expr->dw_loc_oprnd1.v.val_addr
19111                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19112               else
19113                 loc_list_plus_const (loc, off);
19114             }
19115           add_AT_location_description (var_die, DW_AT_location, loc);
19116         }
19117       else if (DECL_EXTERNAL (decl))
19118         add_AT_flag (var_die, DW_AT_declaration, 1);
19119       equate_decl_number_to_die (decl, var_die);
19120       return;
19121     }
19122
19123   /* If the compiler emitted a definition for the DECL declaration
19124      and if we already emitted a DIE for it, don't emit a second
19125      DIE for it again. Allow re-declarations of DECLs that are
19126      inside functions, though.  */
19127   if (old_die && declaration && !local_scope_p (context_die))
19128     return;
19129
19130   /* For static data members, the declaration in the class is supposed
19131      to have DW_TAG_member tag; the specification should still be
19132      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19133   if (declaration && class_scope_p (context_die))
19134     var_die = new_die (DW_TAG_member, context_die, decl);
19135   else
19136     var_die = new_die (DW_TAG_variable, context_die, decl);
19137
19138   origin_die = NULL;
19139   if (origin != NULL)
19140     origin_die = add_abstract_origin_attribute (var_die, origin);
19141
19142   /* Loop unrolling can create multiple blocks that refer to the same
19143      static variable, so we must test for the DW_AT_declaration flag.
19144
19145      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19146      copy decls and set the DECL_ABSTRACT flag on them instead of
19147      sharing them.
19148
19149      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19150
19151      ??? The declare_in_namespace support causes us to get two DIEs for one
19152      variable, both of which are declarations.  We want to avoid considering
19153      one to be a specification, so we must test that this DIE is not a
19154      declaration.  */
19155   else if (old_die && TREE_STATIC (decl) && ! declaration
19156            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19157     {
19158       /* This is a definition of a C++ class level static.  */
19159       add_AT_specification (var_die, old_die);
19160       specialization_p = true;
19161       if (DECL_NAME (decl))
19162         {
19163           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19164           struct dwarf_file_data * file_index = lookup_filename (s.file);
19165
19166           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19167             add_AT_file (var_die, DW_AT_decl_file, file_index);
19168
19169           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19170             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19171
19172           if (old_die->die_tag == DW_TAG_member)
19173             add_linkage_name (var_die, decl);
19174         }
19175     }
19176   else
19177     add_name_and_src_coords_attributes (var_die, decl);
19178
19179   if ((origin == NULL && !specialization_p)
19180       || (origin != NULL
19181           && !DECL_ABSTRACT (decl_or_origin)
19182           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19183                                        decl_function_context
19184                                                         (decl_or_origin))))
19185     {
19186       tree type = TREE_TYPE (decl_or_origin);
19187
19188       if (decl_by_reference_p (decl_or_origin))
19189         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19190       else
19191         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19192                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19193     }
19194
19195   if (origin == NULL && !specialization_p)
19196     {
19197       if (TREE_PUBLIC (decl))
19198         add_AT_flag (var_die, DW_AT_external, 1);
19199
19200       if (DECL_ARTIFICIAL (decl))
19201         add_AT_flag (var_die, DW_AT_artificial, 1);
19202
19203       add_accessibility_attribute (var_die, decl);
19204     }
19205
19206   if (declaration)
19207     add_AT_flag (var_die, DW_AT_declaration, 1);
19208
19209   if (decl && (DECL_ABSTRACT (decl) || declaration))
19210     equate_decl_number_to_die (decl, var_die);
19211
19212   if (! declaration
19213       && (! DECL_ABSTRACT (decl_or_origin)
19214           /* Local static vars are shared between all clones/inlines,
19215              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19216              already set.  */
19217           || (TREE_CODE (decl_or_origin) == VAR_DECL
19218               && TREE_STATIC (decl_or_origin)
19219               && DECL_RTL_SET_P (decl_or_origin)))
19220       /* When abstract origin already has DW_AT_location attribute, no need
19221          to add it again.  */
19222       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19223     {
19224       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19225           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19226         defer_location (decl_or_origin, var_die);
19227       else
19228         add_location_or_const_value_attribute (var_die,
19229                                                decl_or_origin,
19230                                                DW_AT_location);
19231       add_pubname (decl_or_origin, var_die);
19232     }
19233   else
19234     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19235 }
19236
19237 /* Generate a DIE to represent a named constant.  */
19238
19239 static void
19240 gen_const_die (tree decl, dw_die_ref context_die)
19241 {
19242   dw_die_ref const_die;
19243   tree type = TREE_TYPE (decl);
19244
19245   const_die = new_die (DW_TAG_constant, context_die, decl);
19246   add_name_and_src_coords_attributes (const_die, decl);
19247   add_type_attribute (const_die, type, 1, 0, context_die);
19248   if (TREE_PUBLIC (decl))
19249     add_AT_flag (const_die, DW_AT_external, 1);
19250   if (DECL_ARTIFICIAL (decl))
19251     add_AT_flag (const_die, DW_AT_artificial, 1);
19252   tree_add_const_value_attribute_for_decl (const_die, decl);
19253 }
19254
19255 /* Generate a DIE to represent a label identifier.  */
19256
19257 static void
19258 gen_label_die (tree decl, dw_die_ref context_die)
19259 {
19260   tree origin = decl_ultimate_origin (decl);
19261   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19262   rtx insn;
19263   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19264
19265   if (origin != NULL)
19266     add_abstract_origin_attribute (lbl_die, origin);
19267   else
19268     add_name_and_src_coords_attributes (lbl_die, decl);
19269
19270   if (DECL_ABSTRACT (decl))
19271     equate_decl_number_to_die (decl, lbl_die);
19272   else
19273     {
19274       insn = DECL_RTL_IF_SET (decl);
19275
19276       /* Deleted labels are programmer specified labels which have been
19277          eliminated because of various optimizations.  We still emit them
19278          here so that it is possible to put breakpoints on them.  */
19279       if (insn
19280           && (LABEL_P (insn)
19281               || ((NOTE_P (insn)
19282                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19283         {
19284           /* When optimization is enabled (via -O) some parts of the compiler
19285              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19286              represent source-level labels which were explicitly declared by
19287              the user.  This really shouldn't be happening though, so catch
19288              it if it ever does happen.  */
19289           gcc_assert (!INSN_DELETED_P (insn));
19290
19291           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19292           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19293         }
19294     }
19295 }
19296
19297 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19298    attributes to the DIE for a block STMT, to describe where the inlined
19299    function was called from.  This is similar to add_src_coords_attributes.  */
19300
19301 static inline void
19302 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19303 {
19304   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19305
19306   if (dwarf_version >= 3 || !dwarf_strict)
19307     {
19308       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19309       add_AT_unsigned (die, DW_AT_call_line, s.line);
19310     }
19311 }
19312
19313
19314 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19315    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19316
19317 static inline void
19318 add_high_low_attributes (tree stmt, dw_die_ref die)
19319 {
19320   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19321
19322   if (BLOCK_FRAGMENT_CHAIN (stmt)
19323       && (dwarf_version >= 3 || !dwarf_strict))
19324     {
19325       tree chain;
19326
19327       if (inlined_function_outer_scope_p (stmt))
19328         {
19329           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19330                                        BLOCK_NUMBER (stmt));
19331           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19332         }
19333
19334       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19335
19336       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19337       do
19338         {
19339           add_ranges (chain);
19340           chain = BLOCK_FRAGMENT_CHAIN (chain);
19341         }
19342       while (chain);
19343       add_ranges (NULL);
19344     }
19345   else
19346     {
19347       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19348                                    BLOCK_NUMBER (stmt));
19349       add_AT_lbl_id (die, DW_AT_low_pc, label);
19350       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19351                                    BLOCK_NUMBER (stmt));
19352       add_AT_lbl_id (die, DW_AT_high_pc, label);
19353     }
19354 }
19355
19356 /* Generate a DIE for a lexical block.  */
19357
19358 static void
19359 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19360 {
19361   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19362
19363   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19364     add_high_low_attributes (stmt, stmt_die);
19365
19366   decls_for_scope (stmt, stmt_die, depth);
19367 }
19368
19369 /* Generate a DIE for an inlined subprogram.  */
19370
19371 static void
19372 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19373 {
19374   tree decl;
19375
19376   /* The instance of function that is effectively being inlined shall not
19377      be abstract.  */
19378   gcc_assert (! BLOCK_ABSTRACT (stmt));
19379
19380   decl = block_ultimate_origin (stmt);
19381
19382   /* Emit info for the abstract instance first, if we haven't yet.  We
19383      must emit this even if the block is abstract, otherwise when we
19384      emit the block below (or elsewhere), we may end up trying to emit
19385      a die whose origin die hasn't been emitted, and crashing.  */
19386   dwarf2out_abstract_function (decl);
19387
19388   if (! BLOCK_ABSTRACT (stmt))
19389     {
19390       dw_die_ref subr_die
19391         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19392
19393       add_abstract_origin_attribute (subr_die, decl);
19394       if (TREE_ASM_WRITTEN (stmt))
19395         add_high_low_attributes (stmt, subr_die);
19396       add_call_src_coords_attributes (stmt, subr_die);
19397
19398       decls_for_scope (stmt, subr_die, depth);
19399       current_function_has_inlines = 1;
19400     }
19401 }
19402
19403 /* Generate a DIE for a field in a record, or structure.  */
19404
19405 static void
19406 gen_field_die (tree decl, dw_die_ref context_die)
19407 {
19408   dw_die_ref decl_die;
19409
19410   if (TREE_TYPE (decl) == error_mark_node)
19411     return;
19412
19413   decl_die = new_die (DW_TAG_member, context_die, decl);
19414   add_name_and_src_coords_attributes (decl_die, decl);
19415   add_type_attribute (decl_die, member_declared_type (decl),
19416                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19417                       context_die);
19418
19419   if (DECL_BIT_FIELD_TYPE (decl))
19420     {
19421       add_byte_size_attribute (decl_die, decl);
19422       add_bit_size_attribute (decl_die, decl);
19423       add_bit_offset_attribute (decl_die, decl);
19424     }
19425
19426   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19427     add_data_member_location_attribute (decl_die, decl);
19428
19429   if (DECL_ARTIFICIAL (decl))
19430     add_AT_flag (decl_die, DW_AT_artificial, 1);
19431
19432   add_accessibility_attribute (decl_die, decl);
19433
19434   /* Equate decl number to die, so that we can look up this decl later on.  */
19435   equate_decl_number_to_die (decl, decl_die);
19436 }
19437
19438 #if 0
19439 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19440    Use modified_type_die instead.
19441    We keep this code here just in case these types of DIEs may be needed to
19442    represent certain things in other languages (e.g. Pascal) someday.  */
19443
19444 static void
19445 gen_pointer_type_die (tree type, dw_die_ref context_die)
19446 {
19447   dw_die_ref ptr_die
19448     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19449
19450   equate_type_number_to_die (type, ptr_die);
19451   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19452   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19453 }
19454
19455 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19456    Use modified_type_die instead.
19457    We keep this code here just in case these types of DIEs may be needed to
19458    represent certain things in other languages (e.g. Pascal) someday.  */
19459
19460 static void
19461 gen_reference_type_die (tree type, dw_die_ref context_die)
19462 {
19463   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19464
19465   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19466     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19467   else
19468     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19469
19470   equate_type_number_to_die (type, ref_die);
19471   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19472   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19473 }
19474 #endif
19475
19476 /* Generate a DIE for a pointer to a member type.  */
19477
19478 static void
19479 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19480 {
19481   dw_die_ref ptr_die
19482     = new_die (DW_TAG_ptr_to_member_type,
19483                scope_die_for (type, context_die), type);
19484
19485   equate_type_number_to_die (type, ptr_die);
19486   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19487                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19488   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19489 }
19490
19491 /* Generate the DIE for the compilation unit.  */
19492
19493 static dw_die_ref
19494 gen_compile_unit_die (const char *filename)
19495 {
19496   dw_die_ref die;
19497   char producer[250];
19498   const char *language_string = lang_hooks.name;
19499   int language;
19500
19501   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19502
19503   if (filename)
19504     {
19505       add_name_attribute (die, filename);
19506       /* Don't add cwd for <built-in>.  */
19507       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19508         add_comp_dir_attribute (die);
19509     }
19510
19511   sprintf (producer, "%s %s", language_string, version_string);
19512
19513 #ifdef MIPS_DEBUGGING_INFO
19514   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19515      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19516      not appear in the producer string, the debugger reaches the conclusion
19517      that the object file is stripped and has no debugging information.
19518      To get the MIPS/SGI debugger to believe that there is debugging
19519      information in the object file, we add a -g to the producer string.  */
19520   if (debug_info_level > DINFO_LEVEL_TERSE)
19521     strcat (producer, " -g");
19522 #endif
19523
19524   add_AT_string (die, DW_AT_producer, producer);
19525
19526   language = DW_LANG_C89;
19527   if (strcmp (language_string, "GNU C++") == 0)
19528     language = DW_LANG_C_plus_plus;
19529   else if (strcmp (language_string, "GNU F77") == 0)
19530     language = DW_LANG_Fortran77;
19531   else if (strcmp (language_string, "GNU Pascal") == 0)
19532     language = DW_LANG_Pascal83;
19533   else if (dwarf_version >= 3 || !dwarf_strict)
19534     {
19535       if (strcmp (language_string, "GNU Ada") == 0)
19536         language = DW_LANG_Ada95;
19537       else if (strcmp (language_string, "GNU Fortran") == 0)
19538         language = DW_LANG_Fortran95;
19539       else if (strcmp (language_string, "GNU Java") == 0)
19540         language = DW_LANG_Java;
19541       else if (strcmp (language_string, "GNU Objective-C") == 0)
19542         language = DW_LANG_ObjC;
19543       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19544         language = DW_LANG_ObjC_plus_plus;
19545     }
19546
19547   add_AT_unsigned (die, DW_AT_language, language);
19548
19549   switch (language)
19550     {
19551     case DW_LANG_Fortran77:
19552     case DW_LANG_Fortran90:
19553     case DW_LANG_Fortran95:
19554       /* Fortran has case insensitive identifiers and the front-end
19555          lowercases everything.  */
19556       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19557       break;
19558     default:
19559       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19560       break;
19561     }
19562   return die;
19563 }
19564
19565 /* Generate the DIE for a base class.  */
19566
19567 static void
19568 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19569 {
19570   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19571
19572   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19573   add_data_member_location_attribute (die, binfo);
19574
19575   if (BINFO_VIRTUAL_P (binfo))
19576     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19577
19578   if (access == access_public_node)
19579     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19580   else if (access == access_protected_node)
19581     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19582 }
19583
19584 /* Generate a DIE for a class member.  */
19585
19586 static void
19587 gen_member_die (tree type, dw_die_ref context_die)
19588 {
19589   tree member;
19590   tree binfo = TYPE_BINFO (type);
19591   dw_die_ref child;
19592
19593   /* If this is not an incomplete type, output descriptions of each of its
19594      members. Note that as we output the DIEs necessary to represent the
19595      members of this record or union type, we will also be trying to output
19596      DIEs to represent the *types* of those members. However the `type'
19597      function (above) will specifically avoid generating type DIEs for member
19598      types *within* the list of member DIEs for this (containing) type except
19599      for those types (of members) which are explicitly marked as also being
19600      members of this (containing) type themselves.  The g++ front- end can
19601      force any given type to be treated as a member of some other (containing)
19602      type by setting the TYPE_CONTEXT of the given (member) type to point to
19603      the TREE node representing the appropriate (containing) type.  */
19604
19605   /* First output info about the base classes.  */
19606   if (binfo)
19607     {
19608       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19609       int i;
19610       tree base;
19611
19612       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19613         gen_inheritance_die (base,
19614                              (accesses ? VEC_index (tree, accesses, i)
19615                               : access_public_node), context_die);
19616     }
19617
19618   /* Now output info about the data members and type members.  */
19619   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19620     {
19621       /* If we thought we were generating minimal debug info for TYPE
19622          and then changed our minds, some of the member declarations
19623          may have already been defined.  Don't define them again, but
19624          do put them in the right order.  */
19625
19626       child = lookup_decl_die (member);
19627       if (child)
19628         splice_child_die (context_die, child);
19629       else
19630         gen_decl_die (member, NULL, context_die);
19631     }
19632
19633   /* Now output info about the function members (if any).  */
19634   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19635     {
19636       /* Don't include clones in the member list.  */
19637       if (DECL_ABSTRACT_ORIGIN (member))
19638         continue;
19639
19640       child = lookup_decl_die (member);
19641       if (child)
19642         splice_child_die (context_die, child);
19643       else
19644         gen_decl_die (member, NULL, context_die);
19645     }
19646 }
19647
19648 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19649    is set, we pretend that the type was never defined, so we only get the
19650    member DIEs needed by later specification DIEs.  */
19651
19652 static void
19653 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19654                                 enum debug_info_usage usage)
19655 {
19656   dw_die_ref type_die = lookup_type_die (type);
19657   dw_die_ref scope_die = 0;
19658   int nested = 0;
19659   int complete = (TYPE_SIZE (type)
19660                   && (! TYPE_STUB_DECL (type)
19661                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19662   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19663   complete = complete && should_emit_struct_debug (type, usage);
19664
19665   if (type_die && ! complete)
19666     return;
19667
19668   if (TYPE_CONTEXT (type) != NULL_TREE
19669       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19670           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19671     nested = 1;
19672
19673   scope_die = scope_die_for (type, context_die);
19674
19675   if (! type_die || (nested && scope_die == comp_unit_die))
19676     /* First occurrence of type or toplevel definition of nested class.  */
19677     {
19678       dw_die_ref old_die = type_die;
19679
19680       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19681                           ? record_type_tag (type) : DW_TAG_union_type,
19682                           scope_die, type);
19683       equate_type_number_to_die (type, type_die);
19684       if (old_die)
19685         add_AT_specification (type_die, old_die);
19686       else
19687         add_name_attribute (type_die, type_tag (type));
19688     }
19689   else
19690     remove_AT (type_die, DW_AT_declaration);
19691
19692   /* Generate child dies for template paramaters.  */
19693   if (debug_info_level > DINFO_LEVEL_TERSE
19694       && COMPLETE_TYPE_P (type))
19695     gen_generic_params_dies (type);
19696
19697   /* If this type has been completed, then give it a byte_size attribute and
19698      then give a list of members.  */
19699   if (complete && !ns_decl)
19700     {
19701       /* Prevent infinite recursion in cases where the type of some member of
19702          this type is expressed in terms of this type itself.  */
19703       TREE_ASM_WRITTEN (type) = 1;
19704       add_byte_size_attribute (type_die, type);
19705       if (TYPE_STUB_DECL (type) != NULL_TREE)
19706         {
19707           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19708           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19709         }
19710
19711       /* If the first reference to this type was as the return type of an
19712          inline function, then it may not have a parent.  Fix this now.  */
19713       if (type_die->die_parent == NULL)
19714         add_child_die (scope_die, type_die);
19715
19716       push_decl_scope (type);
19717       gen_member_die (type, type_die);
19718       pop_decl_scope ();
19719
19720       /* GNU extension: Record what type our vtable lives in.  */
19721       if (TYPE_VFIELD (type))
19722         {
19723           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19724
19725           gen_type_die (vtype, context_die);
19726           add_AT_die_ref (type_die, DW_AT_containing_type,
19727                           lookup_type_die (vtype));
19728         }
19729     }
19730   else
19731     {
19732       add_AT_flag (type_die, DW_AT_declaration, 1);
19733
19734       /* We don't need to do this for function-local types.  */
19735       if (TYPE_STUB_DECL (type)
19736           && ! decl_function_context (TYPE_STUB_DECL (type)))
19737         VEC_safe_push (tree, gc, incomplete_types, type);
19738     }
19739
19740   if (get_AT (type_die, DW_AT_name))
19741     add_pubtype (type, type_die);
19742 }
19743
19744 /* Generate a DIE for a subroutine _type_.  */
19745
19746 static void
19747 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19748 {
19749   tree return_type = TREE_TYPE (type);
19750   dw_die_ref subr_die
19751     = new_die (DW_TAG_subroutine_type,
19752                scope_die_for (type, context_die), type);
19753
19754   equate_type_number_to_die (type, subr_die);
19755   add_prototyped_attribute (subr_die, type);
19756   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19757   gen_formal_types_die (type, subr_die);
19758
19759   if (get_AT (subr_die, DW_AT_name))
19760     add_pubtype (type, subr_die);
19761 }
19762
19763 /* Generate a DIE for a type definition.  */
19764
19765 static void
19766 gen_typedef_die (tree decl, dw_die_ref context_die)
19767 {
19768   dw_die_ref type_die;
19769   tree origin;
19770
19771   if (TREE_ASM_WRITTEN (decl))
19772     return;
19773
19774   TREE_ASM_WRITTEN (decl) = 1;
19775   type_die = new_die (DW_TAG_typedef, context_die, decl);
19776   origin = decl_ultimate_origin (decl);
19777   if (origin != NULL)
19778     add_abstract_origin_attribute (type_die, origin);
19779   else
19780     {
19781       tree type;
19782
19783       add_name_and_src_coords_attributes (type_die, decl);
19784       if (DECL_ORIGINAL_TYPE (decl))
19785         {
19786           type = DECL_ORIGINAL_TYPE (decl);
19787
19788           gcc_assert (type != TREE_TYPE (decl));
19789           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19790         }
19791       else
19792         {
19793           type = TREE_TYPE (decl);
19794
19795           if (is_naming_typedef_decl (TYPE_NAME (type)))
19796             {
19797               /* Here, we are in the case of decl being a typedef naming
19798                  an anonymous type, e.g:
19799                      typedef struct {...} foo;
19800                  In that case TREE_TYPE (decl) is not a typedef variant
19801                  type and TYPE_NAME of the anonymous type is set to the
19802                  TYPE_DECL of the typedef. This construct is emitted by
19803                  the C++ FE.
19804
19805                  TYPE is the anonymous struct named by the typedef
19806                  DECL. As we need the DW_AT_type attribute of the
19807                  DW_TAG_typedef to point to the DIE of TYPE, let's
19808                  generate that DIE right away. add_type_attribute
19809                  called below will then pick (via lookup_type_die) that
19810                  anonymous struct DIE.  */
19811               if (!TREE_ASM_WRITTEN (type))
19812                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19813             }
19814         }
19815
19816       add_type_attribute (type_die, type, TREE_READONLY (decl),
19817                           TREE_THIS_VOLATILE (decl), context_die);
19818
19819       if (is_naming_typedef_decl (decl))
19820         /* We want that all subsequent calls to lookup_type_die with
19821            TYPE in argument yield the DW_TAG_typedef we have just
19822            created.  */
19823         equate_type_number_to_die (type, type_die);
19824
19825       add_accessibility_attribute (type_die, decl);
19826     }
19827
19828   if (DECL_ABSTRACT (decl))
19829     equate_decl_number_to_die (decl, type_die);
19830
19831   if (get_AT (type_die, DW_AT_name))
19832     add_pubtype (decl, type_die);
19833 }
19834
19835 /* Generate a DIE for a struct, class, enum or union type.  */
19836
19837 static void
19838 gen_tagged_type_die (tree type,
19839                      dw_die_ref context_die,
19840                      enum debug_info_usage usage)
19841 {
19842   int need_pop;
19843
19844   if (type == NULL_TREE
19845       || !is_tagged_type (type))
19846     return;
19847
19848   /* If this is a nested type whose containing class hasn't been written
19849      out yet, writing it out will cover this one, too.  This does not apply
19850      to instantiations of member class templates; they need to be added to
19851      the containing class as they are generated.  FIXME: This hurts the
19852      idea of combining type decls from multiple TUs, since we can't predict
19853      what set of template instantiations we'll get.  */
19854   if (TYPE_CONTEXT (type)
19855       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19856       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19857     {
19858       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19859
19860       if (TREE_ASM_WRITTEN (type))
19861         return;
19862
19863       /* If that failed, attach ourselves to the stub.  */
19864       push_decl_scope (TYPE_CONTEXT (type));
19865       context_die = lookup_type_die (TYPE_CONTEXT (type));
19866       need_pop = 1;
19867     }
19868   else if (TYPE_CONTEXT (type) != NULL_TREE
19869            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19870     {
19871       /* If this type is local to a function that hasn't been written
19872          out yet, use a NULL context for now; it will be fixed up in
19873          decls_for_scope.  */
19874       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19875       need_pop = 0;
19876     }
19877   else
19878     {
19879       context_die = declare_in_namespace (type, context_die);
19880       need_pop = 0;
19881     }
19882
19883   if (TREE_CODE (type) == ENUMERAL_TYPE)
19884     {
19885       /* This might have been written out by the call to
19886          declare_in_namespace.  */
19887       if (!TREE_ASM_WRITTEN (type))
19888         gen_enumeration_type_die (type, context_die);
19889     }
19890   else
19891     gen_struct_or_union_type_die (type, context_die, usage);
19892
19893   if (need_pop)
19894     pop_decl_scope ();
19895
19896   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19897      it up if it is ever completed.  gen_*_type_die will set it for us
19898      when appropriate.  */
19899 }
19900
19901 /* Generate a type description DIE.  */
19902
19903 static void
19904 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19905                                 enum debug_info_usage usage)
19906 {
19907   struct array_descr_info info;
19908
19909   if (type == NULL_TREE || type == error_mark_node)
19910     return;
19911
19912   /* If TYPE is a typedef type variant, let's generate debug info
19913      for the parent typedef which TYPE is a type of.  */
19914   if (typedef_variant_p (type))
19915     {
19916       if (TREE_ASM_WRITTEN (type))
19917         return;
19918
19919       /* Prevent broken recursion; we can't hand off to the same type.  */
19920       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19921
19922       /* Use the DIE of the containing namespace as the parent DIE of
19923          the type description DIE we want to generate.  */
19924       if (DECL_CONTEXT (TYPE_NAME (type))
19925           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19926         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19927
19928       TREE_ASM_WRITTEN (type) = 1;
19929
19930       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19931       return;
19932     }
19933
19934   /* If type is an anonymous tagged type named by a typedef, let's
19935      generate debug info for the typedef.  */
19936   if (is_naming_typedef_decl (TYPE_NAME (type)))
19937     {
19938       /* Use the DIE of the containing namespace as the parent DIE of
19939          the type description DIE we want to generate.  */
19940       if (DECL_CONTEXT (TYPE_NAME (type))
19941           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19942         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19943       
19944       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19945       return;
19946     }
19947
19948   /* If this is an array type with hidden descriptor, handle it first.  */
19949   if (!TREE_ASM_WRITTEN (type)
19950       && lang_hooks.types.get_array_descr_info
19951       && lang_hooks.types.get_array_descr_info (type, &info)
19952       && (dwarf_version >= 3 || !dwarf_strict))
19953     {
19954       gen_descr_array_type_die (type, &info, context_die);
19955       TREE_ASM_WRITTEN (type) = 1;
19956       return;
19957     }
19958
19959   /* We are going to output a DIE to represent the unqualified version
19960      of this type (i.e. without any const or volatile qualifiers) so
19961      get the main variant (i.e. the unqualified version) of this type
19962      now.  (Vectors are special because the debugging info is in the
19963      cloned type itself).  */
19964   if (TREE_CODE (type) != VECTOR_TYPE)
19965     type = type_main_variant (type);
19966
19967   if (TREE_ASM_WRITTEN (type))
19968     return;
19969
19970   switch (TREE_CODE (type))
19971     {
19972     case ERROR_MARK:
19973       break;
19974
19975     case POINTER_TYPE:
19976     case REFERENCE_TYPE:
19977       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19978          ensures that the gen_type_die recursion will terminate even if the
19979          type is recursive.  Recursive types are possible in Ada.  */
19980       /* ??? We could perhaps do this for all types before the switch
19981          statement.  */
19982       TREE_ASM_WRITTEN (type) = 1;
19983
19984       /* For these types, all that is required is that we output a DIE (or a
19985          set of DIEs) to represent the "basis" type.  */
19986       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19987                                 DINFO_USAGE_IND_USE);
19988       break;
19989
19990     case OFFSET_TYPE:
19991       /* This code is used for C++ pointer-to-data-member types.
19992          Output a description of the relevant class type.  */
19993       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19994                                         DINFO_USAGE_IND_USE);
19995
19996       /* Output a description of the type of the object pointed to.  */
19997       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19998                                         DINFO_USAGE_IND_USE);
19999
20000       /* Now output a DIE to represent this pointer-to-data-member type
20001          itself.  */
20002       gen_ptr_to_mbr_type_die (type, context_die);
20003       break;
20004
20005     case FUNCTION_TYPE:
20006       /* Force out return type (in case it wasn't forced out already).  */
20007       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20008                                         DINFO_USAGE_DIR_USE);
20009       gen_subroutine_type_die (type, context_die);
20010       break;
20011
20012     case METHOD_TYPE:
20013       /* Force out return type (in case it wasn't forced out already).  */
20014       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20015                                         DINFO_USAGE_DIR_USE);
20016       gen_subroutine_type_die (type, context_die);
20017       break;
20018
20019     case ARRAY_TYPE:
20020       gen_array_type_die (type, context_die);
20021       break;
20022
20023     case VECTOR_TYPE:
20024       gen_array_type_die (type, context_die);
20025       break;
20026
20027     case ENUMERAL_TYPE:
20028     case RECORD_TYPE:
20029     case UNION_TYPE:
20030     case QUAL_UNION_TYPE:
20031       gen_tagged_type_die (type, context_die, usage);
20032       return;
20033
20034     case VOID_TYPE:
20035     case INTEGER_TYPE:
20036     case REAL_TYPE:
20037     case FIXED_POINT_TYPE:
20038     case COMPLEX_TYPE:
20039     case BOOLEAN_TYPE:
20040       /* No DIEs needed for fundamental types.  */
20041       break;
20042
20043     case LANG_TYPE:
20044       /* Just use DW_TAG_unspecified_type.  */
20045       {
20046         dw_die_ref type_die = lookup_type_die (type);
20047         if (type_die == NULL)
20048           {
20049             tree name = TYPE_NAME (type);
20050             if (TREE_CODE (name) == TYPE_DECL)
20051               name = DECL_NAME (name);
20052             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20053             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20054             equate_type_number_to_die (type, type_die);
20055           }
20056       }
20057       break;
20058
20059     default:
20060       gcc_unreachable ();
20061     }
20062
20063   TREE_ASM_WRITTEN (type) = 1;
20064 }
20065
20066 static void
20067 gen_type_die (tree type, dw_die_ref context_die)
20068 {
20069   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20070 }
20071
20072 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20073    things which are local to the given block.  */
20074
20075 static void
20076 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20077 {
20078   int must_output_die = 0;
20079   bool inlined_func;
20080
20081   /* Ignore blocks that are NULL.  */
20082   if (stmt == NULL_TREE)
20083     return;
20084
20085   inlined_func = inlined_function_outer_scope_p (stmt);
20086
20087   /* If the block is one fragment of a non-contiguous block, do not
20088      process the variables, since they will have been done by the
20089      origin block.  Do process subblocks.  */
20090   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20091     {
20092       tree sub;
20093
20094       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20095         gen_block_die (sub, context_die, depth + 1);
20096
20097       return;
20098     }
20099
20100   /* Determine if we need to output any Dwarf DIEs at all to represent this
20101      block.  */
20102   if (inlined_func)
20103     /* The outer scopes for inlinings *must* always be represented.  We
20104        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20105     must_output_die = 1;
20106   else
20107     {
20108       /* Determine if this block directly contains any "significant"
20109          local declarations which we will need to output DIEs for.  */
20110       if (debug_info_level > DINFO_LEVEL_TERSE)
20111         /* We are not in terse mode so *any* local declaration counts
20112            as being a "significant" one.  */
20113         must_output_die = ((BLOCK_VARS (stmt) != NULL
20114                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20115                            && (TREE_USED (stmt)
20116                                || TREE_ASM_WRITTEN (stmt)
20117                                || BLOCK_ABSTRACT (stmt)));
20118       else if ((TREE_USED (stmt)
20119                 || TREE_ASM_WRITTEN (stmt)
20120                 || BLOCK_ABSTRACT (stmt))
20121                && !dwarf2out_ignore_block (stmt))
20122         must_output_die = 1;
20123     }
20124
20125   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20126      DIE for any block which contains no significant local declarations at
20127      all.  Rather, in such cases we just call `decls_for_scope' so that any
20128      needed Dwarf info for any sub-blocks will get properly generated. Note
20129      that in terse mode, our definition of what constitutes a "significant"
20130      local declaration gets restricted to include only inlined function
20131      instances and local (nested) function definitions.  */
20132   if (must_output_die)
20133     {
20134       if (inlined_func)
20135         {
20136           /* If STMT block is abstract, that means we have been called
20137              indirectly from dwarf2out_abstract_function.
20138              That function rightfully marks the descendent blocks (of
20139              the abstract function it is dealing with) as being abstract,
20140              precisely to prevent us from emitting any
20141              DW_TAG_inlined_subroutine DIE as a descendent
20142              of an abstract function instance. So in that case, we should
20143              not call gen_inlined_subroutine_die.
20144
20145              Later though, when cgraph asks dwarf2out to emit info
20146              for the concrete instance of the function decl into which
20147              the concrete instance of STMT got inlined, the later will lead
20148              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20149           if (! BLOCK_ABSTRACT (stmt))
20150             gen_inlined_subroutine_die (stmt, context_die, depth);
20151         }
20152       else
20153         gen_lexical_block_die (stmt, context_die, depth);
20154     }
20155   else
20156     decls_for_scope (stmt, context_die, depth);
20157 }
20158
20159 /* Process variable DECL (or variable with origin ORIGIN) within
20160    block STMT and add it to CONTEXT_DIE.  */
20161 static void
20162 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20163 {
20164   dw_die_ref die;
20165   tree decl_or_origin = decl ? decl : origin;
20166
20167   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20168     die = lookup_decl_die (decl_or_origin);
20169   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20170            && TYPE_DECL_IS_STUB (decl_or_origin))
20171     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20172   else
20173     die = NULL;
20174
20175   if (die != NULL && die->die_parent == NULL)
20176     add_child_die (context_die, die);
20177   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20178     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20179                                          stmt, context_die);
20180   else
20181     gen_decl_die (decl, origin, context_die);
20182 }
20183
20184 /* Generate all of the decls declared within a given scope and (recursively)
20185    all of its sub-blocks.  */
20186
20187 static void
20188 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20189 {
20190   tree decl;
20191   unsigned int i;
20192   tree subblocks;
20193
20194   /* Ignore NULL blocks.  */
20195   if (stmt == NULL_TREE)
20196     return;
20197
20198   /* Output the DIEs to represent all of the data objects and typedefs
20199      declared directly within this block but not within any nested
20200      sub-blocks.  Also, nested function and tag DIEs have been
20201      generated with a parent of NULL; fix that up now.  */
20202   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20203     process_scope_var (stmt, decl, NULL_TREE, context_die);
20204   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20205     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20206                        context_die);
20207
20208   /* If we're at -g1, we're not interested in subblocks.  */
20209   if (debug_info_level <= DINFO_LEVEL_TERSE)
20210     return;
20211
20212   /* Output the DIEs to represent all sub-blocks (and the items declared
20213      therein) of this block.  */
20214   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20215        subblocks != NULL;
20216        subblocks = BLOCK_CHAIN (subblocks))
20217     gen_block_die (subblocks, context_die, depth + 1);
20218 }
20219
20220 /* Is this a typedef we can avoid emitting?  */
20221
20222 static inline int
20223 is_redundant_typedef (const_tree decl)
20224 {
20225   if (TYPE_DECL_IS_STUB (decl))
20226     return 1;
20227
20228   if (DECL_ARTIFICIAL (decl)
20229       && DECL_CONTEXT (decl)
20230       && is_tagged_type (DECL_CONTEXT (decl))
20231       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20232       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20233     /* Also ignore the artificial member typedef for the class name.  */
20234     return 1;
20235
20236   return 0;
20237 }
20238
20239 /* Return TRUE if TYPE is a typedef that names a type for linkage
20240    purposes. This kind of typedefs is produced by the C++ FE for
20241    constructs like:
20242
20243    typedef struct {...} foo;
20244
20245    In that case, there is no typedef variant type produced for foo.
20246    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20247    struct type.  */
20248
20249 static bool
20250 is_naming_typedef_decl (const_tree decl)
20251 {
20252   if (decl == NULL_TREE
20253       || TREE_CODE (decl) != TYPE_DECL
20254       || !is_tagged_type (TREE_TYPE (decl))
20255       || DECL_IS_BUILTIN (decl)
20256       || is_redundant_typedef (decl)
20257       /* It looks like Ada produces TYPE_DECLs that are very similar
20258          to C++ naming typedefs but that have different
20259          semantics. Let's be specific to c++ for now.  */
20260       || !is_cxx ())
20261     return FALSE;
20262
20263   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20264           && TYPE_NAME (TREE_TYPE (decl)) == decl
20265           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20266               != TYPE_NAME (TREE_TYPE (decl))));
20267 }
20268
20269 /* Returns the DIE for a context.  */
20270
20271 static inline dw_die_ref
20272 get_context_die (tree context)
20273 {
20274   if (context)
20275     {
20276       /* Find die that represents this context.  */
20277       if (TYPE_P (context))
20278         return force_type_die (TYPE_MAIN_VARIANT (context));
20279       else
20280         return force_decl_die (context);
20281     }
20282   return comp_unit_die;
20283 }
20284
20285 /* Returns the DIE for decl.  A DIE will always be returned.  */
20286
20287 static dw_die_ref
20288 force_decl_die (tree decl)
20289 {
20290   dw_die_ref decl_die;
20291   unsigned saved_external_flag;
20292   tree save_fn = NULL_TREE;
20293   decl_die = lookup_decl_die (decl);
20294   if (!decl_die)
20295     {
20296       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20297
20298       decl_die = lookup_decl_die (decl);
20299       if (decl_die)
20300         return decl_die;
20301
20302       switch (TREE_CODE (decl))
20303         {
20304         case FUNCTION_DECL:
20305           /* Clear current_function_decl, so that gen_subprogram_die thinks
20306              that this is a declaration. At this point, we just want to force
20307              declaration die.  */
20308           save_fn = current_function_decl;
20309           current_function_decl = NULL_TREE;
20310           gen_subprogram_die (decl, context_die);
20311           current_function_decl = save_fn;
20312           break;
20313
20314         case VAR_DECL:
20315           /* Set external flag to force declaration die. Restore it after
20316            gen_decl_die() call.  */
20317           saved_external_flag = DECL_EXTERNAL (decl);
20318           DECL_EXTERNAL (decl) = 1;
20319           gen_decl_die (decl, NULL, context_die);
20320           DECL_EXTERNAL (decl) = saved_external_flag;
20321           break;
20322
20323         case NAMESPACE_DECL:
20324           if (dwarf_version >= 3 || !dwarf_strict)
20325             dwarf2out_decl (decl);
20326           else
20327             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20328             decl_die = comp_unit_die;
20329           break;
20330
20331         default:
20332           gcc_unreachable ();
20333         }
20334
20335       /* We should be able to find the DIE now.  */
20336       if (!decl_die)
20337         decl_die = lookup_decl_die (decl);
20338       gcc_assert (decl_die);
20339     }
20340
20341   return decl_die;
20342 }
20343
20344 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20345    always returned.  */
20346
20347 static dw_die_ref
20348 force_type_die (tree type)
20349 {
20350   dw_die_ref type_die;
20351
20352   type_die = lookup_type_die (type);
20353   if (!type_die)
20354     {
20355       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20356
20357       type_die = modified_type_die (type, TYPE_READONLY (type),
20358                                     TYPE_VOLATILE (type), context_die);
20359       gcc_assert (type_die);
20360     }
20361   return type_die;
20362 }
20363
20364 /* Force out any required namespaces to be able to output DECL,
20365    and return the new context_die for it, if it's changed.  */
20366
20367 static dw_die_ref
20368 setup_namespace_context (tree thing, dw_die_ref context_die)
20369 {
20370   tree context = (DECL_P (thing)
20371                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20372   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20373     /* Force out the namespace.  */
20374     context_die = force_decl_die (context);
20375
20376   return context_die;
20377 }
20378
20379 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20380    type) within its namespace, if appropriate.
20381
20382    For compatibility with older debuggers, namespace DIEs only contain
20383    declarations; all definitions are emitted at CU scope.  */
20384
20385 static dw_die_ref
20386 declare_in_namespace (tree thing, dw_die_ref context_die)
20387 {
20388   dw_die_ref ns_context;
20389
20390   if (debug_info_level <= DINFO_LEVEL_TERSE)
20391     return context_die;
20392
20393   /* If this decl is from an inlined function, then don't try to emit it in its
20394      namespace, as we will get confused.  It would have already been emitted
20395      when the abstract instance of the inline function was emitted anyways.  */
20396   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20397     return context_die;
20398
20399   ns_context = setup_namespace_context (thing, context_die);
20400
20401   if (ns_context != context_die)
20402     {
20403       if (is_fortran ())
20404         return ns_context;
20405       if (DECL_P (thing))
20406         gen_decl_die (thing, NULL, ns_context);
20407       else
20408         gen_type_die (thing, ns_context);
20409     }
20410   return context_die;
20411 }
20412
20413 /* Generate a DIE for a namespace or namespace alias.  */
20414
20415 static void
20416 gen_namespace_die (tree decl, dw_die_ref context_die)
20417 {
20418   dw_die_ref namespace_die;
20419
20420   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20421      they are an alias of.  */
20422   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20423     {
20424       /* Output a real namespace or module.  */
20425       context_die = setup_namespace_context (decl, comp_unit_die);
20426       namespace_die = new_die (is_fortran ()
20427                                ? DW_TAG_module : DW_TAG_namespace,
20428                                context_die, decl);
20429       /* For Fortran modules defined in different CU don't add src coords.  */
20430       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20431         {
20432           const char *name = dwarf2_name (decl, 0);
20433           if (name)
20434             add_name_attribute (namespace_die, name);
20435         }
20436       else
20437         add_name_and_src_coords_attributes (namespace_die, decl);
20438       if (DECL_EXTERNAL (decl))
20439         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20440       equate_decl_number_to_die (decl, namespace_die);
20441     }
20442   else
20443     {
20444       /* Output a namespace alias.  */
20445
20446       /* Force out the namespace we are an alias of, if necessary.  */
20447       dw_die_ref origin_die
20448         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20449
20450       if (DECL_CONTEXT (decl) == NULL_TREE
20451           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20452         context_die = setup_namespace_context (decl, comp_unit_die);
20453       /* Now create the namespace alias DIE.  */
20454       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20455       add_name_and_src_coords_attributes (namespace_die, decl);
20456       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20457       equate_decl_number_to_die (decl, namespace_die);
20458     }
20459 }
20460
20461 /* Generate Dwarf debug information for a decl described by DECL.
20462    The return value is currently only meaningful for PARM_DECLs,
20463    for all other decls it returns NULL.  */
20464
20465 static dw_die_ref
20466 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20467 {
20468   tree decl_or_origin = decl ? decl : origin;
20469   tree class_origin = NULL, ultimate_origin;
20470
20471   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20472     return NULL;
20473
20474   switch (TREE_CODE (decl_or_origin))
20475     {
20476     case ERROR_MARK:
20477       break;
20478
20479     case CONST_DECL:
20480       if (!is_fortran ())
20481         {
20482           /* The individual enumerators of an enum type get output when we output
20483              the Dwarf representation of the relevant enum type itself.  */
20484           break;
20485         }
20486
20487       /* Emit its type.  */
20488       gen_type_die (TREE_TYPE (decl), context_die);
20489
20490       /* And its containing namespace.  */
20491       context_die = declare_in_namespace (decl, context_die);
20492
20493       gen_const_die (decl, context_die);
20494       break;
20495
20496     case FUNCTION_DECL:
20497       /* Don't output any DIEs to represent mere function declarations,
20498          unless they are class members or explicit block externs.  */
20499       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20500           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20501           && (current_function_decl == NULL_TREE
20502               || DECL_ARTIFICIAL (decl_or_origin)))
20503         break;
20504
20505 #if 0
20506       /* FIXME */
20507       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20508          on local redeclarations of global functions.  That seems broken.  */
20509       if (current_function_decl != decl)
20510         /* This is only a declaration.  */;
20511 #endif
20512
20513       /* If we're emitting a clone, emit info for the abstract instance.  */
20514       if (origin || DECL_ORIGIN (decl) != decl)
20515         dwarf2out_abstract_function (origin
20516                                      ? DECL_ORIGIN (origin)
20517                                      : DECL_ABSTRACT_ORIGIN (decl));
20518
20519       /* If we're emitting an out-of-line copy of an inline function,
20520          emit info for the abstract instance and set up to refer to it.  */
20521       else if (cgraph_function_possibly_inlined_p (decl)
20522                && ! DECL_ABSTRACT (decl)
20523                && ! class_or_namespace_scope_p (context_die)
20524                /* dwarf2out_abstract_function won't emit a die if this is just
20525                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20526                   that case, because that works only if we have a die.  */
20527                && DECL_INITIAL (decl) != NULL_TREE)
20528         {
20529           dwarf2out_abstract_function (decl);
20530           set_decl_origin_self (decl);
20531         }
20532
20533       /* Otherwise we're emitting the primary DIE for this decl.  */
20534       else if (debug_info_level > DINFO_LEVEL_TERSE)
20535         {
20536           /* Before we describe the FUNCTION_DECL itself, make sure that we
20537              have described its return type.  */
20538           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20539
20540           /* And its virtual context.  */
20541           if (DECL_VINDEX (decl) != NULL_TREE)
20542             gen_type_die (DECL_CONTEXT (decl), context_die);
20543
20544           /* And its containing type.  */
20545           if (!origin)
20546             origin = decl_class_context (decl);
20547           if (origin != NULL_TREE)
20548             gen_type_die_for_member (origin, decl, context_die);
20549
20550           /* And its containing namespace.  */
20551           context_die = declare_in_namespace (decl, context_die);
20552         }
20553
20554       /* Now output a DIE to represent the function itself.  */
20555       if (decl)
20556         gen_subprogram_die (decl, context_die);
20557       break;
20558
20559     case TYPE_DECL:
20560       /* If we are in terse mode, don't generate any DIEs to represent any
20561          actual typedefs.  */
20562       if (debug_info_level <= DINFO_LEVEL_TERSE)
20563         break;
20564
20565       /* In the special case of a TYPE_DECL node representing the declaration
20566          of some type tag, if the given TYPE_DECL is marked as having been
20567          instantiated from some other (original) TYPE_DECL node (e.g. one which
20568          was generated within the original definition of an inline function) we
20569          used to generate a special (abbreviated) DW_TAG_structure_type,
20570          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20571          should be actually referencing those DIEs, as variable DIEs with that
20572          type would be emitted already in the abstract origin, so it was always
20573          removed during unused type prunning.  Don't add anything in this
20574          case.  */
20575       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20576         break;
20577
20578       if (is_redundant_typedef (decl))
20579         gen_type_die (TREE_TYPE (decl), context_die);
20580       else
20581         /* Output a DIE to represent the typedef itself.  */
20582         gen_typedef_die (decl, context_die);
20583       break;
20584
20585     case LABEL_DECL:
20586       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20587         gen_label_die (decl, context_die);
20588       break;
20589
20590     case VAR_DECL:
20591     case RESULT_DECL:
20592       /* If we are in terse mode, don't generate any DIEs to represent any
20593          variable declarations or definitions.  */
20594       if (debug_info_level <= DINFO_LEVEL_TERSE)
20595         break;
20596
20597       /* Output any DIEs that are needed to specify the type of this data
20598          object.  */
20599       if (decl_by_reference_p (decl_or_origin))
20600         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20601       else
20602         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20603
20604       /* And its containing type.  */
20605       class_origin = decl_class_context (decl_or_origin);
20606       if (class_origin != NULL_TREE)
20607         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20608
20609       /* And its containing namespace.  */
20610       context_die = declare_in_namespace (decl_or_origin, context_die);
20611
20612       /* Now output the DIE to represent the data object itself.  This gets
20613          complicated because of the possibility that the VAR_DECL really
20614          represents an inlined instance of a formal parameter for an inline
20615          function.  */
20616       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20617       if (ultimate_origin != NULL_TREE
20618           && TREE_CODE (ultimate_origin) == PARM_DECL)
20619         gen_formal_parameter_die (decl, origin,
20620                                   true /* Emit name attribute.  */,
20621                                   context_die);
20622       else
20623         gen_variable_die (decl, origin, context_die);
20624       break;
20625
20626     case FIELD_DECL:
20627       /* Ignore the nameless fields that are used to skip bits but handle C++
20628          anonymous unions and structs.  */
20629       if (DECL_NAME (decl) != NULL_TREE
20630           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20631           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20632         {
20633           gen_type_die (member_declared_type (decl), context_die);
20634           gen_field_die (decl, context_die);
20635         }
20636       break;
20637
20638     case PARM_DECL:
20639       if (DECL_BY_REFERENCE (decl_or_origin))
20640         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20641       else
20642         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20643       return gen_formal_parameter_die (decl, origin,
20644                                        true /* Emit name attribute.  */,
20645                                        context_die);
20646
20647     case NAMESPACE_DECL:
20648     case IMPORTED_DECL:
20649       if (dwarf_version >= 3 || !dwarf_strict)
20650         gen_namespace_die (decl, context_die);
20651       break;
20652
20653     default:
20654       /* Probably some frontend-internal decl.  Assume we don't care.  */
20655       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20656       break;
20657     }
20658
20659   return NULL;
20660 }
20661 \f
20662 /* Output debug information for global decl DECL.  Called from toplev.c after
20663    compilation proper has finished.  */
20664
20665 static void
20666 dwarf2out_global_decl (tree decl)
20667 {
20668   /* Output DWARF2 information for file-scope tentative data object
20669      declarations, file-scope (extern) function declarations (which
20670      had no corresponding body) and file-scope tagged type declarations
20671      and definitions which have not yet been forced out.  */
20672   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20673     dwarf2out_decl (decl);
20674 }
20675
20676 /* Output debug information for type decl DECL.  Called from toplev.c
20677    and from language front ends (to record built-in types).  */
20678 static void
20679 dwarf2out_type_decl (tree decl, int local)
20680 {
20681   if (!local)
20682     dwarf2out_decl (decl);
20683 }
20684
20685 /* Output debug information for imported module or decl DECL.
20686    NAME is non-NULL name in the lexical block if the decl has been renamed.
20687    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20688    that DECL belongs to.
20689    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20690 static void
20691 dwarf2out_imported_module_or_decl_1 (tree decl,
20692                                      tree name,
20693                                      tree lexical_block,
20694                                      dw_die_ref lexical_block_die)
20695 {
20696   expanded_location xloc;
20697   dw_die_ref imported_die = NULL;
20698   dw_die_ref at_import_die;
20699
20700   if (TREE_CODE (decl) == IMPORTED_DECL)
20701     {
20702       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20703       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20704       gcc_assert (decl);
20705     }
20706   else
20707     xloc = expand_location (input_location);
20708
20709   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20710     {
20711       at_import_die = force_type_die (TREE_TYPE (decl));
20712       /* For namespace N { typedef void T; } using N::T; base_type_die
20713          returns NULL, but DW_TAG_imported_declaration requires
20714          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20715       if (!at_import_die)
20716         {
20717           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20718           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20719           at_import_die = lookup_type_die (TREE_TYPE (decl));
20720           gcc_assert (at_import_die);
20721         }
20722     }
20723   else
20724     {
20725       at_import_die = lookup_decl_die (decl);
20726       if (!at_import_die)
20727         {
20728           /* If we're trying to avoid duplicate debug info, we may not have
20729              emitted the member decl for this field.  Emit it now.  */
20730           if (TREE_CODE (decl) == FIELD_DECL)
20731             {
20732               tree type = DECL_CONTEXT (decl);
20733
20734               if (TYPE_CONTEXT (type)
20735                   && TYPE_P (TYPE_CONTEXT (type))
20736                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20737                                                 DINFO_USAGE_DIR_USE))
20738                 return;
20739               gen_type_die_for_member (type, decl,
20740                                        get_context_die (TYPE_CONTEXT (type)));
20741             }
20742           at_import_die = force_decl_die (decl);
20743         }
20744     }
20745
20746   if (TREE_CODE (decl) == NAMESPACE_DECL)
20747     {
20748       if (dwarf_version >= 3 || !dwarf_strict)
20749         imported_die = new_die (DW_TAG_imported_module,
20750                                 lexical_block_die,
20751                                 lexical_block);
20752       else
20753         return;
20754     }
20755   else
20756     imported_die = new_die (DW_TAG_imported_declaration,
20757                             lexical_block_die,
20758                             lexical_block);
20759
20760   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20761   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20762   if (name)
20763     add_AT_string (imported_die, DW_AT_name,
20764                    IDENTIFIER_POINTER (name));
20765   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20766 }
20767
20768 /* Output debug information for imported module or decl DECL.
20769    NAME is non-NULL name in context if the decl has been renamed.
20770    CHILD is true if decl is one of the renamed decls as part of
20771    importing whole module.  */
20772
20773 static void
20774 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20775                                    bool child)
20776 {
20777   /* dw_die_ref at_import_die;  */
20778   dw_die_ref scope_die;
20779
20780   if (debug_info_level <= DINFO_LEVEL_TERSE)
20781     return;
20782
20783   gcc_assert (decl);
20784
20785   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20786      We need decl DIE for reference and scope die. First, get DIE for the decl
20787      itself.  */
20788
20789   /* Get the scope die for decl context. Use comp_unit_die for global module
20790      or decl. If die is not found for non globals, force new die.  */
20791   if (context
20792       && TYPE_P (context)
20793       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20794     return;
20795
20796   if (!(dwarf_version >= 3 || !dwarf_strict))
20797     return;
20798
20799   scope_die = get_context_die (context);
20800
20801   if (child)
20802     {
20803       gcc_assert (scope_die->die_child);
20804       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20805       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20806       scope_die = scope_die->die_child;
20807     }
20808
20809   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20810   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20811
20812 }
20813
20814 /* Write the debugging output for DECL.  */
20815
20816 void
20817 dwarf2out_decl (tree decl)
20818 {
20819   dw_die_ref context_die = comp_unit_die;
20820
20821   switch (TREE_CODE (decl))
20822     {
20823     case ERROR_MARK:
20824       return;
20825
20826     case FUNCTION_DECL:
20827       /* What we would really like to do here is to filter out all mere
20828          file-scope declarations of file-scope functions which are never
20829          referenced later within this translation unit (and keep all of ones
20830          that *are* referenced later on) but we aren't clairvoyant, so we have
20831          no idea which functions will be referenced in the future (i.e. later
20832          on within the current translation unit). So here we just ignore all
20833          file-scope function declarations which are not also definitions.  If
20834          and when the debugger needs to know something about these functions,
20835          it will have to hunt around and find the DWARF information associated
20836          with the definition of the function.
20837
20838          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20839          nodes represent definitions and which ones represent mere
20840          declarations.  We have to check DECL_INITIAL instead. That's because
20841          the C front-end supports some weird semantics for "extern inline"
20842          function definitions.  These can get inlined within the current
20843          translation unit (and thus, we need to generate Dwarf info for their
20844          abstract instances so that the Dwarf info for the concrete inlined
20845          instances can have something to refer to) but the compiler never
20846          generates any out-of-lines instances of such things (despite the fact
20847          that they *are* definitions).
20848
20849          The important point is that the C front-end marks these "extern
20850          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20851          them anyway. Note that the C++ front-end also plays some similar games
20852          for inline function definitions appearing within include files which
20853          also contain `#pragma interface' pragmas.  */
20854       if (DECL_INITIAL (decl) == NULL_TREE)
20855         return;
20856
20857       /* If we're a nested function, initially use a parent of NULL; if we're
20858          a plain function, this will be fixed up in decls_for_scope.  If
20859          we're a method, it will be ignored, since we already have a DIE.  */
20860       if (decl_function_context (decl)
20861           /* But if we're in terse mode, we don't care about scope.  */
20862           && debug_info_level > DINFO_LEVEL_TERSE)
20863         context_die = NULL;
20864       break;
20865
20866     case VAR_DECL:
20867       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20868          declaration and if the declaration was never even referenced from
20869          within this entire compilation unit.  We suppress these DIEs in
20870          order to save space in the .debug section (by eliminating entries
20871          which are probably useless).  Note that we must not suppress
20872          block-local extern declarations (whether used or not) because that
20873          would screw-up the debugger's name lookup mechanism and cause it to
20874          miss things which really ought to be in scope at a given point.  */
20875       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20876         return;
20877
20878       /* For local statics lookup proper context die.  */
20879       if (TREE_STATIC (decl) && decl_function_context (decl))
20880         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20881
20882       /* If we are in terse mode, don't generate any DIEs to represent any
20883          variable declarations or definitions.  */
20884       if (debug_info_level <= DINFO_LEVEL_TERSE)
20885         return;
20886       break;
20887
20888     case CONST_DECL:
20889       if (debug_info_level <= DINFO_LEVEL_TERSE)
20890         return;
20891       if (!is_fortran ())
20892         return;
20893       if (TREE_STATIC (decl) && decl_function_context (decl))
20894         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20895       break;
20896
20897     case NAMESPACE_DECL:
20898     case IMPORTED_DECL:
20899       if (debug_info_level <= DINFO_LEVEL_TERSE)
20900         return;
20901       if (lookup_decl_die (decl) != NULL)
20902         return;
20903       break;
20904
20905     case TYPE_DECL:
20906       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20907       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20908         return;
20909
20910       /* Don't bother trying to generate any DIEs to represent any of the
20911          normal built-in types for the language we are compiling.  */
20912       if (DECL_IS_BUILTIN (decl))
20913         {
20914           /* OK, we need to generate one for `bool' so GDB knows what type
20915              comparisons have.  */
20916           if (is_cxx ()
20917               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20918               && ! DECL_IGNORED_P (decl))
20919             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20920
20921           return;
20922         }
20923
20924       /* If we are in terse mode, don't generate any DIEs for types.  */
20925       if (debug_info_level <= DINFO_LEVEL_TERSE)
20926         return;
20927
20928       /* If we're a function-scope tag, initially use a parent of NULL;
20929          this will be fixed up in decls_for_scope.  */
20930       if (decl_function_context (decl))
20931         context_die = NULL;
20932
20933       break;
20934
20935     default:
20936       return;
20937     }
20938
20939   gen_decl_die (decl, NULL, context_die);
20940 }
20941
20942 /* Write the debugging output for DECL.  */
20943
20944 static void
20945 dwarf2out_function_decl (tree decl)
20946 {
20947   dwarf2out_decl (decl);
20948
20949   htab_empty (decl_loc_table);
20950 }
20951
20952 /* Output a marker (i.e. a label) for the beginning of the generated code for
20953    a lexical block.  */
20954
20955 static void
20956 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20957                        unsigned int blocknum)
20958 {
20959   switch_to_section (current_function_section ());
20960   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20961 }
20962
20963 /* Output a marker (i.e. a label) for the end of the generated code for a
20964    lexical block.  */
20965
20966 static void
20967 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20968 {
20969   switch_to_section (current_function_section ());
20970   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20971 }
20972
20973 /* Returns nonzero if it is appropriate not to emit any debugging
20974    information for BLOCK, because it doesn't contain any instructions.
20975
20976    Don't allow this for blocks with nested functions or local classes
20977    as we would end up with orphans, and in the presence of scheduling
20978    we may end up calling them anyway.  */
20979
20980 static bool
20981 dwarf2out_ignore_block (const_tree block)
20982 {
20983   tree decl;
20984   unsigned int i;
20985
20986   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20987     if (TREE_CODE (decl) == FUNCTION_DECL
20988         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20989       return 0;
20990   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20991     {
20992       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20993       if (TREE_CODE (decl) == FUNCTION_DECL
20994           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20995       return 0;
20996     }
20997
20998   return 1;
20999 }
21000
21001 /* Hash table routines for file_hash.  */
21002
21003 static int
21004 file_table_eq (const void *p1_p, const void *p2_p)
21005 {
21006   const struct dwarf_file_data *const p1 =
21007     (const struct dwarf_file_data *) p1_p;
21008   const char *const p2 = (const char *) p2_p;
21009   return strcmp (p1->filename, p2) == 0;
21010 }
21011
21012 static hashval_t
21013 file_table_hash (const void *p_p)
21014 {
21015   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21016   return htab_hash_string (p->filename);
21017 }
21018
21019 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21020    dwarf2out.c) and return its "index".  The index of each (known) filename is
21021    just a unique number which is associated with only that one filename.  We
21022    need such numbers for the sake of generating labels (in the .debug_sfnames
21023    section) and references to those files numbers (in the .debug_srcinfo
21024    and.debug_macinfo sections).  If the filename given as an argument is not
21025    found in our current list, add it to the list and assign it the next
21026    available unique index number.  In order to speed up searches, we remember
21027    the index of the filename was looked up last.  This handles the majority of
21028    all searches.  */
21029
21030 static struct dwarf_file_data *
21031 lookup_filename (const char *file_name)
21032 {
21033   void ** slot;
21034   struct dwarf_file_data * created;
21035
21036   /* Check to see if the file name that was searched on the previous
21037      call matches this file name.  If so, return the index.  */
21038   if (file_table_last_lookup
21039       && (file_name == file_table_last_lookup->filename
21040           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21041     return file_table_last_lookup;
21042
21043   /* Didn't match the previous lookup, search the table.  */
21044   slot = htab_find_slot_with_hash (file_table, file_name,
21045                                    htab_hash_string (file_name), INSERT);
21046   if (*slot)
21047     return (struct dwarf_file_data *) *slot;
21048
21049   created = ggc_alloc_dwarf_file_data ();
21050   created->filename = file_name;
21051   created->emitted_number = 0;
21052   *slot = created;
21053   return created;
21054 }
21055
21056 /* If the assembler will construct the file table, then translate the compiler
21057    internal file table number into the assembler file table number, and emit
21058    a .file directive if we haven't already emitted one yet.  The file table
21059    numbers are different because we prune debug info for unused variables and
21060    types, which may include filenames.  */
21061
21062 static int
21063 maybe_emit_file (struct dwarf_file_data * fd)
21064 {
21065   if (! fd->emitted_number)
21066     {
21067       if (last_emitted_file)
21068         fd->emitted_number = last_emitted_file->emitted_number + 1;
21069       else
21070         fd->emitted_number = 1;
21071       last_emitted_file = fd;
21072
21073       if (DWARF2_ASM_LINE_DEBUG_INFO)
21074         {
21075           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21076           output_quoted_string (asm_out_file,
21077                                 remap_debug_filename (fd->filename));
21078           fputc ('\n', asm_out_file);
21079         }
21080     }
21081
21082   return fd->emitted_number;
21083 }
21084
21085 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21086    That generation should happen after function debug info has been
21087    generated. The value of the attribute is the constant value of ARG.  */
21088
21089 static void
21090 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21091 {
21092   die_arg_entry entry;
21093
21094   if (!die || !arg)
21095     return;
21096
21097   if (!tmpl_value_parm_die_table)
21098     tmpl_value_parm_die_table
21099       = VEC_alloc (die_arg_entry, gc, 32);
21100
21101   entry.die = die;
21102   entry.arg = arg;
21103   VEC_safe_push (die_arg_entry, gc,
21104                  tmpl_value_parm_die_table,
21105                  &entry);
21106 }
21107
21108 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21109    by append_entry_to_tmpl_value_parm_die_table. This function must
21110    be called after function DIEs have been generated.  */
21111
21112 static void
21113 gen_remaining_tmpl_value_param_die_attribute (void)
21114 {
21115   if (tmpl_value_parm_die_table)
21116     {
21117       unsigned i;
21118       die_arg_entry *e;
21119
21120       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21121         tree_add_const_value_attribute (e->die, e->arg);
21122     }
21123 }
21124
21125
21126 /* Replace DW_AT_name for the decl with name.  */
21127
21128 static void
21129 dwarf2out_set_name (tree decl, tree name)
21130 {
21131   dw_die_ref die;
21132   dw_attr_ref attr;
21133   const char *dname;
21134
21135   die = TYPE_SYMTAB_DIE (decl);
21136   if (!die)
21137     return;
21138
21139   dname = dwarf2_name (name, 0);
21140   if (!dname)
21141     return;
21142
21143   attr = get_AT (die, DW_AT_name);
21144   if (attr)
21145     {
21146       struct indirect_string_node *node;
21147
21148       node = find_AT_string (dname);
21149       /* replace the string.  */
21150       attr->dw_attr_val.v.val_str = node;
21151     }
21152
21153   else
21154     add_name_attribute (die, dname);
21155 }
21156
21157 /* Called by the final INSN scan whenever we see a direct function call.
21158    Make an entry into the direct call table, recording the point of call
21159    and a reference to the target function's debug entry.  */
21160
21161 static void
21162 dwarf2out_direct_call (tree targ)
21163 {
21164   dcall_entry e;
21165   tree origin = decl_ultimate_origin (targ);
21166
21167   /* If this is a clone, use the abstract origin as the target.  */
21168   if (origin)
21169     targ = origin;
21170
21171   e.poc_label_num = poc_label_num++;
21172   e.poc_decl = current_function_decl;
21173   e.targ_die = force_decl_die (targ);
21174   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21175
21176   /* Drop a label at the return point to mark the point of call.  */
21177   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21178 }
21179
21180 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21181
21182 static hashval_t
21183 vcall_insn_table_hash (const void *x)
21184 {
21185   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21186 }
21187
21188 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21189    insnd_uid of *Y.  */
21190
21191 static int
21192 vcall_insn_table_eq (const void *x, const void *y)
21193 {
21194   return (((const struct vcall_insn *) x)->insn_uid
21195           == ((const struct vcall_insn *) y)->insn_uid);
21196 }
21197
21198 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21199
21200 static void
21201 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21202 {
21203   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21204   struct vcall_insn **slot;
21205
21206   gcc_assert (item);
21207   item->insn_uid = insn_uid;
21208   item->vtable_slot = vtable_slot;
21209   slot = (struct vcall_insn **)
21210       htab_find_slot_with_hash (vcall_insn_table, &item,
21211                                 (hashval_t) insn_uid, INSERT);
21212   *slot = item;
21213 }
21214
21215 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21216
21217 static unsigned int
21218 lookup_vcall_insn (unsigned int insn_uid)
21219 {
21220   struct vcall_insn item;
21221   struct vcall_insn *p;
21222
21223   item.insn_uid = insn_uid;
21224   item.vtable_slot = 0;
21225   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21226                                                  (void *) &item,
21227                                                  (hashval_t) insn_uid);
21228   if (p == NULL)
21229     return (unsigned int) -1;
21230   return p->vtable_slot;
21231 }
21232
21233
21234 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21235    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21236    is the vtable slot index that we will need to put in the virtual call
21237    table later.  */
21238
21239 static void
21240 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21241 {
21242   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21243     {
21244       tree token = OBJ_TYPE_REF_TOKEN (addr);
21245       if (TREE_CODE (token) == INTEGER_CST)
21246         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21247     }
21248 }
21249
21250 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21251    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21252    with NEW_INSN.  */
21253
21254 static void
21255 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21256 {
21257   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21258
21259   if (vtable_slot != (unsigned int) -1)
21260     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21261 }
21262
21263 /* Called by the final INSN scan whenever we see a virtual function call.
21264    Make an entry into the virtual call table, recording the point of call
21265    and the slot index of the vtable entry used to call the virtual member
21266    function.  The slot index was associated with the INSN_UID during the
21267    lowering to RTL.  */
21268
21269 static void
21270 dwarf2out_virtual_call (int insn_uid)
21271 {
21272   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21273   vcall_entry e;
21274
21275   if (vtable_slot == (unsigned int) -1)
21276     return;
21277
21278   e.poc_label_num = poc_label_num++;
21279   e.vtable_slot = vtable_slot;
21280   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21281
21282   /* Drop a label at the return point to mark the point of call.  */
21283   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21284 }
21285
21286 /* Called by the final INSN scan whenever we see a var location.  We
21287    use it to drop labels in the right places, and throw the location in
21288    our lookup table.  */
21289
21290 static void
21291 dwarf2out_var_location (rtx loc_note)
21292 {
21293   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21294   struct var_loc_node *newloc;
21295   rtx next_real;
21296   static const char *last_label;
21297   static const char *last_postcall_label;
21298   static bool last_in_cold_section_p;
21299   tree decl;
21300
21301   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21302     return;
21303
21304   next_real = next_real_insn (loc_note);
21305   /* If there are no instructions which would be affected by this note,
21306      don't do anything.  */
21307   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21308     return;
21309
21310   /* If there were any real insns between note we processed last time
21311      and this note (or if it is the first note), clear
21312      last_{,postcall_}label so that they are not reused this time.  */
21313   if (last_var_location_insn == NULL_RTX
21314       || last_var_location_insn != next_real
21315       || last_in_cold_section_p != in_cold_section_p)
21316     {
21317       last_label = NULL;
21318       last_postcall_label = NULL;
21319     }
21320
21321   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21322   newloc = add_var_loc_to_decl (decl, loc_note,
21323                                 NOTE_DURING_CALL_P (loc_note)
21324                                 ? last_postcall_label : last_label);
21325   if (newloc == NULL)
21326     return;
21327
21328   /* If there were no real insns between note we processed last time
21329      and this note, use the label we emitted last time.  Otherwise
21330      create a new label and emit it.  */
21331   if (last_label == NULL)
21332     {
21333       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21334       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21335       loclabel_num++;
21336       last_label = ggc_strdup (loclabel);
21337     }
21338
21339   if (!NOTE_DURING_CALL_P (loc_note))
21340     newloc->label = last_label;
21341   else
21342     {
21343       if (!last_postcall_label)
21344         {
21345           sprintf (loclabel, "%s-1", last_label);
21346           last_postcall_label = ggc_strdup (loclabel);
21347         }
21348       newloc->label = last_postcall_label;
21349     }
21350
21351   last_var_location_insn = next_real;
21352   last_in_cold_section_p = in_cold_section_p;
21353 }
21354
21355 /* We need to reset the locations at the beginning of each
21356    function. We can't do this in the end_function hook, because the
21357    declarations that use the locations won't have been output when
21358    that hook is called.  Also compute have_multiple_function_sections here.  */
21359
21360 static void
21361 dwarf2out_begin_function (tree fun)
21362 {
21363   if (function_section (fun) != text_section)
21364     have_multiple_function_sections = true;
21365
21366   dwarf2out_note_section_used ();
21367 }
21368
21369 /* Output a label to mark the beginning of a source code line entry
21370    and record information relating to this source line, in
21371    'line_info_table' for later output of the .debug_line section.  */
21372
21373 static void
21374 dwarf2out_source_line (unsigned int line, const char *filename,
21375                        int discriminator, bool is_stmt)
21376 {
21377   static bool last_is_stmt = true;
21378
21379   if (debug_info_level >= DINFO_LEVEL_NORMAL
21380       && line != 0)
21381     {
21382       int file_num = maybe_emit_file (lookup_filename (filename));
21383
21384       switch_to_section (current_function_section ());
21385
21386       /* If requested, emit something human-readable.  */
21387       if (flag_debug_asm)
21388         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21389                  filename, line);
21390
21391       if (DWARF2_ASM_LINE_DEBUG_INFO)
21392         {
21393           /* Emit the .loc directive understood by GNU as.  */
21394           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21395           if (is_stmt != last_is_stmt)
21396             {
21397               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21398               last_is_stmt = is_stmt;
21399             }
21400           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21401             fprintf (asm_out_file, " discriminator %d", discriminator);
21402           fputc ('\n', asm_out_file);
21403
21404           /* Indicate that line number info exists.  */
21405           line_info_table_in_use++;
21406         }
21407       else if (function_section (current_function_decl) != text_section)
21408         {
21409           dw_separate_line_info_ref line_info;
21410           targetm.asm_out.internal_label (asm_out_file,
21411                                           SEPARATE_LINE_CODE_LABEL,
21412                                           separate_line_info_table_in_use);
21413
21414           /* Expand the line info table if necessary.  */
21415           if (separate_line_info_table_in_use
21416               == separate_line_info_table_allocated)
21417             {
21418               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21419               separate_line_info_table
21420                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21421                                  separate_line_info_table,
21422                                  separate_line_info_table_allocated);
21423               memset (separate_line_info_table
21424                        + separate_line_info_table_in_use,
21425                       0,
21426                       (LINE_INFO_TABLE_INCREMENT
21427                        * sizeof (dw_separate_line_info_entry)));
21428             }
21429
21430           /* Add the new entry at the end of the line_info_table.  */
21431           line_info
21432             = &separate_line_info_table[separate_line_info_table_in_use++];
21433           line_info->dw_file_num = file_num;
21434           line_info->dw_line_num = line;
21435           line_info->function = current_function_funcdef_no;
21436         }
21437       else
21438         {
21439           dw_line_info_ref line_info;
21440
21441           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21442                                      line_info_table_in_use);
21443
21444           /* Expand the line info table if necessary.  */
21445           if (line_info_table_in_use == line_info_table_allocated)
21446             {
21447               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21448               line_info_table
21449                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21450                                  line_info_table_allocated);
21451               memset (line_info_table + line_info_table_in_use, 0,
21452                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21453             }
21454
21455           /* Add the new entry at the end of the line_info_table.  */
21456           line_info = &line_info_table[line_info_table_in_use++];
21457           line_info->dw_file_num = file_num;
21458           line_info->dw_line_num = line;
21459         }
21460     }
21461 }
21462
21463 /* Record the beginning of a new source file.  */
21464
21465 static void
21466 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21467 {
21468   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21469     {
21470       /* Record the beginning of the file for break_out_includes.  */
21471       dw_die_ref bincl_die;
21472
21473       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21474       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21475     }
21476
21477   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21478     {
21479       int file_num = maybe_emit_file (lookup_filename (filename));
21480
21481       switch_to_section (debug_macinfo_section);
21482       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21483       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21484                                    lineno);
21485
21486       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21487     }
21488 }
21489
21490 /* Record the end of a source file.  */
21491
21492 static void
21493 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21494 {
21495   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21496     /* Record the end of the file for break_out_includes.  */
21497     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21498
21499   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21500     {
21501       switch_to_section (debug_macinfo_section);
21502       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21503     }
21504 }
21505
21506 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21507    the tail part of the directive line, i.e. the part which is past the
21508    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21509
21510 static void
21511 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21512                   const char *buffer ATTRIBUTE_UNUSED)
21513 {
21514   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21515     {
21516       switch_to_section (debug_macinfo_section);
21517       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21518       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21519       dw2_asm_output_nstring (buffer, -1, "The macro");
21520     }
21521 }
21522
21523 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21524    the tail part of the directive line, i.e. the part which is past the
21525    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21526
21527 static void
21528 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21529                  const char *buffer ATTRIBUTE_UNUSED)
21530 {
21531   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21532     {
21533       switch_to_section (debug_macinfo_section);
21534       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21535       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21536       dw2_asm_output_nstring (buffer, -1, "The macro");
21537     }
21538 }
21539
21540 /* Set up for Dwarf output at the start of compilation.  */
21541
21542 static void
21543 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21544 {
21545   /* Allocate the file_table.  */
21546   file_table = htab_create_ggc (50, file_table_hash,
21547                                 file_table_eq, NULL);
21548
21549   /* Allocate the decl_die_table.  */
21550   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21551                                     decl_die_table_eq, NULL);
21552
21553   /* Allocate the decl_loc_table.  */
21554   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21555                                     decl_loc_table_eq, NULL);
21556
21557   /* Allocate the initial hunk of the decl_scope_table.  */
21558   decl_scope_table = VEC_alloc (tree, gc, 256);
21559
21560   /* Allocate the initial hunk of the abbrev_die_table.  */
21561   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21562     (ABBREV_DIE_TABLE_INCREMENT);
21563   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21564   /* Zero-th entry is allocated, but unused.  */
21565   abbrev_die_table_in_use = 1;
21566
21567   /* Allocate the initial hunk of the line_info_table.  */
21568   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21569     (LINE_INFO_TABLE_INCREMENT);
21570   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21571
21572   /* Zero-th entry is allocated, but unused.  */
21573   line_info_table_in_use = 1;
21574
21575   /* Allocate the pubtypes and pubnames vectors.  */
21576   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21577   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21578
21579   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21580   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21581                                       vcall_insn_table_eq, NULL);
21582
21583   /* Generate the initial DIE for the .debug section.  Note that the (string)
21584      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21585      will (typically) be a relative pathname and that this pathname should be
21586      taken as being relative to the directory from which the compiler was
21587      invoked when the given (base) source file was compiled.  We will fill
21588      in this value in dwarf2out_finish.  */
21589   comp_unit_die = gen_compile_unit_die (NULL);
21590
21591   incomplete_types = VEC_alloc (tree, gc, 64);
21592
21593   used_rtx_array = VEC_alloc (rtx, gc, 32);
21594
21595   debug_info_section = get_section (DEBUG_INFO_SECTION,
21596                                     SECTION_DEBUG, NULL);
21597   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21598                                       SECTION_DEBUG, NULL);
21599   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21600                                        SECTION_DEBUG, NULL);
21601   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21602                                        SECTION_DEBUG, NULL);
21603   debug_line_section = get_section (DEBUG_LINE_SECTION,
21604                                     SECTION_DEBUG, NULL);
21605   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21606                                    SECTION_DEBUG, NULL);
21607   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21608                                         SECTION_DEBUG, NULL);
21609   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21610                                         SECTION_DEBUG, NULL);
21611   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21612                                      SECTION_DEBUG, NULL);
21613   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21614                                      SECTION_DEBUG, NULL);
21615   debug_str_section = get_section (DEBUG_STR_SECTION,
21616                                    DEBUG_STR_SECTION_FLAGS, NULL);
21617   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21618                                       SECTION_DEBUG, NULL);
21619   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21620                                      SECTION_DEBUG, NULL);
21621
21622   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21623   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21624                                DEBUG_ABBREV_SECTION_LABEL, 0);
21625   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21626   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21627                                COLD_TEXT_SECTION_LABEL, 0);
21628   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21629
21630   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21631                                DEBUG_INFO_SECTION_LABEL, 0);
21632   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21633                                DEBUG_LINE_SECTION_LABEL, 0);
21634   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21635                                DEBUG_RANGES_SECTION_LABEL, 0);
21636   switch_to_section (debug_abbrev_section);
21637   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21638   switch_to_section (debug_info_section);
21639   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21640   switch_to_section (debug_line_section);
21641   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21642
21643   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21644     {
21645       switch_to_section (debug_macinfo_section);
21646       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21647                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21648       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21649     }
21650
21651   switch_to_section (text_section);
21652   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21653   if (flag_reorder_blocks_and_partition)
21654     {
21655       cold_text_section = unlikely_text_section ();
21656       switch_to_section (cold_text_section);
21657       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21658     }
21659
21660 }
21661
21662 /* Called before cgraph_optimize starts outputtting functions, variables
21663    and toplevel asms into assembly.  */
21664
21665 static void
21666 dwarf2out_assembly_start (void)
21667 {
21668   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21669     {
21670 #ifndef TARGET_UNWIND_INFO
21671       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21672 #endif
21673         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21674     }
21675 }
21676
21677 /* A helper function for dwarf2out_finish called through
21678    htab_traverse.  Emit one queued .debug_str string.  */
21679
21680 static int
21681 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21682 {
21683   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21684
21685   if (node->label && node->refcount)
21686     {
21687       switch_to_section (debug_str_section);
21688       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21689       assemble_string (node->str, strlen (node->str) + 1);
21690     }
21691
21692   return 1;
21693 }
21694
21695 #if ENABLE_ASSERT_CHECKING
21696 /* Verify that all marks are clear.  */
21697
21698 static void
21699 verify_marks_clear (dw_die_ref die)
21700 {
21701   dw_die_ref c;
21702
21703   gcc_assert (! die->die_mark);
21704   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21705 }
21706 #endif /* ENABLE_ASSERT_CHECKING */
21707
21708 /* Clear the marks for a die and its children.
21709    Be cool if the mark isn't set.  */
21710
21711 static void
21712 prune_unmark_dies (dw_die_ref die)
21713 {
21714   dw_die_ref c;
21715
21716   if (die->die_mark)
21717     die->die_mark = 0;
21718   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21719 }
21720
21721 /* Given DIE that we're marking as used, find any other dies
21722    it references as attributes and mark them as used.  */
21723
21724 static void
21725 prune_unused_types_walk_attribs (dw_die_ref die)
21726 {
21727   dw_attr_ref a;
21728   unsigned ix;
21729
21730   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21731     {
21732       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21733         {
21734           /* A reference to another DIE.
21735              Make sure that it will get emitted.
21736              If it was broken out into a comdat group, don't follow it.  */
21737           if (dwarf_version < 4
21738               || a->dw_attr == DW_AT_specification
21739               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21740             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21741         }
21742       /* Set the string's refcount to 0 so that prune_unused_types_mark
21743          accounts properly for it.  */
21744       if (AT_class (a) == dw_val_class_str)
21745         a->dw_attr_val.v.val_str->refcount = 0;
21746     }
21747 }
21748
21749
21750 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21751    to DIE's children.  */
21752
21753 static void
21754 prune_unused_types_mark (dw_die_ref die, int dokids)
21755 {
21756   dw_die_ref c;
21757
21758   if (die->die_mark == 0)
21759     {
21760       /* We haven't done this node yet.  Mark it as used.  */
21761       die->die_mark = 1;
21762
21763       /* We also have to mark its parents as used.
21764          (But we don't want to mark our parents' kids due to this.)  */
21765       if (die->die_parent)
21766         prune_unused_types_mark (die->die_parent, 0);
21767
21768       /* Mark any referenced nodes.  */
21769       prune_unused_types_walk_attribs (die);
21770
21771       /* If this node is a specification,
21772          also mark the definition, if it exists.  */
21773       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21774         prune_unused_types_mark (die->die_definition, 1);
21775     }
21776
21777   if (dokids && die->die_mark != 2)
21778     {
21779       /* We need to walk the children, but haven't done so yet.
21780          Remember that we've walked the kids.  */
21781       die->die_mark = 2;
21782
21783       /* If this is an array type, we need to make sure our
21784          kids get marked, even if they're types.  If we're
21785          breaking out types into comdat sections, do this
21786          for all type definitions.  */
21787       if (die->die_tag == DW_TAG_array_type
21788           || (dwarf_version >= 4
21789               && is_type_die (die) && ! is_declaration_die (die)))
21790         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21791       else
21792         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21793     }
21794 }
21795
21796 /* For local classes, look if any static member functions were emitted
21797    and if so, mark them.  */
21798
21799 static void
21800 prune_unused_types_walk_local_classes (dw_die_ref die)
21801 {
21802   dw_die_ref c;
21803
21804   if (die->die_mark == 2)
21805     return;
21806
21807   switch (die->die_tag)
21808     {
21809     case DW_TAG_structure_type:
21810     case DW_TAG_union_type:
21811     case DW_TAG_class_type:
21812       break;
21813
21814     case DW_TAG_subprogram:
21815       if (!get_AT_flag (die, DW_AT_declaration)
21816           || die->die_definition != NULL)
21817         prune_unused_types_mark (die, 1);
21818       return;
21819
21820     default:
21821       return;
21822     }
21823
21824   /* Mark children.  */
21825   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21826 }
21827
21828 /* Walk the tree DIE and mark types that we actually use.  */
21829
21830 static void
21831 prune_unused_types_walk (dw_die_ref die)
21832 {
21833   dw_die_ref c;
21834
21835   /* Don't do anything if this node is already marked and
21836      children have been marked as well.  */
21837   if (die->die_mark == 2)
21838     return;
21839
21840   switch (die->die_tag)
21841     {
21842     case DW_TAG_structure_type:
21843     case DW_TAG_union_type:
21844     case DW_TAG_class_type:
21845       if (die->die_perennial_p)
21846         break;
21847
21848       for (c = die->die_parent; c; c = c->die_parent)
21849         if (c->die_tag == DW_TAG_subprogram)
21850           break;
21851
21852       /* Finding used static member functions inside of classes
21853          is needed just for local classes, because for other classes
21854          static member function DIEs with DW_AT_specification
21855          are emitted outside of the DW_TAG_*_type.  If we ever change
21856          it, we'd need to call this even for non-local classes.  */
21857       if (c)
21858         prune_unused_types_walk_local_classes (die);
21859
21860       /* It's a type node --- don't mark it.  */
21861       return;
21862
21863     case DW_TAG_const_type:
21864     case DW_TAG_packed_type:
21865     case DW_TAG_pointer_type:
21866     case DW_TAG_reference_type:
21867     case DW_TAG_rvalue_reference_type:
21868     case DW_TAG_volatile_type:
21869     case DW_TAG_typedef:
21870     case DW_TAG_array_type:
21871     case DW_TAG_interface_type:
21872     case DW_TAG_friend:
21873     case DW_TAG_variant_part:
21874     case DW_TAG_enumeration_type:
21875     case DW_TAG_subroutine_type:
21876     case DW_TAG_string_type:
21877     case DW_TAG_set_type:
21878     case DW_TAG_subrange_type:
21879     case DW_TAG_ptr_to_member_type:
21880     case DW_TAG_file_type:
21881       if (die->die_perennial_p)
21882         break;
21883
21884       /* It's a type node --- don't mark it.  */
21885       return;
21886
21887     default:
21888       /* Mark everything else.  */
21889       break;
21890   }
21891
21892   if (die->die_mark == 0)
21893     {
21894       die->die_mark = 1;
21895
21896       /* Now, mark any dies referenced from here.  */
21897       prune_unused_types_walk_attribs (die);
21898     }
21899
21900   die->die_mark = 2;
21901
21902   /* Mark children.  */
21903   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21904 }
21905
21906 /* Increment the string counts on strings referred to from DIE's
21907    attributes.  */
21908
21909 static void
21910 prune_unused_types_update_strings (dw_die_ref die)
21911 {
21912   dw_attr_ref a;
21913   unsigned ix;
21914
21915   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21916     if (AT_class (a) == dw_val_class_str)
21917       {
21918         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21919         s->refcount++;
21920         /* Avoid unnecessarily putting strings that are used less than
21921            twice in the hash table.  */
21922         if (s->refcount
21923             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21924           {
21925             void ** slot;
21926             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21927                                              htab_hash_string (s->str),
21928                                              INSERT);
21929             gcc_assert (*slot == NULL);
21930             *slot = s;
21931           }
21932       }
21933 }
21934
21935 /* Remove from the tree DIE any dies that aren't marked.  */
21936
21937 static void
21938 prune_unused_types_prune (dw_die_ref die)
21939 {
21940   dw_die_ref c;
21941
21942   gcc_assert (die->die_mark);
21943   prune_unused_types_update_strings (die);
21944
21945   if (! die->die_child)
21946     return;
21947
21948   c = die->die_child;
21949   do {
21950     dw_die_ref prev = c;
21951     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21952       if (c == die->die_child)
21953         {
21954           /* No marked children between 'prev' and the end of the list.  */
21955           if (prev == c)
21956             /* No marked children at all.  */
21957             die->die_child = NULL;
21958           else
21959             {
21960               prev->die_sib = c->die_sib;
21961               die->die_child = prev;
21962             }
21963           return;
21964         }
21965
21966     if (c != prev->die_sib)
21967       prev->die_sib = c;
21968     prune_unused_types_prune (c);
21969   } while (c != die->die_child);
21970 }
21971
21972 /* A helper function for dwarf2out_finish called through
21973    htab_traverse.  Clear .debug_str strings that we haven't already
21974    decided to emit.  */
21975
21976 static int
21977 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21978 {
21979   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21980
21981   if (!node->label || !node->refcount)
21982     htab_clear_slot (debug_str_hash, h);
21983
21984   return 1;
21985 }
21986
21987 /* Remove dies representing declarations that we never use.  */
21988
21989 static void
21990 prune_unused_types (void)
21991 {
21992   unsigned int i;
21993   limbo_die_node *node;
21994   comdat_type_node *ctnode;
21995   pubname_ref pub;
21996   dcall_entry *dcall;
21997
21998 #if ENABLE_ASSERT_CHECKING
21999   /* All the marks should already be clear.  */
22000   verify_marks_clear (comp_unit_die);
22001   for (node = limbo_die_list; node; node = node->next)
22002     verify_marks_clear (node->die);
22003   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22004     verify_marks_clear (ctnode->root_die);
22005 #endif /* ENABLE_ASSERT_CHECKING */
22006
22007   /* Mark types that are used in global variables.  */
22008   premark_types_used_by_global_vars ();
22009
22010   /* Set the mark on nodes that are actually used.  */
22011   prune_unused_types_walk (comp_unit_die);
22012   for (node = limbo_die_list; node; node = node->next)
22013     prune_unused_types_walk (node->die);
22014   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22015     {
22016       prune_unused_types_walk (ctnode->root_die);
22017       prune_unused_types_mark (ctnode->type_die, 1);
22018     }
22019
22020   /* Also set the mark on nodes referenced from the
22021      pubname_table or arange_table.  */
22022   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22023     prune_unused_types_mark (pub->die, 1);
22024   for (i = 0; i < arange_table_in_use; i++)
22025     prune_unused_types_mark (arange_table[i], 1);
22026
22027   /* Mark nodes referenced from the direct call table.  */
22028   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22029     prune_unused_types_mark (dcall->targ_die, 1);
22030
22031   /* Get rid of nodes that aren't marked; and update the string counts.  */
22032   if (debug_str_hash && debug_str_hash_forced)
22033     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22034   else if (debug_str_hash)
22035     htab_empty (debug_str_hash);
22036   prune_unused_types_prune (comp_unit_die);
22037   for (node = limbo_die_list; node; node = node->next)
22038     prune_unused_types_prune (node->die);
22039   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22040     prune_unused_types_prune (ctnode->root_die);
22041
22042   /* Leave the marks clear.  */
22043   prune_unmark_dies (comp_unit_die);
22044   for (node = limbo_die_list; node; node = node->next)
22045     prune_unmark_dies (node->die);
22046   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22047     prune_unmark_dies (ctnode->root_die);
22048 }
22049
22050 /* Set the parameter to true if there are any relative pathnames in
22051    the file table.  */
22052 static int
22053 file_table_relative_p (void ** slot, void *param)
22054 {
22055   bool *p = (bool *) param;
22056   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22057   if (!IS_ABSOLUTE_PATH (d->filename))
22058     {
22059       *p = true;
22060       return 0;
22061     }
22062   return 1;
22063 }
22064
22065 /* Routines to manipulate hash table of comdat type units.  */
22066
22067 static hashval_t
22068 htab_ct_hash (const void *of)
22069 {
22070   hashval_t h;
22071   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22072
22073   memcpy (&h, type_node->signature, sizeof (h));
22074   return h;
22075 }
22076
22077 static int
22078 htab_ct_eq (const void *of1, const void *of2)
22079 {
22080   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22081   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22082
22083   return (! memcmp (type_node_1->signature, type_node_2->signature,
22084                     DWARF_TYPE_SIGNATURE_SIZE));
22085 }
22086
22087 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22088    to the location it would have been added, should we know its
22089    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22090    probably improve compactness of debug info, removing equivalent
22091    abbrevs, and hide any differences caused by deferring the
22092    computation of the assembler name, triggered by e.g. PCH.  */
22093
22094 static inline void
22095 move_linkage_attr (dw_die_ref die)
22096 {
22097   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22098   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22099
22100   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22101               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22102
22103   while (--ix > 0)
22104     {
22105       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22106
22107       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22108         break;
22109     }
22110
22111   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22112     {
22113       VEC_pop (dw_attr_node, die->die_attr);
22114       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22115     }
22116 }
22117
22118 /* Helper function for resolve_addr, attempt to resolve
22119    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22120    SYMBOL_REFs refer to variables emitted in the current CU.  */
22121
22122 static int
22123 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22124 {
22125   rtx rtl = *addr;
22126
22127   if (GET_CODE (rtl) == CONST_STRING)
22128     {
22129       size_t len = strlen (XSTR (rtl, 0)) + 1;
22130       tree t = build_string (len, XSTR (rtl, 0));
22131       tree tlen = build_int_cst (NULL_TREE, len - 1);
22132       TREE_TYPE (t)
22133         = build_array_type (char_type_node, build_index_type (tlen));
22134       rtl = lookup_constant_def (t);
22135       if (!rtl || !MEM_P (rtl))
22136         return 1;
22137       rtl = XEXP (rtl, 0);
22138       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22139       *addr = rtl;
22140       return 0;
22141     }
22142
22143   if (GET_CODE (rtl) == SYMBOL_REF
22144       && SYMBOL_REF_DECL (rtl)
22145       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22146     return 1;
22147
22148   if (GET_CODE (rtl) == CONST
22149       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22150     return 1;
22151
22152   return 0;
22153 }
22154
22155 /* Helper function for resolve_addr, handle one location
22156    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22157    the location list couldn't be resolved.  */
22158
22159 static bool
22160 resolve_addr_in_expr (dw_loc_descr_ref loc)
22161 {
22162   for (; loc; loc = loc->dw_loc_next)
22163     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22164          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22165         || (loc->dw_loc_opc == DW_OP_implicit_value
22166             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22167             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22168       return false;
22169   return true;
22170 }
22171
22172 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22173    an address in .rodata section if the string literal is emitted there,
22174    or remove the containing location list or replace DW_AT_const_value
22175    with DW_AT_location and empty location expression, if it isn't found
22176    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22177    to something that has been emitted in the current CU.  */
22178
22179 static void
22180 resolve_addr (dw_die_ref die)
22181 {
22182   dw_die_ref c;
22183   dw_attr_ref a;
22184   dw_loc_list_ref *curr;
22185   unsigned ix;
22186
22187   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22188     switch (AT_class (a))
22189       {
22190       case dw_val_class_loc_list:
22191         curr = AT_loc_list_ptr (a);
22192         while (*curr)
22193           {
22194             if (!resolve_addr_in_expr ((*curr)->expr))
22195               {
22196                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22197                 if (next && (*curr)->ll_symbol)
22198                   {
22199                     gcc_assert (!next->ll_symbol);
22200                     next->ll_symbol = (*curr)->ll_symbol;
22201                   }
22202                 *curr = next;
22203               }
22204             else
22205               curr = &(*curr)->dw_loc_next;
22206           }
22207         if (!AT_loc_list (a))
22208           {
22209             remove_AT (die, a->dw_attr);
22210             ix--;
22211           }
22212         break;
22213       case dw_val_class_loc:
22214         if (!resolve_addr_in_expr (AT_loc (a)))
22215           {
22216             remove_AT (die, a->dw_attr);
22217             ix--;
22218           }
22219         break;
22220       case dw_val_class_addr:
22221         if (a->dw_attr == DW_AT_const_value
22222             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22223           {
22224             remove_AT (die, a->dw_attr);
22225             ix--;
22226           }
22227         break;
22228       default:
22229         break;
22230       }
22231
22232   FOR_EACH_CHILD (die, c, resolve_addr (c));
22233 }
22234
22235 /* Output stuff that dwarf requires at the end of every file,
22236    and generate the DWARF-2 debugging info.  */
22237
22238 static void
22239 dwarf2out_finish (const char *filename)
22240 {
22241   limbo_die_node *node, *next_node;
22242   comdat_type_node *ctnode;
22243   htab_t comdat_type_table;
22244   dw_die_ref die = 0;
22245   unsigned int i;
22246
22247   gen_remaining_tmpl_value_param_die_attribute ();
22248
22249   /* Add the name for the main input file now.  We delayed this from
22250      dwarf2out_init to avoid complications with PCH.  */
22251   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22252   if (!IS_ABSOLUTE_PATH (filename))
22253     add_comp_dir_attribute (comp_unit_die);
22254   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22255     {
22256       bool p = false;
22257       htab_traverse (file_table, file_table_relative_p, &p);
22258       if (p)
22259         add_comp_dir_attribute (comp_unit_die);
22260     }
22261
22262   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22263     {
22264       add_location_or_const_value_attribute (
22265         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22266         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22267         DW_AT_location);
22268     }
22269
22270   /* Traverse the limbo die list, and add parent/child links.  The only
22271      dies without parents that should be here are concrete instances of
22272      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22273      For concrete instances, we can get the parent die from the abstract
22274      instance.  */
22275   for (node = limbo_die_list; node; node = next_node)
22276     {
22277       next_node = node->next;
22278       die = node->die;
22279
22280       if (die->die_parent == NULL)
22281         {
22282           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22283
22284           if (origin)
22285             add_child_die (origin->die_parent, die);
22286           else if (die == comp_unit_die)
22287             ;
22288           else if (seen_error ())
22289             /* It's OK to be confused by errors in the input.  */
22290             add_child_die (comp_unit_die, die);
22291           else
22292             {
22293               /* In certain situations, the lexical block containing a
22294                  nested function can be optimized away, which results
22295                  in the nested function die being orphaned.  Likewise
22296                  with the return type of that nested function.  Force
22297                  this to be a child of the containing function.
22298
22299                  It may happen that even the containing function got fully
22300                  inlined and optimized out.  In that case we are lost and
22301                  assign the empty child.  This should not be big issue as
22302                  the function is likely unreachable too.  */
22303               tree context = NULL_TREE;
22304
22305               gcc_assert (node->created_for);
22306
22307               if (DECL_P (node->created_for))
22308                 context = DECL_CONTEXT (node->created_for);
22309               else if (TYPE_P (node->created_for))
22310                 context = TYPE_CONTEXT (node->created_for);
22311
22312               gcc_assert (context
22313                           && (TREE_CODE (context) == FUNCTION_DECL
22314                               || TREE_CODE (context) == NAMESPACE_DECL));
22315
22316               origin = lookup_decl_die (context);
22317               if (origin)
22318                 add_child_die (origin, die);
22319               else
22320                 add_child_die (comp_unit_die, die);
22321             }
22322         }
22323     }
22324
22325   limbo_die_list = NULL;
22326
22327   resolve_addr (comp_unit_die);
22328
22329   for (node = deferred_asm_name; node; node = node->next)
22330     {
22331       tree decl = node->created_for;
22332       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22333         {
22334           add_linkage_attr (node->die, decl);
22335           move_linkage_attr (node->die);
22336         }
22337     }
22338
22339   deferred_asm_name = NULL;
22340
22341   /* Walk through the list of incomplete types again, trying once more to
22342      emit full debugging info for them.  */
22343   retry_incomplete_types ();
22344
22345   if (flag_eliminate_unused_debug_types)
22346     prune_unused_types ();
22347
22348   /* Generate separate CUs for each of the include files we've seen.
22349      They will go into limbo_die_list.  */
22350   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22351     break_out_includes (comp_unit_die);
22352
22353   /* Generate separate COMDAT sections for type DIEs. */
22354   if (dwarf_version >= 4)
22355     {
22356       break_out_comdat_types (comp_unit_die);
22357
22358       /* Each new type_unit DIE was added to the limbo die list when created.
22359          Since these have all been added to comdat_type_list, clear the
22360          limbo die list.  */
22361       limbo_die_list = NULL;
22362
22363       /* For each new comdat type unit, copy declarations for incomplete
22364          types to make the new unit self-contained (i.e., no direct
22365          references to the main compile unit).  */
22366       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22367         copy_decls_for_unworthy_types (ctnode->root_die);
22368       copy_decls_for_unworthy_types (comp_unit_die);
22369
22370       /* In the process of copying declarations from one unit to another,
22371          we may have left some declarations behind that are no longer
22372          referenced.  Prune them.  */
22373       prune_unused_types ();
22374     }
22375
22376   /* Traverse the DIE's and add add sibling attributes to those DIE's
22377      that have children.  */
22378   add_sibling_attributes (comp_unit_die);
22379   for (node = limbo_die_list; node; node = node->next)
22380     add_sibling_attributes (node->die);
22381   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22382     add_sibling_attributes (ctnode->root_die);
22383
22384   /* Output a terminator label for the .text section.  */
22385   switch_to_section (text_section);
22386   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22387   if (flag_reorder_blocks_and_partition)
22388     {
22389       switch_to_section (unlikely_text_section ());
22390       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22391     }
22392
22393   /* We can only use the low/high_pc attributes if all of the code was
22394      in .text.  */
22395   if (!have_multiple_function_sections
22396       || !(dwarf_version >= 3 || !dwarf_strict))
22397     {
22398       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22399       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22400     }
22401
22402   else
22403     {
22404       unsigned fde_idx = 0;
22405       bool range_list_added = false;
22406
22407       /* We need to give .debug_loc and .debug_ranges an appropriate
22408          "base address".  Use zero so that these addresses become
22409          absolute.  Historically, we've emitted the unexpected
22410          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22411          Emit both to give time for other tools to adapt.  */
22412       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22413       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22414
22415       if (text_section_used)
22416         add_ranges_by_labels (comp_unit_die, text_section_label,
22417                               text_end_label, &range_list_added);
22418       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22419         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22420                               cold_end_label, &range_list_added);
22421
22422       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22423         {
22424           dw_fde_ref fde = &fde_table[fde_idx];
22425
22426           if (fde->dw_fde_switched_sections)
22427             {
22428               if (!fde->in_std_section)
22429                 add_ranges_by_labels (comp_unit_die,
22430                                       fde->dw_fde_hot_section_label,
22431                                       fde->dw_fde_hot_section_end_label,
22432                                       &range_list_added);
22433               if (!fde->cold_in_std_section)
22434                 add_ranges_by_labels (comp_unit_die,
22435                                       fde->dw_fde_unlikely_section_label,
22436                                       fde->dw_fde_unlikely_section_end_label,
22437                                       &range_list_added);
22438             }
22439           else if (!fde->in_std_section)
22440             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22441                                   fde->dw_fde_end, &range_list_added);
22442         }
22443
22444       if (range_list_added)
22445         add_ranges (NULL);
22446     }
22447
22448   /* Output location list section if necessary.  */
22449   if (have_location_lists)
22450     {
22451       /* Output the location lists info.  */
22452       switch_to_section (debug_loc_section);
22453       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22454                                    DEBUG_LOC_SECTION_LABEL, 0);
22455       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22456       output_location_lists (die);
22457     }
22458
22459   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22460     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22461                     debug_line_section_label);
22462
22463   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22464     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22465
22466   /* Output all of the compilation units.  We put the main one last so that
22467      the offsets are available to output_pubnames.  */
22468   for (node = limbo_die_list; node; node = node->next)
22469     output_comp_unit (node->die, 0);
22470
22471   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22472   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22473     {
22474       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22475
22476       /* Don't output duplicate types.  */
22477       if (*slot != HTAB_EMPTY_ENTRY)
22478         continue;
22479
22480       /* Add a pointer to the line table for the main compilation unit
22481          so that the debugger can make sense of DW_AT_decl_file
22482          attributes.  */
22483       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22484         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22485                         debug_line_section_label);
22486
22487       output_comdat_type_unit (ctnode);
22488       *slot = ctnode;
22489     }
22490   htab_delete (comdat_type_table);
22491
22492   /* Output the main compilation unit if non-empty or if .debug_macinfo
22493      has been emitted.  */
22494   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22495
22496   /* Output the abbreviation table.  */
22497   switch_to_section (debug_abbrev_section);
22498   output_abbrev_section ();
22499
22500   /* Output public names table if necessary.  */
22501   if (!VEC_empty (pubname_entry, pubname_table))
22502     {
22503       switch_to_section (debug_pubnames_section);
22504       output_pubnames (pubname_table);
22505     }
22506
22507   /* Output public types table if necessary.  */
22508   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22509      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22510      simply won't look for the section.  */
22511   if (!VEC_empty (pubname_entry, pubtype_table))
22512     {
22513       switch_to_section (debug_pubtypes_section);
22514       output_pubnames (pubtype_table);
22515     }
22516
22517   /* Output direct and virtual call tables if necessary.  */
22518   if (!VEC_empty (dcall_entry, dcall_table))
22519     {
22520       switch_to_section (debug_dcall_section);
22521       output_dcall_table ();
22522     }
22523   if (!VEC_empty (vcall_entry, vcall_table))
22524     {
22525       switch_to_section (debug_vcall_section);
22526       output_vcall_table ();
22527     }
22528
22529   /* Output the address range information.  We only put functions in the arange
22530      table, so don't write it out if we don't have any.  */
22531   if (fde_table_in_use)
22532     {
22533       switch_to_section (debug_aranges_section);
22534       output_aranges ();
22535     }
22536
22537   /* Output ranges section if necessary.  */
22538   if (ranges_table_in_use)
22539     {
22540       switch_to_section (debug_ranges_section);
22541       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22542       output_ranges ();
22543     }
22544
22545   /* Output the source line correspondence table.  We must do this
22546      even if there is no line information.  Otherwise, on an empty
22547      translation unit, we will generate a present, but empty,
22548      .debug_info section.  IRIX 6.5 `nm' will then complain when
22549      examining the file.  This is done late so that any filenames
22550      used by the debug_info section are marked as 'used'.  */
22551   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22552     {
22553       switch_to_section (debug_line_section);
22554       output_line_info ();
22555     }
22556
22557   /* Have to end the macro section.  */
22558   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22559     {
22560       switch_to_section (debug_macinfo_section);
22561       dw2_asm_output_data (1, 0, "End compilation unit");
22562     }
22563
22564   /* If we emitted any DW_FORM_strp form attribute, output the string
22565      table too.  */
22566   if (debug_str_hash)
22567     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22568 }
22569
22570 #include "gt-dwarf2out.h"