OSDN Git Service

* dwarf2.h (enum dwarf_type): Add DW_ATE_UTF.
[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 #ifdef DWARF2_DEBUGGING_INFO
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98
99 static rtx last_var_location_insn;
100 #endif
101
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104
105 /* Define this macro to be a nonzero value if the directory specifications
106     which are output in the debug info should end with a separator.  */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109    from generating indirect strings in DWARF2 debug information, for instance
110    if your target is stuck with an old version of GDB that is unable to
111    process them properly or uses VMS Debug.  */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
117
118 #ifndef DWARF2_FRAME_INFO
119 # ifdef DWARF2_DEBUGGING_INFO
120 #  define DWARF2_FRAME_INFO \
121   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
122 # else
123 #  define DWARF2_FRAME_INFO 0
124 # endif
125 #endif
126
127 /* Map register numbers held in the call frame info that gcc has
128    collected using DWARF_FRAME_REGNUM to those that should be output in
129    .debug_frame and .eh_frame.  */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
133
134 /* Save the result of dwarf2out_do_frame across PCH.  */
135 static GTY(()) bool saved_do_cfi_asm = 0;
136
137 /* Decide whether we want to emit frame unwind information for the current
138    translation unit.  */
139
140 int
141 dwarf2out_do_frame (void)
142 {
143   /* We want to emit correct CFA location expressions or lists, so we
144      have to return true if we're going to output debug info, even if
145      we're not going to output frame or unwind info.  */
146   return (write_symbols == DWARF2_DEBUG
147           || write_symbols == VMS_AND_DWARF2_DEBUG
148           || DWARF2_FRAME_INFO || saved_do_cfi_asm
149 #ifdef DWARF2_UNWIND_INFO
150           || (DWARF2_UNWIND_INFO
151               && (flag_unwind_tables
152                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
153 #endif
154           );
155 }
156
157 /* Decide whether to emit frame unwind via assembler directives.  */
158
159 int
160 dwarf2out_do_cfi_asm (void)
161 {
162   int enc;
163
164 #ifdef MIPS_DEBUGGING_INFO
165   return false;
166 #endif
167   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
168     return false;
169   if (saved_do_cfi_asm)
170     return true;
171   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
172     return false;
173
174   /* Make sure the personality encoding is one the assembler can support.
175      In particular, aligned addresses can't be handled.  */
176   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
177   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178     return false;
179   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
180   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181     return false;
182
183   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
184     {
185 #ifdef TARGET_UNWIND_INFO
186       return false;
187 #else
188       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
189         return false;
190 #endif
191     }
192
193   saved_do_cfi_asm = true;
194   return true;
195 }
196
197 /* The size of the target's pointer type.  */
198 #ifndef PTR_SIZE
199 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
200 #endif
201
202 /* Array of RTXes referenced by the debugging information, which therefore
203    must be kept around forever.  */
204 static GTY(()) VEC(rtx,gc) *used_rtx_array;
205
206 /* A pointer to the base of a list of incomplete types which might be
207    completed at some later time.  incomplete_types_list needs to be a
208    VEC(tree,gc) because we want to tell the garbage collector about
209    it.  */
210 static GTY(()) VEC(tree,gc) *incomplete_types;
211
212 /* A pointer to the base of a table of references to declaration
213    scopes.  This table is a display which tracks the nesting
214    of declaration scopes at the current scope and containing
215    scopes.  This table is used to find the proper place to
216    define type declaration DIE's.  */
217 static GTY(()) VEC(tree,gc) *decl_scope_table;
218
219 /* Pointers to various DWARF2 sections.  */
220 static GTY(()) section *debug_info_section;
221 static GTY(()) section *debug_abbrev_section;
222 static GTY(()) section *debug_aranges_section;
223 static GTY(()) section *debug_macinfo_section;
224 static GTY(()) section *debug_line_section;
225 static GTY(()) section *debug_loc_section;
226 static GTY(()) section *debug_pubnames_section;
227 static GTY(()) section *debug_pubtypes_section;
228 static GTY(()) section *debug_dcall_section;
229 static GTY(()) section *debug_vcall_section;
230 static GTY(()) section *debug_str_section;
231 static GTY(()) section *debug_ranges_section;
232 static GTY(()) section *debug_frame_section;
233
234 /* Personality decl of current unit.  Used only when assembler does not support
235    personality CFI.  */
236 static GTY(()) rtx current_unit_personality;
237
238 /* How to start an assembler comment.  */
239 #ifndef ASM_COMMENT_START
240 #define ASM_COMMENT_START ";#"
241 #endif
242
243 typedef struct dw_cfi_struct *dw_cfi_ref;
244 typedef struct dw_fde_struct *dw_fde_ref;
245 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
246
247 /* Call frames are described using a sequence of Call Frame
248    Information instructions.  The register number, offset
249    and address fields are provided as possible operands;
250    their use is selected by the opcode field.  */
251
252 enum dw_cfi_oprnd_type {
253   dw_cfi_oprnd_unused,
254   dw_cfi_oprnd_reg_num,
255   dw_cfi_oprnd_offset,
256   dw_cfi_oprnd_addr,
257   dw_cfi_oprnd_loc
258 };
259
260 typedef union GTY(()) dw_cfi_oprnd_struct {
261   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
262   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
263   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
264   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
265 }
266 dw_cfi_oprnd;
267
268 typedef struct GTY(()) dw_cfi_struct {
269   dw_cfi_ref dw_cfi_next;
270   enum dwarf_call_frame_info dw_cfi_opc;
271   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272     dw_cfi_oprnd1;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd2;
275 }
276 dw_cfi_node;
277
278 /* This is how we define the location of the CFA. We use to handle it
279    as REG + OFFSET all the time,  but now it can be more complex.
280    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
281    Instead of passing around REG and OFFSET, we pass a copy
282    of this structure.  */
283 typedef struct GTY(()) cfa_loc {
284   HOST_WIDE_INT offset;
285   HOST_WIDE_INT base_offset;
286   unsigned int reg;
287   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
288   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
289 } dw_cfa_location;
290
291 /* All call frame descriptions (FDE's) in the GCC generated DWARF
292    refer to a single Common Information Entry (CIE), defined at
293    the beginning of the .debug_frame section.  This use of a single
294    CIE obviates the need to keep track of multiple CIE's
295    in the DWARF generation routines below.  */
296
297 typedef struct GTY(()) dw_fde_struct {
298   tree decl;
299   const char *dw_fde_begin;
300   const char *dw_fde_current_label;
301   const char *dw_fde_end;
302   const char *dw_fde_vms_end_prologue;
303   const char *dw_fde_vms_begin_epilogue;
304   const char *dw_fde_hot_section_label;
305   const char *dw_fde_hot_section_end_label;
306   const char *dw_fde_unlikely_section_label;
307   const char *dw_fde_unlikely_section_end_label;
308   dw_cfi_ref dw_fde_cfi;
309   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
310   HOST_WIDE_INT stack_realignment;
311   unsigned funcdef_number;
312   /* Dynamic realign argument pointer register.  */
313   unsigned int drap_reg;
314   /* Virtual dynamic realign argument pointer register.  */
315   unsigned int vdrap_reg;
316   /* These 3 flags are copied from rtl_data in function.h.  */
317   unsigned all_throwers_are_sibcalls : 1;
318   unsigned uses_eh_lsda : 1;
319   unsigned nothrow : 1;
320   /* Whether we did stack realign in this call frame.  */
321   unsigned stack_realign : 1;
322   /* Whether dynamic realign argument pointer register has been saved.  */
323   unsigned drap_reg_saved: 1;
324   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
325   unsigned in_std_section : 1;
326   /* True iff dw_fde_unlikely_section_label is in text_section or
327      cold_text_section.  */
328   unsigned cold_in_std_section : 1;
329   /* True iff switched sections.  */
330   unsigned dw_fde_switched_sections : 1;
331   /* True iff switching from cold to hot section.  */
332   unsigned dw_fde_switched_cold_to_hot : 1;
333 }
334 dw_fde_node;
335
336 /* Maximum size (in bytes) of an artificially generated label.  */
337 #define MAX_ARTIFICIAL_LABEL_BYTES      30
338
339 /* The size of addresses as they appear in the Dwarf 2 data.
340    Some architectures use word addresses to refer to code locations,
341    but Dwarf 2 info always uses byte addresses.  On such machines,
342    Dwarf 2 addresses need to be larger than the architecture's
343    pointers.  */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
347
348 /* The size in bytes of a DWARF field indicating an offset or length
349    relative to a debug info section, specified to be 4 bytes in the
350    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
351    as PTR_SIZE.  */
352
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
356
357 /* The size in bytes of a DWARF 4 type signature.  */
358
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
362
363 /* According to the (draft) DWARF 3 specification, the initial length
364    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
365    bytes are 0xffffffff, followed by the length stored in the next 8
366    bytes.
367
368    However, the SGI/MIPS ABI uses an initial length which is equal to
369    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
370
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
374
375 /* Round SIZE up to the nearest BOUNDARY.  */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
378
379 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
387
388 /* CIE identifier.  */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
395
396 /* A pointer to the base of a table that contains frame description
397    information for each routine.  */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
399
400 /* Number of elements currently allocated for fde_table.  */
401 static GTY(()) unsigned fde_table_allocated;
402
403 /* Number of elements in fde_table currently in use.  */
404 static GTY(()) unsigned fde_table_in_use;
405
406 /* Size (in elements) of increments by which we may expand the
407    fde_table.  */
408 #define FDE_TABLE_INCREMENT 256
409
410 /* Get the current fde_table entry we should use.  */
411
412 static inline dw_fde_ref
413 current_fde (void)
414 {
415   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
416 }
417
418 /* A list of call frame insns for the CIE.  */
419 static GTY(()) dw_cfi_ref cie_cfi_head;
420
421 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427 #endif
428
429 struct GTY(()) indirect_string_node {
430   const char *str;
431   unsigned int refcount;
432   enum dwarf_form form;
433   char *label;
434 };
435
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437
438 /* True if the compilation unit has location entries that reference
439    debug strings.  */
440 static GTY(()) bool debug_str_hash_forced = false;
441
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444
445 /* True if the compilation unit places functions in more than one section.  */
446 static GTY(()) bool have_multiple_function_sections = false;
447
448 /* Whether the default text and cold text sections have been used at all.  */
449
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
452
453 /* The default cold text section.  */
454 static GTY(()) section *cold_text_section;
455
456 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
457
458 /* Forward declarations for functions defined in this file.  */
459
460 static char *stripattributes (const char *);
461 static const char *dwarf_cfi_name (unsigned);
462 static dw_cfi_ref new_cfi (void);
463 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
464 static void add_fde_cfi (const char *, dw_cfi_ref);
465 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
466 static void lookup_cfa (dw_cfa_location *);
467 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
468 #ifdef DWARF2_UNWIND_INFO
469 static void initial_return_save (rtx);
470 #endif
471 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
472                                           HOST_WIDE_INT);
473 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
474 static void output_cfi_directive (dw_cfi_ref);
475 static void output_call_frame_info (int);
476 static void dwarf2out_note_section_used (void);
477 static void flush_queued_reg_saves (void);
478 static bool clobbers_queued_reg_save (const_rtx);
479 static void dwarf2out_frame_debug_expr (rtx, const char *);
480
481 /* Support for complex CFA locations.  */
482 static void output_cfa_loc (dw_cfi_ref);
483 static void output_cfa_loc_raw (dw_cfi_ref);
484 static void get_cfa_from_loc_descr (dw_cfa_location *,
485                                     struct dw_loc_descr_struct *);
486 static struct dw_loc_descr_struct *build_cfa_loc
487   (dw_cfa_location *, HOST_WIDE_INT);
488 static struct dw_loc_descr_struct *build_cfa_aligned_loc
489   (HOST_WIDE_INT, HOST_WIDE_INT);
490 static void def_cfa_1 (const char *, dw_cfa_location *);
491
492 /* How to start an assembler comment.  */
493 #ifndef ASM_COMMENT_START
494 #define ASM_COMMENT_START ";#"
495 #endif
496
497 /* Data and reference forms for relocatable data.  */
498 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
499 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
500
501 #ifndef DEBUG_FRAME_SECTION
502 #define DEBUG_FRAME_SECTION     ".debug_frame"
503 #endif
504
505 #ifndef FUNC_BEGIN_LABEL
506 #define FUNC_BEGIN_LABEL        "LFB"
507 #endif
508
509 #ifndef FUNC_END_LABEL
510 #define FUNC_END_LABEL          "LFE"
511 #endif
512
513 #ifndef PROLOGUE_END_LABEL
514 #define PROLOGUE_END_LABEL      "LPE"
515 #endif
516
517 #ifndef EPILOGUE_BEGIN_LABEL
518 #define EPILOGUE_BEGIN_LABEL    "LEB"
519 #endif
520
521 #ifndef FRAME_BEGIN_LABEL
522 #define FRAME_BEGIN_LABEL       "Lframe"
523 #endif
524 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
525 #define CIE_END_LABEL           "LECIE"
526 #define FDE_LABEL               "LSFDE"
527 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
528 #define FDE_END_LABEL           "LEFDE"
529 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
530 #define LINE_NUMBER_END_LABEL   "LELT"
531 #define LN_PROLOG_AS_LABEL      "LASLTP"
532 #define LN_PROLOG_END_LABEL     "LELTP"
533 #define DIE_LABEL_PREFIX        "DW"
534
535 /* The DWARF 2 CFA column which tracks the return address.  Normally this
536    is the column for PC, or the first column after all of the hard
537    registers.  */
538 #ifndef DWARF_FRAME_RETURN_COLUMN
539 #ifdef PC_REGNUM
540 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
541 #else
542 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
543 #endif
544 #endif
545
546 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
547    default, we just provide columns for all registers.  */
548 #ifndef DWARF_FRAME_REGNUM
549 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
550 #endif
551 \f
552 /* Hook used by __throw.  */
553
554 rtx
555 expand_builtin_dwarf_sp_column (void)
556 {
557   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
558   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
559 }
560
561 /* Return a pointer to a copy of the section string name S with all
562    attributes stripped off, and an asterisk prepended (for assemble_name).  */
563
564 static inline char *
565 stripattributes (const char *s)
566 {
567   char *stripped = XNEWVEC (char, strlen (s) + 2);
568   char *p = stripped;
569
570   *p++ = '*';
571
572   while (*s && *s != ',')
573     *p++ = *s++;
574
575   *p = '\0';
576   return stripped;
577 }
578
579 /* MEM is a memory reference for the register size table, each element of
580    which has mode MODE.  Initialize column C as a return address column.  */
581
582 static void
583 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
584 {
585   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
586   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
587   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
588 }
589
590 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
591
592 static inline HOST_WIDE_INT
593 div_data_align (HOST_WIDE_INT off)
594 {
595   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
596   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
597   return r;
598 }
599
600 /* Return true if we need a signed version of a given opcode
601    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
602
603 static inline bool
604 need_data_align_sf_opcode (HOST_WIDE_INT off)
605 {
606   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
607 }
608
609 /* Generate code to initialize the register size table.  */
610
611 void
612 expand_builtin_init_dwarf_reg_sizes (tree address)
613 {
614   unsigned int i;
615   enum machine_mode mode = TYPE_MODE (char_type_node);
616   rtx addr = expand_normal (address);
617   rtx mem = gen_rtx_MEM (BLKmode, addr);
618   bool wrote_return_column = false;
619
620   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
621     {
622       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
623
624       if (rnum < DWARF_FRAME_REGISTERS)
625         {
626           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
627           enum machine_mode save_mode = reg_raw_mode[i];
628           HOST_WIDE_INT size;
629
630           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
631             save_mode = choose_hard_reg_mode (i, 1, true);
632           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
633             {
634               if (save_mode == VOIDmode)
635                 continue;
636               wrote_return_column = true;
637             }
638           size = GET_MODE_SIZE (save_mode);
639           if (offset < 0)
640             continue;
641
642           emit_move_insn (adjust_address (mem, mode, offset),
643                           gen_int_mode (size, mode));
644         }
645     }
646
647   if (!wrote_return_column)
648     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
649
650 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
651   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
652 #endif
653
654   targetm.init_dwarf_reg_sizes_extra (address);
655 }
656
657 /* Convert a DWARF call frame info. operation to its string name */
658
659 static const char *
660 dwarf_cfi_name (unsigned int cfi_opc)
661 {
662   switch (cfi_opc)
663     {
664     case DW_CFA_advance_loc:
665       return "DW_CFA_advance_loc";
666     case DW_CFA_offset:
667       return "DW_CFA_offset";
668     case DW_CFA_restore:
669       return "DW_CFA_restore";
670     case DW_CFA_nop:
671       return "DW_CFA_nop";
672     case DW_CFA_set_loc:
673       return "DW_CFA_set_loc";
674     case DW_CFA_advance_loc1:
675       return "DW_CFA_advance_loc1";
676     case DW_CFA_advance_loc2:
677       return "DW_CFA_advance_loc2";
678     case DW_CFA_advance_loc4:
679       return "DW_CFA_advance_loc4";
680     case DW_CFA_offset_extended:
681       return "DW_CFA_offset_extended";
682     case DW_CFA_restore_extended:
683       return "DW_CFA_restore_extended";
684     case DW_CFA_undefined:
685       return "DW_CFA_undefined";
686     case DW_CFA_same_value:
687       return "DW_CFA_same_value";
688     case DW_CFA_register:
689       return "DW_CFA_register";
690     case DW_CFA_remember_state:
691       return "DW_CFA_remember_state";
692     case DW_CFA_restore_state:
693       return "DW_CFA_restore_state";
694     case DW_CFA_def_cfa:
695       return "DW_CFA_def_cfa";
696     case DW_CFA_def_cfa_register:
697       return "DW_CFA_def_cfa_register";
698     case DW_CFA_def_cfa_offset:
699       return "DW_CFA_def_cfa_offset";
700
701     /* DWARF 3 */
702     case DW_CFA_def_cfa_expression:
703       return "DW_CFA_def_cfa_expression";
704     case DW_CFA_expression:
705       return "DW_CFA_expression";
706     case DW_CFA_offset_extended_sf:
707       return "DW_CFA_offset_extended_sf";
708     case DW_CFA_def_cfa_sf:
709       return "DW_CFA_def_cfa_sf";
710     case DW_CFA_def_cfa_offset_sf:
711       return "DW_CFA_def_cfa_offset_sf";
712
713     /* SGI/MIPS specific */
714     case DW_CFA_MIPS_advance_loc8:
715       return "DW_CFA_MIPS_advance_loc8";
716
717     /* GNU extensions */
718     case DW_CFA_GNU_window_save:
719       return "DW_CFA_GNU_window_save";
720     case DW_CFA_GNU_args_size:
721       return "DW_CFA_GNU_args_size";
722     case DW_CFA_GNU_negative_offset_extended:
723       return "DW_CFA_GNU_negative_offset_extended";
724
725     default:
726       return "DW_CFA_<unknown>";
727     }
728 }
729
730 /* Return a pointer to a newly allocated Call Frame Instruction.  */
731
732 static inline dw_cfi_ref
733 new_cfi (void)
734 {
735   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
736
737   cfi->dw_cfi_next = NULL;
738   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
739   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
740
741   return cfi;
742 }
743
744 /* Add a Call Frame Instruction to list of instructions.  */
745
746 static inline void
747 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
748 {
749   dw_cfi_ref *p;
750   dw_fde_ref fde = current_fde ();
751
752   /* When DRAP is used, CFA is defined with an expression.  Redefine
753      CFA may lead to a different CFA value.   */
754   /* ??? Of course, this heuristic fails when we're annotating epilogues,
755      because of course we'll always want to redefine the CFA back to the
756      stack pointer on the way out.  Where should we move this check?  */
757   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
758     switch (cfi->dw_cfi_opc)
759       {
760         case DW_CFA_def_cfa_register:
761         case DW_CFA_def_cfa_offset:
762         case DW_CFA_def_cfa_offset_sf:
763         case DW_CFA_def_cfa:
764         case DW_CFA_def_cfa_sf:
765           gcc_unreachable ();
766
767         default:
768           break;
769       }
770
771   /* Find the end of the chain.  */
772   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
773     ;
774
775   *p = cfi;
776 }
777
778 /* Generate a new label for the CFI info to refer to.  FORCE is true
779    if a label needs to be output even when using .cfi_* directives.  */
780
781 char *
782 dwarf2out_cfi_label (bool force)
783 {
784   static char label[20];
785
786   if (!force && dwarf2out_do_cfi_asm ())
787     {
788       /* In this case, we will be emitting the asm directive instead of
789          the label, so just return a placeholder to keep the rest of the
790          interfaces happy.  */
791       strcpy (label, "<do not output>");
792     }
793   else
794     {
795       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
796       ASM_OUTPUT_LABEL (asm_out_file, label);
797     }
798
799   return label;
800 }
801
802 /* True if remember_state should be emitted before following CFI directive.  */
803 static bool emit_cfa_remember;
804
805 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
806    or to the CIE if LABEL is NULL.  */
807
808 static void
809 add_fde_cfi (const char *label, dw_cfi_ref cfi)
810 {
811   dw_cfi_ref *list_head;
812
813   if (emit_cfa_remember)
814     {
815       dw_cfi_ref cfi_remember;
816
817       /* Emit the state save.  */
818       emit_cfa_remember = false;
819       cfi_remember = new_cfi ();
820       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
821       add_fde_cfi (label, cfi_remember);
822     }
823
824   list_head = &cie_cfi_head;
825
826   if (dwarf2out_do_cfi_asm ())
827     {
828       if (label)
829         {
830           dw_fde_ref fde = current_fde ();
831
832           gcc_assert (fde != NULL);
833
834           /* We still have to add the cfi to the list so that lookup_cfa
835              works later on.  When -g2 and above we even need to force
836              emitting of CFI labels and add to list a DW_CFA_set_loc for
837              convert_cfa_to_fb_loc_list purposes.  If we're generating
838              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
839              convert_cfa_to_fb_loc_list.  */
840           if (dwarf_version == 2
841               && debug_info_level > DINFO_LEVEL_TERSE
842               && (write_symbols == DWARF2_DEBUG
843                   || write_symbols == VMS_AND_DWARF2_DEBUG))
844             {
845               switch (cfi->dw_cfi_opc)
846                 {
847                 case DW_CFA_def_cfa_offset:
848                 case DW_CFA_def_cfa_offset_sf:
849                 case DW_CFA_def_cfa_register:
850                 case DW_CFA_def_cfa:
851                 case DW_CFA_def_cfa_sf:
852                 case DW_CFA_def_cfa_expression:
853                 case DW_CFA_restore_state:
854                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
855                     label = dwarf2out_cfi_label (true);
856
857                   if (fde->dw_fde_current_label == NULL
858                       || strcmp (label, fde->dw_fde_current_label) != 0)
859                     {
860                       dw_cfi_ref xcfi;
861
862                       label = xstrdup (label);
863
864                       /* Set the location counter to the new label.  */
865                       xcfi = new_cfi ();
866                       /* It doesn't metter whether DW_CFA_set_loc
867                          or DW_CFA_advance_loc4 is added here, those aren't
868                          emitted into assembly, only looked up by
869                          convert_cfa_to_fb_loc_list.  */
870                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
871                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
872                       add_cfi (&fde->dw_fde_cfi, xcfi);
873                       fde->dw_fde_current_label = label;
874                     }
875                   break;
876                 default:
877                   break;
878                 }
879             }
880
881           output_cfi_directive (cfi);
882
883           list_head = &fde->dw_fde_cfi;
884         }
885       /* ??? If this is a CFI for the CIE, we don't emit.  This
886          assumes that the standard CIE contents that the assembler
887          uses matches the standard CIE contents that the compiler
888          uses.  This is probably a bad assumption.  I'm not quite
889          sure how to address this for now.  */
890     }
891   else if (label)
892     {
893       dw_fde_ref fde = current_fde ();
894
895       gcc_assert (fde != NULL);
896
897       if (*label == 0)
898         label = dwarf2out_cfi_label (false);
899
900       if (fde->dw_fde_current_label == NULL
901           || strcmp (label, fde->dw_fde_current_label) != 0)
902         {
903           dw_cfi_ref xcfi;
904
905           label = xstrdup (label);
906
907           /* Set the location counter to the new label.  */
908           xcfi = new_cfi ();
909           /* If we have a current label, advance from there, otherwise
910              set the location directly using set_loc.  */
911           xcfi->dw_cfi_opc = fde->dw_fde_current_label
912                              ? DW_CFA_advance_loc4
913                              : DW_CFA_set_loc;
914           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
915           add_cfi (&fde->dw_fde_cfi, xcfi);
916
917           fde->dw_fde_current_label = label;
918         }
919
920       list_head = &fde->dw_fde_cfi;
921     }
922
923   add_cfi (list_head, cfi);
924 }
925
926 /* Subroutine of lookup_cfa.  */
927
928 static void
929 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
930 {
931   switch (cfi->dw_cfi_opc)
932     {
933     case DW_CFA_def_cfa_offset:
934     case DW_CFA_def_cfa_offset_sf:
935       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
936       break;
937     case DW_CFA_def_cfa_register:
938       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
939       break;
940     case DW_CFA_def_cfa:
941     case DW_CFA_def_cfa_sf:
942       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
943       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
944       break;
945     case DW_CFA_def_cfa_expression:
946       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
947       break;
948
949     case DW_CFA_remember_state:
950       gcc_assert (!remember->in_use);
951       *remember = *loc;
952       remember->in_use = 1;
953       break;
954     case DW_CFA_restore_state:
955       gcc_assert (remember->in_use);
956       *loc = *remember;
957       remember->in_use = 0;
958       break;
959
960     default:
961       break;
962     }
963 }
964
965 /* Find the previous value for the CFA.  */
966
967 static void
968 lookup_cfa (dw_cfa_location *loc)
969 {
970   dw_cfi_ref cfi;
971   dw_fde_ref fde;
972   dw_cfa_location remember;
973
974   memset (loc, 0, sizeof (*loc));
975   loc->reg = INVALID_REGNUM;
976   remember = *loc;
977
978   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
979     lookup_cfa_1 (cfi, loc, &remember);
980
981   fde = current_fde ();
982   if (fde)
983     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
984       lookup_cfa_1 (cfi, loc, &remember);
985 }
986
987 /* The current rule for calculating the DWARF2 canonical frame address.  */
988 static dw_cfa_location cfa;
989
990 /* The register used for saving registers to the stack, and its offset
991    from the CFA.  */
992 static dw_cfa_location cfa_store;
993
994 /* The current save location around an epilogue.  */
995 static dw_cfa_location cfa_remember;
996
997 /* The running total of the size of arguments pushed onto the stack.  */
998 static HOST_WIDE_INT args_size;
999
1000 /* The last args_size we actually output.  */
1001 static HOST_WIDE_INT old_args_size;
1002
1003 /* Entry point to update the canonical frame address (CFA).
1004    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1005    calculated from REG+OFFSET.  */
1006
1007 void
1008 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1009 {
1010   dw_cfa_location loc;
1011   loc.indirect = 0;
1012   loc.base_offset = 0;
1013   loc.reg = reg;
1014   loc.offset = offset;
1015   def_cfa_1 (label, &loc);
1016 }
1017
1018 /* Determine if two dw_cfa_location structures define the same data.  */
1019
1020 static bool
1021 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1022 {
1023   return (loc1->reg == loc2->reg
1024           && loc1->offset == loc2->offset
1025           && loc1->indirect == loc2->indirect
1026           && (loc1->indirect == 0
1027               || loc1->base_offset == loc2->base_offset));
1028 }
1029
1030 /* This routine does the actual work.  The CFA is now calculated from
1031    the dw_cfa_location structure.  */
1032
1033 static void
1034 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1035 {
1036   dw_cfi_ref cfi;
1037   dw_cfa_location old_cfa, loc;
1038
1039   cfa = *loc_p;
1040   loc = *loc_p;
1041
1042   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1043     cfa_store.offset = loc.offset;
1044
1045   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1046   lookup_cfa (&old_cfa);
1047
1048   /* If nothing changed, no need to issue any call frame instructions.  */
1049   if (cfa_equal_p (&loc, &old_cfa))
1050     return;
1051
1052   cfi = new_cfi ();
1053
1054   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1055     {
1056       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1057          the CFA register did not change but the offset did.  The data
1058          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1059          in the assembler via the .cfi_def_cfa_offset directive.  */
1060       if (loc.offset < 0)
1061         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1062       else
1063         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1064       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1065     }
1066
1067 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1068   else if (loc.offset == old_cfa.offset
1069            && old_cfa.reg != INVALID_REGNUM
1070            && !loc.indirect
1071            && !old_cfa.indirect)
1072     {
1073       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1074          indicating the CFA register has changed to <register> but the
1075          offset has not changed.  */
1076       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1077       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1078     }
1079 #endif
1080
1081   else if (loc.indirect == 0)
1082     {
1083       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1084          indicating the CFA register has changed to <register> with
1085          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1086          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1087          directive.  */
1088       if (loc.offset < 0)
1089         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1090       else
1091         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1092       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1093       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1094     }
1095   else
1096     {
1097       /* Construct a DW_CFA_def_cfa_expression instruction to
1098          calculate the CFA using a full location expression since no
1099          register-offset pair is available.  */
1100       struct dw_loc_descr_struct *loc_list;
1101
1102       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1103       loc_list = build_cfa_loc (&loc, 0);
1104       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1105     }
1106
1107   add_fde_cfi (label, cfi);
1108 }
1109
1110 /* Add the CFI for saving a register.  REG is the CFA column number.
1111    LABEL is passed to add_fde_cfi.
1112    If SREG is -1, the register is saved at OFFSET from the CFA;
1113    otherwise it is saved in SREG.  */
1114
1115 static void
1116 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1117 {
1118   dw_cfi_ref cfi = new_cfi ();
1119   dw_fde_ref fde = current_fde ();
1120
1121   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1122
1123   /* When stack is aligned, store REG using DW_CFA_expression with
1124      FP.  */
1125   if (fde
1126       && fde->stack_realign
1127       && sreg == INVALID_REGNUM)
1128     {
1129       cfi->dw_cfi_opc = DW_CFA_expression;
1130       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1131       cfi->dw_cfi_oprnd2.dw_cfi_loc
1132         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1133     }
1134   else if (sreg == INVALID_REGNUM)
1135     {
1136       if (need_data_align_sf_opcode (offset))
1137         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1138       else if (reg & ~0x3f)
1139         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1140       else
1141         cfi->dw_cfi_opc = DW_CFA_offset;
1142       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1143     }
1144   else if (sreg == reg)
1145     cfi->dw_cfi_opc = DW_CFA_same_value;
1146   else
1147     {
1148       cfi->dw_cfi_opc = DW_CFA_register;
1149       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1150     }
1151
1152   add_fde_cfi (label, cfi);
1153 }
1154
1155 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1156    This CFI tells the unwinder that it needs to restore the window registers
1157    from the previous frame's window save area.
1158
1159    ??? Perhaps we should note in the CIE where windows are saved (instead of
1160    assuming 0(cfa)) and what registers are in the window.  */
1161
1162 void
1163 dwarf2out_window_save (const char *label)
1164 {
1165   dw_cfi_ref cfi = new_cfi ();
1166
1167   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1168   add_fde_cfi (label, cfi);
1169 }
1170
1171 /* Entry point for saving a register to the stack.  REG is the GCC register
1172    number.  LABEL and OFFSET are passed to reg_save.  */
1173
1174 void
1175 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1176 {
1177   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1178 }
1179
1180 /* Entry point for saving the return address in the stack.
1181    LABEL and OFFSET are passed to reg_save.  */
1182
1183 void
1184 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1185 {
1186   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1187 }
1188
1189 /* Entry point for saving the return address in a register.
1190    LABEL and SREG are passed to reg_save.  */
1191
1192 void
1193 dwarf2out_return_reg (const char *label, unsigned int sreg)
1194 {
1195   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1196 }
1197
1198 #ifdef DWARF2_UNWIND_INFO
1199 /* Record the initial position of the return address.  RTL is
1200    INCOMING_RETURN_ADDR_RTX.  */
1201
1202 static void
1203 initial_return_save (rtx rtl)
1204 {
1205   unsigned int reg = INVALID_REGNUM;
1206   HOST_WIDE_INT offset = 0;
1207
1208   switch (GET_CODE (rtl))
1209     {
1210     case REG:
1211       /* RA is in a register.  */
1212       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1213       break;
1214
1215     case MEM:
1216       /* RA is on the stack.  */
1217       rtl = XEXP (rtl, 0);
1218       switch (GET_CODE (rtl))
1219         {
1220         case REG:
1221           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1222           offset = 0;
1223           break;
1224
1225         case PLUS:
1226           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1227           offset = INTVAL (XEXP (rtl, 1));
1228           break;
1229
1230         case MINUS:
1231           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1232           offset = -INTVAL (XEXP (rtl, 1));
1233           break;
1234
1235         default:
1236           gcc_unreachable ();
1237         }
1238
1239       break;
1240
1241     case PLUS:
1242       /* The return address is at some offset from any value we can
1243          actually load.  For instance, on the SPARC it is in %i7+8. Just
1244          ignore the offset for now; it doesn't matter for unwinding frames.  */
1245       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1246       initial_return_save (XEXP (rtl, 0));
1247       return;
1248
1249     default:
1250       gcc_unreachable ();
1251     }
1252
1253   if (reg != DWARF_FRAME_RETURN_COLUMN)
1254     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1255 }
1256 #endif
1257
1258 /* Given a SET, calculate the amount of stack adjustment it
1259    contains.  */
1260
1261 static HOST_WIDE_INT
1262 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1263                      HOST_WIDE_INT cur_offset)
1264 {
1265   const_rtx src = SET_SRC (pattern);
1266   const_rtx dest = SET_DEST (pattern);
1267   HOST_WIDE_INT offset = 0;
1268   enum rtx_code code;
1269
1270   if (dest == stack_pointer_rtx)
1271     {
1272       code = GET_CODE (src);
1273
1274       /* Assume (set (reg sp) (reg whatever)) sets args_size
1275          level to 0.  */
1276       if (code == REG && src != stack_pointer_rtx)
1277         {
1278           offset = -cur_args_size;
1279 #ifndef STACK_GROWS_DOWNWARD
1280           offset = -offset;
1281 #endif
1282           return offset - cur_offset;
1283         }
1284
1285       if (! (code == PLUS || code == MINUS)
1286           || XEXP (src, 0) != stack_pointer_rtx
1287           || !CONST_INT_P (XEXP (src, 1)))
1288         return 0;
1289
1290       /* (set (reg sp) (plus (reg sp) (const_int))) */
1291       offset = INTVAL (XEXP (src, 1));
1292       if (code == PLUS)
1293         offset = -offset;
1294       return offset;
1295     }
1296
1297   if (MEM_P (src) && !MEM_P (dest))
1298     dest = src;
1299   if (MEM_P (dest))
1300     {
1301       /* (set (mem (pre_dec (reg sp))) (foo)) */
1302       src = XEXP (dest, 0);
1303       code = GET_CODE (src);
1304
1305       switch (code)
1306         {
1307         case PRE_MODIFY:
1308         case POST_MODIFY:
1309           if (XEXP (src, 0) == stack_pointer_rtx)
1310             {
1311               rtx val = XEXP (XEXP (src, 1), 1);
1312               /* We handle only adjustments by constant amount.  */
1313               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1314                           && CONST_INT_P (val));
1315               offset = -INTVAL (val);
1316               break;
1317             }
1318           return 0;
1319
1320         case PRE_DEC:
1321         case POST_DEC:
1322           if (XEXP (src, 0) == stack_pointer_rtx)
1323             {
1324               offset = GET_MODE_SIZE (GET_MODE (dest));
1325               break;
1326             }
1327           return 0;
1328
1329         case PRE_INC:
1330         case POST_INC:
1331           if (XEXP (src, 0) == stack_pointer_rtx)
1332             {
1333               offset = -GET_MODE_SIZE (GET_MODE (dest));
1334               break;
1335             }
1336           return 0;
1337
1338         default:
1339           return 0;
1340         }
1341     }
1342   else
1343     return 0;
1344
1345   return offset;
1346 }
1347
1348 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1349    indexed by INSN_UID.  */
1350
1351 static HOST_WIDE_INT *barrier_args_size;
1352
1353 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1354
1355 static HOST_WIDE_INT
1356 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1357                              VEC (rtx, heap) **next)
1358 {
1359   HOST_WIDE_INT offset = 0;
1360   int i;
1361
1362   if (! RTX_FRAME_RELATED_P (insn))
1363     {
1364       if (prologue_epilogue_contains (insn))
1365         /* Nothing */;
1366       else if (GET_CODE (PATTERN (insn)) == SET)
1367         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1368       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1369                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1370         {
1371           /* There may be stack adjustments inside compound insns.  Search
1372              for them.  */
1373           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1374             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1375               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1376                                              cur_args_size, offset);
1377         }
1378     }
1379   else
1380     {
1381       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1382
1383       if (expr)
1384         {
1385           expr = XEXP (expr, 0);
1386           if (GET_CODE (expr) == PARALLEL
1387               || GET_CODE (expr) == SEQUENCE)
1388             for (i = 1; i < XVECLEN (expr, 0); i++)
1389               {
1390                 rtx elem = XVECEXP (expr, 0, i);
1391
1392                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1393                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1394               }
1395         }
1396     }
1397
1398 #ifndef STACK_GROWS_DOWNWARD
1399   offset = -offset;
1400 #endif
1401
1402   cur_args_size += offset;
1403   if (cur_args_size < 0)
1404     cur_args_size = 0;
1405
1406   if (JUMP_P (insn))
1407     {
1408       rtx dest = JUMP_LABEL (insn);
1409
1410       if (dest)
1411         {
1412           if (barrier_args_size [INSN_UID (dest)] < 0)
1413             {
1414               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1415               VEC_safe_push (rtx, heap, *next, dest);
1416             }
1417         }
1418     }
1419
1420   return cur_args_size;
1421 }
1422
1423 /* Walk the whole function and compute args_size on BARRIERs.  */
1424
1425 static void
1426 compute_barrier_args_size (void)
1427 {
1428   int max_uid = get_max_uid (), i;
1429   rtx insn;
1430   VEC (rtx, heap) *worklist, *next, *tmp;
1431
1432   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1433   for (i = 0; i < max_uid; i++)
1434     barrier_args_size[i] = -1;
1435
1436   worklist = VEC_alloc (rtx, heap, 20);
1437   next = VEC_alloc (rtx, heap, 20);
1438   insn = get_insns ();
1439   barrier_args_size[INSN_UID (insn)] = 0;
1440   VEC_quick_push (rtx, worklist, insn);
1441   for (;;)
1442     {
1443       while (!VEC_empty (rtx, worklist))
1444         {
1445           rtx prev, body, first_insn;
1446           HOST_WIDE_INT cur_args_size;
1447
1448           first_insn = insn = VEC_pop (rtx, worklist);
1449           cur_args_size = barrier_args_size[INSN_UID (insn)];
1450           prev = prev_nonnote_insn (insn);
1451           if (prev && BARRIER_P (prev))
1452             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1453
1454           for (; insn; insn = NEXT_INSN (insn))
1455             {
1456               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1457                 continue;
1458               if (BARRIER_P (insn))
1459                 break;
1460
1461               if (LABEL_P (insn))
1462                 {
1463                   if (insn == first_insn)
1464                     continue;
1465                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1466                     {
1467                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1468                       continue;
1469                     }
1470                   else
1471                     {
1472                       /* The insns starting with this label have been
1473                          already scanned or are in the worklist.  */
1474                       break;
1475                     }
1476                 }
1477
1478               body = PATTERN (insn);
1479               if (GET_CODE (body) == SEQUENCE)
1480                 {
1481                   HOST_WIDE_INT dest_args_size = cur_args_size;
1482                   for (i = 1; i < XVECLEN (body, 0); i++)
1483                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1484                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1485                       dest_args_size
1486                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1487                                                        dest_args_size, &next);
1488                     else
1489                       cur_args_size
1490                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1491                                                        cur_args_size, &next);
1492
1493                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1494                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1495                                                  dest_args_size, &next);
1496                   else
1497                     cur_args_size
1498                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1499                                                      cur_args_size, &next);
1500                 }
1501               else
1502                 cur_args_size
1503                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1504             }
1505         }
1506
1507       if (VEC_empty (rtx, next))
1508         break;
1509
1510       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1511       tmp = next;
1512       next = worklist;
1513       worklist = tmp;
1514       VEC_truncate (rtx, next, 0);
1515     }
1516
1517   VEC_free (rtx, heap, worklist);
1518   VEC_free (rtx, heap, next);
1519 }
1520
1521 /* Add a CFI to update the running total of the size of arguments
1522    pushed onto the stack.  */
1523
1524 static void
1525 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1526 {
1527   dw_cfi_ref cfi;
1528
1529   if (size == old_args_size)
1530     return;
1531
1532   old_args_size = size;
1533
1534   cfi = new_cfi ();
1535   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1536   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1537   add_fde_cfi (label, cfi);
1538 }
1539
1540 /* Record a stack adjustment of OFFSET bytes.  */
1541
1542 static void
1543 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1544 {
1545   if (cfa.reg == STACK_POINTER_REGNUM)
1546     cfa.offset += offset;
1547
1548   if (cfa_store.reg == STACK_POINTER_REGNUM)
1549     cfa_store.offset += offset;
1550
1551   if (ACCUMULATE_OUTGOING_ARGS)
1552     return;
1553
1554 #ifndef STACK_GROWS_DOWNWARD
1555   offset = -offset;
1556 #endif
1557
1558   args_size += offset;
1559   if (args_size < 0)
1560     args_size = 0;
1561
1562   def_cfa_1 (label, &cfa);
1563   if (flag_asynchronous_unwind_tables)
1564     dwarf2out_args_size (label, args_size);
1565 }
1566
1567 /* Check INSN to see if it looks like a push or a stack adjustment, and
1568    make a note of it if it does.  EH uses this information to find out
1569    how much extra space it needs to pop off the stack.  */
1570
1571 static void
1572 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1573 {
1574   HOST_WIDE_INT offset;
1575   const char *label;
1576   int i;
1577
1578   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1579      with this function.  Proper support would require all frame-related
1580      insns to be marked, and to be able to handle saving state around
1581      epilogues textually in the middle of the function.  */
1582   if (prologue_epilogue_contains (insn))
1583     return;
1584
1585   /* If INSN is an instruction from target of an annulled branch, the
1586      effects are for the target only and so current argument size
1587      shouldn't change at all.  */
1588   if (final_sequence
1589       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1590       && INSN_FROM_TARGET_P (insn))
1591     return;
1592
1593   /* If only calls can throw, and we have a frame pointer,
1594      save up adjustments until we see the CALL_INSN.  */
1595   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1596     {
1597       if (CALL_P (insn) && !after_p)
1598         {
1599           /* Extract the size of the args from the CALL rtx itself.  */
1600           insn = PATTERN (insn);
1601           if (GET_CODE (insn) == PARALLEL)
1602             insn = XVECEXP (insn, 0, 0);
1603           if (GET_CODE (insn) == SET)
1604             insn = SET_SRC (insn);
1605           gcc_assert (GET_CODE (insn) == CALL);
1606           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1607         }
1608       return;
1609     }
1610
1611   if (CALL_P (insn) && !after_p)
1612     {
1613       if (!flag_asynchronous_unwind_tables)
1614         dwarf2out_args_size ("", args_size);
1615       return;
1616     }
1617   else if (BARRIER_P (insn))
1618     {
1619       /* Don't call compute_barrier_args_size () if the only
1620          BARRIER is at the end of function.  */
1621       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1622         compute_barrier_args_size ();
1623       if (barrier_args_size == NULL)
1624         offset = 0;
1625       else
1626         {
1627           offset = barrier_args_size[INSN_UID (insn)];
1628           if (offset < 0)
1629             offset = 0;
1630         }
1631
1632       offset -= args_size;
1633 #ifndef STACK_GROWS_DOWNWARD
1634       offset = -offset;
1635 #endif
1636     }
1637   else if (GET_CODE (PATTERN (insn)) == SET)
1638     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1639   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1640            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1641     {
1642       /* There may be stack adjustments inside compound insns.  Search
1643          for them.  */
1644       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1645         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1646           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1647                                          args_size, offset);
1648     }
1649   else
1650     return;
1651
1652   if (offset == 0)
1653     return;
1654
1655   label = dwarf2out_cfi_label (false);
1656   dwarf2out_stack_adjust (offset, label);
1657 }
1658
1659 #endif
1660
1661 /* We delay emitting a register save until either (a) we reach the end
1662    of the prologue or (b) the register is clobbered.  This clusters
1663    register saves so that there are fewer pc advances.  */
1664
1665 struct GTY(()) queued_reg_save {
1666   struct queued_reg_save *next;
1667   rtx reg;
1668   HOST_WIDE_INT cfa_offset;
1669   rtx saved_reg;
1670 };
1671
1672 static GTY(()) struct queued_reg_save *queued_reg_saves;
1673
1674 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1675 struct GTY(()) reg_saved_in_data {
1676   rtx orig_reg;
1677   rtx saved_in_reg;
1678 };
1679
1680 /* A list of registers saved in other registers.
1681    The list intentionally has a small maximum capacity of 4; if your
1682    port needs more than that, you might consider implementing a
1683    more efficient data structure.  */
1684 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1685 static GTY(()) size_t num_regs_saved_in_regs;
1686
1687 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1688 static const char *last_reg_save_label;
1689
1690 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1691    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1692
1693 static void
1694 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1695 {
1696   struct queued_reg_save *q;
1697
1698   /* Duplicates waste space, but it's also necessary to remove them
1699      for correctness, since the queue gets output in reverse
1700      order.  */
1701   for (q = queued_reg_saves; q != NULL; q = q->next)
1702     if (REGNO (q->reg) == REGNO (reg))
1703       break;
1704
1705   if (q == NULL)
1706     {
1707       q = ggc_alloc_queued_reg_save ();
1708       q->next = queued_reg_saves;
1709       queued_reg_saves = q;
1710     }
1711
1712   q->reg = reg;
1713   q->cfa_offset = offset;
1714   q->saved_reg = sreg;
1715
1716   last_reg_save_label = label;
1717 }
1718
1719 /* Output all the entries in QUEUED_REG_SAVES.  */
1720
1721 static void
1722 flush_queued_reg_saves (void)
1723 {
1724   struct queued_reg_save *q;
1725
1726   for (q = queued_reg_saves; q; q = q->next)
1727     {
1728       size_t i;
1729       unsigned int reg, sreg;
1730
1731       for (i = 0; i < num_regs_saved_in_regs; i++)
1732         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1733           break;
1734       if (q->saved_reg && i == num_regs_saved_in_regs)
1735         {
1736           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1737           num_regs_saved_in_regs++;
1738         }
1739       if (i != num_regs_saved_in_regs)
1740         {
1741           regs_saved_in_regs[i].orig_reg = q->reg;
1742           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1743         }
1744
1745       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1746       if (q->saved_reg)
1747         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1748       else
1749         sreg = INVALID_REGNUM;
1750       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1751     }
1752
1753   queued_reg_saves = NULL;
1754   last_reg_save_label = NULL;
1755 }
1756
1757 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1758    location for?  Or, does it clobber a register which we've previously
1759    said that some other register is saved in, and for which we now
1760    have a new location for?  */
1761
1762 static bool
1763 clobbers_queued_reg_save (const_rtx insn)
1764 {
1765   struct queued_reg_save *q;
1766
1767   for (q = queued_reg_saves; q; q = q->next)
1768     {
1769       size_t i;
1770       if (modified_in_p (q->reg, insn))
1771         return true;
1772       for (i = 0; i < num_regs_saved_in_regs; i++)
1773         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1774             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1775           return true;
1776     }
1777
1778   return false;
1779 }
1780
1781 /* Entry point for saving the first register into the second.  */
1782
1783 void
1784 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1785 {
1786   size_t i;
1787   unsigned int regno, sregno;
1788
1789   for (i = 0; i < num_regs_saved_in_regs; i++)
1790     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1791       break;
1792   if (i == num_regs_saved_in_regs)
1793     {
1794       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1795       num_regs_saved_in_regs++;
1796     }
1797   regs_saved_in_regs[i].orig_reg = reg;
1798   regs_saved_in_regs[i].saved_in_reg = sreg;
1799
1800   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1801   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1802   reg_save (label, regno, sregno, 0);
1803 }
1804
1805 /* What register, if any, is currently saved in REG?  */
1806
1807 static rtx
1808 reg_saved_in (rtx reg)
1809 {
1810   unsigned int regn = REGNO (reg);
1811   size_t i;
1812   struct queued_reg_save *q;
1813
1814   for (q = queued_reg_saves; q; q = q->next)
1815     if (q->saved_reg && regn == REGNO (q->saved_reg))
1816       return q->reg;
1817
1818   for (i = 0; i < num_regs_saved_in_regs; i++)
1819     if (regs_saved_in_regs[i].saved_in_reg
1820         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1821       return regs_saved_in_regs[i].orig_reg;
1822
1823   return NULL_RTX;
1824 }
1825
1826
1827 /* A temporary register holding an integral value used in adjusting SP
1828    or setting up the store_reg.  The "offset" field holds the integer
1829    value, not an offset.  */
1830 static dw_cfa_location cfa_temp;
1831
1832 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1833
1834 static void
1835 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1836 {
1837   memset (&cfa, 0, sizeof (cfa));
1838
1839   switch (GET_CODE (pat))
1840     {
1841     case PLUS:
1842       cfa.reg = REGNO (XEXP (pat, 0));
1843       cfa.offset = INTVAL (XEXP (pat, 1));
1844       break;
1845
1846     case REG:
1847       cfa.reg = REGNO (pat);
1848       break;
1849
1850     default:
1851       /* Recurse and define an expression.  */
1852       gcc_unreachable ();
1853     }
1854
1855   def_cfa_1 (label, &cfa);
1856 }
1857
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1859
1860 static void
1861 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1862 {
1863   rtx src, dest;
1864
1865   gcc_assert (GET_CODE (pat) == SET);
1866   dest = XEXP (pat, 0);
1867   src = XEXP (pat, 1);
1868
1869   switch (GET_CODE (src))
1870     {
1871     case PLUS:
1872       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1873       cfa.offset -= INTVAL (XEXP (src, 1));
1874       break;
1875
1876     case REG:
1877         break;
1878
1879     default:
1880         gcc_unreachable ();
1881     }
1882
1883   cfa.reg = REGNO (dest);
1884   gcc_assert (cfa.indirect == 0);
1885
1886   def_cfa_1 (label, &cfa);
1887 }
1888
1889 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1890
1891 static void
1892 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1893 {
1894   HOST_WIDE_INT offset;
1895   rtx src, addr, span;
1896
1897   src = XEXP (set, 1);
1898   addr = XEXP (set, 0);
1899   gcc_assert (MEM_P (addr));
1900   addr = XEXP (addr, 0);
1901
1902   /* As documented, only consider extremely simple addresses.  */
1903   switch (GET_CODE (addr))
1904     {
1905     case REG:
1906       gcc_assert (REGNO (addr) == cfa.reg);
1907       offset = -cfa.offset;
1908       break;
1909     case PLUS:
1910       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1911       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1912       break;
1913     default:
1914       gcc_unreachable ();
1915     }
1916
1917   span = targetm.dwarf_register_span (src);
1918
1919   /* ??? We'd like to use queue_reg_save, but we need to come up with
1920      a different flushing heuristic for epilogues.  */
1921   if (!span)
1922     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1923   else
1924     {
1925       /* We have a PARALLEL describing where the contents of SRC live.
1926          Queue register saves for each piece of the PARALLEL.  */
1927       int par_index;
1928       int limit;
1929       HOST_WIDE_INT span_offset = offset;
1930
1931       gcc_assert (GET_CODE (span) == PARALLEL);
1932
1933       limit = XVECLEN (span, 0);
1934       for (par_index = 0; par_index < limit; par_index++)
1935         {
1936           rtx elem = XVECEXP (span, 0, par_index);
1937
1938           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1939                     INVALID_REGNUM, span_offset);
1940           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1941         }
1942     }
1943 }
1944
1945 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1946
1947 static void
1948 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1949 {
1950   rtx src, dest;
1951   unsigned sregno, dregno;
1952
1953   src = XEXP (set, 1);
1954   dest = XEXP (set, 0);
1955
1956   if (src == pc_rtx)
1957     sregno = DWARF_FRAME_RETURN_COLUMN;
1958   else
1959     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1960
1961   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1962
1963   /* ??? We'd like to use queue_reg_save, but we need to come up with
1964      a different flushing heuristic for epilogues.  */
1965   reg_save (label, sregno, dregno, 0);
1966 }
1967
1968 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1969
1970 static void
1971 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1972 {
1973   dw_cfi_ref cfi = new_cfi ();
1974   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1975
1976   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1977   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1978
1979   add_fde_cfi (label, cfi);
1980 }
1981
1982 /* Record call frame debugging information for an expression EXPR,
1983    which either sets SP or FP (adjusting how we calculate the frame
1984    address) or saves a register to the stack or another register.
1985    LABEL indicates the address of EXPR.
1986
1987    This function encodes a state machine mapping rtxes to actions on
1988    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1989    users need not read the source code.
1990
1991   The High-Level Picture
1992
1993   Changes in the register we use to calculate the CFA: Currently we
1994   assume that if you copy the CFA register into another register, we
1995   should take the other one as the new CFA register; this seems to
1996   work pretty well.  If it's wrong for some target, it's simple
1997   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1998
1999   Changes in the register we use for saving registers to the stack:
2000   This is usually SP, but not always.  Again, we deduce that if you
2001   copy SP into another register (and SP is not the CFA register),
2002   then the new register is the one we will be using for register
2003   saves.  This also seems to work.
2004
2005   Register saves: There's not much guesswork about this one; if
2006   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2007   register save, and the register used to calculate the destination
2008   had better be the one we think we're using for this purpose.
2009   It's also assumed that a copy from a call-saved register to another
2010   register is saving that register if RTX_FRAME_RELATED_P is set on
2011   that instruction.  If the copy is from a call-saved register to
2012   the *same* register, that means that the register is now the same
2013   value as in the caller.
2014
2015   Except: If the register being saved is the CFA register, and the
2016   offset is nonzero, we are saving the CFA, so we assume we have to
2017   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2018   the intent is to save the value of SP from the previous frame.
2019
2020   In addition, if a register has previously been saved to a different
2021   register,
2022
2023   Invariants / Summaries of Rules
2024
2025   cfa          current rule for calculating the CFA.  It usually
2026                consists of a register and an offset.
2027   cfa_store    register used by prologue code to save things to the stack
2028                cfa_store.offset is the offset from the value of
2029                cfa_store.reg to the actual CFA
2030   cfa_temp     register holding an integral value.  cfa_temp.offset
2031                stores the value, which will be used to adjust the
2032                stack pointer.  cfa_temp is also used like cfa_store,
2033                to track stores to the stack via fp or a temp reg.
2034
2035   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2036                with cfa.reg as the first operand changes the cfa.reg and its
2037                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2038                cfa_temp.offset.
2039
2040   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2041                expression yielding a constant.  This sets cfa_temp.reg
2042                and cfa_temp.offset.
2043
2044   Rule 5:      Create a new register cfa_store used to save items to the
2045                stack.
2046
2047   Rules 10-14: Save a register to the stack.  Define offset as the
2048                difference of the original location and cfa_store's
2049                location (or cfa_temp's location if cfa_temp is used).
2050
2051   Rules 16-20: If AND operation happens on sp in prologue, we assume
2052                stack is realigned.  We will use a group of DW_OP_XXX
2053                expressions to represent the location of the stored
2054                register instead of CFA+offset.
2055
2056   The Rules
2057
2058   "{a,b}" indicates a choice of a xor b.
2059   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2060
2061   Rule 1:
2062   (set <reg1> <reg2>:cfa.reg)
2063   effects: cfa.reg = <reg1>
2064            cfa.offset unchanged
2065            cfa_temp.reg = <reg1>
2066            cfa_temp.offset = cfa.offset
2067
2068   Rule 2:
2069   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2070                               {<const_int>,<reg>:cfa_temp.reg}))
2071   effects: cfa.reg = sp if fp used
2072            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2073            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2074              if cfa_store.reg==sp
2075
2076   Rule 3:
2077   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2078   effects: cfa.reg = fp
2079            cfa_offset += +/- <const_int>
2080
2081   Rule 4:
2082   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2083   constraints: <reg1> != fp
2084                <reg1> != sp
2085   effects: cfa.reg = <reg1>
2086            cfa_temp.reg = <reg1>
2087            cfa_temp.offset = cfa.offset
2088
2089   Rule 5:
2090   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2091   constraints: <reg1> != fp
2092                <reg1> != sp
2093   effects: cfa_store.reg = <reg1>
2094            cfa_store.offset = cfa.offset - cfa_temp.offset
2095
2096   Rule 6:
2097   (set <reg> <const_int>)
2098   effects: cfa_temp.reg = <reg>
2099            cfa_temp.offset = <const_int>
2100
2101   Rule 7:
2102   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2103   effects: cfa_temp.reg = <reg1>
2104            cfa_temp.offset |= <const_int>
2105
2106   Rule 8:
2107   (set <reg> (high <exp>))
2108   effects: none
2109
2110   Rule 9:
2111   (set <reg> (lo_sum <exp> <const_int>))
2112   effects: cfa_temp.reg = <reg>
2113            cfa_temp.offset = <const_int>
2114
2115   Rule 10:
2116   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2117   effects: cfa_store.offset -= <const_int>
2118            cfa.offset = cfa_store.offset if cfa.reg == sp
2119            cfa.reg = sp
2120            cfa.base_offset = -cfa_store.offset
2121
2122   Rule 11:
2123   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2124   effects: cfa_store.offset += -/+ mode_size(mem)
2125            cfa.offset = cfa_store.offset if cfa.reg == sp
2126            cfa.reg = sp
2127            cfa.base_offset = -cfa_store.offset
2128
2129   Rule 12:
2130   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2131
2132        <reg2>)
2133   effects: cfa.reg = <reg1>
2134            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2135
2136   Rule 13:
2137   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2138   effects: cfa.reg = <reg1>
2139            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2140
2141   Rule 14:
2142   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2143   effects: cfa.reg = <reg1>
2144            cfa.base_offset = -cfa_temp.offset
2145            cfa_temp.offset -= mode_size(mem)
2146
2147   Rule 15:
2148   (set <reg> {unspec, unspec_volatile})
2149   effects: target-dependent
2150
2151   Rule 16:
2152   (set sp (and: sp <const_int>))
2153   constraints: cfa_store.reg == sp
2154   effects: current_fde.stack_realign = 1
2155            cfa_store.offset = 0
2156            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2157
2158   Rule 17:
2159   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2160   effects: cfa_store.offset += -/+ mode_size(mem)
2161
2162   Rule 18:
2163   (set (mem ({pre_inc, pre_dec} sp)) fp)
2164   constraints: fde->stack_realign == 1
2165   effects: cfa_store.offset = 0
2166            cfa.reg != HARD_FRAME_POINTER_REGNUM
2167
2168   Rule 19:
2169   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2170   constraints: fde->stack_realign == 1
2171                && cfa.offset == 0
2172                && cfa.indirect == 0
2173                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2174   effects: Use DW_CFA_def_cfa_expression to define cfa
2175            cfa.reg == fde->drap_reg  */
2176
2177 static void
2178 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2179 {
2180   rtx src, dest, span;
2181   HOST_WIDE_INT offset;
2182   dw_fde_ref fde;
2183
2184   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2185      the PARALLEL independently. The first element is always processed if
2186      it is a SET. This is for backward compatibility.   Other elements
2187      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2188      flag is set in them.  */
2189   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2190     {
2191       int par_index;
2192       int limit = XVECLEN (expr, 0);
2193       rtx elem;
2194
2195       /* PARALLELs have strict read-modify-write semantics, so we
2196          ought to evaluate every rvalue before changing any lvalue.
2197          It's cumbersome to do that in general, but there's an
2198          easy approximation that is enough for all current users:
2199          handle register saves before register assignments.  */
2200       if (GET_CODE (expr) == PARALLEL)
2201         for (par_index = 0; par_index < limit; par_index++)
2202           {
2203             elem = XVECEXP (expr, 0, par_index);
2204             if (GET_CODE (elem) == SET
2205                 && MEM_P (SET_DEST (elem))
2206                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2207               dwarf2out_frame_debug_expr (elem, label);
2208           }
2209
2210       for (par_index = 0; par_index < limit; par_index++)
2211         {
2212           elem = XVECEXP (expr, 0, par_index);
2213           if (GET_CODE (elem) == SET
2214               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2215               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2216             dwarf2out_frame_debug_expr (elem, label);
2217           else if (GET_CODE (elem) == SET
2218                    && par_index != 0
2219                    && !RTX_FRAME_RELATED_P (elem))
2220             {
2221               /* Stack adjustment combining might combine some post-prologue
2222                  stack adjustment into a prologue stack adjustment.  */
2223               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2224
2225               if (offset != 0)
2226                 dwarf2out_stack_adjust (offset, label);
2227             }
2228         }
2229       return;
2230     }
2231
2232   gcc_assert (GET_CODE (expr) == SET);
2233
2234   src = SET_SRC (expr);
2235   dest = SET_DEST (expr);
2236
2237   if (REG_P (src))
2238     {
2239       rtx rsi = reg_saved_in (src);
2240       if (rsi)
2241         src = rsi;
2242     }
2243
2244   fde = current_fde ();
2245
2246   switch (GET_CODE (dest))
2247     {
2248     case REG:
2249       switch (GET_CODE (src))
2250         {
2251           /* Setting FP from SP.  */
2252         case REG:
2253           if (cfa.reg == (unsigned) REGNO (src))
2254             {
2255               /* Rule 1 */
2256               /* Update the CFA rule wrt SP or FP.  Make sure src is
2257                  relative to the current CFA register.
2258
2259                  We used to require that dest be either SP or FP, but the
2260                  ARM copies SP to a temporary register, and from there to
2261                  FP.  So we just rely on the backends to only set
2262                  RTX_FRAME_RELATED_P on appropriate insns.  */
2263               cfa.reg = REGNO (dest);
2264               cfa_temp.reg = cfa.reg;
2265               cfa_temp.offset = cfa.offset;
2266             }
2267           else
2268             {
2269               /* Saving a register in a register.  */
2270               gcc_assert (!fixed_regs [REGNO (dest)]
2271                           /* For the SPARC and its register window.  */
2272                           || (DWARF_FRAME_REGNUM (REGNO (src))
2273                               == DWARF_FRAME_RETURN_COLUMN));
2274
2275               /* After stack is aligned, we can only save SP in FP
2276                  if drap register is used.  In this case, we have
2277                  to restore stack pointer with the CFA value and we
2278                  don't generate this DWARF information.  */
2279               if (fde
2280                   && fde->stack_realign
2281                   && REGNO (src) == STACK_POINTER_REGNUM)
2282                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2283                             && fde->drap_reg != INVALID_REGNUM
2284                             && cfa.reg != REGNO (src));
2285               else
2286                 queue_reg_save (label, src, dest, 0);
2287             }
2288           break;
2289
2290         case PLUS:
2291         case MINUS:
2292         case LO_SUM:
2293           if (dest == stack_pointer_rtx)
2294             {
2295               /* Rule 2 */
2296               /* Adjusting SP.  */
2297               switch (GET_CODE (XEXP (src, 1)))
2298                 {
2299                 case CONST_INT:
2300                   offset = INTVAL (XEXP (src, 1));
2301                   break;
2302                 case REG:
2303                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2304                               == cfa_temp.reg);
2305                   offset = cfa_temp.offset;
2306                   break;
2307                 default:
2308                   gcc_unreachable ();
2309                 }
2310
2311               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2312                 {
2313                   /* Restoring SP from FP in the epilogue.  */
2314                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2315                   cfa.reg = STACK_POINTER_REGNUM;
2316                 }
2317               else if (GET_CODE (src) == LO_SUM)
2318                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2319                 ;
2320               else
2321                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2322
2323               if (GET_CODE (src) != MINUS)
2324                 offset = -offset;
2325               if (cfa.reg == STACK_POINTER_REGNUM)
2326                 cfa.offset += offset;
2327               if (cfa_store.reg == STACK_POINTER_REGNUM)
2328                 cfa_store.offset += offset;
2329             }
2330           else if (dest == hard_frame_pointer_rtx)
2331             {
2332               /* Rule 3 */
2333               /* Either setting the FP from an offset of the SP,
2334                  or adjusting the FP */
2335               gcc_assert (frame_pointer_needed);
2336
2337               gcc_assert (REG_P (XEXP (src, 0))
2338                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2339                           && CONST_INT_P (XEXP (src, 1)));
2340               offset = INTVAL (XEXP (src, 1));
2341               if (GET_CODE (src) != MINUS)
2342                 offset = -offset;
2343               cfa.offset += offset;
2344               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2345             }
2346           else
2347             {
2348               gcc_assert (GET_CODE (src) != MINUS);
2349
2350               /* Rule 4 */
2351               if (REG_P (XEXP (src, 0))
2352                   && REGNO (XEXP (src, 0)) == cfa.reg
2353                   && CONST_INT_P (XEXP (src, 1)))
2354                 {
2355                   /* Setting a temporary CFA register that will be copied
2356                      into the FP later on.  */
2357                   offset = - INTVAL (XEXP (src, 1));
2358                   cfa.offset += offset;
2359                   cfa.reg = REGNO (dest);
2360                   /* Or used to save regs to the stack.  */
2361                   cfa_temp.reg = cfa.reg;
2362                   cfa_temp.offset = cfa.offset;
2363                 }
2364
2365               /* Rule 5 */
2366               else if (REG_P (XEXP (src, 0))
2367                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2368                        && XEXP (src, 1) == stack_pointer_rtx)
2369                 {
2370                   /* Setting a scratch register that we will use instead
2371                      of SP for saving registers to the stack.  */
2372                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2373                   cfa_store.reg = REGNO (dest);
2374                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2375                 }
2376
2377               /* Rule 9 */
2378               else if (GET_CODE (src) == LO_SUM
2379                        && CONST_INT_P (XEXP (src, 1)))
2380                 {
2381                   cfa_temp.reg = REGNO (dest);
2382                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2383                 }
2384               else
2385                 gcc_unreachable ();
2386             }
2387           break;
2388
2389           /* Rule 6 */
2390         case CONST_INT:
2391           cfa_temp.reg = REGNO (dest);
2392           cfa_temp.offset = INTVAL (src);
2393           break;
2394
2395           /* Rule 7 */
2396         case IOR:
2397           gcc_assert (REG_P (XEXP (src, 0))
2398                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2399                       && CONST_INT_P (XEXP (src, 1)));
2400
2401           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2402             cfa_temp.reg = REGNO (dest);
2403           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2404           break;
2405
2406           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2407              which will fill in all of the bits.  */
2408           /* Rule 8 */
2409         case HIGH:
2410           break;
2411
2412           /* Rule 15 */
2413         case UNSPEC:
2414         case UNSPEC_VOLATILE:
2415           gcc_assert (targetm.dwarf_handle_frame_unspec);
2416           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2417           return;
2418
2419           /* Rule 16 */
2420         case AND:
2421           /* If this AND operation happens on stack pointer in prologue,
2422              we assume the stack is realigned and we extract the
2423              alignment.  */
2424           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2425             {
2426               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2427               fde->stack_realign = 1;
2428               fde->stack_realignment = INTVAL (XEXP (src, 1));
2429               cfa_store.offset = 0;
2430
2431               if (cfa.reg != STACK_POINTER_REGNUM
2432                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2433                 fde->drap_reg = cfa.reg;
2434             }
2435           return;
2436
2437         default:
2438           gcc_unreachable ();
2439         }
2440
2441       def_cfa_1 (label, &cfa);
2442       break;
2443
2444     case MEM:
2445
2446       /* Saving a register to the stack.  Make sure dest is relative to the
2447          CFA register.  */
2448       switch (GET_CODE (XEXP (dest, 0)))
2449         {
2450           /* Rule 10 */
2451           /* With a push.  */
2452         case PRE_MODIFY:
2453           /* We can't handle variable size modifications.  */
2454           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2455                       == CONST_INT);
2456           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2457
2458           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2459                       && cfa_store.reg == STACK_POINTER_REGNUM);
2460
2461           cfa_store.offset += offset;
2462           if (cfa.reg == STACK_POINTER_REGNUM)
2463             cfa.offset = cfa_store.offset;
2464
2465           offset = -cfa_store.offset;
2466           break;
2467
2468           /* Rule 11 */
2469         case PRE_INC:
2470         case PRE_DEC:
2471           offset = GET_MODE_SIZE (GET_MODE (dest));
2472           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2473             offset = -offset;
2474
2475           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2476                        == STACK_POINTER_REGNUM)
2477                       && cfa_store.reg == STACK_POINTER_REGNUM);
2478
2479           cfa_store.offset += offset;
2480
2481           /* Rule 18: If stack is aligned, we will use FP as a
2482              reference to represent the address of the stored
2483              regiser.  */
2484           if (fde
2485               && fde->stack_realign
2486               && src == hard_frame_pointer_rtx)
2487             {
2488               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2489               cfa_store.offset = 0;
2490             }
2491
2492           if (cfa.reg == STACK_POINTER_REGNUM)
2493             cfa.offset = cfa_store.offset;
2494
2495           offset = -cfa_store.offset;
2496           break;
2497
2498           /* Rule 12 */
2499           /* With an offset.  */
2500         case PLUS:
2501         case MINUS:
2502         case LO_SUM:
2503           {
2504             int regno;
2505
2506             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2507                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2508             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2509             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2510               offset = -offset;
2511
2512             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2513
2514             if (cfa_store.reg == (unsigned) regno)
2515               offset -= cfa_store.offset;
2516             else
2517               {
2518                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2519                 offset -= cfa_temp.offset;
2520               }
2521           }
2522           break;
2523
2524           /* Rule 13 */
2525           /* Without an offset.  */
2526         case REG:
2527           {
2528             int regno = REGNO (XEXP (dest, 0));
2529
2530             if (cfa_store.reg == (unsigned) regno)
2531               offset = -cfa_store.offset;
2532             else
2533               {
2534                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2535                 offset = -cfa_temp.offset;
2536               }
2537           }
2538           break;
2539
2540           /* Rule 14 */
2541         case POST_INC:
2542           gcc_assert (cfa_temp.reg
2543                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2544           offset = -cfa_temp.offset;
2545           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2546           break;
2547
2548         default:
2549           gcc_unreachable ();
2550         }
2551
2552         /* Rule 17 */
2553         /* If the source operand of this MEM operation is not a
2554            register, basically the source is return address.  Here
2555            we only care how much stack grew and we don't save it.  */
2556       if (!REG_P (src))
2557         break;
2558
2559       if (REGNO (src) != STACK_POINTER_REGNUM
2560           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2561           && (unsigned) REGNO (src) == cfa.reg)
2562         {
2563           /* We're storing the current CFA reg into the stack.  */
2564
2565           if (cfa.offset == 0)
2566             {
2567               /* Rule 19 */
2568               /* If stack is aligned, putting CFA reg into stack means
2569                  we can no longer use reg + offset to represent CFA.
2570                  Here we use DW_CFA_def_cfa_expression instead.  The
2571                  result of this expression equals to the original CFA
2572                  value.  */
2573               if (fde
2574                   && fde->stack_realign
2575                   && cfa.indirect == 0
2576                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2577                 {
2578                   dw_cfa_location cfa_exp;
2579
2580                   gcc_assert (fde->drap_reg == cfa.reg);
2581
2582                   cfa_exp.indirect = 1;
2583                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2584                   cfa_exp.base_offset = offset;
2585                   cfa_exp.offset = 0;
2586
2587                   fde->drap_reg_saved = 1;
2588
2589                   def_cfa_1 (label, &cfa_exp);
2590                   break;
2591                 }
2592
2593               /* If the source register is exactly the CFA, assume
2594                  we're saving SP like any other register; this happens
2595                  on the ARM.  */
2596               def_cfa_1 (label, &cfa);
2597               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2598               break;
2599             }
2600           else
2601             {
2602               /* Otherwise, we'll need to look in the stack to
2603                  calculate the CFA.  */
2604               rtx x = XEXP (dest, 0);
2605
2606               if (!REG_P (x))
2607                 x = XEXP (x, 0);
2608               gcc_assert (REG_P (x));
2609
2610               cfa.reg = REGNO (x);
2611               cfa.base_offset = offset;
2612               cfa.indirect = 1;
2613               def_cfa_1 (label, &cfa);
2614               break;
2615             }
2616         }
2617
2618       def_cfa_1 (label, &cfa);
2619       {
2620         span = targetm.dwarf_register_span (src);
2621
2622         if (!span)
2623           queue_reg_save (label, src, NULL_RTX, offset);
2624         else
2625           {
2626             /* We have a PARALLEL describing where the contents of SRC
2627                live.  Queue register saves for each piece of the
2628                PARALLEL.  */
2629             int par_index;
2630             int limit;
2631             HOST_WIDE_INT span_offset = offset;
2632
2633             gcc_assert (GET_CODE (span) == PARALLEL);
2634
2635             limit = XVECLEN (span, 0);
2636             for (par_index = 0; par_index < limit; par_index++)
2637               {
2638                 rtx elem = XVECEXP (span, 0, par_index);
2639
2640                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2641                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2642               }
2643           }
2644       }
2645       break;
2646
2647     default:
2648       gcc_unreachable ();
2649     }
2650 }
2651
2652 /* Record call frame debugging information for INSN, which either
2653    sets SP or FP (adjusting how we calculate the frame address) or saves a
2654    register to the stack.  If INSN is NULL_RTX, initialize our state.
2655
2656    If AFTER_P is false, we're being called before the insn is emitted,
2657    otherwise after.  Call instructions get invoked twice.  */
2658
2659 void
2660 dwarf2out_frame_debug (rtx insn, bool after_p)
2661 {
2662   const char *label;
2663   rtx note, n;
2664   bool handled_one = false;
2665
2666   if (insn == NULL_RTX)
2667     {
2668       size_t i;
2669
2670       /* Flush any queued register saves.  */
2671       flush_queued_reg_saves ();
2672
2673       /* Set up state for generating call frame debug info.  */
2674       lookup_cfa (&cfa);
2675       gcc_assert (cfa.reg
2676                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2677
2678       cfa.reg = STACK_POINTER_REGNUM;
2679       cfa_store = cfa;
2680       cfa_temp.reg = -1;
2681       cfa_temp.offset = 0;
2682
2683       for (i = 0; i < num_regs_saved_in_regs; i++)
2684         {
2685           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2686           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2687         }
2688       num_regs_saved_in_regs = 0;
2689
2690       if (barrier_args_size)
2691         {
2692           XDELETEVEC (barrier_args_size);
2693           barrier_args_size = NULL;
2694         }
2695       return;
2696     }
2697
2698   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2699     flush_queued_reg_saves ();
2700
2701   if (!RTX_FRAME_RELATED_P (insn))
2702     {
2703       /* ??? This should be done unconditionally since stack adjustments
2704          matter if the stack pointer is not the CFA register anymore but
2705          is still used to save registers.  */
2706       if (!ACCUMULATE_OUTGOING_ARGS)
2707         dwarf2out_notice_stack_adjust (insn, after_p);
2708       return;
2709     }
2710
2711   label = dwarf2out_cfi_label (false);
2712
2713   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2714     switch (REG_NOTE_KIND (note))
2715       {
2716       case REG_FRAME_RELATED_EXPR:
2717         insn = XEXP (note, 0);
2718         goto found;
2719
2720       case REG_CFA_DEF_CFA:
2721         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2722         handled_one = true;
2723         break;
2724
2725       case REG_CFA_ADJUST_CFA:
2726         n = XEXP (note, 0);
2727         if (n == NULL)
2728           {
2729             n = PATTERN (insn);
2730             if (GET_CODE (n) == PARALLEL)
2731               n = XVECEXP (n, 0, 0);
2732           }
2733         dwarf2out_frame_debug_adjust_cfa (n, label);
2734         handled_one = true;
2735         break;
2736
2737       case REG_CFA_OFFSET:
2738         n = XEXP (note, 0);
2739         if (n == NULL)
2740           n = single_set (insn);
2741         dwarf2out_frame_debug_cfa_offset (n, label);
2742         handled_one = true;
2743         break;
2744
2745       case REG_CFA_REGISTER:
2746         n = XEXP (note, 0);
2747         if (n == NULL)
2748           {
2749             n = PATTERN (insn);
2750             if (GET_CODE (n) == PARALLEL)
2751               n = XVECEXP (n, 0, 0);
2752           }
2753         dwarf2out_frame_debug_cfa_register (n, label);
2754         handled_one = true;
2755         break;
2756
2757       case REG_CFA_RESTORE:
2758         n = XEXP (note, 0);
2759         if (n == NULL)
2760           {
2761             n = PATTERN (insn);
2762             if (GET_CODE (n) == PARALLEL)
2763               n = XVECEXP (n, 0, 0);
2764             n = XEXP (n, 0);
2765           }
2766         dwarf2out_frame_debug_cfa_restore (n, label);
2767         handled_one = true;
2768         break;
2769
2770       case REG_CFA_SET_VDRAP:
2771         n = XEXP (note, 0);
2772         if (REG_P (n))
2773           {
2774             dw_fde_ref fde = current_fde ();
2775             if (fde)
2776               {
2777                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2778                 if (REG_P (n))
2779                   fde->vdrap_reg = REGNO (n);
2780               }
2781           }
2782         handled_one = true;
2783         break;
2784
2785       default:
2786         break;
2787       }
2788   if (handled_one)
2789     return;
2790
2791   insn = PATTERN (insn);
2792  found:
2793   dwarf2out_frame_debug_expr (insn, label);
2794 }
2795
2796 /* Determine if we need to save and restore CFI information around this
2797    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2798    we do need to save/restore, then emit the save now, and insert a
2799    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2800
2801 void
2802 dwarf2out_cfi_begin_epilogue (rtx insn)
2803 {
2804   bool saw_frp = false;
2805   rtx i;
2806
2807   /* Scan forward to the return insn, noticing if there are possible
2808      frame related insns.  */
2809   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2810     {
2811       if (!INSN_P (i))
2812         continue;
2813
2814       /* Look for both regular and sibcalls to end the block.  */
2815       if (returnjump_p (i))
2816         break;
2817       if (CALL_P (i) && SIBLING_CALL_P (i))
2818         break;
2819
2820       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2821         {
2822           int idx;
2823           rtx seq = PATTERN (i);
2824
2825           if (returnjump_p (XVECEXP (seq, 0, 0)))
2826             break;
2827           if (CALL_P (XVECEXP (seq, 0, 0))
2828               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2829             break;
2830
2831           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2832             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2833               saw_frp = true;
2834         }
2835
2836       if (RTX_FRAME_RELATED_P (i))
2837         saw_frp = true;
2838     }
2839
2840   /* If the port doesn't emit epilogue unwind info, we don't need a
2841      save/restore pair.  */
2842   if (!saw_frp)
2843     return;
2844
2845   /* Otherwise, search forward to see if the return insn was the last
2846      basic block of the function.  If so, we don't need save/restore.  */
2847   gcc_assert (i != NULL);
2848   i = next_real_insn (i);
2849   if (i == NULL)
2850     return;
2851
2852   /* Insert the restore before that next real insn in the stream, and before
2853      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2854      properly nested.  This should be after any label or alignment.  This
2855      will be pushed into the CFI stream by the function below.  */
2856   while (1)
2857     {
2858       rtx p = PREV_INSN (i);
2859       if (!NOTE_P (p))
2860         break;
2861       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2862         break;
2863       i = p;
2864     }
2865   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2866
2867   emit_cfa_remember = true;
2868
2869   /* And emulate the state save.  */
2870   gcc_assert (!cfa_remember.in_use);
2871   cfa_remember = cfa;
2872   cfa_remember.in_use = 1;
2873 }
2874
2875 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2876    required.  */
2877
2878 void
2879 dwarf2out_frame_debug_restore_state (void)
2880 {
2881   dw_cfi_ref cfi = new_cfi ();
2882   const char *label = dwarf2out_cfi_label (false);
2883
2884   cfi->dw_cfi_opc = DW_CFA_restore_state;
2885   add_fde_cfi (label, cfi);
2886
2887   gcc_assert (cfa_remember.in_use);
2888   cfa = cfa_remember;
2889   cfa_remember.in_use = 0;
2890 }
2891
2892 #endif
2893
2894 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2895 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2896  (enum dwarf_call_frame_info cfi);
2897
2898 static enum dw_cfi_oprnd_type
2899 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2900 {
2901   switch (cfi)
2902     {
2903     case DW_CFA_nop:
2904     case DW_CFA_GNU_window_save:
2905     case DW_CFA_remember_state:
2906     case DW_CFA_restore_state:
2907       return dw_cfi_oprnd_unused;
2908
2909     case DW_CFA_set_loc:
2910     case DW_CFA_advance_loc1:
2911     case DW_CFA_advance_loc2:
2912     case DW_CFA_advance_loc4:
2913     case DW_CFA_MIPS_advance_loc8:
2914       return dw_cfi_oprnd_addr;
2915
2916     case DW_CFA_offset:
2917     case DW_CFA_offset_extended:
2918     case DW_CFA_def_cfa:
2919     case DW_CFA_offset_extended_sf:
2920     case DW_CFA_def_cfa_sf:
2921     case DW_CFA_restore:
2922     case DW_CFA_restore_extended:
2923     case DW_CFA_undefined:
2924     case DW_CFA_same_value:
2925     case DW_CFA_def_cfa_register:
2926     case DW_CFA_register:
2927     case DW_CFA_expression:
2928       return dw_cfi_oprnd_reg_num;
2929
2930     case DW_CFA_def_cfa_offset:
2931     case DW_CFA_GNU_args_size:
2932     case DW_CFA_def_cfa_offset_sf:
2933       return dw_cfi_oprnd_offset;
2934
2935     case DW_CFA_def_cfa_expression:
2936       return dw_cfi_oprnd_loc;
2937
2938     default:
2939       gcc_unreachable ();
2940     }
2941 }
2942
2943 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2944 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2945  (enum dwarf_call_frame_info cfi);
2946
2947 static enum dw_cfi_oprnd_type
2948 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2949 {
2950   switch (cfi)
2951     {
2952     case DW_CFA_def_cfa:
2953     case DW_CFA_def_cfa_sf:
2954     case DW_CFA_offset:
2955     case DW_CFA_offset_extended_sf:
2956     case DW_CFA_offset_extended:
2957       return dw_cfi_oprnd_offset;
2958
2959     case DW_CFA_register:
2960       return dw_cfi_oprnd_reg_num;
2961
2962     case DW_CFA_expression:
2963       return dw_cfi_oprnd_loc;
2964
2965     default:
2966       return dw_cfi_oprnd_unused;
2967     }
2968 }
2969
2970 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2971
2972 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2973    switch to the data section instead, and write out a synthetic start label
2974    for collect2 the first time around.  */
2975
2976 static void
2977 switch_to_eh_frame_section (bool back)
2978 {
2979   tree label;
2980
2981 #ifdef EH_FRAME_SECTION_NAME
2982   if (eh_frame_section == 0)
2983     {
2984       int flags;
2985
2986       if (EH_TABLES_CAN_BE_READ_ONLY)
2987         {
2988           int fde_encoding;
2989           int per_encoding;
2990           int lsda_encoding;
2991
2992           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2993                                                        /*global=*/0);
2994           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2995                                                        /*global=*/1);
2996           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2997                                                         /*global=*/0);
2998           flags = ((! flag_pic
2999                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3000                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3001                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3002                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3003                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3004                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3005                    ? 0 : SECTION_WRITE);
3006         }
3007       else
3008         flags = SECTION_WRITE;
3009       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3010     }
3011 #endif
3012
3013   if (eh_frame_section)
3014     switch_to_section (eh_frame_section);
3015   else
3016     {
3017       /* We have no special eh_frame section.  Put the information in
3018          the data section and emit special labels to guide collect2.  */
3019       switch_to_section (data_section);
3020
3021       if (!back)
3022         {
3023           label = get_file_function_name ("F");
3024           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3025           targetm.asm_out.globalize_label (asm_out_file,
3026                                            IDENTIFIER_POINTER (label));
3027           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3028         }
3029     }
3030 }
3031
3032 /* Switch [BACK] to the eh or debug frame table section, depending on
3033    FOR_EH.  */
3034
3035 static void
3036 switch_to_frame_table_section (int for_eh, bool back)
3037 {
3038   if (for_eh)
3039     switch_to_eh_frame_section (back);
3040   else
3041     {
3042       if (!debug_frame_section)
3043         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3044                                            SECTION_DEBUG, NULL);
3045       switch_to_section (debug_frame_section);
3046     }
3047 }
3048
3049 /* Output a Call Frame Information opcode and its operand(s).  */
3050
3051 static void
3052 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3053 {
3054   unsigned long r;
3055   HOST_WIDE_INT off;
3056
3057   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3058     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3059                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3060                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3061                          ((unsigned HOST_WIDE_INT)
3062                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3063   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3064     {
3065       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3066       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3067                            "DW_CFA_offset, column %#lx", r);
3068       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3069       dw2_asm_output_data_uleb128 (off, NULL);
3070     }
3071   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3072     {
3073       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3074       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3075                            "DW_CFA_restore, column %#lx", r);
3076     }
3077   else
3078     {
3079       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3080                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3081
3082       switch (cfi->dw_cfi_opc)
3083         {
3084         case DW_CFA_set_loc:
3085           if (for_eh)
3086             dw2_asm_output_encoded_addr_rtx (
3087                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3088                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3089                 false, NULL);
3090           else
3091             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3092                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3093           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3094           break;
3095
3096         case DW_CFA_advance_loc1:
3097           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3098                                 fde->dw_fde_current_label, NULL);
3099           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3100           break;
3101
3102         case DW_CFA_advance_loc2:
3103           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3104                                 fde->dw_fde_current_label, NULL);
3105           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3106           break;
3107
3108         case DW_CFA_advance_loc4:
3109           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3110                                 fde->dw_fde_current_label, NULL);
3111           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3112           break;
3113
3114         case DW_CFA_MIPS_advance_loc8:
3115           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3116                                 fde->dw_fde_current_label, NULL);
3117           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3118           break;
3119
3120         case DW_CFA_offset_extended:
3121           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3122           dw2_asm_output_data_uleb128 (r, NULL);
3123           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3124           dw2_asm_output_data_uleb128 (off, NULL);
3125           break;
3126
3127         case DW_CFA_def_cfa:
3128           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3129           dw2_asm_output_data_uleb128 (r, NULL);
3130           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3131           break;
3132
3133         case DW_CFA_offset_extended_sf:
3134           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3135           dw2_asm_output_data_uleb128 (r, NULL);
3136           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3137           dw2_asm_output_data_sleb128 (off, NULL);
3138           break;
3139
3140         case DW_CFA_def_cfa_sf:
3141           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3142           dw2_asm_output_data_uleb128 (r, NULL);
3143           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3144           dw2_asm_output_data_sleb128 (off, NULL);
3145           break;
3146
3147         case DW_CFA_restore_extended:
3148         case DW_CFA_undefined:
3149         case DW_CFA_same_value:
3150         case DW_CFA_def_cfa_register:
3151           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3152           dw2_asm_output_data_uleb128 (r, NULL);
3153           break;
3154
3155         case DW_CFA_register:
3156           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3157           dw2_asm_output_data_uleb128 (r, NULL);
3158           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3159           dw2_asm_output_data_uleb128 (r, NULL);
3160           break;
3161
3162         case DW_CFA_def_cfa_offset:
3163         case DW_CFA_GNU_args_size:
3164           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3165           break;
3166
3167         case DW_CFA_def_cfa_offset_sf:
3168           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3169           dw2_asm_output_data_sleb128 (off, NULL);
3170           break;
3171
3172         case DW_CFA_GNU_window_save:
3173           break;
3174
3175         case DW_CFA_def_cfa_expression:
3176         case DW_CFA_expression:
3177           output_cfa_loc (cfi);
3178           break;
3179
3180         case DW_CFA_GNU_negative_offset_extended:
3181           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3182           gcc_unreachable ();
3183
3184         default:
3185           break;
3186         }
3187     }
3188 }
3189
3190 /* Similar, but do it via assembler directives instead.  */
3191
3192 static void
3193 output_cfi_directive (dw_cfi_ref cfi)
3194 {
3195   unsigned long r, r2;
3196
3197   switch (cfi->dw_cfi_opc)
3198     {
3199     case DW_CFA_advance_loc:
3200     case DW_CFA_advance_loc1:
3201     case DW_CFA_advance_loc2:
3202     case DW_CFA_advance_loc4:
3203     case DW_CFA_MIPS_advance_loc8:
3204     case DW_CFA_set_loc:
3205       /* Should only be created by add_fde_cfi in a code path not
3206          followed when emitting via directives.  The assembler is
3207          going to take care of this for us.  */
3208       gcc_unreachable ();
3209
3210     case DW_CFA_offset:
3211     case DW_CFA_offset_extended:
3212     case DW_CFA_offset_extended_sf:
3213       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3214       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3215                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3216       break;
3217
3218     case DW_CFA_restore:
3219     case DW_CFA_restore_extended:
3220       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3221       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3222       break;
3223
3224     case DW_CFA_undefined:
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3226       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3227       break;
3228
3229     case DW_CFA_same_value:
3230       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3231       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3232       break;
3233
3234     case DW_CFA_def_cfa:
3235     case DW_CFA_def_cfa_sf:
3236       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3237       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3238                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3239       break;
3240
3241     case DW_CFA_def_cfa_register:
3242       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3243       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3244       break;
3245
3246     case DW_CFA_register:
3247       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3248       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3249       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3250       break;
3251
3252     case DW_CFA_def_cfa_offset:
3253     case DW_CFA_def_cfa_offset_sf:
3254       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3255                HOST_WIDE_INT_PRINT_DEC"\n",
3256                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3257       break;
3258
3259     case DW_CFA_remember_state:
3260       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3261       break;
3262     case DW_CFA_restore_state:
3263       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3264       break;
3265
3266     case DW_CFA_GNU_args_size:
3267       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3268       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3269       if (flag_debug_asm)
3270         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3271                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3272       fputc ('\n', asm_out_file);
3273       break;
3274
3275     case DW_CFA_GNU_window_save:
3276       fprintf (asm_out_file, "\t.cfi_window_save\n");
3277       break;
3278
3279     case DW_CFA_def_cfa_expression:
3280     case DW_CFA_expression:
3281       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3282       output_cfa_loc_raw (cfi);
3283       fputc ('\n', asm_out_file);
3284       break;
3285
3286     default:
3287       gcc_unreachable ();
3288     }
3289 }
3290
3291 DEF_VEC_P (dw_cfi_ref);
3292 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3293
3294 /* Output CFIs to bring current FDE to the same state as after executing
3295    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3296    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3297    other arguments to pass to output_cfi.  */
3298
3299 static void
3300 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3301 {
3302   struct dw_cfi_struct cfi_buf;
3303   dw_cfi_ref cfi2;
3304   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3305   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3306   unsigned int len, idx;
3307
3308   for (;; cfi = cfi->dw_cfi_next)
3309     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3310       {
3311       case DW_CFA_advance_loc:
3312       case DW_CFA_advance_loc1:
3313       case DW_CFA_advance_loc2:
3314       case DW_CFA_advance_loc4:
3315       case DW_CFA_MIPS_advance_loc8:
3316       case DW_CFA_set_loc:
3317         /* All advances should be ignored.  */
3318         break;
3319       case DW_CFA_remember_state:
3320         {
3321           dw_cfi_ref args_size = cfi_args_size;
3322
3323           /* Skip everything between .cfi_remember_state and
3324              .cfi_restore_state.  */
3325           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3326             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3327               break;
3328             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3329               args_size = cfi2;
3330             else
3331               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3332
3333           if (cfi2 == NULL)
3334             goto flush_all;
3335           else
3336             {
3337               cfi = cfi2;
3338               cfi_args_size = args_size;
3339             }
3340           break;
3341         }
3342       case DW_CFA_GNU_args_size:
3343         cfi_args_size = cfi;
3344         break;
3345       case DW_CFA_GNU_window_save:
3346         goto flush_all;
3347       case DW_CFA_offset:
3348       case DW_CFA_offset_extended:
3349       case DW_CFA_offset_extended_sf:
3350       case DW_CFA_restore:
3351       case DW_CFA_restore_extended:
3352       case DW_CFA_undefined:
3353       case DW_CFA_same_value:
3354       case DW_CFA_register:
3355       case DW_CFA_val_offset:
3356       case DW_CFA_val_offset_sf:
3357       case DW_CFA_expression:
3358       case DW_CFA_val_expression:
3359       case DW_CFA_GNU_negative_offset_extended:
3360         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3361           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3362                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3363         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3364         break;
3365       case DW_CFA_def_cfa:
3366       case DW_CFA_def_cfa_sf:
3367       case DW_CFA_def_cfa_expression:
3368         cfi_cfa = cfi;
3369         cfi_cfa_offset = cfi;
3370         break;
3371       case DW_CFA_def_cfa_register:
3372         cfi_cfa = cfi;
3373         break;
3374       case DW_CFA_def_cfa_offset:
3375       case DW_CFA_def_cfa_offset_sf:
3376         cfi_cfa_offset = cfi;
3377         break;
3378       case DW_CFA_nop:
3379         gcc_assert (cfi == NULL);
3380       flush_all:
3381         len = VEC_length (dw_cfi_ref, regs);
3382         for (idx = 0; idx < len; idx++)
3383           {
3384             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3385             if (cfi2 != NULL
3386                 && cfi2->dw_cfi_opc != DW_CFA_restore
3387                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3388               {
3389                 if (do_cfi_asm)
3390                   output_cfi_directive (cfi2);
3391                 else
3392                   output_cfi (cfi2, fde, for_eh);
3393               }
3394           }
3395         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3396           {
3397             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3398             cfi_buf = *cfi_cfa;
3399             switch (cfi_cfa_offset->dw_cfi_opc)
3400               {
3401               case DW_CFA_def_cfa_offset:
3402                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3403                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3404                 break;
3405               case DW_CFA_def_cfa_offset_sf:
3406                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3407                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3408                 break;
3409               case DW_CFA_def_cfa:
3410               case DW_CFA_def_cfa_sf:
3411                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3412                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3413                 break;
3414               default:
3415                 gcc_unreachable ();
3416               }
3417             cfi_cfa = &cfi_buf;
3418           }
3419         else if (cfi_cfa_offset)
3420           cfi_cfa = cfi_cfa_offset;
3421         if (cfi_cfa)
3422           {
3423             if (do_cfi_asm)
3424               output_cfi_directive (cfi_cfa);
3425             else
3426               output_cfi (cfi_cfa, fde, for_eh);
3427           }
3428         cfi_cfa = NULL;
3429         cfi_cfa_offset = NULL;
3430         if (cfi_args_size
3431             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3432           {
3433             if (do_cfi_asm)
3434               output_cfi_directive (cfi_args_size);
3435             else
3436               output_cfi (cfi_args_size, fde, for_eh);
3437           }
3438         cfi_args_size = NULL;
3439         if (cfi == NULL)
3440           {
3441             VEC_free (dw_cfi_ref, heap, regs);
3442             return;
3443           }
3444         else if (do_cfi_asm)
3445           output_cfi_directive (cfi);
3446         else
3447           output_cfi (cfi, fde, for_eh);
3448         break;
3449       default:
3450         gcc_unreachable ();
3451     }
3452 }
3453
3454 /* Output one FDE.  */
3455
3456 static void
3457 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3458             char *section_start_label, int fde_encoding, char *augmentation,
3459             bool any_lsda_needed, int lsda_encoding)
3460 {
3461   const char *begin, *end;
3462   static unsigned int j;
3463   char l1[20], l2[20];
3464   dw_cfi_ref cfi;
3465
3466   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3467                                 /* empty */ 0);
3468   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3469                                   for_eh + j);
3470   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3471   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3472   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3473     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3474                          " indicating 64-bit DWARF extension");
3475   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3476                         "FDE Length");
3477   ASM_OUTPUT_LABEL (asm_out_file, l1);
3478
3479   if (for_eh)
3480     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3481   else
3482     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3483                            debug_frame_section, "FDE CIE offset");
3484
3485   if (!fde->dw_fde_switched_sections)
3486     {
3487       begin = fde->dw_fde_begin;
3488       end = fde->dw_fde_end;
3489     }
3490   else
3491     {
3492       /* For the first section, prefer dw_fde_begin over
3493          dw_fde_{hot,cold}_section_label, as the latter
3494          might be separated from the real start of the
3495          function by alignment padding.  */
3496       if (!second)
3497         begin = fde->dw_fde_begin;
3498       else if (fde->dw_fde_switched_cold_to_hot)
3499         begin = fde->dw_fde_hot_section_label;
3500       else
3501         begin = fde->dw_fde_unlikely_section_label;
3502       if (second ^ fde->dw_fde_switched_cold_to_hot)
3503         end = fde->dw_fde_unlikely_section_end_label;
3504       else
3505         end = fde->dw_fde_hot_section_end_label;
3506     }
3507
3508   if (for_eh)
3509     {
3510       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3511       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3512       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3513                                        "FDE initial location");
3514       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3515                             end, begin, "FDE address range");
3516     }
3517   else
3518     {
3519       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3520       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3521     }
3522
3523   if (augmentation[0])
3524     {
3525       if (any_lsda_needed)
3526         {
3527           int size = size_of_encoded_value (lsda_encoding);
3528
3529           if (lsda_encoding == DW_EH_PE_aligned)
3530             {
3531               int offset = (  4         /* Length */
3532                             + 4         /* CIE offset */
3533                             + 2 * size_of_encoded_value (fde_encoding)
3534                             + 1         /* Augmentation size */ );
3535               int pad = -offset & (PTR_SIZE - 1);
3536
3537               size += pad;
3538               gcc_assert (size_of_uleb128 (size) == 1);
3539             }
3540
3541           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3542
3543           if (fde->uses_eh_lsda)
3544             {
3545               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3546                                            fde->funcdef_number);
3547               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3548                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3549                                                false,
3550                                                "Language Specific Data Area");
3551             }
3552           else
3553             {
3554               if (lsda_encoding == DW_EH_PE_aligned)
3555                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3556               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3557                                    "Language Specific Data Area (none)");
3558             }
3559         }
3560       else
3561         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3562     }
3563
3564   /* Loop through the Call Frame Instructions associated with
3565      this FDE.  */
3566   fde->dw_fde_current_label = begin;
3567   if (!fde->dw_fde_switched_sections)
3568     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3569       output_cfi (cfi, fde, for_eh);
3570   else if (!second)
3571     {
3572       if (fde->dw_fde_switch_cfi)
3573         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3574           {
3575             output_cfi (cfi, fde, for_eh);
3576             if (cfi == fde->dw_fde_switch_cfi)
3577               break;
3578           }
3579     }
3580   else
3581     {
3582       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3583
3584       if (fde->dw_fde_switch_cfi)
3585         {
3586           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3587           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3588           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3589           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3590         }
3591       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3592         output_cfi (cfi, fde, for_eh);
3593     }
3594
3595   /* If we are to emit a ref/link from function bodies to their frame tables,
3596      do it now.  This is typically performed to make sure that tables
3597      associated with functions are dragged with them and not discarded in
3598      garbage collecting links. We need to do this on a per function basis to
3599      cope with -ffunction-sections.  */
3600
3601 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3602   /* Switch to the function section, emit the ref to the tables, and
3603      switch *back* into the table section.  */
3604   switch_to_section (function_section (fde->decl));
3605   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3606   switch_to_frame_table_section (for_eh, true);
3607 #endif
3608
3609   /* Pad the FDE out to an address sized boundary.  */
3610   ASM_OUTPUT_ALIGN (asm_out_file,
3611                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3612   ASM_OUTPUT_LABEL (asm_out_file, l2);
3613
3614   j += 2;
3615 }
3616
3617 /* Return true if frame description entry FDE is needed for EH.  */
3618
3619 static bool
3620 fde_needed_for_eh_p (dw_fde_ref fde)
3621 {
3622   if (flag_asynchronous_unwind_tables)
3623     return true;
3624
3625   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3626     return true;
3627
3628   if (fde->uses_eh_lsda)
3629     return true;
3630
3631   /* If exceptions are enabled, we have collected nothrow info.  */
3632   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3633     return false;
3634
3635   return true;
3636 }
3637
3638 /* Output the call frame information used to record information
3639    that relates to calculating the frame pointer, and records the
3640    location of saved registers.  */
3641
3642 static void
3643 output_call_frame_info (int for_eh)
3644 {
3645   unsigned int i;
3646   dw_fde_ref fde;
3647   dw_cfi_ref cfi;
3648   char l1[20], l2[20], section_start_label[20];
3649   bool any_lsda_needed = false;
3650   char augmentation[6];
3651   int augmentation_size;
3652   int fde_encoding = DW_EH_PE_absptr;
3653   int per_encoding = DW_EH_PE_absptr;
3654   int lsda_encoding = DW_EH_PE_absptr;
3655   int return_reg;
3656   rtx personality = NULL;
3657   int dw_cie_version;
3658
3659   /* Don't emit a CIE if there won't be any FDEs.  */
3660   if (fde_table_in_use == 0)
3661     return;
3662
3663   /* Nothing to do if the assembler's doing it all.  */
3664   if (dwarf2out_do_cfi_asm ())
3665     return;
3666
3667   /* If we don't have any functions we'll want to unwind out of, don't emit
3668      any EH unwind information.  If we make FDEs linkonce, we may have to
3669      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3670      want to avoid having an FDE kept around when the function it refers to
3671      is discarded.  Example where this matters: a primary function template
3672      in C++ requires EH information, an explicit specialization doesn't.  */
3673   if (for_eh)
3674     {
3675       bool any_eh_needed = false;
3676
3677       for (i = 0; i < fde_table_in_use; i++)
3678         if (fde_table[i].uses_eh_lsda)
3679           any_eh_needed = any_lsda_needed = true;
3680         else if (fde_needed_for_eh_p (&fde_table[i]))
3681           any_eh_needed = true;
3682         else if (TARGET_USES_WEAK_UNWIND_INFO)
3683           targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl, 1, 1);
3684
3685       if (!any_eh_needed)
3686         return;
3687     }
3688
3689   /* We're going to be generating comments, so turn on app.  */
3690   if (flag_debug_asm)
3691     app_enable ();
3692
3693   /* Switch to the proper frame section, first time.  */
3694   switch_to_frame_table_section (for_eh, false);
3695
3696   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3697   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3698
3699   /* Output the CIE.  */
3700   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3701   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3702   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3703     dw2_asm_output_data (4, 0xffffffff,
3704       "Initial length escape value indicating 64-bit DWARF extension");
3705   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3706                         "Length of Common Information Entry");
3707   ASM_OUTPUT_LABEL (asm_out_file, l1);
3708
3709   /* Now that the CIE pointer is PC-relative for EH,
3710      use 0 to identify the CIE.  */
3711   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3712                        (for_eh ? 0 : DWARF_CIE_ID),
3713                        "CIE Identifier Tag");
3714
3715   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3716      use CIE version 1, unless that would produce incorrect results
3717      due to overflowing the return register column.  */
3718   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3719   dw_cie_version = 1;
3720   if (return_reg >= 256 || dwarf_version > 2)
3721     dw_cie_version = 3;
3722   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3723
3724   augmentation[0] = 0;
3725   augmentation_size = 0;
3726
3727   personality = current_unit_personality;
3728   if (for_eh)
3729     {
3730       char *p;
3731
3732       /* Augmentation:
3733          z      Indicates that a uleb128 is present to size the
3734                 augmentation section.
3735          L      Indicates the encoding (and thus presence) of
3736                 an LSDA pointer in the FDE augmentation.
3737          R      Indicates a non-default pointer encoding for
3738                 FDE code pointers.
3739          P      Indicates the presence of an encoding + language
3740                 personality routine in the CIE augmentation.  */
3741
3742       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3743       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3744       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3745
3746       p = augmentation + 1;
3747       if (personality)
3748         {
3749           *p++ = 'P';
3750           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3751           assemble_external_libcall (personality);
3752         }
3753       if (any_lsda_needed)
3754         {
3755           *p++ = 'L';
3756           augmentation_size += 1;
3757         }
3758       if (fde_encoding != DW_EH_PE_absptr)
3759         {
3760           *p++ = 'R';
3761           augmentation_size += 1;
3762         }
3763       if (p > augmentation + 1)
3764         {
3765           augmentation[0] = 'z';
3766           *p = '\0';
3767         }
3768
3769       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3770       if (personality && per_encoding == DW_EH_PE_aligned)
3771         {
3772           int offset = (  4             /* Length */
3773                         + 4             /* CIE Id */
3774                         + 1             /* CIE version */
3775                         + strlen (augmentation) + 1     /* Augmentation */
3776                         + size_of_uleb128 (1)           /* Code alignment */
3777                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3778                         + 1             /* RA column */
3779                         + 1             /* Augmentation size */
3780                         + 1             /* Personality encoding */ );
3781           int pad = -offset & (PTR_SIZE - 1);
3782
3783           augmentation_size += pad;
3784
3785           /* Augmentations should be small, so there's scarce need to
3786              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3787           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3788         }
3789     }
3790
3791   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3792   if (dw_cie_version >= 4)
3793     {
3794       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3795       dw2_asm_output_data (1, 0, "CIE Segment Size");
3796     }
3797   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3798   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3799                                "CIE Data Alignment Factor");
3800
3801   if (dw_cie_version == 1)
3802     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3803   else
3804     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3805
3806   if (augmentation[0])
3807     {
3808       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3809       if (personality)
3810         {
3811           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3812                                eh_data_format_name (per_encoding));
3813           dw2_asm_output_encoded_addr_rtx (per_encoding,
3814                                            personality,
3815                                            true, NULL);
3816         }
3817
3818       if (any_lsda_needed)
3819         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3820                              eh_data_format_name (lsda_encoding));
3821
3822       if (fde_encoding != DW_EH_PE_absptr)
3823         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3824                              eh_data_format_name (fde_encoding));
3825     }
3826
3827   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3828     output_cfi (cfi, NULL, for_eh);
3829
3830   /* Pad the CIE out to an address sized boundary.  */
3831   ASM_OUTPUT_ALIGN (asm_out_file,
3832                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3833   ASM_OUTPUT_LABEL (asm_out_file, l2);
3834
3835   /* Loop through all of the FDE's.  */
3836   for (i = 0; i < fde_table_in_use; i++)
3837     {
3838       unsigned int k;
3839       fde = &fde_table[i];
3840
3841       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3842       if (for_eh && !fde_needed_for_eh_p (fde))
3843         continue;
3844
3845       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3846         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3847                     augmentation, any_lsda_needed, lsda_encoding);
3848     }
3849
3850   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3851     dw2_asm_output_data (4, 0, "End of Table");
3852 #ifdef MIPS_DEBUGGING_INFO
3853   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3854      get a value of 0.  Putting .align 0 after the label fixes it.  */
3855   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3856 #endif
3857
3858   /* Turn off app to make assembly quicker.  */
3859   if (flag_debug_asm)
3860     app_disable ();
3861 }
3862
3863 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3864
3865 static void
3866 dwarf2out_do_cfi_startproc (bool second)
3867 {
3868   int enc;
3869   rtx ref;
3870   rtx personality = get_personality_function (current_function_decl);
3871
3872   fprintf (asm_out_file, "\t.cfi_startproc\n");
3873
3874   if (personality)
3875     {
3876       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3877       ref = personality;
3878
3879       /* ??? The GAS support isn't entirely consistent.  We have to
3880          handle indirect support ourselves, but PC-relative is done
3881          in the assembler.  Further, the assembler can't handle any
3882          of the weirder relocation types.  */
3883       if (enc & DW_EH_PE_indirect)
3884         ref = dw2_force_const_mem (ref, true);
3885
3886       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3887       output_addr_const (asm_out_file, ref);
3888       fputc ('\n', asm_out_file);
3889     }
3890
3891   if (crtl->uses_eh_lsda)
3892     {
3893       char lab[20];
3894
3895       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3896       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3897                                    current_function_funcdef_no);
3898       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3899       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3900
3901       if (enc & DW_EH_PE_indirect)
3902         ref = dw2_force_const_mem (ref, true);
3903
3904       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3905       output_addr_const (asm_out_file, ref);
3906       fputc ('\n', asm_out_file);
3907     }
3908 }
3909
3910 /* Output a marker (i.e. a label) for the beginning of a function, before
3911    the prologue.  */
3912
3913 void
3914 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3915                           const char *file ATTRIBUTE_UNUSED)
3916 {
3917   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3918   char * dup_label;
3919   dw_fde_ref fde;
3920   section *fnsec;
3921
3922   current_function_func_begin_label = NULL;
3923
3924 #ifdef TARGET_UNWIND_INFO
3925   /* ??? current_function_func_begin_label is also used by except.c
3926      for call-site information.  We must emit this label if it might
3927      be used.  */
3928   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3929       && ! dwarf2out_do_frame ())
3930     return;
3931 #else
3932   if (! dwarf2out_do_frame ())
3933     return;
3934 #endif
3935
3936   fnsec = function_section (current_function_decl);
3937   switch_to_section (fnsec);
3938   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3939                                current_function_funcdef_no);
3940   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3941                           current_function_funcdef_no);
3942   dup_label = xstrdup (label);
3943   current_function_func_begin_label = dup_label;
3944
3945 #ifdef TARGET_UNWIND_INFO
3946   /* We can elide the fde allocation if we're not emitting debug info.  */
3947   if (! dwarf2out_do_frame ())
3948     return;
3949 #endif
3950
3951   /* Expand the fde table if necessary.  */
3952   if (fde_table_in_use == fde_table_allocated)
3953     {
3954       fde_table_allocated += FDE_TABLE_INCREMENT;
3955       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3956       memset (fde_table + fde_table_in_use, 0,
3957               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3958     }
3959
3960   /* Record the FDE associated with this function.  */
3961   current_funcdef_fde = fde_table_in_use;
3962
3963   /* Add the new FDE at the end of the fde_table.  */
3964   fde = &fde_table[fde_table_in_use++];
3965   fde->decl = current_function_decl;
3966   fde->dw_fde_begin = dup_label;
3967   fde->dw_fde_current_label = dup_label;
3968   fde->dw_fde_hot_section_label = NULL;
3969   fde->dw_fde_hot_section_end_label = NULL;
3970   fde->dw_fde_unlikely_section_label = NULL;
3971   fde->dw_fde_unlikely_section_end_label = NULL;
3972   fde->dw_fde_switched_sections = 0;
3973   fde->dw_fde_switched_cold_to_hot = 0;
3974   fde->dw_fde_end = NULL;
3975   fde->dw_fde_vms_end_prologue = NULL;
3976   fde->dw_fde_vms_begin_epilogue = NULL;
3977   fde->dw_fde_cfi = NULL;
3978   fde->dw_fde_switch_cfi = NULL;
3979   fde->funcdef_number = current_function_funcdef_no;
3980   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3981   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3982   fde->nothrow = crtl->nothrow;
3983   fde->drap_reg = INVALID_REGNUM;
3984   fde->vdrap_reg = INVALID_REGNUM;
3985   if (flag_reorder_blocks_and_partition)
3986     {
3987       section *unlikelysec;
3988       if (first_function_block_is_cold)
3989         fde->in_std_section = 1;
3990       else
3991         fde->in_std_section
3992           = (fnsec == text_section
3993              || (cold_text_section && fnsec == cold_text_section));
3994       unlikelysec = unlikely_text_section ();
3995       fde->cold_in_std_section
3996         = (unlikelysec == text_section
3997            || (cold_text_section && unlikelysec == cold_text_section));
3998     }
3999   else
4000     {
4001       fde->in_std_section
4002         = (fnsec == text_section
4003            || (cold_text_section && fnsec == cold_text_section));
4004       fde->cold_in_std_section = 0;
4005     }
4006
4007   args_size = old_args_size = 0;
4008
4009   /* We only want to output line number information for the genuine dwarf2
4010      prologue case, not the eh frame case.  */
4011 #ifdef DWARF2_DEBUGGING_INFO
4012   if (file)
4013     dwarf2out_source_line (line, file, 0, true);
4014 #endif
4015
4016   if (dwarf2out_do_cfi_asm ())
4017     dwarf2out_do_cfi_startproc (false);
4018   else
4019     {
4020       rtx personality = get_personality_function (current_function_decl);
4021       if (!current_unit_personality)
4022         current_unit_personality = personality;
4023
4024       /* We cannot keep a current personality per function as without CFI
4025          asm, at the point where we emit the CFI data, there is no current
4026          function anymore.  */
4027       if (personality && current_unit_personality != personality)
4028         sorry ("multiple EH personalities are supported only with assemblers "
4029                "supporting .cfi_personality directive");
4030     }
4031 }
4032
4033 /* Output a marker (i.e. a label) for the end of the generated code
4034    for a function prologue.  This gets called *after* the prologue code has
4035    been generated.  */
4036
4037 void
4038 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4039                         const char *file ATTRIBUTE_UNUSED)
4040 {
4041   dw_fde_ref fde;
4042   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4043
4044   /* Output a label to mark the endpoint of the code generated for this
4045      function.  */
4046   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4047                                current_function_funcdef_no);
4048   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4049                           current_function_funcdef_no);
4050   fde = &fde_table[fde_table_in_use - 1];
4051   fde->dw_fde_vms_end_prologue = xstrdup (label);
4052 }
4053
4054 /* Output a marker (i.e. a label) for the beginning of the generated code
4055    for a function epilogue.  This gets called *before* the prologue code has
4056    been generated.  */
4057
4058 void
4059 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4060                           const char *file ATTRIBUTE_UNUSED)
4061 {
4062   dw_fde_ref fde;
4063   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4064
4065   fde = &fde_table[fde_table_in_use - 1];
4066   if (fde->dw_fde_vms_begin_epilogue)
4067     return;
4068
4069   /* Output a label to mark the endpoint of the code generated for this
4070      function.  */
4071   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4072                                current_function_funcdef_no);
4073   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4074                           current_function_funcdef_no);
4075   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4076 }
4077
4078 /* Output a marker (i.e. a label) for the absolute end of the generated code
4079    for a function definition.  This gets called *after* the epilogue code has
4080    been generated.  */
4081
4082 void
4083 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4084                         const char *file ATTRIBUTE_UNUSED)
4085 {
4086   dw_fde_ref fde;
4087   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4088
4089 #ifdef DWARF2_DEBUGGING_INFO
4090   last_var_location_insn = NULL_RTX;
4091 #endif
4092
4093   if (dwarf2out_do_cfi_asm ())
4094     fprintf (asm_out_file, "\t.cfi_endproc\n");
4095
4096   /* Output a label to mark the endpoint of the code generated for this
4097      function.  */
4098   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4099                                current_function_funcdef_no);
4100   ASM_OUTPUT_LABEL (asm_out_file, label);
4101   fde = current_fde ();
4102   gcc_assert (fde != NULL);
4103   fde->dw_fde_end = xstrdup (label);
4104 }
4105
4106 void
4107 dwarf2out_frame_init (void)
4108 {
4109   /* Allocate the initial hunk of the fde_table.  */
4110   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4111   fde_table_allocated = FDE_TABLE_INCREMENT;
4112   fde_table_in_use = 0;
4113
4114   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4115      sake of lookup_cfa.  */
4116
4117   /* On entry, the Canonical Frame Address is at SP.  */
4118   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4119
4120 #ifdef DWARF2_UNWIND_INFO
4121   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4122     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4123 #endif
4124 }
4125
4126 void
4127 dwarf2out_frame_finish (void)
4128 {
4129   /* Output call frame information.  */
4130   if (DWARF2_FRAME_INFO)
4131     output_call_frame_info (0);
4132
4133 #ifndef TARGET_UNWIND_INFO
4134   /* Output another copy for the unwinder.  */
4135   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4136     output_call_frame_info (1);
4137 #endif
4138 }
4139
4140 /* Note that the current function section is being used for code.  */
4141
4142 static void
4143 dwarf2out_note_section_used (void)
4144 {
4145   section *sec = current_function_section ();
4146   if (sec == text_section)
4147     text_section_used = true;
4148   else if (sec == cold_text_section)
4149     cold_text_section_used = true;
4150 }
4151
4152 void
4153 dwarf2out_switch_text_section (void)
4154 {
4155   dw_fde_ref fde = current_fde ();
4156
4157   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4158
4159   fde->dw_fde_switched_sections = 1;
4160   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4161
4162   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4163   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4164   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4165   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4166   have_multiple_function_sections = true;
4167
4168   /* Reset the current label on switching text sections, so that we
4169      don't attempt to advance_loc4 between labels in different sections.  */
4170   fde->dw_fde_current_label = NULL;
4171
4172   /* There is no need to mark used sections when not debugging.  */
4173   if (cold_text_section != NULL)
4174     dwarf2out_note_section_used ();
4175
4176   if (dwarf2out_do_cfi_asm ())
4177     fprintf (asm_out_file, "\t.cfi_endproc\n");
4178
4179   /* Now do the real section switch.  */
4180   switch_to_section (current_function_section ());
4181
4182   if (dwarf2out_do_cfi_asm ())
4183     {
4184       dwarf2out_do_cfi_startproc (true);
4185       /* As this is a different FDE, insert all current CFI instructions
4186          again.  */
4187       output_cfis (fde->dw_fde_cfi, true, fde, true);
4188     }
4189   else
4190     {
4191       dw_cfi_ref cfi = fde->dw_fde_cfi;
4192
4193       cfi = fde->dw_fde_cfi;
4194       if (cfi)
4195         while (cfi->dw_cfi_next != NULL)
4196           cfi = cfi->dw_cfi_next;
4197       fde->dw_fde_switch_cfi = cfi;
4198     }
4199 }
4200 #endif
4201 \f
4202 /* And now, the subset of the debugging information support code necessary
4203    for emitting location expressions.  */
4204
4205 /* Data about a single source file.  */
4206 struct GTY(()) dwarf_file_data {
4207   const char * filename;
4208   int emitted_number;
4209 };
4210
4211 typedef struct dw_val_struct *dw_val_ref;
4212 typedef struct die_struct *dw_die_ref;
4213 typedef const struct die_struct *const_dw_die_ref;
4214 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4215 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4216
4217 typedef struct GTY(()) deferred_locations_struct
4218 {
4219   tree variable;
4220   dw_die_ref die;
4221 } deferred_locations;
4222
4223 DEF_VEC_O(deferred_locations);
4224 DEF_VEC_ALLOC_O(deferred_locations,gc);
4225
4226 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4227
4228 DEF_VEC_P(dw_die_ref);
4229 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4230
4231 /* Each DIE may have a series of attribute/value pairs.  Values
4232    can take on several forms.  The forms that are used in this
4233    implementation are listed below.  */
4234
4235 enum dw_val_class
4236 {
4237   dw_val_class_addr,
4238   dw_val_class_offset,
4239   dw_val_class_loc,
4240   dw_val_class_loc_list,
4241   dw_val_class_range_list,
4242   dw_val_class_const,
4243   dw_val_class_unsigned_const,
4244   dw_val_class_const_double,
4245   dw_val_class_vec,
4246   dw_val_class_flag,
4247   dw_val_class_die_ref,
4248   dw_val_class_fde_ref,
4249   dw_val_class_lbl_id,
4250   dw_val_class_lineptr,
4251   dw_val_class_str,
4252   dw_val_class_macptr,
4253   dw_val_class_file,
4254   dw_val_class_data8,
4255   dw_val_class_vms_delta
4256 };
4257
4258 /* Describe a floating point constant value, or a vector constant value.  */
4259
4260 typedef struct GTY(()) dw_vec_struct {
4261   unsigned char * GTY((length ("%h.length"))) array;
4262   unsigned length;
4263   unsigned elt_size;
4264 }
4265 dw_vec_const;
4266
4267 /* The dw_val_node describes an attribute's value, as it is
4268    represented internally.  */
4269
4270 typedef struct GTY(()) dw_val_struct {
4271   enum dw_val_class val_class;
4272   union dw_val_struct_union
4273     {
4274       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4275       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4276       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4277       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4278       HOST_WIDE_INT GTY ((default)) val_int;
4279       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4280       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4281       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4282       struct dw_val_die_union
4283         {
4284           dw_die_ref die;
4285           int external;
4286         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4287       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4288       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4289       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4290       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4291       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4292       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4293       struct dw_val_vms_delta_union
4294         {
4295           char * lbl1;
4296           char * lbl2;
4297         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4298     }
4299   GTY ((desc ("%1.val_class"))) v;
4300 }
4301 dw_val_node;
4302
4303 /* Locations in memory are described using a sequence of stack machine
4304    operations.  */
4305
4306 typedef struct GTY(()) dw_loc_descr_struct {
4307   dw_loc_descr_ref dw_loc_next;
4308   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4309   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4310      from DW_OP_addr with a dtp-relative symbol relocation.  */
4311   unsigned int dtprel : 1;
4312   int dw_loc_addr;
4313   dw_val_node dw_loc_oprnd1;
4314   dw_val_node dw_loc_oprnd2;
4315 }
4316 dw_loc_descr_node;
4317
4318 /* Location lists are ranges + location descriptions for that range,
4319    so you can track variables that are in different places over
4320    their entire life.  */
4321 typedef struct GTY(()) dw_loc_list_struct {
4322   dw_loc_list_ref dw_loc_next;
4323   const char *begin; /* Label for begin address of range */
4324   const char *end;  /* Label for end address of range */
4325   char *ll_symbol; /* Label for beginning of location list.
4326                       Only on head of list */
4327   const char *section; /* Section this loclist is relative to */
4328   dw_loc_descr_ref expr;
4329 } dw_loc_list_node;
4330
4331 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4332
4333 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4334
4335 /* Convert a DWARF stack opcode into its string name.  */
4336
4337 static const char *
4338 dwarf_stack_op_name (unsigned int op)
4339 {
4340   switch (op)
4341     {
4342     case DW_OP_addr:
4343       return "DW_OP_addr";
4344     case DW_OP_deref:
4345       return "DW_OP_deref";
4346     case DW_OP_const1u:
4347       return "DW_OP_const1u";
4348     case DW_OP_const1s:
4349       return "DW_OP_const1s";
4350     case DW_OP_const2u:
4351       return "DW_OP_const2u";
4352     case DW_OP_const2s:
4353       return "DW_OP_const2s";
4354     case DW_OP_const4u:
4355       return "DW_OP_const4u";
4356     case DW_OP_const4s:
4357       return "DW_OP_const4s";
4358     case DW_OP_const8u:
4359       return "DW_OP_const8u";
4360     case DW_OP_const8s:
4361       return "DW_OP_const8s";
4362     case DW_OP_constu:
4363       return "DW_OP_constu";
4364     case DW_OP_consts:
4365       return "DW_OP_consts";
4366     case DW_OP_dup:
4367       return "DW_OP_dup";
4368     case DW_OP_drop:
4369       return "DW_OP_drop";
4370     case DW_OP_over:
4371       return "DW_OP_over";
4372     case DW_OP_pick:
4373       return "DW_OP_pick";
4374     case DW_OP_swap:
4375       return "DW_OP_swap";
4376     case DW_OP_rot:
4377       return "DW_OP_rot";
4378     case DW_OP_xderef:
4379       return "DW_OP_xderef";
4380     case DW_OP_abs:
4381       return "DW_OP_abs";
4382     case DW_OP_and:
4383       return "DW_OP_and";
4384     case DW_OP_div:
4385       return "DW_OP_div";
4386     case DW_OP_minus:
4387       return "DW_OP_minus";
4388     case DW_OP_mod:
4389       return "DW_OP_mod";
4390     case DW_OP_mul:
4391       return "DW_OP_mul";
4392     case DW_OP_neg:
4393       return "DW_OP_neg";
4394     case DW_OP_not:
4395       return "DW_OP_not";
4396     case DW_OP_or:
4397       return "DW_OP_or";
4398     case DW_OP_plus:
4399       return "DW_OP_plus";
4400     case DW_OP_plus_uconst:
4401       return "DW_OP_plus_uconst";
4402     case DW_OP_shl:
4403       return "DW_OP_shl";
4404     case DW_OP_shr:
4405       return "DW_OP_shr";
4406     case DW_OP_shra:
4407       return "DW_OP_shra";
4408     case DW_OP_xor:
4409       return "DW_OP_xor";
4410     case DW_OP_bra:
4411       return "DW_OP_bra";
4412     case DW_OP_eq:
4413       return "DW_OP_eq";
4414     case DW_OP_ge:
4415       return "DW_OP_ge";
4416     case DW_OP_gt:
4417       return "DW_OP_gt";
4418     case DW_OP_le:
4419       return "DW_OP_le";
4420     case DW_OP_lt:
4421       return "DW_OP_lt";
4422     case DW_OP_ne:
4423       return "DW_OP_ne";
4424     case DW_OP_skip:
4425       return "DW_OP_skip";
4426     case DW_OP_lit0:
4427       return "DW_OP_lit0";
4428     case DW_OP_lit1:
4429       return "DW_OP_lit1";
4430     case DW_OP_lit2:
4431       return "DW_OP_lit2";
4432     case DW_OP_lit3:
4433       return "DW_OP_lit3";
4434     case DW_OP_lit4:
4435       return "DW_OP_lit4";
4436     case DW_OP_lit5:
4437       return "DW_OP_lit5";
4438     case DW_OP_lit6:
4439       return "DW_OP_lit6";
4440     case DW_OP_lit7:
4441       return "DW_OP_lit7";
4442     case DW_OP_lit8:
4443       return "DW_OP_lit8";
4444     case DW_OP_lit9:
4445       return "DW_OP_lit9";
4446     case DW_OP_lit10:
4447       return "DW_OP_lit10";
4448     case DW_OP_lit11:
4449       return "DW_OP_lit11";
4450     case DW_OP_lit12:
4451       return "DW_OP_lit12";
4452     case DW_OP_lit13:
4453       return "DW_OP_lit13";
4454     case DW_OP_lit14:
4455       return "DW_OP_lit14";
4456     case DW_OP_lit15:
4457       return "DW_OP_lit15";
4458     case DW_OP_lit16:
4459       return "DW_OP_lit16";
4460     case DW_OP_lit17:
4461       return "DW_OP_lit17";
4462     case DW_OP_lit18:
4463       return "DW_OP_lit18";
4464     case DW_OP_lit19:
4465       return "DW_OP_lit19";
4466     case DW_OP_lit20:
4467       return "DW_OP_lit20";
4468     case DW_OP_lit21:
4469       return "DW_OP_lit21";
4470     case DW_OP_lit22:
4471       return "DW_OP_lit22";
4472     case DW_OP_lit23:
4473       return "DW_OP_lit23";
4474     case DW_OP_lit24:
4475       return "DW_OP_lit24";
4476     case DW_OP_lit25:
4477       return "DW_OP_lit25";
4478     case DW_OP_lit26:
4479       return "DW_OP_lit26";
4480     case DW_OP_lit27:
4481       return "DW_OP_lit27";
4482     case DW_OP_lit28:
4483       return "DW_OP_lit28";
4484     case DW_OP_lit29:
4485       return "DW_OP_lit29";
4486     case DW_OP_lit30:
4487       return "DW_OP_lit30";
4488     case DW_OP_lit31:
4489       return "DW_OP_lit31";
4490     case DW_OP_reg0:
4491       return "DW_OP_reg0";
4492     case DW_OP_reg1:
4493       return "DW_OP_reg1";
4494     case DW_OP_reg2:
4495       return "DW_OP_reg2";
4496     case DW_OP_reg3:
4497       return "DW_OP_reg3";
4498     case DW_OP_reg4:
4499       return "DW_OP_reg4";
4500     case DW_OP_reg5:
4501       return "DW_OP_reg5";
4502     case DW_OP_reg6:
4503       return "DW_OP_reg6";
4504     case DW_OP_reg7:
4505       return "DW_OP_reg7";
4506     case DW_OP_reg8:
4507       return "DW_OP_reg8";
4508     case DW_OP_reg9:
4509       return "DW_OP_reg9";
4510     case DW_OP_reg10:
4511       return "DW_OP_reg10";
4512     case DW_OP_reg11:
4513       return "DW_OP_reg11";
4514     case DW_OP_reg12:
4515       return "DW_OP_reg12";
4516     case DW_OP_reg13:
4517       return "DW_OP_reg13";
4518     case DW_OP_reg14:
4519       return "DW_OP_reg14";
4520     case DW_OP_reg15:
4521       return "DW_OP_reg15";
4522     case DW_OP_reg16:
4523       return "DW_OP_reg16";
4524     case DW_OP_reg17:
4525       return "DW_OP_reg17";
4526     case DW_OP_reg18:
4527       return "DW_OP_reg18";
4528     case DW_OP_reg19:
4529       return "DW_OP_reg19";
4530     case DW_OP_reg20:
4531       return "DW_OP_reg20";
4532     case DW_OP_reg21:
4533       return "DW_OP_reg21";
4534     case DW_OP_reg22:
4535       return "DW_OP_reg22";
4536     case DW_OP_reg23:
4537       return "DW_OP_reg23";
4538     case DW_OP_reg24:
4539       return "DW_OP_reg24";
4540     case DW_OP_reg25:
4541       return "DW_OP_reg25";
4542     case DW_OP_reg26:
4543       return "DW_OP_reg26";
4544     case DW_OP_reg27:
4545       return "DW_OP_reg27";
4546     case DW_OP_reg28:
4547       return "DW_OP_reg28";
4548     case DW_OP_reg29:
4549       return "DW_OP_reg29";
4550     case DW_OP_reg30:
4551       return "DW_OP_reg30";
4552     case DW_OP_reg31:
4553       return "DW_OP_reg31";
4554     case DW_OP_breg0:
4555       return "DW_OP_breg0";
4556     case DW_OP_breg1:
4557       return "DW_OP_breg1";
4558     case DW_OP_breg2:
4559       return "DW_OP_breg2";
4560     case DW_OP_breg3:
4561       return "DW_OP_breg3";
4562     case DW_OP_breg4:
4563       return "DW_OP_breg4";
4564     case DW_OP_breg5:
4565       return "DW_OP_breg5";
4566     case DW_OP_breg6:
4567       return "DW_OP_breg6";
4568     case DW_OP_breg7:
4569       return "DW_OP_breg7";
4570     case DW_OP_breg8:
4571       return "DW_OP_breg8";
4572     case DW_OP_breg9:
4573       return "DW_OP_breg9";
4574     case DW_OP_breg10:
4575       return "DW_OP_breg10";
4576     case DW_OP_breg11:
4577       return "DW_OP_breg11";
4578     case DW_OP_breg12:
4579       return "DW_OP_breg12";
4580     case DW_OP_breg13:
4581       return "DW_OP_breg13";
4582     case DW_OP_breg14:
4583       return "DW_OP_breg14";
4584     case DW_OP_breg15:
4585       return "DW_OP_breg15";
4586     case DW_OP_breg16:
4587       return "DW_OP_breg16";
4588     case DW_OP_breg17:
4589       return "DW_OP_breg17";
4590     case DW_OP_breg18:
4591       return "DW_OP_breg18";
4592     case DW_OP_breg19:
4593       return "DW_OP_breg19";
4594     case DW_OP_breg20:
4595       return "DW_OP_breg20";
4596     case DW_OP_breg21:
4597       return "DW_OP_breg21";
4598     case DW_OP_breg22:
4599       return "DW_OP_breg22";
4600     case DW_OP_breg23:
4601       return "DW_OP_breg23";
4602     case DW_OP_breg24:
4603       return "DW_OP_breg24";
4604     case DW_OP_breg25:
4605       return "DW_OP_breg25";
4606     case DW_OP_breg26:
4607       return "DW_OP_breg26";
4608     case DW_OP_breg27:
4609       return "DW_OP_breg27";
4610     case DW_OP_breg28:
4611       return "DW_OP_breg28";
4612     case DW_OP_breg29:
4613       return "DW_OP_breg29";
4614     case DW_OP_breg30:
4615       return "DW_OP_breg30";
4616     case DW_OP_breg31:
4617       return "DW_OP_breg31";
4618     case DW_OP_regx:
4619       return "DW_OP_regx";
4620     case DW_OP_fbreg:
4621       return "DW_OP_fbreg";
4622     case DW_OP_bregx:
4623       return "DW_OP_bregx";
4624     case DW_OP_piece:
4625       return "DW_OP_piece";
4626     case DW_OP_deref_size:
4627       return "DW_OP_deref_size";
4628     case DW_OP_xderef_size:
4629       return "DW_OP_xderef_size";
4630     case DW_OP_nop:
4631       return "DW_OP_nop";
4632
4633     case DW_OP_push_object_address:
4634       return "DW_OP_push_object_address";
4635     case DW_OP_call2:
4636       return "DW_OP_call2";
4637     case DW_OP_call4:
4638       return "DW_OP_call4";
4639     case DW_OP_call_ref:
4640       return "DW_OP_call_ref";
4641     case DW_OP_implicit_value:
4642       return "DW_OP_implicit_value";
4643     case DW_OP_stack_value:
4644       return "DW_OP_stack_value";
4645     case DW_OP_form_tls_address:
4646       return "DW_OP_form_tls_address";
4647     case DW_OP_call_frame_cfa:
4648       return "DW_OP_call_frame_cfa";
4649     case DW_OP_bit_piece:
4650       return "DW_OP_bit_piece";
4651
4652     case DW_OP_GNU_push_tls_address:
4653       return "DW_OP_GNU_push_tls_address";
4654     case DW_OP_GNU_uninit:
4655       return "DW_OP_GNU_uninit";
4656     case DW_OP_GNU_encoded_addr:
4657       return "DW_OP_GNU_encoded_addr";
4658
4659     default:
4660       return "OP_<unknown>";
4661     }
4662 }
4663
4664 /* Return a pointer to a newly allocated location description.  Location
4665    descriptions are simple expression terms that can be strung
4666    together to form more complicated location (address) descriptions.  */
4667
4668 static inline dw_loc_descr_ref
4669 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4670                unsigned HOST_WIDE_INT oprnd2)
4671 {
4672   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4673
4674   descr->dw_loc_opc = op;
4675   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4676   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4677   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4678   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4679
4680   return descr;
4681 }
4682
4683 /* Return a pointer to a newly allocated location description for
4684    REG and OFFSET.  */
4685
4686 static inline dw_loc_descr_ref
4687 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4688 {
4689   if (reg <= 31)
4690     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4691                           offset, 0);
4692   else
4693     return new_loc_descr (DW_OP_bregx, reg, offset);
4694 }
4695
4696 /* Add a location description term to a location description expression.  */
4697
4698 static inline void
4699 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4700 {
4701   dw_loc_descr_ref *d;
4702
4703   /* Find the end of the chain.  */
4704   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4705     ;
4706
4707   *d = descr;
4708 }
4709
4710 /* Add a constant OFFSET to a location expression.  */
4711
4712 static void
4713 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4714 {
4715   dw_loc_descr_ref loc;
4716   HOST_WIDE_INT *p;
4717
4718   gcc_assert (*list_head != NULL);
4719
4720   if (!offset)
4721     return;
4722
4723   /* Find the end of the chain.  */
4724   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4725     ;
4726
4727   p = NULL;
4728   if (loc->dw_loc_opc == DW_OP_fbreg
4729       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4730     p = &loc->dw_loc_oprnd1.v.val_int;
4731   else if (loc->dw_loc_opc == DW_OP_bregx)
4732     p = &loc->dw_loc_oprnd2.v.val_int;
4733
4734   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4735      offset.  Don't optimize if an signed integer overflow would happen.  */
4736   if (p != NULL
4737       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4738           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4739     *p += offset;
4740
4741   else if (offset > 0)
4742     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4743
4744   else
4745     {
4746       loc->dw_loc_next = int_loc_descriptor (-offset);
4747       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4748     }
4749 }
4750
4751 #ifdef DWARF2_DEBUGGING_INFO
4752 /* Add a constant OFFSET to a location list.  */
4753
4754 static void
4755 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4756 {
4757   dw_loc_list_ref d;
4758   for (d = list_head; d != NULL; d = d->dw_loc_next)
4759     loc_descr_plus_const (&d->expr, offset);
4760 }
4761 #endif
4762
4763 /* Return the size of a location descriptor.  */
4764
4765 static unsigned long
4766 size_of_loc_descr (dw_loc_descr_ref loc)
4767 {
4768   unsigned long size = 1;
4769
4770   switch (loc->dw_loc_opc)
4771     {
4772     case DW_OP_addr:
4773       size += DWARF2_ADDR_SIZE;
4774       break;
4775     case DW_OP_const1u:
4776     case DW_OP_const1s:
4777       size += 1;
4778       break;
4779     case DW_OP_const2u:
4780     case DW_OP_const2s:
4781       size += 2;
4782       break;
4783     case DW_OP_const4u:
4784     case DW_OP_const4s:
4785       size += 4;
4786       break;
4787     case DW_OP_const8u:
4788     case DW_OP_const8s:
4789       size += 8;
4790       break;
4791     case DW_OP_constu:
4792       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4793       break;
4794     case DW_OP_consts:
4795       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4796       break;
4797     case DW_OP_pick:
4798       size += 1;
4799       break;
4800     case DW_OP_plus_uconst:
4801       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4802       break;
4803     case DW_OP_skip:
4804     case DW_OP_bra:
4805       size += 2;
4806       break;
4807     case DW_OP_breg0:
4808     case DW_OP_breg1:
4809     case DW_OP_breg2:
4810     case DW_OP_breg3:
4811     case DW_OP_breg4:
4812     case DW_OP_breg5:
4813     case DW_OP_breg6:
4814     case DW_OP_breg7:
4815     case DW_OP_breg8:
4816     case DW_OP_breg9:
4817     case DW_OP_breg10:
4818     case DW_OP_breg11:
4819     case DW_OP_breg12:
4820     case DW_OP_breg13:
4821     case DW_OP_breg14:
4822     case DW_OP_breg15:
4823     case DW_OP_breg16:
4824     case DW_OP_breg17:
4825     case DW_OP_breg18:
4826     case DW_OP_breg19:
4827     case DW_OP_breg20:
4828     case DW_OP_breg21:
4829     case DW_OP_breg22:
4830     case DW_OP_breg23:
4831     case DW_OP_breg24:
4832     case DW_OP_breg25:
4833     case DW_OP_breg26:
4834     case DW_OP_breg27:
4835     case DW_OP_breg28:
4836     case DW_OP_breg29:
4837     case DW_OP_breg30:
4838     case DW_OP_breg31:
4839       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4840       break;
4841     case DW_OP_regx:
4842       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4843       break;
4844     case DW_OP_fbreg:
4845       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4846       break;
4847     case DW_OP_bregx:
4848       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4849       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4850       break;
4851     case DW_OP_piece:
4852       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4853       break;
4854     case DW_OP_bit_piece:
4855       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4856       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4857       break;
4858     case DW_OP_deref_size:
4859     case DW_OP_xderef_size:
4860       size += 1;
4861       break;
4862     case DW_OP_call2:
4863       size += 2;
4864       break;
4865     case DW_OP_call4:
4866       size += 4;
4867       break;
4868     case DW_OP_call_ref:
4869       size += DWARF2_ADDR_SIZE;
4870       break;
4871     case DW_OP_implicit_value:
4872       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4873               + loc->dw_loc_oprnd1.v.val_unsigned;
4874       break;
4875     default:
4876       break;
4877     }
4878
4879   return size;
4880 }
4881
4882 /* Return the size of a series of location descriptors.  */
4883
4884 static unsigned long
4885 size_of_locs (dw_loc_descr_ref loc)
4886 {
4887   dw_loc_descr_ref l;
4888   unsigned long size;
4889
4890   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4891      field, to avoid writing to a PCH file.  */
4892   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4893     {
4894       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4895         break;
4896       size += size_of_loc_descr (l);
4897     }
4898   if (! l)
4899     return size;
4900
4901   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4902     {
4903       l->dw_loc_addr = size;
4904       size += size_of_loc_descr (l);
4905     }
4906
4907   return size;
4908 }
4909
4910 #ifdef DWARF2_DEBUGGING_INFO
4911 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4912 #endif
4913
4914 /* Output location description stack opcode's operands (if any).  */
4915
4916 static void
4917 output_loc_operands (dw_loc_descr_ref loc)
4918 {
4919   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4920   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4921
4922   switch (loc->dw_loc_opc)
4923     {
4924 #ifdef DWARF2_DEBUGGING_INFO
4925     case DW_OP_const2u:
4926     case DW_OP_const2s:
4927       dw2_asm_output_data (2, val1->v.val_int, NULL);
4928       break;
4929     case DW_OP_const4u:
4930     case DW_OP_const4s:
4931       dw2_asm_output_data (4, val1->v.val_int, NULL);
4932       break;
4933     case DW_OP_const8u:
4934     case DW_OP_const8s:
4935       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4936       dw2_asm_output_data (8, val1->v.val_int, NULL);
4937       break;
4938     case DW_OP_skip:
4939     case DW_OP_bra:
4940       {
4941         int offset;
4942
4943         gcc_assert (val1->val_class == dw_val_class_loc);
4944         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4945
4946         dw2_asm_output_data (2, offset, NULL);
4947       }
4948       break;
4949     case DW_OP_implicit_value:
4950       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4951       switch (val2->val_class)
4952         {
4953         case dw_val_class_const:
4954           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4955           break;
4956         case dw_val_class_vec:
4957           {
4958             unsigned int elt_size = val2->v.val_vec.elt_size;
4959             unsigned int len = val2->v.val_vec.length;
4960             unsigned int i;
4961             unsigned char *p;
4962
4963             if (elt_size > sizeof (HOST_WIDE_INT))
4964               {
4965                 elt_size /= 2;
4966                 len *= 2;
4967               }
4968             for (i = 0, p = val2->v.val_vec.array;
4969                  i < len;
4970                  i++, p += elt_size)
4971               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4972                                    "fp or vector constant word %u", i);
4973           }
4974           break;
4975         case dw_val_class_const_double:
4976           {
4977             unsigned HOST_WIDE_INT first, second;
4978
4979             if (WORDS_BIG_ENDIAN)
4980               {
4981                 first = val2->v.val_double.high;
4982                 second = val2->v.val_double.low;
4983               }
4984             else
4985               {
4986                 first = val2->v.val_double.low;
4987                 second = val2->v.val_double.high;
4988               }
4989             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4990                                  first, NULL);
4991             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4992                                  second, NULL);
4993           }
4994           break;
4995         case dw_val_class_addr:
4996           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4997           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4998           break;
4999         default:
5000           gcc_unreachable ();
5001         }
5002       break;
5003 #else
5004     case DW_OP_const2u:
5005     case DW_OP_const2s:
5006     case DW_OP_const4u:
5007     case DW_OP_const4s:
5008     case DW_OP_const8u:
5009     case DW_OP_const8s:
5010     case DW_OP_skip:
5011     case DW_OP_bra:
5012     case DW_OP_implicit_value:
5013       /* We currently don't make any attempt to make sure these are
5014          aligned properly like we do for the main unwind info, so
5015          don't support emitting things larger than a byte if we're
5016          only doing unwinding.  */
5017       gcc_unreachable ();
5018 #endif
5019     case DW_OP_const1u:
5020     case DW_OP_const1s:
5021       dw2_asm_output_data (1, val1->v.val_int, NULL);
5022       break;
5023     case DW_OP_constu:
5024       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5025       break;
5026     case DW_OP_consts:
5027       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5028       break;
5029     case DW_OP_pick:
5030       dw2_asm_output_data (1, val1->v.val_int, NULL);
5031       break;
5032     case DW_OP_plus_uconst:
5033       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5034       break;
5035     case DW_OP_breg0:
5036     case DW_OP_breg1:
5037     case DW_OP_breg2:
5038     case DW_OP_breg3:
5039     case DW_OP_breg4:
5040     case DW_OP_breg5:
5041     case DW_OP_breg6:
5042     case DW_OP_breg7:
5043     case DW_OP_breg8:
5044     case DW_OP_breg9:
5045     case DW_OP_breg10:
5046     case DW_OP_breg11:
5047     case DW_OP_breg12:
5048     case DW_OP_breg13:
5049     case DW_OP_breg14:
5050     case DW_OP_breg15:
5051     case DW_OP_breg16:
5052     case DW_OP_breg17:
5053     case DW_OP_breg18:
5054     case DW_OP_breg19:
5055     case DW_OP_breg20:
5056     case DW_OP_breg21:
5057     case DW_OP_breg22:
5058     case DW_OP_breg23:
5059     case DW_OP_breg24:
5060     case DW_OP_breg25:
5061     case DW_OP_breg26:
5062     case DW_OP_breg27:
5063     case DW_OP_breg28:
5064     case DW_OP_breg29:
5065     case DW_OP_breg30:
5066     case DW_OP_breg31:
5067       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5068       break;
5069     case DW_OP_regx:
5070       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5071       break;
5072     case DW_OP_fbreg:
5073       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5074       break;
5075     case DW_OP_bregx:
5076       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5077       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5078       break;
5079     case DW_OP_piece:
5080       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5081       break;
5082     case DW_OP_bit_piece:
5083       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5084       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5085       break;
5086     case DW_OP_deref_size:
5087     case DW_OP_xderef_size:
5088       dw2_asm_output_data (1, val1->v.val_int, NULL);
5089       break;
5090
5091     case DW_OP_addr:
5092       if (loc->dtprel)
5093         {
5094           if (targetm.asm_out.output_dwarf_dtprel)
5095             {
5096               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5097                                                    DWARF2_ADDR_SIZE,
5098                                                    val1->v.val_addr);
5099               fputc ('\n', asm_out_file);
5100             }
5101           else
5102             gcc_unreachable ();
5103         }
5104       else
5105         {
5106 #ifdef DWARF2_DEBUGGING_INFO
5107           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5108 #else
5109           gcc_unreachable ();
5110 #endif
5111         }
5112       break;
5113
5114     default:
5115       /* Other codes have no operands.  */
5116       break;
5117     }
5118 }
5119
5120 /* Output a sequence of location operations.  */
5121
5122 static void
5123 output_loc_sequence (dw_loc_descr_ref loc)
5124 {
5125   for (; loc != NULL; loc = loc->dw_loc_next)
5126     {
5127       /* Output the opcode.  */
5128       dw2_asm_output_data (1, loc->dw_loc_opc,
5129                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5130
5131       /* Output the operand(s) (if any).  */
5132       output_loc_operands (loc);
5133     }
5134 }
5135
5136 /* Output location description stack opcode's operands (if any).
5137    The output is single bytes on a line, suitable for .cfi_escape.  */
5138
5139 static void
5140 output_loc_operands_raw (dw_loc_descr_ref loc)
5141 {
5142   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5143   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5144
5145   switch (loc->dw_loc_opc)
5146     {
5147     case DW_OP_addr:
5148     case DW_OP_implicit_value:
5149       /* We cannot output addresses in .cfi_escape, only bytes.  */
5150       gcc_unreachable ();
5151
5152     case DW_OP_const1u:
5153     case DW_OP_const1s:
5154     case DW_OP_pick:
5155     case DW_OP_deref_size:
5156     case DW_OP_xderef_size:
5157       fputc (',', asm_out_file);
5158       dw2_asm_output_data_raw (1, val1->v.val_int);
5159       break;
5160
5161     case DW_OP_const2u:
5162     case DW_OP_const2s:
5163       fputc (',', asm_out_file);
5164       dw2_asm_output_data_raw (2, val1->v.val_int);
5165       break;
5166
5167     case DW_OP_const4u:
5168     case DW_OP_const4s:
5169       fputc (',', asm_out_file);
5170       dw2_asm_output_data_raw (4, val1->v.val_int);
5171       break;
5172
5173     case DW_OP_const8u:
5174     case DW_OP_const8s:
5175       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5176       fputc (',', asm_out_file);
5177       dw2_asm_output_data_raw (8, val1->v.val_int);
5178       break;
5179
5180     case DW_OP_skip:
5181     case DW_OP_bra:
5182       {
5183         int offset;
5184
5185         gcc_assert (val1->val_class == dw_val_class_loc);
5186         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5187
5188         fputc (',', asm_out_file);
5189         dw2_asm_output_data_raw (2, offset);
5190       }
5191       break;
5192
5193     case DW_OP_constu:
5194     case DW_OP_plus_uconst:
5195     case DW_OP_regx:
5196     case DW_OP_piece:
5197       fputc (',', asm_out_file);
5198       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5199       break;
5200
5201     case DW_OP_bit_piece:
5202       fputc (',', asm_out_file);
5203       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5204       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5205       break;
5206
5207     case DW_OP_consts:
5208     case DW_OP_breg0:
5209     case DW_OP_breg1:
5210     case DW_OP_breg2:
5211     case DW_OP_breg3:
5212     case DW_OP_breg4:
5213     case DW_OP_breg5:
5214     case DW_OP_breg6:
5215     case DW_OP_breg7:
5216     case DW_OP_breg8:
5217     case DW_OP_breg9:
5218     case DW_OP_breg10:
5219     case DW_OP_breg11:
5220     case DW_OP_breg12:
5221     case DW_OP_breg13:
5222     case DW_OP_breg14:
5223     case DW_OP_breg15:
5224     case DW_OP_breg16:
5225     case DW_OP_breg17:
5226     case DW_OP_breg18:
5227     case DW_OP_breg19:
5228     case DW_OP_breg20:
5229     case DW_OP_breg21:
5230     case DW_OP_breg22:
5231     case DW_OP_breg23:
5232     case DW_OP_breg24:
5233     case DW_OP_breg25:
5234     case DW_OP_breg26:
5235     case DW_OP_breg27:
5236     case DW_OP_breg28:
5237     case DW_OP_breg29:
5238     case DW_OP_breg30:
5239     case DW_OP_breg31:
5240     case DW_OP_fbreg:
5241       fputc (',', asm_out_file);
5242       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5243       break;
5244
5245     case DW_OP_bregx:
5246       fputc (',', asm_out_file);
5247       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5248       fputc (',', asm_out_file);
5249       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5250       break;
5251
5252     default:
5253       /* Other codes have no operands.  */
5254       break;
5255     }
5256 }
5257
5258 static void
5259 output_loc_sequence_raw (dw_loc_descr_ref loc)
5260 {
5261   while (1)
5262     {
5263       /* Output the opcode.  */
5264       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5265       output_loc_operands_raw (loc);
5266
5267       if (!loc->dw_loc_next)
5268         break;
5269       loc = loc->dw_loc_next;
5270
5271       fputc (',', asm_out_file);
5272     }
5273 }
5274
5275 /* This routine will generate the correct assembly data for a location
5276    description based on a cfi entry with a complex address.  */
5277
5278 static void
5279 output_cfa_loc (dw_cfi_ref cfi)
5280 {
5281   dw_loc_descr_ref loc;
5282   unsigned long size;
5283
5284   if (cfi->dw_cfi_opc == DW_CFA_expression)
5285     {
5286       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5287       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5288     }
5289   else
5290     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5291
5292   /* Output the size of the block.  */
5293   size = size_of_locs (loc);
5294   dw2_asm_output_data_uleb128 (size, NULL);
5295
5296   /* Now output the operations themselves.  */
5297   output_loc_sequence (loc);
5298 }
5299
5300 /* Similar, but used for .cfi_escape.  */
5301
5302 static void
5303 output_cfa_loc_raw (dw_cfi_ref cfi)
5304 {
5305   dw_loc_descr_ref loc;
5306   unsigned long size;
5307
5308   if (cfi->dw_cfi_opc == DW_CFA_expression)
5309     {
5310       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5311       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5312     }
5313   else
5314     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5315
5316   /* Output the size of the block.  */
5317   size = size_of_locs (loc);
5318   dw2_asm_output_data_uleb128_raw (size);
5319   fputc (',', asm_out_file);
5320
5321   /* Now output the operations themselves.  */
5322   output_loc_sequence_raw (loc);
5323 }
5324
5325 /* This function builds a dwarf location descriptor sequence from a
5326    dw_cfa_location, adding the given OFFSET to the result of the
5327    expression.  */
5328
5329 static struct dw_loc_descr_struct *
5330 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5331 {
5332   struct dw_loc_descr_struct *head, *tmp;
5333
5334   offset += cfa->offset;
5335
5336   if (cfa->indirect)
5337     {
5338       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5339       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5340       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5341       add_loc_descr (&head, tmp);
5342       if (offset != 0)
5343         {
5344           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5345           add_loc_descr (&head, tmp);
5346         }
5347     }
5348   else
5349     head = new_reg_loc_descr (cfa->reg, offset);
5350
5351   return head;
5352 }
5353
5354 /* This function builds a dwarf location descriptor sequence for
5355    the address at OFFSET from the CFA when stack is aligned to
5356    ALIGNMENT byte.  */
5357
5358 static struct dw_loc_descr_struct *
5359 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5360 {
5361   struct dw_loc_descr_struct *head;
5362   unsigned int dwarf_fp
5363     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5364
5365  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5366   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5367     {
5368       head = new_reg_loc_descr (dwarf_fp, 0);
5369       add_loc_descr (&head, int_loc_descriptor (alignment));
5370       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5371       loc_descr_plus_const (&head, offset);
5372     }
5373   else
5374     head = new_reg_loc_descr (dwarf_fp, offset);
5375   return head;
5376 }
5377
5378 /* This function fills in aa dw_cfa_location structure from a dwarf location
5379    descriptor sequence.  */
5380
5381 static void
5382 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5383 {
5384   struct dw_loc_descr_struct *ptr;
5385   cfa->offset = 0;
5386   cfa->base_offset = 0;
5387   cfa->indirect = 0;
5388   cfa->reg = -1;
5389
5390   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5391     {
5392       enum dwarf_location_atom op = ptr->dw_loc_opc;
5393
5394       switch (op)
5395         {
5396         case DW_OP_reg0:
5397         case DW_OP_reg1:
5398         case DW_OP_reg2:
5399         case DW_OP_reg3:
5400         case DW_OP_reg4:
5401         case DW_OP_reg5:
5402         case DW_OP_reg6:
5403         case DW_OP_reg7:
5404         case DW_OP_reg8:
5405         case DW_OP_reg9:
5406         case DW_OP_reg10:
5407         case DW_OP_reg11:
5408         case DW_OP_reg12:
5409         case DW_OP_reg13:
5410         case DW_OP_reg14:
5411         case DW_OP_reg15:
5412         case DW_OP_reg16:
5413         case DW_OP_reg17:
5414         case DW_OP_reg18:
5415         case DW_OP_reg19:
5416         case DW_OP_reg20:
5417         case DW_OP_reg21:
5418         case DW_OP_reg22:
5419         case DW_OP_reg23:
5420         case DW_OP_reg24:
5421         case DW_OP_reg25:
5422         case DW_OP_reg26:
5423         case DW_OP_reg27:
5424         case DW_OP_reg28:
5425         case DW_OP_reg29:
5426         case DW_OP_reg30:
5427         case DW_OP_reg31:
5428           cfa->reg = op - DW_OP_reg0;
5429           break;
5430         case DW_OP_regx:
5431           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5432           break;
5433         case DW_OP_breg0:
5434         case DW_OP_breg1:
5435         case DW_OP_breg2:
5436         case DW_OP_breg3:
5437         case DW_OP_breg4:
5438         case DW_OP_breg5:
5439         case DW_OP_breg6:
5440         case DW_OP_breg7:
5441         case DW_OP_breg8:
5442         case DW_OP_breg9:
5443         case DW_OP_breg10:
5444         case DW_OP_breg11:
5445         case DW_OP_breg12:
5446         case DW_OP_breg13:
5447         case DW_OP_breg14:
5448         case DW_OP_breg15:
5449         case DW_OP_breg16:
5450         case DW_OP_breg17:
5451         case DW_OP_breg18:
5452         case DW_OP_breg19:
5453         case DW_OP_breg20:
5454         case DW_OP_breg21:
5455         case DW_OP_breg22:
5456         case DW_OP_breg23:
5457         case DW_OP_breg24:
5458         case DW_OP_breg25:
5459         case DW_OP_breg26:
5460         case DW_OP_breg27:
5461         case DW_OP_breg28:
5462         case DW_OP_breg29:
5463         case DW_OP_breg30:
5464         case DW_OP_breg31:
5465           cfa->reg = op - DW_OP_breg0;
5466           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5467           break;
5468         case DW_OP_bregx:
5469           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5470           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5471           break;
5472         case DW_OP_deref:
5473           cfa->indirect = 1;
5474           break;
5475         case DW_OP_plus_uconst:
5476           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5477           break;
5478         default:
5479           internal_error ("DW_LOC_OP %s not implemented",
5480                           dwarf_stack_op_name (ptr->dw_loc_opc));
5481         }
5482     }
5483 }
5484 #endif /* .debug_frame support */
5485 \f
5486 /* And now, the support for symbolic debugging information.  */
5487 #ifdef DWARF2_DEBUGGING_INFO
5488
5489 /* .debug_str support.  */
5490 static int output_indirect_string (void **, void *);
5491
5492 static void dwarf2out_init (const char *);
5493 static void dwarf2out_finish (const char *);
5494 static void dwarf2out_assembly_start (void);
5495 static void dwarf2out_define (unsigned int, const char *);
5496 static void dwarf2out_undef (unsigned int, const char *);
5497 static void dwarf2out_start_source_file (unsigned, const char *);
5498 static void dwarf2out_end_source_file (unsigned);
5499 static void dwarf2out_function_decl (tree);
5500 static void dwarf2out_begin_block (unsigned, unsigned);
5501 static void dwarf2out_end_block (unsigned, unsigned);
5502 static bool dwarf2out_ignore_block (const_tree);
5503 static void dwarf2out_global_decl (tree);
5504 static void dwarf2out_type_decl (tree, int);
5505 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5506 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5507                                                  dw_die_ref);
5508 static void dwarf2out_abstract_function (tree);
5509 static void dwarf2out_var_location (rtx);
5510 static void dwarf2out_direct_call (tree);
5511 static void dwarf2out_virtual_call_token (tree, int);
5512 static void dwarf2out_copy_call_info (rtx, rtx);
5513 static void dwarf2out_virtual_call (int);
5514 static void dwarf2out_begin_function (tree);
5515 static void dwarf2out_set_name (tree, tree);
5516
5517 /* The debug hooks structure.  */
5518
5519 const struct gcc_debug_hooks dwarf2_debug_hooks =
5520 {
5521   dwarf2out_init,
5522   dwarf2out_finish,
5523   dwarf2out_assembly_start,
5524   dwarf2out_define,
5525   dwarf2out_undef,
5526   dwarf2out_start_source_file,
5527   dwarf2out_end_source_file,
5528   dwarf2out_begin_block,
5529   dwarf2out_end_block,
5530   dwarf2out_ignore_block,
5531   dwarf2out_source_line,
5532   dwarf2out_begin_prologue,
5533 #if VMS_DEBUGGING_INFO
5534   dwarf2out_vms_end_prologue,
5535   dwarf2out_vms_begin_epilogue,
5536 #else
5537   debug_nothing_int_charstar,
5538   debug_nothing_int_charstar,
5539 #endif
5540   dwarf2out_end_epilogue,
5541   dwarf2out_begin_function,
5542   debug_nothing_int,            /* end_function */
5543   dwarf2out_function_decl,      /* function_decl */
5544   dwarf2out_global_decl,
5545   dwarf2out_type_decl,          /* type_decl */
5546   dwarf2out_imported_module_or_decl,
5547   debug_nothing_tree,           /* deferred_inline_function */
5548   /* The DWARF 2 backend tries to reduce debugging bloat by not
5549      emitting the abstract description of inline functions until
5550      something tries to reference them.  */
5551   dwarf2out_abstract_function,  /* outlining_inline_function */
5552   debug_nothing_rtx,            /* label */
5553   debug_nothing_int,            /* handle_pch */
5554   dwarf2out_var_location,
5555   dwarf2out_switch_text_section,
5556   dwarf2out_direct_call,
5557   dwarf2out_virtual_call_token,
5558   dwarf2out_copy_call_info,
5559   dwarf2out_virtual_call,
5560   dwarf2out_set_name,
5561   1                             /* start_end_main_source_file */
5562 };
5563 #endif
5564 \f
5565 /* NOTE: In the comments in this file, many references are made to
5566    "Debugging Information Entries".  This term is abbreviated as `DIE'
5567    throughout the remainder of this file.  */
5568
5569 /* An internal representation of the DWARF output is built, and then
5570    walked to generate the DWARF debugging info.  The walk of the internal
5571    representation is done after the entire program has been compiled.
5572    The types below are used to describe the internal representation.  */
5573
5574 /* Various DIE's use offsets relative to the beginning of the
5575    .debug_info section to refer to each other.  */
5576
5577 typedef long int dw_offset;
5578
5579 /* Define typedefs here to avoid circular dependencies.  */
5580
5581 typedef struct dw_attr_struct *dw_attr_ref;
5582 typedef struct dw_line_info_struct *dw_line_info_ref;
5583 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5584 typedef struct pubname_struct *pubname_ref;
5585 typedef struct dw_ranges_struct *dw_ranges_ref;
5586 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5587 typedef struct comdat_type_struct *comdat_type_node_ref;
5588
5589 /* Each entry in the line_info_table maintains the file and
5590    line number associated with the label generated for that
5591    entry.  The label gives the PC value associated with
5592    the line number entry.  */
5593
5594 typedef struct GTY(()) dw_line_info_struct {
5595   unsigned long dw_file_num;
5596   unsigned long dw_line_num;
5597 }
5598 dw_line_info_entry;
5599
5600 /* Line information for functions in separate sections; each one gets its
5601    own sequence.  */
5602 typedef struct GTY(()) dw_separate_line_info_struct {
5603   unsigned long dw_file_num;
5604   unsigned long dw_line_num;
5605   unsigned long function;
5606 }
5607 dw_separate_line_info_entry;
5608
5609 /* Each DIE attribute has a field specifying the attribute kind,
5610    a link to the next attribute in the chain, and an attribute value.
5611    Attributes are typically linked below the DIE they modify.  */
5612
5613 typedef struct GTY(()) dw_attr_struct {
5614   enum dwarf_attribute dw_attr;
5615   dw_val_node dw_attr_val;
5616 }
5617 dw_attr_node;
5618
5619 DEF_VEC_O(dw_attr_node);
5620 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5621
5622 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5623    The children of each node form a circular list linked by
5624    die_sib.  die_child points to the node *before* the "first" child node.  */
5625
5626 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5627   enum dwarf_tag die_tag;
5628   union die_symbol_or_type_node
5629     {
5630       char * GTY ((tag ("0"))) die_symbol;
5631       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5632     }
5633   GTY ((desc ("dwarf_version >= 4"))) die_id;
5634   VEC(dw_attr_node,gc) * die_attr;
5635   dw_die_ref die_parent;
5636   dw_die_ref die_child;
5637   dw_die_ref die_sib;
5638   dw_die_ref die_definition; /* ref from a specification to its definition */
5639   dw_offset die_offset;
5640   unsigned long die_abbrev;
5641   int die_mark;
5642   /* Die is used and must not be pruned as unused.  */
5643   int die_perennial_p;
5644   unsigned int decl_id;
5645 }
5646 die_node;
5647
5648 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5649 #define FOR_EACH_CHILD(die, c, expr) do {       \
5650   c = die->die_child;                           \
5651   if (c) do {                                   \
5652     c = c->die_sib;                             \
5653     expr;                                       \
5654   } while (c != die->die_child);                \
5655 } while (0)
5656
5657 /* The pubname structure */
5658
5659 typedef struct GTY(()) pubname_struct {
5660   dw_die_ref die;
5661   const char *name;
5662 }
5663 pubname_entry;
5664
5665 DEF_VEC_O(pubname_entry);
5666 DEF_VEC_ALLOC_O(pubname_entry, gc);
5667
5668 struct GTY(()) dw_ranges_struct {
5669   /* If this is positive, it's a block number, otherwise it's a
5670      bitwise-negated index into dw_ranges_by_label.  */
5671   int num;
5672 };
5673
5674 struct GTY(()) dw_ranges_by_label_struct {
5675   const char *begin;
5676   const char *end;
5677 };
5678
5679 /* The comdat type node structure.  */
5680 typedef struct GTY(()) comdat_type_struct
5681 {
5682   dw_die_ref root_die;
5683   dw_die_ref type_die;
5684   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5685   struct comdat_type_struct *next;
5686 }
5687 comdat_type_node;
5688
5689 /* The limbo die list structure.  */
5690 typedef struct GTY(()) limbo_die_struct {
5691   dw_die_ref die;
5692   tree created_for;
5693   struct limbo_die_struct *next;
5694 }
5695 limbo_die_node;
5696
5697 typedef struct GTY(()) skeleton_chain_struct
5698 {
5699   dw_die_ref old_die;
5700   dw_die_ref new_die;
5701   struct skeleton_chain_struct *parent;
5702 }
5703 skeleton_chain_node;
5704
5705 /* How to start an assembler comment.  */
5706 #ifndef ASM_COMMENT_START
5707 #define ASM_COMMENT_START ";#"
5708 #endif
5709
5710 /* Define a macro which returns nonzero for a TYPE_DECL which was
5711    implicitly generated for a tagged type.
5712
5713    Note that unlike the gcc front end (which generates a NULL named
5714    TYPE_DECL node for each complete tagged type, each array type, and
5715    each function type node created) the g++ front end generates a
5716    _named_ TYPE_DECL node for each tagged type node created.
5717    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5718    generate a DW_TAG_typedef DIE for them.  */
5719
5720 #define TYPE_DECL_IS_STUB(decl)                         \
5721   (DECL_NAME (decl) == NULL_TREE                        \
5722    || (DECL_ARTIFICIAL (decl)                           \
5723        && is_tagged_type (TREE_TYPE (decl))             \
5724        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5725            /* This is necessary for stub decls that     \
5726               appear in nested inline functions.  */    \
5727            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5728                && (decl_ultimate_origin (decl)          \
5729                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5730
5731 /* Information concerning the compilation unit's programming
5732    language, and compiler version.  */
5733
5734 /* Fixed size portion of the DWARF compilation unit header.  */
5735 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5736   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5737
5738 /* Fixed size portion of the DWARF comdat type unit header.  */
5739 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5740   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5741    + DWARF_OFFSET_SIZE)
5742
5743 /* Fixed size portion of public names info.  */
5744 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5745
5746 /* Fixed size portion of the address range info.  */
5747 #define DWARF_ARANGES_HEADER_SIZE                                       \
5748   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5749                 DWARF2_ADDR_SIZE * 2)                                   \
5750    - DWARF_INITIAL_LENGTH_SIZE)
5751
5752 /* Size of padding portion in the address range info.  It must be
5753    aligned to twice the pointer size.  */
5754 #define DWARF_ARANGES_PAD_SIZE \
5755   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5756                 DWARF2_ADDR_SIZE * 2)                              \
5757    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5758
5759 /* Use assembler line directives if available.  */
5760 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5761 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5762 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5763 #else
5764 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5765 #endif
5766 #endif
5767
5768 /* Minimum line offset in a special line info. opcode.
5769    This value was chosen to give a reasonable range of values.  */
5770 #define DWARF_LINE_BASE  -10
5771
5772 /* First special line opcode - leave room for the standard opcodes.  */
5773 #define DWARF_LINE_OPCODE_BASE  10
5774
5775 /* Range of line offsets in a special line info. opcode.  */
5776 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5777
5778 /* Flag that indicates the initial value of the is_stmt_start flag.
5779    In the present implementation, we do not mark any lines as
5780    the beginning of a source statement, because that information
5781    is not made available by the GCC front-end.  */
5782 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5783
5784 /* Maximum number of operations per instruction bundle.  */
5785 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5786 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5787 #endif
5788
5789 #ifdef DWARF2_DEBUGGING_INFO
5790 /* This location is used by calc_die_sizes() to keep track
5791    the offset of each DIE within the .debug_info section.  */
5792 static unsigned long next_die_offset;
5793 #endif
5794
5795 /* Record the root of the DIE's built for the current compilation unit.  */
5796 static GTY(()) dw_die_ref comp_unit_die;
5797
5798 /* A list of type DIEs that have been separated into comdat sections.  */
5799 static GTY(()) comdat_type_node *comdat_type_list;
5800
5801 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5802 static GTY(()) limbo_die_node *limbo_die_list;
5803
5804 /* A list of DIEs for which we may have to generate
5805    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5806 static GTY(()) limbo_die_node *deferred_asm_name;
5807
5808 /* Filenames referenced by this compilation unit.  */
5809 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5810
5811 /* A hash table of references to DIE's that describe declarations.
5812    The key is a DECL_UID() which is a unique number identifying each decl.  */
5813 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5814
5815 /* A hash table of references to DIE's that describe COMMON blocks.
5816    The key is DECL_UID() ^ die_parent.  */
5817 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5818
5819 typedef struct GTY(()) die_arg_entry_struct {
5820     dw_die_ref die;
5821     tree arg;
5822 } die_arg_entry;
5823
5824 DEF_VEC_O(die_arg_entry);
5825 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5826
5827 /* Node of the variable location list.  */
5828 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5829   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5830      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5831      in mode of the EXPR_LIST node and first EXPR_LIST operand
5832      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5833      location or NULL for padding.  For larger bitsizes,
5834      mode is 0 and first operand is a CONCAT with bitsize
5835      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5836      NULL as second operand.  */
5837   rtx GTY (()) loc;
5838   const char * GTY (()) label;
5839   struct var_loc_node * GTY (()) next;
5840 };
5841
5842 /* Variable location list.  */
5843 struct GTY (()) var_loc_list_def {
5844   struct var_loc_node * GTY (()) first;
5845
5846   /* Pointer to the last but one or last element of the
5847      chained list.  If the list is empty, both first and
5848      last are NULL, if the list contains just one node
5849      or the last node certainly is not redundant, it points
5850      to the last node, otherwise points to the last but one.
5851      Do not mark it for GC because it is marked through the chain.  */
5852   struct var_loc_node * GTY ((skip ("%h"))) last;
5853
5854   /* DECL_UID of the variable decl.  */
5855   unsigned int decl_id;
5856 };
5857 typedef struct var_loc_list_def var_loc_list;
5858
5859
5860 /* Table of decl location linked lists.  */
5861 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5862
5863 /* A pointer to the base of a list of references to DIE's that
5864    are uniquely identified by their tag, presence/absence of
5865    children DIE's, and list of attribute/value pairs.  */
5866 static GTY((length ("abbrev_die_table_allocated")))
5867   dw_die_ref *abbrev_die_table;
5868
5869 /* Number of elements currently allocated for abbrev_die_table.  */
5870 static GTY(()) unsigned abbrev_die_table_allocated;
5871
5872 /* Number of elements in type_die_table currently in use.  */
5873 static GTY(()) unsigned abbrev_die_table_in_use;
5874
5875 /* Size (in elements) of increments by which we may expand the
5876    abbrev_die_table.  */
5877 #define ABBREV_DIE_TABLE_INCREMENT 256
5878
5879 /* A pointer to the base of a table that contains line information
5880    for each source code line in .text in the compilation unit.  */
5881 static GTY((length ("line_info_table_allocated")))
5882      dw_line_info_ref line_info_table;
5883
5884 /* Number of elements currently allocated for line_info_table.  */
5885 static GTY(()) unsigned line_info_table_allocated;
5886
5887 /* Number of elements in line_info_table currently in use.  */
5888 static GTY(()) unsigned line_info_table_in_use;
5889
5890 /* A pointer to the base of a table that contains line information
5891    for each source code line outside of .text in the compilation unit.  */
5892 static GTY ((length ("separate_line_info_table_allocated")))
5893      dw_separate_line_info_ref separate_line_info_table;
5894
5895 /* Number of elements currently allocated for separate_line_info_table.  */
5896 static GTY(()) unsigned separate_line_info_table_allocated;
5897
5898 /* Number of elements in separate_line_info_table currently in use.  */
5899 static GTY(()) unsigned separate_line_info_table_in_use;
5900
5901 /* Size (in elements) of increments by which we may expand the
5902    line_info_table.  */
5903 #define LINE_INFO_TABLE_INCREMENT 1024
5904
5905 /* A pointer to the base of a table that contains a list of publicly
5906    accessible names.  */
5907 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5908
5909 /* A pointer to the base of a table that contains a list of publicly
5910    accessible types.  */
5911 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5912
5913 /* Array of dies for which we should generate .debug_arange info.  */
5914 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5915
5916 /* Number of elements currently allocated for arange_table.  */
5917 static GTY(()) unsigned arange_table_allocated;
5918
5919 /* Number of elements in arange_table currently in use.  */
5920 static GTY(()) unsigned arange_table_in_use;
5921
5922 /* Size (in elements) of increments by which we may expand the
5923    arange_table.  */
5924 #define ARANGE_TABLE_INCREMENT 64
5925
5926 /* Array of dies for which we should generate .debug_ranges info.  */
5927 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5928
5929 /* Number of elements currently allocated for ranges_table.  */
5930 static GTY(()) unsigned ranges_table_allocated;
5931
5932 /* Number of elements in ranges_table currently in use.  */
5933 static GTY(()) unsigned ranges_table_in_use;
5934
5935 /* Array of pairs of labels referenced in ranges_table.  */
5936 static GTY ((length ("ranges_by_label_allocated")))
5937      dw_ranges_by_label_ref ranges_by_label;
5938
5939 /* Number of elements currently allocated for ranges_by_label.  */
5940 static GTY(()) unsigned ranges_by_label_allocated;
5941
5942 /* Number of elements in ranges_by_label currently in use.  */
5943 static GTY(()) unsigned ranges_by_label_in_use;
5944
5945 /* Size (in elements) of increments by which we may expand the
5946    ranges_table.  */
5947 #define RANGES_TABLE_INCREMENT 64
5948
5949 /* Whether we have location lists that need outputting */
5950 static GTY(()) bool have_location_lists;
5951
5952 /* Unique label counter.  */
5953 static GTY(()) unsigned int loclabel_num;
5954
5955 /* Unique label counter for point-of-call tables.  */
5956 static GTY(()) unsigned int poc_label_num;
5957
5958 /* The direct call table structure.  */
5959
5960 typedef struct GTY(()) dcall_struct {
5961   unsigned int poc_label_num;
5962   tree poc_decl;
5963   dw_die_ref targ_die;
5964 }
5965 dcall_entry;
5966
5967 DEF_VEC_O(dcall_entry);
5968 DEF_VEC_ALLOC_O(dcall_entry, gc);
5969
5970 /* The virtual call table structure.  */
5971
5972 typedef struct GTY(()) vcall_struct {
5973   unsigned int poc_label_num;
5974   unsigned int vtable_slot;
5975 }
5976 vcall_entry;
5977
5978 DEF_VEC_O(vcall_entry);
5979 DEF_VEC_ALLOC_O(vcall_entry, gc);
5980
5981 /* Pointers to the direct and virtual call tables.  */
5982 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
5983 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
5984
5985 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
5986
5987 struct GTY (()) vcall_insn {
5988   int insn_uid;
5989   unsigned int vtable_slot;
5990 };
5991
5992 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
5993
5994 #ifdef DWARF2_DEBUGGING_INFO
5995 /* Record whether the function being analyzed contains inlined functions.  */
5996 static int current_function_has_inlines;
5997 #endif
5998 #if 0 && defined (MIPS_DEBUGGING_INFO)
5999 static int comp_unit_has_inlines;
6000 #endif
6001
6002 /* The last file entry emitted by maybe_emit_file().  */
6003 static GTY(()) struct dwarf_file_data * last_emitted_file;
6004
6005 /* Number of internal labels generated by gen_internal_sym().  */
6006 static GTY(()) int label_num;
6007
6008 /* Cached result of previous call to lookup_filename.  */
6009 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6010
6011 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6012
6013 #ifdef DWARF2_DEBUGGING_INFO
6014
6015 /* Offset from the "steady-state frame pointer" to the frame base,
6016    within the current function.  */
6017 static HOST_WIDE_INT frame_pointer_fb_offset;
6018
6019 /* Forward declarations for functions defined in this file.  */
6020
6021 static int is_pseudo_reg (const_rtx);
6022 static tree type_main_variant (tree);
6023 static int is_tagged_type (const_tree);
6024 static const char *dwarf_tag_name (unsigned);
6025 static const char *dwarf_attr_name (unsigned);
6026 static const char *dwarf_form_name (unsigned);
6027 static tree decl_ultimate_origin (const_tree);
6028 static tree decl_class_context (tree);
6029 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6030 static inline enum dw_val_class AT_class (dw_attr_ref);
6031 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6032 static inline unsigned AT_flag (dw_attr_ref);
6033 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6034 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6035 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6036 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6037 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6038                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6039 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6040                                unsigned int, unsigned char *);
6041 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6042 static hashval_t debug_str_do_hash (const void *);
6043 static int debug_str_eq (const void *, const void *);
6044 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6045 static inline const char *AT_string (dw_attr_ref);
6046 static enum dwarf_form AT_string_form (dw_attr_ref);
6047 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6048 static void add_AT_specification (dw_die_ref, dw_die_ref);
6049 static inline dw_die_ref AT_ref (dw_attr_ref);
6050 static inline int AT_ref_external (dw_attr_ref);
6051 static inline void set_AT_ref_external (dw_attr_ref, int);
6052 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6053 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6054 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6055 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6056                              dw_loc_list_ref);
6057 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6058 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6059 static inline rtx AT_addr (dw_attr_ref);
6060 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6061 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6062 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6063 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6064                            unsigned HOST_WIDE_INT);
6065 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6066                                unsigned long);
6067 static inline const char *AT_lbl (dw_attr_ref);
6068 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6069 static const char *get_AT_low_pc (dw_die_ref);
6070 static const char *get_AT_hi_pc (dw_die_ref);
6071 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6072 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6073 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6074 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6075 static bool is_cxx (void);
6076 static bool is_fortran (void);
6077 static bool is_ada (void);
6078 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6079 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6080 static void add_child_die (dw_die_ref, dw_die_ref);
6081 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6082 static dw_die_ref lookup_type_die (tree);
6083 static void equate_type_number_to_die (tree, dw_die_ref);
6084 static hashval_t decl_die_table_hash (const void *);
6085 static int decl_die_table_eq (const void *, const void *);
6086 static dw_die_ref lookup_decl_die (tree);
6087 static hashval_t common_block_die_table_hash (const void *);
6088 static int common_block_die_table_eq (const void *, const void *);
6089 static hashval_t decl_loc_table_hash (const void *);
6090 static int decl_loc_table_eq (const void *, const void *);
6091 static var_loc_list *lookup_decl_loc (const_tree);
6092 static void equate_decl_number_to_die (tree, dw_die_ref);
6093 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6094 static void print_spaces (FILE *);
6095 static void print_die (dw_die_ref, FILE *);
6096 static void print_dwarf_line_table (FILE *);
6097 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6098 static dw_die_ref pop_compile_unit (dw_die_ref);
6099 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6100 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6101 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6102 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6103 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6104 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6105 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6106                                    struct md5_ctx *, int *);
6107 struct checksum_attributes;
6108 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6109 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6110 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6111 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6112 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6113 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6114 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6115 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6116 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6117 static void compute_section_prefix (dw_die_ref);
6118 static int is_type_die (dw_die_ref);
6119 static int is_comdat_die (dw_die_ref);
6120 static int is_symbol_die (dw_die_ref);
6121 static void assign_symbol_names (dw_die_ref);
6122 static void break_out_includes (dw_die_ref);
6123 static int is_declaration_die (dw_die_ref);
6124 static int should_move_die_to_comdat (dw_die_ref);
6125 static dw_die_ref clone_as_declaration (dw_die_ref);
6126 static dw_die_ref clone_die (dw_die_ref);
6127 static dw_die_ref clone_tree (dw_die_ref);
6128 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6129 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6130 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6131 static dw_die_ref generate_skeleton (dw_die_ref);
6132 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6133                                                          dw_die_ref);
6134 static void break_out_comdat_types (dw_die_ref);
6135 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6136 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6137 static void copy_decls_for_unworthy_types (dw_die_ref);
6138
6139 static hashval_t htab_cu_hash (const void *);
6140 static int htab_cu_eq (const void *, const void *);
6141 static void htab_cu_del (void *);
6142 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6143 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6144 static void add_sibling_attributes (dw_die_ref);
6145 static void build_abbrev_table (dw_die_ref);
6146 static void output_location_lists (dw_die_ref);
6147 static int constant_size (unsigned HOST_WIDE_INT);
6148 static unsigned long size_of_die (dw_die_ref);
6149 static void calc_die_sizes (dw_die_ref);
6150 static void mark_dies (dw_die_ref);
6151 static void unmark_dies (dw_die_ref);
6152 static void unmark_all_dies (dw_die_ref);
6153 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6154 static unsigned long size_of_aranges (void);
6155 static enum dwarf_form value_format (dw_attr_ref);
6156 static void output_value_format (dw_attr_ref);
6157 static void output_abbrev_section (void);
6158 static void output_die_symbol (dw_die_ref);
6159 static void output_die (dw_die_ref);
6160 static void output_compilation_unit_header (void);
6161 static void output_comp_unit (dw_die_ref, int);
6162 static void output_comdat_type_unit (comdat_type_node *);
6163 static const char *dwarf2_name (tree, int);
6164 static void add_pubname (tree, dw_die_ref);
6165 static void add_pubname_string (const char *, dw_die_ref);
6166 static void add_pubtype (tree, dw_die_ref);
6167 static void output_pubnames (VEC (pubname_entry,gc) *);
6168 static void add_arange (tree, dw_die_ref);
6169 static void output_aranges (void);
6170 static unsigned int add_ranges_num (int);
6171 static unsigned int add_ranges (const_tree);
6172 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6173                                   bool *);
6174 static void output_ranges (void);
6175 static void output_line_info (void);
6176 static void output_file_names (void);
6177 static dw_die_ref base_type_die (tree);
6178 static int is_base_type (tree);
6179 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6180 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6181 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6182 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6183 static int type_is_enum (const_tree);
6184 static unsigned int dbx_reg_number (const_rtx);
6185 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6186 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6187 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6188                                                 enum var_init_status);
6189 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6190                                                      enum var_init_status);
6191 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6192                                          enum var_init_status);
6193 static int is_based_loc (const_rtx);
6194 static int resolve_one_addr (rtx *, void *);
6195 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6196                                             enum var_init_status);
6197 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6198                                                enum var_init_status);
6199 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6200                                         enum var_init_status);
6201 static dw_loc_list_ref loc_list_from_tree (tree, int);
6202 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6203 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6204 static tree field_type (const_tree);
6205 static unsigned int simple_type_align_in_bits (const_tree);
6206 static unsigned int simple_decl_align_in_bits (const_tree);
6207 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6208 static HOST_WIDE_INT field_byte_offset (const_tree);
6209 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6210                                          dw_loc_list_ref);
6211 static void add_data_member_location_attribute (dw_die_ref, tree);
6212 static bool add_const_value_attribute (dw_die_ref, rtx);
6213 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6214 static void insert_double (double_int, unsigned char *);
6215 static void insert_float (const_rtx, unsigned char *);
6216 static rtx rtl_for_decl_location (tree);
6217 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6218                                                    enum dwarf_attribute);
6219 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6220 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6221 static void add_name_attribute (dw_die_ref, const char *);
6222 static void add_comp_dir_attribute (dw_die_ref);
6223 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6224 static void add_subscript_info (dw_die_ref, tree, bool);
6225 static void add_byte_size_attribute (dw_die_ref, tree);
6226 static void add_bit_offset_attribute (dw_die_ref, tree);
6227 static void add_bit_size_attribute (dw_die_ref, tree);
6228 static void add_prototyped_attribute (dw_die_ref, tree);
6229 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6230 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6231 static void add_src_coords_attributes (dw_die_ref, tree);
6232 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6233 static void push_decl_scope (tree);
6234 static void pop_decl_scope (void);
6235 static dw_die_ref scope_die_for (tree, dw_die_ref);
6236 static inline int local_scope_p (dw_die_ref);
6237 static inline int class_scope_p (dw_die_ref);
6238 static inline int class_or_namespace_scope_p (dw_die_ref);
6239 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6240 static void add_calling_convention_attribute (dw_die_ref, tree);
6241 static const char *type_tag (const_tree);
6242 static tree member_declared_type (const_tree);
6243 #if 0
6244 static const char *decl_start_label (tree);
6245 #endif
6246 static void gen_array_type_die (tree, dw_die_ref);
6247 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6248 #if 0
6249 static void gen_entry_point_die (tree, dw_die_ref);
6250 #endif
6251 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6252 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6253 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6254 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6255 static void gen_formal_types_die (tree, dw_die_ref);
6256 static void gen_subprogram_die (tree, dw_die_ref);
6257 static void gen_variable_die (tree, tree, dw_die_ref);
6258 static void gen_const_die (tree, dw_die_ref);
6259 static void gen_label_die (tree, dw_die_ref);
6260 static void gen_lexical_block_die (tree, dw_die_ref, int);
6261 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6262 static void gen_field_die (tree, dw_die_ref);
6263 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6264 static dw_die_ref gen_compile_unit_die (const char *);
6265 static void gen_inheritance_die (tree, tree, dw_die_ref);
6266 static void gen_member_die (tree, dw_die_ref);
6267 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6268                                                 enum debug_info_usage);
6269 static void gen_subroutine_type_die (tree, dw_die_ref);
6270 static void gen_typedef_die (tree, dw_die_ref);
6271 static void gen_type_die (tree, dw_die_ref);
6272 static void gen_block_die (tree, dw_die_ref, int);
6273 static void decls_for_scope (tree, dw_die_ref, int);
6274 static int is_redundant_typedef (const_tree);
6275 static bool is_naming_typedef_decl (const_tree);
6276 static inline dw_die_ref get_context_die (tree);
6277 static void gen_namespace_die (tree, dw_die_ref);
6278 static void gen_decl_die (tree, tree, dw_die_ref);
6279 static dw_die_ref force_decl_die (tree);
6280 static dw_die_ref force_type_die (tree);
6281 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6282 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6283 static struct dwarf_file_data * lookup_filename (const char *);
6284 static void retry_incomplete_types (void);
6285 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6286 static void gen_generic_params_dies (tree);
6287 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6288 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6289 static void splice_child_die (dw_die_ref, dw_die_ref);
6290 static int file_info_cmp (const void *, const void *);
6291 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6292                                      const char *, const char *);
6293 static void output_loc_list (dw_loc_list_ref);
6294 static char *gen_internal_sym (const char *);
6295
6296 static void prune_unmark_dies (dw_die_ref);
6297 static void prune_unused_types_mark (dw_die_ref, int);
6298 static void prune_unused_types_walk (dw_die_ref);
6299 static void prune_unused_types_walk_attribs (dw_die_ref);
6300 static void prune_unused_types_prune (dw_die_ref);
6301 static void prune_unused_types (void);
6302 static int maybe_emit_file (struct dwarf_file_data *fd);
6303 static inline const char *AT_vms_delta1 (dw_attr_ref);
6304 static inline const char *AT_vms_delta2 (dw_attr_ref);
6305 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6306                                      const char *, const char *);
6307 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6308 static void gen_remaining_tmpl_value_param_die_attribute (void);
6309
6310 /* Section names used to hold DWARF debugging information.  */
6311 #ifndef DEBUG_INFO_SECTION
6312 #define DEBUG_INFO_SECTION      ".debug_info"
6313 #endif
6314 #ifndef DEBUG_ABBREV_SECTION
6315 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6316 #endif
6317 #ifndef DEBUG_ARANGES_SECTION
6318 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6319 #endif
6320 #ifndef DEBUG_MACINFO_SECTION
6321 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6322 #endif
6323 #ifndef DEBUG_LINE_SECTION
6324 #define DEBUG_LINE_SECTION      ".debug_line"
6325 #endif
6326 #ifndef DEBUG_LOC_SECTION
6327 #define DEBUG_LOC_SECTION       ".debug_loc"
6328 #endif
6329 #ifndef DEBUG_PUBNAMES_SECTION
6330 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6331 #endif
6332 #ifndef DEBUG_PUBTYPES_SECTION
6333 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6334 #endif
6335 #ifndef DEBUG_DCALL_SECTION
6336 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6337 #endif
6338 #ifndef DEBUG_VCALL_SECTION
6339 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6340 #endif
6341 #ifndef DEBUG_STR_SECTION
6342 #define DEBUG_STR_SECTION       ".debug_str"
6343 #endif
6344 #ifndef DEBUG_RANGES_SECTION
6345 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6346 #endif
6347
6348 /* Standard ELF section names for compiled code and data.  */
6349 #ifndef TEXT_SECTION_NAME
6350 #define TEXT_SECTION_NAME       ".text"
6351 #endif
6352
6353 /* Section flags for .debug_str section.  */
6354 #define DEBUG_STR_SECTION_FLAGS \
6355   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6356    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6357    : SECTION_DEBUG)
6358
6359 /* Labels we insert at beginning sections we can reference instead of
6360    the section names themselves.  */
6361
6362 #ifndef TEXT_SECTION_LABEL
6363 #define TEXT_SECTION_LABEL              "Ltext"
6364 #endif
6365 #ifndef COLD_TEXT_SECTION_LABEL
6366 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6367 #endif
6368 #ifndef DEBUG_LINE_SECTION_LABEL
6369 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6370 #endif
6371 #ifndef DEBUG_INFO_SECTION_LABEL
6372 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6373 #endif
6374 #ifndef DEBUG_ABBREV_SECTION_LABEL
6375 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6376 #endif
6377 #ifndef DEBUG_LOC_SECTION_LABEL
6378 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6379 #endif
6380 #ifndef DEBUG_RANGES_SECTION_LABEL
6381 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6382 #endif
6383 #ifndef DEBUG_MACINFO_SECTION_LABEL
6384 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6385 #endif
6386
6387 /* Mangled name attribute to use.  This used to be a vendor extension
6388    until DWARF 4 standardized it.  */
6389 #define AT_linkage_name \
6390   (dwarf_version >= 4 ? DW_AT_linkage_name : DW_AT_MIPS_linkage_name)
6391
6392
6393 /* Definitions of defaults for formats and names of various special
6394    (artificial) labels which may be generated within this file (when the -g
6395    options is used and DWARF2_DEBUGGING_INFO is in effect.
6396    If necessary, these may be overridden from within the tm.h file, but
6397    typically, overriding these defaults is unnecessary.  */
6398
6399 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6400 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6401 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6402 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6403 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6404 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6405 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6406 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6407 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6408 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6409
6410 #ifndef TEXT_END_LABEL
6411 #define TEXT_END_LABEL          "Letext"
6412 #endif
6413 #ifndef COLD_END_LABEL
6414 #define COLD_END_LABEL          "Letext_cold"
6415 #endif
6416 #ifndef BLOCK_BEGIN_LABEL
6417 #define BLOCK_BEGIN_LABEL       "LBB"
6418 #endif
6419 #ifndef BLOCK_END_LABEL
6420 #define BLOCK_END_LABEL         "LBE"
6421 #endif
6422 #ifndef LINE_CODE_LABEL
6423 #define LINE_CODE_LABEL         "LM"
6424 #endif
6425 #ifndef SEPARATE_LINE_CODE_LABEL
6426 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6427 #endif
6428
6429 \f
6430 /* We allow a language front-end to designate a function that is to be
6431    called to "demangle" any name before it is put into a DIE.  */
6432
6433 static const char *(*demangle_name_func) (const char *);
6434
6435 void
6436 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6437 {
6438   demangle_name_func = func;
6439 }
6440
6441 /* Test if rtl node points to a pseudo register.  */
6442
6443 static inline int
6444 is_pseudo_reg (const_rtx rtl)
6445 {
6446   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6447           || (GET_CODE (rtl) == SUBREG
6448               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6449 }
6450
6451 /* Return a reference to a type, with its const and volatile qualifiers
6452    removed.  */
6453
6454 static inline tree
6455 type_main_variant (tree type)
6456 {
6457   type = TYPE_MAIN_VARIANT (type);
6458
6459   /* ??? There really should be only one main variant among any group of
6460      variants of a given type (and all of the MAIN_VARIANT values for all
6461      members of the group should point to that one type) but sometimes the C
6462      front-end messes this up for array types, so we work around that bug
6463      here.  */
6464   if (TREE_CODE (type) == ARRAY_TYPE)
6465     while (type != TYPE_MAIN_VARIANT (type))
6466       type = TYPE_MAIN_VARIANT (type);
6467
6468   return type;
6469 }
6470
6471 /* Return nonzero if the given type node represents a tagged type.  */
6472
6473 static inline int
6474 is_tagged_type (const_tree type)
6475 {
6476   enum tree_code code = TREE_CODE (type);
6477
6478   return (code == RECORD_TYPE || code == UNION_TYPE
6479           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6480 }
6481
6482 /* Convert a DIE tag into its string name.  */
6483
6484 static const char *
6485 dwarf_tag_name (unsigned int tag)
6486 {
6487   switch (tag)
6488     {
6489     case DW_TAG_padding:
6490       return "DW_TAG_padding";
6491     case DW_TAG_array_type:
6492       return "DW_TAG_array_type";
6493     case DW_TAG_class_type:
6494       return "DW_TAG_class_type";
6495     case DW_TAG_entry_point:
6496       return "DW_TAG_entry_point";
6497     case DW_TAG_enumeration_type:
6498       return "DW_TAG_enumeration_type";
6499     case DW_TAG_formal_parameter:
6500       return "DW_TAG_formal_parameter";
6501     case DW_TAG_imported_declaration:
6502       return "DW_TAG_imported_declaration";
6503     case DW_TAG_label:
6504       return "DW_TAG_label";
6505     case DW_TAG_lexical_block:
6506       return "DW_TAG_lexical_block";
6507     case DW_TAG_member:
6508       return "DW_TAG_member";
6509     case DW_TAG_pointer_type:
6510       return "DW_TAG_pointer_type";
6511     case DW_TAG_reference_type:
6512       return "DW_TAG_reference_type";
6513     case DW_TAG_compile_unit:
6514       return "DW_TAG_compile_unit";
6515     case DW_TAG_string_type:
6516       return "DW_TAG_string_type";
6517     case DW_TAG_structure_type:
6518       return "DW_TAG_structure_type";
6519     case DW_TAG_subroutine_type:
6520       return "DW_TAG_subroutine_type";
6521     case DW_TAG_typedef:
6522       return "DW_TAG_typedef";
6523     case DW_TAG_union_type:
6524       return "DW_TAG_union_type";
6525     case DW_TAG_unspecified_parameters:
6526       return "DW_TAG_unspecified_parameters";
6527     case DW_TAG_variant:
6528       return "DW_TAG_variant";
6529     case DW_TAG_common_block:
6530       return "DW_TAG_common_block";
6531     case DW_TAG_common_inclusion:
6532       return "DW_TAG_common_inclusion";
6533     case DW_TAG_inheritance:
6534       return "DW_TAG_inheritance";
6535     case DW_TAG_inlined_subroutine:
6536       return "DW_TAG_inlined_subroutine";
6537     case DW_TAG_module:
6538       return "DW_TAG_module";
6539     case DW_TAG_ptr_to_member_type:
6540       return "DW_TAG_ptr_to_member_type";
6541     case DW_TAG_set_type:
6542       return "DW_TAG_set_type";
6543     case DW_TAG_subrange_type:
6544       return "DW_TAG_subrange_type";
6545     case DW_TAG_with_stmt:
6546       return "DW_TAG_with_stmt";
6547     case DW_TAG_access_declaration:
6548       return "DW_TAG_access_declaration";
6549     case DW_TAG_base_type:
6550       return "DW_TAG_base_type";
6551     case DW_TAG_catch_block:
6552       return "DW_TAG_catch_block";
6553     case DW_TAG_const_type:
6554       return "DW_TAG_const_type";
6555     case DW_TAG_constant:
6556       return "DW_TAG_constant";
6557     case DW_TAG_enumerator:
6558       return "DW_TAG_enumerator";
6559     case DW_TAG_file_type:
6560       return "DW_TAG_file_type";
6561     case DW_TAG_friend:
6562       return "DW_TAG_friend";
6563     case DW_TAG_namelist:
6564       return "DW_TAG_namelist";
6565     case DW_TAG_namelist_item:
6566       return "DW_TAG_namelist_item";
6567     case DW_TAG_packed_type:
6568       return "DW_TAG_packed_type";
6569     case DW_TAG_subprogram:
6570       return "DW_TAG_subprogram";
6571     case DW_TAG_template_type_param:
6572       return "DW_TAG_template_type_param";
6573     case DW_TAG_template_value_param:
6574       return "DW_TAG_template_value_param";
6575     case DW_TAG_thrown_type:
6576       return "DW_TAG_thrown_type";
6577     case DW_TAG_try_block:
6578       return "DW_TAG_try_block";
6579     case DW_TAG_variant_part:
6580       return "DW_TAG_variant_part";
6581     case DW_TAG_variable:
6582       return "DW_TAG_variable";
6583     case DW_TAG_volatile_type:
6584       return "DW_TAG_volatile_type";
6585     case DW_TAG_dwarf_procedure:
6586       return "DW_TAG_dwarf_procedure";
6587     case DW_TAG_restrict_type:
6588       return "DW_TAG_restrict_type";
6589     case DW_TAG_interface_type:
6590       return "DW_TAG_interface_type";
6591     case DW_TAG_namespace:
6592       return "DW_TAG_namespace";
6593     case DW_TAG_imported_module:
6594       return "DW_TAG_imported_module";
6595     case DW_TAG_unspecified_type:
6596       return "DW_TAG_unspecified_type";
6597     case DW_TAG_partial_unit:
6598       return "DW_TAG_partial_unit";
6599     case DW_TAG_imported_unit:
6600       return "DW_TAG_imported_unit";
6601     case DW_TAG_condition:
6602       return "DW_TAG_condition";
6603     case DW_TAG_shared_type:
6604       return "DW_TAG_shared_type";
6605     case DW_TAG_type_unit:
6606       return "DW_TAG_type_unit";
6607     case DW_TAG_rvalue_reference_type:
6608       return "DW_TAG_rvalue_reference_type";
6609     case DW_TAG_template_alias:
6610       return "DW_TAG_template_alias";
6611     case DW_TAG_GNU_template_parameter_pack:
6612       return "DW_TAG_GNU_template_parameter_pack";
6613     case DW_TAG_GNU_formal_parameter_pack:
6614       return "DW_TAG_GNU_formal_parameter_pack";
6615     case DW_TAG_MIPS_loop:
6616       return "DW_TAG_MIPS_loop";
6617     case DW_TAG_format_label:
6618       return "DW_TAG_format_label";
6619     case DW_TAG_function_template:
6620       return "DW_TAG_function_template";
6621     case DW_TAG_class_template:
6622       return "DW_TAG_class_template";
6623     case DW_TAG_GNU_BINCL:
6624       return "DW_TAG_GNU_BINCL";
6625     case DW_TAG_GNU_EINCL:
6626       return "DW_TAG_GNU_EINCL";
6627     case DW_TAG_GNU_template_template_param:
6628       return "DW_TAG_GNU_template_template_param";
6629     default:
6630       return "DW_TAG_<unknown>";
6631     }
6632 }
6633
6634 /* Convert a DWARF attribute code into its string name.  */
6635
6636 static const char *
6637 dwarf_attr_name (unsigned int attr)
6638 {
6639   switch (attr)
6640     {
6641     case DW_AT_sibling:
6642       return "DW_AT_sibling";
6643     case DW_AT_location:
6644       return "DW_AT_location";
6645     case DW_AT_name:
6646       return "DW_AT_name";
6647     case DW_AT_ordering:
6648       return "DW_AT_ordering";
6649     case DW_AT_subscr_data:
6650       return "DW_AT_subscr_data";
6651     case DW_AT_byte_size:
6652       return "DW_AT_byte_size";
6653     case DW_AT_bit_offset:
6654       return "DW_AT_bit_offset";
6655     case DW_AT_bit_size:
6656       return "DW_AT_bit_size";
6657     case DW_AT_element_list:
6658       return "DW_AT_element_list";
6659     case DW_AT_stmt_list:
6660       return "DW_AT_stmt_list";
6661     case DW_AT_low_pc:
6662       return "DW_AT_low_pc";
6663     case DW_AT_high_pc:
6664       return "DW_AT_high_pc";
6665     case DW_AT_language:
6666       return "DW_AT_language";
6667     case DW_AT_member:
6668       return "DW_AT_member";
6669     case DW_AT_discr:
6670       return "DW_AT_discr";
6671     case DW_AT_discr_value:
6672       return "DW_AT_discr_value";
6673     case DW_AT_visibility:
6674       return "DW_AT_visibility";
6675     case DW_AT_import:
6676       return "DW_AT_import";
6677     case DW_AT_string_length:
6678       return "DW_AT_string_length";
6679     case DW_AT_common_reference:
6680       return "DW_AT_common_reference";
6681     case DW_AT_comp_dir:
6682       return "DW_AT_comp_dir";
6683     case DW_AT_const_value:
6684       return "DW_AT_const_value";
6685     case DW_AT_containing_type:
6686       return "DW_AT_containing_type";
6687     case DW_AT_default_value:
6688       return "DW_AT_default_value";
6689     case DW_AT_inline:
6690       return "DW_AT_inline";
6691     case DW_AT_is_optional:
6692       return "DW_AT_is_optional";
6693     case DW_AT_lower_bound:
6694       return "DW_AT_lower_bound";
6695     case DW_AT_producer:
6696       return "DW_AT_producer";
6697     case DW_AT_prototyped:
6698       return "DW_AT_prototyped";
6699     case DW_AT_return_addr:
6700       return "DW_AT_return_addr";
6701     case DW_AT_start_scope:
6702       return "DW_AT_start_scope";
6703     case DW_AT_bit_stride:
6704       return "DW_AT_bit_stride";
6705     case DW_AT_upper_bound:
6706       return "DW_AT_upper_bound";
6707     case DW_AT_abstract_origin:
6708       return "DW_AT_abstract_origin";
6709     case DW_AT_accessibility:
6710       return "DW_AT_accessibility";
6711     case DW_AT_address_class:
6712       return "DW_AT_address_class";
6713     case DW_AT_artificial:
6714       return "DW_AT_artificial";
6715     case DW_AT_base_types:
6716       return "DW_AT_base_types";
6717     case DW_AT_calling_convention:
6718       return "DW_AT_calling_convention";
6719     case DW_AT_count:
6720       return "DW_AT_count";
6721     case DW_AT_data_member_location:
6722       return "DW_AT_data_member_location";
6723     case DW_AT_decl_column:
6724       return "DW_AT_decl_column";
6725     case DW_AT_decl_file:
6726       return "DW_AT_decl_file";
6727     case DW_AT_decl_line:
6728       return "DW_AT_decl_line";
6729     case DW_AT_declaration:
6730       return "DW_AT_declaration";
6731     case DW_AT_discr_list:
6732       return "DW_AT_discr_list";
6733     case DW_AT_encoding:
6734       return "DW_AT_encoding";
6735     case DW_AT_external:
6736       return "DW_AT_external";
6737     case DW_AT_explicit:
6738       return "DW_AT_explicit";
6739     case DW_AT_frame_base:
6740       return "DW_AT_frame_base";
6741     case DW_AT_friend:
6742       return "DW_AT_friend";
6743     case DW_AT_identifier_case:
6744       return "DW_AT_identifier_case";
6745     case DW_AT_macro_info:
6746       return "DW_AT_macro_info";
6747     case DW_AT_namelist_items:
6748       return "DW_AT_namelist_items";
6749     case DW_AT_priority:
6750       return "DW_AT_priority";
6751     case DW_AT_segment:
6752       return "DW_AT_segment";
6753     case DW_AT_specification:
6754       return "DW_AT_specification";
6755     case DW_AT_static_link:
6756       return "DW_AT_static_link";
6757     case DW_AT_type:
6758       return "DW_AT_type";
6759     case DW_AT_use_location:
6760       return "DW_AT_use_location";
6761     case DW_AT_variable_parameter:
6762       return "DW_AT_variable_parameter";
6763     case DW_AT_virtuality:
6764       return "DW_AT_virtuality";
6765     case DW_AT_vtable_elem_location:
6766       return "DW_AT_vtable_elem_location";
6767
6768     case DW_AT_allocated:
6769       return "DW_AT_allocated";
6770     case DW_AT_associated:
6771       return "DW_AT_associated";
6772     case DW_AT_data_location:
6773       return "DW_AT_data_location";
6774     case DW_AT_byte_stride:
6775       return "DW_AT_byte_stride";
6776     case DW_AT_entry_pc:
6777       return "DW_AT_entry_pc";
6778     case DW_AT_use_UTF8:
6779       return "DW_AT_use_UTF8";
6780     case DW_AT_extension:
6781       return "DW_AT_extension";
6782     case DW_AT_ranges:
6783       return "DW_AT_ranges";
6784     case DW_AT_trampoline:
6785       return "DW_AT_trampoline";
6786     case DW_AT_call_column:
6787       return "DW_AT_call_column";
6788     case DW_AT_call_file:
6789       return "DW_AT_call_file";
6790     case DW_AT_call_line:
6791       return "DW_AT_call_line";
6792
6793     case DW_AT_signature:
6794       return "DW_AT_signature";
6795     case DW_AT_main_subprogram:
6796       return "DW_AT_main_subprogram";
6797     case DW_AT_data_bit_offset:
6798       return "DW_AT_data_bit_offset";
6799     case DW_AT_const_expr:
6800       return "DW_AT_const_expr";
6801     case DW_AT_enum_class:
6802       return "DW_AT_enum_class";
6803     case DW_AT_linkage_name:
6804       return "DW_AT_linkage_name";
6805
6806     case DW_AT_MIPS_fde:
6807       return "DW_AT_MIPS_fde";
6808     case DW_AT_MIPS_loop_begin:
6809       return "DW_AT_MIPS_loop_begin";
6810     case DW_AT_MIPS_tail_loop_begin:
6811       return "DW_AT_MIPS_tail_loop_begin";
6812     case DW_AT_MIPS_epilog_begin:
6813       return "DW_AT_MIPS_epilog_begin";
6814 #if VMS_DEBUGGING_INFO
6815     case DW_AT_HP_prologue:
6816       return "DW_AT_HP_prologue";
6817 #else
6818     case DW_AT_MIPS_loop_unroll_factor:
6819       return "DW_AT_MIPS_loop_unroll_factor";
6820 #endif
6821     case DW_AT_MIPS_software_pipeline_depth:
6822       return "DW_AT_MIPS_software_pipeline_depth";
6823     case DW_AT_MIPS_linkage_name:
6824       return "DW_AT_MIPS_linkage_name";
6825 #if VMS_DEBUGGING_INFO
6826     case DW_AT_HP_epilogue:
6827       return "DW_AT_HP_epilogue";
6828 #else
6829     case DW_AT_MIPS_stride:
6830       return "DW_AT_MIPS_stride";
6831 #endif
6832     case DW_AT_MIPS_abstract_name:
6833       return "DW_AT_MIPS_abstract_name";
6834     case DW_AT_MIPS_clone_origin:
6835       return "DW_AT_MIPS_clone_origin";
6836     case DW_AT_MIPS_has_inlines:
6837       return "DW_AT_MIPS_has_inlines";
6838
6839     case DW_AT_sf_names:
6840       return "DW_AT_sf_names";
6841     case DW_AT_src_info:
6842       return "DW_AT_src_info";
6843     case DW_AT_mac_info:
6844       return "DW_AT_mac_info";
6845     case DW_AT_src_coords:
6846       return "DW_AT_src_coords";
6847     case DW_AT_body_begin:
6848       return "DW_AT_body_begin";
6849     case DW_AT_body_end:
6850       return "DW_AT_body_end";
6851     case DW_AT_GNU_vector:
6852       return "DW_AT_GNU_vector";
6853     case DW_AT_GNU_guarded_by:
6854       return "DW_AT_GNU_guarded_by";
6855     case DW_AT_GNU_pt_guarded_by:
6856       return "DW_AT_GNU_pt_guarded_by";
6857     case DW_AT_GNU_guarded:
6858       return "DW_AT_GNU_guarded";
6859     case DW_AT_GNU_pt_guarded:
6860       return "DW_AT_GNU_pt_guarded";
6861     case DW_AT_GNU_locks_excluded:
6862       return "DW_AT_GNU_locks_excluded";
6863     case DW_AT_GNU_exclusive_locks_required:
6864       return "DW_AT_GNU_exclusive_locks_required";
6865     case DW_AT_GNU_shared_locks_required:
6866       return "DW_AT_GNU_shared_locks_required";
6867     case DW_AT_GNU_odr_signature:
6868       return "DW_AT_GNU_odr_signature";
6869     case DW_AT_GNU_template_name:
6870       return "DW_AT_GNU_template_name";
6871
6872     case DW_AT_VMS_rtnbeg_pd_address:
6873       return "DW_AT_VMS_rtnbeg_pd_address";
6874
6875     default:
6876       return "DW_AT_<unknown>";
6877     }
6878 }
6879
6880 /* Convert a DWARF value form code into its string name.  */
6881
6882 static const char *
6883 dwarf_form_name (unsigned int form)
6884 {
6885   switch (form)
6886     {
6887     case DW_FORM_addr:
6888       return "DW_FORM_addr";
6889     case DW_FORM_block2:
6890       return "DW_FORM_block2";
6891     case DW_FORM_block4:
6892       return "DW_FORM_block4";
6893     case DW_FORM_data2:
6894       return "DW_FORM_data2";
6895     case DW_FORM_data4:
6896       return "DW_FORM_data4";
6897     case DW_FORM_data8:
6898       return "DW_FORM_data8";
6899     case DW_FORM_string:
6900       return "DW_FORM_string";
6901     case DW_FORM_block:
6902       return "DW_FORM_block";
6903     case DW_FORM_block1:
6904       return "DW_FORM_block1";
6905     case DW_FORM_data1:
6906       return "DW_FORM_data1";
6907     case DW_FORM_flag:
6908       return "DW_FORM_flag";
6909     case DW_FORM_sdata:
6910       return "DW_FORM_sdata";
6911     case DW_FORM_strp:
6912       return "DW_FORM_strp";
6913     case DW_FORM_udata:
6914       return "DW_FORM_udata";
6915     case DW_FORM_ref_addr:
6916       return "DW_FORM_ref_addr";
6917     case DW_FORM_ref1:
6918       return "DW_FORM_ref1";
6919     case DW_FORM_ref2:
6920       return "DW_FORM_ref2";
6921     case DW_FORM_ref4:
6922       return "DW_FORM_ref4";
6923     case DW_FORM_ref8:
6924       return "DW_FORM_ref8";
6925     case DW_FORM_ref_udata:
6926       return "DW_FORM_ref_udata";
6927     case DW_FORM_indirect:
6928       return "DW_FORM_indirect";
6929     case DW_FORM_sec_offset:
6930       return "DW_FORM_sec_offset";
6931     case DW_FORM_exprloc:
6932       return "DW_FORM_exprloc";
6933     case DW_FORM_flag_present:
6934       return "DW_FORM_flag_present";
6935     case DW_FORM_ref_sig8:
6936       return "DW_FORM_ref_sig8";
6937     default:
6938       return "DW_FORM_<unknown>";
6939     }
6940 }
6941 \f
6942 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6943    instance of an inlined instance of a decl which is local to an inline
6944    function, so we have to trace all of the way back through the origin chain
6945    to find out what sort of node actually served as the original seed for the
6946    given block.  */
6947
6948 static tree
6949 decl_ultimate_origin (const_tree decl)
6950 {
6951   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6952     return NULL_TREE;
6953
6954   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6955      nodes in the function to point to themselves; ignore that if
6956      we're trying to output the abstract instance of this function.  */
6957   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6958     return NULL_TREE;
6959
6960   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6961      most distant ancestor, this should never happen.  */
6962   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6963
6964   return DECL_ABSTRACT_ORIGIN (decl);
6965 }
6966
6967 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6968    of a virtual function may refer to a base class, so we check the 'this'
6969    parameter.  */
6970
6971 static tree
6972 decl_class_context (tree decl)
6973 {
6974   tree context = NULL_TREE;
6975
6976   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6977     context = DECL_CONTEXT (decl);
6978   else
6979     context = TYPE_MAIN_VARIANT
6980       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6981
6982   if (context && !TYPE_P (context))
6983     context = NULL_TREE;
6984
6985   return context;
6986 }
6987 \f
6988 /* Add an attribute/value pair to a DIE.  */
6989
6990 static inline void
6991 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6992 {
6993   /* Maybe this should be an assert?  */
6994   if (die == NULL)
6995     return;
6996
6997   if (die->die_attr == NULL)
6998     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6999   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7000 }
7001
7002 static inline enum dw_val_class
7003 AT_class (dw_attr_ref a)
7004 {
7005   return a->dw_attr_val.val_class;
7006 }
7007
7008 /* Add a flag value attribute to a DIE.  */
7009
7010 static inline void
7011 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7012 {
7013   dw_attr_node attr;
7014
7015   attr.dw_attr = attr_kind;
7016   attr.dw_attr_val.val_class = dw_val_class_flag;
7017   attr.dw_attr_val.v.val_flag = flag;
7018   add_dwarf_attr (die, &attr);
7019 }
7020
7021 static inline unsigned
7022 AT_flag (dw_attr_ref a)
7023 {
7024   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7025   return a->dw_attr_val.v.val_flag;
7026 }
7027
7028 /* Add a signed integer attribute value to a DIE.  */
7029
7030 static inline void
7031 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7032 {
7033   dw_attr_node attr;
7034
7035   attr.dw_attr = attr_kind;
7036   attr.dw_attr_val.val_class = dw_val_class_const;
7037   attr.dw_attr_val.v.val_int = int_val;
7038   add_dwarf_attr (die, &attr);
7039 }
7040
7041 static inline HOST_WIDE_INT
7042 AT_int (dw_attr_ref a)
7043 {
7044   gcc_assert (a && AT_class (a) == dw_val_class_const);
7045   return a->dw_attr_val.v.val_int;
7046 }
7047
7048 /* Add an unsigned integer attribute value to a DIE.  */
7049
7050 static inline void
7051 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7052                  unsigned HOST_WIDE_INT unsigned_val)
7053 {
7054   dw_attr_node attr;
7055
7056   attr.dw_attr = attr_kind;
7057   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7058   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7059   add_dwarf_attr (die, &attr);
7060 }
7061
7062 static inline unsigned HOST_WIDE_INT
7063 AT_unsigned (dw_attr_ref a)
7064 {
7065   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7066   return a->dw_attr_val.v.val_unsigned;
7067 }
7068
7069 /* Add an unsigned double integer attribute value to a DIE.  */
7070
7071 static inline void
7072 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7073                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7074 {
7075   dw_attr_node attr;
7076
7077   attr.dw_attr = attr_kind;
7078   attr.dw_attr_val.val_class = dw_val_class_const_double;
7079   attr.dw_attr_val.v.val_double.high = high;
7080   attr.dw_attr_val.v.val_double.low = low;
7081   add_dwarf_attr (die, &attr);
7082 }
7083
7084 /* Add a floating point attribute value to a DIE and return it.  */
7085
7086 static inline void
7087 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7088             unsigned int length, unsigned int elt_size, unsigned char *array)
7089 {
7090   dw_attr_node attr;
7091
7092   attr.dw_attr = attr_kind;
7093   attr.dw_attr_val.val_class = dw_val_class_vec;
7094   attr.dw_attr_val.v.val_vec.length = length;
7095   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7096   attr.dw_attr_val.v.val_vec.array = array;
7097   add_dwarf_attr (die, &attr);
7098 }
7099
7100 /* Add an 8-byte data attribute value to a DIE.  */
7101
7102 static inline void
7103 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7104               unsigned char data8[8])
7105 {
7106   dw_attr_node attr;
7107
7108   attr.dw_attr = attr_kind;
7109   attr.dw_attr_val.val_class = dw_val_class_data8;
7110   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7111   add_dwarf_attr (die, &attr);
7112 }
7113
7114 /* Hash and equality functions for debug_str_hash.  */
7115
7116 static hashval_t
7117 debug_str_do_hash (const void *x)
7118 {
7119   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7120 }
7121
7122 static int
7123 debug_str_eq (const void *x1, const void *x2)
7124 {
7125   return strcmp ((((const struct indirect_string_node *)x1)->str),
7126                  (const char *)x2) == 0;
7127 }
7128
7129 /* Add STR to the indirect string hash table.  */
7130
7131 static struct indirect_string_node *
7132 find_AT_string (const char *str)
7133 {
7134   struct indirect_string_node *node;
7135   void **slot;
7136
7137   if (! debug_str_hash)
7138     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7139                                       debug_str_eq, NULL);
7140
7141   slot = htab_find_slot_with_hash (debug_str_hash, str,
7142                                    htab_hash_string (str), INSERT);
7143   if (*slot == NULL)
7144     {
7145       node = ggc_alloc_cleared_indirect_string_node ();
7146       node->str = ggc_strdup (str);
7147       *slot = node;
7148     }
7149   else
7150     node = (struct indirect_string_node *) *slot;
7151
7152   node->refcount++;
7153   return node;
7154 }
7155
7156 /* Add a string attribute value to a DIE.  */
7157
7158 static inline void
7159 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7160 {
7161   dw_attr_node attr;
7162   struct indirect_string_node *node;
7163
7164   node = find_AT_string (str);
7165
7166   attr.dw_attr = attr_kind;
7167   attr.dw_attr_val.val_class = dw_val_class_str;
7168   attr.dw_attr_val.v.val_str = node;
7169   add_dwarf_attr (die, &attr);
7170 }
7171
7172 /* Create a label for an indirect string node, ensuring it is going to
7173    be output, unless its reference count goes down to zero.  */
7174
7175 static inline void
7176 gen_label_for_indirect_string (struct indirect_string_node *node)
7177 {
7178   char label[32];
7179
7180   if (node->label)
7181     return;
7182
7183   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7184   ++dw2_string_counter;
7185   node->label = xstrdup (label);
7186 }
7187
7188 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7189    debug string STR.  */
7190
7191 static inline rtx
7192 get_debug_string_label (const char *str)
7193 {
7194   struct indirect_string_node *node = find_AT_string (str);
7195
7196   debug_str_hash_forced = true;
7197
7198   gen_label_for_indirect_string (node);
7199
7200   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7201 }
7202
7203 static inline const char *
7204 AT_string (dw_attr_ref a)
7205 {
7206   gcc_assert (a && AT_class (a) == dw_val_class_str);
7207   return a->dw_attr_val.v.val_str->str;
7208 }
7209
7210 /* Find out whether a string should be output inline in DIE
7211    or out-of-line in .debug_str section.  */
7212
7213 static enum dwarf_form
7214 AT_string_form (dw_attr_ref a)
7215 {
7216   struct indirect_string_node *node;
7217   unsigned int len;
7218
7219   gcc_assert (a && AT_class (a) == dw_val_class_str);
7220
7221   node = a->dw_attr_val.v.val_str;
7222   if (node->form)
7223     return node->form;
7224
7225   len = strlen (node->str) + 1;
7226
7227   /* If the string is shorter or equal to the size of the reference, it is
7228      always better to put it inline.  */
7229   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7230     return node->form = DW_FORM_string;
7231
7232   /* If we cannot expect the linker to merge strings in .debug_str
7233      section, only put it into .debug_str if it is worth even in this
7234      single module.  */
7235   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7236       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7237       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7238     return node->form = DW_FORM_string;
7239
7240   gen_label_for_indirect_string (node);
7241
7242   return node->form = DW_FORM_strp;
7243 }
7244
7245 /* Add a DIE reference attribute value to a DIE.  */
7246
7247 static inline void
7248 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7249 {
7250   dw_attr_node attr;
7251
7252   attr.dw_attr = attr_kind;
7253   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7254   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7255   attr.dw_attr_val.v.val_die_ref.external = 0;
7256   add_dwarf_attr (die, &attr);
7257 }
7258
7259 /* Add an AT_specification attribute to a DIE, and also make the back
7260    pointer from the specification to the definition.  */
7261
7262 static inline void
7263 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7264 {
7265   add_AT_die_ref (die, DW_AT_specification, targ_die);
7266   gcc_assert (!targ_die->die_definition);
7267   targ_die->die_definition = die;
7268 }
7269
7270 static inline dw_die_ref
7271 AT_ref (dw_attr_ref a)
7272 {
7273   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7274   return a->dw_attr_val.v.val_die_ref.die;
7275 }
7276
7277 static inline int
7278 AT_ref_external (dw_attr_ref a)
7279 {
7280   if (a && AT_class (a) == dw_val_class_die_ref)
7281     return a->dw_attr_val.v.val_die_ref.external;
7282
7283   return 0;
7284 }
7285
7286 static inline void
7287 set_AT_ref_external (dw_attr_ref a, int i)
7288 {
7289   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7290   a->dw_attr_val.v.val_die_ref.external = i;
7291 }
7292
7293 /* Add an FDE reference attribute value to a DIE.  */
7294
7295 static inline void
7296 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7297 {
7298   dw_attr_node attr;
7299
7300   attr.dw_attr = attr_kind;
7301   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7302   attr.dw_attr_val.v.val_fde_index = targ_fde;
7303   add_dwarf_attr (die, &attr);
7304 }
7305
7306 /* Add a location description attribute value to a DIE.  */
7307
7308 static inline void
7309 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7310 {
7311   dw_attr_node attr;
7312
7313   attr.dw_attr = attr_kind;
7314   attr.dw_attr_val.val_class = dw_val_class_loc;
7315   attr.dw_attr_val.v.val_loc = loc;
7316   add_dwarf_attr (die, &attr);
7317 }
7318
7319 static inline dw_loc_descr_ref
7320 AT_loc (dw_attr_ref a)
7321 {
7322   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7323   return a->dw_attr_val.v.val_loc;
7324 }
7325
7326 static inline void
7327 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7328 {
7329   dw_attr_node attr;
7330
7331   attr.dw_attr = attr_kind;
7332   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7333   attr.dw_attr_val.v.val_loc_list = loc_list;
7334   add_dwarf_attr (die, &attr);
7335   have_location_lists = true;
7336 }
7337
7338 static inline dw_loc_list_ref
7339 AT_loc_list (dw_attr_ref a)
7340 {
7341   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7342   return a->dw_attr_val.v.val_loc_list;
7343 }
7344
7345 static inline dw_loc_list_ref *
7346 AT_loc_list_ptr (dw_attr_ref a)
7347 {
7348   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7349   return &a->dw_attr_val.v.val_loc_list;
7350 }
7351
7352 /* Add an address constant attribute value to a DIE.  */
7353
7354 static inline void
7355 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7356 {
7357   dw_attr_node attr;
7358
7359   attr.dw_attr = attr_kind;
7360   attr.dw_attr_val.val_class = dw_val_class_addr;
7361   attr.dw_attr_val.v.val_addr = addr;
7362   add_dwarf_attr (die, &attr);
7363 }
7364
7365 /* Get the RTX from to an address DIE attribute.  */
7366
7367 static inline rtx
7368 AT_addr (dw_attr_ref a)
7369 {
7370   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7371   return a->dw_attr_val.v.val_addr;
7372 }
7373
7374 /* Add a file attribute value to a DIE.  */
7375
7376 static inline void
7377 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7378              struct dwarf_file_data *fd)
7379 {
7380   dw_attr_node attr;
7381
7382   attr.dw_attr = attr_kind;
7383   attr.dw_attr_val.val_class = dw_val_class_file;
7384   attr.dw_attr_val.v.val_file = fd;
7385   add_dwarf_attr (die, &attr);
7386 }
7387
7388 /* Get the dwarf_file_data from a file DIE attribute.  */
7389
7390 static inline struct dwarf_file_data *
7391 AT_file (dw_attr_ref a)
7392 {
7393   gcc_assert (a && AT_class (a) == dw_val_class_file);
7394   return a->dw_attr_val.v.val_file;
7395 }
7396
7397 /* Add a vms delta attribute value to a DIE.  */
7398
7399 static inline void
7400 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7401                   const char *lbl1, const char *lbl2)
7402 {
7403   dw_attr_node attr;
7404
7405   attr.dw_attr = attr_kind;
7406   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7407   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7408   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7409   add_dwarf_attr (die, &attr);
7410 }
7411
7412 /* Add a label identifier attribute value to a DIE.  */
7413
7414 static inline void
7415 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7416 {
7417   dw_attr_node attr;
7418
7419   attr.dw_attr = attr_kind;
7420   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7421   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7422   add_dwarf_attr (die, &attr);
7423 }
7424
7425 /* Add a section offset attribute value to a DIE, an offset into the
7426    debug_line section.  */
7427
7428 static inline void
7429 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7430                 const char *label)
7431 {
7432   dw_attr_node attr;
7433
7434   attr.dw_attr = attr_kind;
7435   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7436   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7437   add_dwarf_attr (die, &attr);
7438 }
7439
7440 /* Add a section offset attribute value to a DIE, an offset into the
7441    debug_macinfo section.  */
7442
7443 static inline void
7444 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7445                const char *label)
7446 {
7447   dw_attr_node attr;
7448
7449   attr.dw_attr = attr_kind;
7450   attr.dw_attr_val.val_class = dw_val_class_macptr;
7451   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7452   add_dwarf_attr (die, &attr);
7453 }
7454
7455 /* Add an offset attribute value to a DIE.  */
7456
7457 static inline void
7458 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7459                unsigned HOST_WIDE_INT offset)
7460 {
7461   dw_attr_node attr;
7462
7463   attr.dw_attr = attr_kind;
7464   attr.dw_attr_val.val_class = dw_val_class_offset;
7465   attr.dw_attr_val.v.val_offset = offset;
7466   add_dwarf_attr (die, &attr);
7467 }
7468
7469 /* Add an range_list attribute value to a DIE.  */
7470
7471 static void
7472 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7473                    long unsigned int offset)
7474 {
7475   dw_attr_node attr;
7476
7477   attr.dw_attr = attr_kind;
7478   attr.dw_attr_val.val_class = dw_val_class_range_list;
7479   attr.dw_attr_val.v.val_offset = offset;
7480   add_dwarf_attr (die, &attr);
7481 }
7482
7483 /* Return the start label of a delta attribute.  */
7484
7485 static inline const char *
7486 AT_vms_delta1 (dw_attr_ref a)
7487 {
7488   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7489   return a->dw_attr_val.v.val_vms_delta.lbl1;
7490 }
7491
7492 /* Return the end label of a delta attribute.  */
7493
7494 static inline const char *
7495 AT_vms_delta2 (dw_attr_ref a)
7496 {
7497   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7498   return a->dw_attr_val.v.val_vms_delta.lbl2;
7499 }
7500
7501 static inline const char *
7502 AT_lbl (dw_attr_ref a)
7503 {
7504   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7505                     || AT_class (a) == dw_val_class_lineptr
7506                     || AT_class (a) == dw_val_class_macptr));
7507   return a->dw_attr_val.v.val_lbl_id;
7508 }
7509
7510 /* Get the attribute of type attr_kind.  */
7511
7512 static dw_attr_ref
7513 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7514 {
7515   dw_attr_ref a;
7516   unsigned ix;
7517   dw_die_ref spec = NULL;
7518
7519   if (! die)
7520     return NULL;
7521
7522   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7523     if (a->dw_attr == attr_kind)
7524       return a;
7525     else if (a->dw_attr == DW_AT_specification
7526              || a->dw_attr == DW_AT_abstract_origin)
7527       spec = AT_ref (a);
7528
7529   if (spec)
7530     return get_AT (spec, attr_kind);
7531
7532   return NULL;
7533 }
7534
7535 /* Return the "low pc" attribute value, typically associated with a subprogram
7536    DIE.  Return null if the "low pc" attribute is either not present, or if it
7537    cannot be represented as an assembler label identifier.  */
7538
7539 static inline const char *
7540 get_AT_low_pc (dw_die_ref die)
7541 {
7542   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7543
7544   return a ? AT_lbl (a) : NULL;
7545 }
7546
7547 /* Return the "high pc" attribute value, typically associated with a subprogram
7548    DIE.  Return null if the "high pc" attribute is either not present, or if it
7549    cannot be represented as an assembler label identifier.  */
7550
7551 static inline const char *
7552 get_AT_hi_pc (dw_die_ref die)
7553 {
7554   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7555
7556   return a ? AT_lbl (a) : NULL;
7557 }
7558
7559 /* Return the value of the string attribute designated by ATTR_KIND, or
7560    NULL if it is not present.  */
7561
7562 static inline const char *
7563 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7564 {
7565   dw_attr_ref a = get_AT (die, attr_kind);
7566
7567   return a ? AT_string (a) : NULL;
7568 }
7569
7570 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7571    if it is not present.  */
7572
7573 static inline int
7574 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7575 {
7576   dw_attr_ref a = get_AT (die, attr_kind);
7577
7578   return a ? AT_flag (a) : 0;
7579 }
7580
7581 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7582    if it is not present.  */
7583
7584 static inline unsigned
7585 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7586 {
7587   dw_attr_ref a = get_AT (die, attr_kind);
7588
7589   return a ? AT_unsigned (a) : 0;
7590 }
7591
7592 static inline dw_die_ref
7593 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7594 {
7595   dw_attr_ref a = get_AT (die, attr_kind);
7596
7597   return a ? AT_ref (a) : NULL;
7598 }
7599
7600 static inline struct dwarf_file_data *
7601 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7602 {
7603   dw_attr_ref a = get_AT (die, attr_kind);
7604
7605   return a ? AT_file (a) : NULL;
7606 }
7607
7608 /* Return TRUE if the language is C++.  */
7609
7610 static inline bool
7611 is_cxx (void)
7612 {
7613   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7614
7615   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7616 }
7617
7618 /* Return TRUE if the language is Fortran.  */
7619
7620 static inline bool
7621 is_fortran (void)
7622 {
7623   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7624
7625   return (lang == DW_LANG_Fortran77
7626           || lang == DW_LANG_Fortran90
7627           || lang == DW_LANG_Fortran95);
7628 }
7629
7630 /* Return TRUE if the language is Ada.  */
7631
7632 static inline bool
7633 is_ada (void)
7634 {
7635   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7636
7637   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7638 }
7639
7640 /* Remove the specified attribute if present.  */
7641
7642 static void
7643 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7644 {
7645   dw_attr_ref a;
7646   unsigned ix;
7647
7648   if (! die)
7649     return;
7650
7651   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7652     if (a->dw_attr == attr_kind)
7653       {
7654         if (AT_class (a) == dw_val_class_str)
7655           if (a->dw_attr_val.v.val_str->refcount)
7656             a->dw_attr_val.v.val_str->refcount--;
7657
7658         /* VEC_ordered_remove should help reduce the number of abbrevs
7659            that are needed.  */
7660         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7661         return;
7662       }
7663 }
7664
7665 /* Remove CHILD from its parent.  PREV must have the property that
7666    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7667
7668 static void
7669 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7670 {
7671   gcc_assert (child->die_parent == prev->die_parent);
7672   gcc_assert (prev->die_sib == child);
7673   if (prev == child)
7674     {
7675       gcc_assert (child->die_parent->die_child == child);
7676       prev = NULL;
7677     }
7678   else
7679     prev->die_sib = child->die_sib;
7680   if (child->die_parent->die_child == child)
7681     child->die_parent->die_child = prev;
7682 }
7683
7684 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7685    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7686
7687 static void
7688 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7689 {
7690   dw_die_ref parent = old_child->die_parent;
7691
7692   gcc_assert (parent == prev->die_parent);
7693   gcc_assert (prev->die_sib == old_child);
7694
7695   new_child->die_parent = parent;
7696   if (prev == old_child)
7697     {
7698       gcc_assert (parent->die_child == old_child);
7699       new_child->die_sib = new_child;
7700     }
7701   else
7702     {
7703       prev->die_sib = new_child;
7704       new_child->die_sib = old_child->die_sib;
7705     }
7706   if (old_child->die_parent->die_child == old_child)
7707     old_child->die_parent->die_child = new_child;
7708 }
7709
7710 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7711
7712 static void
7713 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7714 {
7715   dw_die_ref c;
7716   new_parent->die_child = old_parent->die_child;
7717   old_parent->die_child = NULL;
7718   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7719 }
7720
7721 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7722    matches TAG.  */
7723
7724 static void
7725 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7726 {
7727   dw_die_ref c;
7728
7729   c = die->die_child;
7730   if (c) do {
7731     dw_die_ref prev = c;
7732     c = c->die_sib;
7733     while (c->die_tag == tag)
7734       {
7735         remove_child_with_prev (c, prev);
7736         /* Might have removed every child.  */
7737         if (c == c->die_sib)
7738           return;
7739         c = c->die_sib;
7740       }
7741   } while (c != die->die_child);
7742 }
7743
7744 /* Add a CHILD_DIE as the last child of DIE.  */
7745
7746 static void
7747 add_child_die (dw_die_ref die, dw_die_ref child_die)
7748 {
7749   /* FIXME this should probably be an assert.  */
7750   if (! die || ! child_die)
7751     return;
7752   gcc_assert (die != child_die);
7753
7754   child_die->die_parent = die;
7755   if (die->die_child)
7756     {
7757       child_die->die_sib = die->die_child->die_sib;
7758       die->die_child->die_sib = child_die;
7759     }
7760   else
7761     child_die->die_sib = child_die;
7762   die->die_child = child_die;
7763 }
7764
7765 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7766    is the specification, to the end of PARENT's list of children.
7767    This is done by removing and re-adding it.  */
7768
7769 static void
7770 splice_child_die (dw_die_ref parent, dw_die_ref child)
7771 {
7772   dw_die_ref p;
7773
7774   /* We want the declaration DIE from inside the class, not the
7775      specification DIE at toplevel.  */
7776   if (child->die_parent != parent)
7777     {
7778       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7779
7780       if (tmp)
7781         child = tmp;
7782     }
7783
7784   gcc_assert (child->die_parent == parent
7785               || (child->die_parent
7786                   == get_AT_ref (parent, DW_AT_specification)));
7787
7788   for (p = child->die_parent->die_child; ; p = p->die_sib)
7789     if (p->die_sib == child)
7790       {
7791         remove_child_with_prev (child, p);
7792         break;
7793       }
7794
7795   add_child_die (parent, child);
7796 }
7797
7798 /* Return a pointer to a newly created DIE node.  */
7799
7800 static inline dw_die_ref
7801 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7802 {
7803   dw_die_ref die = ggc_alloc_cleared_die_node ();
7804
7805   die->die_tag = tag_value;
7806
7807   if (parent_die != NULL)
7808     add_child_die (parent_die, die);
7809   else
7810     {
7811       limbo_die_node *limbo_node;
7812
7813       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7814       limbo_node->die = die;
7815       limbo_node->created_for = t;
7816       limbo_node->next = limbo_die_list;
7817       limbo_die_list = limbo_node;
7818     }
7819
7820   return die;
7821 }
7822
7823 /* Return the DIE associated with the given type specifier.  */
7824
7825 static inline dw_die_ref
7826 lookup_type_die (tree type)
7827 {
7828   return TYPE_SYMTAB_DIE (type);
7829 }
7830
7831 /* Equate a DIE to a given type specifier.  */
7832
7833 static inline void
7834 equate_type_number_to_die (tree type, dw_die_ref type_die)
7835 {
7836   TYPE_SYMTAB_DIE (type) = type_die;
7837 }
7838
7839 /* Returns a hash value for X (which really is a die_struct).  */
7840
7841 static hashval_t
7842 decl_die_table_hash (const void *x)
7843 {
7844   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7845 }
7846
7847 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7848
7849 static int
7850 decl_die_table_eq (const void *x, const void *y)
7851 {
7852   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7853 }
7854
7855 /* Return the DIE associated with a given declaration.  */
7856
7857 static inline dw_die_ref
7858 lookup_decl_die (tree decl)
7859 {
7860   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7861 }
7862
7863 /* Returns a hash value for X (which really is a var_loc_list).  */
7864
7865 static hashval_t
7866 decl_loc_table_hash (const void *x)
7867 {
7868   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7869 }
7870
7871 /* Return nonzero if decl_id of var_loc_list X is the same as
7872    UID of decl *Y.  */
7873
7874 static int
7875 decl_loc_table_eq (const void *x, const void *y)
7876 {
7877   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7878 }
7879
7880 /* Return the var_loc list associated with a given declaration.  */
7881
7882 static inline var_loc_list *
7883 lookup_decl_loc (const_tree decl)
7884 {
7885   if (!decl_loc_table)
7886     return NULL;
7887   return (var_loc_list *)
7888     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7889 }
7890
7891 /* Equate a DIE to a particular declaration.  */
7892
7893 static void
7894 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7895 {
7896   unsigned int decl_id = DECL_UID (decl);
7897   void **slot;
7898
7899   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7900   *slot = decl_die;
7901   decl_die->decl_id = decl_id;
7902 }
7903
7904 /* Return how many bits covers PIECE EXPR_LIST.  */
7905
7906 static int
7907 decl_piece_bitsize (rtx piece)
7908 {
7909   int ret = (int) GET_MODE (piece);
7910   if (ret)
7911     return ret;
7912   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7913               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7914   return INTVAL (XEXP (XEXP (piece, 0), 0));
7915 }
7916
7917 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7918
7919 static rtx *
7920 decl_piece_varloc_ptr (rtx piece)
7921 {
7922   if ((int) GET_MODE (piece))
7923     return &XEXP (piece, 0);
7924   else
7925     return &XEXP (XEXP (piece, 0), 1);
7926 }
7927
7928 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7929    Next is the chain of following piece nodes.  */
7930
7931 static rtx
7932 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7933 {
7934   if (bitsize <= (int) MAX_MACHINE_MODE)
7935     return alloc_EXPR_LIST (bitsize, loc_note, next);
7936   else
7937     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7938                                                GEN_INT (bitsize),
7939                                                loc_note), next);
7940 }
7941
7942 /* Return rtx that should be stored into loc field for
7943    LOC_NOTE and BITPOS/BITSIZE.  */
7944
7945 static rtx
7946 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7947                       HOST_WIDE_INT bitsize)
7948 {
7949   if (bitsize != -1)
7950     {
7951       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7952       if (bitpos != 0)
7953         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7954     }
7955   return loc_note;
7956 }
7957
7958 /* This function either modifies location piece list *DEST in
7959    place (if SRC and INNER is NULL), or copies location piece list
7960    *SRC to *DEST while modifying it.  Location BITPOS is modified
7961    to contain LOC_NOTE, any pieces overlapping it are removed resp.
7962    not copied and if needed some padding around it is added.
7963    When modifying in place, DEST should point to EXPR_LIST where
7964    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
7965    to the start of the whole list and INNER points to the EXPR_LIST
7966    where earlier pieces cover PIECE_BITPOS bits.  */
7967
7968 static void
7969 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
7970                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
7971                    HOST_WIDE_INT bitsize, rtx loc_note)
7972 {
7973   int diff;
7974   bool copy = inner != NULL;
7975
7976   if (copy)
7977     {
7978       /* First copy all nodes preceeding the current bitpos.  */
7979       while (src != inner)
7980         {
7981           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
7982                                    decl_piece_bitsize (*src), NULL_RTX);
7983           dest = &XEXP (*dest, 1);
7984           src = &XEXP (*src, 1);
7985         }
7986     }
7987   /* Add padding if needed.  */
7988   if (bitpos != piece_bitpos)
7989     {
7990       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
7991                                copy ? NULL_RTX : *dest);
7992       dest = &XEXP (*dest, 1);
7993     }
7994   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
7995     {
7996       gcc_assert (!copy);
7997       /* A piece with correct bitpos and bitsize already exist,
7998          just update the location for it and return.  */
7999       *decl_piece_varloc_ptr (*dest) = loc_note;
8000       return;
8001     }
8002   /* Add the piece that changed.  */
8003   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8004   dest = &XEXP (*dest, 1);
8005   /* Skip over pieces that overlap it.  */
8006   diff = bitpos - piece_bitpos + bitsize;
8007   if (!copy)
8008     src = dest;
8009   while (diff > 0 && *src)
8010     {
8011       rtx piece = *src;
8012       diff -= decl_piece_bitsize (piece);
8013       if (copy)
8014         src = &XEXP (piece, 1);
8015       else
8016         {
8017           *src = XEXP (piece, 1);
8018           free_EXPR_LIST_node (piece);
8019         }
8020     }
8021   /* Add padding if needed.  */
8022   if (diff < 0 && *src)
8023     {
8024       if (!copy)
8025         dest = src;
8026       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8027       dest = &XEXP (*dest, 1);
8028     }
8029   if (!copy)
8030     return;
8031   /* Finally copy all nodes following it.  */
8032   while (*src)
8033     {
8034       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8035                                decl_piece_bitsize (*src), NULL_RTX);
8036       dest = &XEXP (*dest, 1);
8037       src = &XEXP (*src, 1);
8038     }
8039 }
8040
8041 /* Add a variable location node to the linked list for DECL.  */
8042
8043 static struct var_loc_node *
8044 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8045 {
8046   unsigned int decl_id;
8047   var_loc_list *temp;
8048   void **slot;
8049   struct var_loc_node *loc = NULL;
8050   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8051
8052   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8053     {
8054       tree realdecl = DECL_DEBUG_EXPR (decl);
8055       if (realdecl && handled_component_p (realdecl))
8056         {
8057           HOST_WIDE_INT maxsize;
8058           tree innerdecl;
8059           innerdecl
8060             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8061           if (!DECL_P (innerdecl)
8062               || DECL_IGNORED_P (innerdecl)
8063               || TREE_STATIC (innerdecl)
8064               || bitsize <= 0
8065               || bitpos + bitsize > 256
8066               || bitsize != maxsize)
8067             return NULL;
8068           decl = innerdecl;
8069         }
8070     }
8071
8072   decl_id = DECL_UID (decl);
8073   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8074   if (*slot == NULL)
8075     {
8076       temp = ggc_alloc_cleared_var_loc_list ();
8077       temp->decl_id = decl_id;
8078       *slot = temp;
8079     }
8080   else
8081     temp = (var_loc_list *) *slot;
8082
8083   if (temp->last)
8084     {
8085       struct var_loc_node *last = temp->last, *unused = NULL;
8086       rtx *piece_loc = NULL, last_loc_note;
8087       int piece_bitpos = 0;
8088       if (last->next)
8089         {
8090           last = last->next;
8091           gcc_assert (last->next == NULL);
8092         }
8093       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8094         {
8095           piece_loc = &last->loc;
8096           do
8097             {
8098               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8099               if (piece_bitpos + cur_bitsize > bitpos)
8100                 break;
8101               piece_bitpos += cur_bitsize;
8102               piece_loc = &XEXP (*piece_loc, 1);
8103             }
8104           while (*piece_loc);
8105         }
8106       /* TEMP->LAST here is either pointer to the last but one or
8107          last element in the chained list, LAST is pointer to the
8108          last element.  */
8109       if (label && strcmp (last->label, label) == 0)
8110         {
8111           /* For SRA optimized variables if there weren't any real
8112              insns since last note, just modify the last node.  */
8113           if (piece_loc != NULL)
8114             {
8115               adjust_piece_list (piece_loc, NULL, NULL,
8116                                  bitpos, piece_bitpos, bitsize, loc_note);
8117               return NULL;
8118             }
8119           /* If the last note doesn't cover any instructions, remove it.  */
8120           if (temp->last != last)
8121             {
8122               temp->last->next = NULL;
8123               unused = last;
8124               last = temp->last;
8125               gcc_assert (strcmp (last->label, label) != 0);
8126             }
8127           else
8128             {
8129               gcc_assert (temp->first == temp->last);
8130               memset (temp->last, '\0', sizeof (*temp->last));
8131               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8132               return temp->last;
8133             }
8134         }
8135       if (bitsize == -1 && NOTE_P (last->loc))
8136         last_loc_note = last->loc;
8137       else if (piece_loc != NULL
8138                && *piece_loc != NULL_RTX
8139                && piece_bitpos == bitpos
8140                && decl_piece_bitsize (*piece_loc) == bitsize)
8141         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8142       else
8143         last_loc_note = NULL_RTX;
8144       /* If the current location is the same as the end of the list,
8145          and either both or neither of the locations is uninitialized,
8146          we have nothing to do.  */
8147       if (last_loc_note == NULL_RTX
8148           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8149                             NOTE_VAR_LOCATION_LOC (loc_note)))
8150           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8151                != NOTE_VAR_LOCATION_STATUS (loc_note))
8152               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8153                    == VAR_INIT_STATUS_UNINITIALIZED)
8154                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8155                       == VAR_INIT_STATUS_UNINITIALIZED))))
8156         {
8157           /* Add LOC to the end of list and update LAST.  If the last
8158              element of the list has been removed above, reuse its
8159              memory for the new node, otherwise allocate a new one.  */
8160           if (unused)
8161             {
8162               loc = unused;
8163               memset (loc, '\0', sizeof (*loc));
8164             }
8165           else
8166             loc = ggc_alloc_cleared_var_loc_node ();
8167           if (bitsize == -1 || piece_loc == NULL)
8168             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8169           else
8170             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8171                                bitpos, piece_bitpos, bitsize, loc_note);
8172           last->next = loc;
8173           /* Ensure TEMP->LAST will point either to the new last but one
8174              element of the chain, or to the last element in it.  */
8175           if (last != temp->last)
8176             temp->last = last;
8177         }
8178       else if (unused)
8179         ggc_free (unused);
8180     }
8181   else
8182     {
8183       loc = ggc_alloc_cleared_var_loc_node ();
8184       temp->first = loc;
8185       temp->last = loc;
8186       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8187     }
8188   return loc;
8189 }
8190 \f
8191 /* Keep track of the number of spaces used to indent the
8192    output of the debugging routines that print the structure of
8193    the DIE internal representation.  */
8194 static int print_indent;
8195
8196 /* Indent the line the number of spaces given by print_indent.  */
8197
8198 static inline void
8199 print_spaces (FILE *outfile)
8200 {
8201   fprintf (outfile, "%*s", print_indent, "");
8202 }
8203
8204 /* Print a type signature in hex.  */
8205
8206 static inline void
8207 print_signature (FILE *outfile, char *sig)
8208 {
8209   int i;
8210
8211   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8212     fprintf (outfile, "%02x", sig[i] & 0xff);
8213 }
8214
8215 /* Print the information associated with a given DIE, and its children.
8216    This routine is a debugging aid only.  */
8217
8218 static void
8219 print_die (dw_die_ref die, FILE *outfile)
8220 {
8221   dw_attr_ref a;
8222   dw_die_ref c;
8223   unsigned ix;
8224
8225   print_spaces (outfile);
8226   fprintf (outfile, "DIE %4ld: %s\n",
8227            die->die_offset, dwarf_tag_name (die->die_tag));
8228   print_spaces (outfile);
8229   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8230   fprintf (outfile, " offset: %ld\n", die->die_offset);
8231   if (dwarf_version >= 4 && die->die_id.die_type_node)
8232     {
8233       print_spaces (outfile);
8234       fprintf (outfile, "  signature: ");
8235       print_signature (outfile, die->die_id.die_type_node->signature);
8236       fprintf (outfile, "\n");
8237     }
8238
8239   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8240     {
8241       print_spaces (outfile);
8242       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8243
8244       switch (AT_class (a))
8245         {
8246         case dw_val_class_addr:
8247           fprintf (outfile, "address");
8248           break;
8249         case dw_val_class_offset:
8250           fprintf (outfile, "offset");
8251           break;
8252         case dw_val_class_loc:
8253           fprintf (outfile, "location descriptor");
8254           break;
8255         case dw_val_class_loc_list:
8256           fprintf (outfile, "location list -> label:%s",
8257                    AT_loc_list (a)->ll_symbol);
8258           break;
8259         case dw_val_class_range_list:
8260           fprintf (outfile, "range list");
8261           break;
8262         case dw_val_class_const:
8263           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8264           break;
8265         case dw_val_class_unsigned_const:
8266           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8267           break;
8268         case dw_val_class_const_double:
8269           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8270                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8271                    a->dw_attr_val.v.val_double.high,
8272                    a->dw_attr_val.v.val_double.low);
8273           break;
8274         case dw_val_class_vec:
8275           fprintf (outfile, "floating-point or vector constant");
8276           break;
8277         case dw_val_class_flag:
8278           fprintf (outfile, "%u", AT_flag (a));
8279           break;
8280         case dw_val_class_die_ref:
8281           if (AT_ref (a) != NULL)
8282             {
8283               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8284                 {
8285                   fprintf (outfile, "die -> signature: ");
8286                   print_signature (outfile,
8287                                    AT_ref (a)->die_id.die_type_node->signature);
8288                 }
8289               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8290                 fprintf (outfile, "die -> label: %s",
8291                          AT_ref (a)->die_id.die_symbol);
8292               else
8293                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8294             }
8295           else
8296             fprintf (outfile, "die -> <null>");
8297           break;
8298         case dw_val_class_vms_delta:
8299           fprintf (outfile, "delta: @slotcount(%s-%s)",
8300                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8301           break;
8302         case dw_val_class_lbl_id:
8303         case dw_val_class_lineptr:
8304         case dw_val_class_macptr:
8305           fprintf (outfile, "label: %s", AT_lbl (a));
8306           break;
8307         case dw_val_class_str:
8308           if (AT_string (a) != NULL)
8309             fprintf (outfile, "\"%s\"", AT_string (a));
8310           else
8311             fprintf (outfile, "<null>");
8312           break;
8313         case dw_val_class_file:
8314           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8315                    AT_file (a)->emitted_number);
8316           break;
8317         case dw_val_class_data8:
8318           {
8319             int i;
8320
8321             for (i = 0; i < 8; i++)
8322               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8323             break;
8324           }
8325         default:
8326           break;
8327         }
8328
8329       fprintf (outfile, "\n");
8330     }
8331
8332   if (die->die_child != NULL)
8333     {
8334       print_indent += 4;
8335       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8336       print_indent -= 4;
8337     }
8338   if (print_indent == 0)
8339     fprintf (outfile, "\n");
8340 }
8341
8342 /* Print the contents of the source code line number correspondence table.
8343    This routine is a debugging aid only.  */
8344
8345 static void
8346 print_dwarf_line_table (FILE *outfile)
8347 {
8348   unsigned i;
8349   dw_line_info_ref line_info;
8350
8351   fprintf (outfile, "\n\nDWARF source line information\n");
8352   for (i = 1; i < line_info_table_in_use; i++)
8353     {
8354       line_info = &line_info_table[i];
8355       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8356                line_info->dw_file_num,
8357                line_info->dw_line_num);
8358     }
8359
8360   fprintf (outfile, "\n\n");
8361 }
8362
8363 /* Print the information collected for a given DIE.  */
8364
8365 DEBUG_FUNCTION void
8366 debug_dwarf_die (dw_die_ref die)
8367 {
8368   print_die (die, stderr);
8369 }
8370
8371 /* Print all DWARF information collected for the compilation unit.
8372    This routine is a debugging aid only.  */
8373
8374 DEBUG_FUNCTION void
8375 debug_dwarf (void)
8376 {
8377   print_indent = 0;
8378   print_die (comp_unit_die, stderr);
8379   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8380     print_dwarf_line_table (stderr);
8381 }
8382 \f
8383 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8384    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8385    DIE that marks the start of the DIEs for this include file.  */
8386
8387 static dw_die_ref
8388 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8389 {
8390   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8391   dw_die_ref new_unit = gen_compile_unit_die (filename);
8392
8393   new_unit->die_sib = old_unit;
8394   return new_unit;
8395 }
8396
8397 /* Close an include-file CU and reopen the enclosing one.  */
8398
8399 static dw_die_ref
8400 pop_compile_unit (dw_die_ref old_unit)
8401 {
8402   dw_die_ref new_unit = old_unit->die_sib;
8403
8404   old_unit->die_sib = NULL;
8405   return new_unit;
8406 }
8407
8408 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8409 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8410
8411 /* Calculate the checksum of a location expression.  */
8412
8413 static inline void
8414 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8415 {
8416   int tem;
8417
8418   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8419   CHECKSUM (tem);
8420   CHECKSUM (loc->dw_loc_oprnd1);
8421   CHECKSUM (loc->dw_loc_oprnd2);
8422 }
8423
8424 /* Calculate the checksum of an attribute.  */
8425
8426 static void
8427 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8428 {
8429   dw_loc_descr_ref loc;
8430   rtx r;
8431
8432   CHECKSUM (at->dw_attr);
8433
8434   /* We don't care that this was compiled with a different compiler
8435      snapshot; if the output is the same, that's what matters.  */
8436   if (at->dw_attr == DW_AT_producer)
8437     return;
8438
8439   switch (AT_class (at))
8440     {
8441     case dw_val_class_const:
8442       CHECKSUM (at->dw_attr_val.v.val_int);
8443       break;
8444     case dw_val_class_unsigned_const:
8445       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8446       break;
8447     case dw_val_class_const_double:
8448       CHECKSUM (at->dw_attr_val.v.val_double);
8449       break;
8450     case dw_val_class_vec:
8451       CHECKSUM (at->dw_attr_val.v.val_vec);
8452       break;
8453     case dw_val_class_flag:
8454       CHECKSUM (at->dw_attr_val.v.val_flag);
8455       break;
8456     case dw_val_class_str:
8457       CHECKSUM_STRING (AT_string (at));
8458       break;
8459
8460     case dw_val_class_addr:
8461       r = AT_addr (at);
8462       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8463       CHECKSUM_STRING (XSTR (r, 0));
8464       break;
8465
8466     case dw_val_class_offset:
8467       CHECKSUM (at->dw_attr_val.v.val_offset);
8468       break;
8469
8470     case dw_val_class_loc:
8471       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8472         loc_checksum (loc, ctx);
8473       break;
8474
8475     case dw_val_class_die_ref:
8476       die_checksum (AT_ref (at), ctx, mark);
8477       break;
8478
8479     case dw_val_class_fde_ref:
8480     case dw_val_class_vms_delta:
8481     case dw_val_class_lbl_id:
8482     case dw_val_class_lineptr:
8483     case dw_val_class_macptr:
8484       break;
8485
8486     case dw_val_class_file:
8487       CHECKSUM_STRING (AT_file (at)->filename);
8488       break;
8489
8490     case dw_val_class_data8:
8491       CHECKSUM (at->dw_attr_val.v.val_data8);
8492       break;
8493
8494     default:
8495       break;
8496     }
8497 }
8498
8499 /* Calculate the checksum of a DIE.  */
8500
8501 static void
8502 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8503 {
8504   dw_die_ref c;
8505   dw_attr_ref a;
8506   unsigned ix;
8507
8508   /* To avoid infinite recursion.  */
8509   if (die->die_mark)
8510     {
8511       CHECKSUM (die->die_mark);
8512       return;
8513     }
8514   die->die_mark = ++(*mark);
8515
8516   CHECKSUM (die->die_tag);
8517
8518   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8519     attr_checksum (a, ctx, mark);
8520
8521   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8522 }
8523
8524 #undef CHECKSUM
8525 #undef CHECKSUM_STRING
8526
8527 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8528 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8529 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8530 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8531 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8532 #define CHECKSUM_ATTR(FOO) \
8533   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8534
8535 /* Calculate the checksum of a number in signed LEB128 format.  */
8536
8537 static void
8538 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8539 {
8540   unsigned char byte;
8541   bool more;
8542
8543   while (1)
8544     {
8545       byte = (value & 0x7f);
8546       value >>= 7;
8547       more = !((value == 0 && (byte & 0x40) == 0)
8548                 || (value == -1 && (byte & 0x40) != 0));
8549       if (more)
8550         byte |= 0x80;
8551       CHECKSUM (byte);
8552       if (!more)
8553         break;
8554     }
8555 }
8556
8557 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8558
8559 static void
8560 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8561 {
8562   while (1)
8563     {
8564       unsigned char byte = (value & 0x7f);
8565       value >>= 7;
8566       if (value != 0)
8567         /* More bytes to follow.  */
8568         byte |= 0x80;
8569       CHECKSUM (byte);
8570       if (value == 0)
8571         break;
8572     }
8573 }
8574
8575 /* Checksum the context of the DIE.  This adds the names of any
8576    surrounding namespaces or structures to the checksum.  */
8577
8578 static void
8579 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8580 {
8581   const char *name;
8582   dw_die_ref spec;
8583   int tag = die->die_tag;
8584
8585   if (tag != DW_TAG_namespace
8586       && tag != DW_TAG_structure_type
8587       && tag != DW_TAG_class_type)
8588     return;
8589
8590   name = get_AT_string (die, DW_AT_name);
8591
8592   spec = get_AT_ref (die, DW_AT_specification);
8593   if (spec != NULL)
8594     die = spec;
8595
8596   if (die->die_parent != NULL)
8597     checksum_die_context (die->die_parent, ctx);
8598
8599   CHECKSUM_ULEB128 ('C');
8600   CHECKSUM_ULEB128 (tag);
8601   if (name != NULL)
8602     CHECKSUM_STRING (name);
8603 }
8604
8605 /* Calculate the checksum of a location expression.  */
8606
8607 static inline void
8608 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8609 {
8610   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8611      were emitted as a DW_FORM_sdata instead of a location expression.  */
8612   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8613     {
8614       CHECKSUM_ULEB128 (DW_FORM_sdata);
8615       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8616       return;
8617     }
8618
8619   /* Otherwise, just checksum the raw location expression.  */
8620   while (loc != NULL)
8621     {
8622       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8623       CHECKSUM (loc->dw_loc_oprnd1);
8624       CHECKSUM (loc->dw_loc_oprnd2);
8625       loc = loc->dw_loc_next;
8626     }
8627 }
8628
8629 /* Calculate the checksum of an attribute.  */
8630
8631 static void
8632 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8633                        struct md5_ctx *ctx, int *mark)
8634 {
8635   dw_loc_descr_ref loc;
8636   rtx r;
8637
8638   if (AT_class (at) == dw_val_class_die_ref)
8639     {
8640       dw_die_ref target_die = AT_ref (at);
8641
8642       /* For pointer and reference types, we checksum only the (qualified)
8643          name of the target type (if there is a name).  For friend entries,
8644          we checksum only the (qualified) name of the target type or function.
8645          This allows the checksum to remain the same whether the target type
8646          is complete or not.  */
8647       if ((at->dw_attr == DW_AT_type
8648            && (tag == DW_TAG_pointer_type
8649                || tag == DW_TAG_reference_type
8650                || tag == DW_TAG_rvalue_reference_type
8651                || tag == DW_TAG_ptr_to_member_type))
8652           || (at->dw_attr == DW_AT_friend
8653               && tag == DW_TAG_friend))
8654         {
8655           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8656
8657           if (name_attr != NULL)
8658             {
8659               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8660
8661               if (decl == NULL)
8662                 decl = target_die;
8663               CHECKSUM_ULEB128 ('N');
8664               CHECKSUM_ULEB128 (at->dw_attr);
8665               if (decl->die_parent != NULL)
8666                 checksum_die_context (decl->die_parent, ctx);
8667               CHECKSUM_ULEB128 ('E');
8668               CHECKSUM_STRING (AT_string (name_attr));
8669               return;
8670             }
8671         }
8672
8673       /* For all other references to another DIE, we check to see if the
8674          target DIE has already been visited.  If it has, we emit a
8675          backward reference; if not, we descend recursively.  */
8676       if (target_die->die_mark > 0)
8677         {
8678           CHECKSUM_ULEB128 ('R');
8679           CHECKSUM_ULEB128 (at->dw_attr);
8680           CHECKSUM_ULEB128 (target_die->die_mark);
8681         }
8682       else
8683         {
8684           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8685
8686           if (decl == NULL)
8687             decl = target_die;
8688           target_die->die_mark = ++(*mark);
8689           CHECKSUM_ULEB128 ('T');
8690           CHECKSUM_ULEB128 (at->dw_attr);
8691           if (decl->die_parent != NULL)
8692             checksum_die_context (decl->die_parent, ctx);
8693           die_checksum_ordered (target_die, ctx, mark);
8694         }
8695       return;
8696     }
8697
8698   CHECKSUM_ULEB128 ('A');
8699   CHECKSUM_ULEB128 (at->dw_attr);
8700
8701   switch (AT_class (at))
8702     {
8703     case dw_val_class_const:
8704       CHECKSUM_ULEB128 (DW_FORM_sdata);
8705       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8706       break;
8707
8708     case dw_val_class_unsigned_const:
8709       CHECKSUM_ULEB128 (DW_FORM_sdata);
8710       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8711       break;
8712
8713     case dw_val_class_const_double:
8714       CHECKSUM_ULEB128 (DW_FORM_block);
8715       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8716       CHECKSUM (at->dw_attr_val.v.val_double);
8717       break;
8718
8719     case dw_val_class_vec:
8720       CHECKSUM_ULEB128 (DW_FORM_block);
8721       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8722       CHECKSUM (at->dw_attr_val.v.val_vec);
8723       break;
8724
8725     case dw_val_class_flag:
8726       CHECKSUM_ULEB128 (DW_FORM_flag);
8727       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8728       break;
8729
8730     case dw_val_class_str:
8731       CHECKSUM_ULEB128 (DW_FORM_string);
8732       CHECKSUM_STRING (AT_string (at));
8733       break;
8734
8735     case dw_val_class_addr:
8736       r = AT_addr (at);
8737       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8738       CHECKSUM_ULEB128 (DW_FORM_string);
8739       CHECKSUM_STRING (XSTR (r, 0));
8740       break;
8741
8742     case dw_val_class_offset:
8743       CHECKSUM_ULEB128 (DW_FORM_sdata);
8744       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8745       break;
8746
8747     case dw_val_class_loc:
8748       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8749         loc_checksum_ordered (loc, ctx);
8750       break;
8751
8752     case dw_val_class_fde_ref:
8753     case dw_val_class_lbl_id:
8754     case dw_val_class_lineptr:
8755     case dw_val_class_macptr:
8756       break;
8757
8758     case dw_val_class_file:
8759       CHECKSUM_ULEB128 (DW_FORM_string);
8760       CHECKSUM_STRING (AT_file (at)->filename);
8761       break;
8762
8763     case dw_val_class_data8:
8764       CHECKSUM (at->dw_attr_val.v.val_data8);
8765       break;
8766
8767     default:
8768       break;
8769     }
8770 }
8771
8772 struct checksum_attributes
8773 {
8774   dw_attr_ref at_name;
8775   dw_attr_ref at_type;
8776   dw_attr_ref at_friend;
8777   dw_attr_ref at_accessibility;
8778   dw_attr_ref at_address_class;
8779   dw_attr_ref at_allocated;
8780   dw_attr_ref at_artificial;
8781   dw_attr_ref at_associated;
8782   dw_attr_ref at_binary_scale;
8783   dw_attr_ref at_bit_offset;
8784   dw_attr_ref at_bit_size;
8785   dw_attr_ref at_bit_stride;
8786   dw_attr_ref at_byte_size;
8787   dw_attr_ref at_byte_stride;
8788   dw_attr_ref at_const_value;
8789   dw_attr_ref at_containing_type;
8790   dw_attr_ref at_count;
8791   dw_attr_ref at_data_location;
8792   dw_attr_ref at_data_member_location;
8793   dw_attr_ref at_decimal_scale;
8794   dw_attr_ref at_decimal_sign;
8795   dw_attr_ref at_default_value;
8796   dw_attr_ref at_digit_count;
8797   dw_attr_ref at_discr;
8798   dw_attr_ref at_discr_list;
8799   dw_attr_ref at_discr_value;
8800   dw_attr_ref at_encoding;
8801   dw_attr_ref at_endianity;
8802   dw_attr_ref at_explicit;
8803   dw_attr_ref at_is_optional;
8804   dw_attr_ref at_location;
8805   dw_attr_ref at_lower_bound;
8806   dw_attr_ref at_mutable;
8807   dw_attr_ref at_ordering;
8808   dw_attr_ref at_picture_string;
8809   dw_attr_ref at_prototyped;
8810   dw_attr_ref at_small;
8811   dw_attr_ref at_segment;
8812   dw_attr_ref at_string_length;
8813   dw_attr_ref at_threads_scaled;
8814   dw_attr_ref at_upper_bound;
8815   dw_attr_ref at_use_location;
8816   dw_attr_ref at_use_UTF8;
8817   dw_attr_ref at_variable_parameter;
8818   dw_attr_ref at_virtuality;
8819   dw_attr_ref at_visibility;
8820   dw_attr_ref at_vtable_elem_location;
8821 };
8822
8823 /* Collect the attributes that we will want to use for the checksum.  */
8824
8825 static void
8826 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8827 {
8828   dw_attr_ref a;
8829   unsigned ix;
8830
8831   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8832     {
8833       switch (a->dw_attr)
8834         {
8835         case DW_AT_name:
8836           attrs->at_name = a;
8837           break;
8838         case DW_AT_type:
8839           attrs->at_type = a;
8840           break;
8841         case DW_AT_friend:
8842           attrs->at_friend = a;
8843           break;
8844         case DW_AT_accessibility:
8845           attrs->at_accessibility = a;
8846           break;
8847         case DW_AT_address_class:
8848           attrs->at_address_class = a;
8849           break;
8850         case DW_AT_allocated:
8851           attrs->at_allocated = a;
8852           break;
8853         case DW_AT_artificial:
8854           attrs->at_artificial = a;
8855           break;
8856         case DW_AT_associated:
8857           attrs->at_associated = a;
8858           break;
8859         case DW_AT_binary_scale:
8860           attrs->at_binary_scale = a;
8861           break;
8862         case DW_AT_bit_offset:
8863           attrs->at_bit_offset = a;
8864           break;
8865         case DW_AT_bit_size:
8866           attrs->at_bit_size = a;
8867           break;
8868         case DW_AT_bit_stride:
8869           attrs->at_bit_stride = a;
8870           break;
8871         case DW_AT_byte_size:
8872           attrs->at_byte_size = a;
8873           break;
8874         case DW_AT_byte_stride:
8875           attrs->at_byte_stride = a;
8876           break;
8877         case DW_AT_const_value:
8878           attrs->at_const_value = a;
8879           break;
8880         case DW_AT_containing_type:
8881           attrs->at_containing_type = a;
8882           break;
8883         case DW_AT_count:
8884           attrs->at_count = a;
8885           break;
8886         case DW_AT_data_location:
8887           attrs->at_data_location = a;
8888           break;
8889         case DW_AT_data_member_location:
8890           attrs->at_data_member_location = a;
8891           break;
8892         case DW_AT_decimal_scale:
8893           attrs->at_decimal_scale = a;
8894           break;
8895         case DW_AT_decimal_sign:
8896           attrs->at_decimal_sign = a;
8897           break;
8898         case DW_AT_default_value:
8899           attrs->at_default_value = a;
8900           break;
8901         case DW_AT_digit_count:
8902           attrs->at_digit_count = a;
8903           break;
8904         case DW_AT_discr:
8905           attrs->at_discr = a;
8906           break;
8907         case DW_AT_discr_list:
8908           attrs->at_discr_list = a;
8909           break;
8910         case DW_AT_discr_value:
8911           attrs->at_discr_value = a;
8912           break;
8913         case DW_AT_encoding:
8914           attrs->at_encoding = a;
8915           break;
8916         case DW_AT_endianity:
8917           attrs->at_endianity = a;
8918           break;
8919         case DW_AT_explicit:
8920           attrs->at_explicit = a;
8921           break;
8922         case DW_AT_is_optional:
8923           attrs->at_is_optional = a;
8924           break;
8925         case DW_AT_location:
8926           attrs->at_location = a;
8927           break;
8928         case DW_AT_lower_bound:
8929           attrs->at_lower_bound = a;
8930           break;
8931         case DW_AT_mutable:
8932           attrs->at_mutable = a;
8933           break;
8934         case DW_AT_ordering:
8935           attrs->at_ordering = a;
8936           break;
8937         case DW_AT_picture_string:
8938           attrs->at_picture_string = a;
8939           break;
8940         case DW_AT_prototyped:
8941           attrs->at_prototyped = a;
8942           break;
8943         case DW_AT_small:
8944           attrs->at_small = a;
8945           break;
8946         case DW_AT_segment:
8947           attrs->at_segment = a;
8948           break;
8949         case DW_AT_string_length:
8950           attrs->at_string_length = a;
8951           break;
8952         case DW_AT_threads_scaled:
8953           attrs->at_threads_scaled = a;
8954           break;
8955         case DW_AT_upper_bound:
8956           attrs->at_upper_bound = a;
8957           break;
8958         case DW_AT_use_location:
8959           attrs->at_use_location = a;
8960           break;
8961         case DW_AT_use_UTF8:
8962           attrs->at_use_UTF8 = a;
8963           break;
8964         case DW_AT_variable_parameter:
8965           attrs->at_variable_parameter = a;
8966           break;
8967         case DW_AT_virtuality:
8968           attrs->at_virtuality = a;
8969           break;
8970         case DW_AT_visibility:
8971           attrs->at_visibility = a;
8972           break;
8973         case DW_AT_vtable_elem_location:
8974           attrs->at_vtable_elem_location = a;
8975           break;
8976         default:
8977           break;
8978         }
8979     }
8980 }
8981
8982 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8983
8984 static void
8985 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8986 {
8987   dw_die_ref c;
8988   dw_die_ref decl;
8989   struct checksum_attributes attrs;
8990
8991   CHECKSUM_ULEB128 ('D');
8992   CHECKSUM_ULEB128 (die->die_tag);
8993
8994   memset (&attrs, 0, sizeof (attrs));
8995
8996   decl = get_AT_ref (die, DW_AT_specification);
8997   if (decl != NULL)
8998     collect_checksum_attributes (&attrs, decl);
8999   collect_checksum_attributes (&attrs, die);
9000
9001   CHECKSUM_ATTR (attrs.at_name);
9002   CHECKSUM_ATTR (attrs.at_accessibility);
9003   CHECKSUM_ATTR (attrs.at_address_class);
9004   CHECKSUM_ATTR (attrs.at_allocated);
9005   CHECKSUM_ATTR (attrs.at_artificial);
9006   CHECKSUM_ATTR (attrs.at_associated);
9007   CHECKSUM_ATTR (attrs.at_binary_scale);
9008   CHECKSUM_ATTR (attrs.at_bit_offset);
9009   CHECKSUM_ATTR (attrs.at_bit_size);
9010   CHECKSUM_ATTR (attrs.at_bit_stride);
9011   CHECKSUM_ATTR (attrs.at_byte_size);
9012   CHECKSUM_ATTR (attrs.at_byte_stride);
9013   CHECKSUM_ATTR (attrs.at_const_value);
9014   CHECKSUM_ATTR (attrs.at_containing_type);
9015   CHECKSUM_ATTR (attrs.at_count);
9016   CHECKSUM_ATTR (attrs.at_data_location);
9017   CHECKSUM_ATTR (attrs.at_data_member_location);
9018   CHECKSUM_ATTR (attrs.at_decimal_scale);
9019   CHECKSUM_ATTR (attrs.at_decimal_sign);
9020   CHECKSUM_ATTR (attrs.at_default_value);
9021   CHECKSUM_ATTR (attrs.at_digit_count);
9022   CHECKSUM_ATTR (attrs.at_discr);
9023   CHECKSUM_ATTR (attrs.at_discr_list);
9024   CHECKSUM_ATTR (attrs.at_discr_value);
9025   CHECKSUM_ATTR (attrs.at_encoding);
9026   CHECKSUM_ATTR (attrs.at_endianity);
9027   CHECKSUM_ATTR (attrs.at_explicit);
9028   CHECKSUM_ATTR (attrs.at_is_optional);
9029   CHECKSUM_ATTR (attrs.at_location);
9030   CHECKSUM_ATTR (attrs.at_lower_bound);
9031   CHECKSUM_ATTR (attrs.at_mutable);
9032   CHECKSUM_ATTR (attrs.at_ordering);
9033   CHECKSUM_ATTR (attrs.at_picture_string);
9034   CHECKSUM_ATTR (attrs.at_prototyped);
9035   CHECKSUM_ATTR (attrs.at_small);
9036   CHECKSUM_ATTR (attrs.at_segment);
9037   CHECKSUM_ATTR (attrs.at_string_length);
9038   CHECKSUM_ATTR (attrs.at_threads_scaled);
9039   CHECKSUM_ATTR (attrs.at_upper_bound);
9040   CHECKSUM_ATTR (attrs.at_use_location);
9041   CHECKSUM_ATTR (attrs.at_use_UTF8);
9042   CHECKSUM_ATTR (attrs.at_variable_parameter);
9043   CHECKSUM_ATTR (attrs.at_virtuality);
9044   CHECKSUM_ATTR (attrs.at_visibility);
9045   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9046   CHECKSUM_ATTR (attrs.at_type);
9047   CHECKSUM_ATTR (attrs.at_friend);
9048
9049   /* Checksum the child DIEs, except for nested types and member functions.  */
9050   c = die->die_child;
9051   if (c) do {
9052     dw_attr_ref name_attr;
9053
9054     c = c->die_sib;
9055     name_attr = get_AT (c, DW_AT_name);
9056     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9057         && name_attr != NULL)
9058       {
9059         CHECKSUM_ULEB128 ('S');
9060         CHECKSUM_ULEB128 (c->die_tag);
9061         CHECKSUM_STRING (AT_string (name_attr));
9062       }
9063     else
9064       {
9065         /* Mark this DIE so it gets processed when unmarking.  */
9066         if (c->die_mark == 0)
9067           c->die_mark = -1;
9068         die_checksum_ordered (c, ctx, mark);
9069       }
9070   } while (c != die->die_child);
9071
9072   CHECKSUM_ULEB128 (0);
9073 }
9074
9075 #undef CHECKSUM
9076 #undef CHECKSUM_STRING
9077 #undef CHECKSUM_ATTR
9078 #undef CHECKSUM_LEB128
9079 #undef CHECKSUM_ULEB128
9080
9081 /* Generate the type signature for DIE.  This is computed by generating an
9082    MD5 checksum over the DIE's tag, its relevant attributes, and its
9083    children.  Attributes that are references to other DIEs are processed
9084    by recursion, using the MARK field to prevent infinite recursion.
9085    If the DIE is nested inside a namespace or another type, we also
9086    need to include that context in the signature.  The lower 64 bits
9087    of the resulting MD5 checksum comprise the signature.  */
9088
9089 static void
9090 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9091 {
9092   int mark;
9093   const char *name;
9094   unsigned char checksum[16];
9095   struct md5_ctx ctx;
9096   dw_die_ref decl;
9097
9098   name = get_AT_string (die, DW_AT_name);
9099   decl = get_AT_ref (die, DW_AT_specification);
9100
9101   /* First, compute a signature for just the type name (and its surrounding
9102      context, if any.  This is stored in the type unit DIE for link-time
9103      ODR (one-definition rule) checking.  */
9104
9105   if (is_cxx() && name != NULL)
9106     {
9107       md5_init_ctx (&ctx);
9108
9109       /* Checksum the names of surrounding namespaces and structures.  */
9110       if (decl != NULL && decl->die_parent != NULL)
9111         checksum_die_context (decl->die_parent, &ctx);
9112
9113       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9114       md5_process_bytes (name, strlen (name) + 1, &ctx);
9115       md5_finish_ctx (&ctx, checksum);
9116
9117       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9118     }
9119
9120   /* Next, compute the complete type signature.  */
9121
9122   md5_init_ctx (&ctx);
9123   mark = 1;
9124   die->die_mark = mark;
9125
9126   /* Checksum the names of surrounding namespaces and structures.  */
9127   if (decl != NULL && decl->die_parent != NULL)
9128     checksum_die_context (decl->die_parent, &ctx);
9129
9130   /* Checksum the DIE and its children.  */
9131   die_checksum_ordered (die, &ctx, &mark);
9132   unmark_all_dies (die);
9133   md5_finish_ctx (&ctx, checksum);
9134
9135   /* Store the signature in the type node and link the type DIE and the
9136      type node together.  */
9137   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9138           DWARF_TYPE_SIGNATURE_SIZE);
9139   die->die_id.die_type_node = type_node;
9140   type_node->type_die = die;
9141
9142   /* If the DIE is a specification, link its declaration to the type node
9143      as well.  */
9144   if (decl != NULL)
9145     decl->die_id.die_type_node = type_node;
9146 }
9147
9148 /* Do the location expressions look same?  */
9149 static inline int
9150 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9151 {
9152   return loc1->dw_loc_opc == loc2->dw_loc_opc
9153          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9154          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9155 }
9156
9157 /* Do the values look the same?  */
9158 static int
9159 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9160 {
9161   dw_loc_descr_ref loc1, loc2;
9162   rtx r1, r2;
9163
9164   if (v1->val_class != v2->val_class)
9165     return 0;
9166
9167   switch (v1->val_class)
9168     {
9169     case dw_val_class_const:
9170       return v1->v.val_int == v2->v.val_int;
9171     case dw_val_class_unsigned_const:
9172       return v1->v.val_unsigned == v2->v.val_unsigned;
9173     case dw_val_class_const_double:
9174       return v1->v.val_double.high == v2->v.val_double.high
9175              && v1->v.val_double.low == v2->v.val_double.low;
9176     case dw_val_class_vec:
9177       if (v1->v.val_vec.length != v2->v.val_vec.length
9178           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9179         return 0;
9180       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9181                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9182         return 0;
9183       return 1;
9184     case dw_val_class_flag:
9185       return v1->v.val_flag == v2->v.val_flag;
9186     case dw_val_class_str:
9187       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9188
9189     case dw_val_class_addr:
9190       r1 = v1->v.val_addr;
9191       r2 = v2->v.val_addr;
9192       if (GET_CODE (r1) != GET_CODE (r2))
9193         return 0;
9194       return !rtx_equal_p (r1, r2);
9195
9196     case dw_val_class_offset:
9197       return v1->v.val_offset == v2->v.val_offset;
9198
9199     case dw_val_class_loc:
9200       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9201            loc1 && loc2;
9202            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9203         if (!same_loc_p (loc1, loc2, mark))
9204           return 0;
9205       return !loc1 && !loc2;
9206
9207     case dw_val_class_die_ref:
9208       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9209
9210     case dw_val_class_fde_ref:
9211     case dw_val_class_vms_delta:
9212     case dw_val_class_lbl_id:
9213     case dw_val_class_lineptr:
9214     case dw_val_class_macptr:
9215       return 1;
9216
9217     case dw_val_class_file:
9218       return v1->v.val_file == v2->v.val_file;
9219
9220     case dw_val_class_data8:
9221       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9222
9223     default:
9224       return 1;
9225     }
9226 }
9227
9228 /* Do the attributes look the same?  */
9229
9230 static int
9231 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9232 {
9233   if (at1->dw_attr != at2->dw_attr)
9234     return 0;
9235
9236   /* We don't care that this was compiled with a different compiler
9237      snapshot; if the output is the same, that's what matters. */
9238   if (at1->dw_attr == DW_AT_producer)
9239     return 1;
9240
9241   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9242 }
9243
9244 /* Do the dies look the same?  */
9245
9246 static int
9247 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9248 {
9249   dw_die_ref c1, c2;
9250   dw_attr_ref a1;
9251   unsigned ix;
9252
9253   /* To avoid infinite recursion.  */
9254   if (die1->die_mark)
9255     return die1->die_mark == die2->die_mark;
9256   die1->die_mark = die2->die_mark = ++(*mark);
9257
9258   if (die1->die_tag != die2->die_tag)
9259     return 0;
9260
9261   if (VEC_length (dw_attr_node, die1->die_attr)
9262       != VEC_length (dw_attr_node, die2->die_attr))
9263     return 0;
9264
9265   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9266     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9267       return 0;
9268
9269   c1 = die1->die_child;
9270   c2 = die2->die_child;
9271   if (! c1)
9272     {
9273       if (c2)
9274         return 0;
9275     }
9276   else
9277     for (;;)
9278       {
9279         if (!same_die_p (c1, c2, mark))
9280           return 0;
9281         c1 = c1->die_sib;
9282         c2 = c2->die_sib;
9283         if (c1 == die1->die_child)
9284           {
9285             if (c2 == die2->die_child)
9286               break;
9287             else
9288               return 0;
9289           }
9290     }
9291
9292   return 1;
9293 }
9294
9295 /* Do the dies look the same?  Wrapper around same_die_p.  */
9296
9297 static int
9298 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9299 {
9300   int mark = 0;
9301   int ret = same_die_p (die1, die2, &mark);
9302
9303   unmark_all_dies (die1);
9304   unmark_all_dies (die2);
9305
9306   return ret;
9307 }
9308
9309 /* The prefix to attach to symbols on DIEs in the current comdat debug
9310    info section.  */
9311 static char *comdat_symbol_id;
9312
9313 /* The index of the current symbol within the current comdat CU.  */
9314 static unsigned int comdat_symbol_number;
9315
9316 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9317    children, and set comdat_symbol_id accordingly.  */
9318
9319 static void
9320 compute_section_prefix (dw_die_ref unit_die)
9321 {
9322   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9323   const char *base = die_name ? lbasename (die_name) : "anonymous";
9324   char *name = XALLOCAVEC (char, strlen (base) + 64);
9325   char *p;
9326   int i, mark;
9327   unsigned char checksum[16];
9328   struct md5_ctx ctx;
9329
9330   /* Compute the checksum of the DIE, then append part of it as hex digits to
9331      the name filename of the unit.  */
9332
9333   md5_init_ctx (&ctx);
9334   mark = 0;
9335   die_checksum (unit_die, &ctx, &mark);
9336   unmark_all_dies (unit_die);
9337   md5_finish_ctx (&ctx, checksum);
9338
9339   sprintf (name, "%s.", base);
9340   clean_symbol_name (name);
9341
9342   p = name + strlen (name);
9343   for (i = 0; i < 4; i++)
9344     {
9345       sprintf (p, "%.2x", checksum[i]);
9346       p += 2;
9347     }
9348
9349   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9350   comdat_symbol_number = 0;
9351 }
9352
9353 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9354
9355 static int
9356 is_type_die (dw_die_ref die)
9357 {
9358   switch (die->die_tag)
9359     {
9360     case DW_TAG_array_type:
9361     case DW_TAG_class_type:
9362     case DW_TAG_interface_type:
9363     case DW_TAG_enumeration_type:
9364     case DW_TAG_pointer_type:
9365     case DW_TAG_reference_type:
9366     case DW_TAG_rvalue_reference_type:
9367     case DW_TAG_string_type:
9368     case DW_TAG_structure_type:
9369     case DW_TAG_subroutine_type:
9370     case DW_TAG_union_type:
9371     case DW_TAG_ptr_to_member_type:
9372     case DW_TAG_set_type:
9373     case DW_TAG_subrange_type:
9374     case DW_TAG_base_type:
9375     case DW_TAG_const_type:
9376     case DW_TAG_file_type:
9377     case DW_TAG_packed_type:
9378     case DW_TAG_volatile_type:
9379     case DW_TAG_typedef:
9380       return 1;
9381     default:
9382       return 0;
9383     }
9384 }
9385
9386 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9387    Basically, we want to choose the bits that are likely to be shared between
9388    compilations (types) and leave out the bits that are specific to individual
9389    compilations (functions).  */
9390
9391 static int
9392 is_comdat_die (dw_die_ref c)
9393 {
9394   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9395      we do for stabs.  The advantage is a greater likelihood of sharing between
9396      objects that don't include headers in the same order (and therefore would
9397      put the base types in a different comdat).  jason 8/28/00 */
9398
9399   if (c->die_tag == DW_TAG_base_type)
9400     return 0;
9401
9402   if (c->die_tag == DW_TAG_pointer_type
9403       || c->die_tag == DW_TAG_reference_type
9404       || c->die_tag == DW_TAG_rvalue_reference_type
9405       || c->die_tag == DW_TAG_const_type
9406       || c->die_tag == DW_TAG_volatile_type)
9407     {
9408       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9409
9410       return t ? is_comdat_die (t) : 0;
9411     }
9412
9413   return is_type_die (c);
9414 }
9415
9416 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9417    compilation unit.  */
9418
9419 static int
9420 is_symbol_die (dw_die_ref c)
9421 {
9422   return (is_type_die (c)
9423           || is_declaration_die (c)
9424           || c->die_tag == DW_TAG_namespace
9425           || c->die_tag == DW_TAG_module);
9426 }
9427
9428 static char *
9429 gen_internal_sym (const char *prefix)
9430 {
9431   char buf[256];
9432
9433   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9434   return xstrdup (buf);
9435 }
9436
9437 /* Assign symbols to all worthy DIEs under DIE.  */
9438
9439 static void
9440 assign_symbol_names (dw_die_ref die)
9441 {
9442   dw_die_ref c;
9443
9444   if (is_symbol_die (die))
9445     {
9446       if (comdat_symbol_id)
9447         {
9448           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9449
9450           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9451                    comdat_symbol_id, comdat_symbol_number++);
9452           die->die_id.die_symbol = xstrdup (p);
9453         }
9454       else
9455         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9456     }
9457
9458   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9459 }
9460
9461 struct cu_hash_table_entry
9462 {
9463   dw_die_ref cu;
9464   unsigned min_comdat_num, max_comdat_num;
9465   struct cu_hash_table_entry *next;
9466 };
9467
9468 /* Routines to manipulate hash table of CUs.  */
9469 static hashval_t
9470 htab_cu_hash (const void *of)
9471 {
9472   const struct cu_hash_table_entry *const entry =
9473     (const struct cu_hash_table_entry *) of;
9474
9475   return htab_hash_string (entry->cu->die_id.die_symbol);
9476 }
9477
9478 static int
9479 htab_cu_eq (const void *of1, const void *of2)
9480 {
9481   const struct cu_hash_table_entry *const entry1 =
9482     (const struct cu_hash_table_entry *) of1;
9483   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9484
9485   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9486 }
9487
9488 static void
9489 htab_cu_del (void *what)
9490 {
9491   struct cu_hash_table_entry *next,
9492     *entry = (struct cu_hash_table_entry *) what;
9493
9494   while (entry)
9495     {
9496       next = entry->next;
9497       free (entry);
9498       entry = next;
9499     }
9500 }
9501
9502 /* Check whether we have already seen this CU and set up SYM_NUM
9503    accordingly.  */
9504 static int
9505 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9506 {
9507   struct cu_hash_table_entry dummy;
9508   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9509
9510   dummy.max_comdat_num = 0;
9511
9512   slot = (struct cu_hash_table_entry **)
9513     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9514         INSERT);
9515   entry = *slot;
9516
9517   for (; entry; last = entry, entry = entry->next)
9518     {
9519       if (same_die_p_wrap (cu, entry->cu))
9520         break;
9521     }
9522
9523   if (entry)
9524     {
9525       *sym_num = entry->min_comdat_num;
9526       return 1;
9527     }
9528
9529   entry = XCNEW (struct cu_hash_table_entry);
9530   entry->cu = cu;
9531   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9532   entry->next = *slot;
9533   *slot = entry;
9534
9535   return 0;
9536 }
9537
9538 /* Record SYM_NUM to record of CU in HTABLE.  */
9539 static void
9540 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9541 {
9542   struct cu_hash_table_entry **slot, *entry;
9543
9544   slot = (struct cu_hash_table_entry **)
9545     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9546         NO_INSERT);
9547   entry = *slot;
9548
9549   entry->max_comdat_num = sym_num;
9550 }
9551
9552 /* Traverse the DIE (which is always comp_unit_die), and set up
9553    additional compilation units for each of the include files we see
9554    bracketed by BINCL/EINCL.  */
9555
9556 static void
9557 break_out_includes (dw_die_ref die)
9558 {
9559   dw_die_ref c;
9560   dw_die_ref unit = NULL;
9561   limbo_die_node *node, **pnode;
9562   htab_t cu_hash_table;
9563
9564   c = die->die_child;
9565   if (c) do {
9566     dw_die_ref prev = c;
9567     c = c->die_sib;
9568     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9569            || (unit && is_comdat_die (c)))
9570       {
9571         dw_die_ref next = c->die_sib;
9572
9573         /* This DIE is for a secondary CU; remove it from the main one.  */
9574         remove_child_with_prev (c, prev);
9575
9576         if (c->die_tag == DW_TAG_GNU_BINCL)
9577           unit = push_new_compile_unit (unit, c);
9578         else if (c->die_tag == DW_TAG_GNU_EINCL)
9579           unit = pop_compile_unit (unit);
9580         else
9581           add_child_die (unit, c);
9582         c = next;
9583         if (c == die->die_child)
9584           break;
9585       }
9586   } while (c != die->die_child);
9587
9588 #if 0
9589   /* We can only use this in debugging, since the frontend doesn't check
9590      to make sure that we leave every include file we enter.  */
9591   gcc_assert (!unit);
9592 #endif
9593
9594   assign_symbol_names (die);
9595   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9596   for (node = limbo_die_list, pnode = &limbo_die_list;
9597        node;
9598        node = node->next)
9599     {
9600       int is_dupl;
9601
9602       compute_section_prefix (node->die);
9603       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9604                         &comdat_symbol_number);
9605       assign_symbol_names (node->die);
9606       if (is_dupl)
9607         *pnode = node->next;
9608       else
9609         {
9610           pnode = &node->next;
9611           record_comdat_symbol_number (node->die, cu_hash_table,
9612                 comdat_symbol_number);
9613         }
9614     }
9615   htab_delete (cu_hash_table);
9616 }
9617
9618 /* Return non-zero if this DIE is a declaration.  */
9619
9620 static int
9621 is_declaration_die (dw_die_ref die)
9622 {
9623   dw_attr_ref a;
9624   unsigned ix;
9625
9626   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9627     if (a->dw_attr == DW_AT_declaration)
9628       return 1;
9629
9630   return 0;
9631 }
9632
9633 /* Return non-zero if this is a type DIE that should be moved to a
9634    COMDAT .debug_types section.  */
9635
9636 static int
9637 should_move_die_to_comdat (dw_die_ref die)
9638 {
9639   switch (die->die_tag)
9640     {
9641     case DW_TAG_class_type:
9642     case DW_TAG_structure_type:
9643     case DW_TAG_enumeration_type:
9644     case DW_TAG_union_type:
9645       /* Don't move declarations or inlined instances.  */
9646       if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin))
9647         return 0;
9648       return 1;
9649     case DW_TAG_array_type:
9650     case DW_TAG_interface_type:
9651     case DW_TAG_pointer_type:
9652     case DW_TAG_reference_type:
9653     case DW_TAG_rvalue_reference_type:
9654     case DW_TAG_string_type:
9655     case DW_TAG_subroutine_type:
9656     case DW_TAG_ptr_to_member_type:
9657     case DW_TAG_set_type:
9658     case DW_TAG_subrange_type:
9659     case DW_TAG_base_type:
9660     case DW_TAG_const_type:
9661     case DW_TAG_file_type:
9662     case DW_TAG_packed_type:
9663     case DW_TAG_volatile_type:
9664     case DW_TAG_typedef:
9665     default:
9666       return 0;
9667     }
9668 }
9669
9670 /* Make a clone of DIE.  */
9671
9672 static dw_die_ref
9673 clone_die (dw_die_ref die)
9674 {
9675   dw_die_ref clone;
9676   dw_attr_ref a;
9677   unsigned ix;
9678
9679   clone = ggc_alloc_cleared_die_node ();
9680   clone->die_tag = die->die_tag;
9681
9682   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9683     add_dwarf_attr (clone, a);
9684
9685   return clone;
9686 }
9687
9688 /* Make a clone of the tree rooted at DIE.  */
9689
9690 static dw_die_ref
9691 clone_tree (dw_die_ref die)
9692 {
9693   dw_die_ref c;
9694   dw_die_ref clone = clone_die (die);
9695
9696   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9697
9698   return clone;
9699 }
9700
9701 /* Make a clone of DIE as a declaration.  */
9702
9703 static dw_die_ref
9704 clone_as_declaration (dw_die_ref die)
9705 {
9706   dw_die_ref clone;
9707   dw_die_ref decl;
9708   dw_attr_ref a;
9709   unsigned ix;
9710
9711   /* If the DIE is already a declaration, just clone it.  */
9712   if (is_declaration_die (die))
9713     return clone_die (die);
9714
9715   /* If the DIE is a specification, just clone its declaration DIE.  */
9716   decl = get_AT_ref (die, DW_AT_specification);
9717   if (decl != NULL)
9718     return clone_die (decl);
9719
9720   clone = ggc_alloc_cleared_die_node ();
9721   clone->die_tag = die->die_tag;
9722
9723   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9724     {
9725       /* We don't want to copy over all attributes.
9726          For example we don't want DW_AT_byte_size because otherwise we will no
9727          longer have a declaration and GDB will treat it as a definition.  */
9728
9729       switch (a->dw_attr)
9730         {
9731         case DW_AT_artificial:
9732         case DW_AT_containing_type:
9733         case DW_AT_external:
9734         case DW_AT_name:
9735         case DW_AT_type:
9736         case DW_AT_virtuality:
9737         case DW_AT_linkage_name:
9738         case DW_AT_MIPS_linkage_name:
9739           add_dwarf_attr (clone, a);
9740           break;
9741         case DW_AT_byte_size:
9742         default:
9743           break;
9744         }
9745     }
9746
9747   if (die->die_id.die_type_node)
9748     add_AT_die_ref (clone, DW_AT_signature, die);
9749
9750   add_AT_flag (clone, DW_AT_declaration, 1);
9751   return clone;
9752 }
9753
9754 /* Copy the declaration context to the new compile unit DIE.  This includes
9755    any surrounding namespace or type declarations.  If the DIE has an
9756    AT_specification attribute, it also includes attributes and children
9757    attached to the specification.  */
9758
9759 static void
9760 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9761 {
9762   dw_die_ref decl;
9763   dw_die_ref new_decl;
9764
9765   decl = get_AT_ref (die, DW_AT_specification);
9766   if (decl == NULL)
9767     decl = die;
9768   else
9769     {
9770       unsigned ix;
9771       dw_die_ref c;
9772       dw_attr_ref a;
9773
9774       /* Copy the type node pointer from the new DIE to the original
9775          declaration DIE so we can forward references later.  */
9776       decl->die_id.die_type_node = die->die_id.die_type_node;
9777
9778       remove_AT (die, DW_AT_specification);
9779
9780       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9781         {
9782           if (a->dw_attr != DW_AT_name
9783               && a->dw_attr != DW_AT_declaration
9784               && a->dw_attr != DW_AT_external)
9785             add_dwarf_attr (die, a);
9786         }
9787
9788       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9789     }
9790
9791   if (decl->die_parent != NULL
9792       && decl->die_parent->die_tag != DW_TAG_compile_unit
9793       && decl->die_parent->die_tag != DW_TAG_type_unit)
9794     {
9795       new_decl = copy_ancestor_tree (unit, decl, NULL);
9796       if (new_decl != NULL)
9797         {
9798           remove_AT (new_decl, DW_AT_signature);
9799           add_AT_specification (die, new_decl);
9800         }
9801     }
9802 }
9803
9804 /* Generate the skeleton ancestor tree for the given NODE, then clone
9805    the DIE and add the clone into the tree.  */
9806
9807 static void
9808 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9809 {
9810   if (node->new_die != NULL)
9811     return;
9812
9813   node->new_die = clone_as_declaration (node->old_die);
9814
9815   if (node->parent != NULL)
9816     {
9817       generate_skeleton_ancestor_tree (node->parent);
9818       add_child_die (node->parent->new_die, node->new_die);
9819     }
9820 }
9821
9822 /* Generate a skeleton tree of DIEs containing any declarations that are
9823    found in the original tree.  We traverse the tree looking for declaration
9824    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9825
9826 static void
9827 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9828 {
9829   skeleton_chain_node node;
9830   dw_die_ref c;
9831   dw_die_ref first;
9832   dw_die_ref prev = NULL;
9833   dw_die_ref next = NULL;
9834
9835   node.parent = parent;
9836
9837   first = c = parent->old_die->die_child;
9838   if (c)
9839     next = c->die_sib;
9840   if (c) do {
9841     if (prev == NULL || prev->die_sib == c)
9842       prev = c;
9843     c = next;
9844     next = (c == first ? NULL : c->die_sib);
9845     node.old_die = c;
9846     node.new_die = NULL;
9847     if (is_declaration_die (c))
9848       {
9849         /* Clone the existing DIE, move the original to the skeleton
9850            tree (which is in the main CU), and put the clone, with
9851            all the original's children, where the original came from.  */
9852         dw_die_ref clone = clone_die (c);
9853         move_all_children (c, clone);
9854
9855         replace_child (c, clone, prev);
9856         generate_skeleton_ancestor_tree (parent);
9857         add_child_die (parent->new_die, c);
9858         node.new_die = c;
9859         c = clone;
9860       }
9861     generate_skeleton_bottom_up (&node);
9862   } while (next != NULL);
9863 }
9864
9865 /* Wrapper function for generate_skeleton_bottom_up.  */
9866
9867 static dw_die_ref
9868 generate_skeleton (dw_die_ref die)
9869 {
9870   skeleton_chain_node node;
9871
9872   node.old_die = die;
9873   node.new_die = NULL;
9874   node.parent = NULL;
9875
9876   /* If this type definition is nested inside another type,
9877      always leave at least a declaration in its place.  */
9878   if (die->die_parent != NULL && is_type_die (die->die_parent))
9879     node.new_die = clone_as_declaration (die);
9880
9881   generate_skeleton_bottom_up (&node);
9882   return node.new_die;
9883 }
9884
9885 /* Remove the DIE from its parent, possibly replacing it with a cloned
9886    declaration.  The original DIE will be moved to a new compile unit
9887    so that existing references to it follow it to the new location.  If
9888    any of the original DIE's descendants is a declaration, we need to
9889    replace the original DIE with a skeleton tree and move the
9890    declarations back into the skeleton tree.  */
9891
9892 static dw_die_ref
9893 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9894 {
9895   dw_die_ref skeleton;
9896
9897   skeleton = generate_skeleton (child);
9898   if (skeleton == NULL)
9899     remove_child_with_prev (child, prev);
9900   else
9901     {
9902       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9903       replace_child (child, skeleton, prev);
9904     }
9905
9906   return skeleton;
9907 }
9908
9909 /* Traverse the DIE and set up additional .debug_types sections for each
9910    type worthy of being placed in a COMDAT section.  */
9911
9912 static void
9913 break_out_comdat_types (dw_die_ref die)
9914 {
9915   dw_die_ref c;
9916   dw_die_ref first;
9917   dw_die_ref prev = NULL;
9918   dw_die_ref next = NULL;
9919   dw_die_ref unit = NULL;
9920
9921   first = c = die->die_child;
9922   if (c)
9923     next = c->die_sib;
9924   if (c) do {
9925     if (prev == NULL || prev->die_sib == c)
9926       prev = c;
9927     c = next;
9928     next = (c == first ? NULL : c->die_sib);
9929     if (should_move_die_to_comdat (c))
9930       {
9931         dw_die_ref replacement;
9932         comdat_type_node_ref type_node;
9933
9934         /* Create a new type unit DIE as the root for the new tree, and
9935            add it to the list of comdat types.  */
9936         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9937         add_AT_unsigned (unit, DW_AT_language,
9938                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9939         type_node = ggc_alloc_cleared_comdat_type_node ();
9940         type_node->root_die = unit;
9941         type_node->next = comdat_type_list;
9942         comdat_type_list = type_node;
9943
9944         /* Generate the type signature.  */
9945         generate_type_signature (c, type_node);
9946
9947         /* Copy the declaration context, attributes, and children of the
9948            declaration into the new compile unit DIE.  */
9949         copy_declaration_context (unit, c);
9950
9951         /* Remove this DIE from the main CU.  */
9952         replacement = remove_child_or_replace_with_skeleton (c, prev);
9953
9954         /* Break out nested types into their own type units.  */
9955         break_out_comdat_types (c);
9956
9957         /* Add the DIE to the new compunit.  */
9958         add_child_die (unit, c);
9959
9960         if (replacement != NULL)
9961           c = replacement;
9962       }
9963     else if (c->die_tag == DW_TAG_namespace
9964              || c->die_tag == DW_TAG_class_type
9965              || c->die_tag == DW_TAG_structure_type
9966              || c->die_tag == DW_TAG_union_type)
9967       {
9968         /* Look for nested types that can be broken out.  */
9969         break_out_comdat_types (c);
9970       }
9971   } while (next != NULL);
9972 }
9973
9974 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9975
9976 struct decl_table_entry
9977 {
9978   dw_die_ref orig;
9979   dw_die_ref copy;
9980 };
9981
9982 /* Routines to manipulate hash table of copied declarations.  */
9983
9984 static hashval_t
9985 htab_decl_hash (const void *of)
9986 {
9987   const struct decl_table_entry *const entry =
9988     (const struct decl_table_entry *) of;
9989
9990   return htab_hash_pointer (entry->orig);
9991 }
9992
9993 static int
9994 htab_decl_eq (const void *of1, const void *of2)
9995 {
9996   const struct decl_table_entry *const entry1 =
9997     (const struct decl_table_entry *) of1;
9998   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9999
10000   return entry1->orig == entry2;
10001 }
10002
10003 static void
10004 htab_decl_del (void *what)
10005 {
10006   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10007
10008   free (entry);
10009 }
10010
10011 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10012    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10013    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10014    to check if the ancestor has already been copied into UNIT.  */
10015
10016 static dw_die_ref
10017 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10018 {
10019   dw_die_ref parent = die->die_parent;
10020   dw_die_ref new_parent = unit;
10021   dw_die_ref copy;
10022   void **slot = NULL;
10023   struct decl_table_entry *entry = NULL;
10024
10025   if (decl_table)
10026     {
10027       /* Check if the entry has already been copied to UNIT.  */
10028       slot = htab_find_slot_with_hash (decl_table, die,
10029                                        htab_hash_pointer (die), INSERT);
10030       if (*slot != HTAB_EMPTY_ENTRY)
10031         {
10032           entry = (struct decl_table_entry *) *slot;
10033           return entry->copy;
10034         }
10035
10036       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10037       entry = XCNEW (struct decl_table_entry);
10038       entry->orig = die;
10039       entry->copy = NULL;
10040       *slot = entry;
10041     }
10042
10043   if (parent != NULL)
10044     {
10045       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10046       if (spec != NULL)
10047         parent = spec;
10048       if (parent->die_tag != DW_TAG_compile_unit
10049           && parent->die_tag != DW_TAG_type_unit)
10050         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10051     }
10052
10053   copy = clone_as_declaration (die);
10054   add_child_die (new_parent, copy);
10055
10056   if (decl_table != NULL)
10057     {
10058       /* Make sure the copy is marked as part of the type unit.  */
10059       copy->die_mark = 1;
10060       /* Record the pointer to the copy.  */
10061       entry->copy = copy;
10062     }
10063
10064   return copy;
10065 }
10066
10067 /* Walk the DIE and its children, looking for references to incomplete
10068    or trivial types that are unmarked (i.e., that are not in the current
10069    type_unit).  */
10070
10071 static void
10072 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10073 {
10074   dw_die_ref c;
10075   dw_attr_ref a;
10076   unsigned ix;
10077
10078   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10079     {
10080       if (AT_class (a) == dw_val_class_die_ref)
10081         {
10082           dw_die_ref targ = AT_ref (a);
10083           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10084           void **slot;
10085           struct decl_table_entry *entry;
10086
10087           if (targ->die_mark != 0 || type_node != NULL)
10088             continue;
10089
10090           slot = htab_find_slot_with_hash (decl_table, targ,
10091                                            htab_hash_pointer (targ), INSERT);
10092
10093           if (*slot != HTAB_EMPTY_ENTRY)
10094             {
10095               /* TARG has already been copied, so we just need to
10096                  modify the reference to point to the copy.  */
10097               entry = (struct decl_table_entry *) *slot;
10098               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10099             }
10100           else
10101             {
10102               dw_die_ref parent = unit;
10103               dw_die_ref copy = clone_tree (targ);
10104
10105               /* Make sure the cloned tree is marked as part of the
10106                  type unit.  */
10107               mark_dies (copy);
10108
10109               /* Record in DECL_TABLE that TARG has been copied.
10110                  Need to do this now, before the recursive call,
10111                  because DECL_TABLE may be expanded and SLOT
10112                  would no longer be a valid pointer.  */
10113               entry = XCNEW (struct decl_table_entry);
10114               entry->orig = targ;
10115               entry->copy = copy;
10116               *slot = entry;
10117
10118               /* If TARG has surrounding context, copy its ancestor tree
10119                  into the new type unit.  */
10120               if (targ->die_parent != NULL
10121                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10122                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10123                 parent = copy_ancestor_tree (unit, targ->die_parent,
10124                                              decl_table);
10125
10126               add_child_die (parent, copy);
10127               a->dw_attr_val.v.val_die_ref.die = copy;
10128
10129               /* Make sure the newly-copied DIE is walked.  If it was
10130                  installed in a previously-added context, it won't
10131                  get visited otherwise.  */
10132               if (parent != unit)
10133                 copy_decls_walk (unit, parent, decl_table);
10134             }
10135         }
10136     }
10137
10138   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10139 }
10140
10141 /* Copy declarations for "unworthy" types into the new comdat section.
10142    Incomplete types, modified types, and certain other types aren't broken
10143    out into comdat sections of their own, so they don't have a signature,
10144    and we need to copy the declaration into the same section so that we
10145    don't have an external reference.  */
10146
10147 static void
10148 copy_decls_for_unworthy_types (dw_die_ref unit)
10149 {
10150   htab_t decl_table;
10151
10152   mark_dies (unit);
10153   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10154   copy_decls_walk (unit, unit, decl_table);
10155   htab_delete (decl_table);
10156   unmark_dies (unit);
10157 }
10158
10159 /* Traverse the DIE and add a sibling attribute if it may have the
10160    effect of speeding up access to siblings.  To save some space,
10161    avoid generating sibling attributes for DIE's without children.  */
10162
10163 static void
10164 add_sibling_attributes (dw_die_ref die)
10165 {
10166   dw_die_ref c;
10167
10168   if (! die->die_child)
10169     return;
10170
10171   if (die->die_parent && die != die->die_parent->die_child)
10172     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10173
10174   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10175 }
10176
10177 /* Output all location lists for the DIE and its children.  */
10178
10179 static void
10180 output_location_lists (dw_die_ref die)
10181 {
10182   dw_die_ref c;
10183   dw_attr_ref a;
10184   unsigned ix;
10185
10186   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10187     if (AT_class (a) == dw_val_class_loc_list)
10188       output_loc_list (AT_loc_list (a));
10189
10190   FOR_EACH_CHILD (die, c, output_location_lists (c));
10191 }
10192
10193 /* The format of each DIE (and its attribute value pairs) is encoded in an
10194    abbreviation table.  This routine builds the abbreviation table and assigns
10195    a unique abbreviation id for each abbreviation entry.  The children of each
10196    die are visited recursively.  */
10197
10198 static void
10199 build_abbrev_table (dw_die_ref die)
10200 {
10201   unsigned long abbrev_id;
10202   unsigned int n_alloc;
10203   dw_die_ref c;
10204   dw_attr_ref a;
10205   unsigned ix;
10206
10207   /* Scan the DIE references, and mark as external any that refer to
10208      DIEs from other CUs (i.e. those which are not marked).  */
10209   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10210     if (AT_class (a) == dw_val_class_die_ref
10211         && AT_ref (a)->die_mark == 0)
10212       {
10213         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10214         set_AT_ref_external (a, 1);
10215       }
10216
10217   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10218     {
10219       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10220       dw_attr_ref die_a, abbrev_a;
10221       unsigned ix;
10222       bool ok = true;
10223
10224       if (abbrev->die_tag != die->die_tag)
10225         continue;
10226       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10227         continue;
10228
10229       if (VEC_length (dw_attr_node, abbrev->die_attr)
10230           != VEC_length (dw_attr_node, die->die_attr))
10231         continue;
10232
10233       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10234         {
10235           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10236           if ((abbrev_a->dw_attr != die_a->dw_attr)
10237               || (value_format (abbrev_a) != value_format (die_a)))
10238             {
10239               ok = false;
10240               break;
10241             }
10242         }
10243       if (ok)
10244         break;
10245     }
10246
10247   if (abbrev_id >= abbrev_die_table_in_use)
10248     {
10249       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10250         {
10251           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10252           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10253                                             n_alloc);
10254
10255           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10256                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10257           abbrev_die_table_allocated = n_alloc;
10258         }
10259
10260       ++abbrev_die_table_in_use;
10261       abbrev_die_table[abbrev_id] = die;
10262     }
10263
10264   die->die_abbrev = abbrev_id;
10265   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10266 }
10267 \f
10268 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10269
10270 static int
10271 constant_size (unsigned HOST_WIDE_INT value)
10272 {
10273   int log;
10274
10275   if (value == 0)
10276     log = 0;
10277   else
10278     log = floor_log2 (value);
10279
10280   log = log / 8;
10281   log = 1 << (floor_log2 (log) + 1);
10282
10283   return log;
10284 }
10285
10286 /* Return the size of a DIE as it is represented in the
10287    .debug_info section.  */
10288
10289 static unsigned long
10290 size_of_die (dw_die_ref die)
10291 {
10292   unsigned long size = 0;
10293   dw_attr_ref a;
10294   unsigned ix;
10295
10296   size += size_of_uleb128 (die->die_abbrev);
10297   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10298     {
10299       switch (AT_class (a))
10300         {
10301         case dw_val_class_addr:
10302           size += DWARF2_ADDR_SIZE;
10303           break;
10304         case dw_val_class_offset:
10305           size += DWARF_OFFSET_SIZE;
10306           break;
10307         case dw_val_class_loc:
10308           {
10309             unsigned long lsize = size_of_locs (AT_loc (a));
10310
10311             /* Block length.  */
10312             if (dwarf_version >= 4)
10313               size += size_of_uleb128 (lsize);
10314             else
10315               size += constant_size (lsize);
10316             size += lsize;
10317           }
10318           break;
10319         case dw_val_class_loc_list:
10320           size += DWARF_OFFSET_SIZE;
10321           break;
10322         case dw_val_class_range_list:
10323           size += DWARF_OFFSET_SIZE;
10324           break;
10325         case dw_val_class_const:
10326           size += size_of_sleb128 (AT_int (a));
10327           break;
10328         case dw_val_class_unsigned_const:
10329           size += constant_size (AT_unsigned (a));
10330           break;
10331         case dw_val_class_const_double:
10332           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10333           if (HOST_BITS_PER_WIDE_INT >= 64)
10334             size++; /* block */
10335           break;
10336         case dw_val_class_vec:
10337           size += constant_size (a->dw_attr_val.v.val_vec.length
10338                                  * a->dw_attr_val.v.val_vec.elt_size)
10339                   + a->dw_attr_val.v.val_vec.length
10340                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10341           break;
10342         case dw_val_class_flag:
10343           if (dwarf_version >= 4)
10344             /* Currently all add_AT_flag calls pass in 1 as last argument,
10345                so DW_FORM_flag_present can be used.  If that ever changes,
10346                we'll need to use DW_FORM_flag and have some optimization
10347                in build_abbrev_table that will change those to
10348                DW_FORM_flag_present if it is set to 1 in all DIEs using
10349                the same abbrev entry.  */
10350             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10351           else
10352             size += 1;
10353           break;
10354         case dw_val_class_die_ref:
10355           if (AT_ref_external (a))
10356             {
10357               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10358                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10359                  is sized by target address length, whereas in DWARF3
10360                  it's always sized as an offset.  */
10361               if (dwarf_version >= 4)
10362                 size += DWARF_TYPE_SIGNATURE_SIZE;
10363               else if (dwarf_version == 2)
10364                 size += DWARF2_ADDR_SIZE;
10365               else
10366                 size += DWARF_OFFSET_SIZE;
10367             }
10368           else
10369             size += DWARF_OFFSET_SIZE;
10370           break;
10371         case dw_val_class_fde_ref:
10372           size += DWARF_OFFSET_SIZE;
10373           break;
10374         case dw_val_class_lbl_id:
10375           size += DWARF2_ADDR_SIZE;
10376           break;
10377         case dw_val_class_lineptr:
10378         case dw_val_class_macptr:
10379           size += DWARF_OFFSET_SIZE;
10380           break;
10381         case dw_val_class_str:
10382           if (AT_string_form (a) == DW_FORM_strp)
10383             size += DWARF_OFFSET_SIZE;
10384           else
10385             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10386           break;
10387         case dw_val_class_file:
10388           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10389           break;
10390         case dw_val_class_data8:
10391           size += 8;
10392           break;
10393         case dw_val_class_vms_delta:
10394           size += DWARF_OFFSET_SIZE;
10395           break;
10396         default:
10397           gcc_unreachable ();
10398         }
10399     }
10400
10401   return size;
10402 }
10403
10404 /* Size the debugging information associated with a given DIE.  Visits the
10405    DIE's children recursively.  Updates the global variable next_die_offset, on
10406    each time through.  Uses the current value of next_die_offset to update the
10407    die_offset field in each DIE.  */
10408
10409 static void
10410 calc_die_sizes (dw_die_ref die)
10411 {
10412   dw_die_ref c;
10413
10414   die->die_offset = next_die_offset;
10415   next_die_offset += size_of_die (die);
10416
10417   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10418
10419   if (die->die_child != NULL)
10420     /* Count the null byte used to terminate sibling lists.  */
10421     next_die_offset += 1;
10422 }
10423
10424 /* Set the marks for a die and its children.  We do this so
10425    that we know whether or not a reference needs to use FORM_ref_addr; only
10426    DIEs in the same CU will be marked.  We used to clear out the offset
10427    and use that as the flag, but ran into ordering problems.  */
10428
10429 static void
10430 mark_dies (dw_die_ref die)
10431 {
10432   dw_die_ref c;
10433
10434   gcc_assert (!die->die_mark);
10435
10436   die->die_mark = 1;
10437   FOR_EACH_CHILD (die, c, mark_dies (c));
10438 }
10439
10440 /* Clear the marks for a die and its children.  */
10441
10442 static void
10443 unmark_dies (dw_die_ref die)
10444 {
10445   dw_die_ref c;
10446
10447   if (dwarf_version < 4)
10448     gcc_assert (die->die_mark);
10449
10450   die->die_mark = 0;
10451   FOR_EACH_CHILD (die, c, unmark_dies (c));
10452 }
10453
10454 /* Clear the marks for a die, its children and referred dies.  */
10455
10456 static void
10457 unmark_all_dies (dw_die_ref die)
10458 {
10459   dw_die_ref c;
10460   dw_attr_ref a;
10461   unsigned ix;
10462
10463   if (!die->die_mark)
10464     return;
10465   die->die_mark = 0;
10466
10467   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10468
10469   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10470     if (AT_class (a) == dw_val_class_die_ref)
10471       unmark_all_dies (AT_ref (a));
10472 }
10473
10474 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10475    generated for the compilation unit.  */
10476
10477 static unsigned long
10478 size_of_pubnames (VEC (pubname_entry, gc) * names)
10479 {
10480   unsigned long size;
10481   unsigned i;
10482   pubname_ref p;
10483
10484   size = DWARF_PUBNAMES_HEADER_SIZE;
10485   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10486     if (names != pubtype_table
10487         || p->die->die_offset != 0
10488         || !flag_eliminate_unused_debug_types)
10489       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10490
10491   size += DWARF_OFFSET_SIZE;
10492   return size;
10493 }
10494
10495 /* Return the size of the information in the .debug_aranges section.  */
10496
10497 static unsigned long
10498 size_of_aranges (void)
10499 {
10500   unsigned long size;
10501
10502   size = DWARF_ARANGES_HEADER_SIZE;
10503
10504   /* Count the address/length pair for this compilation unit.  */
10505   if (text_section_used)
10506     size += 2 * DWARF2_ADDR_SIZE;
10507   if (cold_text_section_used)
10508     size += 2 * DWARF2_ADDR_SIZE;
10509   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10510
10511   /* Count the two zero words used to terminated the address range table.  */
10512   size += 2 * DWARF2_ADDR_SIZE;
10513   return size;
10514 }
10515 \f
10516 /* Select the encoding of an attribute value.  */
10517
10518 static enum dwarf_form
10519 value_format (dw_attr_ref a)
10520 {
10521   switch (a->dw_attr_val.val_class)
10522     {
10523     case dw_val_class_addr:
10524       /* Only very few attributes allow DW_FORM_addr.  */
10525       switch (a->dw_attr)
10526         {
10527         case DW_AT_low_pc:
10528         case DW_AT_high_pc:
10529         case DW_AT_entry_pc:
10530         case DW_AT_trampoline:
10531           return DW_FORM_addr;
10532         default:
10533           break;
10534         }
10535       switch (DWARF2_ADDR_SIZE)
10536         {
10537         case 1:
10538           return DW_FORM_data1;
10539         case 2:
10540           return DW_FORM_data2;
10541         case 4:
10542           return DW_FORM_data4;
10543         case 8:
10544           return DW_FORM_data8;
10545         default:
10546           gcc_unreachable ();
10547         }
10548     case dw_val_class_range_list:
10549     case dw_val_class_loc_list:
10550       if (dwarf_version >= 4)
10551         return DW_FORM_sec_offset;
10552       /* FALLTHRU */
10553     case dw_val_class_vms_delta:
10554     case dw_val_class_offset:
10555       switch (DWARF_OFFSET_SIZE)
10556         {
10557         case 4:
10558           return DW_FORM_data4;
10559         case 8:
10560           return DW_FORM_data8;
10561         default:
10562           gcc_unreachable ();
10563         }
10564     case dw_val_class_loc:
10565       if (dwarf_version >= 4)
10566         return DW_FORM_exprloc;
10567       switch (constant_size (size_of_locs (AT_loc (a))))
10568         {
10569         case 1:
10570           return DW_FORM_block1;
10571         case 2:
10572           return DW_FORM_block2;
10573         default:
10574           gcc_unreachable ();
10575         }
10576     case dw_val_class_const:
10577       return DW_FORM_sdata;
10578     case dw_val_class_unsigned_const:
10579       switch (constant_size (AT_unsigned (a)))
10580         {
10581         case 1:
10582           return DW_FORM_data1;
10583         case 2:
10584           return DW_FORM_data2;
10585         case 4:
10586           return DW_FORM_data4;
10587         case 8:
10588           return DW_FORM_data8;
10589         default:
10590           gcc_unreachable ();
10591         }
10592     case dw_val_class_const_double:
10593       switch (HOST_BITS_PER_WIDE_INT)
10594         {
10595         case 8:
10596           return DW_FORM_data2;
10597         case 16:
10598           return DW_FORM_data4;
10599         case 32:
10600           return DW_FORM_data8;
10601         case 64:
10602         default:
10603           return DW_FORM_block1;
10604         }
10605     case dw_val_class_vec:
10606       switch (constant_size (a->dw_attr_val.v.val_vec.length
10607                              * a->dw_attr_val.v.val_vec.elt_size))
10608         {
10609         case 1:
10610           return DW_FORM_block1;
10611         case 2:
10612           return DW_FORM_block2;
10613         case 4:
10614           return DW_FORM_block4;
10615         default:
10616           gcc_unreachable ();
10617         }
10618     case dw_val_class_flag:
10619       if (dwarf_version >= 4)
10620         {
10621           /* Currently all add_AT_flag calls pass in 1 as last argument,
10622              so DW_FORM_flag_present can be used.  If that ever changes,
10623              we'll need to use DW_FORM_flag and have some optimization
10624              in build_abbrev_table that will change those to
10625              DW_FORM_flag_present if it is set to 1 in all DIEs using
10626              the same abbrev entry.  */
10627           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10628           return DW_FORM_flag_present;
10629         }
10630       return DW_FORM_flag;
10631     case dw_val_class_die_ref:
10632       if (AT_ref_external (a))
10633         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10634       else
10635         return DW_FORM_ref;
10636     case dw_val_class_fde_ref:
10637       return DW_FORM_data;
10638     case dw_val_class_lbl_id:
10639       return DW_FORM_addr;
10640     case dw_val_class_lineptr:
10641     case dw_val_class_macptr:
10642       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10643     case dw_val_class_str:
10644       return AT_string_form (a);
10645     case dw_val_class_file:
10646       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10647         {
10648         case 1:
10649           return DW_FORM_data1;
10650         case 2:
10651           return DW_FORM_data2;
10652         case 4:
10653           return DW_FORM_data4;
10654         default:
10655           gcc_unreachable ();
10656         }
10657
10658     case dw_val_class_data8:
10659       return DW_FORM_data8;
10660
10661     default:
10662       gcc_unreachable ();
10663     }
10664 }
10665
10666 /* Output the encoding of an attribute value.  */
10667
10668 static void
10669 output_value_format (dw_attr_ref a)
10670 {
10671   enum dwarf_form form = value_format (a);
10672
10673   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10674 }
10675
10676 /* Output the .debug_abbrev section which defines the DIE abbreviation
10677    table.  */
10678
10679 static void
10680 output_abbrev_section (void)
10681 {
10682   unsigned long abbrev_id;
10683
10684   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10685     {
10686       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10687       unsigned ix;
10688       dw_attr_ref a_attr;
10689
10690       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10691       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10692                                    dwarf_tag_name (abbrev->die_tag));
10693
10694       if (abbrev->die_child != NULL)
10695         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10696       else
10697         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10698
10699       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10700            ix++)
10701         {
10702           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10703                                        dwarf_attr_name (a_attr->dw_attr));
10704           output_value_format (a_attr);
10705         }
10706
10707       dw2_asm_output_data (1, 0, NULL);
10708       dw2_asm_output_data (1, 0, NULL);
10709     }
10710
10711   /* Terminate the table.  */
10712   dw2_asm_output_data (1, 0, NULL);
10713 }
10714
10715 /* Output a symbol we can use to refer to this DIE from another CU.  */
10716
10717 static inline void
10718 output_die_symbol (dw_die_ref die)
10719 {
10720   char *sym = die->die_id.die_symbol;
10721
10722   if (sym == 0)
10723     return;
10724
10725   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10726     /* We make these global, not weak; if the target doesn't support
10727        .linkonce, it doesn't support combining the sections, so debugging
10728        will break.  */
10729     targetm.asm_out.globalize_label (asm_out_file, sym);
10730
10731   ASM_OUTPUT_LABEL (asm_out_file, sym);
10732 }
10733
10734 /* Return a new location list, given the begin and end range, and the
10735    expression.  */
10736
10737 static inline dw_loc_list_ref
10738 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10739               const char *section)
10740 {
10741   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10742
10743   retlist->begin = begin;
10744   retlist->end = end;
10745   retlist->expr = expr;
10746   retlist->section = section;
10747
10748   return retlist;
10749 }
10750
10751 /* Generate a new internal symbol for this location list node, if it
10752    hasn't got one yet.  */
10753
10754 static inline void
10755 gen_llsym (dw_loc_list_ref list)
10756 {
10757   gcc_assert (!list->ll_symbol);
10758   list->ll_symbol = gen_internal_sym ("LLST");
10759 }
10760
10761 /* Output the location list given to us.  */
10762
10763 static void
10764 output_loc_list (dw_loc_list_ref list_head)
10765 {
10766   dw_loc_list_ref curr = list_head;
10767
10768   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10769
10770   /* Walk the location list, and output each range + expression.  */
10771   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10772     {
10773       unsigned long size;
10774       /* Don't output an entry that starts and ends at the same address.  */
10775       if (strcmp (curr->begin, curr->end) == 0)
10776         continue;
10777       if (!have_multiple_function_sections)
10778         {
10779           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10780                                 "Location list begin address (%s)",
10781                                 list_head->ll_symbol);
10782           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10783                                 "Location list end address (%s)",
10784                                 list_head->ll_symbol);
10785         }
10786       else
10787         {
10788           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10789                                "Location list begin address (%s)",
10790                                list_head->ll_symbol);
10791           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10792                                "Location list end address (%s)",
10793                                list_head->ll_symbol);
10794         }
10795       size = size_of_locs (curr->expr);
10796
10797       /* Output the block length for this list of location operations.  */
10798       gcc_assert (size <= 0xffff);
10799       dw2_asm_output_data (2, size, "%s", "Location expression size");
10800
10801       output_loc_sequence (curr->expr);
10802     }
10803
10804   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10805                        "Location list terminator begin (%s)",
10806                        list_head->ll_symbol);
10807   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10808                        "Location list terminator end (%s)",
10809                        list_head->ll_symbol);
10810 }
10811
10812 /* Output a type signature.  */
10813
10814 static inline void
10815 output_signature (const char *sig, const char *name)
10816 {
10817   int i;
10818
10819   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10820     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10821 }
10822
10823 /* Output the DIE and its attributes.  Called recursively to generate
10824    the definitions of each child DIE.  */
10825
10826 static void
10827 output_die (dw_die_ref die)
10828 {
10829   dw_attr_ref a;
10830   dw_die_ref c;
10831   unsigned long size;
10832   unsigned ix;
10833
10834   /* If someone in another CU might refer to us, set up a symbol for
10835      them to point to.  */
10836   if (dwarf_version < 4 && die->die_id.die_symbol)
10837     output_die_symbol (die);
10838
10839   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10840                                (unsigned long)die->die_offset,
10841                                dwarf_tag_name (die->die_tag));
10842
10843   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10844     {
10845       const char *name = dwarf_attr_name (a->dw_attr);
10846
10847       switch (AT_class (a))
10848         {
10849         case dw_val_class_addr:
10850           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10851           break;
10852
10853         case dw_val_class_offset:
10854           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10855                                "%s", name);
10856           break;
10857
10858         case dw_val_class_range_list:
10859           {
10860             char *p = strchr (ranges_section_label, '\0');
10861
10862             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10863                      a->dw_attr_val.v.val_offset);
10864             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10865                                    debug_ranges_section, "%s", name);
10866             *p = '\0';
10867           }
10868           break;
10869
10870         case dw_val_class_loc:
10871           size = size_of_locs (AT_loc (a));
10872
10873           /* Output the block length for this list of location operations.  */
10874           if (dwarf_version >= 4)
10875             dw2_asm_output_data_uleb128 (size, "%s", name);
10876           else
10877             dw2_asm_output_data (constant_size (size), size, "%s", name);
10878
10879           output_loc_sequence (AT_loc (a));
10880           break;
10881
10882         case dw_val_class_const:
10883           /* ??? It would be slightly more efficient to use a scheme like is
10884              used for unsigned constants below, but gdb 4.x does not sign
10885              extend.  Gdb 5.x does sign extend.  */
10886           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10887           break;
10888
10889         case dw_val_class_unsigned_const:
10890           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10891                                AT_unsigned (a), "%s", name);
10892           break;
10893
10894         case dw_val_class_const_double:
10895           {
10896             unsigned HOST_WIDE_INT first, second;
10897
10898             if (HOST_BITS_PER_WIDE_INT >= 64)
10899               dw2_asm_output_data (1,
10900                                    2 * HOST_BITS_PER_WIDE_INT
10901                                    / HOST_BITS_PER_CHAR,
10902                                    NULL);
10903
10904             if (WORDS_BIG_ENDIAN)
10905               {
10906                 first = a->dw_attr_val.v.val_double.high;
10907                 second = a->dw_attr_val.v.val_double.low;
10908               }
10909             else
10910               {
10911                 first = a->dw_attr_val.v.val_double.low;
10912                 second = a->dw_attr_val.v.val_double.high;
10913               }
10914
10915             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10916                                  first, name);
10917             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10918                                  second, NULL);
10919           }
10920           break;
10921
10922         case dw_val_class_vec:
10923           {
10924             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10925             unsigned int len = a->dw_attr_val.v.val_vec.length;
10926             unsigned int i;
10927             unsigned char *p;
10928
10929             dw2_asm_output_data (constant_size (len * elt_size),
10930                                  len * elt_size, "%s", name);
10931             if (elt_size > sizeof (HOST_WIDE_INT))
10932               {
10933                 elt_size /= 2;
10934                 len *= 2;
10935               }
10936             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10937                  i < len;
10938                  i++, p += elt_size)
10939               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10940                                    "fp or vector constant word %u", i);
10941             break;
10942           }
10943
10944         case dw_val_class_flag:
10945           if (dwarf_version >= 4)
10946             {
10947               /* Currently all add_AT_flag calls pass in 1 as last argument,
10948                  so DW_FORM_flag_present can be used.  If that ever changes,
10949                  we'll need to use DW_FORM_flag and have some optimization
10950                  in build_abbrev_table that will change those to
10951                  DW_FORM_flag_present if it is set to 1 in all DIEs using
10952                  the same abbrev entry.  */
10953               gcc_assert (AT_flag (a) == 1);
10954               if (flag_debug_asm)
10955                 fprintf (asm_out_file, "\t\t\t%s %s\n",
10956                          ASM_COMMENT_START, name);
10957               break;
10958             }
10959           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10960           break;
10961
10962         case dw_val_class_loc_list:
10963           {
10964             char *sym = AT_loc_list (a)->ll_symbol;
10965
10966             gcc_assert (sym);
10967             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10968                                    "%s", name);
10969           }
10970           break;
10971
10972         case dw_val_class_die_ref:
10973           if (AT_ref_external (a))
10974             {
10975               if (dwarf_version >= 4)
10976                 {
10977                   comdat_type_node_ref type_node =
10978                     AT_ref (a)->die_id.die_type_node;
10979
10980                   gcc_assert (type_node);
10981                   output_signature (type_node->signature, name);
10982                 }
10983               else
10984                 {
10985                   char *sym = AT_ref (a)->die_id.die_symbol;
10986                   int size;
10987
10988                   gcc_assert (sym);
10989                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
10990                      length, whereas in DWARF3 it's always sized as an
10991                      offset.  */
10992                   if (dwarf_version == 2)
10993                     size = DWARF2_ADDR_SIZE;
10994                   else
10995                     size = DWARF_OFFSET_SIZE;
10996                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10997                                          name);
10998                 }
10999             }
11000           else
11001             {
11002               gcc_assert (AT_ref (a)->die_offset);
11003               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11004                                    "%s", name);
11005             }
11006           break;
11007
11008         case dw_val_class_fde_ref:
11009           {
11010             char l1[20];
11011
11012             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11013                                          a->dw_attr_val.v.val_fde_index * 2);
11014             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11015                                    "%s", name);
11016           }
11017           break;
11018
11019         case dw_val_class_vms_delta:
11020           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11021                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11022                                     "%s", name);
11023           break;
11024
11025         case dw_val_class_lbl_id:
11026           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11027           break;
11028
11029         case dw_val_class_lineptr:
11030           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11031                                  debug_line_section, "%s", name);
11032           break;
11033
11034         case dw_val_class_macptr:
11035           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11036                                  debug_macinfo_section, "%s", name);
11037           break;
11038
11039         case dw_val_class_str:
11040           if (AT_string_form (a) == DW_FORM_strp)
11041             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11042                                    a->dw_attr_val.v.val_str->label,
11043                                    debug_str_section,
11044                                    "%s: \"%s\"", name, AT_string (a));
11045           else
11046             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11047           break;
11048
11049         case dw_val_class_file:
11050           {
11051             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11052
11053             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11054                                  a->dw_attr_val.v.val_file->filename);
11055             break;
11056           }
11057
11058         case dw_val_class_data8:
11059           {
11060             int i;
11061
11062             for (i = 0; i < 8; i++)
11063               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11064                                    i == 0 ? "%s" : NULL, name);
11065             break;
11066           }
11067
11068         default:
11069           gcc_unreachable ();
11070         }
11071     }
11072
11073   FOR_EACH_CHILD (die, c, output_die (c));
11074
11075   /* Add null byte to terminate sibling list.  */
11076   if (die->die_child != NULL)
11077     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11078                          (unsigned long) die->die_offset);
11079 }
11080
11081 /* Output the compilation unit that appears at the beginning of the
11082    .debug_info section, and precedes the DIE descriptions.  */
11083
11084 static void
11085 output_compilation_unit_header (void)
11086 {
11087   int ver = dwarf_version;
11088
11089   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11090     dw2_asm_output_data (4, 0xffffffff,
11091       "Initial length escape value indicating 64-bit DWARF extension");
11092   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11093                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11094                        "Length of Compilation Unit Info");
11095   dw2_asm_output_data (2, ver, "DWARF version number");
11096   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11097                          debug_abbrev_section,
11098                          "Offset Into Abbrev. Section");
11099   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11100 }
11101
11102 /* Output the compilation unit DIE and its children.  */
11103
11104 static void
11105 output_comp_unit (dw_die_ref die, int output_if_empty)
11106 {
11107   const char *secname;
11108   char *oldsym, *tmp;
11109
11110   /* Unless we are outputting main CU, we may throw away empty ones.  */
11111   if (!output_if_empty && die->die_child == NULL)
11112     return;
11113
11114   /* Even if there are no children of this DIE, we must output the information
11115      about the compilation unit.  Otherwise, on an empty translation unit, we
11116      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11117      will then complain when examining the file.  First mark all the DIEs in
11118      this CU so we know which get local refs.  */
11119   mark_dies (die);
11120
11121   build_abbrev_table (die);
11122
11123   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11124   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11125   calc_die_sizes (die);
11126
11127   oldsym = die->die_id.die_symbol;
11128   if (oldsym)
11129     {
11130       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11131
11132       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11133       secname = tmp;
11134       die->die_id.die_symbol = NULL;
11135       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11136     }
11137   else
11138     switch_to_section (debug_info_section);
11139
11140   /* Output debugging information.  */
11141   output_compilation_unit_header ();
11142   output_die (die);
11143
11144   /* Leave the marks on the main CU, so we can check them in
11145      output_pubnames.  */
11146   if (oldsym)
11147     {
11148       unmark_dies (die);
11149       die->die_id.die_symbol = oldsym;
11150     }
11151 }
11152
11153 /* Output a comdat type unit DIE and its children.  */
11154
11155 static void
11156 output_comdat_type_unit (comdat_type_node *node)
11157 {
11158   const char *secname;
11159   char *tmp;
11160   int i;
11161 #if defined (OBJECT_FORMAT_ELF)
11162   tree comdat_key;
11163 #endif
11164
11165   /* First mark all the DIEs in this CU so we know which get local refs.  */
11166   mark_dies (node->root_die);
11167
11168   build_abbrev_table (node->root_die);
11169
11170   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11171   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11172   calc_die_sizes (node->root_die);
11173
11174 #if defined (OBJECT_FORMAT_ELF)
11175   secname = ".debug_types";
11176   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11177   sprintf (tmp, "wt.");
11178   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11179     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11180   comdat_key = get_identifier (tmp);
11181   targetm.asm_out.named_section (secname,
11182                                  SECTION_DEBUG | SECTION_LINKONCE,
11183                                  comdat_key);
11184 #else
11185   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11186   sprintf (tmp, ".gnu.linkonce.wt.");
11187   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11188     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11189   secname = tmp;
11190   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11191 #endif
11192
11193   /* Output debugging information.  */
11194   output_compilation_unit_header ();
11195   output_signature (node->signature, "Type Signature");
11196   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11197                        "Offset to Type DIE");
11198   output_die (node->root_die);
11199
11200   unmark_dies (node->root_die);
11201 }
11202
11203 /* Return the DWARF2/3 pubname associated with a decl.  */
11204
11205 static const char *
11206 dwarf2_name (tree decl, int scope)
11207 {
11208   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11209 }
11210
11211 /* Add a new entry to .debug_pubnames if appropriate.  */
11212
11213 static void
11214 add_pubname_string (const char *str, dw_die_ref die)
11215 {
11216   pubname_entry e;
11217
11218   e.die = die;
11219   e.name = xstrdup (str);
11220   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11221 }
11222
11223 static void
11224 add_pubname (tree decl, dw_die_ref die)
11225 {
11226   if (TREE_PUBLIC (decl))
11227     {
11228       const char *name = dwarf2_name (decl, 1);
11229       if (name)
11230         add_pubname_string (name, die);
11231     }
11232 }
11233
11234 /* Add a new entry to .debug_pubtypes if appropriate.  */
11235
11236 static void
11237 add_pubtype (tree decl, dw_die_ref die)
11238 {
11239   pubname_entry e;
11240
11241   e.name = NULL;
11242   if ((TREE_PUBLIC (decl)
11243        || die->die_parent == comp_unit_die)
11244       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11245     {
11246       e.die = die;
11247       if (TYPE_P (decl))
11248         {
11249           if (TYPE_NAME (decl))
11250             {
11251               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11252                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11253               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11254                        && DECL_NAME (TYPE_NAME (decl)))
11255                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11256               else
11257                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11258             }
11259         }
11260       else
11261         {
11262           e.name = dwarf2_name (decl, 1);
11263           if (e.name)
11264             e.name = xstrdup (e.name);
11265         }
11266
11267       /* If we don't have a name for the type, there's no point in adding
11268          it to the table.  */
11269       if (e.name && e.name[0] != '\0')
11270         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11271     }
11272 }
11273
11274 /* Output the public names table used to speed up access to externally
11275    visible names; or the public types table used to find type definitions.  */
11276
11277 static void
11278 output_pubnames (VEC (pubname_entry, gc) * names)
11279 {
11280   unsigned i;
11281   unsigned long pubnames_length = size_of_pubnames (names);
11282   pubname_ref pub;
11283
11284   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11285     dw2_asm_output_data (4, 0xffffffff,
11286       "Initial length escape value indicating 64-bit DWARF extension");
11287   if (names == pubname_table)
11288     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11289                          "Length of Public Names Info");
11290   else
11291     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11292                          "Length of Public Type Names Info");
11293   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11294   dw2_asm_output_data (2, 2, "DWARF Version");
11295   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11296                          debug_info_section,
11297                          "Offset of Compilation Unit Info");
11298   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11299                        "Compilation Unit Length");
11300
11301   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11302     {
11303       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11304       if (names == pubname_table)
11305         gcc_assert (pub->die->die_mark);
11306
11307       if (names != pubtype_table
11308           || pub->die->die_offset != 0
11309           || !flag_eliminate_unused_debug_types)
11310         {
11311           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11312                                "DIE offset");
11313
11314           dw2_asm_output_nstring (pub->name, -1, "external name");
11315         }
11316     }
11317
11318   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11319 }
11320
11321 /* Add a new entry to .debug_aranges if appropriate.  */
11322
11323 static void
11324 add_arange (tree decl, dw_die_ref die)
11325 {
11326   if (! DECL_SECTION_NAME (decl))
11327     return;
11328
11329   if (arange_table_in_use == arange_table_allocated)
11330     {
11331       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11332       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11333                                     arange_table_allocated);
11334       memset (arange_table + arange_table_in_use, 0,
11335               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11336     }
11337
11338   arange_table[arange_table_in_use++] = die;
11339 }
11340
11341 /* Output the information that goes into the .debug_aranges table.
11342    Namely, define the beginning and ending address range of the
11343    text section generated for this compilation unit.  */
11344
11345 static void
11346 output_aranges (void)
11347 {
11348   unsigned i;
11349   unsigned long aranges_length = size_of_aranges ();
11350
11351   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11352     dw2_asm_output_data (4, 0xffffffff,
11353       "Initial length escape value indicating 64-bit DWARF extension");
11354   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11355                        "Length of Address Ranges Info");
11356   /* Version number for aranges is still 2, even in DWARF3.  */
11357   dw2_asm_output_data (2, 2, "DWARF Version");
11358   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11359                          debug_info_section,
11360                          "Offset of Compilation Unit Info");
11361   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11362   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11363
11364   /* We need to align to twice the pointer size here.  */
11365   if (DWARF_ARANGES_PAD_SIZE)
11366     {
11367       /* Pad using a 2 byte words so that padding is correct for any
11368          pointer size.  */
11369       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11370                            2 * DWARF2_ADDR_SIZE);
11371       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11372         dw2_asm_output_data (2, 0, NULL);
11373     }
11374
11375   /* It is necessary not to output these entries if the sections were
11376      not used; if the sections were not used, the length will be 0 and
11377      the address may end up as 0 if the section is discarded by ld
11378      --gc-sections, leaving an invalid (0, 0) entry that can be
11379      confused with the terminator.  */
11380   if (text_section_used)
11381     {
11382       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11383       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11384                             text_section_label, "Length");
11385     }
11386   if (cold_text_section_used)
11387     {
11388       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11389                            "Address");
11390       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11391                             cold_text_section_label, "Length");
11392     }
11393
11394   for (i = 0; i < arange_table_in_use; i++)
11395     {
11396       dw_die_ref die = arange_table[i];
11397
11398       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11399       gcc_assert (die->die_mark);
11400
11401       if (die->die_tag == DW_TAG_subprogram)
11402         {
11403           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11404                                "Address");
11405           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11406                                 get_AT_low_pc (die), "Length");
11407         }
11408       else
11409         {
11410           /* A static variable; extract the symbol from DW_AT_location.
11411              Note that this code isn't currently hit, as we only emit
11412              aranges for functions (jason 9/23/99).  */
11413           dw_attr_ref a = get_AT (die, DW_AT_location);
11414           dw_loc_descr_ref loc;
11415
11416           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11417
11418           loc = AT_loc (a);
11419           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11420
11421           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11422                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11423           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11424                                get_AT_unsigned (die, DW_AT_byte_size),
11425                                "Length");
11426         }
11427     }
11428
11429   /* Output the terminator words.  */
11430   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11431   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11432 }
11433
11434 /* Add a new entry to .debug_ranges.  Return the offset at which it
11435    was placed.  */
11436
11437 static unsigned int
11438 add_ranges_num (int num)
11439 {
11440   unsigned int in_use = ranges_table_in_use;
11441
11442   if (in_use == ranges_table_allocated)
11443     {
11444       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11445       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11446                                     ranges_table_allocated);
11447       memset (ranges_table + ranges_table_in_use, 0,
11448               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11449     }
11450
11451   ranges_table[in_use].num = num;
11452   ranges_table_in_use = in_use + 1;
11453
11454   return in_use * 2 * DWARF2_ADDR_SIZE;
11455 }
11456
11457 /* Add a new entry to .debug_ranges corresponding to a block, or a
11458    range terminator if BLOCK is NULL.  */
11459
11460 static unsigned int
11461 add_ranges (const_tree block)
11462 {
11463   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11464 }
11465
11466 /* Add a new entry to .debug_ranges corresponding to a pair of
11467    labels.  */
11468
11469 static void
11470 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11471                       bool *added)
11472 {
11473   unsigned int in_use = ranges_by_label_in_use;
11474   unsigned int offset;
11475
11476   if (in_use == ranges_by_label_allocated)
11477     {
11478       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11479       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11480                                        ranges_by_label,
11481                                        ranges_by_label_allocated);
11482       memset (ranges_by_label + ranges_by_label_in_use, 0,
11483               RANGES_TABLE_INCREMENT
11484               * sizeof (struct dw_ranges_by_label_struct));
11485     }
11486
11487   ranges_by_label[in_use].begin = begin;
11488   ranges_by_label[in_use].end = end;
11489   ranges_by_label_in_use = in_use + 1;
11490
11491   offset = add_ranges_num (-(int)in_use - 1);
11492   if (!*added)
11493     {
11494       add_AT_range_list (die, DW_AT_ranges, offset);
11495       *added = true;
11496     }
11497 }
11498
11499 static void
11500 output_ranges (void)
11501 {
11502   unsigned i;
11503   static const char *const start_fmt = "Offset %#x";
11504   const char *fmt = start_fmt;
11505
11506   for (i = 0; i < ranges_table_in_use; i++)
11507     {
11508       int block_num = ranges_table[i].num;
11509
11510       if (block_num > 0)
11511         {
11512           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11513           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11514
11515           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11516           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11517
11518           /* If all code is in the text section, then the compilation
11519              unit base address defaults to DW_AT_low_pc, which is the
11520              base of the text section.  */
11521           if (!have_multiple_function_sections)
11522             {
11523               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11524                                     text_section_label,
11525                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11526               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11527                                     text_section_label, NULL);
11528             }
11529
11530           /* Otherwise, the compilation unit base address is zero,
11531              which allows us to use absolute addresses, and not worry
11532              about whether the target supports cross-section
11533              arithmetic.  */
11534           else
11535             {
11536               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11537                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11538               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11539             }
11540
11541           fmt = NULL;
11542         }
11543
11544       /* Negative block_num stands for an index into ranges_by_label.  */
11545       else if (block_num < 0)
11546         {
11547           int lab_idx = - block_num - 1;
11548
11549           if (!have_multiple_function_sections)
11550             {
11551               gcc_unreachable ();
11552 #if 0
11553               /* If we ever use add_ranges_by_labels () for a single
11554                  function section, all we have to do is to take out
11555                  the #if 0 above.  */
11556               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11557                                     ranges_by_label[lab_idx].begin,
11558                                     text_section_label,
11559                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11560               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11561                                     ranges_by_label[lab_idx].end,
11562                                     text_section_label, NULL);
11563 #endif
11564             }
11565           else
11566             {
11567               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11568                                    ranges_by_label[lab_idx].begin,
11569                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11570               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11571                                    ranges_by_label[lab_idx].end,
11572                                    NULL);
11573             }
11574         }
11575       else
11576         {
11577           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11578           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11579           fmt = start_fmt;
11580         }
11581     }
11582 }
11583
11584 /* Data structure containing information about input files.  */
11585 struct file_info
11586 {
11587   const char *path;     /* Complete file name.  */
11588   const char *fname;    /* File name part.  */
11589   int length;           /* Length of entire string.  */
11590   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11591   int dir_idx;          /* Index in directory table.  */
11592 };
11593
11594 /* Data structure containing information about directories with source
11595    files.  */
11596 struct dir_info
11597 {
11598   const char *path;     /* Path including directory name.  */
11599   int length;           /* Path length.  */
11600   int prefix;           /* Index of directory entry which is a prefix.  */
11601   int count;            /* Number of files in this directory.  */
11602   int dir_idx;          /* Index of directory used as base.  */
11603 };
11604
11605 /* Callback function for file_info comparison.  We sort by looking at
11606    the directories in the path.  */
11607
11608 static int
11609 file_info_cmp (const void *p1, const void *p2)
11610 {
11611   const struct file_info *const s1 = (const struct file_info *) p1;
11612   const struct file_info *const s2 = (const struct file_info *) p2;
11613   const unsigned char *cp1;
11614   const unsigned char *cp2;
11615
11616   /* Take care of file names without directories.  We need to make sure that
11617      we return consistent values to qsort since some will get confused if
11618      we return the same value when identical operands are passed in opposite
11619      orders.  So if neither has a directory, return 0 and otherwise return
11620      1 or -1 depending on which one has the directory.  */
11621   if ((s1->path == s1->fname || s2->path == s2->fname))
11622     return (s2->path == s2->fname) - (s1->path == s1->fname);
11623
11624   cp1 = (const unsigned char *) s1->path;
11625   cp2 = (const unsigned char *) s2->path;
11626
11627   while (1)
11628     {
11629       ++cp1;
11630       ++cp2;
11631       /* Reached the end of the first path?  If so, handle like above.  */
11632       if ((cp1 == (const unsigned char *) s1->fname)
11633           || (cp2 == (const unsigned char *) s2->fname))
11634         return ((cp2 == (const unsigned char *) s2->fname)
11635                 - (cp1 == (const unsigned char *) s1->fname));
11636
11637       /* Character of current path component the same?  */
11638       else if (*cp1 != *cp2)
11639         return *cp1 - *cp2;
11640     }
11641 }
11642
11643 struct file_name_acquire_data
11644 {
11645   struct file_info *files;
11646   int used_files;
11647   int max_files;
11648 };
11649
11650 /* Traversal function for the hash table.  */
11651
11652 static int
11653 file_name_acquire (void ** slot, void *data)
11654 {
11655   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11656   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11657   struct file_info *fi;
11658   const char *f;
11659
11660   gcc_assert (fnad->max_files >= d->emitted_number);
11661
11662   if (! d->emitted_number)
11663     return 1;
11664
11665   gcc_assert (fnad->max_files != fnad->used_files);
11666
11667   fi = fnad->files + fnad->used_files++;
11668
11669   /* Skip all leading "./".  */
11670   f = d->filename;
11671   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11672     f += 2;
11673
11674   /* Create a new array entry.  */
11675   fi->path = f;
11676   fi->length = strlen (f);
11677   fi->file_idx = d;
11678
11679   /* Search for the file name part.  */
11680   f = strrchr (f, DIR_SEPARATOR);
11681 #if defined (DIR_SEPARATOR_2)
11682   {
11683     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11684
11685     if (g != NULL)
11686       {
11687         if (f == NULL || f < g)
11688           f = g;
11689       }
11690   }
11691 #endif
11692
11693   fi->fname = f == NULL ? fi->path : f + 1;
11694   return 1;
11695 }
11696
11697 /* Output the directory table and the file name table.  We try to minimize
11698    the total amount of memory needed.  A heuristic is used to avoid large
11699    slowdowns with many input files.  */
11700
11701 static void
11702 output_file_names (void)
11703 {
11704   struct file_name_acquire_data fnad;
11705   int numfiles;
11706   struct file_info *files;
11707   struct dir_info *dirs;
11708   int *saved;
11709   int *savehere;
11710   int *backmap;
11711   int ndirs;
11712   int idx_offset;
11713   int i;
11714
11715   if (!last_emitted_file)
11716     {
11717       dw2_asm_output_data (1, 0, "End directory table");
11718       dw2_asm_output_data (1, 0, "End file name table");
11719       return;
11720     }
11721
11722   numfiles = last_emitted_file->emitted_number;
11723
11724   /* Allocate the various arrays we need.  */
11725   files = XALLOCAVEC (struct file_info, numfiles);
11726   dirs = XALLOCAVEC (struct dir_info, numfiles);
11727
11728   fnad.files = files;
11729   fnad.used_files = 0;
11730   fnad.max_files = numfiles;
11731   htab_traverse (file_table, file_name_acquire, &fnad);
11732   gcc_assert (fnad.used_files == fnad.max_files);
11733
11734   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11735
11736   /* Find all the different directories used.  */
11737   dirs[0].path = files[0].path;
11738   dirs[0].length = files[0].fname - files[0].path;
11739   dirs[0].prefix = -1;
11740   dirs[0].count = 1;
11741   dirs[0].dir_idx = 0;
11742   files[0].dir_idx = 0;
11743   ndirs = 1;
11744
11745   for (i = 1; i < numfiles; i++)
11746     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11747         && memcmp (dirs[ndirs - 1].path, files[i].path,
11748                    dirs[ndirs - 1].length) == 0)
11749       {
11750         /* Same directory as last entry.  */
11751         files[i].dir_idx = ndirs - 1;
11752         ++dirs[ndirs - 1].count;
11753       }
11754     else
11755       {
11756         int j;
11757
11758         /* This is a new directory.  */
11759         dirs[ndirs].path = files[i].path;
11760         dirs[ndirs].length = files[i].fname - files[i].path;
11761         dirs[ndirs].count = 1;
11762         dirs[ndirs].dir_idx = ndirs;
11763         files[i].dir_idx = ndirs;
11764
11765         /* Search for a prefix.  */
11766         dirs[ndirs].prefix = -1;
11767         for (j = 0; j < ndirs; j++)
11768           if (dirs[j].length < dirs[ndirs].length
11769               && dirs[j].length > 1
11770               && (dirs[ndirs].prefix == -1
11771                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11772               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11773             dirs[ndirs].prefix = j;
11774
11775         ++ndirs;
11776       }
11777
11778   /* Now to the actual work.  We have to find a subset of the directories which
11779      allow expressing the file name using references to the directory table
11780      with the least amount of characters.  We do not do an exhaustive search
11781      where we would have to check out every combination of every single
11782      possible prefix.  Instead we use a heuristic which provides nearly optimal
11783      results in most cases and never is much off.  */
11784   saved = XALLOCAVEC (int, ndirs);
11785   savehere = XALLOCAVEC (int, ndirs);
11786
11787   memset (saved, '\0', ndirs * sizeof (saved[0]));
11788   for (i = 0; i < ndirs; i++)
11789     {
11790       int j;
11791       int total;
11792
11793       /* We can always save some space for the current directory.  But this
11794          does not mean it will be enough to justify adding the directory.  */
11795       savehere[i] = dirs[i].length;
11796       total = (savehere[i] - saved[i]) * dirs[i].count;
11797
11798       for (j = i + 1; j < ndirs; j++)
11799         {
11800           savehere[j] = 0;
11801           if (saved[j] < dirs[i].length)
11802             {
11803               /* Determine whether the dirs[i] path is a prefix of the
11804                  dirs[j] path.  */
11805               int k;
11806
11807               k = dirs[j].prefix;
11808               while (k != -1 && k != (int) i)
11809                 k = dirs[k].prefix;
11810
11811               if (k == (int) i)
11812                 {
11813                   /* Yes it is.  We can possibly save some memory by
11814                      writing the filenames in dirs[j] relative to
11815                      dirs[i].  */
11816                   savehere[j] = dirs[i].length;
11817                   total += (savehere[j] - saved[j]) * dirs[j].count;
11818                 }
11819             }
11820         }
11821
11822       /* Check whether we can save enough to justify adding the dirs[i]
11823          directory.  */
11824       if (total > dirs[i].length + 1)
11825         {
11826           /* It's worthwhile adding.  */
11827           for (j = i; j < ndirs; j++)
11828             if (savehere[j] > 0)
11829               {
11830                 /* Remember how much we saved for this directory so far.  */
11831                 saved[j] = savehere[j];
11832
11833                 /* Remember the prefix directory.  */
11834                 dirs[j].dir_idx = i;
11835               }
11836         }
11837     }
11838
11839   /* Emit the directory name table.  */
11840   idx_offset = dirs[0].length > 0 ? 1 : 0;
11841   for (i = 1 - idx_offset; i < ndirs; i++)
11842     dw2_asm_output_nstring (dirs[i].path,
11843                             dirs[i].length
11844                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11845                             "Directory Entry: %#x", i + idx_offset);
11846
11847   dw2_asm_output_data (1, 0, "End directory table");
11848
11849   /* We have to emit them in the order of emitted_number since that's
11850      used in the debug info generation.  To do this efficiently we
11851      generate a back-mapping of the indices first.  */
11852   backmap = XALLOCAVEC (int, numfiles);
11853   for (i = 0; i < numfiles; i++)
11854     backmap[files[i].file_idx->emitted_number - 1] = i;
11855
11856   /* Now write all the file names.  */
11857   for (i = 0; i < numfiles; i++)
11858     {
11859       int file_idx = backmap[i];
11860       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11861
11862 #ifdef VMS_DEBUGGING_INFO
11863 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11864
11865       /* Setting these fields can lead to debugger miscomparisons,
11866          but VMS Debug requires them to be set correctly.  */
11867
11868       int ver;
11869       long long cdt;
11870       long siz;
11871       int maxfilelen = strlen (files[file_idx].path)
11872                                + dirs[dir_idx].length
11873                                + MAX_VMS_VERSION_LEN + 1;
11874       char *filebuf = XALLOCAVEC (char, maxfilelen);
11875
11876       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11877       snprintf (filebuf, maxfilelen, "%s;%d",
11878                 files[file_idx].path + dirs[dir_idx].length, ver);
11879
11880       dw2_asm_output_nstring
11881         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11882
11883       /* Include directory index.  */
11884       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11885
11886       /* Modification time.  */
11887       dw2_asm_output_data_uleb128
11888         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11889           ? cdt : 0,
11890          NULL);
11891
11892       /* File length in bytes.  */
11893       dw2_asm_output_data_uleb128
11894         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11895           ? siz : 0,
11896          NULL);
11897 #else
11898       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11899                               "File Entry: %#x", (unsigned) i + 1);
11900
11901       /* Include directory index.  */
11902       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11903
11904       /* Modification time.  */
11905       dw2_asm_output_data_uleb128 (0, NULL);
11906
11907       /* File length in bytes.  */
11908       dw2_asm_output_data_uleb128 (0, NULL);
11909 #endif
11910     }
11911
11912   dw2_asm_output_data (1, 0, "End file name table");
11913 }
11914
11915
11916 /* Output the source line number correspondence information.  This
11917    information goes into the .debug_line section.  */
11918
11919 static void
11920 output_line_info (void)
11921 {
11922   char l1[20], l2[20], p1[20], p2[20];
11923   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11924   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11925   unsigned opc;
11926   unsigned n_op_args;
11927   unsigned long lt_index;
11928   unsigned long current_line;
11929   long line_offset;
11930   long line_delta;
11931   unsigned long current_file;
11932   unsigned long function;
11933   int ver = dwarf_version;
11934
11935   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11936   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11937   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11938   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11939
11940   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11941     dw2_asm_output_data (4, 0xffffffff,
11942       "Initial length escape value indicating 64-bit DWARF extension");
11943   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11944                         "Length of Source Line Info");
11945   ASM_OUTPUT_LABEL (asm_out_file, l1);
11946
11947   dw2_asm_output_data (2, ver, "DWARF Version");
11948   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11949   ASM_OUTPUT_LABEL (asm_out_file, p1);
11950
11951   /* Define the architecture-dependent minimum instruction length (in
11952    bytes).  In this implementation of DWARF, this field is used for
11953    information purposes only.  Since GCC generates assembly language,
11954    we have no a priori knowledge of how many instruction bytes are
11955    generated for each source line, and therefore can use only the
11956    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11957    commands.  Accordingly, we fix this as `1', which is "correct
11958    enough" for all architectures, and don't let the target override.  */
11959   dw2_asm_output_data (1, 1,
11960                        "Minimum Instruction Length");
11961
11962   if (ver >= 4)
11963     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11964                          "Maximum Operations Per Instruction");
11965   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11966                        "Default is_stmt_start flag");
11967   dw2_asm_output_data (1, DWARF_LINE_BASE,
11968                        "Line Base Value (Special Opcodes)");
11969   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11970                        "Line Range Value (Special Opcodes)");
11971   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11972                        "Special Opcode Base");
11973
11974   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11975     {
11976       switch (opc)
11977         {
11978         case DW_LNS_advance_pc:
11979         case DW_LNS_advance_line:
11980         case DW_LNS_set_file:
11981         case DW_LNS_set_column:
11982         case DW_LNS_fixed_advance_pc:
11983           n_op_args = 1;
11984           break;
11985         default:
11986           n_op_args = 0;
11987           break;
11988         }
11989
11990       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11991                            opc, n_op_args);
11992     }
11993
11994   /* Write out the information about the files we use.  */
11995   output_file_names ();
11996   ASM_OUTPUT_LABEL (asm_out_file, p2);
11997
11998   /* We used to set the address register to the first location in the text
11999      section here, but that didn't accomplish anything since we already
12000      have a line note for the opening brace of the first function.  */
12001
12002   /* Generate the line number to PC correspondence table, encoded as
12003      a series of state machine operations.  */
12004   current_file = 1;
12005   current_line = 1;
12006
12007   if (cfun && in_cold_section_p)
12008     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12009   else
12010     strcpy (prev_line_label, text_section_label);
12011   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12012     {
12013       dw_line_info_ref line_info = &line_info_table[lt_index];
12014
12015 #if 0
12016       /* Disable this optimization for now; GDB wants to see two line notes
12017          at the beginning of a function so it can find the end of the
12018          prologue.  */
12019
12020       /* Don't emit anything for redundant notes.  Just updating the
12021          address doesn't accomplish anything, because we already assume
12022          that anything after the last address is this line.  */
12023       if (line_info->dw_line_num == current_line
12024           && line_info->dw_file_num == current_file)
12025         continue;
12026 #endif
12027
12028       /* Emit debug info for the address of the current line.
12029
12030          Unfortunately, we have little choice here currently, and must always
12031          use the most general form.  GCC does not know the address delta
12032          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12033          attributes which will give an upper bound on the address range.  We
12034          could perhaps use length attributes to determine when it is safe to
12035          use DW_LNS_fixed_advance_pc.  */
12036
12037       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12038       if (0)
12039         {
12040           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12041           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12042                                "DW_LNS_fixed_advance_pc");
12043           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12044         }
12045       else
12046         {
12047           /* This can handle any delta.  This takes
12048              4+DWARF2_ADDR_SIZE bytes.  */
12049           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12050           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12051           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12052           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12053         }
12054
12055       strcpy (prev_line_label, line_label);
12056
12057       /* Emit debug info for the source file of the current line, if
12058          different from the previous line.  */
12059       if (line_info->dw_file_num != current_file)
12060         {
12061           current_file = line_info->dw_file_num;
12062           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12063           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12064         }
12065
12066       /* Emit debug info for the current line number, choosing the encoding
12067          that uses the least amount of space.  */
12068       if (line_info->dw_line_num != current_line)
12069         {
12070           line_offset = line_info->dw_line_num - current_line;
12071           line_delta = line_offset - DWARF_LINE_BASE;
12072           current_line = line_info->dw_line_num;
12073           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12074             /* This can handle deltas from -10 to 234, using the current
12075                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12076                takes 1 byte.  */
12077             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12078                                  "line %lu", current_line);
12079           else
12080             {
12081               /* This can handle any delta.  This takes at least 4 bytes,
12082                  depending on the value being encoded.  */
12083               dw2_asm_output_data (1, DW_LNS_advance_line,
12084                                    "advance to line %lu", current_line);
12085               dw2_asm_output_data_sleb128 (line_offset, NULL);
12086               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12087             }
12088         }
12089       else
12090         /* We still need to start a new row, so output a copy insn.  */
12091         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12092     }
12093
12094   /* Emit debug info for the address of the end of the function.  */
12095   if (0)
12096     {
12097       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12098                            "DW_LNS_fixed_advance_pc");
12099       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12100     }
12101   else
12102     {
12103       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12104       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12105       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12106       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12107     }
12108
12109   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12110   dw2_asm_output_data_uleb128 (1, NULL);
12111   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12112
12113   function = 0;
12114   current_file = 1;
12115   current_line = 1;
12116   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12117     {
12118       dw_separate_line_info_ref line_info
12119         = &separate_line_info_table[lt_index];
12120
12121 #if 0
12122       /* Don't emit anything for redundant notes.  */
12123       if (line_info->dw_line_num == current_line
12124           && line_info->dw_file_num == current_file
12125           && line_info->function == function)
12126         goto cont;
12127 #endif
12128
12129       /* Emit debug info for the address of the current line.  If this is
12130          a new function, or the first line of a function, then we need
12131          to handle it differently.  */
12132       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12133                                    lt_index);
12134       if (function != line_info->function)
12135         {
12136           function = line_info->function;
12137
12138           /* Set the address register to the first line in the function.  */
12139           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12140           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12141           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12142           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12143         }
12144       else
12145         {
12146           /* ??? See the DW_LNS_advance_pc comment above.  */
12147           if (0)
12148             {
12149               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12150                                    "DW_LNS_fixed_advance_pc");
12151               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12152             }
12153           else
12154             {
12155               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12156               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12157               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12158               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12159             }
12160         }
12161
12162       strcpy (prev_line_label, line_label);
12163
12164       /* Emit debug info for the source file of the current line, if
12165          different from the previous line.  */
12166       if (line_info->dw_file_num != current_file)
12167         {
12168           current_file = line_info->dw_file_num;
12169           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12170           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12171         }
12172
12173       /* Emit debug info for the current line number, choosing the encoding
12174          that uses the least amount of space.  */
12175       if (line_info->dw_line_num != current_line)
12176         {
12177           line_offset = line_info->dw_line_num - current_line;
12178           line_delta = line_offset - DWARF_LINE_BASE;
12179           current_line = line_info->dw_line_num;
12180           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12181             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12182                                  "line %lu", current_line);
12183           else
12184             {
12185               dw2_asm_output_data (1, DW_LNS_advance_line,
12186                                    "advance to line %lu", current_line);
12187               dw2_asm_output_data_sleb128 (line_offset, NULL);
12188               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12189             }
12190         }
12191       else
12192         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12193
12194 #if 0
12195     cont:
12196 #endif
12197
12198       lt_index++;
12199
12200       /* If we're done with a function, end its sequence.  */
12201       if (lt_index == separate_line_info_table_in_use
12202           || separate_line_info_table[lt_index].function != function)
12203         {
12204           current_file = 1;
12205           current_line = 1;
12206
12207           /* Emit debug info for the address of the end of the function.  */
12208           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12209           if (0)
12210             {
12211               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12212                                    "DW_LNS_fixed_advance_pc");
12213               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12214             }
12215           else
12216             {
12217               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12218               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12219               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12220               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12221             }
12222
12223           /* Output the marker for the end of this sequence.  */
12224           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12225           dw2_asm_output_data_uleb128 (1, NULL);
12226           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12227         }
12228     }
12229
12230   /* Output the marker for the end of the line number info.  */
12231   ASM_OUTPUT_LABEL (asm_out_file, l2);
12232 }
12233
12234 /* Return the size of the .debug_dcall table for the compilation unit.  */
12235
12236 static unsigned long
12237 size_of_dcall_table (void)
12238 {
12239   unsigned long size;
12240   unsigned int i;
12241   dcall_entry *p;
12242   tree last_poc_decl = NULL;
12243
12244   /* Header:  version + debug info section pointer + pointer size.  */
12245   size = 2 + DWARF_OFFSET_SIZE + 1;
12246
12247   /* Each entry:  code label + DIE offset.  */
12248   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12249     {
12250       gcc_assert (p->targ_die != NULL);
12251       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12252       if (p->poc_decl != last_poc_decl)
12253         {
12254           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12255           gcc_assert (poc_die);
12256           last_poc_decl = p->poc_decl;
12257           if (poc_die)
12258             size += (DWARF_OFFSET_SIZE
12259                      + size_of_uleb128 (poc_die->die_offset));
12260         }
12261       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12262     }
12263
12264   return size;
12265 }
12266
12267 /* Output the direct call table used to disambiguate PC values when
12268    identical function have been merged.  */
12269
12270 static void
12271 output_dcall_table (void)
12272 {
12273   unsigned i;
12274   unsigned long dcall_length = size_of_dcall_table ();
12275   dcall_entry *p;
12276   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12277   tree last_poc_decl = NULL;
12278
12279   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12280     dw2_asm_output_data (4, 0xffffffff,
12281       "Initial length escape value indicating 64-bit DWARF extension");
12282   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12283                        "Length of Direct Call Table");
12284   dw2_asm_output_data (2, 4, "Version number");
12285   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12286                          debug_info_section,
12287                          "Offset of Compilation Unit Info");
12288   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12289
12290   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12291     {
12292       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12293       if (p->poc_decl != last_poc_decl)
12294         {
12295           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12296           last_poc_decl = p->poc_decl;
12297           if (poc_die)
12298             {
12299               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12300               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12301                                            "Caller DIE offset");
12302             }
12303         }
12304       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12305       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12306       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12307                                    "Callee DIE offset");
12308     }
12309 }
12310 \f
12311 /* Return the size of the .debug_vcall table for the compilation unit.  */
12312
12313 static unsigned long
12314 size_of_vcall_table (void)
12315 {
12316   unsigned long size;
12317   unsigned int i;
12318   vcall_entry *p;
12319
12320   /* Header:  version + pointer size.  */
12321   size = 2 + 1;
12322
12323   /* Each entry:  code label + vtable slot index.  */
12324   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12325     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12326
12327   return size;
12328 }
12329
12330 /* Output the virtual call table used to disambiguate PC values when
12331    identical function have been merged.  */
12332
12333 static void
12334 output_vcall_table (void)
12335 {
12336   unsigned i;
12337   unsigned long vcall_length = size_of_vcall_table ();
12338   vcall_entry *p;
12339   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12340
12341   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12342     dw2_asm_output_data (4, 0xffffffff,
12343       "Initial length escape value indicating 64-bit DWARF extension");
12344   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12345                        "Length of Virtual Call Table");
12346   dw2_asm_output_data (2, 4, "Version number");
12347   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12348
12349   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12350     {
12351       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12352       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12353       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12354     }
12355 }
12356 \f
12357 /* Given a pointer to a tree node for some base type, return a pointer to
12358    a DIE that describes the given type.
12359
12360    This routine must only be called for GCC type nodes that correspond to
12361    Dwarf base (fundamental) types.  */
12362
12363 static dw_die_ref
12364 base_type_die (tree type)
12365 {
12366   dw_die_ref base_type_result;
12367   enum dwarf_type encoding;
12368
12369   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12370     return 0;
12371
12372   /* If this is a subtype that should not be emitted as a subrange type,
12373      use the base type.  See subrange_type_for_debug_p.  */
12374   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12375     type = TREE_TYPE (type);
12376
12377   switch (TREE_CODE (type))
12378     {
12379     case INTEGER_TYPE:
12380       if ((dwarf_version >= 4 || !dwarf_strict)
12381           && TYPE_NAME (type)
12382           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12383           && DECL_IS_BUILTIN (TYPE_NAME (type))
12384           && DECL_NAME (TYPE_NAME (type)))
12385         {
12386           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12387           if (strcmp (name, "char16_t") == 0
12388               || strcmp (name, "char32_t") == 0)
12389             {
12390               encoding = DW_ATE_UTF;
12391               break;
12392             }
12393         }
12394       if (TYPE_STRING_FLAG (type))
12395         {
12396           if (TYPE_UNSIGNED (type))
12397             encoding = DW_ATE_unsigned_char;
12398           else
12399             encoding = DW_ATE_signed_char;
12400         }
12401       else if (TYPE_UNSIGNED (type))
12402         encoding = DW_ATE_unsigned;
12403       else
12404         encoding = DW_ATE_signed;
12405       break;
12406
12407     case REAL_TYPE:
12408       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12409         {
12410           if (dwarf_version >= 3 || !dwarf_strict)
12411             encoding = DW_ATE_decimal_float;
12412           else
12413             encoding = DW_ATE_lo_user;
12414         }
12415       else
12416         encoding = DW_ATE_float;
12417       break;
12418
12419     case FIXED_POINT_TYPE:
12420       if (!(dwarf_version >= 3 || !dwarf_strict))
12421         encoding = DW_ATE_lo_user;
12422       else if (TYPE_UNSIGNED (type))
12423         encoding = DW_ATE_unsigned_fixed;
12424       else
12425         encoding = DW_ATE_signed_fixed;
12426       break;
12427
12428       /* Dwarf2 doesn't know anything about complex ints, so use
12429          a user defined type for it.  */
12430     case COMPLEX_TYPE:
12431       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12432         encoding = DW_ATE_complex_float;
12433       else
12434         encoding = DW_ATE_lo_user;
12435       break;
12436
12437     case BOOLEAN_TYPE:
12438       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12439       encoding = DW_ATE_boolean;
12440       break;
12441
12442     default:
12443       /* No other TREE_CODEs are Dwarf fundamental types.  */
12444       gcc_unreachable ();
12445     }
12446
12447   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12448
12449   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12450                    int_size_in_bytes (type));
12451   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12452
12453   return base_type_result;
12454 }
12455
12456 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12457    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12458
12459 static inline int
12460 is_base_type (tree type)
12461 {
12462   switch (TREE_CODE (type))
12463     {
12464     case ERROR_MARK:
12465     case VOID_TYPE:
12466     case INTEGER_TYPE:
12467     case REAL_TYPE:
12468     case FIXED_POINT_TYPE:
12469     case COMPLEX_TYPE:
12470     case BOOLEAN_TYPE:
12471       return 1;
12472
12473     case ARRAY_TYPE:
12474     case RECORD_TYPE:
12475     case UNION_TYPE:
12476     case QUAL_UNION_TYPE:
12477     case ENUMERAL_TYPE:
12478     case FUNCTION_TYPE:
12479     case METHOD_TYPE:
12480     case POINTER_TYPE:
12481     case REFERENCE_TYPE:
12482     case OFFSET_TYPE:
12483     case LANG_TYPE:
12484     case VECTOR_TYPE:
12485       return 0;
12486
12487     default:
12488       gcc_unreachable ();
12489     }
12490
12491   return 0;
12492 }
12493
12494 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12495    node, return the size in bits for the type if it is a constant, or else
12496    return the alignment for the type if the type's size is not constant, or
12497    else return BITS_PER_WORD if the type actually turns out to be an
12498    ERROR_MARK node.  */
12499
12500 static inline unsigned HOST_WIDE_INT
12501 simple_type_size_in_bits (const_tree type)
12502 {
12503   if (TREE_CODE (type) == ERROR_MARK)
12504     return BITS_PER_WORD;
12505   else if (TYPE_SIZE (type) == NULL_TREE)
12506     return 0;
12507   else if (host_integerp (TYPE_SIZE (type), 1))
12508     return tree_low_cst (TYPE_SIZE (type), 1);
12509   else
12510     return TYPE_ALIGN (type);
12511 }
12512
12513 /* Similarly, but return a double_int instead of UHWI.  */
12514
12515 static inline double_int
12516 double_int_type_size_in_bits (const_tree type)
12517 {
12518   if (TREE_CODE (type) == ERROR_MARK)
12519     return uhwi_to_double_int (BITS_PER_WORD);
12520   else if (TYPE_SIZE (type) == NULL_TREE)
12521     return double_int_zero;
12522   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12523     return tree_to_double_int (TYPE_SIZE (type));
12524   else
12525     return uhwi_to_double_int (TYPE_ALIGN (type));
12526 }
12527
12528 /*  Given a pointer to a tree node for a subrange type, return a pointer
12529     to a DIE that describes the given type.  */
12530
12531 static dw_die_ref
12532 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12533 {
12534   dw_die_ref subrange_die;
12535   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12536
12537   if (context_die == NULL)
12538     context_die = comp_unit_die;
12539
12540   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12541
12542   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12543     {
12544       /* The size of the subrange type and its base type do not match,
12545          so we need to generate a size attribute for the subrange type.  */
12546       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12547     }
12548
12549   if (low)
12550     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12551   if (high)
12552     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12553
12554   return subrange_die;
12555 }
12556
12557 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12558    entry that chains various modifiers in front of the given type.  */
12559
12560 static dw_die_ref
12561 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12562                    dw_die_ref context_die)
12563 {
12564   enum tree_code code = TREE_CODE (type);
12565   dw_die_ref mod_type_die;
12566   dw_die_ref sub_die = NULL;
12567   tree item_type = NULL;
12568   tree qualified_type;
12569   tree name, low, high;
12570
12571   if (code == ERROR_MARK)
12572     return NULL;
12573
12574   /* See if we already have the appropriately qualified variant of
12575      this type.  */
12576   qualified_type
12577     = get_qualified_type (type,
12578                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12579                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12580
12581   if (qualified_type == sizetype
12582       && TYPE_NAME (qualified_type)
12583       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12584     {
12585 #ifdef ENABLE_CHECKING
12586       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12587                   == INTEGER_TYPE
12588                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12589                      == TYPE_PRECISION (qualified_type)
12590                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12591                      == TYPE_UNSIGNED (qualified_type));
12592 #endif
12593       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12594     }
12595
12596   /* If we do, then we can just use its DIE, if it exists.  */
12597   if (qualified_type)
12598     {
12599       mod_type_die = lookup_type_die (qualified_type);
12600       if (mod_type_die)
12601         return mod_type_die;
12602     }
12603
12604   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12605
12606   /* Handle C typedef types.  */
12607   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12608       && !DECL_ARTIFICIAL (name))
12609     {
12610       tree dtype = TREE_TYPE (name);
12611
12612       if (qualified_type == dtype)
12613         {
12614           /* For a named type, use the typedef.  */
12615           gen_type_die (qualified_type, context_die);
12616           return lookup_type_die (qualified_type);
12617         }
12618       else if (is_const_type < TYPE_READONLY (dtype)
12619                || is_volatile_type < TYPE_VOLATILE (dtype)
12620                || (is_const_type <= TYPE_READONLY (dtype)
12621                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12622                    && DECL_ORIGINAL_TYPE (name) != type))
12623         /* cv-unqualified version of named type.  Just use the unnamed
12624            type to which it refers.  */
12625         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12626                                   is_const_type, is_volatile_type,
12627                                   context_die);
12628       /* Else cv-qualified version of named type; fall through.  */
12629     }
12630
12631   if (is_const_type)
12632     {
12633       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12634       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12635     }
12636   else if (is_volatile_type)
12637     {
12638       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12639       sub_die = modified_type_die (type, 0, 0, context_die);
12640     }
12641   else if (code == POINTER_TYPE)
12642     {
12643       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12644       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12645                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12646       item_type = TREE_TYPE (type);
12647       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12648         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12649                          TYPE_ADDR_SPACE (item_type));
12650     }
12651   else if (code == REFERENCE_TYPE)
12652     {
12653       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12654         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12655                                 type);
12656       else
12657         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12658       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12659                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12660       item_type = TREE_TYPE (type);
12661       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12662         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12663                          TYPE_ADDR_SPACE (item_type));
12664     }
12665   else if (code == INTEGER_TYPE
12666            && TREE_TYPE (type) != NULL_TREE
12667            && subrange_type_for_debug_p (type, &low, &high))
12668     {
12669       mod_type_die = subrange_type_die (type, low, high, context_die);
12670       item_type = TREE_TYPE (type);
12671     }
12672   else if (is_base_type (type))
12673     mod_type_die = base_type_die (type);
12674   else
12675     {
12676       gen_type_die (type, context_die);
12677
12678       /* We have to get the type_main_variant here (and pass that to the
12679          `lookup_type_die' routine) because the ..._TYPE node we have
12680          might simply be a *copy* of some original type node (where the
12681          copy was created to help us keep track of typedef names) and
12682          that copy might have a different TYPE_UID from the original
12683          ..._TYPE node.  */
12684       if (TREE_CODE (type) != VECTOR_TYPE)
12685         return lookup_type_die (type_main_variant (type));
12686       else
12687         /* Vectors have the debugging information in the type,
12688            not the main variant.  */
12689         return lookup_type_die (type);
12690     }
12691
12692   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12693      don't output a DW_TAG_typedef, since there isn't one in the
12694      user's program; just attach a DW_AT_name to the type.
12695      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12696      if the base type already has the same name.  */
12697   if (name
12698       && ((TREE_CODE (name) != TYPE_DECL
12699            && (qualified_type == TYPE_MAIN_VARIANT (type)
12700                || (!is_const_type && !is_volatile_type)))
12701           || (TREE_CODE (name) == TYPE_DECL
12702               && TREE_TYPE (name) == qualified_type
12703               && DECL_NAME (name))))
12704     {
12705       if (TREE_CODE (name) == TYPE_DECL)
12706         /* Could just call add_name_and_src_coords_attributes here,
12707            but since this is a builtin type it doesn't have any
12708            useful source coordinates anyway.  */
12709         name = DECL_NAME (name);
12710       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12711     }
12712   /* This probably indicates a bug.  */
12713   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12714     add_name_attribute (mod_type_die, "__unknown__");
12715
12716   if (qualified_type)
12717     equate_type_number_to_die (qualified_type, mod_type_die);
12718
12719   if (item_type)
12720     /* We must do this after the equate_type_number_to_die call, in case
12721        this is a recursive type.  This ensures that the modified_type_die
12722        recursion will terminate even if the type is recursive.  Recursive
12723        types are possible in Ada.  */
12724     sub_die = modified_type_die (item_type,
12725                                  TYPE_READONLY (item_type),
12726                                  TYPE_VOLATILE (item_type),
12727                                  context_die);
12728
12729   if (sub_die != NULL)
12730     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12731
12732   return mod_type_die;
12733 }
12734
12735 /* Generate DIEs for the generic parameters of T.
12736    T must be either a generic type or a generic function.
12737    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12738
12739 static void
12740 gen_generic_params_dies (tree t)
12741 {
12742   tree parms, args;
12743   int parms_num, i;
12744   dw_die_ref die = NULL;
12745
12746   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12747     return;
12748
12749   if (TYPE_P (t))
12750     die = lookup_type_die (t);
12751   else if (DECL_P (t))
12752     die = lookup_decl_die (t);
12753
12754   gcc_assert (die);
12755
12756   parms = lang_hooks.get_innermost_generic_parms (t);
12757   if (!parms)
12758     /* T has no generic parameter. It means T is neither a generic type
12759        or function. End of story.  */
12760     return;
12761
12762   parms_num = TREE_VEC_LENGTH (parms);
12763   args = lang_hooks.get_innermost_generic_args (t);
12764   for (i = 0; i < parms_num; i++)
12765     {
12766       tree parm, arg, arg_pack_elems;
12767
12768       parm = TREE_VEC_ELT (parms, i);
12769       arg = TREE_VEC_ELT (args, i);
12770       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12771       gcc_assert (parm && TREE_VALUE (parm) && arg);
12772
12773       if (parm && TREE_VALUE (parm) && arg)
12774         {
12775           /* If PARM represents a template parameter pack,
12776              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12777              by DW_TAG_template_*_parameter DIEs for the argument
12778              pack elements of ARG. Note that ARG would then be
12779              an argument pack.  */
12780           if (arg_pack_elems)
12781             template_parameter_pack_die (TREE_VALUE (parm),
12782                                          arg_pack_elems,
12783                                          die);
12784           else
12785             generic_parameter_die (TREE_VALUE (parm), arg,
12786                                    true /* Emit DW_AT_name */, die);
12787         }
12788     }
12789 }
12790
12791 /* Create and return a DIE for PARM which should be
12792    the representation of a generic type parameter.
12793    For instance, in the C++ front end, PARM would be a template parameter.
12794    ARG is the argument to PARM.
12795    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12796    name of the PARM.
12797    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12798    as a child node.  */
12799
12800 static dw_die_ref
12801 generic_parameter_die (tree parm, tree arg,
12802                        bool emit_name_p,
12803                        dw_die_ref parent_die)
12804 {
12805   dw_die_ref tmpl_die = NULL;
12806   const char *name = NULL;
12807
12808   if (!parm || !DECL_NAME (parm) || !arg)
12809     return NULL;
12810
12811   /* We support non-type generic parameters and arguments,
12812      type generic parameters and arguments, as well as
12813      generic generic parameters (a.k.a. template template parameters in C++)
12814      and arguments.  */
12815   if (TREE_CODE (parm) == PARM_DECL)
12816     /* PARM is a nontype generic parameter  */
12817     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12818   else if (TREE_CODE (parm) == TYPE_DECL)
12819     /* PARM is a type generic parameter.  */
12820     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12821   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12822     /* PARM is a generic generic parameter.
12823        Its DIE is a GNU extension. It shall have a
12824        DW_AT_name attribute to represent the name of the template template
12825        parameter, and a DW_AT_GNU_template_name attribute to represent the
12826        name of the template template argument.  */
12827     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12828                         parent_die, parm);
12829   else
12830     gcc_unreachable ();
12831
12832   if (tmpl_die)
12833     {
12834       tree tmpl_type;
12835
12836       /* If PARM is a generic parameter pack, it means we are
12837          emitting debug info for a template argument pack element.
12838          In other terms, ARG is a template argument pack element.
12839          In that case, we don't emit any DW_AT_name attribute for
12840          the die.  */
12841       if (emit_name_p)
12842         {
12843           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12844           gcc_assert (name);
12845           add_AT_string (tmpl_die, DW_AT_name, name);
12846         }
12847
12848       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12849         {
12850           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12851              TMPL_DIE should have a child DW_AT_type attribute that is set
12852              to the type of the argument to PARM, which is ARG.
12853              If PARM is a type generic parameter, TMPL_DIE should have a
12854              child DW_AT_type that is set to ARG.  */
12855           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12856           add_type_attribute (tmpl_die, tmpl_type, 0,
12857                               TREE_THIS_VOLATILE (tmpl_type),
12858                               parent_die);
12859         }
12860       else
12861         {
12862           /* So TMPL_DIE is a DIE representing a
12863              a generic generic template parameter, a.k.a template template
12864              parameter in C++ and arg is a template.  */
12865
12866           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12867              to the name of the argument.  */
12868           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12869           if (name)
12870             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12871         }
12872
12873       if (TREE_CODE (parm) == PARM_DECL)
12874         /* So PARM is a non-type generic parameter.
12875            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12876            attribute of TMPL_DIE which value represents the value
12877            of ARG.
12878            We must be careful here:
12879            The value of ARG might reference some function decls.
12880            We might currently be emitting debug info for a generic
12881            type and types are emitted before function decls, we don't
12882            know if the function decls referenced by ARG will actually be
12883            emitted after cgraph computations.
12884            So must defer the generation of the DW_AT_const_value to
12885            after cgraph is ready.  */
12886         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12887     }
12888
12889   return tmpl_die;
12890 }
12891
12892 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12893    PARM_PACK must be a template parameter pack. The returned DIE
12894    will be child DIE of PARENT_DIE.  */
12895
12896 static dw_die_ref
12897 template_parameter_pack_die (tree parm_pack,
12898                              tree parm_pack_args,
12899                              dw_die_ref parent_die)
12900 {
12901   dw_die_ref die;
12902   int j;
12903
12904   gcc_assert (parent_die && parm_pack);
12905
12906   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12907   add_name_and_src_coords_attributes (die, parm_pack);
12908   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12909     generic_parameter_die (parm_pack,
12910                            TREE_VEC_ELT (parm_pack_args, j),
12911                            false /* Don't emit DW_AT_name */,
12912                            die);
12913   return die;
12914 }
12915
12916 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12917    an enumerated type.  */
12918
12919 static inline int
12920 type_is_enum (const_tree type)
12921 {
12922   return TREE_CODE (type) == ENUMERAL_TYPE;
12923 }
12924
12925 /* Return the DBX register number described by a given RTL node.  */
12926
12927 static unsigned int
12928 dbx_reg_number (const_rtx rtl)
12929 {
12930   unsigned regno = REGNO (rtl);
12931
12932   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12933
12934 #ifdef LEAF_REG_REMAP
12935   if (current_function_uses_only_leaf_regs)
12936     {
12937       int leaf_reg = LEAF_REG_REMAP (regno);
12938       if (leaf_reg != -1)
12939         regno = (unsigned) leaf_reg;
12940     }
12941 #endif
12942
12943   return DBX_REGISTER_NUMBER (regno);
12944 }
12945
12946 /* Optionally add a DW_OP_piece term to a location description expression.
12947    DW_OP_piece is only added if the location description expression already
12948    doesn't end with DW_OP_piece.  */
12949
12950 static void
12951 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12952 {
12953   dw_loc_descr_ref loc;
12954
12955   if (*list_head != NULL)
12956     {
12957       /* Find the end of the chain.  */
12958       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12959         ;
12960
12961       if (loc->dw_loc_opc != DW_OP_piece)
12962         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12963     }
12964 }
12965
12966 /* Return a location descriptor that designates a machine register or
12967    zero if there is none.  */
12968
12969 static dw_loc_descr_ref
12970 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12971 {
12972   rtx regs;
12973
12974   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12975     return 0;
12976
12977   regs = targetm.dwarf_register_span (rtl);
12978
12979   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
12980     return multiple_reg_loc_descriptor (rtl, regs, initialized);
12981   else
12982     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
12983 }
12984
12985 /* Return a location descriptor that designates a machine register for
12986    a given hard register number.  */
12987
12988 static dw_loc_descr_ref
12989 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12990 {
12991   dw_loc_descr_ref reg_loc_descr;
12992
12993   if (regno <= 31)
12994     reg_loc_descr
12995       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12996   else
12997     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12998
12999   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13000     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13001
13002   return reg_loc_descr;
13003 }
13004
13005 /* Given an RTL of a register, return a location descriptor that
13006    designates a value that spans more than one register.  */
13007
13008 static dw_loc_descr_ref
13009 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13010                              enum var_init_status initialized)
13011 {
13012   int nregs, size, i;
13013   unsigned reg;
13014   dw_loc_descr_ref loc_result = NULL;
13015
13016   reg = REGNO (rtl);
13017 #ifdef LEAF_REG_REMAP
13018   if (current_function_uses_only_leaf_regs)
13019     {
13020       int leaf_reg = LEAF_REG_REMAP (reg);
13021       if (leaf_reg != -1)
13022         reg = (unsigned) leaf_reg;
13023     }
13024 #endif
13025   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13026   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13027
13028   /* Simple, contiguous registers.  */
13029   if (regs == NULL_RTX)
13030     {
13031       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13032
13033       loc_result = NULL;
13034       while (nregs--)
13035         {
13036           dw_loc_descr_ref t;
13037
13038           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13039                                       VAR_INIT_STATUS_INITIALIZED);
13040           add_loc_descr (&loc_result, t);
13041           add_loc_descr_op_piece (&loc_result, size);
13042           ++reg;
13043         }
13044       return loc_result;
13045     }
13046
13047   /* Now onto stupid register sets in non contiguous locations.  */
13048
13049   gcc_assert (GET_CODE (regs) == PARALLEL);
13050
13051   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13052   loc_result = NULL;
13053
13054   for (i = 0; i < XVECLEN (regs, 0); ++i)
13055     {
13056       dw_loc_descr_ref t;
13057
13058       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13059                                   VAR_INIT_STATUS_INITIALIZED);
13060       add_loc_descr (&loc_result, t);
13061       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13062       add_loc_descr_op_piece (&loc_result, size);
13063     }
13064
13065   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13066     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13067   return loc_result;
13068 }
13069
13070 #endif /* DWARF2_DEBUGGING_INFO */
13071
13072 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
13073
13074 /* Return a location descriptor that designates a constant.  */
13075
13076 static dw_loc_descr_ref
13077 int_loc_descriptor (HOST_WIDE_INT i)
13078 {
13079   enum dwarf_location_atom op;
13080
13081   /* Pick the smallest representation of a constant, rather than just
13082      defaulting to the LEB encoding.  */
13083   if (i >= 0)
13084     {
13085       if (i <= 31)
13086         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13087       else if (i <= 0xff)
13088         op = DW_OP_const1u;
13089       else if (i <= 0xffff)
13090         op = DW_OP_const2u;
13091       else if (HOST_BITS_PER_WIDE_INT == 32
13092                || i <= 0xffffffff)
13093         op = DW_OP_const4u;
13094       else
13095         op = DW_OP_constu;
13096     }
13097   else
13098     {
13099       if (i >= -0x80)
13100         op = DW_OP_const1s;
13101       else if (i >= -0x8000)
13102         op = DW_OP_const2s;
13103       else if (HOST_BITS_PER_WIDE_INT == 32
13104                || i >= -0x80000000)
13105         op = DW_OP_const4s;
13106       else
13107         op = DW_OP_consts;
13108     }
13109
13110   return new_loc_descr (op, i, 0);
13111 }
13112 #endif
13113
13114 #ifdef DWARF2_DEBUGGING_INFO
13115 /* Return loc description representing "address" of integer value.
13116    This can appear only as toplevel expression.  */
13117
13118 static dw_loc_descr_ref
13119 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13120 {
13121   int litsize;
13122   dw_loc_descr_ref loc_result = NULL;
13123
13124   if (!(dwarf_version >= 4 || !dwarf_strict))
13125     return NULL;
13126
13127   if (i >= 0)
13128     {
13129       if (i <= 31)
13130         litsize = 1;
13131       else if (i <= 0xff)
13132         litsize = 2;
13133       else if (i <= 0xffff)
13134         litsize = 3;
13135       else if (HOST_BITS_PER_WIDE_INT == 32
13136                || i <= 0xffffffff)
13137         litsize = 5;
13138       else
13139         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13140     }
13141   else
13142     {
13143       if (i >= -0x80)
13144         litsize = 2;
13145       else if (i >= -0x8000)
13146         litsize = 3;
13147       else if (HOST_BITS_PER_WIDE_INT == 32
13148                || i >= -0x80000000)
13149         litsize = 5;
13150       else
13151         litsize = 1 + size_of_sleb128 (i);
13152     }
13153   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13154      is more compact.  For DW_OP_stack_value we need:
13155      litsize + 1 (DW_OP_stack_value)
13156      and for DW_OP_implicit_value:
13157      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13158   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13159     {
13160       loc_result = int_loc_descriptor (i);
13161       add_loc_descr (&loc_result,
13162                      new_loc_descr (DW_OP_stack_value, 0, 0));
13163       return loc_result;
13164     }
13165
13166   loc_result = new_loc_descr (DW_OP_implicit_value,
13167                               size, 0);
13168   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13169   loc_result->dw_loc_oprnd2.v.val_int = i;
13170   return loc_result;
13171 }
13172
13173 /* Return a location descriptor that designates a base+offset location.  */
13174
13175 static dw_loc_descr_ref
13176 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13177                  enum var_init_status initialized)
13178 {
13179   unsigned int regno;
13180   dw_loc_descr_ref result;
13181   dw_fde_ref fde = current_fde ();
13182
13183   /* We only use "frame base" when we're sure we're talking about the
13184      post-prologue local stack frame.  We do this by *not* running
13185      register elimination until this point, and recognizing the special
13186      argument pointer and soft frame pointer rtx's.  */
13187   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13188     {
13189       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13190
13191       if (elim != reg)
13192         {
13193           if (GET_CODE (elim) == PLUS)
13194             {
13195               offset += INTVAL (XEXP (elim, 1));
13196               elim = XEXP (elim, 0);
13197             }
13198           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13199                        && (elim == hard_frame_pointer_rtx
13200                            || elim == stack_pointer_rtx))
13201                       || elim == (frame_pointer_needed
13202                                   ? hard_frame_pointer_rtx
13203                                   : stack_pointer_rtx));
13204
13205           /* If drap register is used to align stack, use frame
13206              pointer + offset to access stack variables.  If stack
13207              is aligned without drap, use stack pointer + offset to
13208              access stack variables.  */
13209           if (crtl->stack_realign_tried
13210               && reg == frame_pointer_rtx)
13211             {
13212               int base_reg
13213                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13214                                       ? HARD_FRAME_POINTER_REGNUM
13215                                       : STACK_POINTER_REGNUM);
13216               return new_reg_loc_descr (base_reg, offset);
13217             }
13218
13219           offset += frame_pointer_fb_offset;
13220           return new_loc_descr (DW_OP_fbreg, offset, 0);
13221         }
13222     }
13223   else if (!optimize
13224            && fde
13225            && (fde->drap_reg == REGNO (reg)
13226                || fde->vdrap_reg == REGNO (reg)))
13227     {
13228       /* Use cfa+offset to represent the location of arguments passed
13229          on the stack when drap is used to align stack.
13230          Only do this when not optimizing, for optimized code var-tracking
13231          is supposed to track where the arguments live and the register
13232          used as vdrap or drap in some spot might be used for something
13233          else in other part of the routine.  */
13234       return new_loc_descr (DW_OP_fbreg, offset, 0);
13235     }
13236
13237   regno = dbx_reg_number (reg);
13238   if (regno <= 31)
13239     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13240                             offset, 0);
13241   else
13242     result = new_loc_descr (DW_OP_bregx, regno, offset);
13243
13244   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13245     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13246
13247   return result;
13248 }
13249
13250 /* Return true if this RTL expression describes a base+offset calculation.  */
13251
13252 static inline int
13253 is_based_loc (const_rtx rtl)
13254 {
13255   return (GET_CODE (rtl) == PLUS
13256           && ((REG_P (XEXP (rtl, 0))
13257                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13258                && CONST_INT_P (XEXP (rtl, 1)))));
13259 }
13260
13261 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13262    failed.  */
13263
13264 static dw_loc_descr_ref
13265 tls_mem_loc_descriptor (rtx mem)
13266 {
13267   tree base;
13268   dw_loc_descr_ref loc_result;
13269
13270   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13271     return NULL;
13272
13273   base = get_base_address (MEM_EXPR (mem));
13274   if (base == NULL
13275       || TREE_CODE (base) != VAR_DECL
13276       || !DECL_THREAD_LOCAL_P (base))
13277     return NULL;
13278
13279   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13280   if (loc_result == NULL)
13281     return NULL;
13282
13283   if (INTVAL (MEM_OFFSET (mem)))
13284     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13285
13286   return loc_result;
13287 }
13288
13289 /* Output debug info about reason why we failed to expand expression as dwarf
13290    expression.  */
13291
13292 static void
13293 expansion_failed (tree expr, rtx rtl, char const *reason)
13294 {
13295   if (dump_file && (dump_flags & TDF_DETAILS))
13296     {
13297       fprintf (dump_file, "Failed to expand as dwarf: ");
13298       if (expr)
13299         print_generic_expr (dump_file, expr, dump_flags);
13300       if (rtl)
13301         {
13302           fprintf (dump_file, "\n");
13303           print_rtl (dump_file, rtl);
13304         }
13305       fprintf (dump_file, "\nReason: %s\n", reason);
13306     }
13307 }
13308
13309 /* Helper function for const_ok_for_output, called either directly
13310    or via for_each_rtx.  */
13311
13312 static int
13313 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13314 {
13315   rtx rtl = *rtlp;
13316
13317   if (GET_CODE (rtl) == UNSPEC)
13318     {
13319       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13320          we can't express it in the debug info.  */
13321 #ifdef ENABLE_CHECKING
13322       inform (current_function_decl
13323               ? DECL_SOURCE_LOCATION (current_function_decl)
13324               : UNKNOWN_LOCATION,
13325               "non-delegitimized UNSPEC %d found in variable location",
13326               XINT (rtl, 1));
13327 #endif
13328       expansion_failed (NULL_TREE, rtl,
13329                         "UNSPEC hasn't been delegitimized.\n");
13330       return 1;
13331     }
13332
13333   if (GET_CODE (rtl) != SYMBOL_REF)
13334     return 0;
13335
13336   if (CONSTANT_POOL_ADDRESS_P (rtl))
13337     {
13338       bool marked;
13339       get_pool_constant_mark (rtl, &marked);
13340       /* If all references to this pool constant were optimized away,
13341          it was not output and thus we can't represent it.  */
13342       if (!marked)
13343         {
13344           expansion_failed (NULL_TREE, rtl,
13345                             "Constant was removed from constant pool.\n");
13346           return 1;
13347         }
13348     }
13349
13350   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13351     return 1;
13352
13353   /* Avoid references to external symbols in debug info, on several targets
13354      the linker might even refuse to link when linking a shared library,
13355      and in many other cases the relocations for .debug_info/.debug_loc are
13356      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13357      to be defined within the same shared library or executable are fine.  */
13358   if (SYMBOL_REF_EXTERNAL_P (rtl))
13359     {
13360       tree decl = SYMBOL_REF_DECL (rtl);
13361
13362       if (decl == NULL || !targetm.binds_local_p (decl))
13363         {
13364           expansion_failed (NULL_TREE, rtl,
13365                             "Symbol not defined in current TU.\n");
13366           return 1;
13367         }
13368     }
13369
13370   return 0;
13371 }
13372
13373 /* Return true if constant RTL can be emitted in DW_OP_addr or
13374    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13375    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13376
13377 static bool
13378 const_ok_for_output (rtx rtl)
13379 {
13380   if (GET_CODE (rtl) == SYMBOL_REF)
13381     return const_ok_for_output_1 (&rtl, NULL) == 0;
13382
13383   if (GET_CODE (rtl) == CONST)
13384     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13385
13386   return true;
13387 }
13388
13389 /* The following routine converts the RTL for a variable or parameter
13390    (resident in memory) into an equivalent Dwarf representation of a
13391    mechanism for getting the address of that same variable onto the top of a
13392    hypothetical "address evaluation" stack.
13393
13394    When creating memory location descriptors, we are effectively transforming
13395    the RTL for a memory-resident object into its Dwarf postfix expression
13396    equivalent.  This routine recursively descends an RTL tree, turning
13397    it into Dwarf postfix code as it goes.
13398
13399    MODE is the mode of the memory reference, needed to handle some
13400    autoincrement addressing modes.
13401
13402    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13403    location list for RTL.
13404
13405    Return 0 if we can't represent the location.  */
13406
13407 static dw_loc_descr_ref
13408 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13409                     enum var_init_status initialized)
13410 {
13411   dw_loc_descr_ref mem_loc_result = NULL;
13412   enum dwarf_location_atom op;
13413   dw_loc_descr_ref op0, op1;
13414
13415   /* Note that for a dynamically sized array, the location we will generate a
13416      description of here will be the lowest numbered location which is
13417      actually within the array.  That's *not* necessarily the same as the
13418      zeroth element of the array.  */
13419
13420   rtl = targetm.delegitimize_address (rtl);
13421
13422   switch (GET_CODE (rtl))
13423     {
13424     case POST_INC:
13425     case POST_DEC:
13426     case POST_MODIFY:
13427       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13428
13429     case SUBREG:
13430       /* The case of a subreg may arise when we have a local (register)
13431          variable or a formal (register) parameter which doesn't quite fill
13432          up an entire register.  For now, just assume that it is
13433          legitimate to make the Dwarf info refer to the whole register which
13434          contains the given subreg.  */
13435       if (!subreg_lowpart_p (rtl))
13436         break;
13437       rtl = SUBREG_REG (rtl);
13438       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13439         break;
13440       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13441         break;
13442       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13443       break;
13444
13445     case REG:
13446       /* Whenever a register number forms a part of the description of the
13447          method for calculating the (dynamic) address of a memory resident
13448          object, DWARF rules require the register number be referred to as
13449          a "base register".  This distinction is not based in any way upon
13450          what category of register the hardware believes the given register
13451          belongs to.  This is strictly DWARF terminology we're dealing with
13452          here. Note that in cases where the location of a memory-resident
13453          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13454          OP_CONST (0)) the actual DWARF location descriptor that we generate
13455          may just be OP_BASEREG (basereg).  This may look deceptively like
13456          the object in question was allocated to a register (rather than in
13457          memory) so DWARF consumers need to be aware of the subtle
13458          distinction between OP_REG and OP_BASEREG.  */
13459       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13460         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13461       else if (stack_realign_drap
13462                && crtl->drap_reg
13463                && crtl->args.internal_arg_pointer == rtl
13464                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13465         {
13466           /* If RTL is internal_arg_pointer, which has been optimized
13467              out, use DRAP instead.  */
13468           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13469                                             VAR_INIT_STATUS_INITIALIZED);
13470         }
13471       break;
13472
13473     case SIGN_EXTEND:
13474     case ZERO_EXTEND:
13475       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13476                                 VAR_INIT_STATUS_INITIALIZED);
13477       if (op0 == 0)
13478         break;
13479       else
13480         {
13481           int shift = DWARF2_ADDR_SIZE
13482                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13483           shift *= BITS_PER_UNIT;
13484           if (GET_CODE (rtl) == SIGN_EXTEND)
13485             op = DW_OP_shra;
13486           else
13487             op = DW_OP_shr;
13488           mem_loc_result = op0;
13489           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13490           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13491           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13492           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13493         }
13494       break;
13495
13496     case MEM:
13497       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13498                                            VAR_INIT_STATUS_INITIALIZED);
13499       if (mem_loc_result == NULL)
13500         mem_loc_result = tls_mem_loc_descriptor (rtl);
13501       if (mem_loc_result != 0)
13502         {
13503           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13504             {
13505               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13506               return 0;
13507             }
13508           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13509             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13510           else
13511             add_loc_descr (&mem_loc_result,
13512                            new_loc_descr (DW_OP_deref_size,
13513                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13514         }
13515       else
13516         {
13517           rtx new_rtl = avoid_constant_pool_reference (rtl);
13518           if (new_rtl != rtl)
13519             return mem_loc_descriptor (new_rtl, mode, initialized);
13520         }
13521       break;
13522
13523     case LO_SUM:
13524          rtl = XEXP (rtl, 1);
13525
13526       /* ... fall through ...  */
13527
13528     case LABEL_REF:
13529       /* Some ports can transform a symbol ref into a label ref, because
13530          the symbol ref is too far away and has to be dumped into a constant
13531          pool.  */
13532     case CONST:
13533     case SYMBOL_REF:
13534       if (GET_CODE (rtl) == SYMBOL_REF
13535           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13536         {
13537           dw_loc_descr_ref temp;
13538
13539           /* If this is not defined, we have no way to emit the data.  */
13540           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13541             break;
13542
13543           temp = new_loc_descr (DW_OP_addr, 0, 0);
13544           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13545           temp->dw_loc_oprnd1.v.val_addr = rtl;
13546           temp->dtprel = true;
13547
13548           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13549           add_loc_descr (&mem_loc_result, temp);
13550
13551           break;
13552         }
13553
13554       if (!const_ok_for_output (rtl))
13555         break;
13556
13557     symref:
13558       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13559       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13560       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13561       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13562       break;
13563
13564     case CONCAT:
13565     case CONCATN:
13566     case VAR_LOCATION:
13567       expansion_failed (NULL_TREE, rtl,
13568                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13569       return 0;
13570
13571     case PRE_MODIFY:
13572       /* Extract the PLUS expression nested inside and fall into
13573          PLUS code below.  */
13574       rtl = XEXP (rtl, 1);
13575       goto plus;
13576
13577     case PRE_INC:
13578     case PRE_DEC:
13579       /* Turn these into a PLUS expression and fall into the PLUS code
13580          below.  */
13581       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13582                           GEN_INT (GET_CODE (rtl) == PRE_INC
13583                                    ? GET_MODE_UNIT_SIZE (mode)
13584                                    : -GET_MODE_UNIT_SIZE (mode)));
13585
13586       /* ... fall through ...  */
13587
13588     case PLUS:
13589     plus:
13590       if (is_based_loc (rtl))
13591         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13592                                           INTVAL (XEXP (rtl, 1)),
13593                                           VAR_INIT_STATUS_INITIALIZED);
13594       else
13595         {
13596           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13597                                                VAR_INIT_STATUS_INITIALIZED);
13598           if (mem_loc_result == 0)
13599             break;
13600
13601           if (CONST_INT_P (XEXP (rtl, 1)))
13602             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13603           else
13604             {
13605               dw_loc_descr_ref mem_loc_result2
13606                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13607                                       VAR_INIT_STATUS_INITIALIZED);
13608               if (mem_loc_result2 == 0)
13609                 break;
13610               add_loc_descr (&mem_loc_result, mem_loc_result2);
13611               add_loc_descr (&mem_loc_result,
13612                              new_loc_descr (DW_OP_plus, 0, 0));
13613             }
13614         }
13615       break;
13616
13617     /* If a pseudo-reg is optimized away, it is possible for it to
13618        be replaced with a MEM containing a multiply or shift.  */
13619     case MINUS:
13620       op = DW_OP_minus;
13621       goto do_binop;
13622
13623     case MULT:
13624       op = DW_OP_mul;
13625       goto do_binop;
13626
13627     case DIV:
13628       op = DW_OP_div;
13629       goto do_binop;
13630
13631     case UMOD:
13632       op = DW_OP_mod;
13633       goto do_binop;
13634
13635     case ASHIFT:
13636       op = DW_OP_shl;
13637       goto do_binop;
13638
13639     case ASHIFTRT:
13640       op = DW_OP_shra;
13641       goto do_binop;
13642
13643     case LSHIFTRT:
13644       op = DW_OP_shr;
13645       goto do_binop;
13646
13647     case AND:
13648       op = DW_OP_and;
13649       goto do_binop;
13650
13651     case IOR:
13652       op = DW_OP_or;
13653       goto do_binop;
13654
13655     case XOR:
13656       op = DW_OP_xor;
13657       goto do_binop;
13658
13659     do_binop:
13660       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13661                                 VAR_INIT_STATUS_INITIALIZED);
13662       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13663                                 VAR_INIT_STATUS_INITIALIZED);
13664
13665       if (op0 == 0 || op1 == 0)
13666         break;
13667
13668       mem_loc_result = op0;
13669       add_loc_descr (&mem_loc_result, op1);
13670       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13671       break;
13672
13673     case MOD:
13674       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13675                                 VAR_INIT_STATUS_INITIALIZED);
13676       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13677                                 VAR_INIT_STATUS_INITIALIZED);
13678
13679       if (op0 == 0 || op1 == 0)
13680         break;
13681
13682       mem_loc_result = op0;
13683       add_loc_descr (&mem_loc_result, op1);
13684       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13685       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13686       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13687       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13688       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13689       break;
13690
13691     case NOT:
13692       op = DW_OP_not;
13693       goto do_unop;
13694
13695     case ABS:
13696       op = DW_OP_abs;
13697       goto do_unop;
13698
13699     case NEG:
13700       op = DW_OP_neg;
13701       goto do_unop;
13702
13703     do_unop:
13704       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13705                                 VAR_INIT_STATUS_INITIALIZED);
13706
13707       if (op0 == 0)
13708         break;
13709
13710       mem_loc_result = op0;
13711       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13712       break;
13713
13714     case CONST_INT:
13715       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13716       break;
13717
13718     case EQ:
13719       op = DW_OP_eq;
13720       goto do_scompare;
13721
13722     case GE:
13723       op = DW_OP_ge;
13724       goto do_scompare;
13725
13726     case GT:
13727       op = DW_OP_gt;
13728       goto do_scompare;
13729
13730     case LE:
13731       op = DW_OP_le;
13732       goto do_scompare;
13733
13734     case LT:
13735       op = DW_OP_lt;
13736       goto do_scompare;
13737
13738     case NE:
13739       op = DW_OP_ne;
13740       goto do_scompare;
13741
13742     do_scompare:
13743       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13744           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13745         break;
13746       else
13747         {
13748           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13749
13750           if (op_mode == VOIDmode)
13751             op_mode = GET_MODE (XEXP (rtl, 1));
13752           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13753             break;
13754
13755           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13756                                     VAR_INIT_STATUS_INITIALIZED);
13757           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13758                                     VAR_INIT_STATUS_INITIALIZED);
13759
13760           if (op0 == 0 || op1 == 0)
13761             break;
13762
13763           if (op_mode != VOIDmode
13764               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13765             {
13766               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13767               shift *= BITS_PER_UNIT;
13768               /* For eq/ne, if the operands are known to be zero-extended,
13769                  there is no need to do the fancy shifting up.  */
13770               if (op == DW_OP_eq || op == DW_OP_ne)
13771                 {
13772                   dw_loc_descr_ref last0, last1;
13773                   for (last0 = op0;
13774                        last0->dw_loc_next != NULL;
13775                        last0 = last0->dw_loc_next)
13776                     ;
13777                   for (last1 = op1;
13778                        last1->dw_loc_next != NULL;
13779                        last1 = last1->dw_loc_next)
13780                     ;
13781                   /* deref_size zero extends, and for constants we can check
13782                      whether they are zero extended or not.  */
13783                   if (((last0->dw_loc_opc == DW_OP_deref_size
13784                         && last0->dw_loc_oprnd1.v.val_int
13785                            <= GET_MODE_SIZE (op_mode))
13786                        || (CONST_INT_P (XEXP (rtl, 0))
13787                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13788                                == (INTVAL (XEXP (rtl, 0))
13789                                    & GET_MODE_MASK (op_mode))))
13790                       && ((last1->dw_loc_opc == DW_OP_deref_size
13791                            && last1->dw_loc_oprnd1.v.val_int
13792                               <= GET_MODE_SIZE (op_mode))
13793                           || (CONST_INT_P (XEXP (rtl, 1))
13794                               && (unsigned HOST_WIDE_INT)
13795                                  INTVAL (XEXP (rtl, 1))
13796                                  == (INTVAL (XEXP (rtl, 1))
13797                                      & GET_MODE_MASK (op_mode)))))
13798                     goto do_compare;
13799                 }
13800               add_loc_descr (&op0, int_loc_descriptor (shift));
13801               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13802               if (CONST_INT_P (XEXP (rtl, 1)))
13803                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13804               else
13805                 {
13806                   add_loc_descr (&op1, int_loc_descriptor (shift));
13807                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13808                 }
13809             }
13810         }
13811
13812     do_compare:
13813       mem_loc_result = op0;
13814       add_loc_descr (&mem_loc_result, op1);
13815       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13816       if (STORE_FLAG_VALUE != 1)
13817         {
13818           add_loc_descr (&mem_loc_result,
13819                          int_loc_descriptor (STORE_FLAG_VALUE));
13820           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13821         }
13822       break;
13823
13824     case GEU:
13825       op = DW_OP_ge;
13826       goto do_ucompare;
13827
13828     case GTU:
13829       op = DW_OP_gt;
13830       goto do_ucompare;
13831
13832     case LEU:
13833       op = DW_OP_le;
13834       goto do_ucompare;
13835
13836     case LTU:
13837       op = DW_OP_lt;
13838       goto do_ucompare;
13839
13840     do_ucompare:
13841       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13842           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13843         break;
13844       else
13845         {
13846           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13847
13848           if (op_mode == VOIDmode)
13849             op_mode = GET_MODE (XEXP (rtl, 1));
13850           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13851             break;
13852
13853           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13854                                     VAR_INIT_STATUS_INITIALIZED);
13855           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13856                                     VAR_INIT_STATUS_INITIALIZED);
13857
13858           if (op0 == 0 || op1 == 0)
13859             break;
13860
13861           if (op_mode != VOIDmode
13862               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13863             {
13864               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13865               dw_loc_descr_ref last0, last1;
13866               for (last0 = op0;
13867                    last0->dw_loc_next != NULL;
13868                    last0 = last0->dw_loc_next)
13869                 ;
13870               for (last1 = op1;
13871                    last1->dw_loc_next != NULL;
13872                    last1 = last1->dw_loc_next)
13873                 ;
13874               if (CONST_INT_P (XEXP (rtl, 0)))
13875                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13876               /* deref_size zero extends, so no need to mask it again.  */
13877               else if (last0->dw_loc_opc != DW_OP_deref_size
13878                        || last0->dw_loc_oprnd1.v.val_int
13879                           > GET_MODE_SIZE (op_mode))
13880                 {
13881                   add_loc_descr (&op0, int_loc_descriptor (mask));
13882                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13883                 }
13884               if (CONST_INT_P (XEXP (rtl, 1)))
13885                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13886               /* deref_size zero extends, so no need to mask it again.  */
13887               else if (last1->dw_loc_opc != DW_OP_deref_size
13888                        || last1->dw_loc_oprnd1.v.val_int
13889                           > GET_MODE_SIZE (op_mode))
13890                 {
13891                   add_loc_descr (&op1, int_loc_descriptor (mask));
13892                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13893                 }
13894             }
13895           else
13896             {
13897               HOST_WIDE_INT bias = 1;
13898               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13899               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13900               if (CONST_INT_P (XEXP (rtl, 1)))
13901                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13902                                           + INTVAL (XEXP (rtl, 1)));
13903               else
13904                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13905                                                     bias, 0));
13906             }
13907         }
13908       goto do_compare;
13909
13910     case SMIN:
13911     case SMAX:
13912     case UMIN:
13913     case UMAX:
13914       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13915           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13916           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13917         break;
13918
13919       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13920                                 VAR_INIT_STATUS_INITIALIZED);
13921       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13922                                 VAR_INIT_STATUS_INITIALIZED);
13923
13924       if (op0 == 0 || op1 == 0)
13925         break;
13926
13927       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13928       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13929       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13930       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13931         {
13932           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13933             {
13934               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13935               add_loc_descr (&op0, int_loc_descriptor (mask));
13936               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13937               add_loc_descr (&op1, int_loc_descriptor (mask));
13938               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13939             }
13940           else
13941             {
13942               HOST_WIDE_INT bias = 1;
13943               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13944               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13945               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13946             }
13947         }
13948       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13949         {
13950           int shift = DWARF2_ADDR_SIZE
13951                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13952           shift *= BITS_PER_UNIT;
13953           add_loc_descr (&op0, int_loc_descriptor (shift));
13954           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13955           add_loc_descr (&op1, int_loc_descriptor (shift));
13956           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13957         }
13958
13959       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13960         op = DW_OP_lt;
13961       else
13962         op = DW_OP_gt;
13963       mem_loc_result = op0;
13964       add_loc_descr (&mem_loc_result, op1);
13965       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13966       {
13967         dw_loc_descr_ref bra_node, drop_node;
13968
13969         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13970         add_loc_descr (&mem_loc_result, bra_node);
13971         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13972         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13973         add_loc_descr (&mem_loc_result, drop_node);
13974         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13975         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13976       }
13977       break;
13978
13979     case ZERO_EXTRACT:
13980     case SIGN_EXTRACT:
13981       if (CONST_INT_P (XEXP (rtl, 1))
13982           && CONST_INT_P (XEXP (rtl, 2))
13983           && ((unsigned) INTVAL (XEXP (rtl, 1))
13984               + (unsigned) INTVAL (XEXP (rtl, 2))
13985               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
13986           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13987           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13988         {
13989           int shift, size;
13990           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13991                                     VAR_INIT_STATUS_INITIALIZED);
13992           if (op0 == 0)
13993             break;
13994           if (GET_CODE (rtl) == SIGN_EXTRACT)
13995             op = DW_OP_shra;
13996           else
13997             op = DW_OP_shr;
13998           mem_loc_result = op0;
13999           size = INTVAL (XEXP (rtl, 1));
14000           shift = INTVAL (XEXP (rtl, 2));
14001           if (BITS_BIG_ENDIAN)
14002             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14003                     - shift - size;
14004           if (shift + size != (int) DWARF2_ADDR_SIZE)
14005             {
14006               add_loc_descr (&mem_loc_result,
14007                              int_loc_descriptor (DWARF2_ADDR_SIZE
14008                                                  - shift - size));
14009               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14010             }
14011           if (size != (int) DWARF2_ADDR_SIZE)
14012             {
14013               add_loc_descr (&mem_loc_result,
14014                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14015               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14016             }
14017         }
14018       break;
14019
14020     case COMPARE:
14021     case IF_THEN_ELSE:
14022     case ROTATE:
14023     case ROTATERT:
14024     case TRUNCATE:
14025       /* In theory, we could implement the above.  */
14026       /* DWARF cannot represent the unsigned compare operations
14027          natively.  */
14028     case SS_MULT:
14029     case US_MULT:
14030     case SS_DIV:
14031     case US_DIV:
14032     case SS_PLUS:
14033     case US_PLUS:
14034     case SS_MINUS:
14035     case US_MINUS:
14036     case SS_NEG:
14037     case US_NEG:
14038     case SS_ABS:
14039     case SS_ASHIFT:
14040     case US_ASHIFT:
14041     case SS_TRUNCATE:
14042     case US_TRUNCATE:
14043     case UDIV:
14044     case UNORDERED:
14045     case ORDERED:
14046     case UNEQ:
14047     case UNGE:
14048     case UNGT:
14049     case UNLE:
14050     case UNLT:
14051     case LTGT:
14052     case FLOAT_EXTEND:
14053     case FLOAT_TRUNCATE:
14054     case FLOAT:
14055     case UNSIGNED_FLOAT:
14056     case FIX:
14057     case UNSIGNED_FIX:
14058     case FRACT_CONVERT:
14059     case UNSIGNED_FRACT_CONVERT:
14060     case SAT_FRACT:
14061     case UNSIGNED_SAT_FRACT:
14062     case SQRT:
14063     case BSWAP:
14064     case FFS:
14065     case CLZ:
14066     case CTZ:
14067     case POPCOUNT:
14068     case PARITY:
14069     case ASM_OPERANDS:
14070     case VEC_MERGE:
14071     case VEC_SELECT:
14072     case VEC_CONCAT:
14073     case VEC_DUPLICATE:
14074     case UNSPEC:
14075     case HIGH:
14076       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14077          can't express it in the debug info.  This can happen e.g. with some
14078          TLS UNSPECs.  */
14079       break;
14080
14081     case CONST_STRING:
14082       resolve_one_addr (&rtl, NULL);
14083       goto symref;
14084
14085     default:
14086 #ifdef ENABLE_CHECKING
14087       print_rtl (stderr, rtl);
14088       gcc_unreachable ();
14089 #else
14090       break;
14091 #endif
14092     }
14093
14094   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14095     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14096
14097   return mem_loc_result;
14098 }
14099
14100 /* Return a descriptor that describes the concatenation of two locations.
14101    This is typically a complex variable.  */
14102
14103 static dw_loc_descr_ref
14104 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14105 {
14106   dw_loc_descr_ref cc_loc_result = NULL;
14107   dw_loc_descr_ref x0_ref
14108     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14109   dw_loc_descr_ref x1_ref
14110     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14111
14112   if (x0_ref == 0 || x1_ref == 0)
14113     return 0;
14114
14115   cc_loc_result = x0_ref;
14116   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14117
14118   add_loc_descr (&cc_loc_result, x1_ref);
14119   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14120
14121   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14122     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14123
14124   return cc_loc_result;
14125 }
14126
14127 /* Return a descriptor that describes the concatenation of N
14128    locations.  */
14129
14130 static dw_loc_descr_ref
14131 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14132 {
14133   unsigned int i;
14134   dw_loc_descr_ref cc_loc_result = NULL;
14135   unsigned int n = XVECLEN (concatn, 0);
14136
14137   for (i = 0; i < n; ++i)
14138     {
14139       dw_loc_descr_ref ref;
14140       rtx x = XVECEXP (concatn, 0, i);
14141
14142       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14143       if (ref == NULL)
14144         return NULL;
14145
14146       add_loc_descr (&cc_loc_result, ref);
14147       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14148     }
14149
14150   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14151     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14152
14153   return cc_loc_result;
14154 }
14155
14156 /* Output a proper Dwarf location descriptor for a variable or parameter
14157    which is either allocated in a register or in a memory location.  For a
14158    register, we just generate an OP_REG and the register number.  For a
14159    memory location we provide a Dwarf postfix expression describing how to
14160    generate the (dynamic) address of the object onto the address stack.
14161
14162    MODE is mode of the decl if this loc_descriptor is going to be used in
14163    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14164    allowed, VOIDmode otherwise.
14165
14166    If we don't know how to describe it, return 0.  */
14167
14168 static dw_loc_descr_ref
14169 loc_descriptor (rtx rtl, enum machine_mode mode,
14170                 enum var_init_status initialized)
14171 {
14172   dw_loc_descr_ref loc_result = NULL;
14173
14174   switch (GET_CODE (rtl))
14175     {
14176     case SUBREG:
14177       /* The case of a subreg may arise when we have a local (register)
14178          variable or a formal (register) parameter which doesn't quite fill
14179          up an entire register.  For now, just assume that it is
14180          legitimate to make the Dwarf info refer to the whole register which
14181          contains the given subreg.  */
14182       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14183       break;
14184
14185     case REG:
14186       loc_result = reg_loc_descriptor (rtl, initialized);
14187       break;
14188
14189     case SIGN_EXTEND:
14190     case ZERO_EXTEND:
14191       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14192       break;
14193
14194     case MEM:
14195       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14196                                        initialized);
14197       if (loc_result == NULL)
14198         loc_result = tls_mem_loc_descriptor (rtl);
14199       if (loc_result == NULL)
14200         {
14201           rtx new_rtl = avoid_constant_pool_reference (rtl);
14202           if (new_rtl != rtl)
14203             loc_result = loc_descriptor (new_rtl, mode, initialized);
14204         }
14205       break;
14206
14207     case CONCAT:
14208       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14209                                           initialized);
14210       break;
14211
14212     case CONCATN:
14213       loc_result = concatn_loc_descriptor (rtl, initialized);
14214       break;
14215
14216     case VAR_LOCATION:
14217       /* Single part.  */
14218       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14219         {
14220           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14221           if (GET_CODE (loc) == EXPR_LIST)
14222             loc = XEXP (loc, 0);
14223           loc_result = loc_descriptor (loc, mode, initialized);
14224           break;
14225         }
14226
14227       rtl = XEXP (rtl, 1);
14228       /* FALLTHRU */
14229
14230     case PARALLEL:
14231       {
14232         rtvec par_elems = XVEC (rtl, 0);
14233         int num_elem = GET_NUM_ELEM (par_elems);
14234         enum machine_mode mode;
14235         int i;
14236
14237         /* Create the first one, so we have something to add to.  */
14238         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14239                                      VOIDmode, initialized);
14240         if (loc_result == NULL)
14241           return NULL;
14242         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14243         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14244         for (i = 1; i < num_elem; i++)
14245           {
14246             dw_loc_descr_ref temp;
14247
14248             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14249                                    VOIDmode, initialized);
14250             if (temp == NULL)
14251               return NULL;
14252             add_loc_descr (&loc_result, temp);
14253             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14254             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14255           }
14256       }
14257       break;
14258
14259     case CONST_INT:
14260       if (mode != VOIDmode && mode != BLKmode)
14261         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14262                                                     INTVAL (rtl));
14263       break;
14264
14265     case CONST_DOUBLE:
14266       if (mode == VOIDmode)
14267         mode = GET_MODE (rtl);
14268
14269       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14270         {
14271           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14272
14273           /* Note that a CONST_DOUBLE rtx could represent either an integer
14274              or a floating-point constant.  A CONST_DOUBLE is used whenever
14275              the constant requires more than one word in order to be
14276              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14277           loc_result = new_loc_descr (DW_OP_implicit_value,
14278                                       GET_MODE_SIZE (mode), 0);
14279           if (SCALAR_FLOAT_MODE_P (mode))
14280             {
14281               unsigned int length = GET_MODE_SIZE (mode);
14282               unsigned char *array
14283                   = (unsigned char*) ggc_alloc_atomic (length);
14284
14285               insert_float (rtl, array);
14286               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14287               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14288               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14289               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14290             }
14291           else
14292             {
14293               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14294               loc_result->dw_loc_oprnd2.v.val_double
14295                 = rtx_to_double_int (rtl);
14296             }
14297         }
14298       break;
14299
14300     case CONST_VECTOR:
14301       if (mode == VOIDmode)
14302         mode = GET_MODE (rtl);
14303
14304       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14305         {
14306           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14307           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14308           unsigned char *array = (unsigned char *)
14309             ggc_alloc_atomic (length * elt_size);
14310           unsigned int i;
14311           unsigned char *p;
14312
14313           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14314           switch (GET_MODE_CLASS (mode))
14315             {
14316             case MODE_VECTOR_INT:
14317               for (i = 0, p = array; i < length; i++, p += elt_size)
14318                 {
14319                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14320                   double_int val = rtx_to_double_int (elt);
14321
14322                   if (elt_size <= sizeof (HOST_WIDE_INT))
14323                     insert_int (double_int_to_shwi (val), elt_size, p);
14324                   else
14325                     {
14326                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14327                       insert_double (val, p);
14328                     }
14329                 }
14330               break;
14331
14332             case MODE_VECTOR_FLOAT:
14333               for (i = 0, p = array; i < length; i++, p += elt_size)
14334                 {
14335                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14336                   insert_float (elt, p);
14337                 }
14338               break;
14339
14340             default:
14341               gcc_unreachable ();
14342             }
14343
14344           loc_result = new_loc_descr (DW_OP_implicit_value,
14345                                       length * elt_size, 0);
14346           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14347           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14348           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14349           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14350         }
14351       break;
14352
14353     case CONST:
14354       if (mode == VOIDmode
14355           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14356           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14357           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14358         {
14359           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14360           break;
14361         }
14362       /* FALLTHROUGH */
14363     case SYMBOL_REF:
14364       if (!const_ok_for_output (rtl))
14365         break;
14366     case LABEL_REF:
14367       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14368           && (dwarf_version >= 4 || !dwarf_strict))
14369         {
14370           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14371           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14372           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14373           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14374           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14375         }
14376       break;
14377
14378     default:
14379       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14380           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14381           && (dwarf_version >= 4 || !dwarf_strict))
14382         {
14383           /* Value expression.  */
14384           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14385           if (loc_result)
14386             add_loc_descr (&loc_result,
14387                            new_loc_descr (DW_OP_stack_value, 0, 0));
14388         }
14389       break;
14390     }
14391
14392   return loc_result;
14393 }
14394
14395 /* We need to figure out what section we should use as the base for the
14396    address ranges where a given location is valid.
14397    1. If this particular DECL has a section associated with it, use that.
14398    2. If this function has a section associated with it, use that.
14399    3. Otherwise, use the text section.
14400    XXX: If you split a variable across multiple sections, we won't notice.  */
14401
14402 static const char *
14403 secname_for_decl (const_tree decl)
14404 {
14405   const char *secname;
14406
14407   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14408     {
14409       tree sectree = DECL_SECTION_NAME (decl);
14410       secname = TREE_STRING_POINTER (sectree);
14411     }
14412   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14413     {
14414       tree sectree = DECL_SECTION_NAME (current_function_decl);
14415       secname = TREE_STRING_POINTER (sectree);
14416     }
14417   else if (cfun && in_cold_section_p)
14418     secname = crtl->subsections.cold_section_label;
14419   else
14420     secname = text_section_label;
14421
14422   return secname;
14423 }
14424
14425 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14426
14427 static bool
14428 decl_by_reference_p (tree decl)
14429 {
14430   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14431            || TREE_CODE (decl) == VAR_DECL)
14432           && DECL_BY_REFERENCE (decl));
14433 }
14434
14435 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14436    for VARLOC.  */
14437
14438 static dw_loc_descr_ref
14439 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14440                enum var_init_status initialized)
14441 {
14442   int have_address = 0;
14443   dw_loc_descr_ref descr;
14444   enum machine_mode mode;
14445
14446   if (want_address != 2)
14447     {
14448       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14449       /* Single part.  */
14450       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14451         {
14452           varloc = PAT_VAR_LOCATION_LOC (varloc);
14453           if (GET_CODE (varloc) == EXPR_LIST)
14454             varloc = XEXP (varloc, 0);
14455           mode = GET_MODE (varloc);
14456           if (MEM_P (varloc))
14457             {
14458               rtx addr = XEXP (varloc, 0);
14459               descr = mem_loc_descriptor (addr, mode, initialized);
14460               if (descr)
14461                 have_address = 1;
14462               else
14463                 {
14464                   rtx x = avoid_constant_pool_reference (varloc);
14465                   if (x != varloc)
14466                     descr = mem_loc_descriptor (x, mode, initialized);
14467                 }
14468             }
14469           else
14470             descr = mem_loc_descriptor (varloc, mode, initialized);
14471         }
14472       else
14473         return 0;
14474     }
14475   else
14476     {
14477       if (GET_CODE (varloc) == VAR_LOCATION)
14478         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14479       else
14480         mode = DECL_MODE (loc);
14481       descr = loc_descriptor (varloc, mode, initialized);
14482       have_address = 1;
14483     }
14484
14485   if (!descr)
14486     return 0;
14487
14488   if (want_address == 2 && !have_address
14489       && (dwarf_version >= 4 || !dwarf_strict))
14490     {
14491       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14492         {
14493           expansion_failed (loc, NULL_RTX,
14494                             "DWARF address size mismatch");
14495           return 0;
14496         }
14497       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14498       have_address = 1;
14499     }
14500   /* Show if we can't fill the request for an address.  */
14501   if (want_address && !have_address)
14502     {
14503       expansion_failed (loc, NULL_RTX,
14504                         "Want address and only have value");
14505       return 0;
14506     }
14507
14508   /* If we've got an address and don't want one, dereference.  */
14509   if (!want_address && have_address)
14510     {
14511       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14512       enum dwarf_location_atom op;
14513
14514       if (size > DWARF2_ADDR_SIZE || size == -1)
14515         {
14516           expansion_failed (loc, NULL_RTX,
14517                             "DWARF address size mismatch");
14518           return 0;
14519         }
14520       else if (size == DWARF2_ADDR_SIZE)
14521         op = DW_OP_deref;
14522       else
14523         op = DW_OP_deref_size;
14524
14525       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14526     }
14527
14528   return descr;
14529 }
14530
14531 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14532    if it is not possible.  */
14533
14534 static dw_loc_descr_ref
14535 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14536 {
14537   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14538     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14539   else if (dwarf_version >= 3 || !dwarf_strict)
14540     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14541   else
14542     return NULL;
14543 }
14544
14545 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14546    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14547
14548 static dw_loc_descr_ref
14549 dw_sra_loc_expr (tree decl, rtx loc)
14550 {
14551   rtx p;
14552   unsigned int padsize = 0;
14553   dw_loc_descr_ref descr, *descr_tail;
14554   unsigned HOST_WIDE_INT decl_size;
14555   rtx varloc;
14556   enum var_init_status initialized;
14557
14558   if (DECL_SIZE (decl) == NULL
14559       || !host_integerp (DECL_SIZE (decl), 1))
14560     return NULL;
14561
14562   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14563   descr = NULL;
14564   descr_tail = &descr;
14565
14566   for (p = loc; p; p = XEXP (p, 1))
14567     {
14568       unsigned int bitsize = decl_piece_bitsize (p);
14569       rtx loc_note = *decl_piece_varloc_ptr (p);
14570       dw_loc_descr_ref cur_descr;
14571       dw_loc_descr_ref *tail, last = NULL;
14572       unsigned int opsize = 0;
14573
14574       if (loc_note == NULL_RTX
14575           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14576         {
14577           padsize += bitsize;
14578           continue;
14579         }
14580       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14581       varloc = NOTE_VAR_LOCATION (loc_note);
14582       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14583       if (cur_descr == NULL)
14584         {
14585           padsize += bitsize;
14586           continue;
14587         }
14588
14589       /* Check that cur_descr either doesn't use
14590          DW_OP_*piece operations, or their sum is equal
14591          to bitsize.  Otherwise we can't embed it.  */
14592       for (tail = &cur_descr; *tail != NULL;
14593            tail = &(*tail)->dw_loc_next)
14594         if ((*tail)->dw_loc_opc == DW_OP_piece)
14595           {
14596             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14597                       * BITS_PER_UNIT;
14598             last = *tail;
14599           }
14600         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14601           {
14602             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14603             last = *tail;
14604           }
14605
14606       if (last != NULL && opsize != bitsize)
14607         {
14608           padsize += bitsize;
14609           continue;
14610         }
14611
14612       /* If there is a hole, add DW_OP_*piece after empty DWARF
14613          expression, which means that those bits are optimized out.  */
14614       if (padsize)
14615         {
14616           if (padsize > decl_size)
14617             return NULL;
14618           decl_size -= padsize;
14619           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14620           if (*descr_tail == NULL)
14621             return NULL;
14622           descr_tail = &(*descr_tail)->dw_loc_next;
14623           padsize = 0;
14624         }
14625       *descr_tail = cur_descr;
14626       descr_tail = tail;
14627       if (bitsize > decl_size)
14628         return NULL;
14629       decl_size -= bitsize;
14630       if (last == NULL)
14631         {
14632           HOST_WIDE_INT offset = 0;
14633           if (GET_CODE (varloc) == VAR_LOCATION
14634               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14635             {
14636               varloc = PAT_VAR_LOCATION_LOC (varloc);
14637               if (GET_CODE (varloc) == EXPR_LIST)
14638                 varloc = XEXP (varloc, 0);
14639             }
14640           do 
14641             {
14642               if (GET_CODE (varloc) == CONST
14643                   || GET_CODE (varloc) == SIGN_EXTEND
14644                   || GET_CODE (varloc) == ZERO_EXTEND)
14645                 varloc = XEXP (varloc, 0);
14646               else if (GET_CODE (varloc) == SUBREG)
14647                 varloc = SUBREG_REG (varloc);
14648               else
14649                 break;
14650             }
14651           while (1);
14652           /* DW_OP_bit_size offset should be zero for register
14653              or implicit location descriptions and empty location
14654              descriptions, but for memory addresses needs big endian
14655              adjustment.  */
14656           if (MEM_P (varloc))
14657             {
14658               unsigned HOST_WIDE_INT memsize
14659                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14660               if (memsize != bitsize)
14661                 {
14662                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14663                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14664                     return NULL;
14665                   if (memsize < bitsize)
14666                     return NULL;
14667                   if (BITS_BIG_ENDIAN)
14668                     offset = memsize - bitsize;
14669                 }
14670             }
14671
14672           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14673           if (*descr_tail == NULL)
14674             return NULL;
14675           descr_tail = &(*descr_tail)->dw_loc_next;
14676         }
14677     }
14678
14679   /* If there were any non-empty expressions, add padding till the end of
14680      the decl.  */
14681   if (descr != NULL && decl_size != 0)
14682     {
14683       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14684       if (*descr_tail == NULL)
14685         return NULL;
14686     }
14687   return descr;
14688 }
14689
14690 /* Return the dwarf representation of the location list LOC_LIST of
14691    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14692    function.  */
14693
14694 static dw_loc_list_ref
14695 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14696 {
14697   const char *endname, *secname;
14698   rtx varloc;
14699   enum var_init_status initialized;
14700   struct var_loc_node *node;
14701   dw_loc_descr_ref descr;
14702   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14703   dw_loc_list_ref list = NULL;
14704   dw_loc_list_ref *listp = &list;
14705
14706   /* Now that we know what section we are using for a base,
14707      actually construct the list of locations.
14708      The first location information is what is passed to the
14709      function that creates the location list, and the remaining
14710      locations just get added on to that list.
14711      Note that we only know the start address for a location
14712      (IE location changes), so to build the range, we use
14713      the range [current location start, next location start].
14714      This means we have to special case the last node, and generate
14715      a range of [last location start, end of function label].  */
14716
14717   secname = secname_for_decl (decl);
14718
14719   for (node = loc_list->first; node; node = node->next)
14720     if (GET_CODE (node->loc) == EXPR_LIST
14721         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14722       {
14723         if (GET_CODE (node->loc) == EXPR_LIST)
14724           {
14725             /* This requires DW_OP_{,bit_}piece, which is not usable
14726                inside DWARF expressions.  */
14727             if (want_address != 2)
14728               continue;
14729             descr = dw_sra_loc_expr (decl, node->loc);
14730             if (descr == NULL)
14731               continue;
14732           }
14733         else
14734           {
14735             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14736             varloc = NOTE_VAR_LOCATION (node->loc);
14737             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14738           }
14739         if (descr)
14740           {
14741             /* The variable has a location between NODE->LABEL and
14742                NODE->NEXT->LABEL.  */
14743             if (node->next)
14744               endname = node->next->label;
14745             /* If the variable has a location at the last label
14746                it keeps its location until the end of function.  */
14747             else if (!current_function_decl)
14748               endname = text_end_label;
14749             else
14750               {
14751                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14752                                              current_function_funcdef_no);
14753                 endname = ggc_strdup (label_id);
14754               }
14755
14756             *listp = new_loc_list (descr, node->label, endname, secname);
14757             listp = &(*listp)->dw_loc_next;
14758           }
14759       }
14760
14761   /* Try to avoid the overhead of a location list emitting a location
14762      expression instead, but only if we didn't have more than one
14763      location entry in the first place.  If some entries were not
14764      representable, we don't want to pretend a single entry that was
14765      applies to the entire scope in which the variable is
14766      available.  */
14767   if (list && loc_list->first->next)
14768     gen_llsym (list);
14769
14770   return list;
14771 }
14772
14773 /* Return if the loc_list has only single element and thus can be represented
14774    as location description.   */
14775
14776 static bool
14777 single_element_loc_list_p (dw_loc_list_ref list)
14778 {
14779   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14780   return !list->ll_symbol;
14781 }
14782
14783 /* To each location in list LIST add loc descr REF.  */
14784
14785 static void
14786 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14787 {
14788   dw_loc_descr_ref copy;
14789   add_loc_descr (&list->expr, ref);
14790   list = list->dw_loc_next;
14791   while (list)
14792     {
14793       copy = ggc_alloc_dw_loc_descr_node ();
14794       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14795       add_loc_descr (&list->expr, copy);
14796       while (copy->dw_loc_next)
14797         {
14798           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14799           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14800           copy->dw_loc_next = new_copy;
14801           copy = new_copy;
14802         }
14803       list = list->dw_loc_next;
14804     }
14805 }
14806
14807 /* Given two lists RET and LIST
14808    produce location list that is result of adding expression in LIST
14809    to expression in RET on each possition in program.
14810    Might be destructive on both RET and LIST.
14811
14812    TODO: We handle only simple cases of RET or LIST having at most one
14813    element. General case would inolve sorting the lists in program order
14814    and merging them that will need some additional work.
14815    Adding that will improve quality of debug info especially for SRA-ed
14816    structures.  */
14817
14818 static void
14819 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14820 {
14821   if (!list)
14822     return;
14823   if (!*ret)
14824     {
14825       *ret = list;
14826       return;
14827     }
14828   if (!list->dw_loc_next)
14829     {
14830       add_loc_descr_to_each (*ret, list->expr);
14831       return;
14832     }
14833   if (!(*ret)->dw_loc_next)
14834     {
14835       add_loc_descr_to_each (list, (*ret)->expr);
14836       *ret = list;
14837       return;
14838     }
14839   expansion_failed (NULL_TREE, NULL_RTX,
14840                     "Don't know how to merge two non-trivial"
14841                     " location lists.\n");
14842   *ret = NULL;
14843   return;
14844 }
14845
14846 /* LOC is constant expression.  Try a luck, look it up in constant
14847    pool and return its loc_descr of its address.  */
14848
14849 static dw_loc_descr_ref
14850 cst_pool_loc_descr (tree loc)
14851 {
14852   /* Get an RTL for this, if something has been emitted.  */
14853   rtx rtl = lookup_constant_def (loc);
14854   enum machine_mode mode;
14855
14856   if (!rtl || !MEM_P (rtl))
14857     {
14858       gcc_assert (!rtl);
14859       return 0;
14860     }
14861   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14862
14863   /* TODO: We might get more coverage if we was actually delaying expansion
14864      of all expressions till end of compilation when constant pools are fully
14865      populated.  */
14866   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14867     {
14868       expansion_failed (loc, NULL_RTX,
14869                         "CST value in contant pool but not marked.");
14870       return 0;
14871     }
14872   mode = GET_MODE (rtl);
14873   rtl = XEXP (rtl, 0);
14874   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14875 }
14876
14877 /* Return dw_loc_list representing address of addr_expr LOC
14878    by looking for innder INDIRECT_REF expression and turing it
14879    into simple arithmetics.  */
14880
14881 static dw_loc_list_ref
14882 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14883 {
14884   tree obj, offset;
14885   HOST_WIDE_INT bitsize, bitpos, bytepos;
14886   enum machine_mode mode;
14887   int volatilep;
14888   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14889   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14890
14891   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14892                              &bitsize, &bitpos, &offset, &mode,
14893                              &unsignedp, &volatilep, false);
14894   STRIP_NOPS (obj);
14895   if (bitpos % BITS_PER_UNIT)
14896     {
14897       expansion_failed (loc, NULL_RTX, "bitfield access");
14898       return 0;
14899     }
14900   if (!INDIRECT_REF_P (obj))
14901     {
14902       expansion_failed (obj,
14903                         NULL_RTX, "no indirect ref in inner refrence");
14904       return 0;
14905     }
14906   if (!offset && !bitpos)
14907     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14908   else if (toplev
14909            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14910            && (dwarf_version >= 4 || !dwarf_strict))
14911     {
14912       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14913       if (!list_ret)
14914         return 0;
14915       if (offset)
14916         {
14917           /* Variable offset.  */
14918           list_ret1 = loc_list_from_tree (offset, 0);
14919           if (list_ret1 == 0)
14920             return 0;
14921           add_loc_list (&list_ret, list_ret1);
14922           if (!list_ret)
14923             return 0;
14924           add_loc_descr_to_each (list_ret,
14925                                  new_loc_descr (DW_OP_plus, 0, 0));
14926         }
14927       bytepos = bitpos / BITS_PER_UNIT;
14928       if (bytepos > 0)
14929         add_loc_descr_to_each (list_ret,
14930                                new_loc_descr (DW_OP_plus_uconst,
14931                                               bytepos, 0));
14932       else if (bytepos < 0)
14933         loc_list_plus_const (list_ret, bytepos);
14934       add_loc_descr_to_each (list_ret,
14935                              new_loc_descr (DW_OP_stack_value, 0, 0));
14936     }
14937   return list_ret;
14938 }
14939
14940
14941 /* Generate Dwarf location list representing LOC.
14942    If WANT_ADDRESS is false, expression computing LOC will be computed
14943    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14944    if WANT_ADDRESS is 2, expression computing address useable in location
14945      will be returned (i.e. DW_OP_reg can be used
14946      to refer to register values).  */
14947
14948 static dw_loc_list_ref
14949 loc_list_from_tree (tree loc, int want_address)
14950 {
14951   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14952   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14953   int have_address = 0;
14954   enum dwarf_location_atom op;
14955
14956   /* ??? Most of the time we do not take proper care for sign/zero
14957      extending the values properly.  Hopefully this won't be a real
14958      problem...  */
14959
14960   switch (TREE_CODE (loc))
14961     {
14962     case ERROR_MARK:
14963       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14964       return 0;
14965
14966     case PLACEHOLDER_EXPR:
14967       /* This case involves extracting fields from an object to determine the
14968          position of other fields.  We don't try to encode this here.  The
14969          only user of this is Ada, which encodes the needed information using
14970          the names of types.  */
14971       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14972       return 0;
14973
14974     case CALL_EXPR:
14975       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14976       /* There are no opcodes for these operations.  */
14977       return 0;
14978
14979     case PREINCREMENT_EXPR:
14980     case PREDECREMENT_EXPR:
14981     case POSTINCREMENT_EXPR:
14982     case POSTDECREMENT_EXPR:
14983       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14984       /* There are no opcodes for these operations.  */
14985       return 0;
14986
14987     case ADDR_EXPR:
14988       /* If we already want an address, see if there is INDIRECT_REF inside
14989          e.g. for &this->field.  */
14990       if (want_address)
14991         {
14992           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14993                        (loc, want_address == 2);
14994           if (list_ret)
14995             have_address = 1;
14996           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14997                    && (ret = cst_pool_loc_descr (loc)))
14998             have_address = 1;
14999         }
15000         /* Otherwise, process the argument and look for the address.  */
15001       if (!list_ret && !ret)
15002         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15003       else
15004         {
15005           if (want_address)
15006             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15007           return NULL;
15008         }
15009       break;
15010
15011     case VAR_DECL:
15012       if (DECL_THREAD_LOCAL_P (loc))
15013         {
15014           rtx rtl;
15015           enum dwarf_location_atom first_op;
15016           enum dwarf_location_atom second_op;
15017           bool dtprel = false;
15018
15019           if (targetm.have_tls)
15020             {
15021               /* If this is not defined, we have no way to emit the
15022                  data.  */
15023               if (!targetm.asm_out.output_dwarf_dtprel)
15024                 return 0;
15025
15026                /* The way DW_OP_GNU_push_tls_address is specified, we
15027                   can only look up addresses of objects in the current
15028                   module.  */
15029               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15030                 return 0;
15031               first_op = DW_OP_addr;
15032               dtprel = true;
15033               second_op = DW_OP_GNU_push_tls_address;
15034             }
15035           else
15036             {
15037               if (!targetm.emutls.debug_form_tls_address
15038                   || !(dwarf_version >= 3 || !dwarf_strict))
15039                 return 0;
15040               loc = emutls_decl (loc);
15041               first_op = DW_OP_addr;
15042               second_op = DW_OP_form_tls_address;
15043             }
15044
15045           rtl = rtl_for_decl_location (loc);
15046           if (rtl == NULL_RTX)
15047             return 0;
15048
15049           if (!MEM_P (rtl))
15050             return 0;
15051           rtl = XEXP (rtl, 0);
15052           if (! CONSTANT_P (rtl))
15053             return 0;
15054
15055           ret = new_loc_descr (first_op, 0, 0);
15056           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15057           ret->dw_loc_oprnd1.v.val_addr = rtl;
15058           ret->dtprel = dtprel;
15059
15060           ret1 = new_loc_descr (second_op, 0, 0);
15061           add_loc_descr (&ret, ret1);
15062
15063           have_address = 1;
15064           break;
15065         }
15066       /* FALLTHRU */
15067
15068     case PARM_DECL:
15069       if (DECL_HAS_VALUE_EXPR_P (loc))
15070         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15071                                    want_address);
15072       /* FALLTHRU */
15073
15074     case RESULT_DECL:
15075     case FUNCTION_DECL:
15076       {
15077         rtx rtl;
15078         var_loc_list *loc_list = lookup_decl_loc (loc);
15079
15080         if (loc_list && loc_list->first)
15081           {
15082             list_ret = dw_loc_list (loc_list, loc, want_address);
15083             have_address = want_address != 0;
15084             break;
15085           }
15086         rtl = rtl_for_decl_location (loc);
15087         if (rtl == NULL_RTX)
15088           {
15089             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15090             return 0;
15091           }
15092         else if (CONST_INT_P (rtl))
15093           {
15094             HOST_WIDE_INT val = INTVAL (rtl);
15095             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15096               val &= GET_MODE_MASK (DECL_MODE (loc));
15097             ret = int_loc_descriptor (val);
15098           }
15099         else if (GET_CODE (rtl) == CONST_STRING)
15100           {
15101             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15102             return 0;
15103           }
15104         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15105           {
15106             ret = new_loc_descr (DW_OP_addr, 0, 0);
15107             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15108             ret->dw_loc_oprnd1.v.val_addr = rtl;
15109           }
15110         else
15111           {
15112             enum machine_mode mode;
15113
15114             /* Certain constructs can only be represented at top-level.  */
15115             if (want_address == 2)
15116               {
15117                 ret = loc_descriptor (rtl, VOIDmode,
15118                                       VAR_INIT_STATUS_INITIALIZED);
15119                 have_address = 1;
15120               }
15121             else
15122               {
15123                 mode = GET_MODE (rtl);
15124                 if (MEM_P (rtl))
15125                   {
15126                     rtl = XEXP (rtl, 0);
15127                     have_address = 1;
15128                   }
15129                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15130               }
15131             if (!ret)
15132               expansion_failed (loc, rtl,
15133                                 "failed to produce loc descriptor for rtl");
15134           }
15135       }
15136       break;
15137
15138     case INDIRECT_REF:
15139     case ALIGN_INDIRECT_REF:
15140     case MISALIGNED_INDIRECT_REF:
15141       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15142       have_address = 1;
15143       break;
15144
15145     case COMPOUND_EXPR:
15146       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15147
15148     CASE_CONVERT:
15149     case VIEW_CONVERT_EXPR:
15150     case SAVE_EXPR:
15151     case MODIFY_EXPR:
15152       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15153
15154     case COMPONENT_REF:
15155     case BIT_FIELD_REF:
15156     case ARRAY_REF:
15157     case ARRAY_RANGE_REF:
15158     case REALPART_EXPR:
15159     case IMAGPART_EXPR:
15160       {
15161         tree obj, offset;
15162         HOST_WIDE_INT bitsize, bitpos, bytepos;
15163         enum machine_mode mode;
15164         int volatilep;
15165         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15166
15167         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15168                                    &unsignedp, &volatilep, false);
15169
15170         gcc_assert (obj != loc);
15171
15172         list_ret = loc_list_from_tree (obj,
15173                                        want_address == 2
15174                                        && !bitpos && !offset ? 2 : 1);
15175         /* TODO: We can extract value of the small expression via shifting even
15176            for nonzero bitpos.  */
15177         if (list_ret == 0)
15178           return 0;
15179         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15180           {
15181             expansion_failed (loc, NULL_RTX,
15182                               "bitfield access");
15183             return 0;
15184           }
15185
15186         if (offset != NULL_TREE)
15187           {
15188             /* Variable offset.  */
15189             list_ret1 = loc_list_from_tree (offset, 0);
15190             if (list_ret1 == 0)
15191               return 0;
15192             add_loc_list (&list_ret, list_ret1);
15193             if (!list_ret)
15194               return 0;
15195             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15196           }
15197
15198         bytepos = bitpos / BITS_PER_UNIT;
15199         if (bytepos > 0)
15200           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15201         else if (bytepos < 0)
15202           loc_list_plus_const (list_ret, bytepos);
15203
15204         have_address = 1;
15205         break;
15206       }
15207
15208     case INTEGER_CST:
15209       if ((want_address || !host_integerp (loc, 0))
15210           && (ret = cst_pool_loc_descr (loc)))
15211         have_address = 1;
15212       else if (want_address == 2
15213                && host_integerp (loc, 0)
15214                && (ret = address_of_int_loc_descriptor
15215                            (int_size_in_bytes (TREE_TYPE (loc)),
15216                             tree_low_cst (loc, 0))))
15217         have_address = 1;
15218       else if (host_integerp (loc, 0))
15219         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15220       else
15221         {
15222           expansion_failed (loc, NULL_RTX,
15223                             "Integer operand is not host integer");
15224           return 0;
15225         }
15226       break;
15227
15228     case CONSTRUCTOR:
15229     case REAL_CST:
15230     case STRING_CST:
15231     case COMPLEX_CST:
15232       if ((ret = cst_pool_loc_descr (loc)))
15233         have_address = 1;
15234       else
15235       /* We can construct small constants here using int_loc_descriptor.  */
15236         expansion_failed (loc, NULL_RTX,
15237                           "constructor or constant not in constant pool");
15238       break;
15239
15240     case TRUTH_AND_EXPR:
15241     case TRUTH_ANDIF_EXPR:
15242     case BIT_AND_EXPR:
15243       op = DW_OP_and;
15244       goto do_binop;
15245
15246     case TRUTH_XOR_EXPR:
15247     case BIT_XOR_EXPR:
15248       op = DW_OP_xor;
15249       goto do_binop;
15250
15251     case TRUTH_OR_EXPR:
15252     case TRUTH_ORIF_EXPR:
15253     case BIT_IOR_EXPR:
15254       op = DW_OP_or;
15255       goto do_binop;
15256
15257     case FLOOR_DIV_EXPR:
15258     case CEIL_DIV_EXPR:
15259     case ROUND_DIV_EXPR:
15260     case TRUNC_DIV_EXPR:
15261       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15262         return 0;
15263       op = DW_OP_div;
15264       goto do_binop;
15265
15266     case MINUS_EXPR:
15267       op = DW_OP_minus;
15268       goto do_binop;
15269
15270     case FLOOR_MOD_EXPR:
15271     case CEIL_MOD_EXPR:
15272     case ROUND_MOD_EXPR:
15273     case TRUNC_MOD_EXPR:
15274       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15275         {
15276           op = DW_OP_mod;
15277           goto do_binop;
15278         }
15279       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15280       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15281       if (list_ret == 0 || list_ret1 == 0)
15282         return 0;
15283
15284       add_loc_list (&list_ret, list_ret1);
15285       if (list_ret == 0)
15286         return 0;
15287       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15288       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15289       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15290       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15291       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15292       break;
15293
15294     case MULT_EXPR:
15295       op = DW_OP_mul;
15296       goto do_binop;
15297
15298     case LSHIFT_EXPR:
15299       op = DW_OP_shl;
15300       goto do_binop;
15301
15302     case RSHIFT_EXPR:
15303       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15304       goto do_binop;
15305
15306     case POINTER_PLUS_EXPR:
15307     case PLUS_EXPR:
15308       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15309         {
15310           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15311           if (list_ret == 0)
15312             return 0;
15313
15314           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15315           break;
15316         }
15317
15318       op = DW_OP_plus;
15319       goto do_binop;
15320
15321     case LE_EXPR:
15322       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15323         return 0;
15324
15325       op = DW_OP_le;
15326       goto do_binop;
15327
15328     case GE_EXPR:
15329       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15330         return 0;
15331
15332       op = DW_OP_ge;
15333       goto do_binop;
15334
15335     case LT_EXPR:
15336       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15337         return 0;
15338
15339       op = DW_OP_lt;
15340       goto do_binop;
15341
15342     case GT_EXPR:
15343       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15344         return 0;
15345
15346       op = DW_OP_gt;
15347       goto do_binop;
15348
15349     case EQ_EXPR:
15350       op = DW_OP_eq;
15351       goto do_binop;
15352
15353     case NE_EXPR:
15354       op = DW_OP_ne;
15355       goto do_binop;
15356
15357     do_binop:
15358       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15359       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15360       if (list_ret == 0 || list_ret1 == 0)
15361         return 0;
15362
15363       add_loc_list (&list_ret, list_ret1);
15364       if (list_ret == 0)
15365         return 0;
15366       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15367       break;
15368
15369     case TRUTH_NOT_EXPR:
15370     case BIT_NOT_EXPR:
15371       op = DW_OP_not;
15372       goto do_unop;
15373
15374     case ABS_EXPR:
15375       op = DW_OP_abs;
15376       goto do_unop;
15377
15378     case NEGATE_EXPR:
15379       op = DW_OP_neg;
15380       goto do_unop;
15381
15382     do_unop:
15383       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15384       if (list_ret == 0)
15385         return 0;
15386
15387       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15388       break;
15389
15390     case MIN_EXPR:
15391     case MAX_EXPR:
15392       {
15393         const enum tree_code code =
15394           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15395
15396         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15397                       build2 (code, integer_type_node,
15398                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15399                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15400       }
15401
15402       /* ... fall through ...  */
15403
15404     case COND_EXPR:
15405       {
15406         dw_loc_descr_ref lhs
15407           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15408         dw_loc_list_ref rhs
15409           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15410         dw_loc_descr_ref bra_node, jump_node, tmp;
15411
15412         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15413         if (list_ret == 0 || lhs == 0 || rhs == 0)
15414           return 0;
15415
15416         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15417         add_loc_descr_to_each (list_ret, bra_node);
15418
15419         add_loc_list (&list_ret, rhs);
15420         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15421         add_loc_descr_to_each (list_ret, jump_node);
15422
15423         add_loc_descr_to_each (list_ret, lhs);
15424         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15425         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15426
15427         /* ??? Need a node to point the skip at.  Use a nop.  */
15428         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15429         add_loc_descr_to_each (list_ret, tmp);
15430         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15431         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15432       }
15433       break;
15434
15435     case FIX_TRUNC_EXPR:
15436       return 0;
15437
15438     default:
15439       /* Leave front-end specific codes as simply unknown.  This comes
15440          up, for instance, with the C STMT_EXPR.  */
15441       if ((unsigned int) TREE_CODE (loc)
15442           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15443         {
15444           expansion_failed (loc, NULL_RTX,
15445                             "language specific tree node");
15446           return 0;
15447         }
15448
15449 #ifdef ENABLE_CHECKING
15450       /* Otherwise this is a generic code; we should just lists all of
15451          these explicitly.  We forgot one.  */
15452       gcc_unreachable ();
15453 #else
15454       /* In a release build, we want to degrade gracefully: better to
15455          generate incomplete debugging information than to crash.  */
15456       return NULL;
15457 #endif
15458     }
15459
15460   if (!ret && !list_ret)
15461     return 0;
15462
15463   if (want_address == 2 && !have_address
15464       && (dwarf_version >= 4 || !dwarf_strict))
15465     {
15466       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15467         {
15468           expansion_failed (loc, NULL_RTX,
15469                             "DWARF address size mismatch");
15470           return 0;
15471         }
15472       if (ret)
15473         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15474       else
15475         add_loc_descr_to_each (list_ret,
15476                                new_loc_descr (DW_OP_stack_value, 0, 0));
15477       have_address = 1;
15478     }
15479   /* Show if we can't fill the request for an address.  */
15480   if (want_address && !have_address)
15481     {
15482       expansion_failed (loc, NULL_RTX,
15483                         "Want address and only have value");
15484       return 0;
15485     }
15486
15487   gcc_assert (!ret || !list_ret);
15488
15489   /* If we've got an address and don't want one, dereference.  */
15490   if (!want_address && have_address)
15491     {
15492       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15493
15494       if (size > DWARF2_ADDR_SIZE || size == -1)
15495         {
15496           expansion_failed (loc, NULL_RTX,
15497                             "DWARF address size mismatch");
15498           return 0;
15499         }
15500       else if (size == DWARF2_ADDR_SIZE)
15501         op = DW_OP_deref;
15502       else
15503         op = DW_OP_deref_size;
15504
15505       if (ret)
15506         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15507       else
15508         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15509     }
15510   if (ret)
15511     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15512
15513   return list_ret;
15514 }
15515
15516 /* Same as above but return only single location expression.  */
15517 static dw_loc_descr_ref
15518 loc_descriptor_from_tree (tree loc, int want_address)
15519 {
15520   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15521   if (!ret)
15522     return NULL;
15523   if (ret->dw_loc_next)
15524     {
15525       expansion_failed (loc, NULL_RTX,
15526                         "Location list where only loc descriptor needed");
15527       return NULL;
15528     }
15529   return ret->expr;
15530 }
15531
15532 /* Given a value, round it up to the lowest multiple of `boundary'
15533    which is not less than the value itself.  */
15534
15535 static inline HOST_WIDE_INT
15536 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15537 {
15538   return (((value + boundary - 1) / boundary) * boundary);
15539 }
15540
15541 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15542    pointer to the declared type for the relevant field variable, or return
15543    `integer_type_node' if the given node turns out to be an
15544    ERROR_MARK node.  */
15545
15546 static inline tree
15547 field_type (const_tree decl)
15548 {
15549   tree type;
15550
15551   if (TREE_CODE (decl) == ERROR_MARK)
15552     return integer_type_node;
15553
15554   type = DECL_BIT_FIELD_TYPE (decl);
15555   if (type == NULL_TREE)
15556     type = TREE_TYPE (decl);
15557
15558   return type;
15559 }
15560
15561 /* Given a pointer to a tree node, return the alignment in bits for
15562    it, or else return BITS_PER_WORD if the node actually turns out to
15563    be an ERROR_MARK node.  */
15564
15565 static inline unsigned
15566 simple_type_align_in_bits (const_tree type)
15567 {
15568   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15569 }
15570
15571 static inline unsigned
15572 simple_decl_align_in_bits (const_tree decl)
15573 {
15574   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15575 }
15576
15577 /* Return the result of rounding T up to ALIGN.  */
15578
15579 static inline double_int
15580 round_up_to_align (double_int t, unsigned int align)
15581 {
15582   double_int alignd = uhwi_to_double_int (align);
15583   t = double_int_add (t, alignd);
15584   t = double_int_add (t, double_int_minus_one);
15585   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15586   t = double_int_mul (t, alignd);
15587   return t;
15588 }
15589
15590 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15591    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15592    or return 0 if we are unable to determine what that offset is, either
15593    because the argument turns out to be a pointer to an ERROR_MARK node, or
15594    because the offset is actually variable.  (We can't handle the latter case
15595    just yet).  */
15596
15597 static HOST_WIDE_INT
15598 field_byte_offset (const_tree decl)
15599 {
15600   double_int object_offset_in_bits;
15601   double_int object_offset_in_bytes;
15602   double_int bitpos_int;
15603
15604   if (TREE_CODE (decl) == ERROR_MARK)
15605     return 0;
15606
15607   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15608
15609   /* We cannot yet cope with fields whose positions are variable, so
15610      for now, when we see such things, we simply return 0.  Someday, we may
15611      be able to handle such cases, but it will be damn difficult.  */
15612   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15613     return 0;
15614
15615   bitpos_int = tree_to_double_int (bit_position (decl));
15616
15617 #ifdef PCC_BITFIELD_TYPE_MATTERS
15618   if (PCC_BITFIELD_TYPE_MATTERS)
15619     {
15620       tree type;
15621       tree field_size_tree;
15622       double_int deepest_bitpos;
15623       double_int field_size_in_bits;
15624       unsigned int type_align_in_bits;
15625       unsigned int decl_align_in_bits;
15626       double_int type_size_in_bits;
15627
15628       type = field_type (decl);
15629       type_size_in_bits = double_int_type_size_in_bits (type);
15630       type_align_in_bits = simple_type_align_in_bits (type);
15631
15632       field_size_tree = DECL_SIZE (decl);
15633
15634       /* The size could be unspecified if there was an error, or for
15635          a flexible array member.  */
15636       if (!field_size_tree)
15637         field_size_tree = bitsize_zero_node;
15638
15639       /* If the size of the field is not constant, use the type size.  */
15640       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15641         field_size_in_bits = tree_to_double_int (field_size_tree);
15642       else
15643         field_size_in_bits = type_size_in_bits;
15644
15645       decl_align_in_bits = simple_decl_align_in_bits (decl);
15646
15647       /* The GCC front-end doesn't make any attempt to keep track of the
15648          starting bit offset (relative to the start of the containing
15649          structure type) of the hypothetical "containing object" for a
15650          bit-field.  Thus, when computing the byte offset value for the
15651          start of the "containing object" of a bit-field, we must deduce
15652          this information on our own. This can be rather tricky to do in
15653          some cases.  For example, handling the following structure type
15654          definition when compiling for an i386/i486 target (which only
15655          aligns long long's to 32-bit boundaries) can be very tricky:
15656
15657          struct S { int field1; long long field2:31; };
15658
15659          Fortunately, there is a simple rule-of-thumb which can be used
15660          in such cases.  When compiling for an i386/i486, GCC will
15661          allocate 8 bytes for the structure shown above.  It decides to
15662          do this based upon one simple rule for bit-field allocation.
15663          GCC allocates each "containing object" for each bit-field at
15664          the first (i.e. lowest addressed) legitimate alignment boundary
15665          (based upon the required minimum alignment for the declared
15666          type of the field) which it can possibly use, subject to the
15667          condition that there is still enough available space remaining
15668          in the containing object (when allocated at the selected point)
15669          to fully accommodate all of the bits of the bit-field itself.
15670
15671          This simple rule makes it obvious why GCC allocates 8 bytes for
15672          each object of the structure type shown above.  When looking
15673          for a place to allocate the "containing object" for `field2',
15674          the compiler simply tries to allocate a 64-bit "containing
15675          object" at each successive 32-bit boundary (starting at zero)
15676          until it finds a place to allocate that 64- bit field such that
15677          at least 31 contiguous (and previously unallocated) bits remain
15678          within that selected 64 bit field.  (As it turns out, for the
15679          example above, the compiler finds it is OK to allocate the
15680          "containing object" 64-bit field at bit-offset zero within the
15681          structure type.)
15682
15683          Here we attempt to work backwards from the limited set of facts
15684          we're given, and we try to deduce from those facts, where GCC
15685          must have believed that the containing object started (within
15686          the structure type). The value we deduce is then used (by the
15687          callers of this routine) to generate DW_AT_location and
15688          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15689          the case of DW_AT_location, regular fields as well).  */
15690
15691       /* Figure out the bit-distance from the start of the structure to
15692          the "deepest" bit of the bit-field.  */
15693       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15694
15695       /* This is the tricky part.  Use some fancy footwork to deduce
15696          where the lowest addressed bit of the containing object must
15697          be.  */
15698       object_offset_in_bits
15699         = double_int_add (deepest_bitpos, double_int_neg (type_size_in_bits));
15700
15701       /* Round up to type_align by default.  This works best for
15702          bitfields.  */
15703       object_offset_in_bits
15704         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15705
15706       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15707         {
15708           object_offset_in_bits
15709             = double_int_add (deepest_bitpos,
15710                               double_int_neg (type_size_in_bits));
15711
15712           /* Round up to decl_align instead.  */
15713           object_offset_in_bits
15714             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15715         }
15716     }
15717   else
15718 #endif
15719     object_offset_in_bits = bitpos_int;
15720
15721   object_offset_in_bytes
15722     = double_int_div (object_offset_in_bits,
15723                       uhwi_to_double_int (BITS_PER_UNIT), true,
15724                       TRUNC_DIV_EXPR);
15725   return double_int_to_shwi (object_offset_in_bytes);
15726 }
15727 \f
15728 /* The following routines define various Dwarf attributes and any data
15729    associated with them.  */
15730
15731 /* Add a location description attribute value to a DIE.
15732
15733    This emits location attributes suitable for whole variables and
15734    whole parameters.  Note that the location attributes for struct fields are
15735    generated by the routine `data_member_location_attribute' below.  */
15736
15737 static inline void
15738 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15739                              dw_loc_list_ref descr)
15740 {
15741   if (descr == 0)
15742     return;
15743   if (single_element_loc_list_p (descr))
15744     add_AT_loc (die, attr_kind, descr->expr);
15745   else
15746     add_AT_loc_list (die, attr_kind, descr);
15747 }
15748
15749 /* Attach the specialized form of location attribute used for data members of
15750    struct and union types.  In the special case of a FIELD_DECL node which
15751    represents a bit-field, the "offset" part of this special location
15752    descriptor must indicate the distance in bytes from the lowest-addressed
15753    byte of the containing struct or union type to the lowest-addressed byte of
15754    the "containing object" for the bit-field.  (See the `field_byte_offset'
15755    function above).
15756
15757    For any given bit-field, the "containing object" is a hypothetical object
15758    (of some integral or enum type) within which the given bit-field lives.  The
15759    type of this hypothetical "containing object" is always the same as the
15760    declared type of the individual bit-field itself (for GCC anyway... the
15761    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15762    bytes) of the hypothetical "containing object" which will be given in the
15763    DW_AT_byte_size attribute for this bit-field.  (See the
15764    `byte_size_attribute' function below.)  It is also used when calculating the
15765    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15766    function below.)  */
15767
15768 static void
15769 add_data_member_location_attribute (dw_die_ref die, tree decl)
15770 {
15771   HOST_WIDE_INT offset;
15772   dw_loc_descr_ref loc_descr = 0;
15773
15774   if (TREE_CODE (decl) == TREE_BINFO)
15775     {
15776       /* We're working on the TAG_inheritance for a base class.  */
15777       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15778         {
15779           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15780              aren't at a fixed offset from all (sub)objects of the same
15781              type.  We need to extract the appropriate offset from our
15782              vtable.  The following dwarf expression means
15783
15784                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15785
15786              This is specific to the V3 ABI, of course.  */
15787
15788           dw_loc_descr_ref tmp;
15789
15790           /* Make a copy of the object address.  */
15791           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15792           add_loc_descr (&loc_descr, tmp);
15793
15794           /* Extract the vtable address.  */
15795           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15796           add_loc_descr (&loc_descr, tmp);
15797
15798           /* Calculate the address of the offset.  */
15799           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15800           gcc_assert (offset < 0);
15801
15802           tmp = int_loc_descriptor (-offset);
15803           add_loc_descr (&loc_descr, tmp);
15804           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15805           add_loc_descr (&loc_descr, tmp);
15806
15807           /* Extract the offset.  */
15808           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15809           add_loc_descr (&loc_descr, tmp);
15810
15811           /* Add it to the object address.  */
15812           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15813           add_loc_descr (&loc_descr, tmp);
15814         }
15815       else
15816         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15817     }
15818   else
15819     offset = field_byte_offset (decl);
15820
15821   if (! loc_descr)
15822     {
15823       if (dwarf_version > 2)
15824         {
15825           /* Don't need to output a location expression, just the constant. */
15826           add_AT_int (die, DW_AT_data_member_location, offset);
15827           return;
15828         }
15829       else
15830         {
15831           enum dwarf_location_atom op;
15832
15833           /* The DWARF2 standard says that we should assume that the structure
15834              address is already on the stack, so we can specify a structure
15835              field address by using DW_OP_plus_uconst.  */
15836
15837 #ifdef MIPS_DEBUGGING_INFO
15838           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15839              operator correctly.  It works only if we leave the offset on the
15840              stack.  */
15841           op = DW_OP_constu;
15842 #else
15843           op = DW_OP_plus_uconst;
15844 #endif
15845
15846           loc_descr = new_loc_descr (op, offset, 0);
15847         }
15848     }
15849
15850   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15851 }
15852
15853 /* Writes integer values to dw_vec_const array.  */
15854
15855 static void
15856 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15857 {
15858   while (size != 0)
15859     {
15860       *dest++ = val & 0xff;
15861       val >>= 8;
15862       --size;
15863     }
15864 }
15865
15866 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15867
15868 static HOST_WIDE_INT
15869 extract_int (const unsigned char *src, unsigned int size)
15870 {
15871   HOST_WIDE_INT val = 0;
15872
15873   src += size;
15874   while (size != 0)
15875     {
15876       val <<= 8;
15877       val |= *--src & 0xff;
15878       --size;
15879     }
15880   return val;
15881 }
15882
15883 /* Writes double_int values to dw_vec_const array.  */
15884
15885 static void
15886 insert_double (double_int val, unsigned char *dest)
15887 {
15888   unsigned char *p0 = dest;
15889   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15890
15891   if (WORDS_BIG_ENDIAN)
15892     {
15893       p0 = p1;
15894       p1 = dest;
15895     }
15896
15897   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15898   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15899 }
15900
15901 /* Writes floating point values to dw_vec_const array.  */
15902
15903 static void
15904 insert_float (const_rtx rtl, unsigned char *array)
15905 {
15906   REAL_VALUE_TYPE rv;
15907   long val[4];
15908   int i;
15909
15910   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15911   real_to_target (val, &rv, GET_MODE (rtl));
15912
15913   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15914   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15915     {
15916       insert_int (val[i], 4, array);
15917       array += 4;
15918     }
15919 }
15920
15921 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15922    does not have a "location" either in memory or in a register.  These
15923    things can arise in GNU C when a constant is passed as an actual parameter
15924    to an inlined function.  They can also arise in C++ where declared
15925    constants do not necessarily get memory "homes".  */
15926
15927 static bool
15928 add_const_value_attribute (dw_die_ref die, rtx rtl)
15929 {
15930   switch (GET_CODE (rtl))
15931     {
15932     case CONST_INT:
15933       {
15934         HOST_WIDE_INT val = INTVAL (rtl);
15935
15936         if (val < 0)
15937           add_AT_int (die, DW_AT_const_value, val);
15938         else
15939           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15940       }
15941       return true;
15942
15943     case CONST_DOUBLE:
15944       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15945          floating-point constant.  A CONST_DOUBLE is used whenever the
15946          constant requires more than one word in order to be adequately
15947          represented.  */
15948       {
15949         enum machine_mode mode = GET_MODE (rtl);
15950
15951         if (SCALAR_FLOAT_MODE_P (mode))
15952           {
15953             unsigned int length = GET_MODE_SIZE (mode);
15954             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15955
15956             insert_float (rtl, array);
15957             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15958           }
15959         else
15960           add_AT_double (die, DW_AT_const_value,
15961                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15962       }
15963       return true;
15964
15965     case CONST_VECTOR:
15966       {
15967         enum machine_mode mode = GET_MODE (rtl);
15968         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15969         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15970         unsigned char *array = (unsigned char *) ggc_alloc_atomic
15971           (length * elt_size);
15972         unsigned int i;
15973         unsigned char *p;
15974
15975         switch (GET_MODE_CLASS (mode))
15976           {
15977           case MODE_VECTOR_INT:
15978             for (i = 0, p = array; i < length; i++, p += elt_size)
15979               {
15980                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15981                 double_int val = rtx_to_double_int (elt);
15982
15983                 if (elt_size <= sizeof (HOST_WIDE_INT))
15984                   insert_int (double_int_to_shwi (val), elt_size, p);
15985                 else
15986                   {
15987                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15988                     insert_double (val, p);
15989                   }
15990               }
15991             break;
15992
15993           case MODE_VECTOR_FLOAT:
15994             for (i = 0, p = array; i < length; i++, p += elt_size)
15995               {
15996                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15997                 insert_float (elt, p);
15998               }
15999             break;
16000
16001           default:
16002             gcc_unreachable ();
16003           }
16004
16005         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16006       }
16007       return true;
16008
16009     case CONST_STRING:
16010       if (dwarf_version >= 4 || !dwarf_strict)
16011         {
16012           dw_loc_descr_ref loc_result;
16013           resolve_one_addr (&rtl, NULL);
16014         rtl_addr:
16015           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16016           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16017           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16018           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16019           add_AT_loc (die, DW_AT_location, loc_result);
16020           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16021           return true;
16022         }
16023       return false;
16024
16025     case CONST:
16026       if (CONSTANT_P (XEXP (rtl, 0)))
16027         return add_const_value_attribute (die, XEXP (rtl, 0));
16028       /* FALLTHROUGH */
16029     case SYMBOL_REF:
16030       if (!const_ok_for_output (rtl))
16031         return false;
16032     case LABEL_REF:
16033       if (dwarf_version >= 4 || !dwarf_strict)
16034         goto rtl_addr;
16035       return false;
16036
16037     case PLUS:
16038       /* In cases where an inlined instance of an inline function is passed
16039          the address of an `auto' variable (which is local to the caller) we
16040          can get a situation where the DECL_RTL of the artificial local
16041          variable (for the inlining) which acts as a stand-in for the
16042          corresponding formal parameter (of the inline function) will look
16043          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16044          exactly a compile-time constant expression, but it isn't the address
16045          of the (artificial) local variable either.  Rather, it represents the
16046          *value* which the artificial local variable always has during its
16047          lifetime.  We currently have no way to represent such quasi-constant
16048          values in Dwarf, so for now we just punt and generate nothing.  */
16049       return false;
16050
16051     case HIGH:
16052     case CONST_FIXED:
16053       return false;
16054
16055     case MEM:
16056       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16057           && MEM_READONLY_P (rtl)
16058           && GET_MODE (rtl) == BLKmode)
16059         {
16060           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16061           return true;
16062         }
16063       return false;
16064
16065     default:
16066       /* No other kinds of rtx should be possible here.  */
16067       gcc_unreachable ();
16068     }
16069   return false;
16070 }
16071
16072 /* Determine whether the evaluation of EXPR references any variables
16073    or functions which aren't otherwise used (and therefore may not be
16074    output).  */
16075 static tree
16076 reference_to_unused (tree * tp, int * walk_subtrees,
16077                      void * data ATTRIBUTE_UNUSED)
16078 {
16079   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16080     *walk_subtrees = 0;
16081
16082   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16083       && ! TREE_ASM_WRITTEN (*tp))
16084     return *tp;
16085   /* ???  The C++ FE emits debug information for using decls, so
16086      putting gcc_unreachable here falls over.  See PR31899.  For now
16087      be conservative.  */
16088   else if (!cgraph_global_info_ready
16089            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16090     return *tp;
16091   else if (TREE_CODE (*tp) == VAR_DECL)
16092     {
16093       struct varpool_node *node = varpool_get_node (*tp);
16094       if (!node || !node->needed)
16095         return *tp;
16096     }
16097   else if (TREE_CODE (*tp) == FUNCTION_DECL
16098            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16099     {
16100       /* The call graph machinery must have finished analyzing,
16101          optimizing and gimplifying the CU by now.
16102          So if *TP has no call graph node associated
16103          to it, it means *TP will not be emitted.  */
16104       if (!cgraph_get_node (*tp))
16105         return *tp;
16106     }
16107   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16108     return *tp;
16109
16110   return NULL_TREE;
16111 }
16112
16113 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16114    for use in a later add_const_value_attribute call.  */
16115
16116 static rtx
16117 rtl_for_decl_init (tree init, tree type)
16118 {
16119   rtx rtl = NULL_RTX;
16120
16121   /* If a variable is initialized with a string constant without embedded
16122      zeros, build CONST_STRING.  */
16123   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16124     {
16125       tree enttype = TREE_TYPE (type);
16126       tree domain = TYPE_DOMAIN (type);
16127       enum machine_mode mode = TYPE_MODE (enttype);
16128
16129       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16130           && domain
16131           && integer_zerop (TYPE_MIN_VALUE (domain))
16132           && compare_tree_int (TYPE_MAX_VALUE (domain),
16133                                TREE_STRING_LENGTH (init) - 1) == 0
16134           && ((size_t) TREE_STRING_LENGTH (init)
16135               == strlen (TREE_STRING_POINTER (init)) + 1))
16136         {
16137           rtl = gen_rtx_CONST_STRING (VOIDmode,
16138                                       ggc_strdup (TREE_STRING_POINTER (init)));
16139           rtl = gen_rtx_MEM (BLKmode, rtl);
16140           MEM_READONLY_P (rtl) = 1;
16141         }
16142     }
16143   /* Other aggregates, and complex values, could be represented using
16144      CONCAT: FIXME!  */
16145   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16146     ;
16147   /* Vectors only work if their mode is supported by the target.
16148      FIXME: generic vectors ought to work too.  */
16149   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16150     ;
16151   /* If the initializer is something that we know will expand into an
16152      immediate RTL constant, expand it now.  We must be careful not to
16153      reference variables which won't be output.  */
16154   else if (initializer_constant_valid_p (init, type)
16155            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16156     {
16157       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16158          possible.  */
16159       if (TREE_CODE (type) == VECTOR_TYPE)
16160         switch (TREE_CODE (init))
16161           {
16162           case VECTOR_CST:
16163             break;
16164           case CONSTRUCTOR:
16165             if (TREE_CONSTANT (init))
16166               {
16167                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16168                 bool constant_p = true;
16169                 tree value;
16170                 unsigned HOST_WIDE_INT ix;
16171
16172                 /* Even when ctor is constant, it might contain non-*_CST
16173                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16174                    belong into VECTOR_CST nodes.  */
16175                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16176                   if (!CONSTANT_CLASS_P (value))
16177                     {
16178                       constant_p = false;
16179                       break;
16180                     }
16181
16182                 if (constant_p)
16183                   {
16184                     init = build_vector_from_ctor (type, elts);
16185                     break;
16186                   }
16187               }
16188             /* FALLTHRU */
16189
16190           default:
16191             return NULL;
16192           }
16193
16194       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16195
16196       /* If expand_expr returns a MEM, it wasn't immediate.  */
16197       gcc_assert (!rtl || !MEM_P (rtl));
16198     }
16199
16200   return rtl;
16201 }
16202
16203 /* Generate RTL for the variable DECL to represent its location.  */
16204
16205 static rtx
16206 rtl_for_decl_location (tree decl)
16207 {
16208   rtx rtl;
16209
16210   /* Here we have to decide where we are going to say the parameter "lives"
16211      (as far as the debugger is concerned).  We only have a couple of
16212      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16213
16214      DECL_RTL normally indicates where the parameter lives during most of the
16215      activation of the function.  If optimization is enabled however, this
16216      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16217      that the parameter doesn't really live anywhere (as far as the code
16218      generation parts of GCC are concerned) during most of the function's
16219      activation.  That will happen (for example) if the parameter is never
16220      referenced within the function.
16221
16222      We could just generate a location descriptor here for all non-NULL
16223      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16224      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16225      where DECL_RTL is NULL or is a pseudo-reg.
16226
16227      Note however that we can only get away with using DECL_INCOMING_RTL as
16228      a backup substitute for DECL_RTL in certain limited cases.  In cases
16229      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16230      we can be sure that the parameter was passed using the same type as it is
16231      declared to have within the function, and that its DECL_INCOMING_RTL
16232      points us to a place where a value of that type is passed.
16233
16234      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16235      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16236      because in these cases DECL_INCOMING_RTL points us to a value of some
16237      type which is *different* from the type of the parameter itself.  Thus,
16238      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16239      such cases, the debugger would end up (for example) trying to fetch a
16240      `float' from a place which actually contains the first part of a
16241      `double'.  That would lead to really incorrect and confusing
16242      output at debug-time.
16243
16244      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16245      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16246      are a couple of exceptions however.  On little-endian machines we can
16247      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16248      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16249      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16250      when (on a little-endian machine) a non-prototyped function has a
16251      parameter declared to be of type `short' or `char'.  In such cases,
16252      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16253      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16254      passed `int' value.  If the debugger then uses that address to fetch
16255      a `short' or a `char' (on a little-endian machine) the result will be
16256      the correct data, so we allow for such exceptional cases below.
16257
16258      Note that our goal here is to describe the place where the given formal
16259      parameter lives during most of the function's activation (i.e. between the
16260      end of the prologue and the start of the epilogue).  We'll do that as best
16261      as we can. Note however that if the given formal parameter is modified
16262      sometime during the execution of the function, then a stack backtrace (at
16263      debug-time) will show the function as having been called with the *new*
16264      value rather than the value which was originally passed in.  This happens
16265      rarely enough that it is not a major problem, but it *is* a problem, and
16266      I'd like to fix it.
16267
16268      A future version of dwarf2out.c may generate two additional attributes for
16269      any given DW_TAG_formal_parameter DIE which will describe the "passed
16270      type" and the "passed location" for the given formal parameter in addition
16271      to the attributes we now generate to indicate the "declared type" and the
16272      "active location" for each parameter.  This additional set of attributes
16273      could be used by debuggers for stack backtraces. Separately, note that
16274      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16275      This happens (for example) for inlined-instances of inline function formal
16276      parameters which are never referenced.  This really shouldn't be
16277      happening.  All PARM_DECL nodes should get valid non-NULL
16278      DECL_INCOMING_RTL values.  FIXME.  */
16279
16280   /* Use DECL_RTL as the "location" unless we find something better.  */
16281   rtl = DECL_RTL_IF_SET (decl);
16282
16283   /* When generating abstract instances, ignore everything except
16284      constants, symbols living in memory, and symbols living in
16285      fixed registers.  */
16286   if (! reload_completed)
16287     {
16288       if (rtl
16289           && (CONSTANT_P (rtl)
16290               || (MEM_P (rtl)
16291                   && CONSTANT_P (XEXP (rtl, 0)))
16292               || (REG_P (rtl)
16293                   && TREE_CODE (decl) == VAR_DECL
16294                   && TREE_STATIC (decl))))
16295         {
16296           rtl = targetm.delegitimize_address (rtl);
16297           return rtl;
16298         }
16299       rtl = NULL_RTX;
16300     }
16301   else if (TREE_CODE (decl) == PARM_DECL)
16302     {
16303       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16304         {
16305           tree declared_type = TREE_TYPE (decl);
16306           tree passed_type = DECL_ARG_TYPE (decl);
16307           enum machine_mode dmode = TYPE_MODE (declared_type);
16308           enum machine_mode pmode = TYPE_MODE (passed_type);
16309
16310           /* This decl represents a formal parameter which was optimized out.
16311              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16312              all cases where (rtl == NULL_RTX) just below.  */
16313           if (dmode == pmode)
16314             rtl = DECL_INCOMING_RTL (decl);
16315           else if (SCALAR_INT_MODE_P (dmode)
16316                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16317                    && DECL_INCOMING_RTL (decl))
16318             {
16319               rtx inc = DECL_INCOMING_RTL (decl);
16320               if (REG_P (inc))
16321                 rtl = inc;
16322               else if (MEM_P (inc))
16323                 {
16324                   if (BYTES_BIG_ENDIAN)
16325                     rtl = adjust_address_nv (inc, dmode,
16326                                              GET_MODE_SIZE (pmode)
16327                                              - GET_MODE_SIZE (dmode));
16328                   else
16329                     rtl = inc;
16330                 }
16331             }
16332         }
16333
16334       /* If the parm was passed in registers, but lives on the stack, then
16335          make a big endian correction if the mode of the type of the
16336          parameter is not the same as the mode of the rtl.  */
16337       /* ??? This is the same series of checks that are made in dbxout.c before
16338          we reach the big endian correction code there.  It isn't clear if all
16339          of these checks are necessary here, but keeping them all is the safe
16340          thing to do.  */
16341       else if (MEM_P (rtl)
16342                && XEXP (rtl, 0) != const0_rtx
16343                && ! CONSTANT_P (XEXP (rtl, 0))
16344                /* Not passed in memory.  */
16345                && !MEM_P (DECL_INCOMING_RTL (decl))
16346                /* Not passed by invisible reference.  */
16347                && (!REG_P (XEXP (rtl, 0))
16348                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16349                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16350 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16351                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16352 #endif
16353                      )
16354                /* Big endian correction check.  */
16355                && BYTES_BIG_ENDIAN
16356                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16357                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16358                    < UNITS_PER_WORD))
16359         {
16360           int offset = (UNITS_PER_WORD
16361                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16362
16363           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16364                              plus_constant (XEXP (rtl, 0), offset));
16365         }
16366     }
16367   else if (TREE_CODE (decl) == VAR_DECL
16368            && rtl
16369            && MEM_P (rtl)
16370            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16371            && BYTES_BIG_ENDIAN)
16372     {
16373       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16374       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16375
16376       /* If a variable is declared "register" yet is smaller than
16377          a register, then if we store the variable to memory, it
16378          looks like we're storing a register-sized value, when in
16379          fact we are not.  We need to adjust the offset of the
16380          storage location to reflect the actual value's bytes,
16381          else gdb will not be able to display it.  */
16382       if (rsize > dsize)
16383         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16384                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16385     }
16386
16387   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16388      and will have been substituted directly into all expressions that use it.
16389      C does not have such a concept, but C++ and other languages do.  */
16390   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16391     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16392
16393   if (rtl)
16394     rtl = targetm.delegitimize_address (rtl);
16395
16396   /* If we don't look past the constant pool, we risk emitting a
16397      reference to a constant pool entry that isn't referenced from
16398      code, and thus is not emitted.  */
16399   if (rtl)
16400     rtl = avoid_constant_pool_reference (rtl);
16401
16402   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16403      in the current CU, resolve_addr will remove the expression referencing
16404      it.  */
16405   if (rtl == NULL_RTX
16406       && TREE_CODE (decl) == VAR_DECL
16407       && !DECL_EXTERNAL (decl)
16408       && TREE_STATIC (decl)
16409       && DECL_NAME (decl)
16410       && !DECL_HARD_REGISTER (decl)
16411       && DECL_MODE (decl) != VOIDmode)
16412     {
16413       rtl = make_decl_rtl_for_debug (decl);
16414       if (!MEM_P (rtl)
16415           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16416           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16417         rtl = NULL_RTX;
16418     }
16419
16420   return rtl;
16421 }
16422
16423 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16424    returned.  If so, the decl for the COMMON block is returned, and the
16425    value is the offset into the common block for the symbol.  */
16426
16427 static tree
16428 fortran_common (tree decl, HOST_WIDE_INT *value)
16429 {
16430   tree val_expr, cvar;
16431   enum machine_mode mode;
16432   HOST_WIDE_INT bitsize, bitpos;
16433   tree offset;
16434   int volatilep = 0, unsignedp = 0;
16435
16436   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16437      it does not have a value (the offset into the common area), or if it
16438      is thread local (as opposed to global) then it isn't common, and shouldn't
16439      be handled as such.  */
16440   if (TREE_CODE (decl) != VAR_DECL
16441       || !TREE_STATIC (decl)
16442       || !DECL_HAS_VALUE_EXPR_P (decl)
16443       || !is_fortran ())
16444     return NULL_TREE;
16445
16446   val_expr = DECL_VALUE_EXPR (decl);
16447   if (TREE_CODE (val_expr) != COMPONENT_REF)
16448     return NULL_TREE;
16449
16450   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16451                               &mode, &unsignedp, &volatilep, true);
16452
16453   if (cvar == NULL_TREE
16454       || TREE_CODE (cvar) != VAR_DECL
16455       || DECL_ARTIFICIAL (cvar)
16456       || !TREE_PUBLIC (cvar))
16457     return NULL_TREE;
16458
16459   *value = 0;
16460   if (offset != NULL)
16461     {
16462       if (!host_integerp (offset, 0))
16463         return NULL_TREE;
16464       *value = tree_low_cst (offset, 0);
16465     }
16466   if (bitpos != 0)
16467     *value += bitpos / BITS_PER_UNIT;
16468
16469   return cvar;
16470 }
16471
16472 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16473    data attribute for a variable or a parameter.  We generate the
16474    DW_AT_const_value attribute only in those cases where the given variable
16475    or parameter does not have a true "location" either in memory or in a
16476    register.  This can happen (for example) when a constant is passed as an
16477    actual argument in a call to an inline function.  (It's possible that
16478    these things can crop up in other ways also.)  Note that one type of
16479    constant value which can be passed into an inlined function is a constant
16480    pointer.  This can happen for example if an actual argument in an inlined
16481    function call evaluates to a compile-time constant address.  */
16482
16483 static bool
16484 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16485                                        enum dwarf_attribute attr)
16486 {
16487   rtx rtl;
16488   dw_loc_list_ref list;
16489   var_loc_list *loc_list;
16490
16491   if (TREE_CODE (decl) == ERROR_MARK)
16492     return false;
16493
16494   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16495               || TREE_CODE (decl) == RESULT_DECL);
16496
16497   /* Try to get some constant RTL for this decl, and use that as the value of
16498      the location.  */
16499
16500   rtl = rtl_for_decl_location (decl);
16501   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16502       && add_const_value_attribute (die, rtl))
16503     return true;
16504
16505   /* See if we have single element location list that is equivalent to
16506      a constant value.  That way we are better to use add_const_value_attribute
16507      rather than expanding constant value equivalent.  */
16508   loc_list = lookup_decl_loc (decl);
16509   if (loc_list
16510       && loc_list->first
16511       && loc_list->first->next == NULL
16512       && NOTE_P (loc_list->first->loc)
16513       && NOTE_VAR_LOCATION (loc_list->first->loc)
16514       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16515     {
16516       struct var_loc_node *node;
16517
16518       node = loc_list->first;
16519       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16520       if (GET_CODE (rtl) == EXPR_LIST)
16521         rtl = XEXP (rtl, 0);
16522       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16523           && add_const_value_attribute (die, rtl))
16524          return true;
16525     }
16526   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16527   if (list)
16528     {
16529       add_AT_location_description (die, attr, list);
16530       return true;
16531     }
16532   /* None of that worked, so it must not really have a location;
16533      try adding a constant value attribute from the DECL_INITIAL.  */
16534   return tree_add_const_value_attribute_for_decl (die, decl);
16535 }
16536
16537 /* Add VARIABLE and DIE into deferred locations list.  */
16538
16539 static void
16540 defer_location (tree variable, dw_die_ref die)
16541 {
16542   deferred_locations entry;
16543   entry.variable = variable;
16544   entry.die = die;
16545   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16546 }
16547
16548 /* Helper function for tree_add_const_value_attribute.  Natively encode
16549    initializer INIT into an array.  Return true if successful.  */
16550
16551 static bool
16552 native_encode_initializer (tree init, unsigned char *array, int size)
16553 {
16554   tree type;
16555
16556   if (init == NULL_TREE)
16557     return false;
16558
16559   STRIP_NOPS (init);
16560   switch (TREE_CODE (init))
16561     {
16562     case STRING_CST:
16563       type = TREE_TYPE (init);
16564       if (TREE_CODE (type) == ARRAY_TYPE)
16565         {
16566           tree enttype = TREE_TYPE (type);
16567           enum machine_mode mode = TYPE_MODE (enttype);
16568
16569           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16570             return false;
16571           if (int_size_in_bytes (type) != size)
16572             return false;
16573           if (size > TREE_STRING_LENGTH (init))
16574             {
16575               memcpy (array, TREE_STRING_POINTER (init),
16576                       TREE_STRING_LENGTH (init));
16577               memset (array + TREE_STRING_LENGTH (init),
16578                       '\0', size - TREE_STRING_LENGTH (init));
16579             }
16580           else
16581             memcpy (array, TREE_STRING_POINTER (init), size);
16582           return true;
16583         }
16584       return false;
16585     case CONSTRUCTOR:
16586       type = TREE_TYPE (init);
16587       if (int_size_in_bytes (type) != size)
16588         return false;
16589       if (TREE_CODE (type) == ARRAY_TYPE)
16590         {
16591           HOST_WIDE_INT min_index;
16592           unsigned HOST_WIDE_INT cnt;
16593           int curpos = 0, fieldsize;
16594           constructor_elt *ce;
16595
16596           if (TYPE_DOMAIN (type) == NULL_TREE
16597               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16598             return false;
16599
16600           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16601           if (fieldsize <= 0)
16602             return false;
16603
16604           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16605           memset (array, '\0', size);
16606           for (cnt = 0;
16607                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16608                cnt++)
16609             {
16610               tree val = ce->value;
16611               tree index = ce->index;
16612               int pos = curpos;
16613               if (index && TREE_CODE (index) == RANGE_EXPR)
16614                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16615                       * fieldsize;
16616               else if (index)
16617                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16618
16619               if (val)
16620                 {
16621                   STRIP_NOPS (val);
16622                   if (!native_encode_initializer (val, array + pos, fieldsize))
16623                     return false;
16624                 }
16625               curpos = pos + fieldsize;
16626               if (index && TREE_CODE (index) == RANGE_EXPR)
16627                 {
16628                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16629                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16630                   while (count > 0)
16631                     {
16632                       if (val)
16633                         memcpy (array + curpos, array + pos, fieldsize);
16634                       curpos += fieldsize;
16635                     }
16636                 }
16637               gcc_assert (curpos <= size);
16638             }
16639           return true;
16640         }
16641       else if (TREE_CODE (type) == RECORD_TYPE
16642                || TREE_CODE (type) == UNION_TYPE)
16643         {
16644           tree field = NULL_TREE;
16645           unsigned HOST_WIDE_INT cnt;
16646           constructor_elt *ce;
16647
16648           if (int_size_in_bytes (type) != size)
16649             return false;
16650
16651           if (TREE_CODE (type) == RECORD_TYPE)
16652             field = TYPE_FIELDS (type);
16653
16654           for (cnt = 0;
16655                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16656                cnt++, field = field ? TREE_CHAIN (field) : 0)
16657             {
16658               tree val = ce->value;
16659               int pos, fieldsize;
16660
16661               if (ce->index != 0)
16662                 field = ce->index;
16663
16664               if (val)
16665                 STRIP_NOPS (val);
16666
16667               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16668                 return false;
16669
16670               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16671                   && TYPE_DOMAIN (TREE_TYPE (field))
16672                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16673                 return false;
16674               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16675                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16676                 return false;
16677               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16678               pos = int_byte_position (field);
16679               gcc_assert (pos + fieldsize <= size);
16680               if (val
16681                   && !native_encode_initializer (val, array + pos, fieldsize))
16682                 return false;
16683             }
16684           return true;
16685         }
16686       return false;
16687     case VIEW_CONVERT_EXPR:
16688     case NON_LVALUE_EXPR:
16689       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16690     default:
16691       return native_encode_expr (init, array, size) == size;
16692     }
16693 }
16694
16695 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16696    attribute is the const value T.  */
16697
16698 static bool
16699 tree_add_const_value_attribute (dw_die_ref die, tree t)
16700 {
16701   tree init;
16702   tree type = TREE_TYPE (t);
16703   rtx rtl;
16704
16705   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16706     return false;
16707
16708   init = t;
16709   gcc_assert (!DECL_P (init));
16710
16711   rtl = rtl_for_decl_init (init, type);
16712   if (rtl)
16713     return add_const_value_attribute (die, rtl);
16714   /* If the host and target are sane, try harder.  */
16715   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16716            && initializer_constant_valid_p (init, type))
16717     {
16718       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16719       if (size > 0 && (int) size == size)
16720         {
16721           unsigned char *array = (unsigned char *)
16722             ggc_alloc_cleared_atomic (size);
16723
16724           if (native_encode_initializer (init, array, size))
16725             {
16726               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16727               return true;
16728             }
16729         }
16730     }
16731   return false;
16732 }
16733
16734 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16735    attribute is the const value of T, where T is an integral constant
16736    variable with static storage duration
16737    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16738
16739 static bool
16740 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16741 {
16742
16743   if (!decl
16744       || (TREE_CODE (decl) != VAR_DECL
16745           && TREE_CODE (decl) != CONST_DECL))
16746     return false;
16747
16748     if (TREE_READONLY (decl)
16749         && ! TREE_THIS_VOLATILE (decl)
16750         && DECL_INITIAL (decl))
16751       /* OK */;
16752     else
16753       return false;
16754
16755   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16756   if (get_AT (var_die, DW_AT_const_value))
16757     return false;
16758
16759   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16760 }
16761
16762 /* Convert the CFI instructions for the current function into a
16763    location list.  This is used for DW_AT_frame_base when we targeting
16764    a dwarf2 consumer that does not support the dwarf3
16765    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16766    expressions.  */
16767
16768 static dw_loc_list_ref
16769 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16770 {
16771   dw_fde_ref fde;
16772   dw_loc_list_ref list, *list_tail;
16773   dw_cfi_ref cfi;
16774   dw_cfa_location last_cfa, next_cfa;
16775   const char *start_label, *last_label, *section;
16776   dw_cfa_location remember;
16777
16778   fde = current_fde ();
16779   gcc_assert (fde != NULL);
16780
16781   section = secname_for_decl (current_function_decl);
16782   list_tail = &list;
16783   list = NULL;
16784
16785   memset (&next_cfa, 0, sizeof (next_cfa));
16786   next_cfa.reg = INVALID_REGNUM;
16787   remember = next_cfa;
16788
16789   start_label = fde->dw_fde_begin;
16790
16791   /* ??? Bald assumption that the CIE opcode list does not contain
16792      advance opcodes.  */
16793   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16794     lookup_cfa_1 (cfi, &next_cfa, &remember);
16795
16796   last_cfa = next_cfa;
16797   last_label = start_label;
16798
16799   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16800     switch (cfi->dw_cfi_opc)
16801       {
16802       case DW_CFA_set_loc:
16803       case DW_CFA_advance_loc1:
16804       case DW_CFA_advance_loc2:
16805       case DW_CFA_advance_loc4:
16806         if (!cfa_equal_p (&last_cfa, &next_cfa))
16807           {
16808             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16809                                        start_label, last_label, section);
16810
16811             list_tail = &(*list_tail)->dw_loc_next;
16812             last_cfa = next_cfa;
16813             start_label = last_label;
16814           }
16815         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16816         break;
16817
16818       case DW_CFA_advance_loc:
16819         /* The encoding is complex enough that we should never emit this.  */
16820         gcc_unreachable ();
16821
16822       default:
16823         lookup_cfa_1 (cfi, &next_cfa, &remember);
16824         break;
16825       }
16826
16827   if (!cfa_equal_p (&last_cfa, &next_cfa))
16828     {
16829       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16830                                  start_label, last_label, section);
16831       list_tail = &(*list_tail)->dw_loc_next;
16832       start_label = last_label;
16833     }
16834
16835   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16836                              start_label, fde->dw_fde_end, section);
16837
16838   if (list && list->dw_loc_next)
16839     gen_llsym (list);
16840
16841   return list;
16842 }
16843
16844 /* Compute a displacement from the "steady-state frame pointer" to the
16845    frame base (often the same as the CFA), and store it in
16846    frame_pointer_fb_offset.  OFFSET is added to the displacement
16847    before the latter is negated.  */
16848
16849 static void
16850 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16851 {
16852   rtx reg, elim;
16853
16854 #ifdef FRAME_POINTER_CFA_OFFSET
16855   reg = frame_pointer_rtx;
16856   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16857 #else
16858   reg = arg_pointer_rtx;
16859   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16860 #endif
16861
16862   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16863   if (GET_CODE (elim) == PLUS)
16864     {
16865       offset += INTVAL (XEXP (elim, 1));
16866       elim = XEXP (elim, 0);
16867     }
16868
16869   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16870                && (elim == hard_frame_pointer_rtx
16871                    || elim == stack_pointer_rtx))
16872               || elim == (frame_pointer_needed
16873                           ? hard_frame_pointer_rtx
16874                           : stack_pointer_rtx));
16875
16876   frame_pointer_fb_offset = -offset;
16877 }
16878
16879 /* Generate a DW_AT_name attribute given some string value to be included as
16880    the value of the attribute.  */
16881
16882 static void
16883 add_name_attribute (dw_die_ref die, const char *name_string)
16884 {
16885   if (name_string != NULL && *name_string != 0)
16886     {
16887       if (demangle_name_func)
16888         name_string = (*demangle_name_func) (name_string);
16889
16890       add_AT_string (die, DW_AT_name, name_string);
16891     }
16892 }
16893
16894 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16895
16896 static void
16897 add_comp_dir_attribute (dw_die_ref die)
16898 {
16899   const char *wd = get_src_pwd ();
16900   char *wd1;
16901
16902   if (wd == NULL)
16903     return;
16904
16905   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16906     {
16907       int wdlen;
16908
16909       wdlen = strlen (wd);
16910       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16911       strcpy (wd1, wd);
16912       wd1 [wdlen] = DIR_SEPARATOR;
16913       wd1 [wdlen + 1] = 0;
16914       wd = wd1;
16915     }
16916
16917     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16918 }
16919
16920 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16921    default.  */
16922
16923 static int
16924 lower_bound_default (void)
16925 {
16926   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
16927     {
16928     case DW_LANG_C:
16929     case DW_LANG_C89:
16930     case DW_LANG_C99:
16931     case DW_LANG_C_plus_plus:
16932     case DW_LANG_ObjC:
16933     case DW_LANG_ObjC_plus_plus:
16934     case DW_LANG_Java:
16935       return 0;
16936     case DW_LANG_Fortran77:
16937     case DW_LANG_Fortran90:
16938     case DW_LANG_Fortran95:
16939       return 1;
16940     case DW_LANG_UPC:
16941     case DW_LANG_D:
16942     case DW_LANG_Python:
16943       return dwarf_version >= 4 ? 0 : -1;
16944     case DW_LANG_Ada95:
16945     case DW_LANG_Ada83:
16946     case DW_LANG_Cobol74:
16947     case DW_LANG_Cobol85:
16948     case DW_LANG_Pascal83:
16949     case DW_LANG_Modula2:
16950     case DW_LANG_PLI:
16951       return dwarf_version >= 4 ? 1 : -1;
16952     default:
16953       return -1;
16954     }
16955 }
16956
16957 /* Given a tree node describing an array bound (either lower or upper) output
16958    a representation for that bound.  */
16959
16960 static void
16961 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16962 {
16963   switch (TREE_CODE (bound))
16964     {
16965     case ERROR_MARK:
16966       return;
16967
16968     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
16969     case INTEGER_CST:
16970       {
16971         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16972         int dflt;
16973
16974         /* Use the default if possible.  */
16975         if (bound_attr == DW_AT_lower_bound
16976             && host_integerp (bound, 0)
16977             && (dflt = lower_bound_default ()) != -1
16978             && tree_low_cst (bound, 0) == dflt)
16979           ;
16980
16981         /* Otherwise represent the bound as an unsigned value with the
16982            precision of its type.  The precision and signedness of the
16983            type will be necessary to re-interpret it unambiguously.  */
16984         else if (prec < HOST_BITS_PER_WIDE_INT)
16985           {
16986             unsigned HOST_WIDE_INT mask
16987               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16988             add_AT_unsigned (subrange_die, bound_attr,
16989                              TREE_INT_CST_LOW (bound) & mask);
16990           }
16991         else if (prec == HOST_BITS_PER_WIDE_INT
16992                  || TREE_INT_CST_HIGH (bound) == 0)
16993           add_AT_unsigned (subrange_die, bound_attr,
16994                            TREE_INT_CST_LOW (bound));
16995         else
16996           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16997                          TREE_INT_CST_LOW (bound));
16998       }
16999       break;
17000
17001     CASE_CONVERT:
17002     case VIEW_CONVERT_EXPR:
17003       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17004       break;
17005
17006     case SAVE_EXPR:
17007       break;
17008
17009     case VAR_DECL:
17010     case PARM_DECL:
17011     case RESULT_DECL:
17012       {
17013         dw_die_ref decl_die = lookup_decl_die (bound);
17014
17015         /* ??? Can this happen, or should the variable have been bound
17016            first?  Probably it can, since I imagine that we try to create
17017            the types of parameters in the order in which they exist in
17018            the list, and won't have created a forward reference to a
17019            later parameter.  */
17020         if (decl_die != NULL)
17021           {
17022             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17023             break;
17024           }
17025       }
17026       /* FALLTHRU */
17027
17028     default:
17029       {
17030         /* Otherwise try to create a stack operation procedure to
17031            evaluate the value of the array bound.  */
17032
17033         dw_die_ref ctx, decl_die;
17034         dw_loc_list_ref list;
17035
17036         list = loc_list_from_tree (bound, 2);
17037         if (list == NULL || single_element_loc_list_p (list))
17038           {
17039             /* If DW_AT_*bound is not a reference nor constant, it is
17040                a DWARF expression rather than location description.
17041                For that loc_list_from_tree (bound, 0) is needed.
17042                If that fails to give a single element list,
17043                fall back to outputting this as a reference anyway.  */
17044             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17045             if (list2 && single_element_loc_list_p (list2))
17046               {
17047                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17048                 break;
17049               }
17050           }
17051         if (list == NULL)
17052           break;
17053
17054         if (current_function_decl == 0)
17055           ctx = comp_unit_die;
17056         else
17057           ctx = lookup_decl_die (current_function_decl);
17058
17059         decl_die = new_die (DW_TAG_variable, ctx, bound);
17060         add_AT_flag (decl_die, DW_AT_artificial, 1);
17061         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17062         add_AT_location_description (decl_die, DW_AT_location, list);
17063         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17064         break;
17065       }
17066     }
17067 }
17068
17069 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17070    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17071    Note that the block of subscript information for an array type also
17072    includes information about the element type of the given array type.  */
17073
17074 static void
17075 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17076 {
17077   unsigned dimension_number;
17078   tree lower, upper;
17079   dw_die_ref subrange_die;
17080
17081   for (dimension_number = 0;
17082        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17083        type = TREE_TYPE (type), dimension_number++)
17084     {
17085       tree domain = TYPE_DOMAIN (type);
17086
17087       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17088         break;
17089
17090       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17091          and (in GNU C only) variable bounds.  Handle all three forms
17092          here.  */
17093       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17094       if (domain)
17095         {
17096           /* We have an array type with specified bounds.  */
17097           lower = TYPE_MIN_VALUE (domain);
17098           upper = TYPE_MAX_VALUE (domain);
17099
17100           /* Define the index type.  */
17101           if (TREE_TYPE (domain))
17102             {
17103               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17104                  TREE_TYPE field.  We can't emit debug info for this
17105                  because it is an unnamed integral type.  */
17106               if (TREE_CODE (domain) == INTEGER_TYPE
17107                   && TYPE_NAME (domain) == NULL_TREE
17108                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17109                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17110                 ;
17111               else
17112                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17113                                     type_die);
17114             }
17115
17116           /* ??? If upper is NULL, the array has unspecified length,
17117              but it does have a lower bound.  This happens with Fortran
17118                dimension arr(N:*)
17119              Since the debugger is definitely going to need to know N
17120              to produce useful results, go ahead and output the lower
17121              bound solo, and hope the debugger can cope.  */
17122
17123           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17124           if (upper)
17125             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17126         }
17127
17128       /* Otherwise we have an array type with an unspecified length.  The
17129          DWARF-2 spec does not say how to handle this; let's just leave out the
17130          bounds.  */
17131     }
17132 }
17133
17134 static void
17135 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17136 {
17137   unsigned size;
17138
17139   switch (TREE_CODE (tree_node))
17140     {
17141     case ERROR_MARK:
17142       size = 0;
17143       break;
17144     case ENUMERAL_TYPE:
17145     case RECORD_TYPE:
17146     case UNION_TYPE:
17147     case QUAL_UNION_TYPE:
17148       size = int_size_in_bytes (tree_node);
17149       break;
17150     case FIELD_DECL:
17151       /* For a data member of a struct or union, the DW_AT_byte_size is
17152          generally given as the number of bytes normally allocated for an
17153          object of the *declared* type of the member itself.  This is true
17154          even for bit-fields.  */
17155       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17156       break;
17157     default:
17158       gcc_unreachable ();
17159     }
17160
17161   /* Note that `size' might be -1 when we get to this point.  If it is, that
17162      indicates that the byte size of the entity in question is variable.  We
17163      have no good way of expressing this fact in Dwarf at the present time,
17164      so just let the -1 pass on through.  */
17165   add_AT_unsigned (die, DW_AT_byte_size, size);
17166 }
17167
17168 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17169    which specifies the distance in bits from the highest order bit of the
17170    "containing object" for the bit-field to the highest order bit of the
17171    bit-field itself.
17172
17173    For any given bit-field, the "containing object" is a hypothetical object
17174    (of some integral or enum type) within which the given bit-field lives.  The
17175    type of this hypothetical "containing object" is always the same as the
17176    declared type of the individual bit-field itself.  The determination of the
17177    exact location of the "containing object" for a bit-field is rather
17178    complicated.  It's handled by the `field_byte_offset' function (above).
17179
17180    Note that it is the size (in bytes) of the hypothetical "containing object"
17181    which will be given in the DW_AT_byte_size attribute for this bit-field.
17182    (See `byte_size_attribute' above).  */
17183
17184 static inline void
17185 add_bit_offset_attribute (dw_die_ref die, tree decl)
17186 {
17187   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17188   tree type = DECL_BIT_FIELD_TYPE (decl);
17189   HOST_WIDE_INT bitpos_int;
17190   HOST_WIDE_INT highest_order_object_bit_offset;
17191   HOST_WIDE_INT highest_order_field_bit_offset;
17192   HOST_WIDE_INT unsigned bit_offset;
17193
17194   /* Must be a field and a bit field.  */
17195   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17196
17197   /* We can't yet handle bit-fields whose offsets are variable, so if we
17198      encounter such things, just return without generating any attribute
17199      whatsoever.  Likewise for variable or too large size.  */
17200   if (! host_integerp (bit_position (decl), 0)
17201       || ! host_integerp (DECL_SIZE (decl), 1))
17202     return;
17203
17204   bitpos_int = int_bit_position (decl);
17205
17206   /* Note that the bit offset is always the distance (in bits) from the
17207      highest-order bit of the "containing object" to the highest-order bit of
17208      the bit-field itself.  Since the "high-order end" of any object or field
17209      is different on big-endian and little-endian machines, the computation
17210      below must take account of these differences.  */
17211   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17212   highest_order_field_bit_offset = bitpos_int;
17213
17214   if (! BYTES_BIG_ENDIAN)
17215     {
17216       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17217       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17218     }
17219
17220   bit_offset
17221     = (! BYTES_BIG_ENDIAN
17222        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17223        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17224
17225   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17226 }
17227
17228 /* For a FIELD_DECL node which represents a bit field, output an attribute
17229    which specifies the length in bits of the given field.  */
17230
17231 static inline void
17232 add_bit_size_attribute (dw_die_ref die, tree decl)
17233 {
17234   /* Must be a field and a bit field.  */
17235   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17236               && DECL_BIT_FIELD_TYPE (decl));
17237
17238   if (host_integerp (DECL_SIZE (decl), 1))
17239     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17240 }
17241
17242 /* If the compiled language is ANSI C, then add a 'prototyped'
17243    attribute, if arg types are given for the parameters of a function.  */
17244
17245 static inline void
17246 add_prototyped_attribute (dw_die_ref die, tree func_type)
17247 {
17248   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17249       && TYPE_ARG_TYPES (func_type) != NULL)
17250     add_AT_flag (die, DW_AT_prototyped, 1);
17251 }
17252
17253 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17254    by looking in either the type declaration or object declaration
17255    equate table.  */
17256
17257 static inline dw_die_ref
17258 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17259 {
17260   dw_die_ref origin_die = NULL;
17261
17262   if (TREE_CODE (origin) != FUNCTION_DECL)
17263     {
17264       /* We may have gotten separated from the block for the inlined
17265          function, if we're in an exception handler or some such; make
17266          sure that the abstract function has been written out.
17267
17268          Doing this for nested functions is wrong, however; functions are
17269          distinct units, and our context might not even be inline.  */
17270       tree fn = origin;
17271
17272       if (TYPE_P (fn))
17273         fn = TYPE_STUB_DECL (fn);
17274
17275       fn = decl_function_context (fn);
17276       if (fn)
17277         dwarf2out_abstract_function (fn);
17278     }
17279
17280   if (DECL_P (origin))
17281     origin_die = lookup_decl_die (origin);
17282   else if (TYPE_P (origin))
17283     origin_die = lookup_type_die (origin);
17284
17285   /* XXX: Functions that are never lowered don't always have correct block
17286      trees (in the case of java, they simply have no block tree, in some other
17287      languages).  For these functions, there is nothing we can really do to
17288      output correct debug info for inlined functions in all cases.  Rather
17289      than die, we'll just produce deficient debug info now, in that we will
17290      have variables without a proper abstract origin.  In the future, when all
17291      functions are lowered, we should re-add a gcc_assert (origin_die)
17292      here.  */
17293
17294   if (origin_die)
17295     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17296   return origin_die;
17297 }
17298
17299 /* We do not currently support the pure_virtual attribute.  */
17300
17301 static inline void
17302 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17303 {
17304   if (DECL_VINDEX (func_decl))
17305     {
17306       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17307
17308       if (host_integerp (DECL_VINDEX (func_decl), 0))
17309         add_AT_loc (die, DW_AT_vtable_elem_location,
17310                     new_loc_descr (DW_OP_constu,
17311                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17312                                    0));
17313
17314       /* GNU extension: Record what type this method came from originally.  */
17315       if (debug_info_level > DINFO_LEVEL_TERSE
17316           && DECL_CONTEXT (func_decl))
17317         add_AT_die_ref (die, DW_AT_containing_type,
17318                         lookup_type_die (DECL_CONTEXT (func_decl)));
17319     }
17320 }
17321 \f
17322 /* Add source coordinate attributes for the given decl.  */
17323
17324 static void
17325 add_src_coords_attributes (dw_die_ref die, tree decl)
17326 {
17327   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17328
17329   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17330   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17331 }
17332
17333 /* Add a DW_AT_name attribute and source coordinate attribute for the
17334    given decl, but only if it actually has a name.  */
17335
17336 static void
17337 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17338 {
17339   tree decl_name;
17340
17341   decl_name = DECL_NAME (decl);
17342   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17343     {
17344       const char *name = dwarf2_name (decl, 0);
17345       if (name)
17346         add_name_attribute (die, name);
17347       if (! DECL_ARTIFICIAL (decl))
17348         add_src_coords_attributes (die, decl);
17349
17350       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17351           && TREE_PUBLIC (decl)
17352           && !DECL_ABSTRACT (decl)
17353           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
17354         {
17355           /* Defer until we have an assembler name set.  */
17356           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17357             {
17358               limbo_die_node *asm_name;
17359
17360               asm_name = ggc_alloc_cleared_limbo_die_node ();
17361               asm_name->die = die;
17362               asm_name->created_for = decl;
17363               asm_name->next = deferred_asm_name;
17364               deferred_asm_name = asm_name;
17365             }
17366           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17367             add_AT_string (die, AT_linkage_name,
17368                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17369         }
17370     }
17371
17372 #ifdef VMS_DEBUGGING_INFO
17373   /* Get the function's name, as described by its RTL.  This may be different
17374      from the DECL_NAME name used in the source file.  */
17375   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17376     {
17377       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17378                    XEXP (DECL_RTL (decl), 0));
17379       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17380     }
17381 #endif
17382 }
17383
17384 /* Push a new declaration scope.  */
17385
17386 static void
17387 push_decl_scope (tree scope)
17388 {
17389   VEC_safe_push (tree, gc, decl_scope_table, scope);
17390 }
17391
17392 /* Pop a declaration scope.  */
17393
17394 static inline void
17395 pop_decl_scope (void)
17396 {
17397   VEC_pop (tree, decl_scope_table);
17398 }
17399
17400 /* Return the DIE for the scope that immediately contains this type.
17401    Non-named types get global scope.  Named types nested in other
17402    types get their containing scope if it's open, or global scope
17403    otherwise.  All other types (i.e. function-local named types) get
17404    the current active scope.  */
17405
17406 static dw_die_ref
17407 scope_die_for (tree t, dw_die_ref context_die)
17408 {
17409   dw_die_ref scope_die = NULL;
17410   tree containing_scope;
17411   int i;
17412
17413   /* Non-types always go in the current scope.  */
17414   gcc_assert (TYPE_P (t));
17415
17416   containing_scope = TYPE_CONTEXT (t);
17417
17418   /* Use the containing namespace if it was passed in (for a declaration).  */
17419   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17420     {
17421       if (context_die == lookup_decl_die (containing_scope))
17422         /* OK */;
17423       else
17424         containing_scope = NULL_TREE;
17425     }
17426
17427   /* Ignore function type "scopes" from the C frontend.  They mean that
17428      a tagged type is local to a parmlist of a function declarator, but
17429      that isn't useful to DWARF.  */
17430   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17431     containing_scope = NULL_TREE;
17432
17433   if (containing_scope == NULL_TREE)
17434     scope_die = comp_unit_die;
17435   else if (TYPE_P (containing_scope))
17436     {
17437       /* For types, we can just look up the appropriate DIE.  But
17438          first we check to see if we're in the middle of emitting it
17439          so we know where the new DIE should go.  */
17440       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17441         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17442           break;
17443
17444       if (i < 0)
17445         {
17446           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17447                       || TREE_ASM_WRITTEN (containing_scope));
17448
17449           /* If none of the current dies are suitable, we get file scope.  */
17450           scope_die = comp_unit_die;
17451         }
17452       else
17453         scope_die = lookup_type_die (containing_scope);
17454     }
17455   else
17456     scope_die = context_die;
17457
17458   return scope_die;
17459 }
17460
17461 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17462
17463 static inline int
17464 local_scope_p (dw_die_ref context_die)
17465 {
17466   for (; context_die; context_die = context_die->die_parent)
17467     if (context_die->die_tag == DW_TAG_inlined_subroutine
17468         || context_die->die_tag == DW_TAG_subprogram)
17469       return 1;
17470
17471   return 0;
17472 }
17473
17474 /* Returns nonzero if CONTEXT_DIE is a class.  */
17475
17476 static inline int
17477 class_scope_p (dw_die_ref context_die)
17478 {
17479   return (context_die
17480           && (context_die->die_tag == DW_TAG_structure_type
17481               || context_die->die_tag == DW_TAG_class_type
17482               || context_die->die_tag == DW_TAG_interface_type
17483               || context_die->die_tag == DW_TAG_union_type));
17484 }
17485
17486 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17487    whether or not to treat a DIE in this context as a declaration.  */
17488
17489 static inline int
17490 class_or_namespace_scope_p (dw_die_ref context_die)
17491 {
17492   return (class_scope_p (context_die)
17493           || (context_die && context_die->die_tag == DW_TAG_namespace));
17494 }
17495
17496 /* Many forms of DIEs require a "type description" attribute.  This
17497    routine locates the proper "type descriptor" die for the type given
17498    by 'type', and adds a DW_AT_type attribute below the given die.  */
17499
17500 static void
17501 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17502                     int decl_volatile, dw_die_ref context_die)
17503 {
17504   enum tree_code code  = TREE_CODE (type);
17505   dw_die_ref type_die  = NULL;
17506
17507   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17508      or fixed-point type, use the inner type.  This is because we have no
17509      support for unnamed types in base_type_die.  This can happen if this is
17510      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17511   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17512       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17513     type = TREE_TYPE (type), code = TREE_CODE (type);
17514
17515   if (code == ERROR_MARK
17516       /* Handle a special case.  For functions whose return type is void, we
17517          generate *no* type attribute.  (Note that no object may have type
17518          `void', so this only applies to function return types).  */
17519       || code == VOID_TYPE)
17520     return;
17521
17522   type_die = modified_type_die (type,
17523                                 decl_const || TYPE_READONLY (type),
17524                                 decl_volatile || TYPE_VOLATILE (type),
17525                                 context_die);
17526
17527   if (type_die != NULL)
17528     add_AT_die_ref (object_die, DW_AT_type, type_die);
17529 }
17530
17531 /* Given an object die, add the calling convention attribute for the
17532    function call type.  */
17533 static void
17534 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17535 {
17536   enum dwarf_calling_convention value = DW_CC_normal;
17537
17538   value = ((enum dwarf_calling_convention)
17539            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17540
17541   /* DWARF doesn't provide a way to identify a program's source-level
17542      entry point.  DW_AT_calling_convention attributes are only meant
17543      to describe functions' calling conventions.  However, lacking a
17544      better way to signal the Fortran main program, we use this for the
17545      time being, following existing custom.  */
17546   if (is_fortran ()
17547       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17548     value = DW_CC_program;
17549
17550   /* Only add the attribute if the backend requests it, and
17551      is not DW_CC_normal.  */
17552   if (value && (value != DW_CC_normal))
17553     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17554 }
17555
17556 /* Given a tree pointer to a struct, class, union, or enum type node, return
17557    a pointer to the (string) tag name for the given type, or zero if the type
17558    was declared without a tag.  */
17559
17560 static const char *
17561 type_tag (const_tree type)
17562 {
17563   const char *name = 0;
17564
17565   if (TYPE_NAME (type) != 0)
17566     {
17567       tree t = 0;
17568
17569       /* Find the IDENTIFIER_NODE for the type name.  */
17570       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
17571         t = TYPE_NAME (type);
17572
17573       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17574          a TYPE_DECL node, regardless of whether or not a `typedef' was
17575          involved.  */
17576       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17577                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17578         {
17579           /* We want to be extra verbose.  Don't call dwarf_name if
17580              DECL_NAME isn't set.  The default hook for decl_printable_name
17581              doesn't like that, and in this context it's correct to return
17582              0, instead of "<anonymous>" or the like.  */
17583           if (DECL_NAME (TYPE_NAME (type)))
17584             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17585         }
17586
17587       /* Now get the name as a string, or invent one.  */
17588       if (!name && t != 0)
17589         name = IDENTIFIER_POINTER (t);
17590     }
17591
17592   return (name == 0 || *name == '\0') ? 0 : name;
17593 }
17594
17595 /* Return the type associated with a data member, make a special check
17596    for bit field types.  */
17597
17598 static inline tree
17599 member_declared_type (const_tree member)
17600 {
17601   return (DECL_BIT_FIELD_TYPE (member)
17602           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17603 }
17604
17605 /* Get the decl's label, as described by its RTL. This may be different
17606    from the DECL_NAME name used in the source file.  */
17607
17608 #if 0
17609 static const char *
17610 decl_start_label (tree decl)
17611 {
17612   rtx x;
17613   const char *fnname;
17614
17615   x = DECL_RTL (decl);
17616   gcc_assert (MEM_P (x));
17617
17618   x = XEXP (x, 0);
17619   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17620
17621   fnname = XSTR (x, 0);
17622   return fnname;
17623 }
17624 #endif
17625 \f
17626 /* These routines generate the internal representation of the DIE's for
17627    the compilation unit.  Debugging information is collected by walking
17628    the declaration trees passed in from dwarf2out_decl().  */
17629
17630 static void
17631 gen_array_type_die (tree type, dw_die_ref context_die)
17632 {
17633   dw_die_ref scope_die = scope_die_for (type, context_die);
17634   dw_die_ref array_die;
17635
17636   /* GNU compilers represent multidimensional array types as sequences of one
17637      dimensional array types whose element types are themselves array types.
17638      We sometimes squish that down to a single array_type DIE with multiple
17639      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17640      say that we are allowed to do this kind of compression in C, because
17641      there is no difference between an array of arrays and a multidimensional
17642      array.  We don't do this for Ada to remain as close as possible to the
17643      actual representation, which is especially important against the language
17644      flexibilty wrt arrays of variable size.  */
17645
17646   bool collapse_nested_arrays = !is_ada ();
17647   tree element_type;
17648
17649   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17650      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17651   if (TYPE_STRING_FLAG (type)
17652       && TREE_CODE (type) == ARRAY_TYPE
17653       && is_fortran ()
17654       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17655     {
17656       HOST_WIDE_INT size;
17657
17658       array_die = new_die (DW_TAG_string_type, scope_die, type);
17659       add_name_attribute (array_die, type_tag (type));
17660       equate_type_number_to_die (type, array_die);
17661       size = int_size_in_bytes (type);
17662       if (size >= 0)
17663         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17664       else if (TYPE_DOMAIN (type) != NULL_TREE
17665                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17666                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17667         {
17668           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17669           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17670
17671           size = int_size_in_bytes (TREE_TYPE (szdecl));
17672           if (loc && size > 0)
17673             {
17674               add_AT_location_description (array_die, DW_AT_string_length, loc);
17675               if (size != DWARF2_ADDR_SIZE)
17676                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17677             }
17678         }
17679       return;
17680     }
17681
17682   /* ??? The SGI dwarf reader fails for array of array of enum types
17683      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17684      array type comes before the outer array type.  We thus call gen_type_die
17685      before we new_die and must prevent nested array types collapsing for this
17686      target.  */
17687
17688 #ifdef MIPS_DEBUGGING_INFO
17689   gen_type_die (TREE_TYPE (type), context_die);
17690   collapse_nested_arrays = false;
17691 #endif
17692
17693   array_die = new_die (DW_TAG_array_type, scope_die, type);
17694   add_name_attribute (array_die, type_tag (type));
17695   equate_type_number_to_die (type, array_die);
17696
17697   if (TREE_CODE (type) == VECTOR_TYPE)
17698     {
17699       /* The frontend feeds us a representation for the vector as a struct
17700          containing an array.  Pull out the array type.  */
17701       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17702       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17703     }
17704
17705   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17706   if (is_fortran ()
17707       && TREE_CODE (type) == ARRAY_TYPE
17708       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17709       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17710     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17711
17712 #if 0
17713   /* We default the array ordering.  SDB will probably do
17714      the right things even if DW_AT_ordering is not present.  It's not even
17715      an issue until we start to get into multidimensional arrays anyway.  If
17716      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17717      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17718      and when we find out that we need to put these in, we will only do so
17719      for multidimensional arrays.  */
17720   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17721 #endif
17722
17723 #ifdef MIPS_DEBUGGING_INFO
17724   /* The SGI compilers handle arrays of unknown bound by setting
17725      AT_declaration and not emitting any subrange DIEs.  */
17726   if (! TYPE_DOMAIN (type))
17727     add_AT_flag (array_die, DW_AT_declaration, 1);
17728   else
17729 #endif
17730     add_subscript_info (array_die, type, collapse_nested_arrays);
17731
17732   /* Add representation of the type of the elements of this array type and
17733      emit the corresponding DIE if we haven't done it already.  */
17734   element_type = TREE_TYPE (type);
17735   if (collapse_nested_arrays)
17736     while (TREE_CODE (element_type) == ARRAY_TYPE)
17737       {
17738         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17739           break;
17740         element_type = TREE_TYPE (element_type);
17741       }
17742
17743 #ifndef MIPS_DEBUGGING_INFO
17744   gen_type_die (element_type, context_die);
17745 #endif
17746
17747   add_type_attribute (array_die, element_type, 0, 0, context_die);
17748
17749   if (get_AT (array_die, DW_AT_name))
17750     add_pubtype (type, array_die);
17751 }
17752
17753 static dw_loc_descr_ref
17754 descr_info_loc (tree val, tree base_decl)
17755 {
17756   HOST_WIDE_INT size;
17757   dw_loc_descr_ref loc, loc2;
17758   enum dwarf_location_atom op;
17759
17760   if (val == base_decl)
17761     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17762
17763   switch (TREE_CODE (val))
17764     {
17765     CASE_CONVERT:
17766       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17767     case VAR_DECL:
17768       return loc_descriptor_from_tree (val, 0);
17769     case INTEGER_CST:
17770       if (host_integerp (val, 0))
17771         return int_loc_descriptor (tree_low_cst (val, 0));
17772       break;
17773     case INDIRECT_REF:
17774       size = int_size_in_bytes (TREE_TYPE (val));
17775       if (size < 0)
17776         break;
17777       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17778       if (!loc)
17779         break;
17780       if (size == DWARF2_ADDR_SIZE)
17781         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17782       else
17783         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17784       return loc;
17785     case POINTER_PLUS_EXPR:
17786     case PLUS_EXPR:
17787       if (host_integerp (TREE_OPERAND (val, 1), 1)
17788           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17789              < 16384)
17790         {
17791           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17792           if (!loc)
17793             break;
17794           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17795         }
17796       else
17797         {
17798           op = DW_OP_plus;
17799         do_binop:
17800           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17801           if (!loc)
17802             break;
17803           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17804           if (!loc2)
17805             break;
17806           add_loc_descr (&loc, loc2);
17807           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17808         }
17809       return loc;
17810     case MINUS_EXPR:
17811       op = DW_OP_minus;
17812       goto do_binop;
17813     case MULT_EXPR:
17814       op = DW_OP_mul;
17815       goto do_binop;
17816     case EQ_EXPR:
17817       op = DW_OP_eq;
17818       goto do_binop;
17819     case NE_EXPR:
17820       op = DW_OP_ne;
17821       goto do_binop;
17822     default:
17823       break;
17824     }
17825   return NULL;
17826 }
17827
17828 static void
17829 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17830                       tree val, tree base_decl)
17831 {
17832   dw_loc_descr_ref loc;
17833
17834   if (host_integerp (val, 0))
17835     {
17836       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17837       return;
17838     }
17839
17840   loc = descr_info_loc (val, base_decl);
17841   if (!loc)
17842     return;
17843
17844   add_AT_loc (die, attr, loc);
17845 }
17846
17847 /* This routine generates DIE for array with hidden descriptor, details
17848    are filled into *info by a langhook.  */
17849
17850 static void
17851 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17852                           dw_die_ref context_die)
17853 {
17854   dw_die_ref scope_die = scope_die_for (type, context_die);
17855   dw_die_ref array_die;
17856   int dim;
17857
17858   array_die = new_die (DW_TAG_array_type, scope_die, type);
17859   add_name_attribute (array_die, type_tag (type));
17860   equate_type_number_to_die (type, array_die);
17861
17862   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17863   if (is_fortran ()
17864       && info->ndimensions >= 2)
17865     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17866
17867   if (info->data_location)
17868     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17869                           info->base_decl);
17870   if (info->associated)
17871     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17872                           info->base_decl);
17873   if (info->allocated)
17874     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17875                           info->base_decl);
17876
17877   for (dim = 0; dim < info->ndimensions; dim++)
17878     {
17879       dw_die_ref subrange_die
17880         = new_die (DW_TAG_subrange_type, array_die, NULL);
17881
17882       if (info->dimen[dim].lower_bound)
17883         {
17884           /* If it is the default value, omit it.  */
17885           int dflt;
17886
17887           if (host_integerp (info->dimen[dim].lower_bound, 0)
17888               && (dflt = lower_bound_default ()) != -1
17889               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17890             ;
17891           else
17892             add_descr_info_field (subrange_die, DW_AT_lower_bound,
17893                                   info->dimen[dim].lower_bound,
17894                                   info->base_decl);
17895         }
17896       if (info->dimen[dim].upper_bound)
17897         add_descr_info_field (subrange_die, DW_AT_upper_bound,
17898                               info->dimen[dim].upper_bound,
17899                               info->base_decl);
17900       if (info->dimen[dim].stride)
17901         add_descr_info_field (subrange_die, DW_AT_byte_stride,
17902                               info->dimen[dim].stride,
17903                               info->base_decl);
17904     }
17905
17906   gen_type_die (info->element_type, context_die);
17907   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17908
17909   if (get_AT (array_die, DW_AT_name))
17910     add_pubtype (type, array_die);
17911 }
17912
17913 #if 0
17914 static void
17915 gen_entry_point_die (tree decl, dw_die_ref context_die)
17916 {
17917   tree origin = decl_ultimate_origin (decl);
17918   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17919
17920   if (origin != NULL)
17921     add_abstract_origin_attribute (decl_die, origin);
17922   else
17923     {
17924       add_name_and_src_coords_attributes (decl_die, decl);
17925       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17926                           0, 0, context_die);
17927     }
17928
17929   if (DECL_ABSTRACT (decl))
17930     equate_decl_number_to_die (decl, decl_die);
17931   else
17932     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17933 }
17934 #endif
17935
17936 /* Walk through the list of incomplete types again, trying once more to
17937    emit full debugging info for them.  */
17938
17939 static void
17940 retry_incomplete_types (void)
17941 {
17942   int i;
17943
17944   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
17945     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
17946                                   DINFO_USAGE_DIR_USE))
17947       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
17948 }
17949
17950 /* Determine what tag to use for a record type.  */
17951
17952 static enum dwarf_tag
17953 record_type_tag (tree type)
17954 {
17955   if (! lang_hooks.types.classify_record)
17956     return DW_TAG_structure_type;
17957
17958   switch (lang_hooks.types.classify_record (type))
17959     {
17960     case RECORD_IS_STRUCT:
17961       return DW_TAG_structure_type;
17962
17963     case RECORD_IS_CLASS:
17964       return DW_TAG_class_type;
17965
17966     case RECORD_IS_INTERFACE:
17967       if (dwarf_version >= 3 || !dwarf_strict)
17968         return DW_TAG_interface_type;
17969       return DW_TAG_structure_type;
17970
17971     default:
17972       gcc_unreachable ();
17973     }
17974 }
17975
17976 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17977    include all of the information about the enumeration values also. Each
17978    enumerated type name/value is listed as a child of the enumerated type
17979    DIE.  */
17980
17981 static dw_die_ref
17982 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17983 {
17984   dw_die_ref type_die = lookup_type_die (type);
17985
17986   if (type_die == NULL)
17987     {
17988       type_die = new_die (DW_TAG_enumeration_type,
17989                           scope_die_for (type, context_die), type);
17990       equate_type_number_to_die (type, type_die);
17991       add_name_attribute (type_die, type_tag (type));
17992       if ((dwarf_version >= 4 || !dwarf_strict)
17993           && ENUM_IS_SCOPED (type))
17994         add_AT_flag (type_die, DW_AT_enum_class, 1);
17995     }
17996   else if (! TYPE_SIZE (type))
17997     return type_die;
17998   else
17999     remove_AT (type_die, DW_AT_declaration);
18000
18001   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18002      given enum type is incomplete, do not generate the DW_AT_byte_size
18003      attribute or the DW_AT_element_list attribute.  */
18004   if (TYPE_SIZE (type))
18005     {
18006       tree link;
18007
18008       TREE_ASM_WRITTEN (type) = 1;
18009       add_byte_size_attribute (type_die, type);
18010       if (TYPE_STUB_DECL (type) != NULL_TREE)
18011         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18012
18013       /* If the first reference to this type was as the return type of an
18014          inline function, then it may not have a parent.  Fix this now.  */
18015       if (type_die->die_parent == NULL)
18016         add_child_die (scope_die_for (type, context_die), type_die);
18017
18018       for (link = TYPE_VALUES (type);
18019            link != NULL; link = TREE_CHAIN (link))
18020         {
18021           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18022           tree value = TREE_VALUE (link);
18023
18024           add_name_attribute (enum_die,
18025                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18026
18027           if (TREE_CODE (value) == CONST_DECL)
18028             value = DECL_INITIAL (value);
18029
18030           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18031             /* DWARF2 does not provide a way of indicating whether or
18032                not enumeration constants are signed or unsigned.  GDB
18033                always assumes the values are signed, so we output all
18034                values as if they were signed.  That means that
18035                enumeration constants with very large unsigned values
18036                will appear to have negative values in the debugger.  */
18037             add_AT_int (enum_die, DW_AT_const_value,
18038                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18039         }
18040     }
18041   else
18042     add_AT_flag (type_die, DW_AT_declaration, 1);
18043
18044   if (get_AT (type_die, DW_AT_name))
18045     add_pubtype (type, type_die);
18046
18047   return type_die;
18048 }
18049
18050 /* Generate a DIE to represent either a real live formal parameter decl or to
18051    represent just the type of some formal parameter position in some function
18052    type.
18053
18054    Note that this routine is a bit unusual because its argument may be a
18055    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18056    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18057    node.  If it's the former then this function is being called to output a
18058    DIE to represent a formal parameter object (or some inlining thereof).  If
18059    it's the latter, then this function is only being called to output a
18060    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18061    argument type of some subprogram type.
18062    If EMIT_NAME_P is true, name and source coordinate attributes
18063    are emitted.  */
18064
18065 static dw_die_ref
18066 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18067                           dw_die_ref context_die)
18068 {
18069   tree node_or_origin = node ? node : origin;
18070   tree ultimate_origin;
18071   dw_die_ref parm_die
18072     = new_die (DW_TAG_formal_parameter, context_die, node);
18073
18074   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18075     {
18076     case tcc_declaration:
18077       ultimate_origin = decl_ultimate_origin (node_or_origin);
18078       if (node || ultimate_origin)
18079         origin = ultimate_origin;
18080       if (origin != NULL)
18081         add_abstract_origin_attribute (parm_die, origin);
18082       else if (emit_name_p)
18083         add_name_and_src_coords_attributes (parm_die, node);
18084       if (origin == NULL
18085           || (! DECL_ABSTRACT (node_or_origin)
18086               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18087                                            decl_function_context
18088                                                             (node_or_origin))))
18089         {
18090           tree type = TREE_TYPE (node_or_origin);
18091           if (decl_by_reference_p (node_or_origin))
18092             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18093                                 context_die);
18094           else
18095             add_type_attribute (parm_die, type,
18096                                 TREE_READONLY (node_or_origin),
18097                                 TREE_THIS_VOLATILE (node_or_origin),
18098                                 context_die);
18099         }
18100       if (origin == NULL && DECL_ARTIFICIAL (node))
18101         add_AT_flag (parm_die, DW_AT_artificial, 1);
18102
18103       if (node && node != origin)
18104         equate_decl_number_to_die (node, parm_die);
18105       if (! DECL_ABSTRACT (node_or_origin))
18106         add_location_or_const_value_attribute (parm_die, node_or_origin,
18107                                                DW_AT_location);
18108
18109       break;
18110
18111     case tcc_type:
18112       /* We were called with some kind of a ..._TYPE node.  */
18113       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18114       break;
18115
18116     default:
18117       gcc_unreachable ();
18118     }
18119
18120   return parm_die;
18121 }
18122
18123 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18124    children DW_TAG_formal_parameter DIEs representing the arguments of the
18125    parameter pack.
18126
18127    PARM_PACK must be a function parameter pack.
18128    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18129    must point to the subsequent arguments of the function PACK_ARG belongs to.
18130    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18131    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18132    following the last one for which a DIE was generated.  */
18133
18134 static dw_die_ref
18135 gen_formal_parameter_pack_die  (tree parm_pack,
18136                                 tree pack_arg,
18137                                 dw_die_ref subr_die,
18138                                 tree *next_arg)
18139 {
18140   tree arg;
18141   dw_die_ref parm_pack_die;
18142
18143   gcc_assert (parm_pack
18144               && lang_hooks.function_parameter_pack_p (parm_pack)
18145               && subr_die);
18146
18147   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18148   add_src_coords_attributes (parm_pack_die, parm_pack);
18149
18150   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
18151     {
18152       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18153                                                                  parm_pack))
18154         break;
18155       gen_formal_parameter_die (arg, NULL,
18156                                 false /* Don't emit name attribute.  */,
18157                                 parm_pack_die);
18158     }
18159   if (next_arg)
18160     *next_arg = arg;
18161   return parm_pack_die;
18162 }
18163
18164 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18165    at the end of an (ANSI prototyped) formal parameters list.  */
18166
18167 static void
18168 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18169 {
18170   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18171 }
18172
18173 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18174    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18175    parameters as specified in some function type specification (except for
18176    those which appear as part of a function *definition*).  */
18177
18178 static void
18179 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18180 {
18181   tree link;
18182   tree formal_type = NULL;
18183   tree first_parm_type;
18184   tree arg;
18185
18186   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18187     {
18188       arg = DECL_ARGUMENTS (function_or_method_type);
18189       function_or_method_type = TREE_TYPE (function_or_method_type);
18190     }
18191   else
18192     arg = NULL_TREE;
18193
18194   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18195
18196   /* Make our first pass over the list of formal parameter types and output a
18197      DW_TAG_formal_parameter DIE for each one.  */
18198   for (link = first_parm_type; link; )
18199     {
18200       dw_die_ref parm_die;
18201
18202       formal_type = TREE_VALUE (link);
18203       if (formal_type == void_type_node)
18204         break;
18205
18206       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18207       parm_die = gen_formal_parameter_die (formal_type, NULL,
18208                                            true /* Emit name attribute.  */,
18209                                            context_die);
18210       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
18211            && link == first_parm_type)
18212           || (arg && DECL_ARTIFICIAL (arg)))
18213         add_AT_flag (parm_die, DW_AT_artificial, 1);
18214
18215       link = TREE_CHAIN (link);
18216       if (arg)
18217         arg = TREE_CHAIN (arg);
18218     }
18219
18220   /* If this function type has an ellipsis, add a
18221      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18222   if (formal_type != void_type_node)
18223     gen_unspecified_parameters_die (function_or_method_type, context_die);
18224
18225   /* Make our second (and final) pass over the list of formal parameter types
18226      and output DIEs to represent those types (as necessary).  */
18227   for (link = TYPE_ARG_TYPES (function_or_method_type);
18228        link && TREE_VALUE (link);
18229        link = TREE_CHAIN (link))
18230     gen_type_die (TREE_VALUE (link), context_die);
18231 }
18232
18233 /* We want to generate the DIE for TYPE so that we can generate the
18234    die for MEMBER, which has been defined; we will need to refer back
18235    to the member declaration nested within TYPE.  If we're trying to
18236    generate minimal debug info for TYPE, processing TYPE won't do the
18237    trick; we need to attach the member declaration by hand.  */
18238
18239 static void
18240 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18241 {
18242   gen_type_die (type, context_die);
18243
18244   /* If we're trying to avoid duplicate debug info, we may not have
18245      emitted the member decl for this function.  Emit it now.  */
18246   if (TYPE_STUB_DECL (type)
18247       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18248       && ! lookup_decl_die (member))
18249     {
18250       dw_die_ref type_die;
18251       gcc_assert (!decl_ultimate_origin (member));
18252
18253       push_decl_scope (type);
18254       type_die = lookup_type_die (type);
18255       if (TREE_CODE (member) == FUNCTION_DECL)
18256         gen_subprogram_die (member, type_die);
18257       else if (TREE_CODE (member) == FIELD_DECL)
18258         {
18259           /* Ignore the nameless fields that are used to skip bits but handle
18260              C++ anonymous unions and structs.  */
18261           if (DECL_NAME (member) != NULL_TREE
18262               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18263               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18264             {
18265               gen_type_die (member_declared_type (member), type_die);
18266               gen_field_die (member, type_die);
18267             }
18268         }
18269       else
18270         gen_variable_die (member, NULL_TREE, type_die);
18271
18272       pop_decl_scope ();
18273     }
18274 }
18275
18276 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18277    may later generate inlined and/or out-of-line instances of.  */
18278
18279 static void
18280 dwarf2out_abstract_function (tree decl)
18281 {
18282   dw_die_ref old_die;
18283   tree save_fn;
18284   tree context;
18285   int was_abstract;
18286   htab_t old_decl_loc_table;
18287
18288   /* Make sure we have the actual abstract inline, not a clone.  */
18289   decl = DECL_ORIGIN (decl);
18290
18291   old_die = lookup_decl_die (decl);
18292   if (old_die && get_AT (old_die, DW_AT_inline))
18293     /* We've already generated the abstract instance.  */
18294     return;
18295
18296   /* We can be called while recursively when seeing block defining inlined subroutine
18297      DIE.  Be sure to not clobber the outer location table nor use it or we would
18298      get locations in abstract instantces.  */
18299   old_decl_loc_table = decl_loc_table;
18300   decl_loc_table = NULL;
18301
18302   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18303      we don't get confused by DECL_ABSTRACT.  */
18304   if (debug_info_level > DINFO_LEVEL_TERSE)
18305     {
18306       context = decl_class_context (decl);
18307       if (context)
18308         gen_type_die_for_member
18309           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18310     }
18311
18312   /* Pretend we've just finished compiling this function.  */
18313   save_fn = current_function_decl;
18314   current_function_decl = decl;
18315   push_cfun (DECL_STRUCT_FUNCTION (decl));
18316
18317   was_abstract = DECL_ABSTRACT (decl);
18318   set_decl_abstract_flags (decl, 1);
18319   dwarf2out_decl (decl);
18320   if (! was_abstract)
18321     set_decl_abstract_flags (decl, 0);
18322
18323   current_function_decl = save_fn;
18324   decl_loc_table = old_decl_loc_table;
18325   pop_cfun ();
18326 }
18327
18328 /* Helper function of premark_used_types() which gets called through
18329    htab_traverse.
18330
18331    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18332    marked as unused by prune_unused_types.  */
18333
18334 static int
18335 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18336 {
18337   tree type;
18338   dw_die_ref die;
18339
18340   type = (tree) *slot;
18341   die = lookup_type_die (type);
18342   if (die != NULL)
18343     die->die_perennial_p = 1;
18344   return 1;
18345 }
18346
18347 /* Helper function of premark_types_used_by_global_vars which gets called
18348    through htab_traverse.
18349
18350    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18351    marked as unused by prune_unused_types. The DIE of the type is marked
18352    only if the global variable using the type will actually be emitted.  */
18353
18354 static int
18355 premark_types_used_by_global_vars_helper (void **slot,
18356                                           void *data ATTRIBUTE_UNUSED)
18357 {
18358   struct types_used_by_vars_entry *entry;
18359   dw_die_ref die;
18360
18361   entry = (struct types_used_by_vars_entry *) *slot;
18362   gcc_assert (entry->type != NULL
18363               && entry->var_decl != NULL);
18364   die = lookup_type_die (entry->type);
18365   if (die)
18366     {
18367       /* Ask cgraph if the global variable really is to be emitted.
18368          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18369       struct varpool_node *node = varpool_get_node (entry->var_decl);
18370       if (node && node->needed)
18371         {
18372           die->die_perennial_p = 1;
18373           /* Keep the parent DIEs as well.  */
18374           while ((die = die->die_parent) && die->die_perennial_p == 0)
18375             die->die_perennial_p = 1;
18376         }
18377     }
18378   return 1;
18379 }
18380
18381 /* Mark all members of used_types_hash as perennial.  */
18382
18383 static void
18384 premark_used_types (void)
18385 {
18386   if (cfun && cfun->used_types_hash)
18387     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18388 }
18389
18390 /* Mark all members of types_used_by_vars_entry as perennial.  */
18391
18392 static void
18393 premark_types_used_by_global_vars (void)
18394 {
18395   if (types_used_by_vars_hash)
18396     htab_traverse (types_used_by_vars_hash,
18397                    premark_types_used_by_global_vars_helper, NULL);
18398 }
18399
18400 /* Generate a DIE to represent a declared function (either file-scope or
18401    block-local).  */
18402
18403 static void
18404 gen_subprogram_die (tree decl, dw_die_ref context_die)
18405 {
18406   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18407   tree origin = decl_ultimate_origin (decl);
18408   dw_die_ref subr_die;
18409   tree fn_arg_types;
18410   tree outer_scope;
18411   dw_die_ref old_die = lookup_decl_die (decl);
18412   int declaration = (current_function_decl != decl
18413                      || class_or_namespace_scope_p (context_die));
18414
18415   premark_used_types ();
18416
18417   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18418      started to generate the abstract instance of an inline, decided to output
18419      its containing class, and proceeded to emit the declaration of the inline
18420      from the member list for the class.  If so, DECLARATION takes priority;
18421      we'll get back to the abstract instance when done with the class.  */
18422
18423   /* The class-scope declaration DIE must be the primary DIE.  */
18424   if (origin && declaration && class_or_namespace_scope_p (context_die))
18425     {
18426       origin = NULL;
18427       gcc_assert (!old_die);
18428     }
18429
18430   /* Now that the C++ front end lazily declares artificial member fns, we
18431      might need to retrofit the declaration into its class.  */
18432   if (!declaration && !origin && !old_die
18433       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18434       && !class_or_namespace_scope_p (context_die)
18435       && debug_info_level > DINFO_LEVEL_TERSE)
18436     old_die = force_decl_die (decl);
18437
18438   if (origin != NULL)
18439     {
18440       gcc_assert (!declaration || local_scope_p (context_die));
18441
18442       /* Fixup die_parent for the abstract instance of a nested
18443          inline function.  */
18444       if (old_die && old_die->die_parent == NULL)
18445         add_child_die (context_die, old_die);
18446
18447       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18448       add_abstract_origin_attribute (subr_die, origin);
18449     }
18450   else if (old_die)
18451     {
18452       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18453       struct dwarf_file_data * file_index = lookup_filename (s.file);
18454
18455       if (!get_AT_flag (old_die, DW_AT_declaration)
18456           /* We can have a normal definition following an inline one in the
18457              case of redefinition of GNU C extern inlines.
18458              It seems reasonable to use AT_specification in this case.  */
18459           && !get_AT (old_die, DW_AT_inline))
18460         {
18461           /* Detect and ignore this case, where we are trying to output
18462              something we have already output.  */
18463           return;
18464         }
18465
18466       /* If the definition comes from the same place as the declaration,
18467          maybe use the old DIE.  We always want the DIE for this function
18468          that has the *_pc attributes to be under comp_unit_die so the
18469          debugger can find it.  We also need to do this for abstract
18470          instances of inlines, since the spec requires the out-of-line copy
18471          to have the same parent.  For local class methods, this doesn't
18472          apply; we just use the old DIE.  */
18473       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18474           && (DECL_ARTIFICIAL (decl)
18475               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18476                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18477                       == (unsigned) s.line))))
18478         {
18479           subr_die = old_die;
18480
18481           /* Clear out the declaration attribute and the formal parameters.
18482              Do not remove all children, because it is possible that this
18483              declaration die was forced using force_decl_die(). In such
18484              cases die that forced declaration die (e.g. TAG_imported_module)
18485              is one of the children that we do not want to remove.  */
18486           remove_AT (subr_die, DW_AT_declaration);
18487           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18488         }
18489       else
18490         {
18491           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18492           add_AT_specification (subr_die, old_die);
18493           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18494             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18495           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18496             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18497         }
18498     }
18499   else
18500     {
18501       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18502
18503       if (TREE_PUBLIC (decl))
18504         add_AT_flag (subr_die, DW_AT_external, 1);
18505
18506       add_name_and_src_coords_attributes (subr_die, decl);
18507       if (debug_info_level > DINFO_LEVEL_TERSE)
18508         {
18509           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18510           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18511                               0, 0, context_die);
18512         }
18513
18514       add_pure_or_virtual_attribute (subr_die, decl);
18515       if (DECL_ARTIFICIAL (decl))
18516         add_AT_flag (subr_die, DW_AT_artificial, 1);
18517
18518       if (TREE_PROTECTED (decl))
18519         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
18520       else if (TREE_PRIVATE (decl))
18521         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
18522     }
18523
18524   if (declaration)
18525     {
18526       if (!old_die || !get_AT (old_die, DW_AT_inline))
18527         {
18528           add_AT_flag (subr_die, DW_AT_declaration, 1);
18529
18530           /* If this is an explicit function declaration then generate
18531              a DW_AT_explicit attribute.  */
18532           if (lang_hooks.decls.function_decl_explicit_p (decl)
18533               && (dwarf_version >= 3 || !dwarf_strict))
18534             add_AT_flag (subr_die, DW_AT_explicit, 1);
18535
18536           /* The first time we see a member function, it is in the context of
18537              the class to which it belongs.  We make sure of this by emitting
18538              the class first.  The next time is the definition, which is
18539              handled above.  The two may come from the same source text.
18540
18541              Note that force_decl_die() forces function declaration die. It is
18542              later reused to represent definition.  */
18543           equate_decl_number_to_die (decl, subr_die);
18544         }
18545     }
18546   else if (DECL_ABSTRACT (decl))
18547     {
18548       if (DECL_DECLARED_INLINE_P (decl))
18549         {
18550           if (cgraph_function_possibly_inlined_p (decl))
18551             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18552           else
18553             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18554         }
18555       else
18556         {
18557           if (cgraph_function_possibly_inlined_p (decl))
18558             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18559           else
18560             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18561         }
18562
18563       if (DECL_DECLARED_INLINE_P (decl)
18564           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18565         add_AT_flag (subr_die, DW_AT_artificial, 1);
18566
18567       equate_decl_number_to_die (decl, subr_die);
18568     }
18569   else if (!DECL_EXTERNAL (decl))
18570     {
18571       HOST_WIDE_INT cfa_fb_offset;
18572
18573       if (!old_die || !get_AT (old_die, DW_AT_inline))
18574         equate_decl_number_to_die (decl, subr_die);
18575
18576       if (!flag_reorder_blocks_and_partition)
18577         {
18578           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18579                                        current_function_funcdef_no);
18580           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18581           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18582                                        current_function_funcdef_no);
18583           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18584
18585 #if VMS_DEBUGGING_INFO
18586       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18587          Section 2.3 Prologue and Epilogue Attributes:
18588          When a breakpoint is set on entry to a function, it is generally
18589          desirable for execution to be suspended, not on the very first
18590          instruction of the function, but rather at a point after the
18591          function's frame has been set up, after any language defined local
18592          declaration processing has been completed, and before execution of
18593          the first statement of the function begins. Debuggers generally
18594          cannot properly determine where this point is.  Similarly for a
18595          breakpoint set on exit from a function. The prologue and epilogue
18596          attributes allow a compiler to communicate the location(s) to use.  */
18597
18598       {
18599         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18600
18601         if (fde->dw_fde_vms_end_prologue)
18602           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18603             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18604
18605         if (fde->dw_fde_vms_begin_epilogue)
18606           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18607             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18608       }
18609 #endif
18610
18611           add_pubname (decl, subr_die);
18612           add_arange (decl, subr_die);
18613         }
18614       else
18615         {  /* Do nothing for now; maybe need to duplicate die, one for
18616               hot section and one for cold section, then use the hot/cold
18617               section begin/end labels to generate the aranges...  */
18618           /*
18619             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18620             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18621             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18622             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18623
18624             add_pubname (decl, subr_die);
18625             add_arange (decl, subr_die);
18626             add_arange (decl, subr_die);
18627            */
18628         }
18629
18630 #ifdef MIPS_DEBUGGING_INFO
18631       /* Add a reference to the FDE for this routine.  */
18632       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18633 #endif
18634
18635       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18636
18637       /* We define the "frame base" as the function's CFA.  This is more
18638          convenient for several reasons: (1) It's stable across the prologue
18639          and epilogue, which makes it better than just a frame pointer,
18640          (2) With dwarf3, there exists a one-byte encoding that allows us
18641          to reference the .debug_frame data by proxy, but failing that,
18642          (3) We can at least reuse the code inspection and interpretation
18643          code that determines the CFA position at various points in the
18644          function.  */
18645       if (dwarf_version >= 3)
18646         {
18647           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18648           add_AT_loc (subr_die, DW_AT_frame_base, op);
18649         }
18650       else
18651         {
18652           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18653           if (list->dw_loc_next)
18654             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18655           else
18656             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18657         }
18658
18659       /* Compute a displacement from the "steady-state frame pointer" to
18660          the CFA.  The former is what all stack slots and argument slots
18661          will reference in the rtl; the later is what we've told the
18662          debugger about.  We'll need to adjust all frame_base references
18663          by this displacement.  */
18664       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18665
18666       if (cfun->static_chain_decl)
18667         add_AT_location_description (subr_die, DW_AT_static_link,
18668                  loc_list_from_tree (cfun->static_chain_decl, 2));
18669     }
18670
18671   /* Generate child dies for template paramaters.  */
18672   if (debug_info_level > DINFO_LEVEL_TERSE)
18673     gen_generic_params_dies (decl);
18674
18675   /* Now output descriptions of the arguments for this function. This gets
18676      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18677      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18678      `...' at the end of the formal parameter list.  In order to find out if
18679      there was a trailing ellipsis or not, we must instead look at the type
18680      associated with the FUNCTION_DECL.  This will be a node of type
18681      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18682      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18683      an ellipsis at the end.  */
18684
18685   /* In the case where we are describing a mere function declaration, all we
18686      need to do here (and all we *can* do here) is to describe the *types* of
18687      its formal parameters.  */
18688   if (debug_info_level <= DINFO_LEVEL_TERSE)
18689     ;
18690   else if (declaration)
18691     gen_formal_types_die (decl, subr_die);
18692   else
18693     {
18694       /* Generate DIEs to represent all known formal parameters.  */
18695       tree parm = DECL_ARGUMENTS (decl);
18696       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18697       tree generic_decl_parm = generic_decl
18698                                 ? DECL_ARGUMENTS (generic_decl)
18699                                 : NULL;
18700
18701       /* Now we want to walk the list of parameters of the function and
18702          emit their relevant DIEs.
18703
18704          We consider the case of DECL being an instance of a generic function
18705          as well as it being a normal function.
18706
18707          If DECL is an instance of a generic function we walk the
18708          parameters of the generic function declaration _and_ the parameters of
18709          DECL itself. This is useful because we want to emit specific DIEs for
18710          function parameter packs and those are declared as part of the
18711          generic function declaration. In that particular case,
18712          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18713          That DIE has children DIEs representing the set of arguments
18714          of the pack. Note that the set of pack arguments can be empty.
18715          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18716          children DIE.
18717
18718          Otherwise, we just consider the parameters of DECL.  */
18719       while (generic_decl_parm || parm)
18720         {
18721           if (generic_decl_parm
18722               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18723             gen_formal_parameter_pack_die (generic_decl_parm,
18724                                            parm, subr_die,
18725                                            &parm);
18726           else if (parm)
18727             {
18728               gen_decl_die (parm, NULL, subr_die);
18729               parm = TREE_CHAIN (parm);
18730             }
18731
18732           if (generic_decl_parm)
18733             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
18734         }
18735
18736       /* Decide whether we need an unspecified_parameters DIE at the end.
18737          There are 2 more cases to do this for: 1) the ansi ... declaration -
18738          this is detectable when the end of the arg list is not a
18739          void_type_node 2) an unprototyped function declaration (not a
18740          definition).  This just means that we have no info about the
18741          parameters at all.  */
18742       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18743       if (fn_arg_types != NULL)
18744         {
18745           /* This is the prototyped case, check for....  */
18746           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18747             gen_unspecified_parameters_die (decl, subr_die);
18748         }
18749       else if (DECL_INITIAL (decl) == NULL_TREE)
18750         gen_unspecified_parameters_die (decl, subr_die);
18751     }
18752
18753   /* Output Dwarf info for all of the stuff within the body of the function
18754      (if it has one - it may be just a declaration).  */
18755   outer_scope = DECL_INITIAL (decl);
18756
18757   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18758      a function.  This BLOCK actually represents the outermost binding contour
18759      for the function, i.e. the contour in which the function's formal
18760      parameters and labels get declared. Curiously, it appears that the front
18761      end doesn't actually put the PARM_DECL nodes for the current function onto
18762      the BLOCK_VARS list for this outer scope, but are strung off of the
18763      DECL_ARGUMENTS list for the function instead.
18764
18765      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18766      the LABEL_DECL nodes for the function however, and we output DWARF info
18767      for those in decls_for_scope.  Just within the `outer_scope' there will be
18768      a BLOCK node representing the function's outermost pair of curly braces,
18769      and any blocks used for the base and member initializers of a C++
18770      constructor function.  */
18771   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18772     {
18773       /* Emit a DW_TAG_variable DIE for a named return value.  */
18774       if (DECL_NAME (DECL_RESULT (decl)))
18775         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18776
18777       current_function_has_inlines = 0;
18778       decls_for_scope (outer_scope, subr_die, 0);
18779
18780 #if 0 && defined (MIPS_DEBUGGING_INFO)
18781       if (current_function_has_inlines)
18782         {
18783           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18784           if (! comp_unit_has_inlines)
18785             {
18786               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18787               comp_unit_has_inlines = 1;
18788             }
18789         }
18790 #endif
18791     }
18792   /* Add the calling convention attribute if requested.  */
18793   add_calling_convention_attribute (subr_die, decl);
18794
18795 }
18796
18797 /* Returns a hash value for X (which really is a die_struct).  */
18798
18799 static hashval_t
18800 common_block_die_table_hash (const void *x)
18801 {
18802   const_dw_die_ref d = (const_dw_die_ref) x;
18803   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18804 }
18805
18806 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18807    as decl_id and die_parent of die_struct Y.  */
18808
18809 static int
18810 common_block_die_table_eq (const void *x, const void *y)
18811 {
18812   const_dw_die_ref d = (const_dw_die_ref) x;
18813   const_dw_die_ref e = (const_dw_die_ref) y;
18814   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18815 }
18816
18817 /* Generate a DIE to represent a declared data object.
18818    Either DECL or ORIGIN must be non-null.  */
18819
18820 static void
18821 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18822 {
18823   HOST_WIDE_INT off;
18824   tree com_decl;
18825   tree decl_or_origin = decl ? decl : origin;
18826   tree ultimate_origin;
18827   dw_die_ref var_die;
18828   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18829   dw_die_ref origin_die;
18830   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18831                       || class_or_namespace_scope_p (context_die));
18832   bool specialization_p = false;
18833
18834   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18835   if (decl || ultimate_origin)
18836     origin = ultimate_origin;
18837   com_decl = fortran_common (decl_or_origin, &off);
18838
18839   /* Symbol in common gets emitted as a child of the common block, in the form
18840      of a data member.  */
18841   if (com_decl)
18842     {
18843       dw_die_ref com_die;
18844       dw_loc_list_ref loc;
18845       die_node com_die_arg;
18846
18847       var_die = lookup_decl_die (decl_or_origin);
18848       if (var_die)
18849         {
18850           if (get_AT (var_die, DW_AT_location) == NULL)
18851             {
18852               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18853               if (loc)
18854                 {
18855                   if (off)
18856                     {
18857                       /* Optimize the common case.  */
18858                       if (single_element_loc_list_p (loc)
18859                           && loc->expr->dw_loc_opc == DW_OP_addr
18860                           && loc->expr->dw_loc_next == NULL
18861                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18862                              == SYMBOL_REF)
18863                         loc->expr->dw_loc_oprnd1.v.val_addr
18864                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18865                         else
18866                           loc_list_plus_const (loc, off);
18867                     }
18868                   add_AT_location_description (var_die, DW_AT_location, loc);
18869                   remove_AT (var_die, DW_AT_declaration);
18870                 }
18871             }
18872           return;
18873         }
18874
18875       if (common_block_die_table == NULL)
18876         common_block_die_table
18877           = htab_create_ggc (10, common_block_die_table_hash,
18878                              common_block_die_table_eq, NULL);
18879
18880       com_die_arg.decl_id = DECL_UID (com_decl);
18881       com_die_arg.die_parent = context_die;
18882       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18883       loc = loc_list_from_tree (com_decl, 2);
18884       if (com_die == NULL)
18885         {
18886           const char *cnam
18887             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18888           void **slot;
18889
18890           com_die = new_die (DW_TAG_common_block, context_die, decl);
18891           add_name_and_src_coords_attributes (com_die, com_decl);
18892           if (loc)
18893             {
18894               add_AT_location_description (com_die, DW_AT_location, loc);
18895               /* Avoid sharing the same loc descriptor between
18896                  DW_TAG_common_block and DW_TAG_variable.  */
18897               loc = loc_list_from_tree (com_decl, 2);
18898             }
18899           else if (DECL_EXTERNAL (decl))
18900             add_AT_flag (com_die, DW_AT_declaration, 1);
18901           add_pubname_string (cnam, com_die); /* ??? needed? */
18902           com_die->decl_id = DECL_UID (com_decl);
18903           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18904           *slot = (void *) com_die;
18905         }
18906       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18907         {
18908           add_AT_location_description (com_die, DW_AT_location, loc);
18909           loc = loc_list_from_tree (com_decl, 2);
18910           remove_AT (com_die, DW_AT_declaration);
18911         }
18912       var_die = new_die (DW_TAG_variable, com_die, decl);
18913       add_name_and_src_coords_attributes (var_die, decl);
18914       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18915                           TREE_THIS_VOLATILE (decl), context_die);
18916       add_AT_flag (var_die, DW_AT_external, 1);
18917       if (loc)
18918         {
18919           if (off)
18920             {
18921               /* Optimize the common case.  */
18922               if (single_element_loc_list_p (loc)
18923                   && loc->expr->dw_loc_opc == DW_OP_addr
18924                   && loc->expr->dw_loc_next == NULL
18925                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18926                 loc->expr->dw_loc_oprnd1.v.val_addr
18927                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18928               else
18929                 loc_list_plus_const (loc, off);
18930             }
18931           add_AT_location_description (var_die, DW_AT_location, loc);
18932         }
18933       else if (DECL_EXTERNAL (decl))
18934         add_AT_flag (var_die, DW_AT_declaration, 1);
18935       equate_decl_number_to_die (decl, var_die);
18936       return;
18937     }
18938
18939   /* If the compiler emitted a definition for the DECL declaration
18940      and if we already emitted a DIE for it, don't emit a second
18941      DIE for it again. Allow re-declarations of DECLs that are
18942      inside functions, though.  */
18943   if (old_die && declaration && !local_scope_p (context_die))
18944     return;
18945
18946   /* For static data members, the declaration in the class is supposed
18947      to have DW_TAG_member tag; the specification should still be
18948      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18949   if (declaration && class_scope_p (context_die))
18950     var_die = new_die (DW_TAG_member, context_die, decl);
18951   else
18952     var_die = new_die (DW_TAG_variable, context_die, decl);
18953
18954   origin_die = NULL;
18955   if (origin != NULL)
18956     origin_die = add_abstract_origin_attribute (var_die, origin);
18957
18958   /* Loop unrolling can create multiple blocks that refer to the same
18959      static variable, so we must test for the DW_AT_declaration flag.
18960
18961      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18962      copy decls and set the DECL_ABSTRACT flag on them instead of
18963      sharing them.
18964
18965      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18966
18967      ??? The declare_in_namespace support causes us to get two DIEs for one
18968      variable, both of which are declarations.  We want to avoid considering
18969      one to be a specification, so we must test that this DIE is not a
18970      declaration.  */
18971   else if (old_die && TREE_STATIC (decl) && ! declaration
18972            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18973     {
18974       /* This is a definition of a C++ class level static.  */
18975       add_AT_specification (var_die, old_die);
18976       specialization_p = true;
18977       if (DECL_NAME (decl))
18978         {
18979           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18980           struct dwarf_file_data * file_index = lookup_filename (s.file);
18981
18982           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18983             add_AT_file (var_die, DW_AT_decl_file, file_index);
18984
18985           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18986             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18987         }
18988     }
18989   else
18990     add_name_and_src_coords_attributes (var_die, decl);
18991
18992   if ((origin == NULL && !specialization_p)
18993       || (origin != NULL
18994           && !DECL_ABSTRACT (decl_or_origin)
18995           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18996                                        decl_function_context
18997                                                         (decl_or_origin))))
18998     {
18999       tree type = TREE_TYPE (decl_or_origin);
19000
19001       if (decl_by_reference_p (decl_or_origin))
19002         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19003       else
19004         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19005                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19006     }
19007
19008   if (origin == NULL && !specialization_p)
19009     {
19010       if (TREE_PUBLIC (decl))
19011         add_AT_flag (var_die, DW_AT_external, 1);
19012
19013       if (DECL_ARTIFICIAL (decl))
19014         add_AT_flag (var_die, DW_AT_artificial, 1);
19015
19016       if (TREE_PROTECTED (decl))
19017         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
19018       else if (TREE_PRIVATE (decl))
19019         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
19020     }
19021
19022   if (declaration)
19023     add_AT_flag (var_die, DW_AT_declaration, 1);
19024
19025   if (decl && (DECL_ABSTRACT (decl) || declaration))
19026     equate_decl_number_to_die (decl, var_die);
19027
19028   if (! declaration
19029       && (! DECL_ABSTRACT (decl_or_origin)
19030           /* Local static vars are shared between all clones/inlines,
19031              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19032              already set.  */
19033           || (TREE_CODE (decl_or_origin) == VAR_DECL
19034               && TREE_STATIC (decl_or_origin)
19035               && DECL_RTL_SET_P (decl_or_origin)))
19036       /* When abstract origin already has DW_AT_location attribute, no need
19037          to add it again.  */
19038       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19039     {
19040       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19041           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19042         defer_location (decl_or_origin, var_die);
19043       else
19044         add_location_or_const_value_attribute (var_die,
19045                                                decl_or_origin,
19046                                                DW_AT_location);
19047       add_pubname (decl_or_origin, var_die);
19048     }
19049   else
19050     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19051 }
19052
19053 /* Generate a DIE to represent a named constant.  */
19054
19055 static void
19056 gen_const_die (tree decl, dw_die_ref context_die)
19057 {
19058   dw_die_ref const_die;
19059   tree type = TREE_TYPE (decl);
19060
19061   const_die = new_die (DW_TAG_constant, context_die, decl);
19062   add_name_and_src_coords_attributes (const_die, decl);
19063   add_type_attribute (const_die, type, 1, 0, context_die);
19064   if (TREE_PUBLIC (decl))
19065     add_AT_flag (const_die, DW_AT_external, 1);
19066   if (DECL_ARTIFICIAL (decl))
19067     add_AT_flag (const_die, DW_AT_artificial, 1);
19068   tree_add_const_value_attribute_for_decl (const_die, decl);
19069 }
19070
19071 /* Generate a DIE to represent a label identifier.  */
19072
19073 static void
19074 gen_label_die (tree decl, dw_die_ref context_die)
19075 {
19076   tree origin = decl_ultimate_origin (decl);
19077   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19078   rtx insn;
19079   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19080
19081   if (origin != NULL)
19082     add_abstract_origin_attribute (lbl_die, origin);
19083   else
19084     add_name_and_src_coords_attributes (lbl_die, decl);
19085
19086   if (DECL_ABSTRACT (decl))
19087     equate_decl_number_to_die (decl, lbl_die);
19088   else
19089     {
19090       insn = DECL_RTL_IF_SET (decl);
19091
19092       /* Deleted labels are programmer specified labels which have been
19093          eliminated because of various optimizations.  We still emit them
19094          here so that it is possible to put breakpoints on them.  */
19095       if (insn
19096           && (LABEL_P (insn)
19097               || ((NOTE_P (insn)
19098                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19099         {
19100           /* When optimization is enabled (via -O) some parts of the compiler
19101              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19102              represent source-level labels which were explicitly declared by
19103              the user.  This really shouldn't be happening though, so catch
19104              it if it ever does happen.  */
19105           gcc_assert (!INSN_DELETED_P (insn));
19106
19107           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19108           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19109         }
19110     }
19111 }
19112
19113 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19114    attributes to the DIE for a block STMT, to describe where the inlined
19115    function was called from.  This is similar to add_src_coords_attributes.  */
19116
19117 static inline void
19118 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19119 {
19120   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19121
19122   if (dwarf_version >= 3 || !dwarf_strict)
19123     {
19124       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19125       add_AT_unsigned (die, DW_AT_call_line, s.line);
19126     }
19127 }
19128
19129
19130 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19131    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19132
19133 static inline void
19134 add_high_low_attributes (tree stmt, dw_die_ref die)
19135 {
19136   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19137
19138   if (BLOCK_FRAGMENT_CHAIN (stmt)
19139       && (dwarf_version >= 3 || !dwarf_strict))
19140     {
19141       tree chain;
19142
19143       if (inlined_function_outer_scope_p (stmt))
19144         {
19145           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19146                                        BLOCK_NUMBER (stmt));
19147           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19148         }
19149
19150       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19151
19152       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19153       do
19154         {
19155           add_ranges (chain);
19156           chain = BLOCK_FRAGMENT_CHAIN (chain);
19157         }
19158       while (chain);
19159       add_ranges (NULL);
19160     }
19161   else
19162     {
19163       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19164                                    BLOCK_NUMBER (stmt));
19165       add_AT_lbl_id (die, DW_AT_low_pc, label);
19166       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19167                                    BLOCK_NUMBER (stmt));
19168       add_AT_lbl_id (die, DW_AT_high_pc, label);
19169     }
19170 }
19171
19172 /* Generate a DIE for a lexical block.  */
19173
19174 static void
19175 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19176 {
19177   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19178
19179   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19180     add_high_low_attributes (stmt, stmt_die);
19181
19182   decls_for_scope (stmt, stmt_die, depth);
19183 }
19184
19185 /* Generate a DIE for an inlined subprogram.  */
19186
19187 static void
19188 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19189 {
19190   tree decl;
19191
19192   /* The instance of function that is effectively being inlined shall not
19193      be abstract.  */
19194   gcc_assert (! BLOCK_ABSTRACT (stmt));
19195
19196   decl = block_ultimate_origin (stmt);
19197
19198   /* Emit info for the abstract instance first, if we haven't yet.  We
19199      must emit this even if the block is abstract, otherwise when we
19200      emit the block below (or elsewhere), we may end up trying to emit
19201      a die whose origin die hasn't been emitted, and crashing.  */
19202   dwarf2out_abstract_function (decl);
19203
19204   if (! BLOCK_ABSTRACT (stmt))
19205     {
19206       dw_die_ref subr_die
19207         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19208
19209       add_abstract_origin_attribute (subr_die, decl);
19210       if (TREE_ASM_WRITTEN (stmt))
19211         add_high_low_attributes (stmt, subr_die);
19212       add_call_src_coords_attributes (stmt, subr_die);
19213
19214       decls_for_scope (stmt, subr_die, depth);
19215       current_function_has_inlines = 1;
19216     }
19217 }
19218
19219 /* Generate a DIE for a field in a record, or structure.  */
19220
19221 static void
19222 gen_field_die (tree decl, dw_die_ref context_die)
19223 {
19224   dw_die_ref decl_die;
19225
19226   if (TREE_TYPE (decl) == error_mark_node)
19227     return;
19228
19229   decl_die = new_die (DW_TAG_member, context_die, decl);
19230   add_name_and_src_coords_attributes (decl_die, decl);
19231   add_type_attribute (decl_die, member_declared_type (decl),
19232                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19233                       context_die);
19234
19235   if (DECL_BIT_FIELD_TYPE (decl))
19236     {
19237       add_byte_size_attribute (decl_die, decl);
19238       add_bit_size_attribute (decl_die, decl);
19239       add_bit_offset_attribute (decl_die, decl);
19240     }
19241
19242   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19243     add_data_member_location_attribute (decl_die, decl);
19244
19245   if (DECL_ARTIFICIAL (decl))
19246     add_AT_flag (decl_die, DW_AT_artificial, 1);
19247
19248   if (TREE_PROTECTED (decl))
19249     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
19250   else if (TREE_PRIVATE (decl))
19251     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
19252
19253   /* Equate decl number to die, so that we can look up this decl later on.  */
19254   equate_decl_number_to_die (decl, decl_die);
19255 }
19256
19257 #if 0
19258 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19259    Use modified_type_die instead.
19260    We keep this code here just in case these types of DIEs may be needed to
19261    represent certain things in other languages (e.g. Pascal) someday.  */
19262
19263 static void
19264 gen_pointer_type_die (tree type, dw_die_ref context_die)
19265 {
19266   dw_die_ref ptr_die
19267     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19268
19269   equate_type_number_to_die (type, ptr_die);
19270   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19271   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19272 }
19273
19274 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19275    Use modified_type_die instead.
19276    We keep this code here just in case these types of DIEs may be needed to
19277    represent certain things in other languages (e.g. Pascal) someday.  */
19278
19279 static void
19280 gen_reference_type_die (tree type, dw_die_ref context_die)
19281 {
19282   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19283
19284   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19285     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19286   else
19287     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19288
19289   equate_type_number_to_die (type, ref_die);
19290   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19291   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19292 }
19293 #endif
19294
19295 /* Generate a DIE for a pointer to a member type.  */
19296
19297 static void
19298 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19299 {
19300   dw_die_ref ptr_die
19301     = new_die (DW_TAG_ptr_to_member_type,
19302                scope_die_for (type, context_die), type);
19303
19304   equate_type_number_to_die (type, ptr_die);
19305   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19306                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19307   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19308 }
19309
19310 /* Generate the DIE for the compilation unit.  */
19311
19312 static dw_die_ref
19313 gen_compile_unit_die (const char *filename)
19314 {
19315   dw_die_ref die;
19316   char producer[250];
19317   const char *language_string = lang_hooks.name;
19318   int language;
19319
19320   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19321
19322   if (filename)
19323     {
19324       add_name_attribute (die, filename);
19325       /* Don't add cwd for <built-in>.  */
19326       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19327         add_comp_dir_attribute (die);
19328     }
19329
19330   sprintf (producer, "%s %s", language_string, version_string);
19331
19332 #ifdef MIPS_DEBUGGING_INFO
19333   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19334      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19335      not appear in the producer string, the debugger reaches the conclusion
19336      that the object file is stripped and has no debugging information.
19337      To get the MIPS/SGI debugger to believe that there is debugging
19338      information in the object file, we add a -g to the producer string.  */
19339   if (debug_info_level > DINFO_LEVEL_TERSE)
19340     strcat (producer, " -g");
19341 #endif
19342
19343   add_AT_string (die, DW_AT_producer, producer);
19344
19345   language = DW_LANG_C89;
19346   if (strcmp (language_string, "GNU C++") == 0)
19347     language = DW_LANG_C_plus_plus;
19348   else if (strcmp (language_string, "GNU F77") == 0)
19349     language = DW_LANG_Fortran77;
19350   else if (strcmp (language_string, "GNU Pascal") == 0)
19351     language = DW_LANG_Pascal83;
19352   else if (dwarf_version >= 3 || !dwarf_strict)
19353     {
19354       if (strcmp (language_string, "GNU Ada") == 0)
19355         language = DW_LANG_Ada95;
19356       else if (strcmp (language_string, "GNU Fortran") == 0)
19357         language = DW_LANG_Fortran95;
19358       else if (strcmp (language_string, "GNU Java") == 0)
19359         language = DW_LANG_Java;
19360       else if (strcmp (language_string, "GNU Objective-C") == 0)
19361         language = DW_LANG_ObjC;
19362       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19363         language = DW_LANG_ObjC_plus_plus;
19364     }
19365
19366   add_AT_unsigned (die, DW_AT_language, language);
19367
19368   switch (language)
19369     {
19370     case DW_LANG_Fortran77:
19371     case DW_LANG_Fortran90:
19372     case DW_LANG_Fortran95:
19373       /* Fortran has case insensitive identifiers and the front-end
19374          lowercases everything.  */
19375       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19376       break;
19377     default:
19378       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19379       break;
19380     }
19381   return die;
19382 }
19383
19384 /* Generate the DIE for a base class.  */
19385
19386 static void
19387 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19388 {
19389   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19390
19391   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19392   add_data_member_location_attribute (die, binfo);
19393
19394   if (BINFO_VIRTUAL_P (binfo))
19395     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19396
19397   if (access == access_public_node)
19398     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19399   else if (access == access_protected_node)
19400     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19401 }
19402
19403 /* Generate a DIE for a class member.  */
19404
19405 static void
19406 gen_member_die (tree type, dw_die_ref context_die)
19407 {
19408   tree member;
19409   tree binfo = TYPE_BINFO (type);
19410   dw_die_ref child;
19411
19412   /* If this is not an incomplete type, output descriptions of each of its
19413      members. Note that as we output the DIEs necessary to represent the
19414      members of this record or union type, we will also be trying to output
19415      DIEs to represent the *types* of those members. However the `type'
19416      function (above) will specifically avoid generating type DIEs for member
19417      types *within* the list of member DIEs for this (containing) type except
19418      for those types (of members) which are explicitly marked as also being
19419      members of this (containing) type themselves.  The g++ front- end can
19420      force any given type to be treated as a member of some other (containing)
19421      type by setting the TYPE_CONTEXT of the given (member) type to point to
19422      the TREE node representing the appropriate (containing) type.  */
19423
19424   /* First output info about the base classes.  */
19425   if (binfo)
19426     {
19427       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19428       int i;
19429       tree base;
19430
19431       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19432         gen_inheritance_die (base,
19433                              (accesses ? VEC_index (tree, accesses, i)
19434                               : access_public_node), context_die);
19435     }
19436
19437   /* Now output info about the data members and type members.  */
19438   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
19439     {
19440       /* If we thought we were generating minimal debug info for TYPE
19441          and then changed our minds, some of the member declarations
19442          may have already been defined.  Don't define them again, but
19443          do put them in the right order.  */
19444
19445       child = lookup_decl_die (member);
19446       if (child)
19447         splice_child_die (context_die, child);
19448       else
19449         gen_decl_die (member, NULL, context_die);
19450     }
19451
19452   /* Now output info about the function members (if any).  */
19453   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
19454     {
19455       /* Don't include clones in the member list.  */
19456       if (DECL_ABSTRACT_ORIGIN (member))
19457         continue;
19458
19459       child = lookup_decl_die (member);
19460       if (child)
19461         splice_child_die (context_die, child);
19462       else
19463         gen_decl_die (member, NULL, context_die);
19464     }
19465 }
19466
19467 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19468    is set, we pretend that the type was never defined, so we only get the
19469    member DIEs needed by later specification DIEs.  */
19470
19471 static void
19472 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19473                                 enum debug_info_usage usage)
19474 {
19475   dw_die_ref type_die = lookup_type_die (type);
19476   dw_die_ref scope_die = 0;
19477   int nested = 0;
19478   int complete = (TYPE_SIZE (type)
19479                   && (! TYPE_STUB_DECL (type)
19480                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19481   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19482   complete = complete && should_emit_struct_debug (type, usage);
19483
19484   if (type_die && ! complete)
19485     return;
19486
19487   if (TYPE_CONTEXT (type) != NULL_TREE
19488       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19489           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19490     nested = 1;
19491
19492   scope_die = scope_die_for (type, context_die);
19493
19494   if (! type_die || (nested && scope_die == comp_unit_die))
19495     /* First occurrence of type or toplevel definition of nested class.  */
19496     {
19497       dw_die_ref old_die = type_die;
19498
19499       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19500                           ? record_type_tag (type) : DW_TAG_union_type,
19501                           scope_die, type);
19502       equate_type_number_to_die (type, type_die);
19503       if (old_die)
19504         add_AT_specification (type_die, old_die);
19505       else
19506         add_name_attribute (type_die, type_tag (type));
19507     }
19508   else
19509     remove_AT (type_die, DW_AT_declaration);
19510
19511   /* Generate child dies for template paramaters.  */
19512   if (debug_info_level > DINFO_LEVEL_TERSE
19513       && COMPLETE_TYPE_P (type))
19514     gen_generic_params_dies (type);
19515
19516   /* If this type has been completed, then give it a byte_size attribute and
19517      then give a list of members.  */
19518   if (complete && !ns_decl)
19519     {
19520       /* Prevent infinite recursion in cases where the type of some member of
19521          this type is expressed in terms of this type itself.  */
19522       TREE_ASM_WRITTEN (type) = 1;
19523       add_byte_size_attribute (type_die, type);
19524       if (TYPE_STUB_DECL (type) != NULL_TREE)
19525         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19526
19527       /* If the first reference to this type was as the return type of an
19528          inline function, then it may not have a parent.  Fix this now.  */
19529       if (type_die->die_parent == NULL)
19530         add_child_die (scope_die, type_die);
19531
19532       push_decl_scope (type);
19533       gen_member_die (type, type_die);
19534       pop_decl_scope ();
19535
19536       /* GNU extension: Record what type our vtable lives in.  */
19537       if (TYPE_VFIELD (type))
19538         {
19539           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19540
19541           gen_type_die (vtype, context_die);
19542           add_AT_die_ref (type_die, DW_AT_containing_type,
19543                           lookup_type_die (vtype));
19544         }
19545     }
19546   else
19547     {
19548       add_AT_flag (type_die, DW_AT_declaration, 1);
19549
19550       /* We don't need to do this for function-local types.  */
19551       if (TYPE_STUB_DECL (type)
19552           && ! decl_function_context (TYPE_STUB_DECL (type)))
19553         VEC_safe_push (tree, gc, incomplete_types, type);
19554     }
19555
19556   if (get_AT (type_die, DW_AT_name))
19557     add_pubtype (type, type_die);
19558 }
19559
19560 /* Generate a DIE for a subroutine _type_.  */
19561
19562 static void
19563 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19564 {
19565   tree return_type = TREE_TYPE (type);
19566   dw_die_ref subr_die
19567     = new_die (DW_TAG_subroutine_type,
19568                scope_die_for (type, context_die), type);
19569
19570   equate_type_number_to_die (type, subr_die);
19571   add_prototyped_attribute (subr_die, type);
19572   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19573   gen_formal_types_die (type, subr_die);
19574
19575   if (get_AT (subr_die, DW_AT_name))
19576     add_pubtype (type, subr_die);
19577 }
19578
19579 /* Generate a DIE for a type definition.  */
19580
19581 static void
19582 gen_typedef_die (tree decl, dw_die_ref context_die)
19583 {
19584   dw_die_ref type_die;
19585   tree origin;
19586
19587   if (TREE_ASM_WRITTEN (decl))
19588     return;
19589
19590   TREE_ASM_WRITTEN (decl) = 1;
19591   type_die = new_die (DW_TAG_typedef, context_die, decl);
19592   origin = decl_ultimate_origin (decl);
19593   if (origin != NULL)
19594     add_abstract_origin_attribute (type_die, origin);
19595   else
19596     {
19597       tree type;
19598
19599       add_name_and_src_coords_attributes (type_die, decl);
19600       if (DECL_ORIGINAL_TYPE (decl))
19601         {
19602           type = DECL_ORIGINAL_TYPE (decl);
19603
19604           gcc_assert (type != TREE_TYPE (decl));
19605           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19606         }
19607       else
19608         {
19609           type = TREE_TYPE (decl);
19610
19611           if (is_naming_typedef_decl (TYPE_NAME (type)))
19612             /* 
19613                Here, we are in the case of decl being a typedef naming
19614                an anonymous type, e.g:
19615                      typedef struct {...} foo;
19616                In that case TREE_TYPE (decl) is not a typedef variant
19617                type and TYPE_NAME of the anonymous type is set to the
19618                TYPE_DECL of the typedef. This construct is emitted by
19619                the C++ FE.
19620
19621                TYPE is the anonymous struct named by the typedef
19622                DECL. As we need the DW_AT_type attribute of the
19623                DW_TAG_typedef to point to the DIE of TYPE, let's
19624                generate that DIE right away. add_type_attribute
19625                called below will then pick (via lookup_type_die) that
19626                anonymous struct DIE.  */
19627             gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19628         }
19629
19630       add_type_attribute (type_die, type, TREE_READONLY (decl),
19631                           TREE_THIS_VOLATILE (decl), context_die);
19632
19633       if (is_naming_typedef_decl (decl))
19634         /* We want that all subsequent calls to lookup_type_die with
19635            TYPE in argument yield the DW_TAG_typedef we have just
19636            created.  */
19637         equate_type_number_to_die (type, type_die);
19638     }
19639
19640   if (DECL_ABSTRACT (decl))
19641     equate_decl_number_to_die (decl, type_die);
19642
19643   if (get_AT (type_die, DW_AT_name))
19644     add_pubtype (decl, type_die);
19645 }
19646
19647 /* Generate a DIE for a struct, class, enum or union type.  */
19648
19649 static void
19650 gen_tagged_type_die (tree type,
19651                      dw_die_ref context_die,
19652                      enum debug_info_usage usage)
19653 {
19654   int need_pop;
19655
19656   if (type == NULL_TREE
19657       || !is_tagged_type (type))
19658     return;
19659
19660   /* If this is a nested type whose containing class hasn't been written
19661      out yet, writing it out will cover this one, too.  This does not apply
19662      to instantiations of member class templates; they need to be added to
19663      the containing class as they are generated.  FIXME: This hurts the
19664      idea of combining type decls from multiple TUs, since we can't predict
19665      what set of template instantiations we'll get.  */
19666   if (TYPE_CONTEXT (type)
19667       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19668       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19669     {
19670       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19671
19672       if (TREE_ASM_WRITTEN (type))
19673         return;
19674
19675       /* If that failed, attach ourselves to the stub.  */
19676       push_decl_scope (TYPE_CONTEXT (type));
19677       context_die = lookup_type_die (TYPE_CONTEXT (type));
19678       need_pop = 1;
19679     }
19680   else if (TYPE_CONTEXT (type) != NULL_TREE
19681            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19682     {
19683       /* If this type is local to a function that hasn't been written
19684          out yet, use a NULL context for now; it will be fixed up in
19685          decls_for_scope.  */
19686       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19687       need_pop = 0;
19688     }
19689   else
19690     {
19691       context_die = declare_in_namespace (type, context_die);
19692       need_pop = 0;
19693     }
19694
19695   if (TREE_CODE (type) == ENUMERAL_TYPE)
19696     {
19697       /* This might have been written out by the call to
19698          declare_in_namespace.  */
19699       if (!TREE_ASM_WRITTEN (type))
19700         gen_enumeration_type_die (type, context_die);
19701     }
19702   else
19703     gen_struct_or_union_type_die (type, context_die, usage);
19704
19705   if (need_pop)
19706     pop_decl_scope ();
19707
19708   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19709      it up if it is ever completed.  gen_*_type_die will set it for us
19710      when appropriate.  */
19711 }
19712
19713 /* Generate a type description DIE.  */
19714
19715 static void
19716 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19717                                 enum debug_info_usage usage)
19718 {
19719   struct array_descr_info info;
19720
19721   if (type == NULL_TREE || type == error_mark_node)
19722     return;
19723
19724   /* If TYPE is a typedef type variant, let's generate debug info
19725      for the parent typedef which TYPE is a type of.  */
19726   if (typedef_variant_p (type))
19727     {
19728       if (TREE_ASM_WRITTEN (type))
19729         return;
19730
19731       /* Prevent broken recursion; we can't hand off to the same type.  */
19732       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19733
19734       /* Use the DIE of the containing namespace as the parent DIE of
19735          the type description DIE we want to generate.  */
19736       if (DECL_CONTEXT (TYPE_NAME (type))
19737           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19738         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19739
19740       TREE_ASM_WRITTEN (type) = 1;
19741
19742       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19743       return;
19744     }
19745
19746   /* If type is an anonymous tagged type named by a typedef, let's
19747      generate debug info for the typedef.  */
19748   if (is_naming_typedef_decl (TYPE_NAME (type)))
19749     {
19750       /* Use the DIE of the containing namespace as the parent DIE of
19751          the type description DIE we want to generate.  */
19752       if (DECL_CONTEXT (TYPE_NAME (type))
19753           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19754         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19755       
19756       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19757       return;
19758     }
19759
19760   /* If this is an array type with hidden descriptor, handle it first.  */
19761   if (!TREE_ASM_WRITTEN (type)
19762       && lang_hooks.types.get_array_descr_info
19763       && lang_hooks.types.get_array_descr_info (type, &info)
19764       && (dwarf_version >= 3 || !dwarf_strict))
19765     {
19766       gen_descr_array_type_die (type, &info, context_die);
19767       TREE_ASM_WRITTEN (type) = 1;
19768       return;
19769     }
19770
19771   /* We are going to output a DIE to represent the unqualified version
19772      of this type (i.e. without any const or volatile qualifiers) so
19773      get the main variant (i.e. the unqualified version) of this type
19774      now.  (Vectors are special because the debugging info is in the
19775      cloned type itself).  */
19776   if (TREE_CODE (type) != VECTOR_TYPE)
19777     type = type_main_variant (type);
19778
19779   if (TREE_ASM_WRITTEN (type))
19780     return;
19781
19782   switch (TREE_CODE (type))
19783     {
19784     case ERROR_MARK:
19785       break;
19786
19787     case POINTER_TYPE:
19788     case REFERENCE_TYPE:
19789       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19790          ensures that the gen_type_die recursion will terminate even if the
19791          type is recursive.  Recursive types are possible in Ada.  */
19792       /* ??? We could perhaps do this for all types before the switch
19793          statement.  */
19794       TREE_ASM_WRITTEN (type) = 1;
19795
19796       /* For these types, all that is required is that we output a DIE (or a
19797          set of DIEs) to represent the "basis" type.  */
19798       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19799                                 DINFO_USAGE_IND_USE);
19800       break;
19801
19802     case OFFSET_TYPE:
19803       /* This code is used for C++ pointer-to-data-member types.
19804          Output a description of the relevant class type.  */
19805       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19806                                         DINFO_USAGE_IND_USE);
19807
19808       /* Output a description of the type of the object pointed to.  */
19809       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19810                                         DINFO_USAGE_IND_USE);
19811
19812       /* Now output a DIE to represent this pointer-to-data-member type
19813          itself.  */
19814       gen_ptr_to_mbr_type_die (type, context_die);
19815       break;
19816
19817     case FUNCTION_TYPE:
19818       /* Force out return type (in case it wasn't forced out already).  */
19819       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19820                                         DINFO_USAGE_DIR_USE);
19821       gen_subroutine_type_die (type, context_die);
19822       break;
19823
19824     case METHOD_TYPE:
19825       /* Force out return type (in case it wasn't forced out already).  */
19826       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19827                                         DINFO_USAGE_DIR_USE);
19828       gen_subroutine_type_die (type, context_die);
19829       break;
19830
19831     case ARRAY_TYPE:
19832       gen_array_type_die (type, context_die);
19833       break;
19834
19835     case VECTOR_TYPE:
19836       gen_array_type_die (type, context_die);
19837       break;
19838
19839     case ENUMERAL_TYPE:
19840     case RECORD_TYPE:
19841     case UNION_TYPE:
19842     case QUAL_UNION_TYPE:
19843       gen_tagged_type_die (type, context_die, usage);
19844       return;
19845
19846     case VOID_TYPE:
19847     case INTEGER_TYPE:
19848     case REAL_TYPE:
19849     case FIXED_POINT_TYPE:
19850     case COMPLEX_TYPE:
19851     case BOOLEAN_TYPE:
19852       /* No DIEs needed for fundamental types.  */
19853       break;
19854
19855     case LANG_TYPE:
19856       /* Just use DW_TAG_unspecified_type.  */
19857       {
19858         dw_die_ref type_die = lookup_type_die (type);
19859         if (type_die == NULL)
19860           {
19861             tree name = TYPE_NAME (type);
19862             if (TREE_CODE (name) == TYPE_DECL)
19863               name = DECL_NAME (name);
19864             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
19865             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19866             equate_type_number_to_die (type, type_die);
19867           }
19868       }
19869       break;
19870
19871     default:
19872       gcc_unreachable ();
19873     }
19874
19875   TREE_ASM_WRITTEN (type) = 1;
19876 }
19877
19878 static void
19879 gen_type_die (tree type, dw_die_ref context_die)
19880 {
19881   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19882 }
19883
19884 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19885    things which are local to the given block.  */
19886
19887 static void
19888 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19889 {
19890   int must_output_die = 0;
19891   bool inlined_func;
19892
19893   /* Ignore blocks that are NULL.  */
19894   if (stmt == NULL_TREE)
19895     return;
19896
19897   inlined_func = inlined_function_outer_scope_p (stmt);
19898
19899   /* If the block is one fragment of a non-contiguous block, do not
19900      process the variables, since they will have been done by the
19901      origin block.  Do process subblocks.  */
19902   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19903     {
19904       tree sub;
19905
19906       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19907         gen_block_die (sub, context_die, depth + 1);
19908
19909       return;
19910     }
19911
19912   /* Determine if we need to output any Dwarf DIEs at all to represent this
19913      block.  */
19914   if (inlined_func)
19915     /* The outer scopes for inlinings *must* always be represented.  We
19916        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19917     must_output_die = 1;
19918   else
19919     {
19920       /* Determine if this block directly contains any "significant"
19921          local declarations which we will need to output DIEs for.  */
19922       if (debug_info_level > DINFO_LEVEL_TERSE)
19923         /* We are not in terse mode so *any* local declaration counts
19924            as being a "significant" one.  */
19925         must_output_die = ((BLOCK_VARS (stmt) != NULL
19926                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19927                            && (TREE_USED (stmt)
19928                                || TREE_ASM_WRITTEN (stmt)
19929                                || BLOCK_ABSTRACT (stmt)));
19930       else if ((TREE_USED (stmt)
19931                 || TREE_ASM_WRITTEN (stmt)
19932                 || BLOCK_ABSTRACT (stmt))
19933                && !dwarf2out_ignore_block (stmt))
19934         must_output_die = 1;
19935     }
19936
19937   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19938      DIE for any block which contains no significant local declarations at
19939      all.  Rather, in such cases we just call `decls_for_scope' so that any
19940      needed Dwarf info for any sub-blocks will get properly generated. Note
19941      that in terse mode, our definition of what constitutes a "significant"
19942      local declaration gets restricted to include only inlined function
19943      instances and local (nested) function definitions.  */
19944   if (must_output_die)
19945     {
19946       if (inlined_func)
19947         {
19948           /* If STMT block is abstract, that means we have been called
19949              indirectly from dwarf2out_abstract_function.
19950              That function rightfully marks the descendent blocks (of
19951              the abstract function it is dealing with) as being abstract,
19952              precisely to prevent us from emitting any
19953              DW_TAG_inlined_subroutine DIE as a descendent
19954              of an abstract function instance. So in that case, we should
19955              not call gen_inlined_subroutine_die.
19956
19957              Later though, when cgraph asks dwarf2out to emit info
19958              for the concrete instance of the function decl into which
19959              the concrete instance of STMT got inlined, the later will lead
19960              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19961           if (! BLOCK_ABSTRACT (stmt))
19962             gen_inlined_subroutine_die (stmt, context_die, depth);
19963         }
19964       else
19965         gen_lexical_block_die (stmt, context_die, depth);
19966     }
19967   else
19968     decls_for_scope (stmt, context_die, depth);
19969 }
19970
19971 /* Process variable DECL (or variable with origin ORIGIN) within
19972    block STMT and add it to CONTEXT_DIE.  */
19973 static void
19974 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19975 {
19976   dw_die_ref die;
19977   tree decl_or_origin = decl ? decl : origin;
19978
19979   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19980     die = lookup_decl_die (decl_or_origin);
19981   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19982            && TYPE_DECL_IS_STUB (decl_or_origin))
19983     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19984   else
19985     die = NULL;
19986
19987   if (die != NULL && die->die_parent == NULL)
19988     add_child_die (context_die, die);
19989   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19990     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19991                                          stmt, context_die);
19992   else
19993     gen_decl_die (decl, origin, context_die);
19994 }
19995
19996 /* Generate all of the decls declared within a given scope and (recursively)
19997    all of its sub-blocks.  */
19998
19999 static void
20000 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20001 {
20002   tree decl;
20003   unsigned int i;
20004   tree subblocks;
20005
20006   /* Ignore NULL blocks.  */
20007   if (stmt == NULL_TREE)
20008     return;
20009
20010   /* Output the DIEs to represent all of the data objects and typedefs
20011      declared directly within this block but not within any nested
20012      sub-blocks.  Also, nested function and tag DIEs have been
20013      generated with a parent of NULL; fix that up now.  */
20014   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
20015     process_scope_var (stmt, decl, NULL_TREE, context_die);
20016   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20017     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20018                        context_die);
20019
20020   /* If we're at -g1, we're not interested in subblocks.  */
20021   if (debug_info_level <= DINFO_LEVEL_TERSE)
20022     return;
20023
20024   /* Output the DIEs to represent all sub-blocks (and the items declared
20025      therein) of this block.  */
20026   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20027        subblocks != NULL;
20028        subblocks = BLOCK_CHAIN (subblocks))
20029     gen_block_die (subblocks, context_die, depth + 1);
20030 }
20031
20032 /* Is this a typedef we can avoid emitting?  */
20033
20034 static inline int
20035 is_redundant_typedef (const_tree decl)
20036 {
20037   if (TYPE_DECL_IS_STUB (decl))
20038     return 1;
20039
20040   if (DECL_ARTIFICIAL (decl)
20041       && DECL_CONTEXT (decl)
20042       && is_tagged_type (DECL_CONTEXT (decl))
20043       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20044       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20045     /* Also ignore the artificial member typedef for the class name.  */
20046     return 1;
20047
20048   return 0;
20049 }
20050
20051 /* Return TRUE if TYPE is a typedef that names a type for linkage
20052    purposes. This kind of typedefs is produced by the C++ FE for
20053    constructs like:
20054
20055    typedef struct {...} foo;
20056
20057    In that case, there is no typedef variant type produced for foo.
20058    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20059    struct type.  */
20060
20061 static bool
20062 is_naming_typedef_decl (const_tree decl)
20063 {
20064   if (decl == NULL_TREE
20065       || TREE_CODE (decl) != TYPE_DECL
20066       || !is_tagged_type (TREE_TYPE (decl))
20067       || DECL_IS_BUILTIN (decl)
20068       || is_redundant_typedef (decl)
20069       /* It looks like Ada produces TYPE_DECLs that are very similar
20070          to C++ naming typedefs but that have different
20071          semantics. Let's be specific to c++ for now.  */
20072       || !is_cxx ())
20073     return FALSE;
20074
20075   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20076           && TYPE_NAME (TREE_TYPE (decl)) == decl
20077           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20078               != TYPE_NAME (TREE_TYPE (decl))));
20079 }
20080
20081 /* Returns the DIE for a context.  */
20082
20083 static inline dw_die_ref
20084 get_context_die (tree context)
20085 {
20086   if (context)
20087     {
20088       /* Find die that represents this context.  */
20089       if (TYPE_P (context))
20090         return force_type_die (TYPE_MAIN_VARIANT (context));
20091       else
20092         return force_decl_die (context);
20093     }
20094   return comp_unit_die;
20095 }
20096
20097 /* Returns the DIE for decl.  A DIE will always be returned.  */
20098
20099 static dw_die_ref
20100 force_decl_die (tree decl)
20101 {
20102   dw_die_ref decl_die;
20103   unsigned saved_external_flag;
20104   tree save_fn = NULL_TREE;
20105   decl_die = lookup_decl_die (decl);
20106   if (!decl_die)
20107     {
20108       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20109
20110       decl_die = lookup_decl_die (decl);
20111       if (decl_die)
20112         return decl_die;
20113
20114       switch (TREE_CODE (decl))
20115         {
20116         case FUNCTION_DECL:
20117           /* Clear current_function_decl, so that gen_subprogram_die thinks
20118              that this is a declaration. At this point, we just want to force
20119              declaration die.  */
20120           save_fn = current_function_decl;
20121           current_function_decl = NULL_TREE;
20122           gen_subprogram_die (decl, context_die);
20123           current_function_decl = save_fn;
20124           break;
20125
20126         case VAR_DECL:
20127           /* Set external flag to force declaration die. Restore it after
20128            gen_decl_die() call.  */
20129           saved_external_flag = DECL_EXTERNAL (decl);
20130           DECL_EXTERNAL (decl) = 1;
20131           gen_decl_die (decl, NULL, context_die);
20132           DECL_EXTERNAL (decl) = saved_external_flag;
20133           break;
20134
20135         case NAMESPACE_DECL:
20136           if (dwarf_version >= 3 || !dwarf_strict)
20137             dwarf2out_decl (decl);
20138           else
20139             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20140             decl_die = comp_unit_die;
20141           break;
20142
20143         default:
20144           gcc_unreachable ();
20145         }
20146
20147       /* We should be able to find the DIE now.  */
20148       if (!decl_die)
20149         decl_die = lookup_decl_die (decl);
20150       gcc_assert (decl_die);
20151     }
20152
20153   return decl_die;
20154 }
20155
20156 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20157    always returned.  */
20158
20159 static dw_die_ref
20160 force_type_die (tree type)
20161 {
20162   dw_die_ref type_die;
20163
20164   type_die = lookup_type_die (type);
20165   if (!type_die)
20166     {
20167       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20168
20169       type_die = modified_type_die (type, TYPE_READONLY (type),
20170                                     TYPE_VOLATILE (type), context_die);
20171       gcc_assert (type_die);
20172     }
20173   return type_die;
20174 }
20175
20176 /* Force out any required namespaces to be able to output DECL,
20177    and return the new context_die for it, if it's changed.  */
20178
20179 static dw_die_ref
20180 setup_namespace_context (tree thing, dw_die_ref context_die)
20181 {
20182   tree context = (DECL_P (thing)
20183                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20184   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20185     /* Force out the namespace.  */
20186     context_die = force_decl_die (context);
20187
20188   return context_die;
20189 }
20190
20191 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20192    type) within its namespace, if appropriate.
20193
20194    For compatibility with older debuggers, namespace DIEs only contain
20195    declarations; all definitions are emitted at CU scope.  */
20196
20197 static dw_die_ref
20198 declare_in_namespace (tree thing, dw_die_ref context_die)
20199 {
20200   dw_die_ref ns_context;
20201
20202   if (debug_info_level <= DINFO_LEVEL_TERSE)
20203     return context_die;
20204
20205   /* If this decl is from an inlined function, then don't try to emit it in its
20206      namespace, as we will get confused.  It would have already been emitted
20207      when the abstract instance of the inline function was emitted anyways.  */
20208   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20209     return context_die;
20210
20211   ns_context = setup_namespace_context (thing, context_die);
20212
20213   if (ns_context != context_die)
20214     {
20215       if (is_fortran ())
20216         return ns_context;
20217       if (DECL_P (thing))
20218         gen_decl_die (thing, NULL, ns_context);
20219       else
20220         gen_type_die (thing, ns_context);
20221     }
20222   return context_die;
20223 }
20224
20225 /* Generate a DIE for a namespace or namespace alias.  */
20226
20227 static void
20228 gen_namespace_die (tree decl, dw_die_ref context_die)
20229 {
20230   dw_die_ref namespace_die;
20231
20232   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20233      they are an alias of.  */
20234   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20235     {
20236       /* Output a real namespace or module.  */
20237       context_die = setup_namespace_context (decl, comp_unit_die);
20238       namespace_die = new_die (is_fortran ()
20239                                ? DW_TAG_module : DW_TAG_namespace,
20240                                context_die, decl);
20241       /* For Fortran modules defined in different CU don't add src coords.  */
20242       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20243         {
20244           const char *name = dwarf2_name (decl, 0);
20245           if (name)
20246             add_name_attribute (namespace_die, name);
20247         }
20248       else
20249         add_name_and_src_coords_attributes (namespace_die, decl);
20250       if (DECL_EXTERNAL (decl))
20251         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20252       equate_decl_number_to_die (decl, namespace_die);
20253     }
20254   else
20255     {
20256       /* Output a namespace alias.  */
20257
20258       /* Force out the namespace we are an alias of, if necessary.  */
20259       dw_die_ref origin_die
20260         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20261
20262       if (DECL_CONTEXT (decl) == NULL_TREE
20263           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20264         context_die = setup_namespace_context (decl, comp_unit_die);
20265       /* Now create the namespace alias DIE.  */
20266       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20267       add_name_and_src_coords_attributes (namespace_die, decl);
20268       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20269       equate_decl_number_to_die (decl, namespace_die);
20270     }
20271 }
20272
20273 /* Generate Dwarf debug information for a decl described by DECL.  */
20274
20275 static void
20276 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20277 {
20278   tree decl_or_origin = decl ? decl : origin;
20279   tree class_origin = NULL, ultimate_origin;
20280
20281   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20282     return;
20283
20284   switch (TREE_CODE (decl_or_origin))
20285     {
20286     case ERROR_MARK:
20287       break;
20288
20289     case CONST_DECL:
20290       if (!is_fortran ())
20291         {
20292           /* The individual enumerators of an enum type get output when we output
20293              the Dwarf representation of the relevant enum type itself.  */
20294           break;
20295         }
20296
20297       /* Emit its type.  */
20298       gen_type_die (TREE_TYPE (decl), context_die);
20299
20300       /* And its containing namespace.  */
20301       context_die = declare_in_namespace (decl, context_die);
20302
20303       gen_const_die (decl, context_die);
20304       break;
20305
20306     case FUNCTION_DECL:
20307       /* Don't output any DIEs to represent mere function declarations,
20308          unless they are class members or explicit block externs.  */
20309       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20310           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20311           && (current_function_decl == NULL_TREE
20312               || DECL_ARTIFICIAL (decl_or_origin)))
20313         break;
20314
20315 #if 0
20316       /* FIXME */
20317       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20318          on local redeclarations of global functions.  That seems broken.  */
20319       if (current_function_decl != decl)
20320         /* This is only a declaration.  */;
20321 #endif
20322
20323       /* If we're emitting a clone, emit info for the abstract instance.  */
20324       if (origin || DECL_ORIGIN (decl) != decl)
20325         dwarf2out_abstract_function (origin
20326                                      ? DECL_ORIGIN (origin)
20327                                      : DECL_ABSTRACT_ORIGIN (decl));
20328
20329       /* If we're emitting an out-of-line copy of an inline function,
20330          emit info for the abstract instance and set up to refer to it.  */
20331       else if (cgraph_function_possibly_inlined_p (decl)
20332                && ! DECL_ABSTRACT (decl)
20333                && ! class_or_namespace_scope_p (context_die)
20334                /* dwarf2out_abstract_function won't emit a die if this is just
20335                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20336                   that case, because that works only if we have a die.  */
20337                && DECL_INITIAL (decl) != NULL_TREE)
20338         {
20339           dwarf2out_abstract_function (decl);
20340           set_decl_origin_self (decl);
20341         }
20342
20343       /* Otherwise we're emitting the primary DIE for this decl.  */
20344       else if (debug_info_level > DINFO_LEVEL_TERSE)
20345         {
20346           /* Before we describe the FUNCTION_DECL itself, make sure that we
20347              have described its return type.  */
20348           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20349
20350           /* And its virtual context.  */
20351           if (DECL_VINDEX (decl) != NULL_TREE)
20352             gen_type_die (DECL_CONTEXT (decl), context_die);
20353
20354           /* And its containing type.  */
20355           if (!origin)
20356             origin = decl_class_context (decl);
20357           if (origin != NULL_TREE)
20358             gen_type_die_for_member (origin, decl, context_die);
20359
20360           /* And its containing namespace.  */
20361           context_die = declare_in_namespace (decl, context_die);
20362         }
20363
20364       /* Now output a DIE to represent the function itself.  */
20365       if (decl)
20366         gen_subprogram_die (decl, context_die);
20367       break;
20368
20369     case TYPE_DECL:
20370       /* If we are in terse mode, don't generate any DIEs to represent any
20371          actual typedefs.  */
20372       if (debug_info_level <= DINFO_LEVEL_TERSE)
20373         break;
20374
20375       /* In the special case of a TYPE_DECL node representing the declaration
20376          of some type tag, if the given TYPE_DECL is marked as having been
20377          instantiated from some other (original) TYPE_DECL node (e.g. one which
20378          was generated within the original definition of an inline function) we
20379          used to generate a special (abbreviated) DW_TAG_structure_type,
20380          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20381          should be actually referencing those DIEs, as variable DIEs with that
20382          type would be emitted already in the abstract origin, so it was always
20383          removed during unused type prunning.  Don't add anything in this
20384          case.  */
20385       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20386         break;
20387
20388       if (is_redundant_typedef (decl))
20389         gen_type_die (TREE_TYPE (decl), context_die);
20390       else
20391         /* Output a DIE to represent the typedef itself.  */
20392         gen_typedef_die (decl, context_die);
20393       break;
20394
20395     case LABEL_DECL:
20396       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20397         gen_label_die (decl, context_die);
20398       break;
20399
20400     case VAR_DECL:
20401     case RESULT_DECL:
20402       /* If we are in terse mode, don't generate any DIEs to represent any
20403          variable declarations or definitions.  */
20404       if (debug_info_level <= DINFO_LEVEL_TERSE)
20405         break;
20406
20407       /* Output any DIEs that are needed to specify the type of this data
20408          object.  */
20409       if (decl_by_reference_p (decl_or_origin))
20410         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20411       else
20412         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20413
20414       /* And its containing type.  */
20415       class_origin = decl_class_context (decl_or_origin);
20416       if (class_origin != NULL_TREE)
20417         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20418
20419       /* And its containing namespace.  */
20420       context_die = declare_in_namespace (decl_or_origin, context_die);
20421
20422       /* Now output the DIE to represent the data object itself.  This gets
20423          complicated because of the possibility that the VAR_DECL really
20424          represents an inlined instance of a formal parameter for an inline
20425          function.  */
20426       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20427       if (ultimate_origin != NULL_TREE
20428           && TREE_CODE (ultimate_origin) == PARM_DECL)
20429         gen_formal_parameter_die (decl, origin,
20430                                   true /* Emit name attribute.  */,
20431                                   context_die);
20432       else
20433         gen_variable_die (decl, origin, context_die);
20434       break;
20435
20436     case FIELD_DECL:
20437       /* Ignore the nameless fields that are used to skip bits but handle C++
20438          anonymous unions and structs.  */
20439       if (DECL_NAME (decl) != NULL_TREE
20440           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20441           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20442         {
20443           gen_type_die (member_declared_type (decl), context_die);
20444           gen_field_die (decl, context_die);
20445         }
20446       break;
20447
20448     case PARM_DECL:
20449       if (DECL_BY_REFERENCE (decl_or_origin))
20450         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20451       else
20452         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20453       gen_formal_parameter_die (decl, origin,
20454                                 true /* Emit name attribute.  */,
20455                                 context_die);
20456       break;
20457
20458     case NAMESPACE_DECL:
20459     case IMPORTED_DECL:
20460       if (dwarf_version >= 3 || !dwarf_strict)
20461         gen_namespace_die (decl, context_die);
20462       break;
20463
20464     default:
20465       /* Probably some frontend-internal decl.  Assume we don't care.  */
20466       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20467       break;
20468     }
20469 }
20470 \f
20471 /* Output debug information for global decl DECL.  Called from toplev.c after
20472    compilation proper has finished.  */
20473
20474 static void
20475 dwarf2out_global_decl (tree decl)
20476 {
20477   /* Output DWARF2 information for file-scope tentative data object
20478      declarations, file-scope (extern) function declarations (which
20479      had no corresponding body) and file-scope tagged type declarations
20480      and definitions which have not yet been forced out.  */
20481   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20482     dwarf2out_decl (decl);
20483 }
20484
20485 /* Output debug information for type decl DECL.  Called from toplev.c
20486    and from language front ends (to record built-in types).  */
20487 static void
20488 dwarf2out_type_decl (tree decl, int local)
20489 {
20490   if (!local)
20491     dwarf2out_decl (decl);
20492 }
20493
20494 /* Output debug information for imported module or decl DECL.
20495    NAME is non-NULL name in the lexical block if the decl has been renamed.
20496    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20497    that DECL belongs to.
20498    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20499 static void
20500 dwarf2out_imported_module_or_decl_1 (tree decl,
20501                                      tree name,
20502                                      tree lexical_block,
20503                                      dw_die_ref lexical_block_die)
20504 {
20505   expanded_location xloc;
20506   dw_die_ref imported_die = NULL;
20507   dw_die_ref at_import_die;
20508
20509   if (TREE_CODE (decl) == IMPORTED_DECL)
20510     {
20511       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20512       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20513       gcc_assert (decl);
20514     }
20515   else
20516     xloc = expand_location (input_location);
20517
20518   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20519     {
20520       at_import_die = force_type_die (TREE_TYPE (decl));
20521       /* For namespace N { typedef void T; } using N::T; base_type_die
20522          returns NULL, but DW_TAG_imported_declaration requires
20523          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20524       if (!at_import_die)
20525         {
20526           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20527           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20528           at_import_die = lookup_type_die (TREE_TYPE (decl));
20529           gcc_assert (at_import_die);
20530         }
20531     }
20532   else
20533     {
20534       at_import_die = lookup_decl_die (decl);
20535       if (!at_import_die)
20536         {
20537           /* If we're trying to avoid duplicate debug info, we may not have
20538              emitted the member decl for this field.  Emit it now.  */
20539           if (TREE_CODE (decl) == FIELD_DECL)
20540             {
20541               tree type = DECL_CONTEXT (decl);
20542
20543               if (TYPE_CONTEXT (type)
20544                   && TYPE_P (TYPE_CONTEXT (type))
20545                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20546                                                 DINFO_USAGE_DIR_USE))
20547                 return;
20548               gen_type_die_for_member (type, decl,
20549                                        get_context_die (TYPE_CONTEXT (type)));
20550             }
20551           at_import_die = force_decl_die (decl);
20552         }
20553     }
20554
20555   if (TREE_CODE (decl) == NAMESPACE_DECL)
20556     {
20557       if (dwarf_version >= 3 || !dwarf_strict)
20558         imported_die = new_die (DW_TAG_imported_module,
20559                                 lexical_block_die,
20560                                 lexical_block);
20561       else
20562         return;
20563     }
20564   else
20565     imported_die = new_die (DW_TAG_imported_declaration,
20566                             lexical_block_die,
20567                             lexical_block);
20568
20569   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20570   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20571   if (name)
20572     add_AT_string (imported_die, DW_AT_name,
20573                    IDENTIFIER_POINTER (name));
20574   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20575 }
20576
20577 /* Output debug information for imported module or decl DECL.
20578    NAME is non-NULL name in context if the decl has been renamed.
20579    CHILD is true if decl is one of the renamed decls as part of
20580    importing whole module.  */
20581
20582 static void
20583 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20584                                    bool child)
20585 {
20586   /* dw_die_ref at_import_die;  */
20587   dw_die_ref scope_die;
20588
20589   if (debug_info_level <= DINFO_LEVEL_TERSE)
20590     return;
20591
20592   gcc_assert (decl);
20593
20594   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20595      We need decl DIE for reference and scope die. First, get DIE for the decl
20596      itself.  */
20597
20598   /* Get the scope die for decl context. Use comp_unit_die for global module
20599      or decl. If die is not found for non globals, force new die.  */
20600   if (context
20601       && TYPE_P (context)
20602       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20603     return;
20604
20605   if (!(dwarf_version >= 3 || !dwarf_strict))
20606     return;
20607
20608   scope_die = get_context_die (context);
20609
20610   if (child)
20611     {
20612       gcc_assert (scope_die->die_child);
20613       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20614       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20615       scope_die = scope_die->die_child;
20616     }
20617
20618   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20619   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20620
20621 }
20622
20623 /* Write the debugging output for DECL.  */
20624
20625 void
20626 dwarf2out_decl (tree decl)
20627 {
20628   dw_die_ref context_die = comp_unit_die;
20629
20630   switch (TREE_CODE (decl))
20631     {
20632     case ERROR_MARK:
20633       return;
20634
20635     case FUNCTION_DECL:
20636       /* What we would really like to do here is to filter out all mere
20637          file-scope declarations of file-scope functions which are never
20638          referenced later within this translation unit (and keep all of ones
20639          that *are* referenced later on) but we aren't clairvoyant, so we have
20640          no idea which functions will be referenced in the future (i.e. later
20641          on within the current translation unit). So here we just ignore all
20642          file-scope function declarations which are not also definitions.  If
20643          and when the debugger needs to know something about these functions,
20644          it will have to hunt around and find the DWARF information associated
20645          with the definition of the function.
20646
20647          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20648          nodes represent definitions and which ones represent mere
20649          declarations.  We have to check DECL_INITIAL instead. That's because
20650          the C front-end supports some weird semantics for "extern inline"
20651          function definitions.  These can get inlined within the current
20652          translation unit (and thus, we need to generate Dwarf info for their
20653          abstract instances so that the Dwarf info for the concrete inlined
20654          instances can have something to refer to) but the compiler never
20655          generates any out-of-lines instances of such things (despite the fact
20656          that they *are* definitions).
20657
20658          The important point is that the C front-end marks these "extern
20659          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20660          them anyway. Note that the C++ front-end also plays some similar games
20661          for inline function definitions appearing within include files which
20662          also contain `#pragma interface' pragmas.  */
20663       if (DECL_INITIAL (decl) == NULL_TREE)
20664         return;
20665
20666       /* If we're a nested function, initially use a parent of NULL; if we're
20667          a plain function, this will be fixed up in decls_for_scope.  If
20668          we're a method, it will be ignored, since we already have a DIE.  */
20669       if (decl_function_context (decl)
20670           /* But if we're in terse mode, we don't care about scope.  */
20671           && debug_info_level > DINFO_LEVEL_TERSE)
20672         context_die = NULL;
20673       break;
20674
20675     case VAR_DECL:
20676       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20677          declaration and if the declaration was never even referenced from
20678          within this entire compilation unit.  We suppress these DIEs in
20679          order to save space in the .debug section (by eliminating entries
20680          which are probably useless).  Note that we must not suppress
20681          block-local extern declarations (whether used or not) because that
20682          would screw-up the debugger's name lookup mechanism and cause it to
20683          miss things which really ought to be in scope at a given point.  */
20684       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20685         return;
20686
20687       /* For local statics lookup proper context die.  */
20688       if (TREE_STATIC (decl) && decl_function_context (decl))
20689         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20690
20691       /* If we are in terse mode, don't generate any DIEs to represent any
20692          variable declarations or definitions.  */
20693       if (debug_info_level <= DINFO_LEVEL_TERSE)
20694         return;
20695       break;
20696
20697     case CONST_DECL:
20698       if (debug_info_level <= DINFO_LEVEL_TERSE)
20699         return;
20700       if (!is_fortran ())
20701         return;
20702       if (TREE_STATIC (decl) && decl_function_context (decl))
20703         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20704       break;
20705
20706     case NAMESPACE_DECL:
20707     case IMPORTED_DECL:
20708       if (debug_info_level <= DINFO_LEVEL_TERSE)
20709         return;
20710       if (lookup_decl_die (decl) != NULL)
20711         return;
20712       break;
20713
20714     case TYPE_DECL:
20715       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20716       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20717         return;
20718
20719       /* Don't bother trying to generate any DIEs to represent any of the
20720          normal built-in types for the language we are compiling.  */
20721       if (DECL_IS_BUILTIN (decl))
20722         {
20723           /* OK, we need to generate one for `bool' so GDB knows what type
20724              comparisons have.  */
20725           if (is_cxx ()
20726               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20727               && ! DECL_IGNORED_P (decl))
20728             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20729
20730           return;
20731         }
20732
20733       /* If we are in terse mode, don't generate any DIEs for types.  */
20734       if (debug_info_level <= DINFO_LEVEL_TERSE)
20735         return;
20736
20737       /* If we're a function-scope tag, initially use a parent of NULL;
20738          this will be fixed up in decls_for_scope.  */
20739       if (decl_function_context (decl))
20740         context_die = NULL;
20741
20742       break;
20743
20744     default:
20745       return;
20746     }
20747
20748   gen_decl_die (decl, NULL, context_die);
20749 }
20750
20751 /* Write the debugging output for DECL.  */
20752
20753 static void
20754 dwarf2out_function_decl (tree decl)
20755 {
20756   dwarf2out_decl (decl);
20757
20758   htab_empty (decl_loc_table);
20759 }
20760
20761 /* Output a marker (i.e. a label) for the beginning of the generated code for
20762    a lexical block.  */
20763
20764 static void
20765 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20766                        unsigned int blocknum)
20767 {
20768   switch_to_section (current_function_section ());
20769   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20770 }
20771
20772 /* Output a marker (i.e. a label) for the end of the generated code for a
20773    lexical block.  */
20774
20775 static void
20776 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20777 {
20778   switch_to_section (current_function_section ());
20779   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20780 }
20781
20782 /* Returns nonzero if it is appropriate not to emit any debugging
20783    information for BLOCK, because it doesn't contain any instructions.
20784
20785    Don't allow this for blocks with nested functions or local classes
20786    as we would end up with orphans, and in the presence of scheduling
20787    we may end up calling them anyway.  */
20788
20789 static bool
20790 dwarf2out_ignore_block (const_tree block)
20791 {
20792   tree decl;
20793   unsigned int i;
20794
20795   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
20796     if (TREE_CODE (decl) == FUNCTION_DECL
20797         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20798       return 0;
20799   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20800     {
20801       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20802       if (TREE_CODE (decl) == FUNCTION_DECL
20803           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20804       return 0;
20805     }
20806
20807   return 1;
20808 }
20809
20810 /* Hash table routines for file_hash.  */
20811
20812 static int
20813 file_table_eq (const void *p1_p, const void *p2_p)
20814 {
20815   const struct dwarf_file_data *const p1 =
20816     (const struct dwarf_file_data *) p1_p;
20817   const char *const p2 = (const char *) p2_p;
20818   return strcmp (p1->filename, p2) == 0;
20819 }
20820
20821 static hashval_t
20822 file_table_hash (const void *p_p)
20823 {
20824   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20825   return htab_hash_string (p->filename);
20826 }
20827
20828 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20829    dwarf2out.c) and return its "index".  The index of each (known) filename is
20830    just a unique number which is associated with only that one filename.  We
20831    need such numbers for the sake of generating labels (in the .debug_sfnames
20832    section) and references to those files numbers (in the .debug_srcinfo
20833    and.debug_macinfo sections).  If the filename given as an argument is not
20834    found in our current list, add it to the list and assign it the next
20835    available unique index number.  In order to speed up searches, we remember
20836    the index of the filename was looked up last.  This handles the majority of
20837    all searches.  */
20838
20839 static struct dwarf_file_data *
20840 lookup_filename (const char *file_name)
20841 {
20842   void ** slot;
20843   struct dwarf_file_data * created;
20844
20845   /* Check to see if the file name that was searched on the previous
20846      call matches this file name.  If so, return the index.  */
20847   if (file_table_last_lookup
20848       && (file_name == file_table_last_lookup->filename
20849           || strcmp (file_table_last_lookup->filename, file_name) == 0))
20850     return file_table_last_lookup;
20851
20852   /* Didn't match the previous lookup, search the table.  */
20853   slot = htab_find_slot_with_hash (file_table, file_name,
20854                                    htab_hash_string (file_name), INSERT);
20855   if (*slot)
20856     return (struct dwarf_file_data *) *slot;
20857
20858   created = ggc_alloc_dwarf_file_data ();
20859   created->filename = file_name;
20860   created->emitted_number = 0;
20861   *slot = created;
20862   return created;
20863 }
20864
20865 /* If the assembler will construct the file table, then translate the compiler
20866    internal file table number into the assembler file table number, and emit
20867    a .file directive if we haven't already emitted one yet.  The file table
20868    numbers are different because we prune debug info for unused variables and
20869    types, which may include filenames.  */
20870
20871 static int
20872 maybe_emit_file (struct dwarf_file_data * fd)
20873 {
20874   if (! fd->emitted_number)
20875     {
20876       if (last_emitted_file)
20877         fd->emitted_number = last_emitted_file->emitted_number + 1;
20878       else
20879         fd->emitted_number = 1;
20880       last_emitted_file = fd;
20881
20882       if (DWARF2_ASM_LINE_DEBUG_INFO)
20883         {
20884           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20885           output_quoted_string (asm_out_file,
20886                                 remap_debug_filename (fd->filename));
20887           fputc ('\n', asm_out_file);
20888         }
20889     }
20890
20891   return fd->emitted_number;
20892 }
20893
20894 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20895    That generation should happen after function debug info has been
20896    generated. The value of the attribute is the constant value of ARG.  */
20897
20898 static void
20899 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20900 {
20901   die_arg_entry entry;
20902
20903   if (!die || !arg)
20904     return;
20905
20906   if (!tmpl_value_parm_die_table)
20907     tmpl_value_parm_die_table
20908       = VEC_alloc (die_arg_entry, gc, 32);
20909
20910   entry.die = die;
20911   entry.arg = arg;
20912   VEC_safe_push (die_arg_entry, gc,
20913                  tmpl_value_parm_die_table,
20914                  &entry);
20915 }
20916
20917 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20918    by append_entry_to_tmpl_value_parm_die_table. This function must
20919    be called after function DIEs have been generated.  */
20920
20921 static void
20922 gen_remaining_tmpl_value_param_die_attribute (void)
20923 {
20924   if (tmpl_value_parm_die_table)
20925     {
20926       unsigned i;
20927       die_arg_entry *e;
20928
20929       for (i = 0;
20930            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
20931            i++)
20932         tree_add_const_value_attribute (e->die, e->arg);
20933     }
20934 }
20935
20936
20937 /* Replace DW_AT_name for the decl with name.  */
20938
20939 static void
20940 dwarf2out_set_name (tree decl, tree name)
20941 {
20942   dw_die_ref die;
20943   dw_attr_ref attr;
20944   const char *dname;
20945
20946   die = TYPE_SYMTAB_DIE (decl);
20947   if (!die)
20948     return;
20949
20950   dname = dwarf2_name (name, 0);
20951   if (!dname)
20952     return;
20953
20954   attr = get_AT (die, DW_AT_name);
20955   if (attr)
20956     {
20957       struct indirect_string_node *node;
20958
20959       node = find_AT_string (dname);
20960       /* replace the string.  */
20961       attr->dw_attr_val.v.val_str = node;
20962     }
20963
20964   else
20965     add_name_attribute (die, dname);
20966 }
20967
20968 /* Called by the final INSN scan whenever we see a direct function call.
20969    Make an entry into the direct call table, recording the point of call
20970    and a reference to the target function's debug entry.  */
20971
20972 static void
20973 dwarf2out_direct_call (tree targ)
20974 {
20975   dcall_entry e;
20976   tree origin = decl_ultimate_origin (targ);
20977
20978   /* If this is a clone, use the abstract origin as the target.  */
20979   if (origin)
20980     targ = origin;
20981
20982   e.poc_label_num = poc_label_num++;
20983   e.poc_decl = current_function_decl;
20984   e.targ_die = force_decl_die (targ);
20985   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
20986
20987   /* Drop a label at the return point to mark the point of call.  */
20988   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
20989 }
20990
20991 /* Returns a hash value for X (which really is a struct vcall_insn).  */
20992
20993 static hashval_t
20994 vcall_insn_table_hash (const void *x)
20995 {
20996   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
20997 }
20998
20999 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21000    insnd_uid of *Y.  */
21001
21002 static int
21003 vcall_insn_table_eq (const void *x, const void *y)
21004 {
21005   return (((const struct vcall_insn *) x)->insn_uid
21006           == ((const struct vcall_insn *) y)->insn_uid);
21007 }
21008
21009 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21010
21011 static void
21012 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21013 {
21014   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21015   struct vcall_insn **slot;
21016
21017   gcc_assert (item);
21018   item->insn_uid = insn_uid;
21019   item->vtable_slot = vtable_slot;
21020   slot = (struct vcall_insn **)
21021       htab_find_slot_with_hash (vcall_insn_table, &item,
21022                                 (hashval_t) insn_uid, INSERT);
21023   *slot = item;
21024 }
21025
21026 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21027
21028 static unsigned int
21029 lookup_vcall_insn (unsigned int insn_uid)
21030 {
21031   struct vcall_insn item;
21032   struct vcall_insn *p;
21033
21034   item.insn_uid = insn_uid;
21035   item.vtable_slot = 0;
21036   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21037                                                  (void *) &item,
21038                                                  (hashval_t) insn_uid);
21039   if (p == NULL)
21040     return (unsigned int) -1;
21041   return p->vtable_slot;
21042 }
21043
21044
21045 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21046    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21047    is the vtable slot index that we will need to put in the virtual call
21048    table later.  */
21049
21050 static void
21051 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21052 {
21053   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21054     {
21055       tree token = OBJ_TYPE_REF_TOKEN (addr);
21056       if (TREE_CODE (token) == INTEGER_CST)
21057         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21058     }
21059 }
21060
21061 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21062    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21063    with NEW_INSN.  */
21064
21065 static void
21066 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21067 {
21068   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21069
21070   if (vtable_slot != (unsigned int) -1)
21071     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21072 }
21073
21074 /* Called by the final INSN scan whenever we see a virtual function call.
21075    Make an entry into the virtual call table, recording the point of call
21076    and the slot index of the vtable entry used to call the virtual member
21077    function.  The slot index was associated with the INSN_UID during the
21078    lowering to RTL.  */
21079
21080 static void
21081 dwarf2out_virtual_call (int insn_uid)
21082 {
21083   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21084   vcall_entry e;
21085
21086   if (vtable_slot == (unsigned int) -1)
21087     return;
21088
21089   e.poc_label_num = poc_label_num++;
21090   e.vtable_slot = vtable_slot;
21091   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21092
21093   /* Drop a label at the return point to mark the point of call.  */
21094   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21095 }
21096
21097 /* Called by the final INSN scan whenever we see a var location.  We
21098    use it to drop labels in the right places, and throw the location in
21099    our lookup table.  */
21100
21101 static void
21102 dwarf2out_var_location (rtx loc_note)
21103 {
21104   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21105   struct var_loc_node *newloc;
21106   rtx next_real;
21107   static const char *last_label;
21108   static const char *last_postcall_label;
21109   static bool last_in_cold_section_p;
21110   tree decl;
21111
21112   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21113     return;
21114
21115   next_real = next_real_insn (loc_note);
21116   /* If there are no instructions which would be affected by this note,
21117      don't do anything.  */
21118   if (next_real == NULL_RTX)
21119     return;
21120
21121   /* If there were any real insns between note we processed last time
21122      and this note (or if it is the first note), clear
21123      last_{,postcall_}label so that they are not reused this time.  */
21124   if (last_var_location_insn == NULL_RTX
21125       || last_var_location_insn != next_real
21126       || last_in_cold_section_p != in_cold_section_p)
21127     {
21128       last_label = NULL;
21129       last_postcall_label = NULL;
21130     }
21131
21132   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21133   newloc = add_var_loc_to_decl (decl, loc_note,
21134                                 NOTE_DURING_CALL_P (loc_note)
21135                                 ? last_postcall_label : last_label);
21136   if (newloc == NULL)
21137     return;
21138
21139   /* If there were no real insns between note we processed last time
21140      and this note, use the label we emitted last time.  Otherwise
21141      create a new label and emit it.  */
21142   if (last_label == NULL)
21143     {
21144       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21145       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21146       loclabel_num++;
21147       last_label = ggc_strdup (loclabel);
21148     }
21149
21150   if (!NOTE_DURING_CALL_P (loc_note))
21151     newloc->label = last_label;
21152   else
21153     {
21154       if (!last_postcall_label)
21155         {
21156           sprintf (loclabel, "%s-1", last_label);
21157           last_postcall_label = ggc_strdup (loclabel);
21158         }
21159       newloc->label = last_postcall_label;
21160     }
21161
21162   last_var_location_insn = next_real;
21163   last_in_cold_section_p = in_cold_section_p;
21164 }
21165
21166 /* We need to reset the locations at the beginning of each
21167    function. We can't do this in the end_function hook, because the
21168    declarations that use the locations won't have been output when
21169    that hook is called.  Also compute have_multiple_function_sections here.  */
21170
21171 static void
21172 dwarf2out_begin_function (tree fun)
21173 {
21174   if (function_section (fun) != text_section)
21175     have_multiple_function_sections = true;
21176
21177   dwarf2out_note_section_used ();
21178 }
21179
21180 /* Output a label to mark the beginning of a source code line entry
21181    and record information relating to this source line, in
21182    'line_info_table' for later output of the .debug_line section.  */
21183
21184 static void
21185 dwarf2out_source_line (unsigned int line, const char *filename,
21186                        int discriminator, bool is_stmt)
21187 {
21188   static bool last_is_stmt = true;
21189
21190   if (debug_info_level >= DINFO_LEVEL_NORMAL
21191       && line != 0)
21192     {
21193       int file_num = maybe_emit_file (lookup_filename (filename));
21194
21195       switch_to_section (current_function_section ());
21196
21197       /* If requested, emit something human-readable.  */
21198       if (flag_debug_asm)
21199         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21200                  filename, line);
21201
21202       if (DWARF2_ASM_LINE_DEBUG_INFO)
21203         {
21204           /* Emit the .loc directive understood by GNU as.  */
21205           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21206           if (is_stmt != last_is_stmt)
21207             {
21208               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21209               last_is_stmt = is_stmt;
21210             }
21211           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21212             fprintf (asm_out_file, " discriminator %d", discriminator);
21213           fputc ('\n', asm_out_file);
21214
21215           /* Indicate that line number info exists.  */
21216           line_info_table_in_use++;
21217         }
21218       else if (function_section (current_function_decl) != text_section)
21219         {
21220           dw_separate_line_info_ref line_info;
21221           targetm.asm_out.internal_label (asm_out_file,
21222                                           SEPARATE_LINE_CODE_LABEL,
21223                                           separate_line_info_table_in_use);
21224
21225           /* Expand the line info table if necessary.  */
21226           if (separate_line_info_table_in_use
21227               == separate_line_info_table_allocated)
21228             {
21229               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21230               separate_line_info_table
21231                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21232                                  separate_line_info_table,
21233                                  separate_line_info_table_allocated);
21234               memset (separate_line_info_table
21235                        + separate_line_info_table_in_use,
21236                       0,
21237                       (LINE_INFO_TABLE_INCREMENT
21238                        * sizeof (dw_separate_line_info_entry)));
21239             }
21240
21241           /* Add the new entry at the end of the line_info_table.  */
21242           line_info
21243             = &separate_line_info_table[separate_line_info_table_in_use++];
21244           line_info->dw_file_num = file_num;
21245           line_info->dw_line_num = line;
21246           line_info->function = current_function_funcdef_no;
21247         }
21248       else
21249         {
21250           dw_line_info_ref line_info;
21251
21252           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21253                                      line_info_table_in_use);
21254
21255           /* Expand the line info table if necessary.  */
21256           if (line_info_table_in_use == line_info_table_allocated)
21257             {
21258               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21259               line_info_table
21260                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21261                                  line_info_table_allocated);
21262               memset (line_info_table + line_info_table_in_use, 0,
21263                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21264             }
21265
21266           /* Add the new entry at the end of the line_info_table.  */
21267           line_info = &line_info_table[line_info_table_in_use++];
21268           line_info->dw_file_num = file_num;
21269           line_info->dw_line_num = line;
21270         }
21271     }
21272 }
21273
21274 /* Record the beginning of a new source file.  */
21275
21276 static void
21277 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21278 {
21279   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21280     {
21281       /* Record the beginning of the file for break_out_includes.  */
21282       dw_die_ref bincl_die;
21283
21284       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21285       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21286     }
21287
21288   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21289     {
21290       int file_num = maybe_emit_file (lookup_filename (filename));
21291
21292       switch_to_section (debug_macinfo_section);
21293       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21294       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21295                                    lineno);
21296
21297       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21298     }
21299 }
21300
21301 /* Record the end of a source file.  */
21302
21303 static void
21304 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21305 {
21306   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21307     /* Record the end of the file for break_out_includes.  */
21308     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21309
21310   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21311     {
21312       switch_to_section (debug_macinfo_section);
21313       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21314     }
21315 }
21316
21317 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21318    the tail part of the directive line, i.e. the part which is past the
21319    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21320
21321 static void
21322 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21323                   const char *buffer ATTRIBUTE_UNUSED)
21324 {
21325   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21326     {
21327       switch_to_section (debug_macinfo_section);
21328       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21329       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21330       dw2_asm_output_nstring (buffer, -1, "The macro");
21331     }
21332 }
21333
21334 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21335    the tail part of the directive line, i.e. the part which is past the
21336    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21337
21338 static void
21339 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21340                  const char *buffer ATTRIBUTE_UNUSED)
21341 {
21342   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21343     {
21344       switch_to_section (debug_macinfo_section);
21345       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21346       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21347       dw2_asm_output_nstring (buffer, -1, "The macro");
21348     }
21349 }
21350
21351 /* Set up for Dwarf output at the start of compilation.  */
21352
21353 static void
21354 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21355 {
21356   /* Allocate the file_table.  */
21357   file_table = htab_create_ggc (50, file_table_hash,
21358                                 file_table_eq, NULL);
21359
21360   /* Allocate the decl_die_table.  */
21361   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21362                                     decl_die_table_eq, NULL);
21363
21364   /* Allocate the decl_loc_table.  */
21365   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21366                                     decl_loc_table_eq, NULL);
21367
21368   /* Allocate the initial hunk of the decl_scope_table.  */
21369   decl_scope_table = VEC_alloc (tree, gc, 256);
21370
21371   /* Allocate the initial hunk of the abbrev_die_table.  */
21372   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21373     (ABBREV_DIE_TABLE_INCREMENT);
21374   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21375   /* Zero-th entry is allocated, but unused.  */
21376   abbrev_die_table_in_use = 1;
21377
21378   /* Allocate the initial hunk of the line_info_table.  */
21379   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21380     (LINE_INFO_TABLE_INCREMENT);
21381   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21382
21383   /* Zero-th entry is allocated, but unused.  */
21384   line_info_table_in_use = 1;
21385
21386   /* Allocate the pubtypes and pubnames vectors.  */
21387   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21388   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21389
21390   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21391   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21392                                       vcall_insn_table_eq, NULL);
21393
21394   /* Generate the initial DIE for the .debug section.  Note that the (string)
21395      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21396      will (typically) be a relative pathname and that this pathname should be
21397      taken as being relative to the directory from which the compiler was
21398      invoked when the given (base) source file was compiled.  We will fill
21399      in this value in dwarf2out_finish.  */
21400   comp_unit_die = gen_compile_unit_die (NULL);
21401
21402   incomplete_types = VEC_alloc (tree, gc, 64);
21403
21404   used_rtx_array = VEC_alloc (rtx, gc, 32);
21405
21406   debug_info_section = get_section (DEBUG_INFO_SECTION,
21407                                     SECTION_DEBUG, NULL);
21408   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21409                                       SECTION_DEBUG, NULL);
21410   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21411                                        SECTION_DEBUG, NULL);
21412   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21413                                        SECTION_DEBUG, NULL);
21414   debug_line_section = get_section (DEBUG_LINE_SECTION,
21415                                     SECTION_DEBUG, NULL);
21416   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21417                                    SECTION_DEBUG, NULL);
21418   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21419                                         SECTION_DEBUG, NULL);
21420   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21421                                         SECTION_DEBUG, NULL);
21422   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21423                                      SECTION_DEBUG, NULL);
21424   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21425                                      SECTION_DEBUG, NULL);
21426   debug_str_section = get_section (DEBUG_STR_SECTION,
21427                                    DEBUG_STR_SECTION_FLAGS, NULL);
21428   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21429                                       SECTION_DEBUG, NULL);
21430   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21431                                      SECTION_DEBUG, NULL);
21432
21433   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21434   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21435                                DEBUG_ABBREV_SECTION_LABEL, 0);
21436   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21437   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21438                                COLD_TEXT_SECTION_LABEL, 0);
21439   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21440
21441   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21442                                DEBUG_INFO_SECTION_LABEL, 0);
21443   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21444                                DEBUG_LINE_SECTION_LABEL, 0);
21445   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21446                                DEBUG_RANGES_SECTION_LABEL, 0);
21447   switch_to_section (debug_abbrev_section);
21448   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21449   switch_to_section (debug_info_section);
21450   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21451   switch_to_section (debug_line_section);
21452   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21453
21454   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21455     {
21456       switch_to_section (debug_macinfo_section);
21457       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21458                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21459       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21460     }
21461
21462   switch_to_section (text_section);
21463   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21464   if (flag_reorder_blocks_and_partition)
21465     {
21466       cold_text_section = unlikely_text_section ();
21467       switch_to_section (cold_text_section);
21468       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21469     }
21470
21471 }
21472
21473 /* Called before cgraph_optimize starts outputtting functions, variables
21474    and toplevel asms into assembly.  */
21475
21476 static void
21477 dwarf2out_assembly_start (void)
21478 {
21479   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21480     {
21481 #ifndef TARGET_UNWIND_INFO
21482       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21483 #endif
21484         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21485     }
21486 }
21487
21488 /* A helper function for dwarf2out_finish called through
21489    htab_traverse.  Emit one queued .debug_str string.  */
21490
21491 static int
21492 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21493 {
21494   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21495
21496   if (node->label && node->refcount)
21497     {
21498       switch_to_section (debug_str_section);
21499       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21500       assemble_string (node->str, strlen (node->str) + 1);
21501     }
21502
21503   return 1;
21504 }
21505
21506 #if ENABLE_ASSERT_CHECKING
21507 /* Verify that all marks are clear.  */
21508
21509 static void
21510 verify_marks_clear (dw_die_ref die)
21511 {
21512   dw_die_ref c;
21513
21514   gcc_assert (! die->die_mark);
21515   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21516 }
21517 #endif /* ENABLE_ASSERT_CHECKING */
21518
21519 /* Clear the marks for a die and its children.
21520    Be cool if the mark isn't set.  */
21521
21522 static void
21523 prune_unmark_dies (dw_die_ref die)
21524 {
21525   dw_die_ref c;
21526
21527   if (die->die_mark)
21528     die->die_mark = 0;
21529   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21530 }
21531
21532 /* Given DIE that we're marking as used, find any other dies
21533    it references as attributes and mark them as used.  */
21534
21535 static void
21536 prune_unused_types_walk_attribs (dw_die_ref die)
21537 {
21538   dw_attr_ref a;
21539   unsigned ix;
21540
21541   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21542     {
21543       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21544         {
21545           /* A reference to another DIE.
21546              Make sure that it will get emitted.
21547              If it was broken out into a comdat group, don't follow it.  */
21548           if (dwarf_version < 4
21549               || a->dw_attr == DW_AT_specification
21550               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21551             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21552         }
21553       /* Set the string's refcount to 0 so that prune_unused_types_mark
21554          accounts properly for it.  */
21555       if (AT_class (a) == dw_val_class_str)
21556         a->dw_attr_val.v.val_str->refcount = 0;
21557     }
21558 }
21559
21560
21561 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21562    to DIE's children.  */
21563
21564 static void
21565 prune_unused_types_mark (dw_die_ref die, int dokids)
21566 {
21567   dw_die_ref c;
21568
21569   if (die->die_mark == 0)
21570     {
21571       /* We haven't done this node yet.  Mark it as used.  */
21572       die->die_mark = 1;
21573
21574       /* We also have to mark its parents as used.
21575          (But we don't want to mark our parents' kids due to this.)  */
21576       if (die->die_parent)
21577         prune_unused_types_mark (die->die_parent, 0);
21578
21579       /* Mark any referenced nodes.  */
21580       prune_unused_types_walk_attribs (die);
21581
21582       /* If this node is a specification,
21583          also mark the definition, if it exists.  */
21584       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21585         prune_unused_types_mark (die->die_definition, 1);
21586     }
21587
21588   if (dokids && die->die_mark != 2)
21589     {
21590       /* We need to walk the children, but haven't done so yet.
21591          Remember that we've walked the kids.  */
21592       die->die_mark = 2;
21593
21594       /* If this is an array type, we need to make sure our
21595          kids get marked, even if they're types.  If we're
21596          breaking out types into comdat sections, do this
21597          for all type definitions.  */
21598       if (die->die_tag == DW_TAG_array_type
21599           || (dwarf_version >= 4
21600               && is_type_die (die) && ! is_declaration_die (die)))
21601         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21602       else
21603         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21604     }
21605 }
21606
21607 /* For local classes, look if any static member functions were emitted
21608    and if so, mark them.  */
21609
21610 static void
21611 prune_unused_types_walk_local_classes (dw_die_ref die)
21612 {
21613   dw_die_ref c;
21614
21615   if (die->die_mark == 2)
21616     return;
21617
21618   switch (die->die_tag)
21619     {
21620     case DW_TAG_structure_type:
21621     case DW_TAG_union_type:
21622     case DW_TAG_class_type:
21623       break;
21624
21625     case DW_TAG_subprogram:
21626       if (!get_AT_flag (die, DW_AT_declaration)
21627           || die->die_definition != NULL)
21628         prune_unused_types_mark (die, 1);
21629       return;
21630
21631     default:
21632       return;
21633     }
21634
21635   /* Mark children.  */
21636   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21637 }
21638
21639 /* Walk the tree DIE and mark types that we actually use.  */
21640
21641 static void
21642 prune_unused_types_walk (dw_die_ref die)
21643 {
21644   dw_die_ref c;
21645
21646   /* Don't do anything if this node is already marked and
21647      children have been marked as well.  */
21648   if (die->die_mark == 2)
21649     return;
21650
21651   switch (die->die_tag)
21652     {
21653     case DW_TAG_structure_type:
21654     case DW_TAG_union_type:
21655     case DW_TAG_class_type:
21656       if (die->die_perennial_p)
21657         break;
21658
21659       for (c = die->die_parent; c; c = c->die_parent)
21660         if (c->die_tag == DW_TAG_subprogram)
21661           break;
21662
21663       /* Finding used static member functions inside of classes
21664          is needed just for local classes, because for other classes
21665          static member function DIEs with DW_AT_specification
21666          are emitted outside of the DW_TAG_*_type.  If we ever change
21667          it, we'd need to call this even for non-local classes.  */
21668       if (c)
21669         prune_unused_types_walk_local_classes (die);
21670
21671       /* It's a type node --- don't mark it.  */
21672       return;
21673
21674     case DW_TAG_const_type:
21675     case DW_TAG_packed_type:
21676     case DW_TAG_pointer_type:
21677     case DW_TAG_reference_type:
21678     case DW_TAG_rvalue_reference_type:
21679     case DW_TAG_volatile_type:
21680     case DW_TAG_typedef:
21681     case DW_TAG_array_type:
21682     case DW_TAG_interface_type:
21683     case DW_TAG_friend:
21684     case DW_TAG_variant_part:
21685     case DW_TAG_enumeration_type:
21686     case DW_TAG_subroutine_type:
21687     case DW_TAG_string_type:
21688     case DW_TAG_set_type:
21689     case DW_TAG_subrange_type:
21690     case DW_TAG_ptr_to_member_type:
21691     case DW_TAG_file_type:
21692       if (die->die_perennial_p)
21693         break;
21694
21695       /* It's a type node --- don't mark it.  */
21696       return;
21697
21698     default:
21699       /* Mark everything else.  */
21700       break;
21701   }
21702
21703   if (die->die_mark == 0)
21704     {
21705       die->die_mark = 1;
21706
21707       /* Now, mark any dies referenced from here.  */
21708       prune_unused_types_walk_attribs (die);
21709     }
21710
21711   die->die_mark = 2;
21712
21713   /* Mark children.  */
21714   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21715 }
21716
21717 /* Increment the string counts on strings referred to from DIE's
21718    attributes.  */
21719
21720 static void
21721 prune_unused_types_update_strings (dw_die_ref die)
21722 {
21723   dw_attr_ref a;
21724   unsigned ix;
21725
21726   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21727     if (AT_class (a) == dw_val_class_str)
21728       {
21729         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21730         s->refcount++;
21731         /* Avoid unnecessarily putting strings that are used less than
21732            twice in the hash table.  */
21733         if (s->refcount
21734             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21735           {
21736             void ** slot;
21737             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21738                                              htab_hash_string (s->str),
21739                                              INSERT);
21740             gcc_assert (*slot == NULL);
21741             *slot = s;
21742           }
21743       }
21744 }
21745
21746 /* Remove from the tree DIE any dies that aren't marked.  */
21747
21748 static void
21749 prune_unused_types_prune (dw_die_ref die)
21750 {
21751   dw_die_ref c;
21752
21753   gcc_assert (die->die_mark);
21754   prune_unused_types_update_strings (die);
21755
21756   if (! die->die_child)
21757     return;
21758
21759   c = die->die_child;
21760   do {
21761     dw_die_ref prev = c;
21762     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21763       if (c == die->die_child)
21764         {
21765           /* No marked children between 'prev' and the end of the list.  */
21766           if (prev == c)
21767             /* No marked children at all.  */
21768             die->die_child = NULL;
21769           else
21770             {
21771               prev->die_sib = c->die_sib;
21772               die->die_child = prev;
21773             }
21774           return;
21775         }
21776
21777     if (c != prev->die_sib)
21778       prev->die_sib = c;
21779     prune_unused_types_prune (c);
21780   } while (c != die->die_child);
21781 }
21782
21783 /* A helper function for dwarf2out_finish called through
21784    htab_traverse.  Clear .debug_str strings that we haven't already
21785    decided to emit.  */
21786
21787 static int
21788 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21789 {
21790   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21791
21792   if (!node->label || !node->refcount)
21793     htab_clear_slot (debug_str_hash, h);
21794
21795   return 1;
21796 }
21797
21798 /* Remove dies representing declarations that we never use.  */
21799
21800 static void
21801 prune_unused_types (void)
21802 {
21803   unsigned int i;
21804   limbo_die_node *node;
21805   comdat_type_node *ctnode;
21806   pubname_ref pub;
21807   dcall_entry *dcall;
21808
21809 #if ENABLE_ASSERT_CHECKING
21810   /* All the marks should already be clear.  */
21811   verify_marks_clear (comp_unit_die);
21812   for (node = limbo_die_list; node; node = node->next)
21813     verify_marks_clear (node->die);
21814   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21815     verify_marks_clear (ctnode->root_die);
21816 #endif /* ENABLE_ASSERT_CHECKING */
21817
21818   /* Mark types that are used in global variables.  */
21819   premark_types_used_by_global_vars ();
21820
21821   /* Set the mark on nodes that are actually used.  */
21822   prune_unused_types_walk (comp_unit_die);
21823   for (node = limbo_die_list; node; node = node->next)
21824     prune_unused_types_walk (node->die);
21825   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21826     {
21827       prune_unused_types_walk (ctnode->root_die);
21828       prune_unused_types_mark (ctnode->type_die, 1);
21829     }
21830
21831   /* Also set the mark on nodes referenced from the
21832      pubname_table or arange_table.  */
21833   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
21834     prune_unused_types_mark (pub->die, 1);
21835   for (i = 0; i < arange_table_in_use; i++)
21836     prune_unused_types_mark (arange_table[i], 1);
21837
21838   /* Mark nodes referenced from the direct call table.  */
21839   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
21840     prune_unused_types_mark (dcall->targ_die, 1);
21841
21842   /* Get rid of nodes that aren't marked; and update the string counts.  */
21843   if (debug_str_hash && debug_str_hash_forced)
21844     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
21845   else if (debug_str_hash)
21846     htab_empty (debug_str_hash);
21847   prune_unused_types_prune (comp_unit_die);
21848   for (node = limbo_die_list; node; node = node->next)
21849     prune_unused_types_prune (node->die);
21850   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21851     prune_unused_types_prune (ctnode->root_die);
21852
21853   /* Leave the marks clear.  */
21854   prune_unmark_dies (comp_unit_die);
21855   for (node = limbo_die_list; node; node = node->next)
21856     prune_unmark_dies (node->die);
21857   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21858     prune_unmark_dies (ctnode->root_die);
21859 }
21860
21861 /* Set the parameter to true if there are any relative pathnames in
21862    the file table.  */
21863 static int
21864 file_table_relative_p (void ** slot, void *param)
21865 {
21866   bool *p = (bool *) param;
21867   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21868   if (!IS_ABSOLUTE_PATH (d->filename))
21869     {
21870       *p = true;
21871       return 0;
21872     }
21873   return 1;
21874 }
21875
21876 /* Routines to manipulate hash table of comdat type units.  */
21877
21878 static hashval_t
21879 htab_ct_hash (const void *of)
21880 {
21881   hashval_t h;
21882   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21883
21884   memcpy (&h, type_node->signature, sizeof (h));
21885   return h;
21886 }
21887
21888 static int
21889 htab_ct_eq (const void *of1, const void *of2)
21890 {
21891   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21892   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21893
21894   return (! memcmp (type_node_1->signature, type_node_2->signature,
21895                     DWARF_TYPE_SIGNATURE_SIZE));
21896 }
21897
21898 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21899    to the location it would have been added, should we know its
21900    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21901    probably improve compactness of debug info, removing equivalent
21902    abbrevs, and hide any differences caused by deferring the
21903    computation of the assembler name, triggered by e.g. PCH.  */
21904
21905 static inline void
21906 move_linkage_attr (dw_die_ref die)
21907 {
21908   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21909   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21910
21911   gcc_assert (linkage.dw_attr == AT_linkage_name);
21912
21913   while (--ix > 0)
21914     {
21915       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21916
21917       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21918         break;
21919     }
21920
21921   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21922     {
21923       VEC_pop (dw_attr_node, die->die_attr);
21924       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21925     }
21926 }
21927
21928 /* Helper function for resolve_addr, attempt to resolve
21929    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21930    SYMBOL_REFs refer to variables emitted in the current CU.  */
21931
21932 static int
21933 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21934 {
21935   rtx rtl = *addr;
21936
21937   if (GET_CODE (rtl) == CONST_STRING)
21938     {
21939       size_t len = strlen (XSTR (rtl, 0)) + 1;
21940       tree t = build_string (len, XSTR (rtl, 0));
21941       tree tlen = build_int_cst (NULL_TREE, len - 1);
21942       TREE_TYPE (t)
21943         = build_array_type (char_type_node, build_index_type (tlen));
21944       rtl = lookup_constant_def (t);
21945       if (!rtl || !MEM_P (rtl))
21946         return 1;
21947       rtl = XEXP (rtl, 0);
21948       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21949       *addr = rtl;
21950       return 0;
21951     }
21952
21953   if (GET_CODE (rtl) == SYMBOL_REF
21954       && SYMBOL_REF_DECL (rtl)
21955       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21956     return 1;
21957
21958   if (GET_CODE (rtl) == CONST
21959       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21960     return 1;
21961
21962   return 0;
21963 }
21964
21965 /* Helper function for resolve_addr, handle one location
21966    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21967    the location list couldn't be resolved.  */
21968
21969 static bool
21970 resolve_addr_in_expr (dw_loc_descr_ref loc)
21971 {
21972   for (; loc; loc = loc->dw_loc_next)
21973     if ((loc->dw_loc_opc == DW_OP_addr
21974          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21975         || (loc->dw_loc_opc == DW_OP_implicit_value
21976             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21977             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
21978       return false;
21979   return true;
21980 }
21981
21982 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21983    an address in .rodata section if the string literal is emitted there,
21984    or remove the containing location list or replace DW_AT_const_value
21985    with DW_AT_location and empty location expression, if it isn't found
21986    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21987    to something that has been emitted in the current CU.  */
21988
21989 static void
21990 resolve_addr (dw_die_ref die)
21991 {
21992   dw_die_ref c;
21993   dw_attr_ref a;
21994   dw_loc_list_ref *curr;
21995   unsigned ix;
21996
21997   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21998     switch (AT_class (a))
21999       {
22000       case dw_val_class_loc_list:
22001         curr = AT_loc_list_ptr (a);
22002         while (*curr)
22003           {
22004             if (!resolve_addr_in_expr ((*curr)->expr))
22005               {
22006                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22007                 if (next && (*curr)->ll_symbol)
22008                   {
22009                     gcc_assert (!next->ll_symbol);
22010                     next->ll_symbol = (*curr)->ll_symbol;
22011                   }
22012                 *curr = next;
22013               }
22014             else
22015               curr = &(*curr)->dw_loc_next;
22016           }
22017         if (!AT_loc_list (a))
22018           {
22019             remove_AT (die, a->dw_attr);
22020             ix--;
22021           }
22022         break;
22023       case dw_val_class_loc:
22024         if (!resolve_addr_in_expr (AT_loc (a)))
22025           {
22026             remove_AT (die, a->dw_attr);
22027             ix--;
22028           }
22029         break;
22030       case dw_val_class_addr:
22031         if (a->dw_attr == DW_AT_const_value
22032             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22033           {
22034             remove_AT (die, a->dw_attr);
22035             ix--;
22036           }
22037         break;
22038       default:
22039         break;
22040       }
22041
22042   FOR_EACH_CHILD (die, c, resolve_addr (c));
22043 }
22044
22045 /* Output stuff that dwarf requires at the end of every file,
22046    and generate the DWARF-2 debugging info.  */
22047
22048 static void
22049 dwarf2out_finish (const char *filename)
22050 {
22051   limbo_die_node *node, *next_node;
22052   comdat_type_node *ctnode;
22053   htab_t comdat_type_table;
22054   dw_die_ref die = 0;
22055   unsigned int i;
22056
22057   gen_remaining_tmpl_value_param_die_attribute ();
22058
22059   /* Add the name for the main input file now.  We delayed this from
22060      dwarf2out_init to avoid complications with PCH.  */
22061   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22062   if (!IS_ABSOLUTE_PATH (filename))
22063     add_comp_dir_attribute (comp_unit_die);
22064   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22065     {
22066       bool p = false;
22067       htab_traverse (file_table, file_table_relative_p, &p);
22068       if (p)
22069         add_comp_dir_attribute (comp_unit_die);
22070     }
22071
22072   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22073     {
22074       add_location_or_const_value_attribute (
22075         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22076         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22077         DW_AT_location);
22078     }
22079
22080   /* Traverse the limbo die list, and add parent/child links.  The only
22081      dies without parents that should be here are concrete instances of
22082      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22083      For concrete instances, we can get the parent die from the abstract
22084      instance.  */
22085   for (node = limbo_die_list; node; node = next_node)
22086     {
22087       next_node = node->next;
22088       die = node->die;
22089
22090       if (die->die_parent == NULL)
22091         {
22092           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22093
22094           if (origin)
22095             add_child_die (origin->die_parent, die);
22096           else if (die == comp_unit_die)
22097             ;
22098           else if (seen_error ())
22099             /* It's OK to be confused by errors in the input.  */
22100             add_child_die (comp_unit_die, die);
22101           else
22102             {
22103               /* In certain situations, the lexical block containing a
22104                  nested function can be optimized away, which results
22105                  in the nested function die being orphaned.  Likewise
22106                  with the return type of that nested function.  Force
22107                  this to be a child of the containing function.
22108
22109                  It may happen that even the containing function got fully
22110                  inlined and optimized out.  In that case we are lost and
22111                  assign the empty child.  This should not be big issue as
22112                  the function is likely unreachable too.  */
22113               tree context = NULL_TREE;
22114
22115               gcc_assert (node->created_for);
22116
22117               if (DECL_P (node->created_for))
22118                 context = DECL_CONTEXT (node->created_for);
22119               else if (TYPE_P (node->created_for))
22120                 context = TYPE_CONTEXT (node->created_for);
22121
22122               gcc_assert (context
22123                           && (TREE_CODE (context) == FUNCTION_DECL
22124                               || TREE_CODE (context) == NAMESPACE_DECL));
22125
22126               origin = lookup_decl_die (context);
22127               if (origin)
22128                 add_child_die (origin, die);
22129               else
22130                 add_child_die (comp_unit_die, die);
22131             }
22132         }
22133     }
22134
22135   limbo_die_list = NULL;
22136
22137   resolve_addr (comp_unit_die);
22138
22139   for (node = deferred_asm_name; node; node = node->next)
22140     {
22141       tree decl = node->created_for;
22142       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22143         {
22144           add_AT_string (node->die, AT_linkage_name,
22145                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
22146           move_linkage_attr (node->die);
22147         }
22148     }
22149
22150   deferred_asm_name = NULL;
22151
22152   /* Walk through the list of incomplete types again, trying once more to
22153      emit full debugging info for them.  */
22154   retry_incomplete_types ();
22155
22156   if (flag_eliminate_unused_debug_types)
22157     prune_unused_types ();
22158
22159   /* Generate separate CUs for each of the include files we've seen.
22160      They will go into limbo_die_list.  */
22161   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22162     break_out_includes (comp_unit_die);
22163
22164   /* Generate separate COMDAT sections for type DIEs. */
22165   if (dwarf_version >= 4)
22166     {
22167       break_out_comdat_types (comp_unit_die);
22168
22169       /* Each new type_unit DIE was added to the limbo die list when created.
22170          Since these have all been added to comdat_type_list, clear the
22171          limbo die list.  */
22172       limbo_die_list = NULL;
22173
22174       /* For each new comdat type unit, copy declarations for incomplete
22175          types to make the new unit self-contained (i.e., no direct
22176          references to the main compile unit).  */
22177       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22178         copy_decls_for_unworthy_types (ctnode->root_die);
22179       copy_decls_for_unworthy_types (comp_unit_die);
22180
22181       /* In the process of copying declarations from one unit to another,
22182          we may have left some declarations behind that are no longer
22183          referenced.  Prune them.  */
22184       prune_unused_types ();
22185     }
22186
22187   /* Traverse the DIE's and add add sibling attributes to those DIE's
22188      that have children.  */
22189   add_sibling_attributes (comp_unit_die);
22190   for (node = limbo_die_list; node; node = node->next)
22191     add_sibling_attributes (node->die);
22192   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22193     add_sibling_attributes (ctnode->root_die);
22194
22195   /* Output a terminator label for the .text section.  */
22196   switch_to_section (text_section);
22197   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22198   if (flag_reorder_blocks_and_partition)
22199     {
22200       switch_to_section (unlikely_text_section ());
22201       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22202     }
22203
22204   /* We can only use the low/high_pc attributes if all of the code was
22205      in .text.  */
22206   if (!have_multiple_function_sections
22207       || !(dwarf_version >= 3 || !dwarf_strict))
22208     {
22209       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22210       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22211     }
22212
22213   else
22214     {
22215       unsigned fde_idx = 0;
22216       bool range_list_added = false;
22217
22218       /* We need to give .debug_loc and .debug_ranges an appropriate
22219          "base address".  Use zero so that these addresses become
22220          absolute.  Historically, we've emitted the unexpected
22221          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22222          Emit both to give time for other tools to adapt.  */
22223       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22224       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22225
22226       if (text_section_used)
22227         add_ranges_by_labels (comp_unit_die, text_section_label,
22228                               text_end_label, &range_list_added);
22229       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22230         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22231                               cold_end_label, &range_list_added);
22232
22233       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22234         {
22235           dw_fde_ref fde = &fde_table[fde_idx];
22236
22237           if (fde->dw_fde_switched_sections)
22238             {
22239               if (!fde->in_std_section)
22240                 add_ranges_by_labels (comp_unit_die,
22241                                       fde->dw_fde_hot_section_label,
22242                                       fde->dw_fde_hot_section_end_label,
22243                                       &range_list_added);
22244               if (!fde->cold_in_std_section)
22245                 add_ranges_by_labels (comp_unit_die,
22246                                       fde->dw_fde_unlikely_section_label,
22247                                       fde->dw_fde_unlikely_section_end_label,
22248                                       &range_list_added);
22249             }
22250           else if (!fde->in_std_section)
22251             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22252                                   fde->dw_fde_end, &range_list_added);
22253         }
22254
22255       if (range_list_added)
22256         add_ranges (NULL);
22257     }
22258
22259   /* Output location list section if necessary.  */
22260   if (have_location_lists)
22261     {
22262       /* Output the location lists info.  */
22263       switch_to_section (debug_loc_section);
22264       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22265                                    DEBUG_LOC_SECTION_LABEL, 0);
22266       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22267       output_location_lists (die);
22268     }
22269
22270   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22271     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22272                     debug_line_section_label);
22273
22274   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22275     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22276
22277   /* Output all of the compilation units.  We put the main one last so that
22278      the offsets are available to output_pubnames.  */
22279   for (node = limbo_die_list; node; node = node->next)
22280     output_comp_unit (node->die, 0);
22281
22282   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22283   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22284     {
22285       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22286
22287       /* Don't output duplicate types.  */
22288       if (*slot != HTAB_EMPTY_ENTRY)
22289         continue;
22290
22291       /* Add a pointer to the line table for the main compilation unit
22292          so that the debugger can make sense of DW_AT_decl_file
22293          attributes.  */
22294       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22295         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22296                         debug_line_section_label);
22297
22298       output_comdat_type_unit (ctnode);
22299       *slot = ctnode;
22300     }
22301   htab_delete (comdat_type_table);
22302
22303   /* Output the main compilation unit if non-empty or if .debug_macinfo
22304      has been emitted.  */
22305   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22306
22307   /* Output the abbreviation table.  */
22308   switch_to_section (debug_abbrev_section);
22309   output_abbrev_section ();
22310
22311   /* Output public names table if necessary.  */
22312   if (!VEC_empty (pubname_entry, pubname_table))
22313     {
22314       switch_to_section (debug_pubnames_section);
22315       output_pubnames (pubname_table);
22316     }
22317
22318   /* Output public types table if necessary.  */
22319   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22320      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22321      simply won't look for the section.  */
22322   if (!VEC_empty (pubname_entry, pubtype_table))
22323     {
22324       switch_to_section (debug_pubtypes_section);
22325       output_pubnames (pubtype_table);
22326     }
22327
22328   /* Output direct and virtual call tables if necessary.  */
22329   if (!VEC_empty (dcall_entry, dcall_table))
22330     {
22331       switch_to_section (debug_dcall_section);
22332       output_dcall_table ();
22333     }
22334   if (!VEC_empty (vcall_entry, vcall_table))
22335     {
22336       switch_to_section (debug_vcall_section);
22337       output_vcall_table ();
22338     }
22339
22340   /* Output the address range information.  We only put functions in the arange
22341      table, so don't write it out if we don't have any.  */
22342   if (fde_table_in_use)
22343     {
22344       switch_to_section (debug_aranges_section);
22345       output_aranges ();
22346     }
22347
22348   /* Output ranges section if necessary.  */
22349   if (ranges_table_in_use)
22350     {
22351       switch_to_section (debug_ranges_section);
22352       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22353       output_ranges ();
22354     }
22355
22356   /* Output the source line correspondence table.  We must do this
22357      even if there is no line information.  Otherwise, on an empty
22358      translation unit, we will generate a present, but empty,
22359      .debug_info section.  IRIX 6.5 `nm' will then complain when
22360      examining the file.  This is done late so that any filenames
22361      used by the debug_info section are marked as 'used'.  */
22362   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22363     {
22364       switch_to_section (debug_line_section);
22365       output_line_info ();
22366     }
22367
22368   /* Have to end the macro section.  */
22369   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22370     {
22371       switch_to_section (debug_macinfo_section);
22372       dw2_asm_output_data (1, 0, "End compilation unit");
22373     }
22374
22375   /* If we emitted any DW_FORM_strp form attribute, output the string
22376      table too.  */
22377   if (debug_str_hash)
22378     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22379 }
22380 #else
22381
22382 /* This should never be used, but its address is needed for comparisons.  */
22383 const struct gcc_debug_hooks dwarf2_debug_hooks =
22384 {
22385   0,            /* init */
22386   0,            /* finish */
22387   0,            /* assembly_start */
22388   0,            /* define */
22389   0,            /* undef */
22390   0,            /* start_source_file */
22391   0,            /* end_source_file */
22392   0,            /* begin_block */
22393   0,            /* end_block */
22394   0,            /* ignore_block */
22395   0,            /* source_line */
22396   0,            /* begin_prologue */
22397   0,            /* end_prologue */
22398   0,            /* begin_epilogue */
22399   0,            /* end_epilogue */
22400   0,            /* begin_function */
22401   0,            /* end_function */
22402   0,            /* function_decl */
22403   0,            /* global_decl */
22404   0,            /* type_decl */
22405   0,            /* imported_module_or_decl */
22406   0,            /* deferred_inline_function */
22407   0,            /* outlining_inline_function */
22408   0,            /* label */
22409   0,            /* handle_pch */
22410   0,            /* var_location */
22411   0,            /* switch_text_section */
22412   0,            /* direct_call */
22413   0,            /* virtual_call_token */
22414   0,            /* copy_call_info */
22415   0,            /* virtual_call */
22416   0,            /* set_name */
22417   0             /* start_end_main_source_file */
22418 };
22419
22420 #endif /* DWARF2_DEBUGGING_INFO */
22421
22422 #include "gt-dwarf2out.h"