OSDN Git Service

2010-07-01 Richard Guenther <rguenther@suse.de>
[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.emit_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.emit_unwind_label (asm_out_file, fde_table[i].decl,
3684                                              1, 1);
3685
3686       if (!any_eh_needed)
3687         return;
3688     }
3689
3690   /* We're going to be generating comments, so turn on app.  */
3691   if (flag_debug_asm)
3692     app_enable ();
3693
3694   /* Switch to the proper frame section, first time.  */
3695   switch_to_frame_table_section (for_eh, false);
3696
3697   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3698   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3699
3700   /* Output the CIE.  */
3701   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3702   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3703   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3704     dw2_asm_output_data (4, 0xffffffff,
3705       "Initial length escape value indicating 64-bit DWARF extension");
3706   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3707                         "Length of Common Information Entry");
3708   ASM_OUTPUT_LABEL (asm_out_file, l1);
3709
3710   /* Now that the CIE pointer is PC-relative for EH,
3711      use 0 to identify the CIE.  */
3712   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3713                        (for_eh ? 0 : DWARF_CIE_ID),
3714                        "CIE Identifier Tag");
3715
3716   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3717      use CIE version 1, unless that would produce incorrect results
3718      due to overflowing the return register column.  */
3719   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3720   dw_cie_version = 1;
3721   if (return_reg >= 256 || dwarf_version > 2)
3722     dw_cie_version = 3;
3723   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3724
3725   augmentation[0] = 0;
3726   augmentation_size = 0;
3727
3728   personality = current_unit_personality;
3729   if (for_eh)
3730     {
3731       char *p;
3732
3733       /* Augmentation:
3734          z      Indicates that a uleb128 is present to size the
3735                 augmentation section.
3736          L      Indicates the encoding (and thus presence) of
3737                 an LSDA pointer in the FDE augmentation.
3738          R      Indicates a non-default pointer encoding for
3739                 FDE code pointers.
3740          P      Indicates the presence of an encoding + language
3741                 personality routine in the CIE augmentation.  */
3742
3743       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3744       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3745       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3746
3747       p = augmentation + 1;
3748       if (personality)
3749         {
3750           *p++ = 'P';
3751           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3752           assemble_external_libcall (personality);
3753         }
3754       if (any_lsda_needed)
3755         {
3756           *p++ = 'L';
3757           augmentation_size += 1;
3758         }
3759       if (fde_encoding != DW_EH_PE_absptr)
3760         {
3761           *p++ = 'R';
3762           augmentation_size += 1;
3763         }
3764       if (p > augmentation + 1)
3765         {
3766           augmentation[0] = 'z';
3767           *p = '\0';
3768         }
3769
3770       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3771       if (personality && per_encoding == DW_EH_PE_aligned)
3772         {
3773           int offset = (  4             /* Length */
3774                         + 4             /* CIE Id */
3775                         + 1             /* CIE version */
3776                         + strlen (augmentation) + 1     /* Augmentation */
3777                         + size_of_uleb128 (1)           /* Code alignment */
3778                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3779                         + 1             /* RA column */
3780                         + 1             /* Augmentation size */
3781                         + 1             /* Personality encoding */ );
3782           int pad = -offset & (PTR_SIZE - 1);
3783
3784           augmentation_size += pad;
3785
3786           /* Augmentations should be small, so there's scarce need to
3787              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3788           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3789         }
3790     }
3791
3792   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3793   if (dw_cie_version >= 4)
3794     {
3795       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3796       dw2_asm_output_data (1, 0, "CIE Segment Size");
3797     }
3798   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3799   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3800                                "CIE Data Alignment Factor");
3801
3802   if (dw_cie_version == 1)
3803     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3804   else
3805     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3806
3807   if (augmentation[0])
3808     {
3809       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3810       if (personality)
3811         {
3812           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3813                                eh_data_format_name (per_encoding));
3814           dw2_asm_output_encoded_addr_rtx (per_encoding,
3815                                            personality,
3816                                            true, NULL);
3817         }
3818
3819       if (any_lsda_needed)
3820         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3821                              eh_data_format_name (lsda_encoding));
3822
3823       if (fde_encoding != DW_EH_PE_absptr)
3824         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3825                              eh_data_format_name (fde_encoding));
3826     }
3827
3828   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3829     output_cfi (cfi, NULL, for_eh);
3830
3831   /* Pad the CIE out to an address sized boundary.  */
3832   ASM_OUTPUT_ALIGN (asm_out_file,
3833                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3834   ASM_OUTPUT_LABEL (asm_out_file, l2);
3835
3836   /* Loop through all of the FDE's.  */
3837   for (i = 0; i < fde_table_in_use; i++)
3838     {
3839       unsigned int k;
3840       fde = &fde_table[i];
3841
3842       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3843       if (for_eh && !fde_needed_for_eh_p (fde))
3844         continue;
3845
3846       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3847         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3848                     augmentation, any_lsda_needed, lsda_encoding);
3849     }
3850
3851   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3852     dw2_asm_output_data (4, 0, "End of Table");
3853 #ifdef MIPS_DEBUGGING_INFO
3854   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3855      get a value of 0.  Putting .align 0 after the label fixes it.  */
3856   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3857 #endif
3858
3859   /* Turn off app to make assembly quicker.  */
3860   if (flag_debug_asm)
3861     app_disable ();
3862 }
3863
3864 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3865
3866 static void
3867 dwarf2out_do_cfi_startproc (bool second)
3868 {
3869   int enc;
3870   rtx ref;
3871   rtx personality = get_personality_function (current_function_decl);
3872
3873   fprintf (asm_out_file, "\t.cfi_startproc\n");
3874
3875   if (personality)
3876     {
3877       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3878       ref = personality;
3879
3880       /* ??? The GAS support isn't entirely consistent.  We have to
3881          handle indirect support ourselves, but PC-relative is done
3882          in the assembler.  Further, the assembler can't handle any
3883          of the weirder relocation types.  */
3884       if (enc & DW_EH_PE_indirect)
3885         ref = dw2_force_const_mem (ref, true);
3886
3887       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3888       output_addr_const (asm_out_file, ref);
3889       fputc ('\n', asm_out_file);
3890     }
3891
3892   if (crtl->uses_eh_lsda)
3893     {
3894       char lab[20];
3895
3896       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3897       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3898                                    current_function_funcdef_no);
3899       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3900       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3901
3902       if (enc & DW_EH_PE_indirect)
3903         ref = dw2_force_const_mem (ref, true);
3904
3905       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3906       output_addr_const (asm_out_file, ref);
3907       fputc ('\n', asm_out_file);
3908     }
3909 }
3910
3911 /* Output a marker (i.e. a label) for the beginning of a function, before
3912    the prologue.  */
3913
3914 void
3915 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3916                           const char *file ATTRIBUTE_UNUSED)
3917 {
3918   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3919   char * dup_label;
3920   dw_fde_ref fde;
3921   section *fnsec;
3922
3923   current_function_func_begin_label = NULL;
3924
3925 #ifdef TARGET_UNWIND_INFO
3926   /* ??? current_function_func_begin_label is also used by except.c
3927      for call-site information.  We must emit this label if it might
3928      be used.  */
3929   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3930       && ! dwarf2out_do_frame ())
3931     return;
3932 #else
3933   if (! dwarf2out_do_frame ())
3934     return;
3935 #endif
3936
3937   fnsec = function_section (current_function_decl);
3938   switch_to_section (fnsec);
3939   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3940                                current_function_funcdef_no);
3941   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3942                           current_function_funcdef_no);
3943   dup_label = xstrdup (label);
3944   current_function_func_begin_label = dup_label;
3945
3946 #ifdef TARGET_UNWIND_INFO
3947   /* We can elide the fde allocation if we're not emitting debug info.  */
3948   if (! dwarf2out_do_frame ())
3949     return;
3950 #endif
3951
3952   /* Expand the fde table if necessary.  */
3953   if (fde_table_in_use == fde_table_allocated)
3954     {
3955       fde_table_allocated += FDE_TABLE_INCREMENT;
3956       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3957       memset (fde_table + fde_table_in_use, 0,
3958               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3959     }
3960
3961   /* Record the FDE associated with this function.  */
3962   current_funcdef_fde = fde_table_in_use;
3963
3964   /* Add the new FDE at the end of the fde_table.  */
3965   fde = &fde_table[fde_table_in_use++];
3966   fde->decl = current_function_decl;
3967   fde->dw_fde_begin = dup_label;
3968   fde->dw_fde_current_label = dup_label;
3969   fde->dw_fde_hot_section_label = NULL;
3970   fde->dw_fde_hot_section_end_label = NULL;
3971   fde->dw_fde_unlikely_section_label = NULL;
3972   fde->dw_fde_unlikely_section_end_label = NULL;
3973   fde->dw_fde_switched_sections = 0;
3974   fde->dw_fde_switched_cold_to_hot = 0;
3975   fde->dw_fde_end = NULL;
3976   fde->dw_fde_vms_end_prologue = NULL;
3977   fde->dw_fde_vms_begin_epilogue = NULL;
3978   fde->dw_fde_cfi = NULL;
3979   fde->dw_fde_switch_cfi = NULL;
3980   fde->funcdef_number = current_function_funcdef_no;
3981   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3982   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3983   fde->nothrow = crtl->nothrow;
3984   fde->drap_reg = INVALID_REGNUM;
3985   fde->vdrap_reg = INVALID_REGNUM;
3986   if (flag_reorder_blocks_and_partition)
3987     {
3988       section *unlikelysec;
3989       if (first_function_block_is_cold)
3990         fde->in_std_section = 1;
3991       else
3992         fde->in_std_section
3993           = (fnsec == text_section
3994              || (cold_text_section && fnsec == cold_text_section));
3995       unlikelysec = unlikely_text_section ();
3996       fde->cold_in_std_section
3997         = (unlikelysec == text_section
3998            || (cold_text_section && unlikelysec == cold_text_section));
3999     }
4000   else
4001     {
4002       fde->in_std_section
4003         = (fnsec == text_section
4004            || (cold_text_section && fnsec == cold_text_section));
4005       fde->cold_in_std_section = 0;
4006     }
4007
4008   args_size = old_args_size = 0;
4009
4010   /* We only want to output line number information for the genuine dwarf2
4011      prologue case, not the eh frame case.  */
4012 #ifdef DWARF2_DEBUGGING_INFO
4013   if (file)
4014     dwarf2out_source_line (line, file, 0, true);
4015 #endif
4016
4017   if (dwarf2out_do_cfi_asm ())
4018     dwarf2out_do_cfi_startproc (false);
4019   else
4020     {
4021       rtx personality = get_personality_function (current_function_decl);
4022       if (!current_unit_personality)
4023         current_unit_personality = personality;
4024
4025       /* We cannot keep a current personality per function as without CFI
4026          asm, at the point where we emit the CFI data, there is no current
4027          function anymore.  */
4028       if (personality && current_unit_personality != personality)
4029         sorry ("multiple EH personalities are supported only with assemblers "
4030                "supporting .cfi_personality directive");
4031     }
4032 }
4033
4034 /* Output a marker (i.e. a label) for the end of the generated code
4035    for a function prologue.  This gets called *after* the prologue code has
4036    been generated.  */
4037
4038 void
4039 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4040                         const char *file ATTRIBUTE_UNUSED)
4041 {
4042   dw_fde_ref fde;
4043   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4044
4045   /* Output a label to mark the endpoint of the code generated for this
4046      function.  */
4047   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4048                                current_function_funcdef_no);
4049   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4050                           current_function_funcdef_no);
4051   fde = &fde_table[fde_table_in_use - 1];
4052   fde->dw_fde_vms_end_prologue = xstrdup (label);
4053 }
4054
4055 /* Output a marker (i.e. a label) for the beginning of the generated code
4056    for a function epilogue.  This gets called *before* the prologue code has
4057    been generated.  */
4058
4059 void
4060 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4061                           const char *file ATTRIBUTE_UNUSED)
4062 {
4063   dw_fde_ref fde;
4064   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4065
4066   fde = &fde_table[fde_table_in_use - 1];
4067   if (fde->dw_fde_vms_begin_epilogue)
4068     return;
4069
4070   /* Output a label to mark the endpoint of the code generated for this
4071      function.  */
4072   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4073                                current_function_funcdef_no);
4074   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4075                           current_function_funcdef_no);
4076   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4077 }
4078
4079 /* Output a marker (i.e. a label) for the absolute end of the generated code
4080    for a function definition.  This gets called *after* the epilogue code has
4081    been generated.  */
4082
4083 void
4084 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4085                         const char *file ATTRIBUTE_UNUSED)
4086 {
4087   dw_fde_ref fde;
4088   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4089
4090 #ifdef DWARF2_DEBUGGING_INFO
4091   last_var_location_insn = NULL_RTX;
4092 #endif
4093
4094   if (dwarf2out_do_cfi_asm ())
4095     fprintf (asm_out_file, "\t.cfi_endproc\n");
4096
4097   /* Output a label to mark the endpoint of the code generated for this
4098      function.  */
4099   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4100                                current_function_funcdef_no);
4101   ASM_OUTPUT_LABEL (asm_out_file, label);
4102   fde = current_fde ();
4103   gcc_assert (fde != NULL);
4104   fde->dw_fde_end = xstrdup (label);
4105 }
4106
4107 void
4108 dwarf2out_frame_init (void)
4109 {
4110   /* Allocate the initial hunk of the fde_table.  */
4111   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4112   fde_table_allocated = FDE_TABLE_INCREMENT;
4113   fde_table_in_use = 0;
4114
4115   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4116      sake of lookup_cfa.  */
4117
4118   /* On entry, the Canonical Frame Address is at SP.  */
4119   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4120
4121 #ifdef DWARF2_UNWIND_INFO
4122   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4123     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4124 #endif
4125 }
4126
4127 void
4128 dwarf2out_frame_finish (void)
4129 {
4130   /* Output call frame information.  */
4131   if (DWARF2_FRAME_INFO)
4132     output_call_frame_info (0);
4133
4134 #ifndef TARGET_UNWIND_INFO
4135   /* Output another copy for the unwinder.  */
4136   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4137     output_call_frame_info (1);
4138 #endif
4139 }
4140
4141 /* Note that the current function section is being used for code.  */
4142
4143 static void
4144 dwarf2out_note_section_used (void)
4145 {
4146   section *sec = current_function_section ();
4147   if (sec == text_section)
4148     text_section_used = true;
4149   else if (sec == cold_text_section)
4150     cold_text_section_used = true;
4151 }
4152
4153 void
4154 dwarf2out_switch_text_section (void)
4155 {
4156   dw_fde_ref fde = current_fde ();
4157
4158   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4159
4160   fde->dw_fde_switched_sections = 1;
4161   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4162
4163   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4164   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4165   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4166   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4167   have_multiple_function_sections = true;
4168
4169   /* Reset the current label on switching text sections, so that we
4170      don't attempt to advance_loc4 between labels in different sections.  */
4171   fde->dw_fde_current_label = NULL;
4172
4173   /* There is no need to mark used sections when not debugging.  */
4174   if (cold_text_section != NULL)
4175     dwarf2out_note_section_used ();
4176
4177   if (dwarf2out_do_cfi_asm ())
4178     fprintf (asm_out_file, "\t.cfi_endproc\n");
4179
4180   /* Now do the real section switch.  */
4181   switch_to_section (current_function_section ());
4182
4183   if (dwarf2out_do_cfi_asm ())
4184     {
4185       dwarf2out_do_cfi_startproc (true);
4186       /* As this is a different FDE, insert all current CFI instructions
4187          again.  */
4188       output_cfis (fde->dw_fde_cfi, true, fde, true);
4189     }
4190   else
4191     {
4192       dw_cfi_ref cfi = fde->dw_fde_cfi;
4193
4194       cfi = fde->dw_fde_cfi;
4195       if (cfi)
4196         while (cfi->dw_cfi_next != NULL)
4197           cfi = cfi->dw_cfi_next;
4198       fde->dw_fde_switch_cfi = cfi;
4199     }
4200 }
4201 #endif
4202 \f
4203 /* And now, the subset of the debugging information support code necessary
4204    for emitting location expressions.  */
4205
4206 /* Data about a single source file.  */
4207 struct GTY(()) dwarf_file_data {
4208   const char * filename;
4209   int emitted_number;
4210 };
4211
4212 typedef struct dw_val_struct *dw_val_ref;
4213 typedef struct die_struct *dw_die_ref;
4214 typedef const struct die_struct *const_dw_die_ref;
4215 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4216 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4217
4218 typedef struct GTY(()) deferred_locations_struct
4219 {
4220   tree variable;
4221   dw_die_ref die;
4222 } deferred_locations;
4223
4224 DEF_VEC_O(deferred_locations);
4225 DEF_VEC_ALLOC_O(deferred_locations,gc);
4226
4227 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4228
4229 DEF_VEC_P(dw_die_ref);
4230 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4231
4232 /* Each DIE may have a series of attribute/value pairs.  Values
4233    can take on several forms.  The forms that are used in this
4234    implementation are listed below.  */
4235
4236 enum dw_val_class
4237 {
4238   dw_val_class_addr,
4239   dw_val_class_offset,
4240   dw_val_class_loc,
4241   dw_val_class_loc_list,
4242   dw_val_class_range_list,
4243   dw_val_class_const,
4244   dw_val_class_unsigned_const,
4245   dw_val_class_const_double,
4246   dw_val_class_vec,
4247   dw_val_class_flag,
4248   dw_val_class_die_ref,
4249   dw_val_class_fde_ref,
4250   dw_val_class_lbl_id,
4251   dw_val_class_lineptr,
4252   dw_val_class_str,
4253   dw_val_class_macptr,
4254   dw_val_class_file,
4255   dw_val_class_data8,
4256   dw_val_class_vms_delta
4257 };
4258
4259 /* Describe a floating point constant value, or a vector constant value.  */
4260
4261 typedef struct GTY(()) dw_vec_struct {
4262   unsigned char * GTY((length ("%h.length"))) array;
4263   unsigned length;
4264   unsigned elt_size;
4265 }
4266 dw_vec_const;
4267
4268 /* The dw_val_node describes an attribute's value, as it is
4269    represented internally.  */
4270
4271 typedef struct GTY(()) dw_val_struct {
4272   enum dw_val_class val_class;
4273   union dw_val_struct_union
4274     {
4275       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4276       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4277       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4278       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4279       HOST_WIDE_INT GTY ((default)) val_int;
4280       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4281       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4282       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4283       struct dw_val_die_union
4284         {
4285           dw_die_ref die;
4286           int external;
4287         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4288       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4289       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4290       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4291       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4292       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4293       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4294       struct dw_val_vms_delta_union
4295         {
4296           char * lbl1;
4297           char * lbl2;
4298         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4299     }
4300   GTY ((desc ("%1.val_class"))) v;
4301 }
4302 dw_val_node;
4303
4304 /* Locations in memory are described using a sequence of stack machine
4305    operations.  */
4306
4307 typedef struct GTY(()) dw_loc_descr_struct {
4308   dw_loc_descr_ref dw_loc_next;
4309   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4310   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4311      from DW_OP_addr with a dtp-relative symbol relocation.  */
4312   unsigned int dtprel : 1;
4313   int dw_loc_addr;
4314   dw_val_node dw_loc_oprnd1;
4315   dw_val_node dw_loc_oprnd2;
4316 }
4317 dw_loc_descr_node;
4318
4319 /* Location lists are ranges + location descriptions for that range,
4320    so you can track variables that are in different places over
4321    their entire life.  */
4322 typedef struct GTY(()) dw_loc_list_struct {
4323   dw_loc_list_ref dw_loc_next;
4324   const char *begin; /* Label for begin address of range */
4325   const char *end;  /* Label for end address of range */
4326   char *ll_symbol; /* Label for beginning of location list.
4327                       Only on head of list */
4328   const char *section; /* Section this loclist is relative to */
4329   dw_loc_descr_ref expr;
4330 } dw_loc_list_node;
4331
4332 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4333
4334 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4335
4336 /* Convert a DWARF stack opcode into its string name.  */
4337
4338 static const char *
4339 dwarf_stack_op_name (unsigned int op)
4340 {
4341   switch (op)
4342     {
4343     case DW_OP_addr:
4344       return "DW_OP_addr";
4345     case DW_OP_deref:
4346       return "DW_OP_deref";
4347     case DW_OP_const1u:
4348       return "DW_OP_const1u";
4349     case DW_OP_const1s:
4350       return "DW_OP_const1s";
4351     case DW_OP_const2u:
4352       return "DW_OP_const2u";
4353     case DW_OP_const2s:
4354       return "DW_OP_const2s";
4355     case DW_OP_const4u:
4356       return "DW_OP_const4u";
4357     case DW_OP_const4s:
4358       return "DW_OP_const4s";
4359     case DW_OP_const8u:
4360       return "DW_OP_const8u";
4361     case DW_OP_const8s:
4362       return "DW_OP_const8s";
4363     case DW_OP_constu:
4364       return "DW_OP_constu";
4365     case DW_OP_consts:
4366       return "DW_OP_consts";
4367     case DW_OP_dup:
4368       return "DW_OP_dup";
4369     case DW_OP_drop:
4370       return "DW_OP_drop";
4371     case DW_OP_over:
4372       return "DW_OP_over";
4373     case DW_OP_pick:
4374       return "DW_OP_pick";
4375     case DW_OP_swap:
4376       return "DW_OP_swap";
4377     case DW_OP_rot:
4378       return "DW_OP_rot";
4379     case DW_OP_xderef:
4380       return "DW_OP_xderef";
4381     case DW_OP_abs:
4382       return "DW_OP_abs";
4383     case DW_OP_and:
4384       return "DW_OP_and";
4385     case DW_OP_div:
4386       return "DW_OP_div";
4387     case DW_OP_minus:
4388       return "DW_OP_minus";
4389     case DW_OP_mod:
4390       return "DW_OP_mod";
4391     case DW_OP_mul:
4392       return "DW_OP_mul";
4393     case DW_OP_neg:
4394       return "DW_OP_neg";
4395     case DW_OP_not:
4396       return "DW_OP_not";
4397     case DW_OP_or:
4398       return "DW_OP_or";
4399     case DW_OP_plus:
4400       return "DW_OP_plus";
4401     case DW_OP_plus_uconst:
4402       return "DW_OP_plus_uconst";
4403     case DW_OP_shl:
4404       return "DW_OP_shl";
4405     case DW_OP_shr:
4406       return "DW_OP_shr";
4407     case DW_OP_shra:
4408       return "DW_OP_shra";
4409     case DW_OP_xor:
4410       return "DW_OP_xor";
4411     case DW_OP_bra:
4412       return "DW_OP_bra";
4413     case DW_OP_eq:
4414       return "DW_OP_eq";
4415     case DW_OP_ge:
4416       return "DW_OP_ge";
4417     case DW_OP_gt:
4418       return "DW_OP_gt";
4419     case DW_OP_le:
4420       return "DW_OP_le";
4421     case DW_OP_lt:
4422       return "DW_OP_lt";
4423     case DW_OP_ne:
4424       return "DW_OP_ne";
4425     case DW_OP_skip:
4426       return "DW_OP_skip";
4427     case DW_OP_lit0:
4428       return "DW_OP_lit0";
4429     case DW_OP_lit1:
4430       return "DW_OP_lit1";
4431     case DW_OP_lit2:
4432       return "DW_OP_lit2";
4433     case DW_OP_lit3:
4434       return "DW_OP_lit3";
4435     case DW_OP_lit4:
4436       return "DW_OP_lit4";
4437     case DW_OP_lit5:
4438       return "DW_OP_lit5";
4439     case DW_OP_lit6:
4440       return "DW_OP_lit6";
4441     case DW_OP_lit7:
4442       return "DW_OP_lit7";
4443     case DW_OP_lit8:
4444       return "DW_OP_lit8";
4445     case DW_OP_lit9:
4446       return "DW_OP_lit9";
4447     case DW_OP_lit10:
4448       return "DW_OP_lit10";
4449     case DW_OP_lit11:
4450       return "DW_OP_lit11";
4451     case DW_OP_lit12:
4452       return "DW_OP_lit12";
4453     case DW_OP_lit13:
4454       return "DW_OP_lit13";
4455     case DW_OP_lit14:
4456       return "DW_OP_lit14";
4457     case DW_OP_lit15:
4458       return "DW_OP_lit15";
4459     case DW_OP_lit16:
4460       return "DW_OP_lit16";
4461     case DW_OP_lit17:
4462       return "DW_OP_lit17";
4463     case DW_OP_lit18:
4464       return "DW_OP_lit18";
4465     case DW_OP_lit19:
4466       return "DW_OP_lit19";
4467     case DW_OP_lit20:
4468       return "DW_OP_lit20";
4469     case DW_OP_lit21:
4470       return "DW_OP_lit21";
4471     case DW_OP_lit22:
4472       return "DW_OP_lit22";
4473     case DW_OP_lit23:
4474       return "DW_OP_lit23";
4475     case DW_OP_lit24:
4476       return "DW_OP_lit24";
4477     case DW_OP_lit25:
4478       return "DW_OP_lit25";
4479     case DW_OP_lit26:
4480       return "DW_OP_lit26";
4481     case DW_OP_lit27:
4482       return "DW_OP_lit27";
4483     case DW_OP_lit28:
4484       return "DW_OP_lit28";
4485     case DW_OP_lit29:
4486       return "DW_OP_lit29";
4487     case DW_OP_lit30:
4488       return "DW_OP_lit30";
4489     case DW_OP_lit31:
4490       return "DW_OP_lit31";
4491     case DW_OP_reg0:
4492       return "DW_OP_reg0";
4493     case DW_OP_reg1:
4494       return "DW_OP_reg1";
4495     case DW_OP_reg2:
4496       return "DW_OP_reg2";
4497     case DW_OP_reg3:
4498       return "DW_OP_reg3";
4499     case DW_OP_reg4:
4500       return "DW_OP_reg4";
4501     case DW_OP_reg5:
4502       return "DW_OP_reg5";
4503     case DW_OP_reg6:
4504       return "DW_OP_reg6";
4505     case DW_OP_reg7:
4506       return "DW_OP_reg7";
4507     case DW_OP_reg8:
4508       return "DW_OP_reg8";
4509     case DW_OP_reg9:
4510       return "DW_OP_reg9";
4511     case DW_OP_reg10:
4512       return "DW_OP_reg10";
4513     case DW_OP_reg11:
4514       return "DW_OP_reg11";
4515     case DW_OP_reg12:
4516       return "DW_OP_reg12";
4517     case DW_OP_reg13:
4518       return "DW_OP_reg13";
4519     case DW_OP_reg14:
4520       return "DW_OP_reg14";
4521     case DW_OP_reg15:
4522       return "DW_OP_reg15";
4523     case DW_OP_reg16:
4524       return "DW_OP_reg16";
4525     case DW_OP_reg17:
4526       return "DW_OP_reg17";
4527     case DW_OP_reg18:
4528       return "DW_OP_reg18";
4529     case DW_OP_reg19:
4530       return "DW_OP_reg19";
4531     case DW_OP_reg20:
4532       return "DW_OP_reg20";
4533     case DW_OP_reg21:
4534       return "DW_OP_reg21";
4535     case DW_OP_reg22:
4536       return "DW_OP_reg22";
4537     case DW_OP_reg23:
4538       return "DW_OP_reg23";
4539     case DW_OP_reg24:
4540       return "DW_OP_reg24";
4541     case DW_OP_reg25:
4542       return "DW_OP_reg25";
4543     case DW_OP_reg26:
4544       return "DW_OP_reg26";
4545     case DW_OP_reg27:
4546       return "DW_OP_reg27";
4547     case DW_OP_reg28:
4548       return "DW_OP_reg28";
4549     case DW_OP_reg29:
4550       return "DW_OP_reg29";
4551     case DW_OP_reg30:
4552       return "DW_OP_reg30";
4553     case DW_OP_reg31:
4554       return "DW_OP_reg31";
4555     case DW_OP_breg0:
4556       return "DW_OP_breg0";
4557     case DW_OP_breg1:
4558       return "DW_OP_breg1";
4559     case DW_OP_breg2:
4560       return "DW_OP_breg2";
4561     case DW_OP_breg3:
4562       return "DW_OP_breg3";
4563     case DW_OP_breg4:
4564       return "DW_OP_breg4";
4565     case DW_OP_breg5:
4566       return "DW_OP_breg5";
4567     case DW_OP_breg6:
4568       return "DW_OP_breg6";
4569     case DW_OP_breg7:
4570       return "DW_OP_breg7";
4571     case DW_OP_breg8:
4572       return "DW_OP_breg8";
4573     case DW_OP_breg9:
4574       return "DW_OP_breg9";
4575     case DW_OP_breg10:
4576       return "DW_OP_breg10";
4577     case DW_OP_breg11:
4578       return "DW_OP_breg11";
4579     case DW_OP_breg12:
4580       return "DW_OP_breg12";
4581     case DW_OP_breg13:
4582       return "DW_OP_breg13";
4583     case DW_OP_breg14:
4584       return "DW_OP_breg14";
4585     case DW_OP_breg15:
4586       return "DW_OP_breg15";
4587     case DW_OP_breg16:
4588       return "DW_OP_breg16";
4589     case DW_OP_breg17:
4590       return "DW_OP_breg17";
4591     case DW_OP_breg18:
4592       return "DW_OP_breg18";
4593     case DW_OP_breg19:
4594       return "DW_OP_breg19";
4595     case DW_OP_breg20:
4596       return "DW_OP_breg20";
4597     case DW_OP_breg21:
4598       return "DW_OP_breg21";
4599     case DW_OP_breg22:
4600       return "DW_OP_breg22";
4601     case DW_OP_breg23:
4602       return "DW_OP_breg23";
4603     case DW_OP_breg24:
4604       return "DW_OP_breg24";
4605     case DW_OP_breg25:
4606       return "DW_OP_breg25";
4607     case DW_OP_breg26:
4608       return "DW_OP_breg26";
4609     case DW_OP_breg27:
4610       return "DW_OP_breg27";
4611     case DW_OP_breg28:
4612       return "DW_OP_breg28";
4613     case DW_OP_breg29:
4614       return "DW_OP_breg29";
4615     case DW_OP_breg30:
4616       return "DW_OP_breg30";
4617     case DW_OP_breg31:
4618       return "DW_OP_breg31";
4619     case DW_OP_regx:
4620       return "DW_OP_regx";
4621     case DW_OP_fbreg:
4622       return "DW_OP_fbreg";
4623     case DW_OP_bregx:
4624       return "DW_OP_bregx";
4625     case DW_OP_piece:
4626       return "DW_OP_piece";
4627     case DW_OP_deref_size:
4628       return "DW_OP_deref_size";
4629     case DW_OP_xderef_size:
4630       return "DW_OP_xderef_size";
4631     case DW_OP_nop:
4632       return "DW_OP_nop";
4633
4634     case DW_OP_push_object_address:
4635       return "DW_OP_push_object_address";
4636     case DW_OP_call2:
4637       return "DW_OP_call2";
4638     case DW_OP_call4:
4639       return "DW_OP_call4";
4640     case DW_OP_call_ref:
4641       return "DW_OP_call_ref";
4642     case DW_OP_implicit_value:
4643       return "DW_OP_implicit_value";
4644     case DW_OP_stack_value:
4645       return "DW_OP_stack_value";
4646     case DW_OP_form_tls_address:
4647       return "DW_OP_form_tls_address";
4648     case DW_OP_call_frame_cfa:
4649       return "DW_OP_call_frame_cfa";
4650     case DW_OP_bit_piece:
4651       return "DW_OP_bit_piece";
4652
4653     case DW_OP_GNU_push_tls_address:
4654       return "DW_OP_GNU_push_tls_address";
4655     case DW_OP_GNU_uninit:
4656       return "DW_OP_GNU_uninit";
4657     case DW_OP_GNU_encoded_addr:
4658       return "DW_OP_GNU_encoded_addr";
4659
4660     default:
4661       return "OP_<unknown>";
4662     }
4663 }
4664
4665 /* Return a pointer to a newly allocated location description.  Location
4666    descriptions are simple expression terms that can be strung
4667    together to form more complicated location (address) descriptions.  */
4668
4669 static inline dw_loc_descr_ref
4670 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4671                unsigned HOST_WIDE_INT oprnd2)
4672 {
4673   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4674
4675   descr->dw_loc_opc = op;
4676   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4677   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4678   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4679   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4680
4681   return descr;
4682 }
4683
4684 /* Return a pointer to a newly allocated location description for
4685    REG and OFFSET.  */
4686
4687 static inline dw_loc_descr_ref
4688 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4689 {
4690   if (reg <= 31)
4691     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4692                           offset, 0);
4693   else
4694     return new_loc_descr (DW_OP_bregx, reg, offset);
4695 }
4696
4697 /* Add a location description term to a location description expression.  */
4698
4699 static inline void
4700 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4701 {
4702   dw_loc_descr_ref *d;
4703
4704   /* Find the end of the chain.  */
4705   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4706     ;
4707
4708   *d = descr;
4709 }
4710
4711 /* Add a constant OFFSET to a location expression.  */
4712
4713 static void
4714 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4715 {
4716   dw_loc_descr_ref loc;
4717   HOST_WIDE_INT *p;
4718
4719   gcc_assert (*list_head != NULL);
4720
4721   if (!offset)
4722     return;
4723
4724   /* Find the end of the chain.  */
4725   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4726     ;
4727
4728   p = NULL;
4729   if (loc->dw_loc_opc == DW_OP_fbreg
4730       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4731     p = &loc->dw_loc_oprnd1.v.val_int;
4732   else if (loc->dw_loc_opc == DW_OP_bregx)
4733     p = &loc->dw_loc_oprnd2.v.val_int;
4734
4735   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4736      offset.  Don't optimize if an signed integer overflow would happen.  */
4737   if (p != NULL
4738       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4739           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4740     *p += offset;
4741
4742   else if (offset > 0)
4743     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4744
4745   else
4746     {
4747       loc->dw_loc_next = int_loc_descriptor (-offset);
4748       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4749     }
4750 }
4751
4752 #ifdef DWARF2_DEBUGGING_INFO
4753 /* Add a constant OFFSET to a location list.  */
4754
4755 static void
4756 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4757 {
4758   dw_loc_list_ref d;
4759   for (d = list_head; d != NULL; d = d->dw_loc_next)
4760     loc_descr_plus_const (&d->expr, offset);
4761 }
4762 #endif
4763
4764 /* Return the size of a location descriptor.  */
4765
4766 static unsigned long
4767 size_of_loc_descr (dw_loc_descr_ref loc)
4768 {
4769   unsigned long size = 1;
4770
4771   switch (loc->dw_loc_opc)
4772     {
4773     case DW_OP_addr:
4774       size += DWARF2_ADDR_SIZE;
4775       break;
4776     case DW_OP_const1u:
4777     case DW_OP_const1s:
4778       size += 1;
4779       break;
4780     case DW_OP_const2u:
4781     case DW_OP_const2s:
4782       size += 2;
4783       break;
4784     case DW_OP_const4u:
4785     case DW_OP_const4s:
4786       size += 4;
4787       break;
4788     case DW_OP_const8u:
4789     case DW_OP_const8s:
4790       size += 8;
4791       break;
4792     case DW_OP_constu:
4793       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4794       break;
4795     case DW_OP_consts:
4796       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4797       break;
4798     case DW_OP_pick:
4799       size += 1;
4800       break;
4801     case DW_OP_plus_uconst:
4802       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4803       break;
4804     case DW_OP_skip:
4805     case DW_OP_bra:
4806       size += 2;
4807       break;
4808     case DW_OP_breg0:
4809     case DW_OP_breg1:
4810     case DW_OP_breg2:
4811     case DW_OP_breg3:
4812     case DW_OP_breg4:
4813     case DW_OP_breg5:
4814     case DW_OP_breg6:
4815     case DW_OP_breg7:
4816     case DW_OP_breg8:
4817     case DW_OP_breg9:
4818     case DW_OP_breg10:
4819     case DW_OP_breg11:
4820     case DW_OP_breg12:
4821     case DW_OP_breg13:
4822     case DW_OP_breg14:
4823     case DW_OP_breg15:
4824     case DW_OP_breg16:
4825     case DW_OP_breg17:
4826     case DW_OP_breg18:
4827     case DW_OP_breg19:
4828     case DW_OP_breg20:
4829     case DW_OP_breg21:
4830     case DW_OP_breg22:
4831     case DW_OP_breg23:
4832     case DW_OP_breg24:
4833     case DW_OP_breg25:
4834     case DW_OP_breg26:
4835     case DW_OP_breg27:
4836     case DW_OP_breg28:
4837     case DW_OP_breg29:
4838     case DW_OP_breg30:
4839     case DW_OP_breg31:
4840       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4841       break;
4842     case DW_OP_regx:
4843       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4844       break;
4845     case DW_OP_fbreg:
4846       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4847       break;
4848     case DW_OP_bregx:
4849       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4850       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4851       break;
4852     case DW_OP_piece:
4853       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4854       break;
4855     case DW_OP_bit_piece:
4856       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4857       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4858       break;
4859     case DW_OP_deref_size:
4860     case DW_OP_xderef_size:
4861       size += 1;
4862       break;
4863     case DW_OP_call2:
4864       size += 2;
4865       break;
4866     case DW_OP_call4:
4867       size += 4;
4868       break;
4869     case DW_OP_call_ref:
4870       size += DWARF2_ADDR_SIZE;
4871       break;
4872     case DW_OP_implicit_value:
4873       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4874               + loc->dw_loc_oprnd1.v.val_unsigned;
4875       break;
4876     default:
4877       break;
4878     }
4879
4880   return size;
4881 }
4882
4883 /* Return the size of a series of location descriptors.  */
4884
4885 static unsigned long
4886 size_of_locs (dw_loc_descr_ref loc)
4887 {
4888   dw_loc_descr_ref l;
4889   unsigned long size;
4890
4891   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4892      field, to avoid writing to a PCH file.  */
4893   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4894     {
4895       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4896         break;
4897       size += size_of_loc_descr (l);
4898     }
4899   if (! l)
4900     return size;
4901
4902   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4903     {
4904       l->dw_loc_addr = size;
4905       size += size_of_loc_descr (l);
4906     }
4907
4908   return size;
4909 }
4910
4911 #ifdef DWARF2_DEBUGGING_INFO
4912 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4913 #endif
4914
4915 /* Output location description stack opcode's operands (if any).  */
4916
4917 static void
4918 output_loc_operands (dw_loc_descr_ref loc)
4919 {
4920   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4921   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4922
4923   switch (loc->dw_loc_opc)
4924     {
4925 #ifdef DWARF2_DEBUGGING_INFO
4926     case DW_OP_const2u:
4927     case DW_OP_const2s:
4928       dw2_asm_output_data (2, val1->v.val_int, NULL);
4929       break;
4930     case DW_OP_const4u:
4931     case DW_OP_const4s:
4932       dw2_asm_output_data (4, val1->v.val_int, NULL);
4933       break;
4934     case DW_OP_const8u:
4935     case DW_OP_const8s:
4936       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4937       dw2_asm_output_data (8, val1->v.val_int, NULL);
4938       break;
4939     case DW_OP_skip:
4940     case DW_OP_bra:
4941       {
4942         int offset;
4943
4944         gcc_assert (val1->val_class == dw_val_class_loc);
4945         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4946
4947         dw2_asm_output_data (2, offset, NULL);
4948       }
4949       break;
4950     case DW_OP_implicit_value:
4951       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4952       switch (val2->val_class)
4953         {
4954         case dw_val_class_const:
4955           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4956           break;
4957         case dw_val_class_vec:
4958           {
4959             unsigned int elt_size = val2->v.val_vec.elt_size;
4960             unsigned int len = val2->v.val_vec.length;
4961             unsigned int i;
4962             unsigned char *p;
4963
4964             if (elt_size > sizeof (HOST_WIDE_INT))
4965               {
4966                 elt_size /= 2;
4967                 len *= 2;
4968               }
4969             for (i = 0, p = val2->v.val_vec.array;
4970                  i < len;
4971                  i++, p += elt_size)
4972               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4973                                    "fp or vector constant word %u", i);
4974           }
4975           break;
4976         case dw_val_class_const_double:
4977           {
4978             unsigned HOST_WIDE_INT first, second;
4979
4980             if (WORDS_BIG_ENDIAN)
4981               {
4982                 first = val2->v.val_double.high;
4983                 second = val2->v.val_double.low;
4984               }
4985             else
4986               {
4987                 first = val2->v.val_double.low;
4988                 second = val2->v.val_double.high;
4989               }
4990             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4991                                  first, NULL);
4992             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4993                                  second, NULL);
4994           }
4995           break;
4996         case dw_val_class_addr:
4997           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4998           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4999           break;
5000         default:
5001           gcc_unreachable ();
5002         }
5003       break;
5004 #else
5005     case DW_OP_const2u:
5006     case DW_OP_const2s:
5007     case DW_OP_const4u:
5008     case DW_OP_const4s:
5009     case DW_OP_const8u:
5010     case DW_OP_const8s:
5011     case DW_OP_skip:
5012     case DW_OP_bra:
5013     case DW_OP_implicit_value:
5014       /* We currently don't make any attempt to make sure these are
5015          aligned properly like we do for the main unwind info, so
5016          don't support emitting things larger than a byte if we're
5017          only doing unwinding.  */
5018       gcc_unreachable ();
5019 #endif
5020     case DW_OP_const1u:
5021     case DW_OP_const1s:
5022       dw2_asm_output_data (1, val1->v.val_int, NULL);
5023       break;
5024     case DW_OP_constu:
5025       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5026       break;
5027     case DW_OP_consts:
5028       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5029       break;
5030     case DW_OP_pick:
5031       dw2_asm_output_data (1, val1->v.val_int, NULL);
5032       break;
5033     case DW_OP_plus_uconst:
5034       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5035       break;
5036     case DW_OP_breg0:
5037     case DW_OP_breg1:
5038     case DW_OP_breg2:
5039     case DW_OP_breg3:
5040     case DW_OP_breg4:
5041     case DW_OP_breg5:
5042     case DW_OP_breg6:
5043     case DW_OP_breg7:
5044     case DW_OP_breg8:
5045     case DW_OP_breg9:
5046     case DW_OP_breg10:
5047     case DW_OP_breg11:
5048     case DW_OP_breg12:
5049     case DW_OP_breg13:
5050     case DW_OP_breg14:
5051     case DW_OP_breg15:
5052     case DW_OP_breg16:
5053     case DW_OP_breg17:
5054     case DW_OP_breg18:
5055     case DW_OP_breg19:
5056     case DW_OP_breg20:
5057     case DW_OP_breg21:
5058     case DW_OP_breg22:
5059     case DW_OP_breg23:
5060     case DW_OP_breg24:
5061     case DW_OP_breg25:
5062     case DW_OP_breg26:
5063     case DW_OP_breg27:
5064     case DW_OP_breg28:
5065     case DW_OP_breg29:
5066     case DW_OP_breg30:
5067     case DW_OP_breg31:
5068       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5069       break;
5070     case DW_OP_regx:
5071       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5072       break;
5073     case DW_OP_fbreg:
5074       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5075       break;
5076     case DW_OP_bregx:
5077       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5078       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5079       break;
5080     case DW_OP_piece:
5081       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5082       break;
5083     case DW_OP_bit_piece:
5084       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5085       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5086       break;
5087     case DW_OP_deref_size:
5088     case DW_OP_xderef_size:
5089       dw2_asm_output_data (1, val1->v.val_int, NULL);
5090       break;
5091
5092     case DW_OP_addr:
5093       if (loc->dtprel)
5094         {
5095           if (targetm.asm_out.output_dwarf_dtprel)
5096             {
5097               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5098                                                    DWARF2_ADDR_SIZE,
5099                                                    val1->v.val_addr);
5100               fputc ('\n', asm_out_file);
5101             }
5102           else
5103             gcc_unreachable ();
5104         }
5105       else
5106         {
5107 #ifdef DWARF2_DEBUGGING_INFO
5108           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5109 #else
5110           gcc_unreachable ();
5111 #endif
5112         }
5113       break;
5114
5115     default:
5116       /* Other codes have no operands.  */
5117       break;
5118     }
5119 }
5120
5121 /* Output a sequence of location operations.  */
5122
5123 static void
5124 output_loc_sequence (dw_loc_descr_ref loc)
5125 {
5126   for (; loc != NULL; loc = loc->dw_loc_next)
5127     {
5128       /* Output the opcode.  */
5129       dw2_asm_output_data (1, loc->dw_loc_opc,
5130                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5131
5132       /* Output the operand(s) (if any).  */
5133       output_loc_operands (loc);
5134     }
5135 }
5136
5137 /* Output location description stack opcode's operands (if any).
5138    The output is single bytes on a line, suitable for .cfi_escape.  */
5139
5140 static void
5141 output_loc_operands_raw (dw_loc_descr_ref loc)
5142 {
5143   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5144   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5145
5146   switch (loc->dw_loc_opc)
5147     {
5148     case DW_OP_addr:
5149     case DW_OP_implicit_value:
5150       /* We cannot output addresses in .cfi_escape, only bytes.  */
5151       gcc_unreachable ();
5152
5153     case DW_OP_const1u:
5154     case DW_OP_const1s:
5155     case DW_OP_pick:
5156     case DW_OP_deref_size:
5157     case DW_OP_xderef_size:
5158       fputc (',', asm_out_file);
5159       dw2_asm_output_data_raw (1, val1->v.val_int);
5160       break;
5161
5162     case DW_OP_const2u:
5163     case DW_OP_const2s:
5164       fputc (',', asm_out_file);
5165       dw2_asm_output_data_raw (2, val1->v.val_int);
5166       break;
5167
5168     case DW_OP_const4u:
5169     case DW_OP_const4s:
5170       fputc (',', asm_out_file);
5171       dw2_asm_output_data_raw (4, val1->v.val_int);
5172       break;
5173
5174     case DW_OP_const8u:
5175     case DW_OP_const8s:
5176       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5177       fputc (',', asm_out_file);
5178       dw2_asm_output_data_raw (8, val1->v.val_int);
5179       break;
5180
5181     case DW_OP_skip:
5182     case DW_OP_bra:
5183       {
5184         int offset;
5185
5186         gcc_assert (val1->val_class == dw_val_class_loc);
5187         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5188
5189         fputc (',', asm_out_file);
5190         dw2_asm_output_data_raw (2, offset);
5191       }
5192       break;
5193
5194     case DW_OP_constu:
5195     case DW_OP_plus_uconst:
5196     case DW_OP_regx:
5197     case DW_OP_piece:
5198       fputc (',', asm_out_file);
5199       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5200       break;
5201
5202     case DW_OP_bit_piece:
5203       fputc (',', asm_out_file);
5204       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5205       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5206       break;
5207
5208     case DW_OP_consts:
5209     case DW_OP_breg0:
5210     case DW_OP_breg1:
5211     case DW_OP_breg2:
5212     case DW_OP_breg3:
5213     case DW_OP_breg4:
5214     case DW_OP_breg5:
5215     case DW_OP_breg6:
5216     case DW_OP_breg7:
5217     case DW_OP_breg8:
5218     case DW_OP_breg9:
5219     case DW_OP_breg10:
5220     case DW_OP_breg11:
5221     case DW_OP_breg12:
5222     case DW_OP_breg13:
5223     case DW_OP_breg14:
5224     case DW_OP_breg15:
5225     case DW_OP_breg16:
5226     case DW_OP_breg17:
5227     case DW_OP_breg18:
5228     case DW_OP_breg19:
5229     case DW_OP_breg20:
5230     case DW_OP_breg21:
5231     case DW_OP_breg22:
5232     case DW_OP_breg23:
5233     case DW_OP_breg24:
5234     case DW_OP_breg25:
5235     case DW_OP_breg26:
5236     case DW_OP_breg27:
5237     case DW_OP_breg28:
5238     case DW_OP_breg29:
5239     case DW_OP_breg30:
5240     case DW_OP_breg31:
5241     case DW_OP_fbreg:
5242       fputc (',', asm_out_file);
5243       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5244       break;
5245
5246     case DW_OP_bregx:
5247       fputc (',', asm_out_file);
5248       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5249       fputc (',', asm_out_file);
5250       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5251       break;
5252
5253     default:
5254       /* Other codes have no operands.  */
5255       break;
5256     }
5257 }
5258
5259 static void
5260 output_loc_sequence_raw (dw_loc_descr_ref loc)
5261 {
5262   while (1)
5263     {
5264       /* Output the opcode.  */
5265       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5266       output_loc_operands_raw (loc);
5267
5268       if (!loc->dw_loc_next)
5269         break;
5270       loc = loc->dw_loc_next;
5271
5272       fputc (',', asm_out_file);
5273     }
5274 }
5275
5276 /* This routine will generate the correct assembly data for a location
5277    description based on a cfi entry with a complex address.  */
5278
5279 static void
5280 output_cfa_loc (dw_cfi_ref cfi)
5281 {
5282   dw_loc_descr_ref loc;
5283   unsigned long size;
5284
5285   if (cfi->dw_cfi_opc == DW_CFA_expression)
5286     {
5287       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5288       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5289     }
5290   else
5291     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5292
5293   /* Output the size of the block.  */
5294   size = size_of_locs (loc);
5295   dw2_asm_output_data_uleb128 (size, NULL);
5296
5297   /* Now output the operations themselves.  */
5298   output_loc_sequence (loc);
5299 }
5300
5301 /* Similar, but used for .cfi_escape.  */
5302
5303 static void
5304 output_cfa_loc_raw (dw_cfi_ref cfi)
5305 {
5306   dw_loc_descr_ref loc;
5307   unsigned long size;
5308
5309   if (cfi->dw_cfi_opc == DW_CFA_expression)
5310     {
5311       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5312       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5313     }
5314   else
5315     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5316
5317   /* Output the size of the block.  */
5318   size = size_of_locs (loc);
5319   dw2_asm_output_data_uleb128_raw (size);
5320   fputc (',', asm_out_file);
5321
5322   /* Now output the operations themselves.  */
5323   output_loc_sequence_raw (loc);
5324 }
5325
5326 /* This function builds a dwarf location descriptor sequence from a
5327    dw_cfa_location, adding the given OFFSET to the result of the
5328    expression.  */
5329
5330 static struct dw_loc_descr_struct *
5331 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5332 {
5333   struct dw_loc_descr_struct *head, *tmp;
5334
5335   offset += cfa->offset;
5336
5337   if (cfa->indirect)
5338     {
5339       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5340       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5341       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5342       add_loc_descr (&head, tmp);
5343       if (offset != 0)
5344         {
5345           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5346           add_loc_descr (&head, tmp);
5347         }
5348     }
5349   else
5350     head = new_reg_loc_descr (cfa->reg, offset);
5351
5352   return head;
5353 }
5354
5355 /* This function builds a dwarf location descriptor sequence for
5356    the address at OFFSET from the CFA when stack is aligned to
5357    ALIGNMENT byte.  */
5358
5359 static struct dw_loc_descr_struct *
5360 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5361 {
5362   struct dw_loc_descr_struct *head;
5363   unsigned int dwarf_fp
5364     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5365
5366  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5367   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5368     {
5369       head = new_reg_loc_descr (dwarf_fp, 0);
5370       add_loc_descr (&head, int_loc_descriptor (alignment));
5371       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5372       loc_descr_plus_const (&head, offset);
5373     }
5374   else
5375     head = new_reg_loc_descr (dwarf_fp, offset);
5376   return head;
5377 }
5378
5379 /* This function fills in aa dw_cfa_location structure from a dwarf location
5380    descriptor sequence.  */
5381
5382 static void
5383 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5384 {
5385   struct dw_loc_descr_struct *ptr;
5386   cfa->offset = 0;
5387   cfa->base_offset = 0;
5388   cfa->indirect = 0;
5389   cfa->reg = -1;
5390
5391   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5392     {
5393       enum dwarf_location_atom op = ptr->dw_loc_opc;
5394
5395       switch (op)
5396         {
5397         case DW_OP_reg0:
5398         case DW_OP_reg1:
5399         case DW_OP_reg2:
5400         case DW_OP_reg3:
5401         case DW_OP_reg4:
5402         case DW_OP_reg5:
5403         case DW_OP_reg6:
5404         case DW_OP_reg7:
5405         case DW_OP_reg8:
5406         case DW_OP_reg9:
5407         case DW_OP_reg10:
5408         case DW_OP_reg11:
5409         case DW_OP_reg12:
5410         case DW_OP_reg13:
5411         case DW_OP_reg14:
5412         case DW_OP_reg15:
5413         case DW_OP_reg16:
5414         case DW_OP_reg17:
5415         case DW_OP_reg18:
5416         case DW_OP_reg19:
5417         case DW_OP_reg20:
5418         case DW_OP_reg21:
5419         case DW_OP_reg22:
5420         case DW_OP_reg23:
5421         case DW_OP_reg24:
5422         case DW_OP_reg25:
5423         case DW_OP_reg26:
5424         case DW_OP_reg27:
5425         case DW_OP_reg28:
5426         case DW_OP_reg29:
5427         case DW_OP_reg30:
5428         case DW_OP_reg31:
5429           cfa->reg = op - DW_OP_reg0;
5430           break;
5431         case DW_OP_regx:
5432           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5433           break;
5434         case DW_OP_breg0:
5435         case DW_OP_breg1:
5436         case DW_OP_breg2:
5437         case DW_OP_breg3:
5438         case DW_OP_breg4:
5439         case DW_OP_breg5:
5440         case DW_OP_breg6:
5441         case DW_OP_breg7:
5442         case DW_OP_breg8:
5443         case DW_OP_breg9:
5444         case DW_OP_breg10:
5445         case DW_OP_breg11:
5446         case DW_OP_breg12:
5447         case DW_OP_breg13:
5448         case DW_OP_breg14:
5449         case DW_OP_breg15:
5450         case DW_OP_breg16:
5451         case DW_OP_breg17:
5452         case DW_OP_breg18:
5453         case DW_OP_breg19:
5454         case DW_OP_breg20:
5455         case DW_OP_breg21:
5456         case DW_OP_breg22:
5457         case DW_OP_breg23:
5458         case DW_OP_breg24:
5459         case DW_OP_breg25:
5460         case DW_OP_breg26:
5461         case DW_OP_breg27:
5462         case DW_OP_breg28:
5463         case DW_OP_breg29:
5464         case DW_OP_breg30:
5465         case DW_OP_breg31:
5466           cfa->reg = op - DW_OP_breg0;
5467           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5468           break;
5469         case DW_OP_bregx:
5470           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5471           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5472           break;
5473         case DW_OP_deref:
5474           cfa->indirect = 1;
5475           break;
5476         case DW_OP_plus_uconst:
5477           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5478           break;
5479         default:
5480           internal_error ("DW_LOC_OP %s not implemented",
5481                           dwarf_stack_op_name (ptr->dw_loc_opc));
5482         }
5483     }
5484 }
5485 #endif /* .debug_frame support */
5486 \f
5487 /* And now, the support for symbolic debugging information.  */
5488 #ifdef DWARF2_DEBUGGING_INFO
5489
5490 /* .debug_str support.  */
5491 static int output_indirect_string (void **, void *);
5492
5493 static void dwarf2out_init (const char *);
5494 static void dwarf2out_finish (const char *);
5495 static void dwarf2out_assembly_start (void);
5496 static void dwarf2out_define (unsigned int, const char *);
5497 static void dwarf2out_undef (unsigned int, const char *);
5498 static void dwarf2out_start_source_file (unsigned, const char *);
5499 static void dwarf2out_end_source_file (unsigned);
5500 static void dwarf2out_function_decl (tree);
5501 static void dwarf2out_begin_block (unsigned, unsigned);
5502 static void dwarf2out_end_block (unsigned, unsigned);
5503 static bool dwarf2out_ignore_block (const_tree);
5504 static void dwarf2out_global_decl (tree);
5505 static void dwarf2out_type_decl (tree, int);
5506 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5507 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5508                                                  dw_die_ref);
5509 static void dwarf2out_abstract_function (tree);
5510 static void dwarf2out_var_location (rtx);
5511 static void dwarf2out_direct_call (tree);
5512 static void dwarf2out_virtual_call_token (tree, int);
5513 static void dwarf2out_copy_call_info (rtx, rtx);
5514 static void dwarf2out_virtual_call (int);
5515 static void dwarf2out_begin_function (tree);
5516 static void dwarf2out_set_name (tree, tree);
5517
5518 /* The debug hooks structure.  */
5519
5520 const struct gcc_debug_hooks dwarf2_debug_hooks =
5521 {
5522   dwarf2out_init,
5523   dwarf2out_finish,
5524   dwarf2out_assembly_start,
5525   dwarf2out_define,
5526   dwarf2out_undef,
5527   dwarf2out_start_source_file,
5528   dwarf2out_end_source_file,
5529   dwarf2out_begin_block,
5530   dwarf2out_end_block,
5531   dwarf2out_ignore_block,
5532   dwarf2out_source_line,
5533   dwarf2out_begin_prologue,
5534 #if VMS_DEBUGGING_INFO
5535   dwarf2out_vms_end_prologue,
5536   dwarf2out_vms_begin_epilogue,
5537 #else
5538   debug_nothing_int_charstar,
5539   debug_nothing_int_charstar,
5540 #endif
5541   dwarf2out_end_epilogue,
5542   dwarf2out_begin_function,
5543   debug_nothing_int,            /* end_function */
5544   dwarf2out_function_decl,      /* function_decl */
5545   dwarf2out_global_decl,
5546   dwarf2out_type_decl,          /* type_decl */
5547   dwarf2out_imported_module_or_decl,
5548   debug_nothing_tree,           /* deferred_inline_function */
5549   /* The DWARF 2 backend tries to reduce debugging bloat by not
5550      emitting the abstract description of inline functions until
5551      something tries to reference them.  */
5552   dwarf2out_abstract_function,  /* outlining_inline_function */
5553   debug_nothing_rtx,            /* label */
5554   debug_nothing_int,            /* handle_pch */
5555   dwarf2out_var_location,
5556   dwarf2out_switch_text_section,
5557   dwarf2out_direct_call,
5558   dwarf2out_virtual_call_token,
5559   dwarf2out_copy_call_info,
5560   dwarf2out_virtual_call,
5561   dwarf2out_set_name,
5562   1                             /* start_end_main_source_file */
5563 };
5564 #endif
5565 \f
5566 /* NOTE: In the comments in this file, many references are made to
5567    "Debugging Information Entries".  This term is abbreviated as `DIE'
5568    throughout the remainder of this file.  */
5569
5570 /* An internal representation of the DWARF output is built, and then
5571    walked to generate the DWARF debugging info.  The walk of the internal
5572    representation is done after the entire program has been compiled.
5573    The types below are used to describe the internal representation.  */
5574
5575 /* Various DIE's use offsets relative to the beginning of the
5576    .debug_info section to refer to each other.  */
5577
5578 typedef long int dw_offset;
5579
5580 /* Define typedefs here to avoid circular dependencies.  */
5581
5582 typedef struct dw_attr_struct *dw_attr_ref;
5583 typedef struct dw_line_info_struct *dw_line_info_ref;
5584 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5585 typedef struct pubname_struct *pubname_ref;
5586 typedef struct dw_ranges_struct *dw_ranges_ref;
5587 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5588 typedef struct comdat_type_struct *comdat_type_node_ref;
5589
5590 /* Each entry in the line_info_table maintains the file and
5591    line number associated with the label generated for that
5592    entry.  The label gives the PC value associated with
5593    the line number entry.  */
5594
5595 typedef struct GTY(()) dw_line_info_struct {
5596   unsigned long dw_file_num;
5597   unsigned long dw_line_num;
5598 }
5599 dw_line_info_entry;
5600
5601 /* Line information for functions in separate sections; each one gets its
5602    own sequence.  */
5603 typedef struct GTY(()) dw_separate_line_info_struct {
5604   unsigned long dw_file_num;
5605   unsigned long dw_line_num;
5606   unsigned long function;
5607 }
5608 dw_separate_line_info_entry;
5609
5610 /* Each DIE attribute has a field specifying the attribute kind,
5611    a link to the next attribute in the chain, and an attribute value.
5612    Attributes are typically linked below the DIE they modify.  */
5613
5614 typedef struct GTY(()) dw_attr_struct {
5615   enum dwarf_attribute dw_attr;
5616   dw_val_node dw_attr_val;
5617 }
5618 dw_attr_node;
5619
5620 DEF_VEC_O(dw_attr_node);
5621 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5622
5623 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5624    The children of each node form a circular list linked by
5625    die_sib.  die_child points to the node *before* the "first" child node.  */
5626
5627 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5628   enum dwarf_tag die_tag;
5629   union die_symbol_or_type_node
5630     {
5631       char * GTY ((tag ("0"))) die_symbol;
5632       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5633     }
5634   GTY ((desc ("dwarf_version >= 4"))) die_id;
5635   VEC(dw_attr_node,gc) * die_attr;
5636   dw_die_ref die_parent;
5637   dw_die_ref die_child;
5638   dw_die_ref die_sib;
5639   dw_die_ref die_definition; /* ref from a specification to its definition */
5640   dw_offset die_offset;
5641   unsigned long die_abbrev;
5642   int die_mark;
5643   /* Die is used and must not be pruned as unused.  */
5644   int die_perennial_p;
5645   unsigned int decl_id;
5646 }
5647 die_node;
5648
5649 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5650 #define FOR_EACH_CHILD(die, c, expr) do {       \
5651   c = die->die_child;                           \
5652   if (c) do {                                   \
5653     c = c->die_sib;                             \
5654     expr;                                       \
5655   } while (c != die->die_child);                \
5656 } while (0)
5657
5658 /* The pubname structure */
5659
5660 typedef struct GTY(()) pubname_struct {
5661   dw_die_ref die;
5662   const char *name;
5663 }
5664 pubname_entry;
5665
5666 DEF_VEC_O(pubname_entry);
5667 DEF_VEC_ALLOC_O(pubname_entry, gc);
5668
5669 struct GTY(()) dw_ranges_struct {
5670   /* If this is positive, it's a block number, otherwise it's a
5671      bitwise-negated index into dw_ranges_by_label.  */
5672   int num;
5673 };
5674
5675 struct GTY(()) dw_ranges_by_label_struct {
5676   const char *begin;
5677   const char *end;
5678 };
5679
5680 /* The comdat type node structure.  */
5681 typedef struct GTY(()) comdat_type_struct
5682 {
5683   dw_die_ref root_die;
5684   dw_die_ref type_die;
5685   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5686   struct comdat_type_struct *next;
5687 }
5688 comdat_type_node;
5689
5690 /* The limbo die list structure.  */
5691 typedef struct GTY(()) limbo_die_struct {
5692   dw_die_ref die;
5693   tree created_for;
5694   struct limbo_die_struct *next;
5695 }
5696 limbo_die_node;
5697
5698 typedef struct GTY(()) skeleton_chain_struct
5699 {
5700   dw_die_ref old_die;
5701   dw_die_ref new_die;
5702   struct skeleton_chain_struct *parent;
5703 }
5704 skeleton_chain_node;
5705
5706 /* How to start an assembler comment.  */
5707 #ifndef ASM_COMMENT_START
5708 #define ASM_COMMENT_START ";#"
5709 #endif
5710
5711 /* Define a macro which returns nonzero for a TYPE_DECL which was
5712    implicitly generated for a tagged type.
5713
5714    Note that unlike the gcc front end (which generates a NULL named
5715    TYPE_DECL node for each complete tagged type, each array type, and
5716    each function type node created) the g++ front end generates a
5717    _named_ TYPE_DECL node for each tagged type node created.
5718    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5719    generate a DW_TAG_typedef DIE for them.  */
5720
5721 #define TYPE_DECL_IS_STUB(decl)                         \
5722   (DECL_NAME (decl) == NULL_TREE                        \
5723    || (DECL_ARTIFICIAL (decl)                           \
5724        && is_tagged_type (TREE_TYPE (decl))             \
5725        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5726            /* This is necessary for stub decls that     \
5727               appear in nested inline functions.  */    \
5728            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5729                && (decl_ultimate_origin (decl)          \
5730                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5731
5732 /* Information concerning the compilation unit's programming
5733    language, and compiler version.  */
5734
5735 /* Fixed size portion of the DWARF compilation unit header.  */
5736 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5737   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5738
5739 /* Fixed size portion of the DWARF comdat type unit header.  */
5740 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5741   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5742    + DWARF_OFFSET_SIZE)
5743
5744 /* Fixed size portion of public names info.  */
5745 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5746
5747 /* Fixed size portion of the address range info.  */
5748 #define DWARF_ARANGES_HEADER_SIZE                                       \
5749   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5750                 DWARF2_ADDR_SIZE * 2)                                   \
5751    - DWARF_INITIAL_LENGTH_SIZE)
5752
5753 /* Size of padding portion in the address range info.  It must be
5754    aligned to twice the pointer size.  */
5755 #define DWARF_ARANGES_PAD_SIZE \
5756   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5757                 DWARF2_ADDR_SIZE * 2)                              \
5758    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5759
5760 /* Use assembler line directives if available.  */
5761 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5762 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5763 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5764 #else
5765 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5766 #endif
5767 #endif
5768
5769 /* Minimum line offset in a special line info. opcode.
5770    This value was chosen to give a reasonable range of values.  */
5771 #define DWARF_LINE_BASE  -10
5772
5773 /* First special line opcode - leave room for the standard opcodes.  */
5774 #define DWARF_LINE_OPCODE_BASE  10
5775
5776 /* Range of line offsets in a special line info. opcode.  */
5777 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5778
5779 /* Flag that indicates the initial value of the is_stmt_start flag.
5780    In the present implementation, we do not mark any lines as
5781    the beginning of a source statement, because that information
5782    is not made available by the GCC front-end.  */
5783 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5784
5785 /* Maximum number of operations per instruction bundle.  */
5786 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5787 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5788 #endif
5789
5790 #ifdef DWARF2_DEBUGGING_INFO
5791 /* This location is used by calc_die_sizes() to keep track
5792    the offset of each DIE within the .debug_info section.  */
5793 static unsigned long next_die_offset;
5794 #endif
5795
5796 /* Record the root of the DIE's built for the current compilation unit.  */
5797 static GTY(()) dw_die_ref comp_unit_die;
5798
5799 /* A list of type DIEs that have been separated into comdat sections.  */
5800 static GTY(()) comdat_type_node *comdat_type_list;
5801
5802 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5803 static GTY(()) limbo_die_node *limbo_die_list;
5804
5805 /* A list of DIEs for which we may have to generate
5806    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5807 static GTY(()) limbo_die_node *deferred_asm_name;
5808
5809 /* Filenames referenced by this compilation unit.  */
5810 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5811
5812 /* A hash table of references to DIE's that describe declarations.
5813    The key is a DECL_UID() which is a unique number identifying each decl.  */
5814 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5815
5816 /* A hash table of references to DIE's that describe COMMON blocks.
5817    The key is DECL_UID() ^ die_parent.  */
5818 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5819
5820 typedef struct GTY(()) die_arg_entry_struct {
5821     dw_die_ref die;
5822     tree arg;
5823 } die_arg_entry;
5824
5825 DEF_VEC_O(die_arg_entry);
5826 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5827
5828 /* Node of the variable location list.  */
5829 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5830   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5831      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5832      in mode of the EXPR_LIST node and first EXPR_LIST operand
5833      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5834      location or NULL for padding.  For larger bitsizes,
5835      mode is 0 and first operand is a CONCAT with bitsize
5836      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5837      NULL as second operand.  */
5838   rtx GTY (()) loc;
5839   const char * GTY (()) label;
5840   struct var_loc_node * GTY (()) next;
5841 };
5842
5843 /* Variable location list.  */
5844 struct GTY (()) var_loc_list_def {
5845   struct var_loc_node * GTY (()) first;
5846
5847   /* Pointer to the last but one or last element of the
5848      chained list.  If the list is empty, both first and
5849      last are NULL, if the list contains just one node
5850      or the last node certainly is not redundant, it points
5851      to the last node, otherwise points to the last but one.
5852      Do not mark it for GC because it is marked through the chain.  */
5853   struct var_loc_node * GTY ((skip ("%h"))) last;
5854
5855   /* DECL_UID of the variable decl.  */
5856   unsigned int decl_id;
5857 };
5858 typedef struct var_loc_list_def var_loc_list;
5859
5860
5861 /* Table of decl location linked lists.  */
5862 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5863
5864 /* A pointer to the base of a list of references to DIE's that
5865    are uniquely identified by their tag, presence/absence of
5866    children DIE's, and list of attribute/value pairs.  */
5867 static GTY((length ("abbrev_die_table_allocated")))
5868   dw_die_ref *abbrev_die_table;
5869
5870 /* Number of elements currently allocated for abbrev_die_table.  */
5871 static GTY(()) unsigned abbrev_die_table_allocated;
5872
5873 /* Number of elements in type_die_table currently in use.  */
5874 static GTY(()) unsigned abbrev_die_table_in_use;
5875
5876 /* Size (in elements) of increments by which we may expand the
5877    abbrev_die_table.  */
5878 #define ABBREV_DIE_TABLE_INCREMENT 256
5879
5880 /* A pointer to the base of a table that contains line information
5881    for each source code line in .text in the compilation unit.  */
5882 static GTY((length ("line_info_table_allocated")))
5883      dw_line_info_ref line_info_table;
5884
5885 /* Number of elements currently allocated for line_info_table.  */
5886 static GTY(()) unsigned line_info_table_allocated;
5887
5888 /* Number of elements in line_info_table currently in use.  */
5889 static GTY(()) unsigned line_info_table_in_use;
5890
5891 /* A pointer to the base of a table that contains line information
5892    for each source code line outside of .text in the compilation unit.  */
5893 static GTY ((length ("separate_line_info_table_allocated")))
5894      dw_separate_line_info_ref separate_line_info_table;
5895
5896 /* Number of elements currently allocated for separate_line_info_table.  */
5897 static GTY(()) unsigned separate_line_info_table_allocated;
5898
5899 /* Number of elements in separate_line_info_table currently in use.  */
5900 static GTY(()) unsigned separate_line_info_table_in_use;
5901
5902 /* Size (in elements) of increments by which we may expand the
5903    line_info_table.  */
5904 #define LINE_INFO_TABLE_INCREMENT 1024
5905
5906 /* A pointer to the base of a table that contains a list of publicly
5907    accessible names.  */
5908 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5909
5910 /* A pointer to the base of a table that contains a list of publicly
5911    accessible types.  */
5912 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5913
5914 /* Array of dies for which we should generate .debug_arange info.  */
5915 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5916
5917 /* Number of elements currently allocated for arange_table.  */
5918 static GTY(()) unsigned arange_table_allocated;
5919
5920 /* Number of elements in arange_table currently in use.  */
5921 static GTY(()) unsigned arange_table_in_use;
5922
5923 /* Size (in elements) of increments by which we may expand the
5924    arange_table.  */
5925 #define ARANGE_TABLE_INCREMENT 64
5926
5927 /* Array of dies for which we should generate .debug_ranges info.  */
5928 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5929
5930 /* Number of elements currently allocated for ranges_table.  */
5931 static GTY(()) unsigned ranges_table_allocated;
5932
5933 /* Number of elements in ranges_table currently in use.  */
5934 static GTY(()) unsigned ranges_table_in_use;
5935
5936 /* Array of pairs of labels referenced in ranges_table.  */
5937 static GTY ((length ("ranges_by_label_allocated")))
5938      dw_ranges_by_label_ref ranges_by_label;
5939
5940 /* Number of elements currently allocated for ranges_by_label.  */
5941 static GTY(()) unsigned ranges_by_label_allocated;
5942
5943 /* Number of elements in ranges_by_label currently in use.  */
5944 static GTY(()) unsigned ranges_by_label_in_use;
5945
5946 /* Size (in elements) of increments by which we may expand the
5947    ranges_table.  */
5948 #define RANGES_TABLE_INCREMENT 64
5949
5950 /* Whether we have location lists that need outputting */
5951 static GTY(()) bool have_location_lists;
5952
5953 /* Unique label counter.  */
5954 static GTY(()) unsigned int loclabel_num;
5955
5956 /* Unique label counter for point-of-call tables.  */
5957 static GTY(()) unsigned int poc_label_num;
5958
5959 /* The direct call table structure.  */
5960
5961 typedef struct GTY(()) dcall_struct {
5962   unsigned int poc_label_num;
5963   tree poc_decl;
5964   dw_die_ref targ_die;
5965 }
5966 dcall_entry;
5967
5968 DEF_VEC_O(dcall_entry);
5969 DEF_VEC_ALLOC_O(dcall_entry, gc);
5970
5971 /* The virtual call table structure.  */
5972
5973 typedef struct GTY(()) vcall_struct {
5974   unsigned int poc_label_num;
5975   unsigned int vtable_slot;
5976 }
5977 vcall_entry;
5978
5979 DEF_VEC_O(vcall_entry);
5980 DEF_VEC_ALLOC_O(vcall_entry, gc);
5981
5982 /* Pointers to the direct and virtual call tables.  */
5983 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
5984 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
5985
5986 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
5987
5988 struct GTY (()) vcall_insn {
5989   int insn_uid;
5990   unsigned int vtable_slot;
5991 };
5992
5993 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
5994
5995 #ifdef DWARF2_DEBUGGING_INFO
5996 /* Record whether the function being analyzed contains inlined functions.  */
5997 static int current_function_has_inlines;
5998 #endif
5999 #if 0 && defined (MIPS_DEBUGGING_INFO)
6000 static int comp_unit_has_inlines;
6001 #endif
6002
6003 /* The last file entry emitted by maybe_emit_file().  */
6004 static GTY(()) struct dwarf_file_data * last_emitted_file;
6005
6006 /* Number of internal labels generated by gen_internal_sym().  */
6007 static GTY(()) int label_num;
6008
6009 /* Cached result of previous call to lookup_filename.  */
6010 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6011
6012 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6013
6014 #ifdef DWARF2_DEBUGGING_INFO
6015
6016 /* Offset from the "steady-state frame pointer" to the frame base,
6017    within the current function.  */
6018 static HOST_WIDE_INT frame_pointer_fb_offset;
6019
6020 /* Forward declarations for functions defined in this file.  */
6021
6022 static int is_pseudo_reg (const_rtx);
6023 static tree type_main_variant (tree);
6024 static int is_tagged_type (const_tree);
6025 static const char *dwarf_tag_name (unsigned);
6026 static const char *dwarf_attr_name (unsigned);
6027 static const char *dwarf_form_name (unsigned);
6028 static tree decl_ultimate_origin (const_tree);
6029 static tree decl_class_context (tree);
6030 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6031 static inline enum dw_val_class AT_class (dw_attr_ref);
6032 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6033 static inline unsigned AT_flag (dw_attr_ref);
6034 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6035 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6036 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6037 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6038 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6039                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6040 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6041                                unsigned int, unsigned char *);
6042 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6043 static hashval_t debug_str_do_hash (const void *);
6044 static int debug_str_eq (const void *, const void *);
6045 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6046 static inline const char *AT_string (dw_attr_ref);
6047 static enum dwarf_form AT_string_form (dw_attr_ref);
6048 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6049 static void add_AT_specification (dw_die_ref, dw_die_ref);
6050 static inline dw_die_ref AT_ref (dw_attr_ref);
6051 static inline int AT_ref_external (dw_attr_ref);
6052 static inline void set_AT_ref_external (dw_attr_ref, int);
6053 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6054 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6055 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6056 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6057                              dw_loc_list_ref);
6058 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6059 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6060 static inline rtx AT_addr (dw_attr_ref);
6061 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6062 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6063 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6064 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6065                            unsigned HOST_WIDE_INT);
6066 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6067                                unsigned long);
6068 static inline const char *AT_lbl (dw_attr_ref);
6069 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6070 static const char *get_AT_low_pc (dw_die_ref);
6071 static const char *get_AT_hi_pc (dw_die_ref);
6072 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6073 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6074 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6075 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6076 static bool is_cxx (void);
6077 static bool is_fortran (void);
6078 static bool is_ada (void);
6079 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6080 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6081 static void add_child_die (dw_die_ref, dw_die_ref);
6082 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6083 static dw_die_ref lookup_type_die (tree);
6084 static void equate_type_number_to_die (tree, dw_die_ref);
6085 static hashval_t decl_die_table_hash (const void *);
6086 static int decl_die_table_eq (const void *, const void *);
6087 static dw_die_ref lookup_decl_die (tree);
6088 static hashval_t common_block_die_table_hash (const void *);
6089 static int common_block_die_table_eq (const void *, const void *);
6090 static hashval_t decl_loc_table_hash (const void *);
6091 static int decl_loc_table_eq (const void *, const void *);
6092 static var_loc_list *lookup_decl_loc (const_tree);
6093 static void equate_decl_number_to_die (tree, dw_die_ref);
6094 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6095 static void print_spaces (FILE *);
6096 static void print_die (dw_die_ref, FILE *);
6097 static void print_dwarf_line_table (FILE *);
6098 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6099 static dw_die_ref pop_compile_unit (dw_die_ref);
6100 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6101 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6102 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6103 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6104 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6105 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6106 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6107                                    struct md5_ctx *, int *);
6108 struct checksum_attributes;
6109 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6110 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6111 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6112 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6113 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6114 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6115 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6116 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6117 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6118 static void compute_section_prefix (dw_die_ref);
6119 static int is_type_die (dw_die_ref);
6120 static int is_comdat_die (dw_die_ref);
6121 static int is_symbol_die (dw_die_ref);
6122 static void assign_symbol_names (dw_die_ref);
6123 static void break_out_includes (dw_die_ref);
6124 static int is_declaration_die (dw_die_ref);
6125 static int should_move_die_to_comdat (dw_die_ref);
6126 static dw_die_ref clone_as_declaration (dw_die_ref);
6127 static dw_die_ref clone_die (dw_die_ref);
6128 static dw_die_ref clone_tree (dw_die_ref);
6129 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6130 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6131 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6132 static dw_die_ref generate_skeleton (dw_die_ref);
6133 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6134                                                          dw_die_ref);
6135 static void break_out_comdat_types (dw_die_ref);
6136 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6137 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6138 static void copy_decls_for_unworthy_types (dw_die_ref);
6139
6140 static hashval_t htab_cu_hash (const void *);
6141 static int htab_cu_eq (const void *, const void *);
6142 static void htab_cu_del (void *);
6143 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6144 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6145 static void add_sibling_attributes (dw_die_ref);
6146 static void build_abbrev_table (dw_die_ref);
6147 static void output_location_lists (dw_die_ref);
6148 static int constant_size (unsigned HOST_WIDE_INT);
6149 static unsigned long size_of_die (dw_die_ref);
6150 static void calc_die_sizes (dw_die_ref);
6151 static void mark_dies (dw_die_ref);
6152 static void unmark_dies (dw_die_ref);
6153 static void unmark_all_dies (dw_die_ref);
6154 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6155 static unsigned long size_of_aranges (void);
6156 static enum dwarf_form value_format (dw_attr_ref);
6157 static void output_value_format (dw_attr_ref);
6158 static void output_abbrev_section (void);
6159 static void output_die_symbol (dw_die_ref);
6160 static void output_die (dw_die_ref);
6161 static void output_compilation_unit_header (void);
6162 static void output_comp_unit (dw_die_ref, int);
6163 static void output_comdat_type_unit (comdat_type_node *);
6164 static const char *dwarf2_name (tree, int);
6165 static void add_pubname (tree, dw_die_ref);
6166 static void add_pubname_string (const char *, dw_die_ref);
6167 static void add_pubtype (tree, dw_die_ref);
6168 static void output_pubnames (VEC (pubname_entry,gc) *);
6169 static void add_arange (tree, dw_die_ref);
6170 static void output_aranges (void);
6171 static unsigned int add_ranges_num (int);
6172 static unsigned int add_ranges (const_tree);
6173 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6174                                   bool *);
6175 static void output_ranges (void);
6176 static void output_line_info (void);
6177 static void output_file_names (void);
6178 static dw_die_ref base_type_die (tree);
6179 static int is_base_type (tree);
6180 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6181 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6182 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6183 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6184 static int type_is_enum (const_tree);
6185 static unsigned int dbx_reg_number (const_rtx);
6186 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6187 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6188 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6189                                                 enum var_init_status);
6190 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6191                                                      enum var_init_status);
6192 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6193                                          enum var_init_status);
6194 static int is_based_loc (const_rtx);
6195 static int resolve_one_addr (rtx *, void *);
6196 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6197                                             enum var_init_status);
6198 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6199                                                enum var_init_status);
6200 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6201                                         enum var_init_status);
6202 static dw_loc_list_ref loc_list_from_tree (tree, int);
6203 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6204 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6205 static tree field_type (const_tree);
6206 static unsigned int simple_type_align_in_bits (const_tree);
6207 static unsigned int simple_decl_align_in_bits (const_tree);
6208 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6209 static HOST_WIDE_INT field_byte_offset (const_tree);
6210 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6211                                          dw_loc_list_ref);
6212 static void add_data_member_location_attribute (dw_die_ref, tree);
6213 static bool add_const_value_attribute (dw_die_ref, rtx);
6214 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6215 static void insert_double (double_int, unsigned char *);
6216 static void insert_float (const_rtx, unsigned char *);
6217 static rtx rtl_for_decl_location (tree);
6218 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6219                                                    enum dwarf_attribute);
6220 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6221 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6222 static void add_name_attribute (dw_die_ref, const char *);
6223 static void add_comp_dir_attribute (dw_die_ref);
6224 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6225 static void add_subscript_info (dw_die_ref, tree, bool);
6226 static void add_byte_size_attribute (dw_die_ref, tree);
6227 static void add_bit_offset_attribute (dw_die_ref, tree);
6228 static void add_bit_size_attribute (dw_die_ref, tree);
6229 static void add_prototyped_attribute (dw_die_ref, tree);
6230 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6231 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6232 static void add_src_coords_attributes (dw_die_ref, tree);
6233 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6234 static void push_decl_scope (tree);
6235 static void pop_decl_scope (void);
6236 static dw_die_ref scope_die_for (tree, dw_die_ref);
6237 static inline int local_scope_p (dw_die_ref);
6238 static inline int class_scope_p (dw_die_ref);
6239 static inline int class_or_namespace_scope_p (dw_die_ref);
6240 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6241 static void add_calling_convention_attribute (dw_die_ref, tree);
6242 static const char *type_tag (const_tree);
6243 static tree member_declared_type (const_tree);
6244 #if 0
6245 static const char *decl_start_label (tree);
6246 #endif
6247 static void gen_array_type_die (tree, dw_die_ref);
6248 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6249 #if 0
6250 static void gen_entry_point_die (tree, dw_die_ref);
6251 #endif
6252 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6253 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6254 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6255 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6256 static void gen_formal_types_die (tree, dw_die_ref);
6257 static void gen_subprogram_die (tree, dw_die_ref);
6258 static void gen_variable_die (tree, tree, dw_die_ref);
6259 static void gen_const_die (tree, dw_die_ref);
6260 static void gen_label_die (tree, dw_die_ref);
6261 static void gen_lexical_block_die (tree, dw_die_ref, int);
6262 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6263 static void gen_field_die (tree, dw_die_ref);
6264 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6265 static dw_die_ref gen_compile_unit_die (const char *);
6266 static void gen_inheritance_die (tree, tree, dw_die_ref);
6267 static void gen_member_die (tree, dw_die_ref);
6268 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6269                                                 enum debug_info_usage);
6270 static void gen_subroutine_type_die (tree, dw_die_ref);
6271 static void gen_typedef_die (tree, dw_die_ref);
6272 static void gen_type_die (tree, dw_die_ref);
6273 static void gen_block_die (tree, dw_die_ref, int);
6274 static void decls_for_scope (tree, dw_die_ref, int);
6275 static int is_redundant_typedef (const_tree);
6276 static bool is_naming_typedef_decl (const_tree);
6277 static inline dw_die_ref get_context_die (tree);
6278 static void gen_namespace_die (tree, dw_die_ref);
6279 static void gen_decl_die (tree, tree, dw_die_ref);
6280 static dw_die_ref force_decl_die (tree);
6281 static dw_die_ref force_type_die (tree);
6282 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6283 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6284 static struct dwarf_file_data * lookup_filename (const char *);
6285 static void retry_incomplete_types (void);
6286 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6287 static void gen_generic_params_dies (tree);
6288 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6289 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6290 static void splice_child_die (dw_die_ref, dw_die_ref);
6291 static int file_info_cmp (const void *, const void *);
6292 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6293                                      const char *, const char *);
6294 static void output_loc_list (dw_loc_list_ref);
6295 static char *gen_internal_sym (const char *);
6296
6297 static void prune_unmark_dies (dw_die_ref);
6298 static void prune_unused_types_mark (dw_die_ref, int);
6299 static void prune_unused_types_walk (dw_die_ref);
6300 static void prune_unused_types_walk_attribs (dw_die_ref);
6301 static void prune_unused_types_prune (dw_die_ref);
6302 static void prune_unused_types (void);
6303 static int maybe_emit_file (struct dwarf_file_data *fd);
6304 static inline const char *AT_vms_delta1 (dw_attr_ref);
6305 static inline const char *AT_vms_delta2 (dw_attr_ref);
6306 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6307                                      const char *, const char *);
6308 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6309 static void gen_remaining_tmpl_value_param_die_attribute (void);
6310
6311 /* Section names used to hold DWARF debugging information.  */
6312 #ifndef DEBUG_INFO_SECTION
6313 #define DEBUG_INFO_SECTION      ".debug_info"
6314 #endif
6315 #ifndef DEBUG_ABBREV_SECTION
6316 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6317 #endif
6318 #ifndef DEBUG_ARANGES_SECTION
6319 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6320 #endif
6321 #ifndef DEBUG_MACINFO_SECTION
6322 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6323 #endif
6324 #ifndef DEBUG_LINE_SECTION
6325 #define DEBUG_LINE_SECTION      ".debug_line"
6326 #endif
6327 #ifndef DEBUG_LOC_SECTION
6328 #define DEBUG_LOC_SECTION       ".debug_loc"
6329 #endif
6330 #ifndef DEBUG_PUBNAMES_SECTION
6331 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6332 #endif
6333 #ifndef DEBUG_PUBTYPES_SECTION
6334 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6335 #endif
6336 #ifndef DEBUG_DCALL_SECTION
6337 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6338 #endif
6339 #ifndef DEBUG_VCALL_SECTION
6340 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6341 #endif
6342 #ifndef DEBUG_STR_SECTION
6343 #define DEBUG_STR_SECTION       ".debug_str"
6344 #endif
6345 #ifndef DEBUG_RANGES_SECTION
6346 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6347 #endif
6348
6349 /* Standard ELF section names for compiled code and data.  */
6350 #ifndef TEXT_SECTION_NAME
6351 #define TEXT_SECTION_NAME       ".text"
6352 #endif
6353
6354 /* Section flags for .debug_str section.  */
6355 #define DEBUG_STR_SECTION_FLAGS \
6356   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6357    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6358    : SECTION_DEBUG)
6359
6360 /* Labels we insert at beginning sections we can reference instead of
6361    the section names themselves.  */
6362
6363 #ifndef TEXT_SECTION_LABEL
6364 #define TEXT_SECTION_LABEL              "Ltext"
6365 #endif
6366 #ifndef COLD_TEXT_SECTION_LABEL
6367 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6368 #endif
6369 #ifndef DEBUG_LINE_SECTION_LABEL
6370 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6371 #endif
6372 #ifndef DEBUG_INFO_SECTION_LABEL
6373 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6374 #endif
6375 #ifndef DEBUG_ABBREV_SECTION_LABEL
6376 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6377 #endif
6378 #ifndef DEBUG_LOC_SECTION_LABEL
6379 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6380 #endif
6381 #ifndef DEBUG_RANGES_SECTION_LABEL
6382 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6383 #endif
6384 #ifndef DEBUG_MACINFO_SECTION_LABEL
6385 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6386 #endif
6387
6388 /* Mangled name attribute to use.  This used to be a vendor extension
6389    until DWARF 4 standardized it.  */
6390 #define AT_linkage_name \
6391   (dwarf_version >= 4 ? DW_AT_linkage_name : DW_AT_MIPS_linkage_name)
6392
6393
6394 /* Definitions of defaults for formats and names of various special
6395    (artificial) labels which may be generated within this file (when the -g
6396    options is used and DWARF2_DEBUGGING_INFO is in effect.
6397    If necessary, these may be overridden from within the tm.h file, but
6398    typically, overriding these defaults is unnecessary.  */
6399
6400 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6401 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6402 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6403 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6404 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6405 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6406 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6407 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6408 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6409 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6410
6411 #ifndef TEXT_END_LABEL
6412 #define TEXT_END_LABEL          "Letext"
6413 #endif
6414 #ifndef COLD_END_LABEL
6415 #define COLD_END_LABEL          "Letext_cold"
6416 #endif
6417 #ifndef BLOCK_BEGIN_LABEL
6418 #define BLOCK_BEGIN_LABEL       "LBB"
6419 #endif
6420 #ifndef BLOCK_END_LABEL
6421 #define BLOCK_END_LABEL         "LBE"
6422 #endif
6423 #ifndef LINE_CODE_LABEL
6424 #define LINE_CODE_LABEL         "LM"
6425 #endif
6426 #ifndef SEPARATE_LINE_CODE_LABEL
6427 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6428 #endif
6429
6430 \f
6431 /* We allow a language front-end to designate a function that is to be
6432    called to "demangle" any name before it is put into a DIE.  */
6433
6434 static const char *(*demangle_name_func) (const char *);
6435
6436 void
6437 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6438 {
6439   demangle_name_func = func;
6440 }
6441
6442 /* Test if rtl node points to a pseudo register.  */
6443
6444 static inline int
6445 is_pseudo_reg (const_rtx rtl)
6446 {
6447   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6448           || (GET_CODE (rtl) == SUBREG
6449               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6450 }
6451
6452 /* Return a reference to a type, with its const and volatile qualifiers
6453    removed.  */
6454
6455 static inline tree
6456 type_main_variant (tree type)
6457 {
6458   type = TYPE_MAIN_VARIANT (type);
6459
6460   /* ??? There really should be only one main variant among any group of
6461      variants of a given type (and all of the MAIN_VARIANT values for all
6462      members of the group should point to that one type) but sometimes the C
6463      front-end messes this up for array types, so we work around that bug
6464      here.  */
6465   if (TREE_CODE (type) == ARRAY_TYPE)
6466     while (type != TYPE_MAIN_VARIANT (type))
6467       type = TYPE_MAIN_VARIANT (type);
6468
6469   return type;
6470 }
6471
6472 /* Return nonzero if the given type node represents a tagged type.  */
6473
6474 static inline int
6475 is_tagged_type (const_tree type)
6476 {
6477   enum tree_code code = TREE_CODE (type);
6478
6479   return (code == RECORD_TYPE || code == UNION_TYPE
6480           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6481 }
6482
6483 /* Convert a DIE tag into its string name.  */
6484
6485 static const char *
6486 dwarf_tag_name (unsigned int tag)
6487 {
6488   switch (tag)
6489     {
6490     case DW_TAG_padding:
6491       return "DW_TAG_padding";
6492     case DW_TAG_array_type:
6493       return "DW_TAG_array_type";
6494     case DW_TAG_class_type:
6495       return "DW_TAG_class_type";
6496     case DW_TAG_entry_point:
6497       return "DW_TAG_entry_point";
6498     case DW_TAG_enumeration_type:
6499       return "DW_TAG_enumeration_type";
6500     case DW_TAG_formal_parameter:
6501       return "DW_TAG_formal_parameter";
6502     case DW_TAG_imported_declaration:
6503       return "DW_TAG_imported_declaration";
6504     case DW_TAG_label:
6505       return "DW_TAG_label";
6506     case DW_TAG_lexical_block:
6507       return "DW_TAG_lexical_block";
6508     case DW_TAG_member:
6509       return "DW_TAG_member";
6510     case DW_TAG_pointer_type:
6511       return "DW_TAG_pointer_type";
6512     case DW_TAG_reference_type:
6513       return "DW_TAG_reference_type";
6514     case DW_TAG_compile_unit:
6515       return "DW_TAG_compile_unit";
6516     case DW_TAG_string_type:
6517       return "DW_TAG_string_type";
6518     case DW_TAG_structure_type:
6519       return "DW_TAG_structure_type";
6520     case DW_TAG_subroutine_type:
6521       return "DW_TAG_subroutine_type";
6522     case DW_TAG_typedef:
6523       return "DW_TAG_typedef";
6524     case DW_TAG_union_type:
6525       return "DW_TAG_union_type";
6526     case DW_TAG_unspecified_parameters:
6527       return "DW_TAG_unspecified_parameters";
6528     case DW_TAG_variant:
6529       return "DW_TAG_variant";
6530     case DW_TAG_common_block:
6531       return "DW_TAG_common_block";
6532     case DW_TAG_common_inclusion:
6533       return "DW_TAG_common_inclusion";
6534     case DW_TAG_inheritance:
6535       return "DW_TAG_inheritance";
6536     case DW_TAG_inlined_subroutine:
6537       return "DW_TAG_inlined_subroutine";
6538     case DW_TAG_module:
6539       return "DW_TAG_module";
6540     case DW_TAG_ptr_to_member_type:
6541       return "DW_TAG_ptr_to_member_type";
6542     case DW_TAG_set_type:
6543       return "DW_TAG_set_type";
6544     case DW_TAG_subrange_type:
6545       return "DW_TAG_subrange_type";
6546     case DW_TAG_with_stmt:
6547       return "DW_TAG_with_stmt";
6548     case DW_TAG_access_declaration:
6549       return "DW_TAG_access_declaration";
6550     case DW_TAG_base_type:
6551       return "DW_TAG_base_type";
6552     case DW_TAG_catch_block:
6553       return "DW_TAG_catch_block";
6554     case DW_TAG_const_type:
6555       return "DW_TAG_const_type";
6556     case DW_TAG_constant:
6557       return "DW_TAG_constant";
6558     case DW_TAG_enumerator:
6559       return "DW_TAG_enumerator";
6560     case DW_TAG_file_type:
6561       return "DW_TAG_file_type";
6562     case DW_TAG_friend:
6563       return "DW_TAG_friend";
6564     case DW_TAG_namelist:
6565       return "DW_TAG_namelist";
6566     case DW_TAG_namelist_item:
6567       return "DW_TAG_namelist_item";
6568     case DW_TAG_packed_type:
6569       return "DW_TAG_packed_type";
6570     case DW_TAG_subprogram:
6571       return "DW_TAG_subprogram";
6572     case DW_TAG_template_type_param:
6573       return "DW_TAG_template_type_param";
6574     case DW_TAG_template_value_param:
6575       return "DW_TAG_template_value_param";
6576     case DW_TAG_thrown_type:
6577       return "DW_TAG_thrown_type";
6578     case DW_TAG_try_block:
6579       return "DW_TAG_try_block";
6580     case DW_TAG_variant_part:
6581       return "DW_TAG_variant_part";
6582     case DW_TAG_variable:
6583       return "DW_TAG_variable";
6584     case DW_TAG_volatile_type:
6585       return "DW_TAG_volatile_type";
6586     case DW_TAG_dwarf_procedure:
6587       return "DW_TAG_dwarf_procedure";
6588     case DW_TAG_restrict_type:
6589       return "DW_TAG_restrict_type";
6590     case DW_TAG_interface_type:
6591       return "DW_TAG_interface_type";
6592     case DW_TAG_namespace:
6593       return "DW_TAG_namespace";
6594     case DW_TAG_imported_module:
6595       return "DW_TAG_imported_module";
6596     case DW_TAG_unspecified_type:
6597       return "DW_TAG_unspecified_type";
6598     case DW_TAG_partial_unit:
6599       return "DW_TAG_partial_unit";
6600     case DW_TAG_imported_unit:
6601       return "DW_TAG_imported_unit";
6602     case DW_TAG_condition:
6603       return "DW_TAG_condition";
6604     case DW_TAG_shared_type:
6605       return "DW_TAG_shared_type";
6606     case DW_TAG_type_unit:
6607       return "DW_TAG_type_unit";
6608     case DW_TAG_rvalue_reference_type:
6609       return "DW_TAG_rvalue_reference_type";
6610     case DW_TAG_template_alias:
6611       return "DW_TAG_template_alias";
6612     case DW_TAG_GNU_template_parameter_pack:
6613       return "DW_TAG_GNU_template_parameter_pack";
6614     case DW_TAG_GNU_formal_parameter_pack:
6615       return "DW_TAG_GNU_formal_parameter_pack";
6616     case DW_TAG_MIPS_loop:
6617       return "DW_TAG_MIPS_loop";
6618     case DW_TAG_format_label:
6619       return "DW_TAG_format_label";
6620     case DW_TAG_function_template:
6621       return "DW_TAG_function_template";
6622     case DW_TAG_class_template:
6623       return "DW_TAG_class_template";
6624     case DW_TAG_GNU_BINCL:
6625       return "DW_TAG_GNU_BINCL";
6626     case DW_TAG_GNU_EINCL:
6627       return "DW_TAG_GNU_EINCL";
6628     case DW_TAG_GNU_template_template_param:
6629       return "DW_TAG_GNU_template_template_param";
6630     default:
6631       return "DW_TAG_<unknown>";
6632     }
6633 }
6634
6635 /* Convert a DWARF attribute code into its string name.  */
6636
6637 static const char *
6638 dwarf_attr_name (unsigned int attr)
6639 {
6640   switch (attr)
6641     {
6642     case DW_AT_sibling:
6643       return "DW_AT_sibling";
6644     case DW_AT_location:
6645       return "DW_AT_location";
6646     case DW_AT_name:
6647       return "DW_AT_name";
6648     case DW_AT_ordering:
6649       return "DW_AT_ordering";
6650     case DW_AT_subscr_data:
6651       return "DW_AT_subscr_data";
6652     case DW_AT_byte_size:
6653       return "DW_AT_byte_size";
6654     case DW_AT_bit_offset:
6655       return "DW_AT_bit_offset";
6656     case DW_AT_bit_size:
6657       return "DW_AT_bit_size";
6658     case DW_AT_element_list:
6659       return "DW_AT_element_list";
6660     case DW_AT_stmt_list:
6661       return "DW_AT_stmt_list";
6662     case DW_AT_low_pc:
6663       return "DW_AT_low_pc";
6664     case DW_AT_high_pc:
6665       return "DW_AT_high_pc";
6666     case DW_AT_language:
6667       return "DW_AT_language";
6668     case DW_AT_member:
6669       return "DW_AT_member";
6670     case DW_AT_discr:
6671       return "DW_AT_discr";
6672     case DW_AT_discr_value:
6673       return "DW_AT_discr_value";
6674     case DW_AT_visibility:
6675       return "DW_AT_visibility";
6676     case DW_AT_import:
6677       return "DW_AT_import";
6678     case DW_AT_string_length:
6679       return "DW_AT_string_length";
6680     case DW_AT_common_reference:
6681       return "DW_AT_common_reference";
6682     case DW_AT_comp_dir:
6683       return "DW_AT_comp_dir";
6684     case DW_AT_const_value:
6685       return "DW_AT_const_value";
6686     case DW_AT_containing_type:
6687       return "DW_AT_containing_type";
6688     case DW_AT_default_value:
6689       return "DW_AT_default_value";
6690     case DW_AT_inline:
6691       return "DW_AT_inline";
6692     case DW_AT_is_optional:
6693       return "DW_AT_is_optional";
6694     case DW_AT_lower_bound:
6695       return "DW_AT_lower_bound";
6696     case DW_AT_producer:
6697       return "DW_AT_producer";
6698     case DW_AT_prototyped:
6699       return "DW_AT_prototyped";
6700     case DW_AT_return_addr:
6701       return "DW_AT_return_addr";
6702     case DW_AT_start_scope:
6703       return "DW_AT_start_scope";
6704     case DW_AT_bit_stride:
6705       return "DW_AT_bit_stride";
6706     case DW_AT_upper_bound:
6707       return "DW_AT_upper_bound";
6708     case DW_AT_abstract_origin:
6709       return "DW_AT_abstract_origin";
6710     case DW_AT_accessibility:
6711       return "DW_AT_accessibility";
6712     case DW_AT_address_class:
6713       return "DW_AT_address_class";
6714     case DW_AT_artificial:
6715       return "DW_AT_artificial";
6716     case DW_AT_base_types:
6717       return "DW_AT_base_types";
6718     case DW_AT_calling_convention:
6719       return "DW_AT_calling_convention";
6720     case DW_AT_count:
6721       return "DW_AT_count";
6722     case DW_AT_data_member_location:
6723       return "DW_AT_data_member_location";
6724     case DW_AT_decl_column:
6725       return "DW_AT_decl_column";
6726     case DW_AT_decl_file:
6727       return "DW_AT_decl_file";
6728     case DW_AT_decl_line:
6729       return "DW_AT_decl_line";
6730     case DW_AT_declaration:
6731       return "DW_AT_declaration";
6732     case DW_AT_discr_list:
6733       return "DW_AT_discr_list";
6734     case DW_AT_encoding:
6735       return "DW_AT_encoding";
6736     case DW_AT_external:
6737       return "DW_AT_external";
6738     case DW_AT_explicit:
6739       return "DW_AT_explicit";
6740     case DW_AT_frame_base:
6741       return "DW_AT_frame_base";
6742     case DW_AT_friend:
6743       return "DW_AT_friend";
6744     case DW_AT_identifier_case:
6745       return "DW_AT_identifier_case";
6746     case DW_AT_macro_info:
6747       return "DW_AT_macro_info";
6748     case DW_AT_namelist_items:
6749       return "DW_AT_namelist_items";
6750     case DW_AT_priority:
6751       return "DW_AT_priority";
6752     case DW_AT_segment:
6753       return "DW_AT_segment";
6754     case DW_AT_specification:
6755       return "DW_AT_specification";
6756     case DW_AT_static_link:
6757       return "DW_AT_static_link";
6758     case DW_AT_type:
6759       return "DW_AT_type";
6760     case DW_AT_use_location:
6761       return "DW_AT_use_location";
6762     case DW_AT_variable_parameter:
6763       return "DW_AT_variable_parameter";
6764     case DW_AT_virtuality:
6765       return "DW_AT_virtuality";
6766     case DW_AT_vtable_elem_location:
6767       return "DW_AT_vtable_elem_location";
6768
6769     case DW_AT_allocated:
6770       return "DW_AT_allocated";
6771     case DW_AT_associated:
6772       return "DW_AT_associated";
6773     case DW_AT_data_location:
6774       return "DW_AT_data_location";
6775     case DW_AT_byte_stride:
6776       return "DW_AT_byte_stride";
6777     case DW_AT_entry_pc:
6778       return "DW_AT_entry_pc";
6779     case DW_AT_use_UTF8:
6780       return "DW_AT_use_UTF8";
6781     case DW_AT_extension:
6782       return "DW_AT_extension";
6783     case DW_AT_ranges:
6784       return "DW_AT_ranges";
6785     case DW_AT_trampoline:
6786       return "DW_AT_trampoline";
6787     case DW_AT_call_column:
6788       return "DW_AT_call_column";
6789     case DW_AT_call_file:
6790       return "DW_AT_call_file";
6791     case DW_AT_call_line:
6792       return "DW_AT_call_line";
6793
6794     case DW_AT_signature:
6795       return "DW_AT_signature";
6796     case DW_AT_main_subprogram:
6797       return "DW_AT_main_subprogram";
6798     case DW_AT_data_bit_offset:
6799       return "DW_AT_data_bit_offset";
6800     case DW_AT_const_expr:
6801       return "DW_AT_const_expr";
6802     case DW_AT_enum_class:
6803       return "DW_AT_enum_class";
6804     case DW_AT_linkage_name:
6805       return "DW_AT_linkage_name";
6806
6807     case DW_AT_MIPS_fde:
6808       return "DW_AT_MIPS_fde";
6809     case DW_AT_MIPS_loop_begin:
6810       return "DW_AT_MIPS_loop_begin";
6811     case DW_AT_MIPS_tail_loop_begin:
6812       return "DW_AT_MIPS_tail_loop_begin";
6813     case DW_AT_MIPS_epilog_begin:
6814       return "DW_AT_MIPS_epilog_begin";
6815 #if VMS_DEBUGGING_INFO
6816     case DW_AT_HP_prologue:
6817       return "DW_AT_HP_prologue";
6818 #else
6819     case DW_AT_MIPS_loop_unroll_factor:
6820       return "DW_AT_MIPS_loop_unroll_factor";
6821 #endif
6822     case DW_AT_MIPS_software_pipeline_depth:
6823       return "DW_AT_MIPS_software_pipeline_depth";
6824     case DW_AT_MIPS_linkage_name:
6825       return "DW_AT_MIPS_linkage_name";
6826 #if VMS_DEBUGGING_INFO
6827     case DW_AT_HP_epilogue:
6828       return "DW_AT_HP_epilogue";
6829 #else
6830     case DW_AT_MIPS_stride:
6831       return "DW_AT_MIPS_stride";
6832 #endif
6833     case DW_AT_MIPS_abstract_name:
6834       return "DW_AT_MIPS_abstract_name";
6835     case DW_AT_MIPS_clone_origin:
6836       return "DW_AT_MIPS_clone_origin";
6837     case DW_AT_MIPS_has_inlines:
6838       return "DW_AT_MIPS_has_inlines";
6839
6840     case DW_AT_sf_names:
6841       return "DW_AT_sf_names";
6842     case DW_AT_src_info:
6843       return "DW_AT_src_info";
6844     case DW_AT_mac_info:
6845       return "DW_AT_mac_info";
6846     case DW_AT_src_coords:
6847       return "DW_AT_src_coords";
6848     case DW_AT_body_begin:
6849       return "DW_AT_body_begin";
6850     case DW_AT_body_end:
6851       return "DW_AT_body_end";
6852     case DW_AT_GNU_vector:
6853       return "DW_AT_GNU_vector";
6854     case DW_AT_GNU_guarded_by:
6855       return "DW_AT_GNU_guarded_by";
6856     case DW_AT_GNU_pt_guarded_by:
6857       return "DW_AT_GNU_pt_guarded_by";
6858     case DW_AT_GNU_guarded:
6859       return "DW_AT_GNU_guarded";
6860     case DW_AT_GNU_pt_guarded:
6861       return "DW_AT_GNU_pt_guarded";
6862     case DW_AT_GNU_locks_excluded:
6863       return "DW_AT_GNU_locks_excluded";
6864     case DW_AT_GNU_exclusive_locks_required:
6865       return "DW_AT_GNU_exclusive_locks_required";
6866     case DW_AT_GNU_shared_locks_required:
6867       return "DW_AT_GNU_shared_locks_required";
6868     case DW_AT_GNU_odr_signature:
6869       return "DW_AT_GNU_odr_signature";
6870     case DW_AT_GNU_template_name:
6871       return "DW_AT_GNU_template_name";
6872
6873     case DW_AT_VMS_rtnbeg_pd_address:
6874       return "DW_AT_VMS_rtnbeg_pd_address";
6875
6876     default:
6877       return "DW_AT_<unknown>";
6878     }
6879 }
6880
6881 /* Convert a DWARF value form code into its string name.  */
6882
6883 static const char *
6884 dwarf_form_name (unsigned int form)
6885 {
6886   switch (form)
6887     {
6888     case DW_FORM_addr:
6889       return "DW_FORM_addr";
6890     case DW_FORM_block2:
6891       return "DW_FORM_block2";
6892     case DW_FORM_block4:
6893       return "DW_FORM_block4";
6894     case DW_FORM_data2:
6895       return "DW_FORM_data2";
6896     case DW_FORM_data4:
6897       return "DW_FORM_data4";
6898     case DW_FORM_data8:
6899       return "DW_FORM_data8";
6900     case DW_FORM_string:
6901       return "DW_FORM_string";
6902     case DW_FORM_block:
6903       return "DW_FORM_block";
6904     case DW_FORM_block1:
6905       return "DW_FORM_block1";
6906     case DW_FORM_data1:
6907       return "DW_FORM_data1";
6908     case DW_FORM_flag:
6909       return "DW_FORM_flag";
6910     case DW_FORM_sdata:
6911       return "DW_FORM_sdata";
6912     case DW_FORM_strp:
6913       return "DW_FORM_strp";
6914     case DW_FORM_udata:
6915       return "DW_FORM_udata";
6916     case DW_FORM_ref_addr:
6917       return "DW_FORM_ref_addr";
6918     case DW_FORM_ref1:
6919       return "DW_FORM_ref1";
6920     case DW_FORM_ref2:
6921       return "DW_FORM_ref2";
6922     case DW_FORM_ref4:
6923       return "DW_FORM_ref4";
6924     case DW_FORM_ref8:
6925       return "DW_FORM_ref8";
6926     case DW_FORM_ref_udata:
6927       return "DW_FORM_ref_udata";
6928     case DW_FORM_indirect:
6929       return "DW_FORM_indirect";
6930     case DW_FORM_sec_offset:
6931       return "DW_FORM_sec_offset";
6932     case DW_FORM_exprloc:
6933       return "DW_FORM_exprloc";
6934     case DW_FORM_flag_present:
6935       return "DW_FORM_flag_present";
6936     case DW_FORM_ref_sig8:
6937       return "DW_FORM_ref_sig8";
6938     default:
6939       return "DW_FORM_<unknown>";
6940     }
6941 }
6942 \f
6943 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6944    instance of an inlined instance of a decl which is local to an inline
6945    function, so we have to trace all of the way back through the origin chain
6946    to find out what sort of node actually served as the original seed for the
6947    given block.  */
6948
6949 static tree
6950 decl_ultimate_origin (const_tree decl)
6951 {
6952   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6953     return NULL_TREE;
6954
6955   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6956      nodes in the function to point to themselves; ignore that if
6957      we're trying to output the abstract instance of this function.  */
6958   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6959     return NULL_TREE;
6960
6961   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6962      most distant ancestor, this should never happen.  */
6963   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6964
6965   return DECL_ABSTRACT_ORIGIN (decl);
6966 }
6967
6968 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6969    of a virtual function may refer to a base class, so we check the 'this'
6970    parameter.  */
6971
6972 static tree
6973 decl_class_context (tree decl)
6974 {
6975   tree context = NULL_TREE;
6976
6977   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6978     context = DECL_CONTEXT (decl);
6979   else
6980     context = TYPE_MAIN_VARIANT
6981       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6982
6983   if (context && !TYPE_P (context))
6984     context = NULL_TREE;
6985
6986   return context;
6987 }
6988 \f
6989 /* Add an attribute/value pair to a DIE.  */
6990
6991 static inline void
6992 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6993 {
6994   /* Maybe this should be an assert?  */
6995   if (die == NULL)
6996     return;
6997
6998   if (die->die_attr == NULL)
6999     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7000   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7001 }
7002
7003 static inline enum dw_val_class
7004 AT_class (dw_attr_ref a)
7005 {
7006   return a->dw_attr_val.val_class;
7007 }
7008
7009 /* Add a flag value attribute to a DIE.  */
7010
7011 static inline void
7012 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7013 {
7014   dw_attr_node attr;
7015
7016   attr.dw_attr = attr_kind;
7017   attr.dw_attr_val.val_class = dw_val_class_flag;
7018   attr.dw_attr_val.v.val_flag = flag;
7019   add_dwarf_attr (die, &attr);
7020 }
7021
7022 static inline unsigned
7023 AT_flag (dw_attr_ref a)
7024 {
7025   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7026   return a->dw_attr_val.v.val_flag;
7027 }
7028
7029 /* Add a signed integer attribute value to a DIE.  */
7030
7031 static inline void
7032 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7033 {
7034   dw_attr_node attr;
7035
7036   attr.dw_attr = attr_kind;
7037   attr.dw_attr_val.val_class = dw_val_class_const;
7038   attr.dw_attr_val.v.val_int = int_val;
7039   add_dwarf_attr (die, &attr);
7040 }
7041
7042 static inline HOST_WIDE_INT
7043 AT_int (dw_attr_ref a)
7044 {
7045   gcc_assert (a && AT_class (a) == dw_val_class_const);
7046   return a->dw_attr_val.v.val_int;
7047 }
7048
7049 /* Add an unsigned integer attribute value to a DIE.  */
7050
7051 static inline void
7052 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7053                  unsigned HOST_WIDE_INT unsigned_val)
7054 {
7055   dw_attr_node attr;
7056
7057   attr.dw_attr = attr_kind;
7058   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7059   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7060   add_dwarf_attr (die, &attr);
7061 }
7062
7063 static inline unsigned HOST_WIDE_INT
7064 AT_unsigned (dw_attr_ref a)
7065 {
7066   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7067   return a->dw_attr_val.v.val_unsigned;
7068 }
7069
7070 /* Add an unsigned double integer attribute value to a DIE.  */
7071
7072 static inline void
7073 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7074                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7075 {
7076   dw_attr_node attr;
7077
7078   attr.dw_attr = attr_kind;
7079   attr.dw_attr_val.val_class = dw_val_class_const_double;
7080   attr.dw_attr_val.v.val_double.high = high;
7081   attr.dw_attr_val.v.val_double.low = low;
7082   add_dwarf_attr (die, &attr);
7083 }
7084
7085 /* Add a floating point attribute value to a DIE and return it.  */
7086
7087 static inline void
7088 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7089             unsigned int length, unsigned int elt_size, unsigned char *array)
7090 {
7091   dw_attr_node attr;
7092
7093   attr.dw_attr = attr_kind;
7094   attr.dw_attr_val.val_class = dw_val_class_vec;
7095   attr.dw_attr_val.v.val_vec.length = length;
7096   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7097   attr.dw_attr_val.v.val_vec.array = array;
7098   add_dwarf_attr (die, &attr);
7099 }
7100
7101 /* Add an 8-byte data attribute value to a DIE.  */
7102
7103 static inline void
7104 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7105               unsigned char data8[8])
7106 {
7107   dw_attr_node attr;
7108
7109   attr.dw_attr = attr_kind;
7110   attr.dw_attr_val.val_class = dw_val_class_data8;
7111   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7112   add_dwarf_attr (die, &attr);
7113 }
7114
7115 /* Hash and equality functions for debug_str_hash.  */
7116
7117 static hashval_t
7118 debug_str_do_hash (const void *x)
7119 {
7120   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7121 }
7122
7123 static int
7124 debug_str_eq (const void *x1, const void *x2)
7125 {
7126   return strcmp ((((const struct indirect_string_node *)x1)->str),
7127                  (const char *)x2) == 0;
7128 }
7129
7130 /* Add STR to the indirect string hash table.  */
7131
7132 static struct indirect_string_node *
7133 find_AT_string (const char *str)
7134 {
7135   struct indirect_string_node *node;
7136   void **slot;
7137
7138   if (! debug_str_hash)
7139     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7140                                       debug_str_eq, NULL);
7141
7142   slot = htab_find_slot_with_hash (debug_str_hash, str,
7143                                    htab_hash_string (str), INSERT);
7144   if (*slot == NULL)
7145     {
7146       node = ggc_alloc_cleared_indirect_string_node ();
7147       node->str = ggc_strdup (str);
7148       *slot = node;
7149     }
7150   else
7151     node = (struct indirect_string_node *) *slot;
7152
7153   node->refcount++;
7154   return node;
7155 }
7156
7157 /* Add a string attribute value to a DIE.  */
7158
7159 static inline void
7160 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7161 {
7162   dw_attr_node attr;
7163   struct indirect_string_node *node;
7164
7165   node = find_AT_string (str);
7166
7167   attr.dw_attr = attr_kind;
7168   attr.dw_attr_val.val_class = dw_val_class_str;
7169   attr.dw_attr_val.v.val_str = node;
7170   add_dwarf_attr (die, &attr);
7171 }
7172
7173 /* Create a label for an indirect string node, ensuring it is going to
7174    be output, unless its reference count goes down to zero.  */
7175
7176 static inline void
7177 gen_label_for_indirect_string (struct indirect_string_node *node)
7178 {
7179   char label[32];
7180
7181   if (node->label)
7182     return;
7183
7184   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7185   ++dw2_string_counter;
7186   node->label = xstrdup (label);
7187 }
7188
7189 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7190    debug string STR.  */
7191
7192 static inline rtx
7193 get_debug_string_label (const char *str)
7194 {
7195   struct indirect_string_node *node = find_AT_string (str);
7196
7197   debug_str_hash_forced = true;
7198
7199   gen_label_for_indirect_string (node);
7200
7201   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7202 }
7203
7204 static inline const char *
7205 AT_string (dw_attr_ref a)
7206 {
7207   gcc_assert (a && AT_class (a) == dw_val_class_str);
7208   return a->dw_attr_val.v.val_str->str;
7209 }
7210
7211 /* Find out whether a string should be output inline in DIE
7212    or out-of-line in .debug_str section.  */
7213
7214 static enum dwarf_form
7215 AT_string_form (dw_attr_ref a)
7216 {
7217   struct indirect_string_node *node;
7218   unsigned int len;
7219
7220   gcc_assert (a && AT_class (a) == dw_val_class_str);
7221
7222   node = a->dw_attr_val.v.val_str;
7223   if (node->form)
7224     return node->form;
7225
7226   len = strlen (node->str) + 1;
7227
7228   /* If the string is shorter or equal to the size of the reference, it is
7229      always better to put it inline.  */
7230   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7231     return node->form = DW_FORM_string;
7232
7233   /* If we cannot expect the linker to merge strings in .debug_str
7234      section, only put it into .debug_str if it is worth even in this
7235      single module.  */
7236   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7237       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7238       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7239     return node->form = DW_FORM_string;
7240
7241   gen_label_for_indirect_string (node);
7242
7243   return node->form = DW_FORM_strp;
7244 }
7245
7246 /* Add a DIE reference attribute value to a DIE.  */
7247
7248 static inline void
7249 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7250 {
7251   dw_attr_node attr;
7252
7253   attr.dw_attr = attr_kind;
7254   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7255   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7256   attr.dw_attr_val.v.val_die_ref.external = 0;
7257   add_dwarf_attr (die, &attr);
7258 }
7259
7260 /* Add an AT_specification attribute to a DIE, and also make the back
7261    pointer from the specification to the definition.  */
7262
7263 static inline void
7264 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7265 {
7266   add_AT_die_ref (die, DW_AT_specification, targ_die);
7267   gcc_assert (!targ_die->die_definition);
7268   targ_die->die_definition = die;
7269 }
7270
7271 static inline dw_die_ref
7272 AT_ref (dw_attr_ref a)
7273 {
7274   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7275   return a->dw_attr_val.v.val_die_ref.die;
7276 }
7277
7278 static inline int
7279 AT_ref_external (dw_attr_ref a)
7280 {
7281   if (a && AT_class (a) == dw_val_class_die_ref)
7282     return a->dw_attr_val.v.val_die_ref.external;
7283
7284   return 0;
7285 }
7286
7287 static inline void
7288 set_AT_ref_external (dw_attr_ref a, int i)
7289 {
7290   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7291   a->dw_attr_val.v.val_die_ref.external = i;
7292 }
7293
7294 /* Add an FDE reference attribute value to a DIE.  */
7295
7296 static inline void
7297 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7298 {
7299   dw_attr_node attr;
7300
7301   attr.dw_attr = attr_kind;
7302   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7303   attr.dw_attr_val.v.val_fde_index = targ_fde;
7304   add_dwarf_attr (die, &attr);
7305 }
7306
7307 /* Add a location description attribute value to a DIE.  */
7308
7309 static inline void
7310 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7311 {
7312   dw_attr_node attr;
7313
7314   attr.dw_attr = attr_kind;
7315   attr.dw_attr_val.val_class = dw_val_class_loc;
7316   attr.dw_attr_val.v.val_loc = loc;
7317   add_dwarf_attr (die, &attr);
7318 }
7319
7320 static inline dw_loc_descr_ref
7321 AT_loc (dw_attr_ref a)
7322 {
7323   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7324   return a->dw_attr_val.v.val_loc;
7325 }
7326
7327 static inline void
7328 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7329 {
7330   dw_attr_node attr;
7331
7332   attr.dw_attr = attr_kind;
7333   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7334   attr.dw_attr_val.v.val_loc_list = loc_list;
7335   add_dwarf_attr (die, &attr);
7336   have_location_lists = true;
7337 }
7338
7339 static inline dw_loc_list_ref
7340 AT_loc_list (dw_attr_ref a)
7341 {
7342   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7343   return a->dw_attr_val.v.val_loc_list;
7344 }
7345
7346 static inline dw_loc_list_ref *
7347 AT_loc_list_ptr (dw_attr_ref a)
7348 {
7349   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7350   return &a->dw_attr_val.v.val_loc_list;
7351 }
7352
7353 /* Add an address constant attribute value to a DIE.  */
7354
7355 static inline void
7356 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7357 {
7358   dw_attr_node attr;
7359
7360   attr.dw_attr = attr_kind;
7361   attr.dw_attr_val.val_class = dw_val_class_addr;
7362   attr.dw_attr_val.v.val_addr = addr;
7363   add_dwarf_attr (die, &attr);
7364 }
7365
7366 /* Get the RTX from to an address DIE attribute.  */
7367
7368 static inline rtx
7369 AT_addr (dw_attr_ref a)
7370 {
7371   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7372   return a->dw_attr_val.v.val_addr;
7373 }
7374
7375 /* Add a file attribute value to a DIE.  */
7376
7377 static inline void
7378 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7379              struct dwarf_file_data *fd)
7380 {
7381   dw_attr_node attr;
7382
7383   attr.dw_attr = attr_kind;
7384   attr.dw_attr_val.val_class = dw_val_class_file;
7385   attr.dw_attr_val.v.val_file = fd;
7386   add_dwarf_attr (die, &attr);
7387 }
7388
7389 /* Get the dwarf_file_data from a file DIE attribute.  */
7390
7391 static inline struct dwarf_file_data *
7392 AT_file (dw_attr_ref a)
7393 {
7394   gcc_assert (a && AT_class (a) == dw_val_class_file);
7395   return a->dw_attr_val.v.val_file;
7396 }
7397
7398 /* Add a vms delta attribute value to a DIE.  */
7399
7400 static inline void
7401 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7402                   const char *lbl1, const char *lbl2)
7403 {
7404   dw_attr_node attr;
7405
7406   attr.dw_attr = attr_kind;
7407   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7408   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7409   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7410   add_dwarf_attr (die, &attr);
7411 }
7412
7413 /* Add a label identifier attribute value to a DIE.  */
7414
7415 static inline void
7416 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7417 {
7418   dw_attr_node attr;
7419
7420   attr.dw_attr = attr_kind;
7421   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7422   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7423   add_dwarf_attr (die, &attr);
7424 }
7425
7426 /* Add a section offset attribute value to a DIE, an offset into the
7427    debug_line section.  */
7428
7429 static inline void
7430 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7431                 const char *label)
7432 {
7433   dw_attr_node attr;
7434
7435   attr.dw_attr = attr_kind;
7436   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7437   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7438   add_dwarf_attr (die, &attr);
7439 }
7440
7441 /* Add a section offset attribute value to a DIE, an offset into the
7442    debug_macinfo section.  */
7443
7444 static inline void
7445 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7446                const char *label)
7447 {
7448   dw_attr_node attr;
7449
7450   attr.dw_attr = attr_kind;
7451   attr.dw_attr_val.val_class = dw_val_class_macptr;
7452   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7453   add_dwarf_attr (die, &attr);
7454 }
7455
7456 /* Add an offset attribute value to a DIE.  */
7457
7458 static inline void
7459 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7460                unsigned HOST_WIDE_INT offset)
7461 {
7462   dw_attr_node attr;
7463
7464   attr.dw_attr = attr_kind;
7465   attr.dw_attr_val.val_class = dw_val_class_offset;
7466   attr.dw_attr_val.v.val_offset = offset;
7467   add_dwarf_attr (die, &attr);
7468 }
7469
7470 /* Add an range_list attribute value to a DIE.  */
7471
7472 static void
7473 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7474                    long unsigned int offset)
7475 {
7476   dw_attr_node attr;
7477
7478   attr.dw_attr = attr_kind;
7479   attr.dw_attr_val.val_class = dw_val_class_range_list;
7480   attr.dw_attr_val.v.val_offset = offset;
7481   add_dwarf_attr (die, &attr);
7482 }
7483
7484 /* Return the start label of a delta attribute.  */
7485
7486 static inline const char *
7487 AT_vms_delta1 (dw_attr_ref a)
7488 {
7489   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7490   return a->dw_attr_val.v.val_vms_delta.lbl1;
7491 }
7492
7493 /* Return the end label of a delta attribute.  */
7494
7495 static inline const char *
7496 AT_vms_delta2 (dw_attr_ref a)
7497 {
7498   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7499   return a->dw_attr_val.v.val_vms_delta.lbl2;
7500 }
7501
7502 static inline const char *
7503 AT_lbl (dw_attr_ref a)
7504 {
7505   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7506                     || AT_class (a) == dw_val_class_lineptr
7507                     || AT_class (a) == dw_val_class_macptr));
7508   return a->dw_attr_val.v.val_lbl_id;
7509 }
7510
7511 /* Get the attribute of type attr_kind.  */
7512
7513 static dw_attr_ref
7514 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7515 {
7516   dw_attr_ref a;
7517   unsigned ix;
7518   dw_die_ref spec = NULL;
7519
7520   if (! die)
7521     return NULL;
7522
7523   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7524     if (a->dw_attr == attr_kind)
7525       return a;
7526     else if (a->dw_attr == DW_AT_specification
7527              || a->dw_attr == DW_AT_abstract_origin)
7528       spec = AT_ref (a);
7529
7530   if (spec)
7531     return get_AT (spec, attr_kind);
7532
7533   return NULL;
7534 }
7535
7536 /* Return the "low pc" attribute value, typically associated with a subprogram
7537    DIE.  Return null if the "low pc" attribute is either not present, or if it
7538    cannot be represented as an assembler label identifier.  */
7539
7540 static inline const char *
7541 get_AT_low_pc (dw_die_ref die)
7542 {
7543   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7544
7545   return a ? AT_lbl (a) : NULL;
7546 }
7547
7548 /* Return the "high pc" attribute value, typically associated with a subprogram
7549    DIE.  Return null if the "high pc" attribute is either not present, or if it
7550    cannot be represented as an assembler label identifier.  */
7551
7552 static inline const char *
7553 get_AT_hi_pc (dw_die_ref die)
7554 {
7555   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7556
7557   return a ? AT_lbl (a) : NULL;
7558 }
7559
7560 /* Return the value of the string attribute designated by ATTR_KIND, or
7561    NULL if it is not present.  */
7562
7563 static inline const char *
7564 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7565 {
7566   dw_attr_ref a = get_AT (die, attr_kind);
7567
7568   return a ? AT_string (a) : NULL;
7569 }
7570
7571 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7572    if it is not present.  */
7573
7574 static inline int
7575 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7576 {
7577   dw_attr_ref a = get_AT (die, attr_kind);
7578
7579   return a ? AT_flag (a) : 0;
7580 }
7581
7582 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7583    if it is not present.  */
7584
7585 static inline unsigned
7586 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7587 {
7588   dw_attr_ref a = get_AT (die, attr_kind);
7589
7590   return a ? AT_unsigned (a) : 0;
7591 }
7592
7593 static inline dw_die_ref
7594 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7595 {
7596   dw_attr_ref a = get_AT (die, attr_kind);
7597
7598   return a ? AT_ref (a) : NULL;
7599 }
7600
7601 static inline struct dwarf_file_data *
7602 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7603 {
7604   dw_attr_ref a = get_AT (die, attr_kind);
7605
7606   return a ? AT_file (a) : NULL;
7607 }
7608
7609 /* Return TRUE if the language is C++.  */
7610
7611 static inline bool
7612 is_cxx (void)
7613 {
7614   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7615
7616   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7617 }
7618
7619 /* Return TRUE if the language is Fortran.  */
7620
7621 static inline bool
7622 is_fortran (void)
7623 {
7624   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7625
7626   return (lang == DW_LANG_Fortran77
7627           || lang == DW_LANG_Fortran90
7628           || lang == DW_LANG_Fortran95);
7629 }
7630
7631 /* Return TRUE if the language is Ada.  */
7632
7633 static inline bool
7634 is_ada (void)
7635 {
7636   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7637
7638   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7639 }
7640
7641 /* Remove the specified attribute if present.  */
7642
7643 static void
7644 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7645 {
7646   dw_attr_ref a;
7647   unsigned ix;
7648
7649   if (! die)
7650     return;
7651
7652   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7653     if (a->dw_attr == attr_kind)
7654       {
7655         if (AT_class (a) == dw_val_class_str)
7656           if (a->dw_attr_val.v.val_str->refcount)
7657             a->dw_attr_val.v.val_str->refcount--;
7658
7659         /* VEC_ordered_remove should help reduce the number of abbrevs
7660            that are needed.  */
7661         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7662         return;
7663       }
7664 }
7665
7666 /* Remove CHILD from its parent.  PREV must have the property that
7667    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7668
7669 static void
7670 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7671 {
7672   gcc_assert (child->die_parent == prev->die_parent);
7673   gcc_assert (prev->die_sib == child);
7674   if (prev == child)
7675     {
7676       gcc_assert (child->die_parent->die_child == child);
7677       prev = NULL;
7678     }
7679   else
7680     prev->die_sib = child->die_sib;
7681   if (child->die_parent->die_child == child)
7682     child->die_parent->die_child = prev;
7683 }
7684
7685 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7686    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7687
7688 static void
7689 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7690 {
7691   dw_die_ref parent = old_child->die_parent;
7692
7693   gcc_assert (parent == prev->die_parent);
7694   gcc_assert (prev->die_sib == old_child);
7695
7696   new_child->die_parent = parent;
7697   if (prev == old_child)
7698     {
7699       gcc_assert (parent->die_child == old_child);
7700       new_child->die_sib = new_child;
7701     }
7702   else
7703     {
7704       prev->die_sib = new_child;
7705       new_child->die_sib = old_child->die_sib;
7706     }
7707   if (old_child->die_parent->die_child == old_child)
7708     old_child->die_parent->die_child = new_child;
7709 }
7710
7711 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7712
7713 static void
7714 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7715 {
7716   dw_die_ref c;
7717   new_parent->die_child = old_parent->die_child;
7718   old_parent->die_child = NULL;
7719   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7720 }
7721
7722 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7723    matches TAG.  */
7724
7725 static void
7726 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7727 {
7728   dw_die_ref c;
7729
7730   c = die->die_child;
7731   if (c) do {
7732     dw_die_ref prev = c;
7733     c = c->die_sib;
7734     while (c->die_tag == tag)
7735       {
7736         remove_child_with_prev (c, prev);
7737         /* Might have removed every child.  */
7738         if (c == c->die_sib)
7739           return;
7740         c = c->die_sib;
7741       }
7742   } while (c != die->die_child);
7743 }
7744
7745 /* Add a CHILD_DIE as the last child of DIE.  */
7746
7747 static void
7748 add_child_die (dw_die_ref die, dw_die_ref child_die)
7749 {
7750   /* FIXME this should probably be an assert.  */
7751   if (! die || ! child_die)
7752     return;
7753   gcc_assert (die != child_die);
7754
7755   child_die->die_parent = die;
7756   if (die->die_child)
7757     {
7758       child_die->die_sib = die->die_child->die_sib;
7759       die->die_child->die_sib = child_die;
7760     }
7761   else
7762     child_die->die_sib = child_die;
7763   die->die_child = child_die;
7764 }
7765
7766 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7767    is the specification, to the end of PARENT's list of children.
7768    This is done by removing and re-adding it.  */
7769
7770 static void
7771 splice_child_die (dw_die_ref parent, dw_die_ref child)
7772 {
7773   dw_die_ref p;
7774
7775   /* We want the declaration DIE from inside the class, not the
7776      specification DIE at toplevel.  */
7777   if (child->die_parent != parent)
7778     {
7779       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7780
7781       if (tmp)
7782         child = tmp;
7783     }
7784
7785   gcc_assert (child->die_parent == parent
7786               || (child->die_parent
7787                   == get_AT_ref (parent, DW_AT_specification)));
7788
7789   for (p = child->die_parent->die_child; ; p = p->die_sib)
7790     if (p->die_sib == child)
7791       {
7792         remove_child_with_prev (child, p);
7793         break;
7794       }
7795
7796   add_child_die (parent, child);
7797 }
7798
7799 /* Return a pointer to a newly created DIE node.  */
7800
7801 static inline dw_die_ref
7802 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7803 {
7804   dw_die_ref die = ggc_alloc_cleared_die_node ();
7805
7806   die->die_tag = tag_value;
7807
7808   if (parent_die != NULL)
7809     add_child_die (parent_die, die);
7810   else
7811     {
7812       limbo_die_node *limbo_node;
7813
7814       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7815       limbo_node->die = die;
7816       limbo_node->created_for = t;
7817       limbo_node->next = limbo_die_list;
7818       limbo_die_list = limbo_node;
7819     }
7820
7821   return die;
7822 }
7823
7824 /* Return the DIE associated with the given type specifier.  */
7825
7826 static inline dw_die_ref
7827 lookup_type_die (tree type)
7828 {
7829   return TYPE_SYMTAB_DIE (type);
7830 }
7831
7832 /* Equate a DIE to a given type specifier.  */
7833
7834 static inline void
7835 equate_type_number_to_die (tree type, dw_die_ref type_die)
7836 {
7837   TYPE_SYMTAB_DIE (type) = type_die;
7838 }
7839
7840 /* Returns a hash value for X (which really is a die_struct).  */
7841
7842 static hashval_t
7843 decl_die_table_hash (const void *x)
7844 {
7845   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7846 }
7847
7848 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7849
7850 static int
7851 decl_die_table_eq (const void *x, const void *y)
7852 {
7853   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7854 }
7855
7856 /* Return the DIE associated with a given declaration.  */
7857
7858 static inline dw_die_ref
7859 lookup_decl_die (tree decl)
7860 {
7861   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7862 }
7863
7864 /* Returns a hash value for X (which really is a var_loc_list).  */
7865
7866 static hashval_t
7867 decl_loc_table_hash (const void *x)
7868 {
7869   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7870 }
7871
7872 /* Return nonzero if decl_id of var_loc_list X is the same as
7873    UID of decl *Y.  */
7874
7875 static int
7876 decl_loc_table_eq (const void *x, const void *y)
7877 {
7878   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7879 }
7880
7881 /* Return the var_loc list associated with a given declaration.  */
7882
7883 static inline var_loc_list *
7884 lookup_decl_loc (const_tree decl)
7885 {
7886   if (!decl_loc_table)
7887     return NULL;
7888   return (var_loc_list *)
7889     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7890 }
7891
7892 /* Equate a DIE to a particular declaration.  */
7893
7894 static void
7895 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7896 {
7897   unsigned int decl_id = DECL_UID (decl);
7898   void **slot;
7899
7900   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7901   *slot = decl_die;
7902   decl_die->decl_id = decl_id;
7903 }
7904
7905 /* Return how many bits covers PIECE EXPR_LIST.  */
7906
7907 static int
7908 decl_piece_bitsize (rtx piece)
7909 {
7910   int ret = (int) GET_MODE (piece);
7911   if (ret)
7912     return ret;
7913   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7914               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7915   return INTVAL (XEXP (XEXP (piece, 0), 0));
7916 }
7917
7918 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7919
7920 static rtx *
7921 decl_piece_varloc_ptr (rtx piece)
7922 {
7923   if ((int) GET_MODE (piece))
7924     return &XEXP (piece, 0);
7925   else
7926     return &XEXP (XEXP (piece, 0), 1);
7927 }
7928
7929 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7930    Next is the chain of following piece nodes.  */
7931
7932 static rtx
7933 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7934 {
7935   if (bitsize <= (int) MAX_MACHINE_MODE)
7936     return alloc_EXPR_LIST (bitsize, loc_note, next);
7937   else
7938     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7939                                                GEN_INT (bitsize),
7940                                                loc_note), next);
7941 }
7942
7943 /* Return rtx that should be stored into loc field for
7944    LOC_NOTE and BITPOS/BITSIZE.  */
7945
7946 static rtx
7947 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7948                       HOST_WIDE_INT bitsize)
7949 {
7950   if (bitsize != -1)
7951     {
7952       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7953       if (bitpos != 0)
7954         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7955     }
7956   return loc_note;
7957 }
7958
7959 /* This function either modifies location piece list *DEST in
7960    place (if SRC and INNER is NULL), or copies location piece list
7961    *SRC to *DEST while modifying it.  Location BITPOS is modified
7962    to contain LOC_NOTE, any pieces overlapping it are removed resp.
7963    not copied and if needed some padding around it is added.
7964    When modifying in place, DEST should point to EXPR_LIST where
7965    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
7966    to the start of the whole list and INNER points to the EXPR_LIST
7967    where earlier pieces cover PIECE_BITPOS bits.  */
7968
7969 static void
7970 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
7971                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
7972                    HOST_WIDE_INT bitsize, rtx loc_note)
7973 {
7974   int diff;
7975   bool copy = inner != NULL;
7976
7977   if (copy)
7978     {
7979       /* First copy all nodes preceeding the current bitpos.  */
7980       while (src != inner)
7981         {
7982           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
7983                                    decl_piece_bitsize (*src), NULL_RTX);
7984           dest = &XEXP (*dest, 1);
7985           src = &XEXP (*src, 1);
7986         }
7987     }
7988   /* Add padding if needed.  */
7989   if (bitpos != piece_bitpos)
7990     {
7991       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
7992                                copy ? NULL_RTX : *dest);
7993       dest = &XEXP (*dest, 1);
7994     }
7995   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
7996     {
7997       gcc_assert (!copy);
7998       /* A piece with correct bitpos and bitsize already exist,
7999          just update the location for it and return.  */
8000       *decl_piece_varloc_ptr (*dest) = loc_note;
8001       return;
8002     }
8003   /* Add the piece that changed.  */
8004   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8005   dest = &XEXP (*dest, 1);
8006   /* Skip over pieces that overlap it.  */
8007   diff = bitpos - piece_bitpos + bitsize;
8008   if (!copy)
8009     src = dest;
8010   while (diff > 0 && *src)
8011     {
8012       rtx piece = *src;
8013       diff -= decl_piece_bitsize (piece);
8014       if (copy)
8015         src = &XEXP (piece, 1);
8016       else
8017         {
8018           *src = XEXP (piece, 1);
8019           free_EXPR_LIST_node (piece);
8020         }
8021     }
8022   /* Add padding if needed.  */
8023   if (diff < 0 && *src)
8024     {
8025       if (!copy)
8026         dest = src;
8027       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8028       dest = &XEXP (*dest, 1);
8029     }
8030   if (!copy)
8031     return;
8032   /* Finally copy all nodes following it.  */
8033   while (*src)
8034     {
8035       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8036                                decl_piece_bitsize (*src), NULL_RTX);
8037       dest = &XEXP (*dest, 1);
8038       src = &XEXP (*src, 1);
8039     }
8040 }
8041
8042 /* Add a variable location node to the linked list for DECL.  */
8043
8044 static struct var_loc_node *
8045 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8046 {
8047   unsigned int decl_id;
8048   var_loc_list *temp;
8049   void **slot;
8050   struct var_loc_node *loc = NULL;
8051   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8052
8053   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8054     {
8055       tree realdecl = DECL_DEBUG_EXPR (decl);
8056       if (realdecl && handled_component_p (realdecl))
8057         {
8058           HOST_WIDE_INT maxsize;
8059           tree innerdecl;
8060           innerdecl
8061             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8062           if (!DECL_P (innerdecl)
8063               || DECL_IGNORED_P (innerdecl)
8064               || TREE_STATIC (innerdecl)
8065               || bitsize <= 0
8066               || bitpos + bitsize > 256
8067               || bitsize != maxsize)
8068             return NULL;
8069           decl = innerdecl;
8070         }
8071     }
8072
8073   decl_id = DECL_UID (decl);
8074   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8075   if (*slot == NULL)
8076     {
8077       temp = ggc_alloc_cleared_var_loc_list ();
8078       temp->decl_id = decl_id;
8079       *slot = temp;
8080     }
8081   else
8082     temp = (var_loc_list *) *slot;
8083
8084   if (temp->last)
8085     {
8086       struct var_loc_node *last = temp->last, *unused = NULL;
8087       rtx *piece_loc = NULL, last_loc_note;
8088       int piece_bitpos = 0;
8089       if (last->next)
8090         {
8091           last = last->next;
8092           gcc_assert (last->next == NULL);
8093         }
8094       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8095         {
8096           piece_loc = &last->loc;
8097           do
8098             {
8099               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8100               if (piece_bitpos + cur_bitsize > bitpos)
8101                 break;
8102               piece_bitpos += cur_bitsize;
8103               piece_loc = &XEXP (*piece_loc, 1);
8104             }
8105           while (*piece_loc);
8106         }
8107       /* TEMP->LAST here is either pointer to the last but one or
8108          last element in the chained list, LAST is pointer to the
8109          last element.  */
8110       if (label && strcmp (last->label, label) == 0)
8111         {
8112           /* For SRA optimized variables if there weren't any real
8113              insns since last note, just modify the last node.  */
8114           if (piece_loc != NULL)
8115             {
8116               adjust_piece_list (piece_loc, NULL, NULL,
8117                                  bitpos, piece_bitpos, bitsize, loc_note);
8118               return NULL;
8119             }
8120           /* If the last note doesn't cover any instructions, remove it.  */
8121           if (temp->last != last)
8122             {
8123               temp->last->next = NULL;
8124               unused = last;
8125               last = temp->last;
8126               gcc_assert (strcmp (last->label, label) != 0);
8127             }
8128           else
8129             {
8130               gcc_assert (temp->first == temp->last);
8131               memset (temp->last, '\0', sizeof (*temp->last));
8132               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8133               return temp->last;
8134             }
8135         }
8136       if (bitsize == -1 && NOTE_P (last->loc))
8137         last_loc_note = last->loc;
8138       else if (piece_loc != NULL
8139                && *piece_loc != NULL_RTX
8140                && piece_bitpos == bitpos
8141                && decl_piece_bitsize (*piece_loc) == bitsize)
8142         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8143       else
8144         last_loc_note = NULL_RTX;
8145       /* If the current location is the same as the end of the list,
8146          and either both or neither of the locations is uninitialized,
8147          we have nothing to do.  */
8148       if (last_loc_note == NULL_RTX
8149           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8150                             NOTE_VAR_LOCATION_LOC (loc_note)))
8151           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8152                != NOTE_VAR_LOCATION_STATUS (loc_note))
8153               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8154                    == VAR_INIT_STATUS_UNINITIALIZED)
8155                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8156                       == VAR_INIT_STATUS_UNINITIALIZED))))
8157         {
8158           /* Add LOC to the end of list and update LAST.  If the last
8159              element of the list has been removed above, reuse its
8160              memory for the new node, otherwise allocate a new one.  */
8161           if (unused)
8162             {
8163               loc = unused;
8164               memset (loc, '\0', sizeof (*loc));
8165             }
8166           else
8167             loc = ggc_alloc_cleared_var_loc_node ();
8168           if (bitsize == -1 || piece_loc == NULL)
8169             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8170           else
8171             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8172                                bitpos, piece_bitpos, bitsize, loc_note);
8173           last->next = loc;
8174           /* Ensure TEMP->LAST will point either to the new last but one
8175              element of the chain, or to the last element in it.  */
8176           if (last != temp->last)
8177             temp->last = last;
8178         }
8179       else if (unused)
8180         ggc_free (unused);
8181     }
8182   else
8183     {
8184       loc = ggc_alloc_cleared_var_loc_node ();
8185       temp->first = loc;
8186       temp->last = loc;
8187       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8188     }
8189   return loc;
8190 }
8191 \f
8192 /* Keep track of the number of spaces used to indent the
8193    output of the debugging routines that print the structure of
8194    the DIE internal representation.  */
8195 static int print_indent;
8196
8197 /* Indent the line the number of spaces given by print_indent.  */
8198
8199 static inline void
8200 print_spaces (FILE *outfile)
8201 {
8202   fprintf (outfile, "%*s", print_indent, "");
8203 }
8204
8205 /* Print a type signature in hex.  */
8206
8207 static inline void
8208 print_signature (FILE *outfile, char *sig)
8209 {
8210   int i;
8211
8212   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8213     fprintf (outfile, "%02x", sig[i] & 0xff);
8214 }
8215
8216 /* Print the information associated with a given DIE, and its children.
8217    This routine is a debugging aid only.  */
8218
8219 static void
8220 print_die (dw_die_ref die, FILE *outfile)
8221 {
8222   dw_attr_ref a;
8223   dw_die_ref c;
8224   unsigned ix;
8225
8226   print_spaces (outfile);
8227   fprintf (outfile, "DIE %4ld: %s\n",
8228            die->die_offset, dwarf_tag_name (die->die_tag));
8229   print_spaces (outfile);
8230   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8231   fprintf (outfile, " offset: %ld\n", die->die_offset);
8232   if (dwarf_version >= 4 && die->die_id.die_type_node)
8233     {
8234       print_spaces (outfile);
8235       fprintf (outfile, "  signature: ");
8236       print_signature (outfile, die->die_id.die_type_node->signature);
8237       fprintf (outfile, "\n");
8238     }
8239
8240   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8241     {
8242       print_spaces (outfile);
8243       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8244
8245       switch (AT_class (a))
8246         {
8247         case dw_val_class_addr:
8248           fprintf (outfile, "address");
8249           break;
8250         case dw_val_class_offset:
8251           fprintf (outfile, "offset");
8252           break;
8253         case dw_val_class_loc:
8254           fprintf (outfile, "location descriptor");
8255           break;
8256         case dw_val_class_loc_list:
8257           fprintf (outfile, "location list -> label:%s",
8258                    AT_loc_list (a)->ll_symbol);
8259           break;
8260         case dw_val_class_range_list:
8261           fprintf (outfile, "range list");
8262           break;
8263         case dw_val_class_const:
8264           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8265           break;
8266         case dw_val_class_unsigned_const:
8267           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8268           break;
8269         case dw_val_class_const_double:
8270           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8271                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8272                    a->dw_attr_val.v.val_double.high,
8273                    a->dw_attr_val.v.val_double.low);
8274           break;
8275         case dw_val_class_vec:
8276           fprintf (outfile, "floating-point or vector constant");
8277           break;
8278         case dw_val_class_flag:
8279           fprintf (outfile, "%u", AT_flag (a));
8280           break;
8281         case dw_val_class_die_ref:
8282           if (AT_ref (a) != NULL)
8283             {
8284               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8285                 {
8286                   fprintf (outfile, "die -> signature: ");
8287                   print_signature (outfile,
8288                                    AT_ref (a)->die_id.die_type_node->signature);
8289                 }
8290               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8291                 fprintf (outfile, "die -> label: %s",
8292                          AT_ref (a)->die_id.die_symbol);
8293               else
8294                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8295             }
8296           else
8297             fprintf (outfile, "die -> <null>");
8298           break;
8299         case dw_val_class_vms_delta:
8300           fprintf (outfile, "delta: @slotcount(%s-%s)",
8301                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8302           break;
8303         case dw_val_class_lbl_id:
8304         case dw_val_class_lineptr:
8305         case dw_val_class_macptr:
8306           fprintf (outfile, "label: %s", AT_lbl (a));
8307           break;
8308         case dw_val_class_str:
8309           if (AT_string (a) != NULL)
8310             fprintf (outfile, "\"%s\"", AT_string (a));
8311           else
8312             fprintf (outfile, "<null>");
8313           break;
8314         case dw_val_class_file:
8315           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8316                    AT_file (a)->emitted_number);
8317           break;
8318         case dw_val_class_data8:
8319           {
8320             int i;
8321
8322             for (i = 0; i < 8; i++)
8323               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8324             break;
8325           }
8326         default:
8327           break;
8328         }
8329
8330       fprintf (outfile, "\n");
8331     }
8332
8333   if (die->die_child != NULL)
8334     {
8335       print_indent += 4;
8336       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8337       print_indent -= 4;
8338     }
8339   if (print_indent == 0)
8340     fprintf (outfile, "\n");
8341 }
8342
8343 /* Print the contents of the source code line number correspondence table.
8344    This routine is a debugging aid only.  */
8345
8346 static void
8347 print_dwarf_line_table (FILE *outfile)
8348 {
8349   unsigned i;
8350   dw_line_info_ref line_info;
8351
8352   fprintf (outfile, "\n\nDWARF source line information\n");
8353   for (i = 1; i < line_info_table_in_use; i++)
8354     {
8355       line_info = &line_info_table[i];
8356       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8357                line_info->dw_file_num,
8358                line_info->dw_line_num);
8359     }
8360
8361   fprintf (outfile, "\n\n");
8362 }
8363
8364 /* Print the information collected for a given DIE.  */
8365
8366 DEBUG_FUNCTION void
8367 debug_dwarf_die (dw_die_ref die)
8368 {
8369   print_die (die, stderr);
8370 }
8371
8372 /* Print all DWARF information collected for the compilation unit.
8373    This routine is a debugging aid only.  */
8374
8375 DEBUG_FUNCTION void
8376 debug_dwarf (void)
8377 {
8378   print_indent = 0;
8379   print_die (comp_unit_die, stderr);
8380   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8381     print_dwarf_line_table (stderr);
8382 }
8383 \f
8384 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8385    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8386    DIE that marks the start of the DIEs for this include file.  */
8387
8388 static dw_die_ref
8389 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8390 {
8391   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8392   dw_die_ref new_unit = gen_compile_unit_die (filename);
8393
8394   new_unit->die_sib = old_unit;
8395   return new_unit;
8396 }
8397
8398 /* Close an include-file CU and reopen the enclosing one.  */
8399
8400 static dw_die_ref
8401 pop_compile_unit (dw_die_ref old_unit)
8402 {
8403   dw_die_ref new_unit = old_unit->die_sib;
8404
8405   old_unit->die_sib = NULL;
8406   return new_unit;
8407 }
8408
8409 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8410 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8411
8412 /* Calculate the checksum of a location expression.  */
8413
8414 static inline void
8415 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8416 {
8417   int tem;
8418
8419   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8420   CHECKSUM (tem);
8421   CHECKSUM (loc->dw_loc_oprnd1);
8422   CHECKSUM (loc->dw_loc_oprnd2);
8423 }
8424
8425 /* Calculate the checksum of an attribute.  */
8426
8427 static void
8428 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8429 {
8430   dw_loc_descr_ref loc;
8431   rtx r;
8432
8433   CHECKSUM (at->dw_attr);
8434
8435   /* We don't care that this was compiled with a different compiler
8436      snapshot; if the output is the same, that's what matters.  */
8437   if (at->dw_attr == DW_AT_producer)
8438     return;
8439
8440   switch (AT_class (at))
8441     {
8442     case dw_val_class_const:
8443       CHECKSUM (at->dw_attr_val.v.val_int);
8444       break;
8445     case dw_val_class_unsigned_const:
8446       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8447       break;
8448     case dw_val_class_const_double:
8449       CHECKSUM (at->dw_attr_val.v.val_double);
8450       break;
8451     case dw_val_class_vec:
8452       CHECKSUM (at->dw_attr_val.v.val_vec);
8453       break;
8454     case dw_val_class_flag:
8455       CHECKSUM (at->dw_attr_val.v.val_flag);
8456       break;
8457     case dw_val_class_str:
8458       CHECKSUM_STRING (AT_string (at));
8459       break;
8460
8461     case dw_val_class_addr:
8462       r = AT_addr (at);
8463       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8464       CHECKSUM_STRING (XSTR (r, 0));
8465       break;
8466
8467     case dw_val_class_offset:
8468       CHECKSUM (at->dw_attr_val.v.val_offset);
8469       break;
8470
8471     case dw_val_class_loc:
8472       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8473         loc_checksum (loc, ctx);
8474       break;
8475
8476     case dw_val_class_die_ref:
8477       die_checksum (AT_ref (at), ctx, mark);
8478       break;
8479
8480     case dw_val_class_fde_ref:
8481     case dw_val_class_vms_delta:
8482     case dw_val_class_lbl_id:
8483     case dw_val_class_lineptr:
8484     case dw_val_class_macptr:
8485       break;
8486
8487     case dw_val_class_file:
8488       CHECKSUM_STRING (AT_file (at)->filename);
8489       break;
8490
8491     case dw_val_class_data8:
8492       CHECKSUM (at->dw_attr_val.v.val_data8);
8493       break;
8494
8495     default:
8496       break;
8497     }
8498 }
8499
8500 /* Calculate the checksum of a DIE.  */
8501
8502 static void
8503 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8504 {
8505   dw_die_ref c;
8506   dw_attr_ref a;
8507   unsigned ix;
8508
8509   /* To avoid infinite recursion.  */
8510   if (die->die_mark)
8511     {
8512       CHECKSUM (die->die_mark);
8513       return;
8514     }
8515   die->die_mark = ++(*mark);
8516
8517   CHECKSUM (die->die_tag);
8518
8519   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8520     attr_checksum (a, ctx, mark);
8521
8522   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8523 }
8524
8525 #undef CHECKSUM
8526 #undef CHECKSUM_STRING
8527
8528 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8529 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8530 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8531 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8532 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8533 #define CHECKSUM_ATTR(FOO) \
8534   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8535
8536 /* Calculate the checksum of a number in signed LEB128 format.  */
8537
8538 static void
8539 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8540 {
8541   unsigned char byte;
8542   bool more;
8543
8544   while (1)
8545     {
8546       byte = (value & 0x7f);
8547       value >>= 7;
8548       more = !((value == 0 && (byte & 0x40) == 0)
8549                 || (value == -1 && (byte & 0x40) != 0));
8550       if (more)
8551         byte |= 0x80;
8552       CHECKSUM (byte);
8553       if (!more)
8554         break;
8555     }
8556 }
8557
8558 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8559
8560 static void
8561 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8562 {
8563   while (1)
8564     {
8565       unsigned char byte = (value & 0x7f);
8566       value >>= 7;
8567       if (value != 0)
8568         /* More bytes to follow.  */
8569         byte |= 0x80;
8570       CHECKSUM (byte);
8571       if (value == 0)
8572         break;
8573     }
8574 }
8575
8576 /* Checksum the context of the DIE.  This adds the names of any
8577    surrounding namespaces or structures to the checksum.  */
8578
8579 static void
8580 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8581 {
8582   const char *name;
8583   dw_die_ref spec;
8584   int tag = die->die_tag;
8585
8586   if (tag != DW_TAG_namespace
8587       && tag != DW_TAG_structure_type
8588       && tag != DW_TAG_class_type)
8589     return;
8590
8591   name = get_AT_string (die, DW_AT_name);
8592
8593   spec = get_AT_ref (die, DW_AT_specification);
8594   if (spec != NULL)
8595     die = spec;
8596
8597   if (die->die_parent != NULL)
8598     checksum_die_context (die->die_parent, ctx);
8599
8600   CHECKSUM_ULEB128 ('C');
8601   CHECKSUM_ULEB128 (tag);
8602   if (name != NULL)
8603     CHECKSUM_STRING (name);
8604 }
8605
8606 /* Calculate the checksum of a location expression.  */
8607
8608 static inline void
8609 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8610 {
8611   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8612      were emitted as a DW_FORM_sdata instead of a location expression.  */
8613   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8614     {
8615       CHECKSUM_ULEB128 (DW_FORM_sdata);
8616       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8617       return;
8618     }
8619
8620   /* Otherwise, just checksum the raw location expression.  */
8621   while (loc != NULL)
8622     {
8623       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8624       CHECKSUM (loc->dw_loc_oprnd1);
8625       CHECKSUM (loc->dw_loc_oprnd2);
8626       loc = loc->dw_loc_next;
8627     }
8628 }
8629
8630 /* Calculate the checksum of an attribute.  */
8631
8632 static void
8633 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8634                        struct md5_ctx *ctx, int *mark)
8635 {
8636   dw_loc_descr_ref loc;
8637   rtx r;
8638
8639   if (AT_class (at) == dw_val_class_die_ref)
8640     {
8641       dw_die_ref target_die = AT_ref (at);
8642
8643       /* For pointer and reference types, we checksum only the (qualified)
8644          name of the target type (if there is a name).  For friend entries,
8645          we checksum only the (qualified) name of the target type or function.
8646          This allows the checksum to remain the same whether the target type
8647          is complete or not.  */
8648       if ((at->dw_attr == DW_AT_type
8649            && (tag == DW_TAG_pointer_type
8650                || tag == DW_TAG_reference_type
8651                || tag == DW_TAG_rvalue_reference_type
8652                || tag == DW_TAG_ptr_to_member_type))
8653           || (at->dw_attr == DW_AT_friend
8654               && tag == DW_TAG_friend))
8655         {
8656           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8657
8658           if (name_attr != NULL)
8659             {
8660               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8661
8662               if (decl == NULL)
8663                 decl = target_die;
8664               CHECKSUM_ULEB128 ('N');
8665               CHECKSUM_ULEB128 (at->dw_attr);
8666               if (decl->die_parent != NULL)
8667                 checksum_die_context (decl->die_parent, ctx);
8668               CHECKSUM_ULEB128 ('E');
8669               CHECKSUM_STRING (AT_string (name_attr));
8670               return;
8671             }
8672         }
8673
8674       /* For all other references to another DIE, we check to see if the
8675          target DIE has already been visited.  If it has, we emit a
8676          backward reference; if not, we descend recursively.  */
8677       if (target_die->die_mark > 0)
8678         {
8679           CHECKSUM_ULEB128 ('R');
8680           CHECKSUM_ULEB128 (at->dw_attr);
8681           CHECKSUM_ULEB128 (target_die->die_mark);
8682         }
8683       else
8684         {
8685           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8686
8687           if (decl == NULL)
8688             decl = target_die;
8689           target_die->die_mark = ++(*mark);
8690           CHECKSUM_ULEB128 ('T');
8691           CHECKSUM_ULEB128 (at->dw_attr);
8692           if (decl->die_parent != NULL)
8693             checksum_die_context (decl->die_parent, ctx);
8694           die_checksum_ordered (target_die, ctx, mark);
8695         }
8696       return;
8697     }
8698
8699   CHECKSUM_ULEB128 ('A');
8700   CHECKSUM_ULEB128 (at->dw_attr);
8701
8702   switch (AT_class (at))
8703     {
8704     case dw_val_class_const:
8705       CHECKSUM_ULEB128 (DW_FORM_sdata);
8706       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8707       break;
8708
8709     case dw_val_class_unsigned_const:
8710       CHECKSUM_ULEB128 (DW_FORM_sdata);
8711       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8712       break;
8713
8714     case dw_val_class_const_double:
8715       CHECKSUM_ULEB128 (DW_FORM_block);
8716       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8717       CHECKSUM (at->dw_attr_val.v.val_double);
8718       break;
8719
8720     case dw_val_class_vec:
8721       CHECKSUM_ULEB128 (DW_FORM_block);
8722       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8723       CHECKSUM (at->dw_attr_val.v.val_vec);
8724       break;
8725
8726     case dw_val_class_flag:
8727       CHECKSUM_ULEB128 (DW_FORM_flag);
8728       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8729       break;
8730
8731     case dw_val_class_str:
8732       CHECKSUM_ULEB128 (DW_FORM_string);
8733       CHECKSUM_STRING (AT_string (at));
8734       break;
8735
8736     case dw_val_class_addr:
8737       r = AT_addr (at);
8738       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8739       CHECKSUM_ULEB128 (DW_FORM_string);
8740       CHECKSUM_STRING (XSTR (r, 0));
8741       break;
8742
8743     case dw_val_class_offset:
8744       CHECKSUM_ULEB128 (DW_FORM_sdata);
8745       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8746       break;
8747
8748     case dw_val_class_loc:
8749       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8750         loc_checksum_ordered (loc, ctx);
8751       break;
8752
8753     case dw_val_class_fde_ref:
8754     case dw_val_class_lbl_id:
8755     case dw_val_class_lineptr:
8756     case dw_val_class_macptr:
8757       break;
8758
8759     case dw_val_class_file:
8760       CHECKSUM_ULEB128 (DW_FORM_string);
8761       CHECKSUM_STRING (AT_file (at)->filename);
8762       break;
8763
8764     case dw_val_class_data8:
8765       CHECKSUM (at->dw_attr_val.v.val_data8);
8766       break;
8767
8768     default:
8769       break;
8770     }
8771 }
8772
8773 struct checksum_attributes
8774 {
8775   dw_attr_ref at_name;
8776   dw_attr_ref at_type;
8777   dw_attr_ref at_friend;
8778   dw_attr_ref at_accessibility;
8779   dw_attr_ref at_address_class;
8780   dw_attr_ref at_allocated;
8781   dw_attr_ref at_artificial;
8782   dw_attr_ref at_associated;
8783   dw_attr_ref at_binary_scale;
8784   dw_attr_ref at_bit_offset;
8785   dw_attr_ref at_bit_size;
8786   dw_attr_ref at_bit_stride;
8787   dw_attr_ref at_byte_size;
8788   dw_attr_ref at_byte_stride;
8789   dw_attr_ref at_const_value;
8790   dw_attr_ref at_containing_type;
8791   dw_attr_ref at_count;
8792   dw_attr_ref at_data_location;
8793   dw_attr_ref at_data_member_location;
8794   dw_attr_ref at_decimal_scale;
8795   dw_attr_ref at_decimal_sign;
8796   dw_attr_ref at_default_value;
8797   dw_attr_ref at_digit_count;
8798   dw_attr_ref at_discr;
8799   dw_attr_ref at_discr_list;
8800   dw_attr_ref at_discr_value;
8801   dw_attr_ref at_encoding;
8802   dw_attr_ref at_endianity;
8803   dw_attr_ref at_explicit;
8804   dw_attr_ref at_is_optional;
8805   dw_attr_ref at_location;
8806   dw_attr_ref at_lower_bound;
8807   dw_attr_ref at_mutable;
8808   dw_attr_ref at_ordering;
8809   dw_attr_ref at_picture_string;
8810   dw_attr_ref at_prototyped;
8811   dw_attr_ref at_small;
8812   dw_attr_ref at_segment;
8813   dw_attr_ref at_string_length;
8814   dw_attr_ref at_threads_scaled;
8815   dw_attr_ref at_upper_bound;
8816   dw_attr_ref at_use_location;
8817   dw_attr_ref at_use_UTF8;
8818   dw_attr_ref at_variable_parameter;
8819   dw_attr_ref at_virtuality;
8820   dw_attr_ref at_visibility;
8821   dw_attr_ref at_vtable_elem_location;
8822 };
8823
8824 /* Collect the attributes that we will want to use for the checksum.  */
8825
8826 static void
8827 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8828 {
8829   dw_attr_ref a;
8830   unsigned ix;
8831
8832   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8833     {
8834       switch (a->dw_attr)
8835         {
8836         case DW_AT_name:
8837           attrs->at_name = a;
8838           break;
8839         case DW_AT_type:
8840           attrs->at_type = a;
8841           break;
8842         case DW_AT_friend:
8843           attrs->at_friend = a;
8844           break;
8845         case DW_AT_accessibility:
8846           attrs->at_accessibility = a;
8847           break;
8848         case DW_AT_address_class:
8849           attrs->at_address_class = a;
8850           break;
8851         case DW_AT_allocated:
8852           attrs->at_allocated = a;
8853           break;
8854         case DW_AT_artificial:
8855           attrs->at_artificial = a;
8856           break;
8857         case DW_AT_associated:
8858           attrs->at_associated = a;
8859           break;
8860         case DW_AT_binary_scale:
8861           attrs->at_binary_scale = a;
8862           break;
8863         case DW_AT_bit_offset:
8864           attrs->at_bit_offset = a;
8865           break;
8866         case DW_AT_bit_size:
8867           attrs->at_bit_size = a;
8868           break;
8869         case DW_AT_bit_stride:
8870           attrs->at_bit_stride = a;
8871           break;
8872         case DW_AT_byte_size:
8873           attrs->at_byte_size = a;
8874           break;
8875         case DW_AT_byte_stride:
8876           attrs->at_byte_stride = a;
8877           break;
8878         case DW_AT_const_value:
8879           attrs->at_const_value = a;
8880           break;
8881         case DW_AT_containing_type:
8882           attrs->at_containing_type = a;
8883           break;
8884         case DW_AT_count:
8885           attrs->at_count = a;
8886           break;
8887         case DW_AT_data_location:
8888           attrs->at_data_location = a;
8889           break;
8890         case DW_AT_data_member_location:
8891           attrs->at_data_member_location = a;
8892           break;
8893         case DW_AT_decimal_scale:
8894           attrs->at_decimal_scale = a;
8895           break;
8896         case DW_AT_decimal_sign:
8897           attrs->at_decimal_sign = a;
8898           break;
8899         case DW_AT_default_value:
8900           attrs->at_default_value = a;
8901           break;
8902         case DW_AT_digit_count:
8903           attrs->at_digit_count = a;
8904           break;
8905         case DW_AT_discr:
8906           attrs->at_discr = a;
8907           break;
8908         case DW_AT_discr_list:
8909           attrs->at_discr_list = a;
8910           break;
8911         case DW_AT_discr_value:
8912           attrs->at_discr_value = a;
8913           break;
8914         case DW_AT_encoding:
8915           attrs->at_encoding = a;
8916           break;
8917         case DW_AT_endianity:
8918           attrs->at_endianity = a;
8919           break;
8920         case DW_AT_explicit:
8921           attrs->at_explicit = a;
8922           break;
8923         case DW_AT_is_optional:
8924           attrs->at_is_optional = a;
8925           break;
8926         case DW_AT_location:
8927           attrs->at_location = a;
8928           break;
8929         case DW_AT_lower_bound:
8930           attrs->at_lower_bound = a;
8931           break;
8932         case DW_AT_mutable:
8933           attrs->at_mutable = a;
8934           break;
8935         case DW_AT_ordering:
8936           attrs->at_ordering = a;
8937           break;
8938         case DW_AT_picture_string:
8939           attrs->at_picture_string = a;
8940           break;
8941         case DW_AT_prototyped:
8942           attrs->at_prototyped = a;
8943           break;
8944         case DW_AT_small:
8945           attrs->at_small = a;
8946           break;
8947         case DW_AT_segment:
8948           attrs->at_segment = a;
8949           break;
8950         case DW_AT_string_length:
8951           attrs->at_string_length = a;
8952           break;
8953         case DW_AT_threads_scaled:
8954           attrs->at_threads_scaled = a;
8955           break;
8956         case DW_AT_upper_bound:
8957           attrs->at_upper_bound = a;
8958           break;
8959         case DW_AT_use_location:
8960           attrs->at_use_location = a;
8961           break;
8962         case DW_AT_use_UTF8:
8963           attrs->at_use_UTF8 = a;
8964           break;
8965         case DW_AT_variable_parameter:
8966           attrs->at_variable_parameter = a;
8967           break;
8968         case DW_AT_virtuality:
8969           attrs->at_virtuality = a;
8970           break;
8971         case DW_AT_visibility:
8972           attrs->at_visibility = a;
8973           break;
8974         case DW_AT_vtable_elem_location:
8975           attrs->at_vtable_elem_location = a;
8976           break;
8977         default:
8978           break;
8979         }
8980     }
8981 }
8982
8983 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8984
8985 static void
8986 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8987 {
8988   dw_die_ref c;
8989   dw_die_ref decl;
8990   struct checksum_attributes attrs;
8991
8992   CHECKSUM_ULEB128 ('D');
8993   CHECKSUM_ULEB128 (die->die_tag);
8994
8995   memset (&attrs, 0, sizeof (attrs));
8996
8997   decl = get_AT_ref (die, DW_AT_specification);
8998   if (decl != NULL)
8999     collect_checksum_attributes (&attrs, decl);
9000   collect_checksum_attributes (&attrs, die);
9001
9002   CHECKSUM_ATTR (attrs.at_name);
9003   CHECKSUM_ATTR (attrs.at_accessibility);
9004   CHECKSUM_ATTR (attrs.at_address_class);
9005   CHECKSUM_ATTR (attrs.at_allocated);
9006   CHECKSUM_ATTR (attrs.at_artificial);
9007   CHECKSUM_ATTR (attrs.at_associated);
9008   CHECKSUM_ATTR (attrs.at_binary_scale);
9009   CHECKSUM_ATTR (attrs.at_bit_offset);
9010   CHECKSUM_ATTR (attrs.at_bit_size);
9011   CHECKSUM_ATTR (attrs.at_bit_stride);
9012   CHECKSUM_ATTR (attrs.at_byte_size);
9013   CHECKSUM_ATTR (attrs.at_byte_stride);
9014   CHECKSUM_ATTR (attrs.at_const_value);
9015   CHECKSUM_ATTR (attrs.at_containing_type);
9016   CHECKSUM_ATTR (attrs.at_count);
9017   CHECKSUM_ATTR (attrs.at_data_location);
9018   CHECKSUM_ATTR (attrs.at_data_member_location);
9019   CHECKSUM_ATTR (attrs.at_decimal_scale);
9020   CHECKSUM_ATTR (attrs.at_decimal_sign);
9021   CHECKSUM_ATTR (attrs.at_default_value);
9022   CHECKSUM_ATTR (attrs.at_digit_count);
9023   CHECKSUM_ATTR (attrs.at_discr);
9024   CHECKSUM_ATTR (attrs.at_discr_list);
9025   CHECKSUM_ATTR (attrs.at_discr_value);
9026   CHECKSUM_ATTR (attrs.at_encoding);
9027   CHECKSUM_ATTR (attrs.at_endianity);
9028   CHECKSUM_ATTR (attrs.at_explicit);
9029   CHECKSUM_ATTR (attrs.at_is_optional);
9030   CHECKSUM_ATTR (attrs.at_location);
9031   CHECKSUM_ATTR (attrs.at_lower_bound);
9032   CHECKSUM_ATTR (attrs.at_mutable);
9033   CHECKSUM_ATTR (attrs.at_ordering);
9034   CHECKSUM_ATTR (attrs.at_picture_string);
9035   CHECKSUM_ATTR (attrs.at_prototyped);
9036   CHECKSUM_ATTR (attrs.at_small);
9037   CHECKSUM_ATTR (attrs.at_segment);
9038   CHECKSUM_ATTR (attrs.at_string_length);
9039   CHECKSUM_ATTR (attrs.at_threads_scaled);
9040   CHECKSUM_ATTR (attrs.at_upper_bound);
9041   CHECKSUM_ATTR (attrs.at_use_location);
9042   CHECKSUM_ATTR (attrs.at_use_UTF8);
9043   CHECKSUM_ATTR (attrs.at_variable_parameter);
9044   CHECKSUM_ATTR (attrs.at_virtuality);
9045   CHECKSUM_ATTR (attrs.at_visibility);
9046   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9047   CHECKSUM_ATTR (attrs.at_type);
9048   CHECKSUM_ATTR (attrs.at_friend);
9049
9050   /* Checksum the child DIEs, except for nested types and member functions.  */
9051   c = die->die_child;
9052   if (c) do {
9053     dw_attr_ref name_attr;
9054
9055     c = c->die_sib;
9056     name_attr = get_AT (c, DW_AT_name);
9057     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9058         && name_attr != NULL)
9059       {
9060         CHECKSUM_ULEB128 ('S');
9061         CHECKSUM_ULEB128 (c->die_tag);
9062         CHECKSUM_STRING (AT_string (name_attr));
9063       }
9064     else
9065       {
9066         /* Mark this DIE so it gets processed when unmarking.  */
9067         if (c->die_mark == 0)
9068           c->die_mark = -1;
9069         die_checksum_ordered (c, ctx, mark);
9070       }
9071   } while (c != die->die_child);
9072
9073   CHECKSUM_ULEB128 (0);
9074 }
9075
9076 #undef CHECKSUM
9077 #undef CHECKSUM_STRING
9078 #undef CHECKSUM_ATTR
9079 #undef CHECKSUM_LEB128
9080 #undef CHECKSUM_ULEB128
9081
9082 /* Generate the type signature for DIE.  This is computed by generating an
9083    MD5 checksum over the DIE's tag, its relevant attributes, and its
9084    children.  Attributes that are references to other DIEs are processed
9085    by recursion, using the MARK field to prevent infinite recursion.
9086    If the DIE is nested inside a namespace or another type, we also
9087    need to include that context in the signature.  The lower 64 bits
9088    of the resulting MD5 checksum comprise the signature.  */
9089
9090 static void
9091 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9092 {
9093   int mark;
9094   const char *name;
9095   unsigned char checksum[16];
9096   struct md5_ctx ctx;
9097   dw_die_ref decl;
9098
9099   name = get_AT_string (die, DW_AT_name);
9100   decl = get_AT_ref (die, DW_AT_specification);
9101
9102   /* First, compute a signature for just the type name (and its surrounding
9103      context, if any.  This is stored in the type unit DIE for link-time
9104      ODR (one-definition rule) checking.  */
9105
9106   if (is_cxx() && name != NULL)
9107     {
9108       md5_init_ctx (&ctx);
9109
9110       /* Checksum the names of surrounding namespaces and structures.  */
9111       if (decl != NULL && decl->die_parent != NULL)
9112         checksum_die_context (decl->die_parent, &ctx);
9113
9114       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9115       md5_process_bytes (name, strlen (name) + 1, &ctx);
9116       md5_finish_ctx (&ctx, checksum);
9117
9118       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9119     }
9120
9121   /* Next, compute the complete type signature.  */
9122
9123   md5_init_ctx (&ctx);
9124   mark = 1;
9125   die->die_mark = mark;
9126
9127   /* Checksum the names of surrounding namespaces and structures.  */
9128   if (decl != NULL && decl->die_parent != NULL)
9129     checksum_die_context (decl->die_parent, &ctx);
9130
9131   /* Checksum the DIE and its children.  */
9132   die_checksum_ordered (die, &ctx, &mark);
9133   unmark_all_dies (die);
9134   md5_finish_ctx (&ctx, checksum);
9135
9136   /* Store the signature in the type node and link the type DIE and the
9137      type node together.  */
9138   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9139           DWARF_TYPE_SIGNATURE_SIZE);
9140   die->die_id.die_type_node = type_node;
9141   type_node->type_die = die;
9142
9143   /* If the DIE is a specification, link its declaration to the type node
9144      as well.  */
9145   if (decl != NULL)
9146     decl->die_id.die_type_node = type_node;
9147 }
9148
9149 /* Do the location expressions look same?  */
9150 static inline int
9151 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9152 {
9153   return loc1->dw_loc_opc == loc2->dw_loc_opc
9154          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9155          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9156 }
9157
9158 /* Do the values look the same?  */
9159 static int
9160 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9161 {
9162   dw_loc_descr_ref loc1, loc2;
9163   rtx r1, r2;
9164
9165   if (v1->val_class != v2->val_class)
9166     return 0;
9167
9168   switch (v1->val_class)
9169     {
9170     case dw_val_class_const:
9171       return v1->v.val_int == v2->v.val_int;
9172     case dw_val_class_unsigned_const:
9173       return v1->v.val_unsigned == v2->v.val_unsigned;
9174     case dw_val_class_const_double:
9175       return v1->v.val_double.high == v2->v.val_double.high
9176              && v1->v.val_double.low == v2->v.val_double.low;
9177     case dw_val_class_vec:
9178       if (v1->v.val_vec.length != v2->v.val_vec.length
9179           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9180         return 0;
9181       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9182                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9183         return 0;
9184       return 1;
9185     case dw_val_class_flag:
9186       return v1->v.val_flag == v2->v.val_flag;
9187     case dw_val_class_str:
9188       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9189
9190     case dw_val_class_addr:
9191       r1 = v1->v.val_addr;
9192       r2 = v2->v.val_addr;
9193       if (GET_CODE (r1) != GET_CODE (r2))
9194         return 0;
9195       return !rtx_equal_p (r1, r2);
9196
9197     case dw_val_class_offset:
9198       return v1->v.val_offset == v2->v.val_offset;
9199
9200     case dw_val_class_loc:
9201       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9202            loc1 && loc2;
9203            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9204         if (!same_loc_p (loc1, loc2, mark))
9205           return 0;
9206       return !loc1 && !loc2;
9207
9208     case dw_val_class_die_ref:
9209       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9210
9211     case dw_val_class_fde_ref:
9212     case dw_val_class_vms_delta:
9213     case dw_val_class_lbl_id:
9214     case dw_val_class_lineptr:
9215     case dw_val_class_macptr:
9216       return 1;
9217
9218     case dw_val_class_file:
9219       return v1->v.val_file == v2->v.val_file;
9220
9221     case dw_val_class_data8:
9222       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9223
9224     default:
9225       return 1;
9226     }
9227 }
9228
9229 /* Do the attributes look the same?  */
9230
9231 static int
9232 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9233 {
9234   if (at1->dw_attr != at2->dw_attr)
9235     return 0;
9236
9237   /* We don't care that this was compiled with a different compiler
9238      snapshot; if the output is the same, that's what matters. */
9239   if (at1->dw_attr == DW_AT_producer)
9240     return 1;
9241
9242   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9243 }
9244
9245 /* Do the dies look the same?  */
9246
9247 static int
9248 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9249 {
9250   dw_die_ref c1, c2;
9251   dw_attr_ref a1;
9252   unsigned ix;
9253
9254   /* To avoid infinite recursion.  */
9255   if (die1->die_mark)
9256     return die1->die_mark == die2->die_mark;
9257   die1->die_mark = die2->die_mark = ++(*mark);
9258
9259   if (die1->die_tag != die2->die_tag)
9260     return 0;
9261
9262   if (VEC_length (dw_attr_node, die1->die_attr)
9263       != VEC_length (dw_attr_node, die2->die_attr))
9264     return 0;
9265
9266   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9267     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9268       return 0;
9269
9270   c1 = die1->die_child;
9271   c2 = die2->die_child;
9272   if (! c1)
9273     {
9274       if (c2)
9275         return 0;
9276     }
9277   else
9278     for (;;)
9279       {
9280         if (!same_die_p (c1, c2, mark))
9281           return 0;
9282         c1 = c1->die_sib;
9283         c2 = c2->die_sib;
9284         if (c1 == die1->die_child)
9285           {
9286             if (c2 == die2->die_child)
9287               break;
9288             else
9289               return 0;
9290           }
9291     }
9292
9293   return 1;
9294 }
9295
9296 /* Do the dies look the same?  Wrapper around same_die_p.  */
9297
9298 static int
9299 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9300 {
9301   int mark = 0;
9302   int ret = same_die_p (die1, die2, &mark);
9303
9304   unmark_all_dies (die1);
9305   unmark_all_dies (die2);
9306
9307   return ret;
9308 }
9309
9310 /* The prefix to attach to symbols on DIEs in the current comdat debug
9311    info section.  */
9312 static char *comdat_symbol_id;
9313
9314 /* The index of the current symbol within the current comdat CU.  */
9315 static unsigned int comdat_symbol_number;
9316
9317 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9318    children, and set comdat_symbol_id accordingly.  */
9319
9320 static void
9321 compute_section_prefix (dw_die_ref unit_die)
9322 {
9323   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9324   const char *base = die_name ? lbasename (die_name) : "anonymous";
9325   char *name = XALLOCAVEC (char, strlen (base) + 64);
9326   char *p;
9327   int i, mark;
9328   unsigned char checksum[16];
9329   struct md5_ctx ctx;
9330
9331   /* Compute the checksum of the DIE, then append part of it as hex digits to
9332      the name filename of the unit.  */
9333
9334   md5_init_ctx (&ctx);
9335   mark = 0;
9336   die_checksum (unit_die, &ctx, &mark);
9337   unmark_all_dies (unit_die);
9338   md5_finish_ctx (&ctx, checksum);
9339
9340   sprintf (name, "%s.", base);
9341   clean_symbol_name (name);
9342
9343   p = name + strlen (name);
9344   for (i = 0; i < 4; i++)
9345     {
9346       sprintf (p, "%.2x", checksum[i]);
9347       p += 2;
9348     }
9349
9350   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9351   comdat_symbol_number = 0;
9352 }
9353
9354 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9355
9356 static int
9357 is_type_die (dw_die_ref die)
9358 {
9359   switch (die->die_tag)
9360     {
9361     case DW_TAG_array_type:
9362     case DW_TAG_class_type:
9363     case DW_TAG_interface_type:
9364     case DW_TAG_enumeration_type:
9365     case DW_TAG_pointer_type:
9366     case DW_TAG_reference_type:
9367     case DW_TAG_rvalue_reference_type:
9368     case DW_TAG_string_type:
9369     case DW_TAG_structure_type:
9370     case DW_TAG_subroutine_type:
9371     case DW_TAG_union_type:
9372     case DW_TAG_ptr_to_member_type:
9373     case DW_TAG_set_type:
9374     case DW_TAG_subrange_type:
9375     case DW_TAG_base_type:
9376     case DW_TAG_const_type:
9377     case DW_TAG_file_type:
9378     case DW_TAG_packed_type:
9379     case DW_TAG_volatile_type:
9380     case DW_TAG_typedef:
9381       return 1;
9382     default:
9383       return 0;
9384     }
9385 }
9386
9387 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9388    Basically, we want to choose the bits that are likely to be shared between
9389    compilations (types) and leave out the bits that are specific to individual
9390    compilations (functions).  */
9391
9392 static int
9393 is_comdat_die (dw_die_ref c)
9394 {
9395   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9396      we do for stabs.  The advantage is a greater likelihood of sharing between
9397      objects that don't include headers in the same order (and therefore would
9398      put the base types in a different comdat).  jason 8/28/00 */
9399
9400   if (c->die_tag == DW_TAG_base_type)
9401     return 0;
9402
9403   if (c->die_tag == DW_TAG_pointer_type
9404       || c->die_tag == DW_TAG_reference_type
9405       || c->die_tag == DW_TAG_rvalue_reference_type
9406       || c->die_tag == DW_TAG_const_type
9407       || c->die_tag == DW_TAG_volatile_type)
9408     {
9409       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9410
9411       return t ? is_comdat_die (t) : 0;
9412     }
9413
9414   return is_type_die (c);
9415 }
9416
9417 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9418    compilation unit.  */
9419
9420 static int
9421 is_symbol_die (dw_die_ref c)
9422 {
9423   return (is_type_die (c)
9424           || is_declaration_die (c)
9425           || c->die_tag == DW_TAG_namespace
9426           || c->die_tag == DW_TAG_module);
9427 }
9428
9429 static char *
9430 gen_internal_sym (const char *prefix)
9431 {
9432   char buf[256];
9433
9434   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9435   return xstrdup (buf);
9436 }
9437
9438 /* Assign symbols to all worthy DIEs under DIE.  */
9439
9440 static void
9441 assign_symbol_names (dw_die_ref die)
9442 {
9443   dw_die_ref c;
9444
9445   if (is_symbol_die (die))
9446     {
9447       if (comdat_symbol_id)
9448         {
9449           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9450
9451           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9452                    comdat_symbol_id, comdat_symbol_number++);
9453           die->die_id.die_symbol = xstrdup (p);
9454         }
9455       else
9456         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9457     }
9458
9459   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9460 }
9461
9462 struct cu_hash_table_entry
9463 {
9464   dw_die_ref cu;
9465   unsigned min_comdat_num, max_comdat_num;
9466   struct cu_hash_table_entry *next;
9467 };
9468
9469 /* Routines to manipulate hash table of CUs.  */
9470 static hashval_t
9471 htab_cu_hash (const void *of)
9472 {
9473   const struct cu_hash_table_entry *const entry =
9474     (const struct cu_hash_table_entry *) of;
9475
9476   return htab_hash_string (entry->cu->die_id.die_symbol);
9477 }
9478
9479 static int
9480 htab_cu_eq (const void *of1, const void *of2)
9481 {
9482   const struct cu_hash_table_entry *const entry1 =
9483     (const struct cu_hash_table_entry *) of1;
9484   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9485
9486   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9487 }
9488
9489 static void
9490 htab_cu_del (void *what)
9491 {
9492   struct cu_hash_table_entry *next,
9493     *entry = (struct cu_hash_table_entry *) what;
9494
9495   while (entry)
9496     {
9497       next = entry->next;
9498       free (entry);
9499       entry = next;
9500     }
9501 }
9502
9503 /* Check whether we have already seen this CU and set up SYM_NUM
9504    accordingly.  */
9505 static int
9506 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9507 {
9508   struct cu_hash_table_entry dummy;
9509   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9510
9511   dummy.max_comdat_num = 0;
9512
9513   slot = (struct cu_hash_table_entry **)
9514     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9515         INSERT);
9516   entry = *slot;
9517
9518   for (; entry; last = entry, entry = entry->next)
9519     {
9520       if (same_die_p_wrap (cu, entry->cu))
9521         break;
9522     }
9523
9524   if (entry)
9525     {
9526       *sym_num = entry->min_comdat_num;
9527       return 1;
9528     }
9529
9530   entry = XCNEW (struct cu_hash_table_entry);
9531   entry->cu = cu;
9532   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9533   entry->next = *slot;
9534   *slot = entry;
9535
9536   return 0;
9537 }
9538
9539 /* Record SYM_NUM to record of CU in HTABLE.  */
9540 static void
9541 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9542 {
9543   struct cu_hash_table_entry **slot, *entry;
9544
9545   slot = (struct cu_hash_table_entry **)
9546     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9547         NO_INSERT);
9548   entry = *slot;
9549
9550   entry->max_comdat_num = sym_num;
9551 }
9552
9553 /* Traverse the DIE (which is always comp_unit_die), and set up
9554    additional compilation units for each of the include files we see
9555    bracketed by BINCL/EINCL.  */
9556
9557 static void
9558 break_out_includes (dw_die_ref die)
9559 {
9560   dw_die_ref c;
9561   dw_die_ref unit = NULL;
9562   limbo_die_node *node, **pnode;
9563   htab_t cu_hash_table;
9564
9565   c = die->die_child;
9566   if (c) do {
9567     dw_die_ref prev = c;
9568     c = c->die_sib;
9569     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9570            || (unit && is_comdat_die (c)))
9571       {
9572         dw_die_ref next = c->die_sib;
9573
9574         /* This DIE is for a secondary CU; remove it from the main one.  */
9575         remove_child_with_prev (c, prev);
9576
9577         if (c->die_tag == DW_TAG_GNU_BINCL)
9578           unit = push_new_compile_unit (unit, c);
9579         else if (c->die_tag == DW_TAG_GNU_EINCL)
9580           unit = pop_compile_unit (unit);
9581         else
9582           add_child_die (unit, c);
9583         c = next;
9584         if (c == die->die_child)
9585           break;
9586       }
9587   } while (c != die->die_child);
9588
9589 #if 0
9590   /* We can only use this in debugging, since the frontend doesn't check
9591      to make sure that we leave every include file we enter.  */
9592   gcc_assert (!unit);
9593 #endif
9594
9595   assign_symbol_names (die);
9596   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9597   for (node = limbo_die_list, pnode = &limbo_die_list;
9598        node;
9599        node = node->next)
9600     {
9601       int is_dupl;
9602
9603       compute_section_prefix (node->die);
9604       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9605                         &comdat_symbol_number);
9606       assign_symbol_names (node->die);
9607       if (is_dupl)
9608         *pnode = node->next;
9609       else
9610         {
9611           pnode = &node->next;
9612           record_comdat_symbol_number (node->die, cu_hash_table,
9613                 comdat_symbol_number);
9614         }
9615     }
9616   htab_delete (cu_hash_table);
9617 }
9618
9619 /* Return non-zero if this DIE is a declaration.  */
9620
9621 static int
9622 is_declaration_die (dw_die_ref die)
9623 {
9624   dw_attr_ref a;
9625   unsigned ix;
9626
9627   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9628     if (a->dw_attr == DW_AT_declaration)
9629       return 1;
9630
9631   return 0;
9632 }
9633
9634 /* Return non-zero if this DIE is nested inside a subprogram.  */
9635
9636 static int
9637 is_nested_in_subprogram (dw_die_ref die)
9638 {
9639   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9640
9641   if (decl == NULL)
9642     decl = die;
9643   return local_scope_p (decl);
9644 }
9645
9646 /* Return non-zero if this is a type DIE that should be moved to a
9647    COMDAT .debug_types section.  */
9648
9649 static int
9650 should_move_die_to_comdat (dw_die_ref die)
9651 {
9652   switch (die->die_tag)
9653     {
9654     case DW_TAG_class_type:
9655     case DW_TAG_structure_type:
9656     case DW_TAG_enumeration_type:
9657     case DW_TAG_union_type:
9658       /* Don't move declarations, inlined instances, or types nested in a
9659          subprogram.  */
9660       if (is_declaration_die (die)
9661           || get_AT (die, DW_AT_abstract_origin)
9662           || is_nested_in_subprogram (die))
9663         return 0;
9664       return 1;
9665     case DW_TAG_array_type:
9666     case DW_TAG_interface_type:
9667     case DW_TAG_pointer_type:
9668     case DW_TAG_reference_type:
9669     case DW_TAG_rvalue_reference_type:
9670     case DW_TAG_string_type:
9671     case DW_TAG_subroutine_type:
9672     case DW_TAG_ptr_to_member_type:
9673     case DW_TAG_set_type:
9674     case DW_TAG_subrange_type:
9675     case DW_TAG_base_type:
9676     case DW_TAG_const_type:
9677     case DW_TAG_file_type:
9678     case DW_TAG_packed_type:
9679     case DW_TAG_volatile_type:
9680     case DW_TAG_typedef:
9681     default:
9682       return 0;
9683     }
9684 }
9685
9686 /* Make a clone of DIE.  */
9687
9688 static dw_die_ref
9689 clone_die (dw_die_ref die)
9690 {
9691   dw_die_ref clone;
9692   dw_attr_ref a;
9693   unsigned ix;
9694
9695   clone = ggc_alloc_cleared_die_node ();
9696   clone->die_tag = die->die_tag;
9697
9698   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9699     add_dwarf_attr (clone, a);
9700
9701   return clone;
9702 }
9703
9704 /* Make a clone of the tree rooted at DIE.  */
9705
9706 static dw_die_ref
9707 clone_tree (dw_die_ref die)
9708 {
9709   dw_die_ref c;
9710   dw_die_ref clone = clone_die (die);
9711
9712   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9713
9714   return clone;
9715 }
9716
9717 /* Make a clone of DIE as a declaration.  */
9718
9719 static dw_die_ref
9720 clone_as_declaration (dw_die_ref die)
9721 {
9722   dw_die_ref clone;
9723   dw_die_ref decl;
9724   dw_attr_ref a;
9725   unsigned ix;
9726
9727   /* If the DIE is already a declaration, just clone it.  */
9728   if (is_declaration_die (die))
9729     return clone_die (die);
9730
9731   /* If the DIE is a specification, just clone its declaration DIE.  */
9732   decl = get_AT_ref (die, DW_AT_specification);
9733   if (decl != NULL)
9734     return clone_die (decl);
9735
9736   clone = ggc_alloc_cleared_die_node ();
9737   clone->die_tag = die->die_tag;
9738
9739   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9740     {
9741       /* We don't want to copy over all attributes.
9742          For example we don't want DW_AT_byte_size because otherwise we will no
9743          longer have a declaration and GDB will treat it as a definition.  */
9744
9745       switch (a->dw_attr)
9746         {
9747         case DW_AT_artificial:
9748         case DW_AT_containing_type:
9749         case DW_AT_external:
9750         case DW_AT_name:
9751         case DW_AT_type:
9752         case DW_AT_virtuality:
9753         case DW_AT_linkage_name:
9754         case DW_AT_MIPS_linkage_name:
9755           add_dwarf_attr (clone, a);
9756           break;
9757         case DW_AT_byte_size:
9758         default:
9759           break;
9760         }
9761     }
9762
9763   if (die->die_id.die_type_node)
9764     add_AT_die_ref (clone, DW_AT_signature, die);
9765
9766   add_AT_flag (clone, DW_AT_declaration, 1);
9767   return clone;
9768 }
9769
9770 /* Copy the declaration context to the new compile unit DIE.  This includes
9771    any surrounding namespace or type declarations.  If the DIE has an
9772    AT_specification attribute, it also includes attributes and children
9773    attached to the specification.  */
9774
9775 static void
9776 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9777 {
9778   dw_die_ref decl;
9779   dw_die_ref new_decl;
9780
9781   decl = get_AT_ref (die, DW_AT_specification);
9782   if (decl == NULL)
9783     decl = die;
9784   else
9785     {
9786       unsigned ix;
9787       dw_die_ref c;
9788       dw_attr_ref a;
9789
9790       /* Copy the type node pointer from the new DIE to the original
9791          declaration DIE so we can forward references later.  */
9792       decl->die_id.die_type_node = die->die_id.die_type_node;
9793
9794       remove_AT (die, DW_AT_specification);
9795
9796       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9797         {
9798           if (a->dw_attr != DW_AT_name
9799               && a->dw_attr != DW_AT_declaration
9800               && a->dw_attr != DW_AT_external)
9801             add_dwarf_attr (die, a);
9802         }
9803
9804       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9805     }
9806
9807   if (decl->die_parent != NULL
9808       && decl->die_parent->die_tag != DW_TAG_compile_unit
9809       && decl->die_parent->die_tag != DW_TAG_type_unit)
9810     {
9811       new_decl = copy_ancestor_tree (unit, decl, NULL);
9812       if (new_decl != NULL)
9813         {
9814           remove_AT (new_decl, DW_AT_signature);
9815           add_AT_specification (die, new_decl);
9816         }
9817     }
9818 }
9819
9820 /* Generate the skeleton ancestor tree for the given NODE, then clone
9821    the DIE and add the clone into the tree.  */
9822
9823 static void
9824 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9825 {
9826   if (node->new_die != NULL)
9827     return;
9828
9829   node->new_die = clone_as_declaration (node->old_die);
9830
9831   if (node->parent != NULL)
9832     {
9833       generate_skeleton_ancestor_tree (node->parent);
9834       add_child_die (node->parent->new_die, node->new_die);
9835     }
9836 }
9837
9838 /* Generate a skeleton tree of DIEs containing any declarations that are
9839    found in the original tree.  We traverse the tree looking for declaration
9840    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9841
9842 static void
9843 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9844 {
9845   skeleton_chain_node node;
9846   dw_die_ref c;
9847   dw_die_ref first;
9848   dw_die_ref prev = NULL;
9849   dw_die_ref next = NULL;
9850
9851   node.parent = parent;
9852
9853   first = c = parent->old_die->die_child;
9854   if (c)
9855     next = c->die_sib;
9856   if (c) do {
9857     if (prev == NULL || prev->die_sib == c)
9858       prev = c;
9859     c = next;
9860     next = (c == first ? NULL : c->die_sib);
9861     node.old_die = c;
9862     node.new_die = NULL;
9863     if (is_declaration_die (c))
9864       {
9865         /* Clone the existing DIE, move the original to the skeleton
9866            tree (which is in the main CU), and put the clone, with
9867            all the original's children, where the original came from.  */
9868         dw_die_ref clone = clone_die (c);
9869         move_all_children (c, clone);
9870
9871         replace_child (c, clone, prev);
9872         generate_skeleton_ancestor_tree (parent);
9873         add_child_die (parent->new_die, c);
9874         node.new_die = c;
9875         c = clone;
9876       }
9877     generate_skeleton_bottom_up (&node);
9878   } while (next != NULL);
9879 }
9880
9881 /* Wrapper function for generate_skeleton_bottom_up.  */
9882
9883 static dw_die_ref
9884 generate_skeleton (dw_die_ref die)
9885 {
9886   skeleton_chain_node node;
9887
9888   node.old_die = die;
9889   node.new_die = NULL;
9890   node.parent = NULL;
9891
9892   /* If this type definition is nested inside another type,
9893      always leave at least a declaration in its place.  */
9894   if (die->die_parent != NULL && is_type_die (die->die_parent))
9895     node.new_die = clone_as_declaration (die);
9896
9897   generate_skeleton_bottom_up (&node);
9898   return node.new_die;
9899 }
9900
9901 /* Remove the DIE from its parent, possibly replacing it with a cloned
9902    declaration.  The original DIE will be moved to a new compile unit
9903    so that existing references to it follow it to the new location.  If
9904    any of the original DIE's descendants is a declaration, we need to
9905    replace the original DIE with a skeleton tree and move the
9906    declarations back into the skeleton tree.  */
9907
9908 static dw_die_ref
9909 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9910 {
9911   dw_die_ref skeleton;
9912
9913   skeleton = generate_skeleton (child);
9914   if (skeleton == NULL)
9915     remove_child_with_prev (child, prev);
9916   else
9917     {
9918       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9919       replace_child (child, skeleton, prev);
9920     }
9921
9922   return skeleton;
9923 }
9924
9925 /* Traverse the DIE and set up additional .debug_types sections for each
9926    type worthy of being placed in a COMDAT section.  */
9927
9928 static void
9929 break_out_comdat_types (dw_die_ref die)
9930 {
9931   dw_die_ref c;
9932   dw_die_ref first;
9933   dw_die_ref prev = NULL;
9934   dw_die_ref next = NULL;
9935   dw_die_ref unit = NULL;
9936
9937   first = c = die->die_child;
9938   if (c)
9939     next = c->die_sib;
9940   if (c) do {
9941     if (prev == NULL || prev->die_sib == c)
9942       prev = c;
9943     c = next;
9944     next = (c == first ? NULL : c->die_sib);
9945     if (should_move_die_to_comdat (c))
9946       {
9947         dw_die_ref replacement;
9948         comdat_type_node_ref type_node;
9949
9950         /* Create a new type unit DIE as the root for the new tree, and
9951            add it to the list of comdat types.  */
9952         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9953         add_AT_unsigned (unit, DW_AT_language,
9954                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9955         type_node = ggc_alloc_cleared_comdat_type_node ();
9956         type_node->root_die = unit;
9957         type_node->next = comdat_type_list;
9958         comdat_type_list = type_node;
9959
9960         /* Generate the type signature.  */
9961         generate_type_signature (c, type_node);
9962
9963         /* Copy the declaration context, attributes, and children of the
9964            declaration into the new compile unit DIE.  */
9965         copy_declaration_context (unit, c);
9966
9967         /* Remove this DIE from the main CU.  */
9968         replacement = remove_child_or_replace_with_skeleton (c, prev);
9969
9970         /* Break out nested types into their own type units.  */
9971         break_out_comdat_types (c);
9972
9973         /* Add the DIE to the new compunit.  */
9974         add_child_die (unit, c);
9975
9976         if (replacement != NULL)
9977           c = replacement;
9978       }
9979     else if (c->die_tag == DW_TAG_namespace
9980              || c->die_tag == DW_TAG_class_type
9981              || c->die_tag == DW_TAG_structure_type
9982              || c->die_tag == DW_TAG_union_type)
9983       {
9984         /* Look for nested types that can be broken out.  */
9985         break_out_comdat_types (c);
9986       }
9987   } while (next != NULL);
9988 }
9989
9990 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9991
9992 struct decl_table_entry
9993 {
9994   dw_die_ref orig;
9995   dw_die_ref copy;
9996 };
9997
9998 /* Routines to manipulate hash table of copied declarations.  */
9999
10000 static hashval_t
10001 htab_decl_hash (const void *of)
10002 {
10003   const struct decl_table_entry *const entry =
10004     (const struct decl_table_entry *) of;
10005
10006   return htab_hash_pointer (entry->orig);
10007 }
10008
10009 static int
10010 htab_decl_eq (const void *of1, const void *of2)
10011 {
10012   const struct decl_table_entry *const entry1 =
10013     (const struct decl_table_entry *) of1;
10014   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10015
10016   return entry1->orig == entry2;
10017 }
10018
10019 static void
10020 htab_decl_del (void *what)
10021 {
10022   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10023
10024   free (entry);
10025 }
10026
10027 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10028    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10029    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10030    to check if the ancestor has already been copied into UNIT.  */
10031
10032 static dw_die_ref
10033 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10034 {
10035   dw_die_ref parent = die->die_parent;
10036   dw_die_ref new_parent = unit;
10037   dw_die_ref copy;
10038   void **slot = NULL;
10039   struct decl_table_entry *entry = NULL;
10040
10041   if (decl_table)
10042     {
10043       /* Check if the entry has already been copied to UNIT.  */
10044       slot = htab_find_slot_with_hash (decl_table, die,
10045                                        htab_hash_pointer (die), INSERT);
10046       if (*slot != HTAB_EMPTY_ENTRY)
10047         {
10048           entry = (struct decl_table_entry *) *slot;
10049           return entry->copy;
10050         }
10051
10052       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10053       entry = XCNEW (struct decl_table_entry);
10054       entry->orig = die;
10055       entry->copy = NULL;
10056       *slot = entry;
10057     }
10058
10059   if (parent != NULL)
10060     {
10061       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10062       if (spec != NULL)
10063         parent = spec;
10064       if (parent->die_tag != DW_TAG_compile_unit
10065           && parent->die_tag != DW_TAG_type_unit)
10066         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10067     }
10068
10069   copy = clone_as_declaration (die);
10070   add_child_die (new_parent, copy);
10071
10072   if (decl_table != NULL)
10073     {
10074       /* Record the pointer to the copy.  */
10075       entry->copy = copy;
10076     }
10077
10078   return copy;
10079 }
10080
10081 /* Walk the DIE and its children, looking for references to incomplete
10082    or trivial types that are unmarked (i.e., that are not in the current
10083    type_unit).  */
10084
10085 static void
10086 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10087 {
10088   dw_die_ref c;
10089   dw_attr_ref a;
10090   unsigned ix;
10091
10092   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10093     {
10094       if (AT_class (a) == dw_val_class_die_ref)
10095         {
10096           dw_die_ref targ = AT_ref (a);
10097           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10098           void **slot;
10099           struct decl_table_entry *entry;
10100
10101           if (targ->die_mark != 0 || type_node != NULL)
10102             continue;
10103
10104           slot = htab_find_slot_with_hash (decl_table, targ,
10105                                            htab_hash_pointer (targ), INSERT);
10106
10107           if (*slot != HTAB_EMPTY_ENTRY)
10108             {
10109               /* TARG has already been copied, so we just need to
10110                  modify the reference to point to the copy.  */
10111               entry = (struct decl_table_entry *) *slot;
10112               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10113             }
10114           else
10115             {
10116               dw_die_ref parent = unit;
10117               dw_die_ref copy = clone_tree (targ);
10118
10119               /* Make sure the cloned tree is marked as part of the
10120                  type unit.  */
10121               mark_dies (copy);
10122
10123               /* Record in DECL_TABLE that TARG has been copied.
10124                  Need to do this now, before the recursive call,
10125                  because DECL_TABLE may be expanded and SLOT
10126                  would no longer be a valid pointer.  */
10127               entry = XCNEW (struct decl_table_entry);
10128               entry->orig = targ;
10129               entry->copy = copy;
10130               *slot = entry;
10131
10132               /* If TARG has surrounding context, copy its ancestor tree
10133                  into the new type unit.  */
10134               if (targ->die_parent != NULL
10135                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10136                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10137                 parent = copy_ancestor_tree (unit, targ->die_parent,
10138                                              decl_table);
10139
10140               add_child_die (parent, copy);
10141               a->dw_attr_val.v.val_die_ref.die = copy;
10142
10143               /* Make sure the newly-copied DIE is walked.  If it was
10144                  installed in a previously-added context, it won't
10145                  get visited otherwise.  */
10146               if (parent != unit)
10147                 {
10148                   /* Find the highest point of the newly-added tree,
10149                      mark each node along the way, and walk from there.  */
10150                   parent->die_mark = 1;
10151                   while (parent->die_parent
10152                          && parent->die_parent->die_mark == 0)
10153                     {
10154                       parent = parent->die_parent;
10155                       parent->die_mark = 1;
10156                     }
10157                   copy_decls_walk (unit, parent, decl_table);
10158                 }
10159             }
10160         }
10161     }
10162
10163   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10164 }
10165
10166 /* Copy declarations for "unworthy" types into the new comdat section.
10167    Incomplete types, modified types, and certain other types aren't broken
10168    out into comdat sections of their own, so they don't have a signature,
10169    and we need to copy the declaration into the same section so that we
10170    don't have an external reference.  */
10171
10172 static void
10173 copy_decls_for_unworthy_types (dw_die_ref unit)
10174 {
10175   htab_t decl_table;
10176
10177   mark_dies (unit);
10178   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10179   copy_decls_walk (unit, unit, decl_table);
10180   htab_delete (decl_table);
10181   unmark_dies (unit);
10182 }
10183
10184 /* Traverse the DIE and add a sibling attribute if it may have the
10185    effect of speeding up access to siblings.  To save some space,
10186    avoid generating sibling attributes for DIE's without children.  */
10187
10188 static void
10189 add_sibling_attributes (dw_die_ref die)
10190 {
10191   dw_die_ref c;
10192
10193   if (! die->die_child)
10194     return;
10195
10196   if (die->die_parent && die != die->die_parent->die_child)
10197     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10198
10199   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10200 }
10201
10202 /* Output all location lists for the DIE and its children.  */
10203
10204 static void
10205 output_location_lists (dw_die_ref die)
10206 {
10207   dw_die_ref c;
10208   dw_attr_ref a;
10209   unsigned ix;
10210
10211   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10212     if (AT_class (a) == dw_val_class_loc_list)
10213       output_loc_list (AT_loc_list (a));
10214
10215   FOR_EACH_CHILD (die, c, output_location_lists (c));
10216 }
10217
10218 /* The format of each DIE (and its attribute value pairs) is encoded in an
10219    abbreviation table.  This routine builds the abbreviation table and assigns
10220    a unique abbreviation id for each abbreviation entry.  The children of each
10221    die are visited recursively.  */
10222
10223 static void
10224 build_abbrev_table (dw_die_ref die)
10225 {
10226   unsigned long abbrev_id;
10227   unsigned int n_alloc;
10228   dw_die_ref c;
10229   dw_attr_ref a;
10230   unsigned ix;
10231
10232   /* Scan the DIE references, and mark as external any that refer to
10233      DIEs from other CUs (i.e. those which are not marked).  */
10234   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10235     if (AT_class (a) == dw_val_class_die_ref
10236         && AT_ref (a)->die_mark == 0)
10237       {
10238         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10239         set_AT_ref_external (a, 1);
10240       }
10241
10242   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10243     {
10244       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10245       dw_attr_ref die_a, abbrev_a;
10246       unsigned ix;
10247       bool ok = true;
10248
10249       if (abbrev->die_tag != die->die_tag)
10250         continue;
10251       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10252         continue;
10253
10254       if (VEC_length (dw_attr_node, abbrev->die_attr)
10255           != VEC_length (dw_attr_node, die->die_attr))
10256         continue;
10257
10258       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10259         {
10260           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10261           if ((abbrev_a->dw_attr != die_a->dw_attr)
10262               || (value_format (abbrev_a) != value_format (die_a)))
10263             {
10264               ok = false;
10265               break;
10266             }
10267         }
10268       if (ok)
10269         break;
10270     }
10271
10272   if (abbrev_id >= abbrev_die_table_in_use)
10273     {
10274       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10275         {
10276           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10277           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10278                                             n_alloc);
10279
10280           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10281                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10282           abbrev_die_table_allocated = n_alloc;
10283         }
10284
10285       ++abbrev_die_table_in_use;
10286       abbrev_die_table[abbrev_id] = die;
10287     }
10288
10289   die->die_abbrev = abbrev_id;
10290   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10291 }
10292 \f
10293 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10294
10295 static int
10296 constant_size (unsigned HOST_WIDE_INT value)
10297 {
10298   int log;
10299
10300   if (value == 0)
10301     log = 0;
10302   else
10303     log = floor_log2 (value);
10304
10305   log = log / 8;
10306   log = 1 << (floor_log2 (log) + 1);
10307
10308   return log;
10309 }
10310
10311 /* Return the size of a DIE as it is represented in the
10312    .debug_info section.  */
10313
10314 static unsigned long
10315 size_of_die (dw_die_ref die)
10316 {
10317   unsigned long size = 0;
10318   dw_attr_ref a;
10319   unsigned ix;
10320
10321   size += size_of_uleb128 (die->die_abbrev);
10322   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10323     {
10324       switch (AT_class (a))
10325         {
10326         case dw_val_class_addr:
10327           size += DWARF2_ADDR_SIZE;
10328           break;
10329         case dw_val_class_offset:
10330           size += DWARF_OFFSET_SIZE;
10331           break;
10332         case dw_val_class_loc:
10333           {
10334             unsigned long lsize = size_of_locs (AT_loc (a));
10335
10336             /* Block length.  */
10337             if (dwarf_version >= 4)
10338               size += size_of_uleb128 (lsize);
10339             else
10340               size += constant_size (lsize);
10341             size += lsize;
10342           }
10343           break;
10344         case dw_val_class_loc_list:
10345           size += DWARF_OFFSET_SIZE;
10346           break;
10347         case dw_val_class_range_list:
10348           size += DWARF_OFFSET_SIZE;
10349           break;
10350         case dw_val_class_const:
10351           size += size_of_sleb128 (AT_int (a));
10352           break;
10353         case dw_val_class_unsigned_const:
10354           size += constant_size (AT_unsigned (a));
10355           break;
10356         case dw_val_class_const_double:
10357           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10358           if (HOST_BITS_PER_WIDE_INT >= 64)
10359             size++; /* block */
10360           break;
10361         case dw_val_class_vec:
10362           size += constant_size (a->dw_attr_val.v.val_vec.length
10363                                  * a->dw_attr_val.v.val_vec.elt_size)
10364                   + a->dw_attr_val.v.val_vec.length
10365                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10366           break;
10367         case dw_val_class_flag:
10368           if (dwarf_version >= 4)
10369             /* Currently all add_AT_flag calls pass in 1 as last argument,
10370                so DW_FORM_flag_present can be used.  If that ever changes,
10371                we'll need to use DW_FORM_flag and have some optimization
10372                in build_abbrev_table that will change those to
10373                DW_FORM_flag_present if it is set to 1 in all DIEs using
10374                the same abbrev entry.  */
10375             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10376           else
10377             size += 1;
10378           break;
10379         case dw_val_class_die_ref:
10380           if (AT_ref_external (a))
10381             {
10382               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10383                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10384                  is sized by target address length, whereas in DWARF3
10385                  it's always sized as an offset.  */
10386               if (dwarf_version >= 4)
10387                 size += DWARF_TYPE_SIGNATURE_SIZE;
10388               else if (dwarf_version == 2)
10389                 size += DWARF2_ADDR_SIZE;
10390               else
10391                 size += DWARF_OFFSET_SIZE;
10392             }
10393           else
10394             size += DWARF_OFFSET_SIZE;
10395           break;
10396         case dw_val_class_fde_ref:
10397           size += DWARF_OFFSET_SIZE;
10398           break;
10399         case dw_val_class_lbl_id:
10400           size += DWARF2_ADDR_SIZE;
10401           break;
10402         case dw_val_class_lineptr:
10403         case dw_val_class_macptr:
10404           size += DWARF_OFFSET_SIZE;
10405           break;
10406         case dw_val_class_str:
10407           if (AT_string_form (a) == DW_FORM_strp)
10408             size += DWARF_OFFSET_SIZE;
10409           else
10410             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10411           break;
10412         case dw_val_class_file:
10413           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10414           break;
10415         case dw_val_class_data8:
10416           size += 8;
10417           break;
10418         case dw_val_class_vms_delta:
10419           size += DWARF_OFFSET_SIZE;
10420           break;
10421         default:
10422           gcc_unreachable ();
10423         }
10424     }
10425
10426   return size;
10427 }
10428
10429 /* Size the debugging information associated with a given DIE.  Visits the
10430    DIE's children recursively.  Updates the global variable next_die_offset, on
10431    each time through.  Uses the current value of next_die_offset to update the
10432    die_offset field in each DIE.  */
10433
10434 static void
10435 calc_die_sizes (dw_die_ref die)
10436 {
10437   dw_die_ref c;
10438
10439   die->die_offset = next_die_offset;
10440   next_die_offset += size_of_die (die);
10441
10442   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10443
10444   if (die->die_child != NULL)
10445     /* Count the null byte used to terminate sibling lists.  */
10446     next_die_offset += 1;
10447 }
10448
10449 /* Set the marks for a die and its children.  We do this so
10450    that we know whether or not a reference needs to use FORM_ref_addr; only
10451    DIEs in the same CU will be marked.  We used to clear out the offset
10452    and use that as the flag, but ran into ordering problems.  */
10453
10454 static void
10455 mark_dies (dw_die_ref die)
10456 {
10457   dw_die_ref c;
10458
10459   gcc_assert (!die->die_mark);
10460
10461   die->die_mark = 1;
10462   FOR_EACH_CHILD (die, c, mark_dies (c));
10463 }
10464
10465 /* Clear the marks for a die and its children.  */
10466
10467 static void
10468 unmark_dies (dw_die_ref die)
10469 {
10470   dw_die_ref c;
10471
10472   if (dwarf_version < 4)
10473     gcc_assert (die->die_mark);
10474
10475   die->die_mark = 0;
10476   FOR_EACH_CHILD (die, c, unmark_dies (c));
10477 }
10478
10479 /* Clear the marks for a die, its children and referred dies.  */
10480
10481 static void
10482 unmark_all_dies (dw_die_ref die)
10483 {
10484   dw_die_ref c;
10485   dw_attr_ref a;
10486   unsigned ix;
10487
10488   if (!die->die_mark)
10489     return;
10490   die->die_mark = 0;
10491
10492   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10493
10494   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10495     if (AT_class (a) == dw_val_class_die_ref)
10496       unmark_all_dies (AT_ref (a));
10497 }
10498
10499 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10500    generated for the compilation unit.  */
10501
10502 static unsigned long
10503 size_of_pubnames (VEC (pubname_entry, gc) * names)
10504 {
10505   unsigned long size;
10506   unsigned i;
10507   pubname_ref p;
10508
10509   size = DWARF_PUBNAMES_HEADER_SIZE;
10510   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10511     if (names != pubtype_table
10512         || p->die->die_offset != 0
10513         || !flag_eliminate_unused_debug_types)
10514       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10515
10516   size += DWARF_OFFSET_SIZE;
10517   return size;
10518 }
10519
10520 /* Return the size of the information in the .debug_aranges section.  */
10521
10522 static unsigned long
10523 size_of_aranges (void)
10524 {
10525   unsigned long size;
10526
10527   size = DWARF_ARANGES_HEADER_SIZE;
10528
10529   /* Count the address/length pair for this compilation unit.  */
10530   if (text_section_used)
10531     size += 2 * DWARF2_ADDR_SIZE;
10532   if (cold_text_section_used)
10533     size += 2 * DWARF2_ADDR_SIZE;
10534   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10535
10536   /* Count the two zero words used to terminated the address range table.  */
10537   size += 2 * DWARF2_ADDR_SIZE;
10538   return size;
10539 }
10540 \f
10541 /* Select the encoding of an attribute value.  */
10542
10543 static enum dwarf_form
10544 value_format (dw_attr_ref a)
10545 {
10546   switch (a->dw_attr_val.val_class)
10547     {
10548     case dw_val_class_addr:
10549       /* Only very few attributes allow DW_FORM_addr.  */
10550       switch (a->dw_attr)
10551         {
10552         case DW_AT_low_pc:
10553         case DW_AT_high_pc:
10554         case DW_AT_entry_pc:
10555         case DW_AT_trampoline:
10556           return DW_FORM_addr;
10557         default:
10558           break;
10559         }
10560       switch (DWARF2_ADDR_SIZE)
10561         {
10562         case 1:
10563           return DW_FORM_data1;
10564         case 2:
10565           return DW_FORM_data2;
10566         case 4:
10567           return DW_FORM_data4;
10568         case 8:
10569           return DW_FORM_data8;
10570         default:
10571           gcc_unreachable ();
10572         }
10573     case dw_val_class_range_list:
10574     case dw_val_class_loc_list:
10575       if (dwarf_version >= 4)
10576         return DW_FORM_sec_offset;
10577       /* FALLTHRU */
10578     case dw_val_class_vms_delta:
10579     case dw_val_class_offset:
10580       switch (DWARF_OFFSET_SIZE)
10581         {
10582         case 4:
10583           return DW_FORM_data4;
10584         case 8:
10585           return DW_FORM_data8;
10586         default:
10587           gcc_unreachable ();
10588         }
10589     case dw_val_class_loc:
10590       if (dwarf_version >= 4)
10591         return DW_FORM_exprloc;
10592       switch (constant_size (size_of_locs (AT_loc (a))))
10593         {
10594         case 1:
10595           return DW_FORM_block1;
10596         case 2:
10597           return DW_FORM_block2;
10598         default:
10599           gcc_unreachable ();
10600         }
10601     case dw_val_class_const:
10602       return DW_FORM_sdata;
10603     case dw_val_class_unsigned_const:
10604       switch (constant_size (AT_unsigned (a)))
10605         {
10606         case 1:
10607           return DW_FORM_data1;
10608         case 2:
10609           return DW_FORM_data2;
10610         case 4:
10611           return DW_FORM_data4;
10612         case 8:
10613           return DW_FORM_data8;
10614         default:
10615           gcc_unreachable ();
10616         }
10617     case dw_val_class_const_double:
10618       switch (HOST_BITS_PER_WIDE_INT)
10619         {
10620         case 8:
10621           return DW_FORM_data2;
10622         case 16:
10623           return DW_FORM_data4;
10624         case 32:
10625           return DW_FORM_data8;
10626         case 64:
10627         default:
10628           return DW_FORM_block1;
10629         }
10630     case dw_val_class_vec:
10631       switch (constant_size (a->dw_attr_val.v.val_vec.length
10632                              * a->dw_attr_val.v.val_vec.elt_size))
10633         {
10634         case 1:
10635           return DW_FORM_block1;
10636         case 2:
10637           return DW_FORM_block2;
10638         case 4:
10639           return DW_FORM_block4;
10640         default:
10641           gcc_unreachable ();
10642         }
10643     case dw_val_class_flag:
10644       if (dwarf_version >= 4)
10645         {
10646           /* Currently all add_AT_flag calls pass in 1 as last argument,
10647              so DW_FORM_flag_present can be used.  If that ever changes,
10648              we'll need to use DW_FORM_flag and have some optimization
10649              in build_abbrev_table that will change those to
10650              DW_FORM_flag_present if it is set to 1 in all DIEs using
10651              the same abbrev entry.  */
10652           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10653           return DW_FORM_flag_present;
10654         }
10655       return DW_FORM_flag;
10656     case dw_val_class_die_ref:
10657       if (AT_ref_external (a))
10658         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10659       else
10660         return DW_FORM_ref;
10661     case dw_val_class_fde_ref:
10662       return DW_FORM_data;
10663     case dw_val_class_lbl_id:
10664       return DW_FORM_addr;
10665     case dw_val_class_lineptr:
10666     case dw_val_class_macptr:
10667       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10668     case dw_val_class_str:
10669       return AT_string_form (a);
10670     case dw_val_class_file:
10671       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10672         {
10673         case 1:
10674           return DW_FORM_data1;
10675         case 2:
10676           return DW_FORM_data2;
10677         case 4:
10678           return DW_FORM_data4;
10679         default:
10680           gcc_unreachable ();
10681         }
10682
10683     case dw_val_class_data8:
10684       return DW_FORM_data8;
10685
10686     default:
10687       gcc_unreachable ();
10688     }
10689 }
10690
10691 /* Output the encoding of an attribute value.  */
10692
10693 static void
10694 output_value_format (dw_attr_ref a)
10695 {
10696   enum dwarf_form form = value_format (a);
10697
10698   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10699 }
10700
10701 /* Output the .debug_abbrev section which defines the DIE abbreviation
10702    table.  */
10703
10704 static void
10705 output_abbrev_section (void)
10706 {
10707   unsigned long abbrev_id;
10708
10709   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10710     {
10711       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10712       unsigned ix;
10713       dw_attr_ref a_attr;
10714
10715       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10716       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10717                                    dwarf_tag_name (abbrev->die_tag));
10718
10719       if (abbrev->die_child != NULL)
10720         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10721       else
10722         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10723
10724       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10725            ix++)
10726         {
10727           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10728                                        dwarf_attr_name (a_attr->dw_attr));
10729           output_value_format (a_attr);
10730         }
10731
10732       dw2_asm_output_data (1, 0, NULL);
10733       dw2_asm_output_data (1, 0, NULL);
10734     }
10735
10736   /* Terminate the table.  */
10737   dw2_asm_output_data (1, 0, NULL);
10738 }
10739
10740 /* Output a symbol we can use to refer to this DIE from another CU.  */
10741
10742 static inline void
10743 output_die_symbol (dw_die_ref die)
10744 {
10745   char *sym = die->die_id.die_symbol;
10746
10747   if (sym == 0)
10748     return;
10749
10750   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10751     /* We make these global, not weak; if the target doesn't support
10752        .linkonce, it doesn't support combining the sections, so debugging
10753        will break.  */
10754     targetm.asm_out.globalize_label (asm_out_file, sym);
10755
10756   ASM_OUTPUT_LABEL (asm_out_file, sym);
10757 }
10758
10759 /* Return a new location list, given the begin and end range, and the
10760    expression.  */
10761
10762 static inline dw_loc_list_ref
10763 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10764               const char *section)
10765 {
10766   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10767
10768   retlist->begin = begin;
10769   retlist->end = end;
10770   retlist->expr = expr;
10771   retlist->section = section;
10772
10773   return retlist;
10774 }
10775
10776 /* Generate a new internal symbol for this location list node, if it
10777    hasn't got one yet.  */
10778
10779 static inline void
10780 gen_llsym (dw_loc_list_ref list)
10781 {
10782   gcc_assert (!list->ll_symbol);
10783   list->ll_symbol = gen_internal_sym ("LLST");
10784 }
10785
10786 /* Output the location list given to us.  */
10787
10788 static void
10789 output_loc_list (dw_loc_list_ref list_head)
10790 {
10791   dw_loc_list_ref curr = list_head;
10792
10793   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10794
10795   /* Walk the location list, and output each range + expression.  */
10796   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10797     {
10798       unsigned long size;
10799       /* Don't output an entry that starts and ends at the same address.  */
10800       if (strcmp (curr->begin, curr->end) == 0)
10801         continue;
10802       if (!have_multiple_function_sections)
10803         {
10804           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10805                                 "Location list begin address (%s)",
10806                                 list_head->ll_symbol);
10807           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10808                                 "Location list end address (%s)",
10809                                 list_head->ll_symbol);
10810         }
10811       else
10812         {
10813           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10814                                "Location list begin address (%s)",
10815                                list_head->ll_symbol);
10816           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10817                                "Location list end address (%s)",
10818                                list_head->ll_symbol);
10819         }
10820       size = size_of_locs (curr->expr);
10821
10822       /* Output the block length for this list of location operations.  */
10823       gcc_assert (size <= 0xffff);
10824       dw2_asm_output_data (2, size, "%s", "Location expression size");
10825
10826       output_loc_sequence (curr->expr);
10827     }
10828
10829   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10830                        "Location list terminator begin (%s)",
10831                        list_head->ll_symbol);
10832   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10833                        "Location list terminator end (%s)",
10834                        list_head->ll_symbol);
10835 }
10836
10837 /* Output a type signature.  */
10838
10839 static inline void
10840 output_signature (const char *sig, const char *name)
10841 {
10842   int i;
10843
10844   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10845     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10846 }
10847
10848 /* Output the DIE and its attributes.  Called recursively to generate
10849    the definitions of each child DIE.  */
10850
10851 static void
10852 output_die (dw_die_ref die)
10853 {
10854   dw_attr_ref a;
10855   dw_die_ref c;
10856   unsigned long size;
10857   unsigned ix;
10858
10859   /* If someone in another CU might refer to us, set up a symbol for
10860      them to point to.  */
10861   if (dwarf_version < 4 && die->die_id.die_symbol)
10862     output_die_symbol (die);
10863
10864   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10865                                (unsigned long)die->die_offset,
10866                                dwarf_tag_name (die->die_tag));
10867
10868   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10869     {
10870       const char *name = dwarf_attr_name (a->dw_attr);
10871
10872       switch (AT_class (a))
10873         {
10874         case dw_val_class_addr:
10875           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10876           break;
10877
10878         case dw_val_class_offset:
10879           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10880                                "%s", name);
10881           break;
10882
10883         case dw_val_class_range_list:
10884           {
10885             char *p = strchr (ranges_section_label, '\0');
10886
10887             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10888                      a->dw_attr_val.v.val_offset);
10889             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10890                                    debug_ranges_section, "%s", name);
10891             *p = '\0';
10892           }
10893           break;
10894
10895         case dw_val_class_loc:
10896           size = size_of_locs (AT_loc (a));
10897
10898           /* Output the block length for this list of location operations.  */
10899           if (dwarf_version >= 4)
10900             dw2_asm_output_data_uleb128 (size, "%s", name);
10901           else
10902             dw2_asm_output_data (constant_size (size), size, "%s", name);
10903
10904           output_loc_sequence (AT_loc (a));
10905           break;
10906
10907         case dw_val_class_const:
10908           /* ??? It would be slightly more efficient to use a scheme like is
10909              used for unsigned constants below, but gdb 4.x does not sign
10910              extend.  Gdb 5.x does sign extend.  */
10911           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10912           break;
10913
10914         case dw_val_class_unsigned_const:
10915           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10916                                AT_unsigned (a), "%s", name);
10917           break;
10918
10919         case dw_val_class_const_double:
10920           {
10921             unsigned HOST_WIDE_INT first, second;
10922
10923             if (HOST_BITS_PER_WIDE_INT >= 64)
10924               dw2_asm_output_data (1,
10925                                    2 * HOST_BITS_PER_WIDE_INT
10926                                    / HOST_BITS_PER_CHAR,
10927                                    NULL);
10928
10929             if (WORDS_BIG_ENDIAN)
10930               {
10931                 first = a->dw_attr_val.v.val_double.high;
10932                 second = a->dw_attr_val.v.val_double.low;
10933               }
10934             else
10935               {
10936                 first = a->dw_attr_val.v.val_double.low;
10937                 second = a->dw_attr_val.v.val_double.high;
10938               }
10939
10940             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10941                                  first, name);
10942             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10943                                  second, NULL);
10944           }
10945           break;
10946
10947         case dw_val_class_vec:
10948           {
10949             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10950             unsigned int len = a->dw_attr_val.v.val_vec.length;
10951             unsigned int i;
10952             unsigned char *p;
10953
10954             dw2_asm_output_data (constant_size (len * elt_size),
10955                                  len * elt_size, "%s", name);
10956             if (elt_size > sizeof (HOST_WIDE_INT))
10957               {
10958                 elt_size /= 2;
10959                 len *= 2;
10960               }
10961             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10962                  i < len;
10963                  i++, p += elt_size)
10964               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10965                                    "fp or vector constant word %u", i);
10966             break;
10967           }
10968
10969         case dw_val_class_flag:
10970           if (dwarf_version >= 4)
10971             {
10972               /* Currently all add_AT_flag calls pass in 1 as last argument,
10973                  so DW_FORM_flag_present can be used.  If that ever changes,
10974                  we'll need to use DW_FORM_flag and have some optimization
10975                  in build_abbrev_table that will change those to
10976                  DW_FORM_flag_present if it is set to 1 in all DIEs using
10977                  the same abbrev entry.  */
10978               gcc_assert (AT_flag (a) == 1);
10979               if (flag_debug_asm)
10980                 fprintf (asm_out_file, "\t\t\t%s %s\n",
10981                          ASM_COMMENT_START, name);
10982               break;
10983             }
10984           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10985           break;
10986
10987         case dw_val_class_loc_list:
10988           {
10989             char *sym = AT_loc_list (a)->ll_symbol;
10990
10991             gcc_assert (sym);
10992             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10993                                    "%s", name);
10994           }
10995           break;
10996
10997         case dw_val_class_die_ref:
10998           if (AT_ref_external (a))
10999             {
11000               if (dwarf_version >= 4)
11001                 {
11002                   comdat_type_node_ref type_node =
11003                     AT_ref (a)->die_id.die_type_node;
11004
11005                   gcc_assert (type_node);
11006                   output_signature (type_node->signature, name);
11007                 }
11008               else
11009                 {
11010                   char *sym = AT_ref (a)->die_id.die_symbol;
11011                   int size;
11012
11013                   gcc_assert (sym);
11014                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11015                      length, whereas in DWARF3 it's always sized as an
11016                      offset.  */
11017                   if (dwarf_version == 2)
11018                     size = DWARF2_ADDR_SIZE;
11019                   else
11020                     size = DWARF_OFFSET_SIZE;
11021                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11022                                          name);
11023                 }
11024             }
11025           else
11026             {
11027               gcc_assert (AT_ref (a)->die_offset);
11028               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11029                                    "%s", name);
11030             }
11031           break;
11032
11033         case dw_val_class_fde_ref:
11034           {
11035             char l1[20];
11036
11037             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11038                                          a->dw_attr_val.v.val_fde_index * 2);
11039             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11040                                    "%s", name);
11041           }
11042           break;
11043
11044         case dw_val_class_vms_delta:
11045           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11046                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11047                                     "%s", name);
11048           break;
11049
11050         case dw_val_class_lbl_id:
11051           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11052           break;
11053
11054         case dw_val_class_lineptr:
11055           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11056                                  debug_line_section, "%s", name);
11057           break;
11058
11059         case dw_val_class_macptr:
11060           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11061                                  debug_macinfo_section, "%s", name);
11062           break;
11063
11064         case dw_val_class_str:
11065           if (AT_string_form (a) == DW_FORM_strp)
11066             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11067                                    a->dw_attr_val.v.val_str->label,
11068                                    debug_str_section,
11069                                    "%s: \"%s\"", name, AT_string (a));
11070           else
11071             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11072           break;
11073
11074         case dw_val_class_file:
11075           {
11076             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11077
11078             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11079                                  a->dw_attr_val.v.val_file->filename);
11080             break;
11081           }
11082
11083         case dw_val_class_data8:
11084           {
11085             int i;
11086
11087             for (i = 0; i < 8; i++)
11088               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11089                                    i == 0 ? "%s" : NULL, name);
11090             break;
11091           }
11092
11093         default:
11094           gcc_unreachable ();
11095         }
11096     }
11097
11098   FOR_EACH_CHILD (die, c, output_die (c));
11099
11100   /* Add null byte to terminate sibling list.  */
11101   if (die->die_child != NULL)
11102     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11103                          (unsigned long) die->die_offset);
11104 }
11105
11106 /* Output the compilation unit that appears at the beginning of the
11107    .debug_info section, and precedes the DIE descriptions.  */
11108
11109 static void
11110 output_compilation_unit_header (void)
11111 {
11112   int ver = dwarf_version;
11113
11114   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11115     dw2_asm_output_data (4, 0xffffffff,
11116       "Initial length escape value indicating 64-bit DWARF extension");
11117   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11118                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11119                        "Length of Compilation Unit Info");
11120   dw2_asm_output_data (2, ver, "DWARF version number");
11121   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11122                          debug_abbrev_section,
11123                          "Offset Into Abbrev. Section");
11124   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11125 }
11126
11127 /* Output the compilation unit DIE and its children.  */
11128
11129 static void
11130 output_comp_unit (dw_die_ref die, int output_if_empty)
11131 {
11132   const char *secname;
11133   char *oldsym, *tmp;
11134
11135   /* Unless we are outputting main CU, we may throw away empty ones.  */
11136   if (!output_if_empty && die->die_child == NULL)
11137     return;
11138
11139   /* Even if there are no children of this DIE, we must output the information
11140      about the compilation unit.  Otherwise, on an empty translation unit, we
11141      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11142      will then complain when examining the file.  First mark all the DIEs in
11143      this CU so we know which get local refs.  */
11144   mark_dies (die);
11145
11146   build_abbrev_table (die);
11147
11148   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11149   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11150   calc_die_sizes (die);
11151
11152   oldsym = die->die_id.die_symbol;
11153   if (oldsym)
11154     {
11155       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11156
11157       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11158       secname = tmp;
11159       die->die_id.die_symbol = NULL;
11160       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11161     }
11162   else
11163     switch_to_section (debug_info_section);
11164
11165   /* Output debugging information.  */
11166   output_compilation_unit_header ();
11167   output_die (die);
11168
11169   /* Leave the marks on the main CU, so we can check them in
11170      output_pubnames.  */
11171   if (oldsym)
11172     {
11173       unmark_dies (die);
11174       die->die_id.die_symbol = oldsym;
11175     }
11176 }
11177
11178 /* Output a comdat type unit DIE and its children.  */
11179
11180 static void
11181 output_comdat_type_unit (comdat_type_node *node)
11182 {
11183   const char *secname;
11184   char *tmp;
11185   int i;
11186 #if defined (OBJECT_FORMAT_ELF)
11187   tree comdat_key;
11188 #endif
11189
11190   /* First mark all the DIEs in this CU so we know which get local refs.  */
11191   mark_dies (node->root_die);
11192
11193   build_abbrev_table (node->root_die);
11194
11195   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11196   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11197   calc_die_sizes (node->root_die);
11198
11199 #if defined (OBJECT_FORMAT_ELF)
11200   secname = ".debug_types";
11201   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11202   sprintf (tmp, "wt.");
11203   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11204     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11205   comdat_key = get_identifier (tmp);
11206   targetm.asm_out.named_section (secname,
11207                                  SECTION_DEBUG | SECTION_LINKONCE,
11208                                  comdat_key);
11209 #else
11210   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11211   sprintf (tmp, ".gnu.linkonce.wt.");
11212   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11213     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11214   secname = tmp;
11215   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11216 #endif
11217
11218   /* Output debugging information.  */
11219   output_compilation_unit_header ();
11220   output_signature (node->signature, "Type Signature");
11221   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11222                        "Offset to Type DIE");
11223   output_die (node->root_die);
11224
11225   unmark_dies (node->root_die);
11226 }
11227
11228 /* Return the DWARF2/3 pubname associated with a decl.  */
11229
11230 static const char *
11231 dwarf2_name (tree decl, int scope)
11232 {
11233   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11234 }
11235
11236 /* Add a new entry to .debug_pubnames if appropriate.  */
11237
11238 static void
11239 add_pubname_string (const char *str, dw_die_ref die)
11240 {
11241   pubname_entry e;
11242
11243   e.die = die;
11244   e.name = xstrdup (str);
11245   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11246 }
11247
11248 static void
11249 add_pubname (tree decl, dw_die_ref die)
11250 {
11251   if (TREE_PUBLIC (decl))
11252     {
11253       const char *name = dwarf2_name (decl, 1);
11254       if (name)
11255         add_pubname_string (name, die);
11256     }
11257 }
11258
11259 /* Add a new entry to .debug_pubtypes if appropriate.  */
11260
11261 static void
11262 add_pubtype (tree decl, dw_die_ref die)
11263 {
11264   pubname_entry e;
11265
11266   e.name = NULL;
11267   if ((TREE_PUBLIC (decl)
11268        || die->die_parent == comp_unit_die)
11269       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11270     {
11271       e.die = die;
11272       if (TYPE_P (decl))
11273         {
11274           if (TYPE_NAME (decl))
11275             {
11276               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11277                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11278               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11279                        && DECL_NAME (TYPE_NAME (decl)))
11280                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11281               else
11282                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11283             }
11284         }
11285       else
11286         {
11287           e.name = dwarf2_name (decl, 1);
11288           if (e.name)
11289             e.name = xstrdup (e.name);
11290         }
11291
11292       /* If we don't have a name for the type, there's no point in adding
11293          it to the table.  */
11294       if (e.name && e.name[0] != '\0')
11295         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11296     }
11297 }
11298
11299 /* Output the public names table used to speed up access to externally
11300    visible names; or the public types table used to find type definitions.  */
11301
11302 static void
11303 output_pubnames (VEC (pubname_entry, gc) * names)
11304 {
11305   unsigned i;
11306   unsigned long pubnames_length = size_of_pubnames (names);
11307   pubname_ref pub;
11308
11309   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11310     dw2_asm_output_data (4, 0xffffffff,
11311       "Initial length escape value indicating 64-bit DWARF extension");
11312   if (names == pubname_table)
11313     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11314                          "Length of Public Names Info");
11315   else
11316     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11317                          "Length of Public Type Names Info");
11318   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11319   dw2_asm_output_data (2, 2, "DWARF Version");
11320   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11321                          debug_info_section,
11322                          "Offset of Compilation Unit Info");
11323   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11324                        "Compilation Unit Length");
11325
11326   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11327     {
11328       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11329       if (names == pubname_table)
11330         gcc_assert (pub->die->die_mark);
11331
11332       if (names != pubtype_table
11333           || pub->die->die_offset != 0
11334           || !flag_eliminate_unused_debug_types)
11335         {
11336           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11337                                "DIE offset");
11338
11339           dw2_asm_output_nstring (pub->name, -1, "external name");
11340         }
11341     }
11342
11343   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11344 }
11345
11346 /* Add a new entry to .debug_aranges if appropriate.  */
11347
11348 static void
11349 add_arange (tree decl, dw_die_ref die)
11350 {
11351   if (! DECL_SECTION_NAME (decl))
11352     return;
11353
11354   if (arange_table_in_use == arange_table_allocated)
11355     {
11356       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11357       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11358                                     arange_table_allocated);
11359       memset (arange_table + arange_table_in_use, 0,
11360               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11361     }
11362
11363   arange_table[arange_table_in_use++] = die;
11364 }
11365
11366 /* Output the information that goes into the .debug_aranges table.
11367    Namely, define the beginning and ending address range of the
11368    text section generated for this compilation unit.  */
11369
11370 static void
11371 output_aranges (void)
11372 {
11373   unsigned i;
11374   unsigned long aranges_length = size_of_aranges ();
11375
11376   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11377     dw2_asm_output_data (4, 0xffffffff,
11378       "Initial length escape value indicating 64-bit DWARF extension");
11379   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11380                        "Length of Address Ranges Info");
11381   /* Version number for aranges is still 2, even in DWARF3.  */
11382   dw2_asm_output_data (2, 2, "DWARF Version");
11383   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11384                          debug_info_section,
11385                          "Offset of Compilation Unit Info");
11386   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11387   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11388
11389   /* We need to align to twice the pointer size here.  */
11390   if (DWARF_ARANGES_PAD_SIZE)
11391     {
11392       /* Pad using a 2 byte words so that padding is correct for any
11393          pointer size.  */
11394       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11395                            2 * DWARF2_ADDR_SIZE);
11396       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11397         dw2_asm_output_data (2, 0, NULL);
11398     }
11399
11400   /* It is necessary not to output these entries if the sections were
11401      not used; if the sections were not used, the length will be 0 and
11402      the address may end up as 0 if the section is discarded by ld
11403      --gc-sections, leaving an invalid (0, 0) entry that can be
11404      confused with the terminator.  */
11405   if (text_section_used)
11406     {
11407       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11408       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11409                             text_section_label, "Length");
11410     }
11411   if (cold_text_section_used)
11412     {
11413       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11414                            "Address");
11415       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11416                             cold_text_section_label, "Length");
11417     }
11418
11419   for (i = 0; i < arange_table_in_use; i++)
11420     {
11421       dw_die_ref die = arange_table[i];
11422
11423       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11424       gcc_assert (die->die_mark);
11425
11426       if (die->die_tag == DW_TAG_subprogram)
11427         {
11428           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11429                                "Address");
11430           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11431                                 get_AT_low_pc (die), "Length");
11432         }
11433       else
11434         {
11435           /* A static variable; extract the symbol from DW_AT_location.
11436              Note that this code isn't currently hit, as we only emit
11437              aranges for functions (jason 9/23/99).  */
11438           dw_attr_ref a = get_AT (die, DW_AT_location);
11439           dw_loc_descr_ref loc;
11440
11441           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11442
11443           loc = AT_loc (a);
11444           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11445
11446           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11447                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11448           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11449                                get_AT_unsigned (die, DW_AT_byte_size),
11450                                "Length");
11451         }
11452     }
11453
11454   /* Output the terminator words.  */
11455   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11456   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11457 }
11458
11459 /* Add a new entry to .debug_ranges.  Return the offset at which it
11460    was placed.  */
11461
11462 static unsigned int
11463 add_ranges_num (int num)
11464 {
11465   unsigned int in_use = ranges_table_in_use;
11466
11467   if (in_use == ranges_table_allocated)
11468     {
11469       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11470       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11471                                     ranges_table_allocated);
11472       memset (ranges_table + ranges_table_in_use, 0,
11473               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11474     }
11475
11476   ranges_table[in_use].num = num;
11477   ranges_table_in_use = in_use + 1;
11478
11479   return in_use * 2 * DWARF2_ADDR_SIZE;
11480 }
11481
11482 /* Add a new entry to .debug_ranges corresponding to a block, or a
11483    range terminator if BLOCK is NULL.  */
11484
11485 static unsigned int
11486 add_ranges (const_tree block)
11487 {
11488   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11489 }
11490
11491 /* Add a new entry to .debug_ranges corresponding to a pair of
11492    labels.  */
11493
11494 static void
11495 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11496                       bool *added)
11497 {
11498   unsigned int in_use = ranges_by_label_in_use;
11499   unsigned int offset;
11500
11501   if (in_use == ranges_by_label_allocated)
11502     {
11503       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11504       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11505                                        ranges_by_label,
11506                                        ranges_by_label_allocated);
11507       memset (ranges_by_label + ranges_by_label_in_use, 0,
11508               RANGES_TABLE_INCREMENT
11509               * sizeof (struct dw_ranges_by_label_struct));
11510     }
11511
11512   ranges_by_label[in_use].begin = begin;
11513   ranges_by_label[in_use].end = end;
11514   ranges_by_label_in_use = in_use + 1;
11515
11516   offset = add_ranges_num (-(int)in_use - 1);
11517   if (!*added)
11518     {
11519       add_AT_range_list (die, DW_AT_ranges, offset);
11520       *added = true;
11521     }
11522 }
11523
11524 static void
11525 output_ranges (void)
11526 {
11527   unsigned i;
11528   static const char *const start_fmt = "Offset %#x";
11529   const char *fmt = start_fmt;
11530
11531   for (i = 0; i < ranges_table_in_use; i++)
11532     {
11533       int block_num = ranges_table[i].num;
11534
11535       if (block_num > 0)
11536         {
11537           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11538           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11539
11540           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11541           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11542
11543           /* If all code is in the text section, then the compilation
11544              unit base address defaults to DW_AT_low_pc, which is the
11545              base of the text section.  */
11546           if (!have_multiple_function_sections)
11547             {
11548               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11549                                     text_section_label,
11550                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11551               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11552                                     text_section_label, NULL);
11553             }
11554
11555           /* Otherwise, the compilation unit base address is zero,
11556              which allows us to use absolute addresses, and not worry
11557              about whether the target supports cross-section
11558              arithmetic.  */
11559           else
11560             {
11561               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11562                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11563               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11564             }
11565
11566           fmt = NULL;
11567         }
11568
11569       /* Negative block_num stands for an index into ranges_by_label.  */
11570       else if (block_num < 0)
11571         {
11572           int lab_idx = - block_num - 1;
11573
11574           if (!have_multiple_function_sections)
11575             {
11576               gcc_unreachable ();
11577 #if 0
11578               /* If we ever use add_ranges_by_labels () for a single
11579                  function section, all we have to do is to take out
11580                  the #if 0 above.  */
11581               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11582                                     ranges_by_label[lab_idx].begin,
11583                                     text_section_label,
11584                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11585               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11586                                     ranges_by_label[lab_idx].end,
11587                                     text_section_label, NULL);
11588 #endif
11589             }
11590           else
11591             {
11592               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11593                                    ranges_by_label[lab_idx].begin,
11594                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11595               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11596                                    ranges_by_label[lab_idx].end,
11597                                    NULL);
11598             }
11599         }
11600       else
11601         {
11602           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11603           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11604           fmt = start_fmt;
11605         }
11606     }
11607 }
11608
11609 /* Data structure containing information about input files.  */
11610 struct file_info
11611 {
11612   const char *path;     /* Complete file name.  */
11613   const char *fname;    /* File name part.  */
11614   int length;           /* Length of entire string.  */
11615   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11616   int dir_idx;          /* Index in directory table.  */
11617 };
11618
11619 /* Data structure containing information about directories with source
11620    files.  */
11621 struct dir_info
11622 {
11623   const char *path;     /* Path including directory name.  */
11624   int length;           /* Path length.  */
11625   int prefix;           /* Index of directory entry which is a prefix.  */
11626   int count;            /* Number of files in this directory.  */
11627   int dir_idx;          /* Index of directory used as base.  */
11628 };
11629
11630 /* Callback function for file_info comparison.  We sort by looking at
11631    the directories in the path.  */
11632
11633 static int
11634 file_info_cmp (const void *p1, const void *p2)
11635 {
11636   const struct file_info *const s1 = (const struct file_info *) p1;
11637   const struct file_info *const s2 = (const struct file_info *) p2;
11638   const unsigned char *cp1;
11639   const unsigned char *cp2;
11640
11641   /* Take care of file names without directories.  We need to make sure that
11642      we return consistent values to qsort since some will get confused if
11643      we return the same value when identical operands are passed in opposite
11644      orders.  So if neither has a directory, return 0 and otherwise return
11645      1 or -1 depending on which one has the directory.  */
11646   if ((s1->path == s1->fname || s2->path == s2->fname))
11647     return (s2->path == s2->fname) - (s1->path == s1->fname);
11648
11649   cp1 = (const unsigned char *) s1->path;
11650   cp2 = (const unsigned char *) s2->path;
11651
11652   while (1)
11653     {
11654       ++cp1;
11655       ++cp2;
11656       /* Reached the end of the first path?  If so, handle like above.  */
11657       if ((cp1 == (const unsigned char *) s1->fname)
11658           || (cp2 == (const unsigned char *) s2->fname))
11659         return ((cp2 == (const unsigned char *) s2->fname)
11660                 - (cp1 == (const unsigned char *) s1->fname));
11661
11662       /* Character of current path component the same?  */
11663       else if (*cp1 != *cp2)
11664         return *cp1 - *cp2;
11665     }
11666 }
11667
11668 struct file_name_acquire_data
11669 {
11670   struct file_info *files;
11671   int used_files;
11672   int max_files;
11673 };
11674
11675 /* Traversal function for the hash table.  */
11676
11677 static int
11678 file_name_acquire (void ** slot, void *data)
11679 {
11680   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11681   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11682   struct file_info *fi;
11683   const char *f;
11684
11685   gcc_assert (fnad->max_files >= d->emitted_number);
11686
11687   if (! d->emitted_number)
11688     return 1;
11689
11690   gcc_assert (fnad->max_files != fnad->used_files);
11691
11692   fi = fnad->files + fnad->used_files++;
11693
11694   /* Skip all leading "./".  */
11695   f = d->filename;
11696   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11697     f += 2;
11698
11699   /* Create a new array entry.  */
11700   fi->path = f;
11701   fi->length = strlen (f);
11702   fi->file_idx = d;
11703
11704   /* Search for the file name part.  */
11705   f = strrchr (f, DIR_SEPARATOR);
11706 #if defined (DIR_SEPARATOR_2)
11707   {
11708     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11709
11710     if (g != NULL)
11711       {
11712         if (f == NULL || f < g)
11713           f = g;
11714       }
11715   }
11716 #endif
11717
11718   fi->fname = f == NULL ? fi->path : f + 1;
11719   return 1;
11720 }
11721
11722 /* Output the directory table and the file name table.  We try to minimize
11723    the total amount of memory needed.  A heuristic is used to avoid large
11724    slowdowns with many input files.  */
11725
11726 static void
11727 output_file_names (void)
11728 {
11729   struct file_name_acquire_data fnad;
11730   int numfiles;
11731   struct file_info *files;
11732   struct dir_info *dirs;
11733   int *saved;
11734   int *savehere;
11735   int *backmap;
11736   int ndirs;
11737   int idx_offset;
11738   int i;
11739
11740   if (!last_emitted_file)
11741     {
11742       dw2_asm_output_data (1, 0, "End directory table");
11743       dw2_asm_output_data (1, 0, "End file name table");
11744       return;
11745     }
11746
11747   numfiles = last_emitted_file->emitted_number;
11748
11749   /* Allocate the various arrays we need.  */
11750   files = XALLOCAVEC (struct file_info, numfiles);
11751   dirs = XALLOCAVEC (struct dir_info, numfiles);
11752
11753   fnad.files = files;
11754   fnad.used_files = 0;
11755   fnad.max_files = numfiles;
11756   htab_traverse (file_table, file_name_acquire, &fnad);
11757   gcc_assert (fnad.used_files == fnad.max_files);
11758
11759   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11760
11761   /* Find all the different directories used.  */
11762   dirs[0].path = files[0].path;
11763   dirs[0].length = files[0].fname - files[0].path;
11764   dirs[0].prefix = -1;
11765   dirs[0].count = 1;
11766   dirs[0].dir_idx = 0;
11767   files[0].dir_idx = 0;
11768   ndirs = 1;
11769
11770   for (i = 1; i < numfiles; i++)
11771     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11772         && memcmp (dirs[ndirs - 1].path, files[i].path,
11773                    dirs[ndirs - 1].length) == 0)
11774       {
11775         /* Same directory as last entry.  */
11776         files[i].dir_idx = ndirs - 1;
11777         ++dirs[ndirs - 1].count;
11778       }
11779     else
11780       {
11781         int j;
11782
11783         /* This is a new directory.  */
11784         dirs[ndirs].path = files[i].path;
11785         dirs[ndirs].length = files[i].fname - files[i].path;
11786         dirs[ndirs].count = 1;
11787         dirs[ndirs].dir_idx = ndirs;
11788         files[i].dir_idx = ndirs;
11789
11790         /* Search for a prefix.  */
11791         dirs[ndirs].prefix = -1;
11792         for (j = 0; j < ndirs; j++)
11793           if (dirs[j].length < dirs[ndirs].length
11794               && dirs[j].length > 1
11795               && (dirs[ndirs].prefix == -1
11796                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11797               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11798             dirs[ndirs].prefix = j;
11799
11800         ++ndirs;
11801       }
11802
11803   /* Now to the actual work.  We have to find a subset of the directories which
11804      allow expressing the file name using references to the directory table
11805      with the least amount of characters.  We do not do an exhaustive search
11806      where we would have to check out every combination of every single
11807      possible prefix.  Instead we use a heuristic which provides nearly optimal
11808      results in most cases and never is much off.  */
11809   saved = XALLOCAVEC (int, ndirs);
11810   savehere = XALLOCAVEC (int, ndirs);
11811
11812   memset (saved, '\0', ndirs * sizeof (saved[0]));
11813   for (i = 0; i < ndirs; i++)
11814     {
11815       int j;
11816       int total;
11817
11818       /* We can always save some space for the current directory.  But this
11819          does not mean it will be enough to justify adding the directory.  */
11820       savehere[i] = dirs[i].length;
11821       total = (savehere[i] - saved[i]) * dirs[i].count;
11822
11823       for (j = i + 1; j < ndirs; j++)
11824         {
11825           savehere[j] = 0;
11826           if (saved[j] < dirs[i].length)
11827             {
11828               /* Determine whether the dirs[i] path is a prefix of the
11829                  dirs[j] path.  */
11830               int k;
11831
11832               k = dirs[j].prefix;
11833               while (k != -1 && k != (int) i)
11834                 k = dirs[k].prefix;
11835
11836               if (k == (int) i)
11837                 {
11838                   /* Yes it is.  We can possibly save some memory by
11839                      writing the filenames in dirs[j] relative to
11840                      dirs[i].  */
11841                   savehere[j] = dirs[i].length;
11842                   total += (savehere[j] - saved[j]) * dirs[j].count;
11843                 }
11844             }
11845         }
11846
11847       /* Check whether we can save enough to justify adding the dirs[i]
11848          directory.  */
11849       if (total > dirs[i].length + 1)
11850         {
11851           /* It's worthwhile adding.  */
11852           for (j = i; j < ndirs; j++)
11853             if (savehere[j] > 0)
11854               {
11855                 /* Remember how much we saved for this directory so far.  */
11856                 saved[j] = savehere[j];
11857
11858                 /* Remember the prefix directory.  */
11859                 dirs[j].dir_idx = i;
11860               }
11861         }
11862     }
11863
11864   /* Emit the directory name table.  */
11865   idx_offset = dirs[0].length > 0 ? 1 : 0;
11866   for (i = 1 - idx_offset; i < ndirs; i++)
11867     dw2_asm_output_nstring (dirs[i].path,
11868                             dirs[i].length
11869                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11870                             "Directory Entry: %#x", i + idx_offset);
11871
11872   dw2_asm_output_data (1, 0, "End directory table");
11873
11874   /* We have to emit them in the order of emitted_number since that's
11875      used in the debug info generation.  To do this efficiently we
11876      generate a back-mapping of the indices first.  */
11877   backmap = XALLOCAVEC (int, numfiles);
11878   for (i = 0; i < numfiles; i++)
11879     backmap[files[i].file_idx->emitted_number - 1] = i;
11880
11881   /* Now write all the file names.  */
11882   for (i = 0; i < numfiles; i++)
11883     {
11884       int file_idx = backmap[i];
11885       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11886
11887 #ifdef VMS_DEBUGGING_INFO
11888 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11889
11890       /* Setting these fields can lead to debugger miscomparisons,
11891          but VMS Debug requires them to be set correctly.  */
11892
11893       int ver;
11894       long long cdt;
11895       long siz;
11896       int maxfilelen = strlen (files[file_idx].path)
11897                                + dirs[dir_idx].length
11898                                + MAX_VMS_VERSION_LEN + 1;
11899       char *filebuf = XALLOCAVEC (char, maxfilelen);
11900
11901       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11902       snprintf (filebuf, maxfilelen, "%s;%d",
11903                 files[file_idx].path + dirs[dir_idx].length, ver);
11904
11905       dw2_asm_output_nstring
11906         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11907
11908       /* Include directory index.  */
11909       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11910
11911       /* Modification time.  */
11912       dw2_asm_output_data_uleb128
11913         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11914           ? cdt : 0,
11915          NULL);
11916
11917       /* File length in bytes.  */
11918       dw2_asm_output_data_uleb128
11919         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11920           ? siz : 0,
11921          NULL);
11922 #else
11923       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11924                               "File Entry: %#x", (unsigned) i + 1);
11925
11926       /* Include directory index.  */
11927       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11928
11929       /* Modification time.  */
11930       dw2_asm_output_data_uleb128 (0, NULL);
11931
11932       /* File length in bytes.  */
11933       dw2_asm_output_data_uleb128 (0, NULL);
11934 #endif
11935     }
11936
11937   dw2_asm_output_data (1, 0, "End file name table");
11938 }
11939
11940
11941 /* Output the source line number correspondence information.  This
11942    information goes into the .debug_line section.  */
11943
11944 static void
11945 output_line_info (void)
11946 {
11947   char l1[20], l2[20], p1[20], p2[20];
11948   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11949   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11950   unsigned opc;
11951   unsigned n_op_args;
11952   unsigned long lt_index;
11953   unsigned long current_line;
11954   long line_offset;
11955   long line_delta;
11956   unsigned long current_file;
11957   unsigned long function;
11958   int ver = dwarf_version;
11959
11960   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11961   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11962   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11963   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11964
11965   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11966     dw2_asm_output_data (4, 0xffffffff,
11967       "Initial length escape value indicating 64-bit DWARF extension");
11968   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11969                         "Length of Source Line Info");
11970   ASM_OUTPUT_LABEL (asm_out_file, l1);
11971
11972   dw2_asm_output_data (2, ver, "DWARF Version");
11973   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11974   ASM_OUTPUT_LABEL (asm_out_file, p1);
11975
11976   /* Define the architecture-dependent minimum instruction length (in
11977    bytes).  In this implementation of DWARF, this field is used for
11978    information purposes only.  Since GCC generates assembly language,
11979    we have no a priori knowledge of how many instruction bytes are
11980    generated for each source line, and therefore can use only the
11981    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11982    commands.  Accordingly, we fix this as `1', which is "correct
11983    enough" for all architectures, and don't let the target override.  */
11984   dw2_asm_output_data (1, 1,
11985                        "Minimum Instruction Length");
11986
11987   if (ver >= 4)
11988     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11989                          "Maximum Operations Per Instruction");
11990   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11991                        "Default is_stmt_start flag");
11992   dw2_asm_output_data (1, DWARF_LINE_BASE,
11993                        "Line Base Value (Special Opcodes)");
11994   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11995                        "Line Range Value (Special Opcodes)");
11996   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11997                        "Special Opcode Base");
11998
11999   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12000     {
12001       switch (opc)
12002         {
12003         case DW_LNS_advance_pc:
12004         case DW_LNS_advance_line:
12005         case DW_LNS_set_file:
12006         case DW_LNS_set_column:
12007         case DW_LNS_fixed_advance_pc:
12008           n_op_args = 1;
12009           break;
12010         default:
12011           n_op_args = 0;
12012           break;
12013         }
12014
12015       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12016                            opc, n_op_args);
12017     }
12018
12019   /* Write out the information about the files we use.  */
12020   output_file_names ();
12021   ASM_OUTPUT_LABEL (asm_out_file, p2);
12022
12023   /* We used to set the address register to the first location in the text
12024      section here, but that didn't accomplish anything since we already
12025      have a line note for the opening brace of the first function.  */
12026
12027   /* Generate the line number to PC correspondence table, encoded as
12028      a series of state machine operations.  */
12029   current_file = 1;
12030   current_line = 1;
12031
12032   if (cfun && in_cold_section_p)
12033     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12034   else
12035     strcpy (prev_line_label, text_section_label);
12036   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12037     {
12038       dw_line_info_ref line_info = &line_info_table[lt_index];
12039
12040 #if 0
12041       /* Disable this optimization for now; GDB wants to see two line notes
12042          at the beginning of a function so it can find the end of the
12043          prologue.  */
12044
12045       /* Don't emit anything for redundant notes.  Just updating the
12046          address doesn't accomplish anything, because we already assume
12047          that anything after the last address is this line.  */
12048       if (line_info->dw_line_num == current_line
12049           && line_info->dw_file_num == current_file)
12050         continue;
12051 #endif
12052
12053       /* Emit debug info for the address of the current line.
12054
12055          Unfortunately, we have little choice here currently, and must always
12056          use the most general form.  GCC does not know the address delta
12057          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12058          attributes which will give an upper bound on the address range.  We
12059          could perhaps use length attributes to determine when it is safe to
12060          use DW_LNS_fixed_advance_pc.  */
12061
12062       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12063       if (0)
12064         {
12065           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12066           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12067                                "DW_LNS_fixed_advance_pc");
12068           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12069         }
12070       else
12071         {
12072           /* This can handle any delta.  This takes
12073              4+DWARF2_ADDR_SIZE bytes.  */
12074           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12075           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12076           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12077           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12078         }
12079
12080       strcpy (prev_line_label, line_label);
12081
12082       /* Emit debug info for the source file of the current line, if
12083          different from the previous line.  */
12084       if (line_info->dw_file_num != current_file)
12085         {
12086           current_file = line_info->dw_file_num;
12087           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12088           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12089         }
12090
12091       /* Emit debug info for the current line number, choosing the encoding
12092          that uses the least amount of space.  */
12093       if (line_info->dw_line_num != current_line)
12094         {
12095           line_offset = line_info->dw_line_num - current_line;
12096           line_delta = line_offset - DWARF_LINE_BASE;
12097           current_line = line_info->dw_line_num;
12098           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12099             /* This can handle deltas from -10 to 234, using the current
12100                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12101                takes 1 byte.  */
12102             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12103                                  "line %lu", current_line);
12104           else
12105             {
12106               /* This can handle any delta.  This takes at least 4 bytes,
12107                  depending on the value being encoded.  */
12108               dw2_asm_output_data (1, DW_LNS_advance_line,
12109                                    "advance to line %lu", current_line);
12110               dw2_asm_output_data_sleb128 (line_offset, NULL);
12111               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12112             }
12113         }
12114       else
12115         /* We still need to start a new row, so output a copy insn.  */
12116         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12117     }
12118
12119   /* Emit debug info for the address of the end of the function.  */
12120   if (0)
12121     {
12122       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12123                            "DW_LNS_fixed_advance_pc");
12124       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12125     }
12126   else
12127     {
12128       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12129       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12130       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12131       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12132     }
12133
12134   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12135   dw2_asm_output_data_uleb128 (1, NULL);
12136   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12137
12138   function = 0;
12139   current_file = 1;
12140   current_line = 1;
12141   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12142     {
12143       dw_separate_line_info_ref line_info
12144         = &separate_line_info_table[lt_index];
12145
12146 #if 0
12147       /* Don't emit anything for redundant notes.  */
12148       if (line_info->dw_line_num == current_line
12149           && line_info->dw_file_num == current_file
12150           && line_info->function == function)
12151         goto cont;
12152 #endif
12153
12154       /* Emit debug info for the address of the current line.  If this is
12155          a new function, or the first line of a function, then we need
12156          to handle it differently.  */
12157       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12158                                    lt_index);
12159       if (function != line_info->function)
12160         {
12161           function = line_info->function;
12162
12163           /* Set the address register to the first line in the function.  */
12164           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12165           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12166           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12167           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12168         }
12169       else
12170         {
12171           /* ??? See the DW_LNS_advance_pc comment above.  */
12172           if (0)
12173             {
12174               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12175                                    "DW_LNS_fixed_advance_pc");
12176               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12177             }
12178           else
12179             {
12180               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12181               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12182               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12183               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12184             }
12185         }
12186
12187       strcpy (prev_line_label, line_label);
12188
12189       /* Emit debug info for the source file of the current line, if
12190          different from the previous line.  */
12191       if (line_info->dw_file_num != current_file)
12192         {
12193           current_file = line_info->dw_file_num;
12194           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12195           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12196         }
12197
12198       /* Emit debug info for the current line number, choosing the encoding
12199          that uses the least amount of space.  */
12200       if (line_info->dw_line_num != current_line)
12201         {
12202           line_offset = line_info->dw_line_num - current_line;
12203           line_delta = line_offset - DWARF_LINE_BASE;
12204           current_line = line_info->dw_line_num;
12205           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12206             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12207                                  "line %lu", current_line);
12208           else
12209             {
12210               dw2_asm_output_data (1, DW_LNS_advance_line,
12211                                    "advance to line %lu", current_line);
12212               dw2_asm_output_data_sleb128 (line_offset, NULL);
12213               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12214             }
12215         }
12216       else
12217         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12218
12219 #if 0
12220     cont:
12221 #endif
12222
12223       lt_index++;
12224
12225       /* If we're done with a function, end its sequence.  */
12226       if (lt_index == separate_line_info_table_in_use
12227           || separate_line_info_table[lt_index].function != function)
12228         {
12229           current_file = 1;
12230           current_line = 1;
12231
12232           /* Emit debug info for the address of the end of the function.  */
12233           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12234           if (0)
12235             {
12236               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12237                                    "DW_LNS_fixed_advance_pc");
12238               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12239             }
12240           else
12241             {
12242               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12243               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12244               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12245               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12246             }
12247
12248           /* Output the marker for the end of this sequence.  */
12249           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12250           dw2_asm_output_data_uleb128 (1, NULL);
12251           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12252         }
12253     }
12254
12255   /* Output the marker for the end of the line number info.  */
12256   ASM_OUTPUT_LABEL (asm_out_file, l2);
12257 }
12258
12259 /* Return the size of the .debug_dcall table for the compilation unit.  */
12260
12261 static unsigned long
12262 size_of_dcall_table (void)
12263 {
12264   unsigned long size;
12265   unsigned int i;
12266   dcall_entry *p;
12267   tree last_poc_decl = NULL;
12268
12269   /* Header:  version + debug info section pointer + pointer size.  */
12270   size = 2 + DWARF_OFFSET_SIZE + 1;
12271
12272   /* Each entry:  code label + DIE offset.  */
12273   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12274     {
12275       gcc_assert (p->targ_die != NULL);
12276       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12277       if (p->poc_decl != last_poc_decl)
12278         {
12279           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12280           gcc_assert (poc_die);
12281           last_poc_decl = p->poc_decl;
12282           if (poc_die)
12283             size += (DWARF_OFFSET_SIZE
12284                      + size_of_uleb128 (poc_die->die_offset));
12285         }
12286       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12287     }
12288
12289   return size;
12290 }
12291
12292 /* Output the direct call table used to disambiguate PC values when
12293    identical function have been merged.  */
12294
12295 static void
12296 output_dcall_table (void)
12297 {
12298   unsigned i;
12299   unsigned long dcall_length = size_of_dcall_table ();
12300   dcall_entry *p;
12301   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12302   tree last_poc_decl = NULL;
12303
12304   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12305     dw2_asm_output_data (4, 0xffffffff,
12306       "Initial length escape value indicating 64-bit DWARF extension");
12307   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12308                        "Length of Direct Call Table");
12309   dw2_asm_output_data (2, 4, "Version number");
12310   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12311                          debug_info_section,
12312                          "Offset of Compilation Unit Info");
12313   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12314
12315   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12316     {
12317       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12318       if (p->poc_decl != last_poc_decl)
12319         {
12320           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12321           last_poc_decl = p->poc_decl;
12322           if (poc_die)
12323             {
12324               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12325               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12326                                            "Caller DIE offset");
12327             }
12328         }
12329       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12330       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12331       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12332                                    "Callee DIE offset");
12333     }
12334 }
12335 \f
12336 /* Return the size of the .debug_vcall table for the compilation unit.  */
12337
12338 static unsigned long
12339 size_of_vcall_table (void)
12340 {
12341   unsigned long size;
12342   unsigned int i;
12343   vcall_entry *p;
12344
12345   /* Header:  version + pointer size.  */
12346   size = 2 + 1;
12347
12348   /* Each entry:  code label + vtable slot index.  */
12349   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12350     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12351
12352   return size;
12353 }
12354
12355 /* Output the virtual call table used to disambiguate PC values when
12356    identical function have been merged.  */
12357
12358 static void
12359 output_vcall_table (void)
12360 {
12361   unsigned i;
12362   unsigned long vcall_length = size_of_vcall_table ();
12363   vcall_entry *p;
12364   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12365
12366   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12367     dw2_asm_output_data (4, 0xffffffff,
12368       "Initial length escape value indicating 64-bit DWARF extension");
12369   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12370                        "Length of Virtual Call Table");
12371   dw2_asm_output_data (2, 4, "Version number");
12372   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12373
12374   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12375     {
12376       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12377       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12378       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12379     }
12380 }
12381 \f
12382 /* Given a pointer to a tree node for some base type, return a pointer to
12383    a DIE that describes the given type.
12384
12385    This routine must only be called for GCC type nodes that correspond to
12386    Dwarf base (fundamental) types.  */
12387
12388 static dw_die_ref
12389 base_type_die (tree type)
12390 {
12391   dw_die_ref base_type_result;
12392   enum dwarf_type encoding;
12393
12394   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12395     return 0;
12396
12397   /* If this is a subtype that should not be emitted as a subrange type,
12398      use the base type.  See subrange_type_for_debug_p.  */
12399   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12400     type = TREE_TYPE (type);
12401
12402   switch (TREE_CODE (type))
12403     {
12404     case INTEGER_TYPE:
12405       if ((dwarf_version >= 4 || !dwarf_strict)
12406           && TYPE_NAME (type)
12407           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12408           && DECL_IS_BUILTIN (TYPE_NAME (type))
12409           && DECL_NAME (TYPE_NAME (type)))
12410         {
12411           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12412           if (strcmp (name, "char16_t") == 0
12413               || strcmp (name, "char32_t") == 0)
12414             {
12415               encoding = DW_ATE_UTF;
12416               break;
12417             }
12418         }
12419       if (TYPE_STRING_FLAG (type))
12420         {
12421           if (TYPE_UNSIGNED (type))
12422             encoding = DW_ATE_unsigned_char;
12423           else
12424             encoding = DW_ATE_signed_char;
12425         }
12426       else if (TYPE_UNSIGNED (type))
12427         encoding = DW_ATE_unsigned;
12428       else
12429         encoding = DW_ATE_signed;
12430       break;
12431
12432     case REAL_TYPE:
12433       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12434         {
12435           if (dwarf_version >= 3 || !dwarf_strict)
12436             encoding = DW_ATE_decimal_float;
12437           else
12438             encoding = DW_ATE_lo_user;
12439         }
12440       else
12441         encoding = DW_ATE_float;
12442       break;
12443
12444     case FIXED_POINT_TYPE:
12445       if (!(dwarf_version >= 3 || !dwarf_strict))
12446         encoding = DW_ATE_lo_user;
12447       else if (TYPE_UNSIGNED (type))
12448         encoding = DW_ATE_unsigned_fixed;
12449       else
12450         encoding = DW_ATE_signed_fixed;
12451       break;
12452
12453       /* Dwarf2 doesn't know anything about complex ints, so use
12454          a user defined type for it.  */
12455     case COMPLEX_TYPE:
12456       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12457         encoding = DW_ATE_complex_float;
12458       else
12459         encoding = DW_ATE_lo_user;
12460       break;
12461
12462     case BOOLEAN_TYPE:
12463       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12464       encoding = DW_ATE_boolean;
12465       break;
12466
12467     default:
12468       /* No other TREE_CODEs are Dwarf fundamental types.  */
12469       gcc_unreachable ();
12470     }
12471
12472   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12473
12474   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12475                    int_size_in_bytes (type));
12476   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12477
12478   return base_type_result;
12479 }
12480
12481 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12482    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12483
12484 static inline int
12485 is_base_type (tree type)
12486 {
12487   switch (TREE_CODE (type))
12488     {
12489     case ERROR_MARK:
12490     case VOID_TYPE:
12491     case INTEGER_TYPE:
12492     case REAL_TYPE:
12493     case FIXED_POINT_TYPE:
12494     case COMPLEX_TYPE:
12495     case BOOLEAN_TYPE:
12496       return 1;
12497
12498     case ARRAY_TYPE:
12499     case RECORD_TYPE:
12500     case UNION_TYPE:
12501     case QUAL_UNION_TYPE:
12502     case ENUMERAL_TYPE:
12503     case FUNCTION_TYPE:
12504     case METHOD_TYPE:
12505     case POINTER_TYPE:
12506     case REFERENCE_TYPE:
12507     case OFFSET_TYPE:
12508     case LANG_TYPE:
12509     case VECTOR_TYPE:
12510       return 0;
12511
12512     default:
12513       gcc_unreachable ();
12514     }
12515
12516   return 0;
12517 }
12518
12519 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12520    node, return the size in bits for the type if it is a constant, or else
12521    return the alignment for the type if the type's size is not constant, or
12522    else return BITS_PER_WORD if the type actually turns out to be an
12523    ERROR_MARK node.  */
12524
12525 static inline unsigned HOST_WIDE_INT
12526 simple_type_size_in_bits (const_tree type)
12527 {
12528   if (TREE_CODE (type) == ERROR_MARK)
12529     return BITS_PER_WORD;
12530   else if (TYPE_SIZE (type) == NULL_TREE)
12531     return 0;
12532   else if (host_integerp (TYPE_SIZE (type), 1))
12533     return tree_low_cst (TYPE_SIZE (type), 1);
12534   else
12535     return TYPE_ALIGN (type);
12536 }
12537
12538 /* Similarly, but return a double_int instead of UHWI.  */
12539
12540 static inline double_int
12541 double_int_type_size_in_bits (const_tree type)
12542 {
12543   if (TREE_CODE (type) == ERROR_MARK)
12544     return uhwi_to_double_int (BITS_PER_WORD);
12545   else if (TYPE_SIZE (type) == NULL_TREE)
12546     return double_int_zero;
12547   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12548     return tree_to_double_int (TYPE_SIZE (type));
12549   else
12550     return uhwi_to_double_int (TYPE_ALIGN (type));
12551 }
12552
12553 /*  Given a pointer to a tree node for a subrange type, return a pointer
12554     to a DIE that describes the given type.  */
12555
12556 static dw_die_ref
12557 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12558 {
12559   dw_die_ref subrange_die;
12560   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12561
12562   if (context_die == NULL)
12563     context_die = comp_unit_die;
12564
12565   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12566
12567   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12568     {
12569       /* The size of the subrange type and its base type do not match,
12570          so we need to generate a size attribute for the subrange type.  */
12571       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12572     }
12573
12574   if (low)
12575     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12576   if (high)
12577     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12578
12579   return subrange_die;
12580 }
12581
12582 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12583    entry that chains various modifiers in front of the given type.  */
12584
12585 static dw_die_ref
12586 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12587                    dw_die_ref context_die)
12588 {
12589   enum tree_code code = TREE_CODE (type);
12590   dw_die_ref mod_type_die;
12591   dw_die_ref sub_die = NULL;
12592   tree item_type = NULL;
12593   tree qualified_type;
12594   tree name, low, high;
12595
12596   if (code == ERROR_MARK)
12597     return NULL;
12598
12599   /* See if we already have the appropriately qualified variant of
12600      this type.  */
12601   qualified_type
12602     = get_qualified_type (type,
12603                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12604                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12605
12606   if (qualified_type == sizetype
12607       && TYPE_NAME (qualified_type)
12608       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12609     {
12610 #ifdef ENABLE_CHECKING
12611       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12612                   == INTEGER_TYPE
12613                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12614                      == TYPE_PRECISION (qualified_type)
12615                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12616                      == TYPE_UNSIGNED (qualified_type));
12617 #endif
12618       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12619     }
12620
12621   /* If we do, then we can just use its DIE, if it exists.  */
12622   if (qualified_type)
12623     {
12624       mod_type_die = lookup_type_die (qualified_type);
12625       if (mod_type_die)
12626         return mod_type_die;
12627     }
12628
12629   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12630
12631   /* Handle C typedef types.  */
12632   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12633       && !DECL_ARTIFICIAL (name))
12634     {
12635       tree dtype = TREE_TYPE (name);
12636
12637       if (qualified_type == dtype)
12638         {
12639           /* For a named type, use the typedef.  */
12640           gen_type_die (qualified_type, context_die);
12641           return lookup_type_die (qualified_type);
12642         }
12643       else if (is_const_type < TYPE_READONLY (dtype)
12644                || is_volatile_type < TYPE_VOLATILE (dtype)
12645                || (is_const_type <= TYPE_READONLY (dtype)
12646                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12647                    && DECL_ORIGINAL_TYPE (name) != type))
12648         /* cv-unqualified version of named type.  Just use the unnamed
12649            type to which it refers.  */
12650         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12651                                   is_const_type, is_volatile_type,
12652                                   context_die);
12653       /* Else cv-qualified version of named type; fall through.  */
12654     }
12655
12656   if (is_const_type)
12657     {
12658       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12659       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12660     }
12661   else if (is_volatile_type)
12662     {
12663       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12664       sub_die = modified_type_die (type, 0, 0, context_die);
12665     }
12666   else if (code == POINTER_TYPE)
12667     {
12668       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12669       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12670                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12671       item_type = TREE_TYPE (type);
12672       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12673         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12674                          TYPE_ADDR_SPACE (item_type));
12675     }
12676   else if (code == REFERENCE_TYPE)
12677     {
12678       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12679         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12680                                 type);
12681       else
12682         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12683       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12684                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12685       item_type = TREE_TYPE (type);
12686       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12687         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12688                          TYPE_ADDR_SPACE (item_type));
12689     }
12690   else if (code == INTEGER_TYPE
12691            && TREE_TYPE (type) != NULL_TREE
12692            && subrange_type_for_debug_p (type, &low, &high))
12693     {
12694       mod_type_die = subrange_type_die (type, low, high, context_die);
12695       item_type = TREE_TYPE (type);
12696     }
12697   else if (is_base_type (type))
12698     mod_type_die = base_type_die (type);
12699   else
12700     {
12701       gen_type_die (type, context_die);
12702
12703       /* We have to get the type_main_variant here (and pass that to the
12704          `lookup_type_die' routine) because the ..._TYPE node we have
12705          might simply be a *copy* of some original type node (where the
12706          copy was created to help us keep track of typedef names) and
12707          that copy might have a different TYPE_UID from the original
12708          ..._TYPE node.  */
12709       if (TREE_CODE (type) != VECTOR_TYPE)
12710         return lookup_type_die (type_main_variant (type));
12711       else
12712         /* Vectors have the debugging information in the type,
12713            not the main variant.  */
12714         return lookup_type_die (type);
12715     }
12716
12717   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12718      don't output a DW_TAG_typedef, since there isn't one in the
12719      user's program; just attach a DW_AT_name to the type.
12720      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12721      if the base type already has the same name.  */
12722   if (name
12723       && ((TREE_CODE (name) != TYPE_DECL
12724            && (qualified_type == TYPE_MAIN_VARIANT (type)
12725                || (!is_const_type && !is_volatile_type)))
12726           || (TREE_CODE (name) == TYPE_DECL
12727               && TREE_TYPE (name) == qualified_type
12728               && DECL_NAME (name))))
12729     {
12730       if (TREE_CODE (name) == TYPE_DECL)
12731         /* Could just call add_name_and_src_coords_attributes here,
12732            but since this is a builtin type it doesn't have any
12733            useful source coordinates anyway.  */
12734         name = DECL_NAME (name);
12735       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12736     }
12737   /* This probably indicates a bug.  */
12738   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12739     add_name_attribute (mod_type_die, "__unknown__");
12740
12741   if (qualified_type)
12742     equate_type_number_to_die (qualified_type, mod_type_die);
12743
12744   if (item_type)
12745     /* We must do this after the equate_type_number_to_die call, in case
12746        this is a recursive type.  This ensures that the modified_type_die
12747        recursion will terminate even if the type is recursive.  Recursive
12748        types are possible in Ada.  */
12749     sub_die = modified_type_die (item_type,
12750                                  TYPE_READONLY (item_type),
12751                                  TYPE_VOLATILE (item_type),
12752                                  context_die);
12753
12754   if (sub_die != NULL)
12755     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12756
12757   return mod_type_die;
12758 }
12759
12760 /* Generate DIEs for the generic parameters of T.
12761    T must be either a generic type or a generic function.
12762    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12763
12764 static void
12765 gen_generic_params_dies (tree t)
12766 {
12767   tree parms, args;
12768   int parms_num, i;
12769   dw_die_ref die = NULL;
12770
12771   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12772     return;
12773
12774   if (TYPE_P (t))
12775     die = lookup_type_die (t);
12776   else if (DECL_P (t))
12777     die = lookup_decl_die (t);
12778
12779   gcc_assert (die);
12780
12781   parms = lang_hooks.get_innermost_generic_parms (t);
12782   if (!parms)
12783     /* T has no generic parameter. It means T is neither a generic type
12784        or function. End of story.  */
12785     return;
12786
12787   parms_num = TREE_VEC_LENGTH (parms);
12788   args = lang_hooks.get_innermost_generic_args (t);
12789   for (i = 0; i < parms_num; i++)
12790     {
12791       tree parm, arg, arg_pack_elems;
12792
12793       parm = TREE_VEC_ELT (parms, i);
12794       arg = TREE_VEC_ELT (args, i);
12795       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12796       gcc_assert (parm && TREE_VALUE (parm) && arg);
12797
12798       if (parm && TREE_VALUE (parm) && arg)
12799         {
12800           /* If PARM represents a template parameter pack,
12801              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12802              by DW_TAG_template_*_parameter DIEs for the argument
12803              pack elements of ARG. Note that ARG would then be
12804              an argument pack.  */
12805           if (arg_pack_elems)
12806             template_parameter_pack_die (TREE_VALUE (parm),
12807                                          arg_pack_elems,
12808                                          die);
12809           else
12810             generic_parameter_die (TREE_VALUE (parm), arg,
12811                                    true /* Emit DW_AT_name */, die);
12812         }
12813     }
12814 }
12815
12816 /* Create and return a DIE for PARM which should be
12817    the representation of a generic type parameter.
12818    For instance, in the C++ front end, PARM would be a template parameter.
12819    ARG is the argument to PARM.
12820    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12821    name of the PARM.
12822    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12823    as a child node.  */
12824
12825 static dw_die_ref
12826 generic_parameter_die (tree parm, tree arg,
12827                        bool emit_name_p,
12828                        dw_die_ref parent_die)
12829 {
12830   dw_die_ref tmpl_die = NULL;
12831   const char *name = NULL;
12832
12833   if (!parm || !DECL_NAME (parm) || !arg)
12834     return NULL;
12835
12836   /* We support non-type generic parameters and arguments,
12837      type generic parameters and arguments, as well as
12838      generic generic parameters (a.k.a. template template parameters in C++)
12839      and arguments.  */
12840   if (TREE_CODE (parm) == PARM_DECL)
12841     /* PARM is a nontype generic parameter  */
12842     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12843   else if (TREE_CODE (parm) == TYPE_DECL)
12844     /* PARM is a type generic parameter.  */
12845     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12846   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12847     /* PARM is a generic generic parameter.
12848        Its DIE is a GNU extension. It shall have a
12849        DW_AT_name attribute to represent the name of the template template
12850        parameter, and a DW_AT_GNU_template_name attribute to represent the
12851        name of the template template argument.  */
12852     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12853                         parent_die, parm);
12854   else
12855     gcc_unreachable ();
12856
12857   if (tmpl_die)
12858     {
12859       tree tmpl_type;
12860
12861       /* If PARM is a generic parameter pack, it means we are
12862          emitting debug info for a template argument pack element.
12863          In other terms, ARG is a template argument pack element.
12864          In that case, we don't emit any DW_AT_name attribute for
12865          the die.  */
12866       if (emit_name_p)
12867         {
12868           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12869           gcc_assert (name);
12870           add_AT_string (tmpl_die, DW_AT_name, name);
12871         }
12872
12873       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12874         {
12875           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12876              TMPL_DIE should have a child DW_AT_type attribute that is set
12877              to the type of the argument to PARM, which is ARG.
12878              If PARM is a type generic parameter, TMPL_DIE should have a
12879              child DW_AT_type that is set to ARG.  */
12880           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12881           add_type_attribute (tmpl_die, tmpl_type, 0,
12882                               TREE_THIS_VOLATILE (tmpl_type),
12883                               parent_die);
12884         }
12885       else
12886         {
12887           /* So TMPL_DIE is a DIE representing a
12888              a generic generic template parameter, a.k.a template template
12889              parameter in C++ and arg is a template.  */
12890
12891           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12892              to the name of the argument.  */
12893           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12894           if (name)
12895             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12896         }
12897
12898       if (TREE_CODE (parm) == PARM_DECL)
12899         /* So PARM is a non-type generic parameter.
12900            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12901            attribute of TMPL_DIE which value represents the value
12902            of ARG.
12903            We must be careful here:
12904            The value of ARG might reference some function decls.
12905            We might currently be emitting debug info for a generic
12906            type and types are emitted before function decls, we don't
12907            know if the function decls referenced by ARG will actually be
12908            emitted after cgraph computations.
12909            So must defer the generation of the DW_AT_const_value to
12910            after cgraph is ready.  */
12911         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12912     }
12913
12914   return tmpl_die;
12915 }
12916
12917 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12918    PARM_PACK must be a template parameter pack. The returned DIE
12919    will be child DIE of PARENT_DIE.  */
12920
12921 static dw_die_ref
12922 template_parameter_pack_die (tree parm_pack,
12923                              tree parm_pack_args,
12924                              dw_die_ref parent_die)
12925 {
12926   dw_die_ref die;
12927   int j;
12928
12929   gcc_assert (parent_die && parm_pack);
12930
12931   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12932   add_name_and_src_coords_attributes (die, parm_pack);
12933   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12934     generic_parameter_die (parm_pack,
12935                            TREE_VEC_ELT (parm_pack_args, j),
12936                            false /* Don't emit DW_AT_name */,
12937                            die);
12938   return die;
12939 }
12940
12941 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12942    an enumerated type.  */
12943
12944 static inline int
12945 type_is_enum (const_tree type)
12946 {
12947   return TREE_CODE (type) == ENUMERAL_TYPE;
12948 }
12949
12950 /* Return the DBX register number described by a given RTL node.  */
12951
12952 static unsigned int
12953 dbx_reg_number (const_rtx rtl)
12954 {
12955   unsigned regno = REGNO (rtl);
12956
12957   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12958
12959 #ifdef LEAF_REG_REMAP
12960   if (current_function_uses_only_leaf_regs)
12961     {
12962       int leaf_reg = LEAF_REG_REMAP (regno);
12963       if (leaf_reg != -1)
12964         regno = (unsigned) leaf_reg;
12965     }
12966 #endif
12967
12968   return DBX_REGISTER_NUMBER (regno);
12969 }
12970
12971 /* Optionally add a DW_OP_piece term to a location description expression.
12972    DW_OP_piece is only added if the location description expression already
12973    doesn't end with DW_OP_piece.  */
12974
12975 static void
12976 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12977 {
12978   dw_loc_descr_ref loc;
12979
12980   if (*list_head != NULL)
12981     {
12982       /* Find the end of the chain.  */
12983       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12984         ;
12985
12986       if (loc->dw_loc_opc != DW_OP_piece)
12987         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12988     }
12989 }
12990
12991 /* Return a location descriptor that designates a machine register or
12992    zero if there is none.  */
12993
12994 static dw_loc_descr_ref
12995 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12996 {
12997   rtx regs;
12998
12999   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13000     return 0;
13001
13002   regs = targetm.dwarf_register_span (rtl);
13003
13004   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13005     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13006   else
13007     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13008 }
13009
13010 /* Return a location descriptor that designates a machine register for
13011    a given hard register number.  */
13012
13013 static dw_loc_descr_ref
13014 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13015 {
13016   dw_loc_descr_ref reg_loc_descr;
13017
13018   if (regno <= 31)
13019     reg_loc_descr
13020       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13021   else
13022     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13023
13024   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13025     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13026
13027   return reg_loc_descr;
13028 }
13029
13030 /* Given an RTL of a register, return a location descriptor that
13031    designates a value that spans more than one register.  */
13032
13033 static dw_loc_descr_ref
13034 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13035                              enum var_init_status initialized)
13036 {
13037   int nregs, size, i;
13038   unsigned reg;
13039   dw_loc_descr_ref loc_result = NULL;
13040
13041   reg = REGNO (rtl);
13042 #ifdef LEAF_REG_REMAP
13043   if (current_function_uses_only_leaf_regs)
13044     {
13045       int leaf_reg = LEAF_REG_REMAP (reg);
13046       if (leaf_reg != -1)
13047         reg = (unsigned) leaf_reg;
13048     }
13049 #endif
13050   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13051   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13052
13053   /* Simple, contiguous registers.  */
13054   if (regs == NULL_RTX)
13055     {
13056       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13057
13058       loc_result = NULL;
13059       while (nregs--)
13060         {
13061           dw_loc_descr_ref t;
13062
13063           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13064                                       VAR_INIT_STATUS_INITIALIZED);
13065           add_loc_descr (&loc_result, t);
13066           add_loc_descr_op_piece (&loc_result, size);
13067           ++reg;
13068         }
13069       return loc_result;
13070     }
13071
13072   /* Now onto stupid register sets in non contiguous locations.  */
13073
13074   gcc_assert (GET_CODE (regs) == PARALLEL);
13075
13076   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13077   loc_result = NULL;
13078
13079   for (i = 0; i < XVECLEN (regs, 0); ++i)
13080     {
13081       dw_loc_descr_ref t;
13082
13083       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13084                                   VAR_INIT_STATUS_INITIALIZED);
13085       add_loc_descr (&loc_result, t);
13086       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13087       add_loc_descr_op_piece (&loc_result, size);
13088     }
13089
13090   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13091     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13092   return loc_result;
13093 }
13094
13095 #endif /* DWARF2_DEBUGGING_INFO */
13096
13097 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
13098
13099 /* Return a location descriptor that designates a constant.  */
13100
13101 static dw_loc_descr_ref
13102 int_loc_descriptor (HOST_WIDE_INT i)
13103 {
13104   enum dwarf_location_atom op;
13105
13106   /* Pick the smallest representation of a constant, rather than just
13107      defaulting to the LEB encoding.  */
13108   if (i >= 0)
13109     {
13110       if (i <= 31)
13111         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13112       else if (i <= 0xff)
13113         op = DW_OP_const1u;
13114       else if (i <= 0xffff)
13115         op = DW_OP_const2u;
13116       else if (HOST_BITS_PER_WIDE_INT == 32
13117                || i <= 0xffffffff)
13118         op = DW_OP_const4u;
13119       else
13120         op = DW_OP_constu;
13121     }
13122   else
13123     {
13124       if (i >= -0x80)
13125         op = DW_OP_const1s;
13126       else if (i >= -0x8000)
13127         op = DW_OP_const2s;
13128       else if (HOST_BITS_PER_WIDE_INT == 32
13129                || i >= -0x80000000)
13130         op = DW_OP_const4s;
13131       else
13132         op = DW_OP_consts;
13133     }
13134
13135   return new_loc_descr (op, i, 0);
13136 }
13137 #endif
13138
13139 #ifdef DWARF2_DEBUGGING_INFO
13140 /* Return loc description representing "address" of integer value.
13141    This can appear only as toplevel expression.  */
13142
13143 static dw_loc_descr_ref
13144 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13145 {
13146   int litsize;
13147   dw_loc_descr_ref loc_result = NULL;
13148
13149   if (!(dwarf_version >= 4 || !dwarf_strict))
13150     return NULL;
13151
13152   if (i >= 0)
13153     {
13154       if (i <= 31)
13155         litsize = 1;
13156       else if (i <= 0xff)
13157         litsize = 2;
13158       else if (i <= 0xffff)
13159         litsize = 3;
13160       else if (HOST_BITS_PER_WIDE_INT == 32
13161                || i <= 0xffffffff)
13162         litsize = 5;
13163       else
13164         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13165     }
13166   else
13167     {
13168       if (i >= -0x80)
13169         litsize = 2;
13170       else if (i >= -0x8000)
13171         litsize = 3;
13172       else if (HOST_BITS_PER_WIDE_INT == 32
13173                || i >= -0x80000000)
13174         litsize = 5;
13175       else
13176         litsize = 1 + size_of_sleb128 (i);
13177     }
13178   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13179      is more compact.  For DW_OP_stack_value we need:
13180      litsize + 1 (DW_OP_stack_value)
13181      and for DW_OP_implicit_value:
13182      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13183   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13184     {
13185       loc_result = int_loc_descriptor (i);
13186       add_loc_descr (&loc_result,
13187                      new_loc_descr (DW_OP_stack_value, 0, 0));
13188       return loc_result;
13189     }
13190
13191   loc_result = new_loc_descr (DW_OP_implicit_value,
13192                               size, 0);
13193   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13194   loc_result->dw_loc_oprnd2.v.val_int = i;
13195   return loc_result;
13196 }
13197
13198 /* Return a location descriptor that designates a base+offset location.  */
13199
13200 static dw_loc_descr_ref
13201 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13202                  enum var_init_status initialized)
13203 {
13204   unsigned int regno;
13205   dw_loc_descr_ref result;
13206   dw_fde_ref fde = current_fde ();
13207
13208   /* We only use "frame base" when we're sure we're talking about the
13209      post-prologue local stack frame.  We do this by *not* running
13210      register elimination until this point, and recognizing the special
13211      argument pointer and soft frame pointer rtx's.  */
13212   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13213     {
13214       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13215
13216       if (elim != reg)
13217         {
13218           if (GET_CODE (elim) == PLUS)
13219             {
13220               offset += INTVAL (XEXP (elim, 1));
13221               elim = XEXP (elim, 0);
13222             }
13223           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13224                        && (elim == hard_frame_pointer_rtx
13225                            || elim == stack_pointer_rtx))
13226                       || elim == (frame_pointer_needed
13227                                   ? hard_frame_pointer_rtx
13228                                   : stack_pointer_rtx));
13229
13230           /* If drap register is used to align stack, use frame
13231              pointer + offset to access stack variables.  If stack
13232              is aligned without drap, use stack pointer + offset to
13233              access stack variables.  */
13234           if (crtl->stack_realign_tried
13235               && reg == frame_pointer_rtx)
13236             {
13237               int base_reg
13238                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13239                                       ? HARD_FRAME_POINTER_REGNUM
13240                                       : STACK_POINTER_REGNUM);
13241               return new_reg_loc_descr (base_reg, offset);
13242             }
13243
13244           offset += frame_pointer_fb_offset;
13245           return new_loc_descr (DW_OP_fbreg, offset, 0);
13246         }
13247     }
13248   else if (!optimize
13249            && fde
13250            && (fde->drap_reg == REGNO (reg)
13251                || fde->vdrap_reg == REGNO (reg)))
13252     {
13253       /* Use cfa+offset to represent the location of arguments passed
13254          on the stack when drap is used to align stack.
13255          Only do this when not optimizing, for optimized code var-tracking
13256          is supposed to track where the arguments live and the register
13257          used as vdrap or drap in some spot might be used for something
13258          else in other part of the routine.  */
13259       return new_loc_descr (DW_OP_fbreg, offset, 0);
13260     }
13261
13262   regno = dbx_reg_number (reg);
13263   if (regno <= 31)
13264     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13265                             offset, 0);
13266   else
13267     result = new_loc_descr (DW_OP_bregx, regno, offset);
13268
13269   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13270     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13271
13272   return result;
13273 }
13274
13275 /* Return true if this RTL expression describes a base+offset calculation.  */
13276
13277 static inline int
13278 is_based_loc (const_rtx rtl)
13279 {
13280   return (GET_CODE (rtl) == PLUS
13281           && ((REG_P (XEXP (rtl, 0))
13282                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13283                && CONST_INT_P (XEXP (rtl, 1)))));
13284 }
13285
13286 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13287    failed.  */
13288
13289 static dw_loc_descr_ref
13290 tls_mem_loc_descriptor (rtx mem)
13291 {
13292   tree base;
13293   dw_loc_descr_ref loc_result;
13294
13295   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13296     return NULL;
13297
13298   base = get_base_address (MEM_EXPR (mem));
13299   if (base == NULL
13300       || TREE_CODE (base) != VAR_DECL
13301       || !DECL_THREAD_LOCAL_P (base))
13302     return NULL;
13303
13304   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13305   if (loc_result == NULL)
13306     return NULL;
13307
13308   if (INTVAL (MEM_OFFSET (mem)))
13309     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13310
13311   return loc_result;
13312 }
13313
13314 /* Output debug info about reason why we failed to expand expression as dwarf
13315    expression.  */
13316
13317 static void
13318 expansion_failed (tree expr, rtx rtl, char const *reason)
13319 {
13320   if (dump_file && (dump_flags & TDF_DETAILS))
13321     {
13322       fprintf (dump_file, "Failed to expand as dwarf: ");
13323       if (expr)
13324         print_generic_expr (dump_file, expr, dump_flags);
13325       if (rtl)
13326         {
13327           fprintf (dump_file, "\n");
13328           print_rtl (dump_file, rtl);
13329         }
13330       fprintf (dump_file, "\nReason: %s\n", reason);
13331     }
13332 }
13333
13334 /* Helper function for const_ok_for_output, called either directly
13335    or via for_each_rtx.  */
13336
13337 static int
13338 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13339 {
13340   rtx rtl = *rtlp;
13341
13342   if (GET_CODE (rtl) == UNSPEC)
13343     {
13344       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13345          we can't express it in the debug info.  */
13346 #ifdef ENABLE_CHECKING
13347       inform (current_function_decl
13348               ? DECL_SOURCE_LOCATION (current_function_decl)
13349               : UNKNOWN_LOCATION,
13350               "non-delegitimized UNSPEC %d found in variable location",
13351               XINT (rtl, 1));
13352 #endif
13353       expansion_failed (NULL_TREE, rtl,
13354                         "UNSPEC hasn't been delegitimized.\n");
13355       return 1;
13356     }
13357
13358   if (GET_CODE (rtl) != SYMBOL_REF)
13359     return 0;
13360
13361   if (CONSTANT_POOL_ADDRESS_P (rtl))
13362     {
13363       bool marked;
13364       get_pool_constant_mark (rtl, &marked);
13365       /* If all references to this pool constant were optimized away,
13366          it was not output and thus we can't represent it.  */
13367       if (!marked)
13368         {
13369           expansion_failed (NULL_TREE, rtl,
13370                             "Constant was removed from constant pool.\n");
13371           return 1;
13372         }
13373     }
13374
13375   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13376     return 1;
13377
13378   /* Avoid references to external symbols in debug info, on several targets
13379      the linker might even refuse to link when linking a shared library,
13380      and in many other cases the relocations for .debug_info/.debug_loc are
13381      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13382      to be defined within the same shared library or executable are fine.  */
13383   if (SYMBOL_REF_EXTERNAL_P (rtl))
13384     {
13385       tree decl = SYMBOL_REF_DECL (rtl);
13386
13387       if (decl == NULL || !targetm.binds_local_p (decl))
13388         {
13389           expansion_failed (NULL_TREE, rtl,
13390                             "Symbol not defined in current TU.\n");
13391           return 1;
13392         }
13393     }
13394
13395   return 0;
13396 }
13397
13398 /* Return true if constant RTL can be emitted in DW_OP_addr or
13399    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13400    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13401
13402 static bool
13403 const_ok_for_output (rtx rtl)
13404 {
13405   if (GET_CODE (rtl) == SYMBOL_REF)
13406     return const_ok_for_output_1 (&rtl, NULL) == 0;
13407
13408   if (GET_CODE (rtl) == CONST)
13409     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13410
13411   return true;
13412 }
13413
13414 /* The following routine converts the RTL for a variable or parameter
13415    (resident in memory) into an equivalent Dwarf representation of a
13416    mechanism for getting the address of that same variable onto the top of a
13417    hypothetical "address evaluation" stack.
13418
13419    When creating memory location descriptors, we are effectively transforming
13420    the RTL for a memory-resident object into its Dwarf postfix expression
13421    equivalent.  This routine recursively descends an RTL tree, turning
13422    it into Dwarf postfix code as it goes.
13423
13424    MODE is the mode of the memory reference, needed to handle some
13425    autoincrement addressing modes.
13426
13427    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13428    location list for RTL.
13429
13430    Return 0 if we can't represent the location.  */
13431
13432 static dw_loc_descr_ref
13433 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13434                     enum var_init_status initialized)
13435 {
13436   dw_loc_descr_ref mem_loc_result = NULL;
13437   enum dwarf_location_atom op;
13438   dw_loc_descr_ref op0, op1;
13439
13440   /* Note that for a dynamically sized array, the location we will generate a
13441      description of here will be the lowest numbered location which is
13442      actually within the array.  That's *not* necessarily the same as the
13443      zeroth element of the array.  */
13444
13445   rtl = targetm.delegitimize_address (rtl);
13446
13447   switch (GET_CODE (rtl))
13448     {
13449     case POST_INC:
13450     case POST_DEC:
13451     case POST_MODIFY:
13452       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13453
13454     case SUBREG:
13455       /* The case of a subreg may arise when we have a local (register)
13456          variable or a formal (register) parameter which doesn't quite fill
13457          up an entire register.  For now, just assume that it is
13458          legitimate to make the Dwarf info refer to the whole register which
13459          contains the given subreg.  */
13460       if (!subreg_lowpart_p (rtl))
13461         break;
13462       rtl = SUBREG_REG (rtl);
13463       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13464         break;
13465       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13466         break;
13467       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13468       break;
13469
13470     case REG:
13471       /* Whenever a register number forms a part of the description of the
13472          method for calculating the (dynamic) address of a memory resident
13473          object, DWARF rules require the register number be referred to as
13474          a "base register".  This distinction is not based in any way upon
13475          what category of register the hardware believes the given register
13476          belongs to.  This is strictly DWARF terminology we're dealing with
13477          here. Note that in cases where the location of a memory-resident
13478          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13479          OP_CONST (0)) the actual DWARF location descriptor that we generate
13480          may just be OP_BASEREG (basereg).  This may look deceptively like
13481          the object in question was allocated to a register (rather than in
13482          memory) so DWARF consumers need to be aware of the subtle
13483          distinction between OP_REG and OP_BASEREG.  */
13484       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13485         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13486       else if (stack_realign_drap
13487                && crtl->drap_reg
13488                && crtl->args.internal_arg_pointer == rtl
13489                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13490         {
13491           /* If RTL is internal_arg_pointer, which has been optimized
13492              out, use DRAP instead.  */
13493           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13494                                             VAR_INIT_STATUS_INITIALIZED);
13495         }
13496       break;
13497
13498     case SIGN_EXTEND:
13499     case ZERO_EXTEND:
13500       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13501                                 VAR_INIT_STATUS_INITIALIZED);
13502       if (op0 == 0)
13503         break;
13504       else
13505         {
13506           int shift = DWARF2_ADDR_SIZE
13507                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13508           shift *= BITS_PER_UNIT;
13509           if (GET_CODE (rtl) == SIGN_EXTEND)
13510             op = DW_OP_shra;
13511           else
13512             op = DW_OP_shr;
13513           mem_loc_result = op0;
13514           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13515           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13516           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13517           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13518         }
13519       break;
13520
13521     case MEM:
13522       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13523                                            VAR_INIT_STATUS_INITIALIZED);
13524       if (mem_loc_result == NULL)
13525         mem_loc_result = tls_mem_loc_descriptor (rtl);
13526       if (mem_loc_result != 0)
13527         {
13528           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13529             {
13530               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13531               return 0;
13532             }
13533           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13534             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13535           else
13536             add_loc_descr (&mem_loc_result,
13537                            new_loc_descr (DW_OP_deref_size,
13538                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13539         }
13540       else
13541         {
13542           rtx new_rtl = avoid_constant_pool_reference (rtl);
13543           if (new_rtl != rtl)
13544             return mem_loc_descriptor (new_rtl, mode, initialized);
13545         }
13546       break;
13547
13548     case LO_SUM:
13549          rtl = XEXP (rtl, 1);
13550
13551       /* ... fall through ...  */
13552
13553     case LABEL_REF:
13554       /* Some ports can transform a symbol ref into a label ref, because
13555          the symbol ref is too far away and has to be dumped into a constant
13556          pool.  */
13557     case CONST:
13558     case SYMBOL_REF:
13559       if (GET_CODE (rtl) == SYMBOL_REF
13560           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13561         {
13562           dw_loc_descr_ref temp;
13563
13564           /* If this is not defined, we have no way to emit the data.  */
13565           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13566             break;
13567
13568           temp = new_loc_descr (DW_OP_addr, 0, 0);
13569           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13570           temp->dw_loc_oprnd1.v.val_addr = rtl;
13571           temp->dtprel = true;
13572
13573           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13574           add_loc_descr (&mem_loc_result, temp);
13575
13576           break;
13577         }
13578
13579       if (!const_ok_for_output (rtl))
13580         break;
13581
13582     symref:
13583       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13584       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13585       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13586       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13587       break;
13588
13589     case CONCAT:
13590     case CONCATN:
13591     case VAR_LOCATION:
13592       expansion_failed (NULL_TREE, rtl,
13593                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13594       return 0;
13595
13596     case PRE_MODIFY:
13597       /* Extract the PLUS expression nested inside and fall into
13598          PLUS code below.  */
13599       rtl = XEXP (rtl, 1);
13600       goto plus;
13601
13602     case PRE_INC:
13603     case PRE_DEC:
13604       /* Turn these into a PLUS expression and fall into the PLUS code
13605          below.  */
13606       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13607                           GEN_INT (GET_CODE (rtl) == PRE_INC
13608                                    ? GET_MODE_UNIT_SIZE (mode)
13609                                    : -GET_MODE_UNIT_SIZE (mode)));
13610
13611       /* ... fall through ...  */
13612
13613     case PLUS:
13614     plus:
13615       if (is_based_loc (rtl))
13616         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13617                                           INTVAL (XEXP (rtl, 1)),
13618                                           VAR_INIT_STATUS_INITIALIZED);
13619       else
13620         {
13621           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13622                                                VAR_INIT_STATUS_INITIALIZED);
13623           if (mem_loc_result == 0)
13624             break;
13625
13626           if (CONST_INT_P (XEXP (rtl, 1)))
13627             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13628           else
13629             {
13630               dw_loc_descr_ref mem_loc_result2
13631                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13632                                       VAR_INIT_STATUS_INITIALIZED);
13633               if (mem_loc_result2 == 0)
13634                 break;
13635               add_loc_descr (&mem_loc_result, mem_loc_result2);
13636               add_loc_descr (&mem_loc_result,
13637                              new_loc_descr (DW_OP_plus, 0, 0));
13638             }
13639         }
13640       break;
13641
13642     /* If a pseudo-reg is optimized away, it is possible for it to
13643        be replaced with a MEM containing a multiply or shift.  */
13644     case MINUS:
13645       op = DW_OP_minus;
13646       goto do_binop;
13647
13648     case MULT:
13649       op = DW_OP_mul;
13650       goto do_binop;
13651
13652     case DIV:
13653       op = DW_OP_div;
13654       goto do_binop;
13655
13656     case UMOD:
13657       op = DW_OP_mod;
13658       goto do_binop;
13659
13660     case ASHIFT:
13661       op = DW_OP_shl;
13662       goto do_binop;
13663
13664     case ASHIFTRT:
13665       op = DW_OP_shra;
13666       goto do_binop;
13667
13668     case LSHIFTRT:
13669       op = DW_OP_shr;
13670       goto do_binop;
13671
13672     case AND:
13673       op = DW_OP_and;
13674       goto do_binop;
13675
13676     case IOR:
13677       op = DW_OP_or;
13678       goto do_binop;
13679
13680     case XOR:
13681       op = DW_OP_xor;
13682       goto do_binop;
13683
13684     do_binop:
13685       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13686                                 VAR_INIT_STATUS_INITIALIZED);
13687       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13688                                 VAR_INIT_STATUS_INITIALIZED);
13689
13690       if (op0 == 0 || op1 == 0)
13691         break;
13692
13693       mem_loc_result = op0;
13694       add_loc_descr (&mem_loc_result, op1);
13695       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13696       break;
13697
13698     case MOD:
13699       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13700                                 VAR_INIT_STATUS_INITIALIZED);
13701       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13702                                 VAR_INIT_STATUS_INITIALIZED);
13703
13704       if (op0 == 0 || op1 == 0)
13705         break;
13706
13707       mem_loc_result = op0;
13708       add_loc_descr (&mem_loc_result, op1);
13709       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13710       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13711       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13712       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13713       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13714       break;
13715
13716     case NOT:
13717       op = DW_OP_not;
13718       goto do_unop;
13719
13720     case ABS:
13721       op = DW_OP_abs;
13722       goto do_unop;
13723
13724     case NEG:
13725       op = DW_OP_neg;
13726       goto do_unop;
13727
13728     do_unop:
13729       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13730                                 VAR_INIT_STATUS_INITIALIZED);
13731
13732       if (op0 == 0)
13733         break;
13734
13735       mem_loc_result = op0;
13736       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13737       break;
13738
13739     case CONST_INT:
13740       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13741       break;
13742
13743     case EQ:
13744       op = DW_OP_eq;
13745       goto do_scompare;
13746
13747     case GE:
13748       op = DW_OP_ge;
13749       goto do_scompare;
13750
13751     case GT:
13752       op = DW_OP_gt;
13753       goto do_scompare;
13754
13755     case LE:
13756       op = DW_OP_le;
13757       goto do_scompare;
13758
13759     case LT:
13760       op = DW_OP_lt;
13761       goto do_scompare;
13762
13763     case NE:
13764       op = DW_OP_ne;
13765       goto do_scompare;
13766
13767     do_scompare:
13768       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13769           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13770         break;
13771       else
13772         {
13773           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13774
13775           if (op_mode == VOIDmode)
13776             op_mode = GET_MODE (XEXP (rtl, 1));
13777           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13778             break;
13779
13780           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13781                                     VAR_INIT_STATUS_INITIALIZED);
13782           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13783                                     VAR_INIT_STATUS_INITIALIZED);
13784
13785           if (op0 == 0 || op1 == 0)
13786             break;
13787
13788           if (op_mode != VOIDmode
13789               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13790             {
13791               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13792               shift *= BITS_PER_UNIT;
13793               /* For eq/ne, if the operands are known to be zero-extended,
13794                  there is no need to do the fancy shifting up.  */
13795               if (op == DW_OP_eq || op == DW_OP_ne)
13796                 {
13797                   dw_loc_descr_ref last0, last1;
13798                   for (last0 = op0;
13799                        last0->dw_loc_next != NULL;
13800                        last0 = last0->dw_loc_next)
13801                     ;
13802                   for (last1 = op1;
13803                        last1->dw_loc_next != NULL;
13804                        last1 = last1->dw_loc_next)
13805                     ;
13806                   /* deref_size zero extends, and for constants we can check
13807                      whether they are zero extended or not.  */
13808                   if (((last0->dw_loc_opc == DW_OP_deref_size
13809                         && last0->dw_loc_oprnd1.v.val_int
13810                            <= GET_MODE_SIZE (op_mode))
13811                        || (CONST_INT_P (XEXP (rtl, 0))
13812                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13813                                == (INTVAL (XEXP (rtl, 0))
13814                                    & GET_MODE_MASK (op_mode))))
13815                       && ((last1->dw_loc_opc == DW_OP_deref_size
13816                            && last1->dw_loc_oprnd1.v.val_int
13817                               <= GET_MODE_SIZE (op_mode))
13818                           || (CONST_INT_P (XEXP (rtl, 1))
13819                               && (unsigned HOST_WIDE_INT)
13820                                  INTVAL (XEXP (rtl, 1))
13821                                  == (INTVAL (XEXP (rtl, 1))
13822                                      & GET_MODE_MASK (op_mode)))))
13823                     goto do_compare;
13824                 }
13825               add_loc_descr (&op0, int_loc_descriptor (shift));
13826               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13827               if (CONST_INT_P (XEXP (rtl, 1)))
13828                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13829               else
13830                 {
13831                   add_loc_descr (&op1, int_loc_descriptor (shift));
13832                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13833                 }
13834             }
13835         }
13836
13837     do_compare:
13838       mem_loc_result = op0;
13839       add_loc_descr (&mem_loc_result, op1);
13840       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13841       if (STORE_FLAG_VALUE != 1)
13842         {
13843           add_loc_descr (&mem_loc_result,
13844                          int_loc_descriptor (STORE_FLAG_VALUE));
13845           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13846         }
13847       break;
13848
13849     case GEU:
13850       op = DW_OP_ge;
13851       goto do_ucompare;
13852
13853     case GTU:
13854       op = DW_OP_gt;
13855       goto do_ucompare;
13856
13857     case LEU:
13858       op = DW_OP_le;
13859       goto do_ucompare;
13860
13861     case LTU:
13862       op = DW_OP_lt;
13863       goto do_ucompare;
13864
13865     do_ucompare:
13866       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13867           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13868         break;
13869       else
13870         {
13871           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13872
13873           if (op_mode == VOIDmode)
13874             op_mode = GET_MODE (XEXP (rtl, 1));
13875           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13876             break;
13877
13878           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13879                                     VAR_INIT_STATUS_INITIALIZED);
13880           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13881                                     VAR_INIT_STATUS_INITIALIZED);
13882
13883           if (op0 == 0 || op1 == 0)
13884             break;
13885
13886           if (op_mode != VOIDmode
13887               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13888             {
13889               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13890               dw_loc_descr_ref last0, last1;
13891               for (last0 = op0;
13892                    last0->dw_loc_next != NULL;
13893                    last0 = last0->dw_loc_next)
13894                 ;
13895               for (last1 = op1;
13896                    last1->dw_loc_next != NULL;
13897                    last1 = last1->dw_loc_next)
13898                 ;
13899               if (CONST_INT_P (XEXP (rtl, 0)))
13900                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13901               /* deref_size zero extends, so no need to mask it again.  */
13902               else if (last0->dw_loc_opc != DW_OP_deref_size
13903                        || last0->dw_loc_oprnd1.v.val_int
13904                           > GET_MODE_SIZE (op_mode))
13905                 {
13906                   add_loc_descr (&op0, int_loc_descriptor (mask));
13907                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13908                 }
13909               if (CONST_INT_P (XEXP (rtl, 1)))
13910                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13911               /* deref_size zero extends, so no need to mask it again.  */
13912               else if (last1->dw_loc_opc != DW_OP_deref_size
13913                        || last1->dw_loc_oprnd1.v.val_int
13914                           > GET_MODE_SIZE (op_mode))
13915                 {
13916                   add_loc_descr (&op1, int_loc_descriptor (mask));
13917                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13918                 }
13919             }
13920           else
13921             {
13922               HOST_WIDE_INT bias = 1;
13923               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13924               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13925               if (CONST_INT_P (XEXP (rtl, 1)))
13926                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13927                                           + INTVAL (XEXP (rtl, 1)));
13928               else
13929                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13930                                                     bias, 0));
13931             }
13932         }
13933       goto do_compare;
13934
13935     case SMIN:
13936     case SMAX:
13937     case UMIN:
13938     case UMAX:
13939       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13940           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13941           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13942         break;
13943
13944       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13945                                 VAR_INIT_STATUS_INITIALIZED);
13946       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13947                                 VAR_INIT_STATUS_INITIALIZED);
13948
13949       if (op0 == 0 || op1 == 0)
13950         break;
13951
13952       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13953       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13954       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13955       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13956         {
13957           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13958             {
13959               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13960               add_loc_descr (&op0, int_loc_descriptor (mask));
13961               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13962               add_loc_descr (&op1, int_loc_descriptor (mask));
13963               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13964             }
13965           else
13966             {
13967               HOST_WIDE_INT bias = 1;
13968               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13969               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13970               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13971             }
13972         }
13973       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13974         {
13975           int shift = DWARF2_ADDR_SIZE
13976                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13977           shift *= BITS_PER_UNIT;
13978           add_loc_descr (&op0, int_loc_descriptor (shift));
13979           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13980           add_loc_descr (&op1, int_loc_descriptor (shift));
13981           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13982         }
13983
13984       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13985         op = DW_OP_lt;
13986       else
13987         op = DW_OP_gt;
13988       mem_loc_result = op0;
13989       add_loc_descr (&mem_loc_result, op1);
13990       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13991       {
13992         dw_loc_descr_ref bra_node, drop_node;
13993
13994         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13995         add_loc_descr (&mem_loc_result, bra_node);
13996         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13997         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13998         add_loc_descr (&mem_loc_result, drop_node);
13999         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14000         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14001       }
14002       break;
14003
14004     case ZERO_EXTRACT:
14005     case SIGN_EXTRACT:
14006       if (CONST_INT_P (XEXP (rtl, 1))
14007           && CONST_INT_P (XEXP (rtl, 2))
14008           && ((unsigned) INTVAL (XEXP (rtl, 1))
14009               + (unsigned) INTVAL (XEXP (rtl, 2))
14010               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14011           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14012           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14013         {
14014           int shift, size;
14015           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14016                                     VAR_INIT_STATUS_INITIALIZED);
14017           if (op0 == 0)
14018             break;
14019           if (GET_CODE (rtl) == SIGN_EXTRACT)
14020             op = DW_OP_shra;
14021           else
14022             op = DW_OP_shr;
14023           mem_loc_result = op0;
14024           size = INTVAL (XEXP (rtl, 1));
14025           shift = INTVAL (XEXP (rtl, 2));
14026           if (BITS_BIG_ENDIAN)
14027             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14028                     - shift - size;
14029           if (shift + size != (int) DWARF2_ADDR_SIZE)
14030             {
14031               add_loc_descr (&mem_loc_result,
14032                              int_loc_descriptor (DWARF2_ADDR_SIZE
14033                                                  - shift - size));
14034               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14035             }
14036           if (size != (int) DWARF2_ADDR_SIZE)
14037             {
14038               add_loc_descr (&mem_loc_result,
14039                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14040               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14041             }
14042         }
14043       break;
14044
14045     case COMPARE:
14046     case IF_THEN_ELSE:
14047     case ROTATE:
14048     case ROTATERT:
14049     case TRUNCATE:
14050       /* In theory, we could implement the above.  */
14051       /* DWARF cannot represent the unsigned compare operations
14052          natively.  */
14053     case SS_MULT:
14054     case US_MULT:
14055     case SS_DIV:
14056     case US_DIV:
14057     case SS_PLUS:
14058     case US_PLUS:
14059     case SS_MINUS:
14060     case US_MINUS:
14061     case SS_NEG:
14062     case US_NEG:
14063     case SS_ABS:
14064     case SS_ASHIFT:
14065     case US_ASHIFT:
14066     case SS_TRUNCATE:
14067     case US_TRUNCATE:
14068     case UDIV:
14069     case UNORDERED:
14070     case ORDERED:
14071     case UNEQ:
14072     case UNGE:
14073     case UNGT:
14074     case UNLE:
14075     case UNLT:
14076     case LTGT:
14077     case FLOAT_EXTEND:
14078     case FLOAT_TRUNCATE:
14079     case FLOAT:
14080     case UNSIGNED_FLOAT:
14081     case FIX:
14082     case UNSIGNED_FIX:
14083     case FRACT_CONVERT:
14084     case UNSIGNED_FRACT_CONVERT:
14085     case SAT_FRACT:
14086     case UNSIGNED_SAT_FRACT:
14087     case SQRT:
14088     case BSWAP:
14089     case FFS:
14090     case CLZ:
14091     case CTZ:
14092     case POPCOUNT:
14093     case PARITY:
14094     case ASM_OPERANDS:
14095     case VEC_MERGE:
14096     case VEC_SELECT:
14097     case VEC_CONCAT:
14098     case VEC_DUPLICATE:
14099     case UNSPEC:
14100     case HIGH:
14101       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14102          can't express it in the debug info.  This can happen e.g. with some
14103          TLS UNSPECs.  */
14104       break;
14105
14106     case CONST_STRING:
14107       resolve_one_addr (&rtl, NULL);
14108       goto symref;
14109
14110     default:
14111 #ifdef ENABLE_CHECKING
14112       print_rtl (stderr, rtl);
14113       gcc_unreachable ();
14114 #else
14115       break;
14116 #endif
14117     }
14118
14119   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14120     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14121
14122   return mem_loc_result;
14123 }
14124
14125 /* Return a descriptor that describes the concatenation of two locations.
14126    This is typically a complex variable.  */
14127
14128 static dw_loc_descr_ref
14129 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14130 {
14131   dw_loc_descr_ref cc_loc_result = NULL;
14132   dw_loc_descr_ref x0_ref
14133     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14134   dw_loc_descr_ref x1_ref
14135     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14136
14137   if (x0_ref == 0 || x1_ref == 0)
14138     return 0;
14139
14140   cc_loc_result = x0_ref;
14141   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14142
14143   add_loc_descr (&cc_loc_result, x1_ref);
14144   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14145
14146   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14147     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14148
14149   return cc_loc_result;
14150 }
14151
14152 /* Return a descriptor that describes the concatenation of N
14153    locations.  */
14154
14155 static dw_loc_descr_ref
14156 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14157 {
14158   unsigned int i;
14159   dw_loc_descr_ref cc_loc_result = NULL;
14160   unsigned int n = XVECLEN (concatn, 0);
14161
14162   for (i = 0; i < n; ++i)
14163     {
14164       dw_loc_descr_ref ref;
14165       rtx x = XVECEXP (concatn, 0, i);
14166
14167       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14168       if (ref == NULL)
14169         return NULL;
14170
14171       add_loc_descr (&cc_loc_result, ref);
14172       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14173     }
14174
14175   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14176     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14177
14178   return cc_loc_result;
14179 }
14180
14181 /* Output a proper Dwarf location descriptor for a variable or parameter
14182    which is either allocated in a register or in a memory location.  For a
14183    register, we just generate an OP_REG and the register number.  For a
14184    memory location we provide a Dwarf postfix expression describing how to
14185    generate the (dynamic) address of the object onto the address stack.
14186
14187    MODE is mode of the decl if this loc_descriptor is going to be used in
14188    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14189    allowed, VOIDmode otherwise.
14190
14191    If we don't know how to describe it, return 0.  */
14192
14193 static dw_loc_descr_ref
14194 loc_descriptor (rtx rtl, enum machine_mode mode,
14195                 enum var_init_status initialized)
14196 {
14197   dw_loc_descr_ref loc_result = NULL;
14198
14199   switch (GET_CODE (rtl))
14200     {
14201     case SUBREG:
14202       /* The case of a subreg may arise when we have a local (register)
14203          variable or a formal (register) parameter which doesn't quite fill
14204          up an entire register.  For now, just assume that it is
14205          legitimate to make the Dwarf info refer to the whole register which
14206          contains the given subreg.  */
14207       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14208       break;
14209
14210     case REG:
14211       loc_result = reg_loc_descriptor (rtl, initialized);
14212       break;
14213
14214     case SIGN_EXTEND:
14215     case ZERO_EXTEND:
14216       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14217       break;
14218
14219     case MEM:
14220       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14221                                        initialized);
14222       if (loc_result == NULL)
14223         loc_result = tls_mem_loc_descriptor (rtl);
14224       if (loc_result == NULL)
14225         {
14226           rtx new_rtl = avoid_constant_pool_reference (rtl);
14227           if (new_rtl != rtl)
14228             loc_result = loc_descriptor (new_rtl, mode, initialized);
14229         }
14230       break;
14231
14232     case CONCAT:
14233       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14234                                           initialized);
14235       break;
14236
14237     case CONCATN:
14238       loc_result = concatn_loc_descriptor (rtl, initialized);
14239       break;
14240
14241     case VAR_LOCATION:
14242       /* Single part.  */
14243       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14244         {
14245           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14246           if (GET_CODE (loc) == EXPR_LIST)
14247             loc = XEXP (loc, 0);
14248           loc_result = loc_descriptor (loc, mode, initialized);
14249           break;
14250         }
14251
14252       rtl = XEXP (rtl, 1);
14253       /* FALLTHRU */
14254
14255     case PARALLEL:
14256       {
14257         rtvec par_elems = XVEC (rtl, 0);
14258         int num_elem = GET_NUM_ELEM (par_elems);
14259         enum machine_mode mode;
14260         int i;
14261
14262         /* Create the first one, so we have something to add to.  */
14263         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14264                                      VOIDmode, initialized);
14265         if (loc_result == NULL)
14266           return NULL;
14267         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14268         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14269         for (i = 1; i < num_elem; i++)
14270           {
14271             dw_loc_descr_ref temp;
14272
14273             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14274                                    VOIDmode, initialized);
14275             if (temp == NULL)
14276               return NULL;
14277             add_loc_descr (&loc_result, temp);
14278             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14279             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14280           }
14281       }
14282       break;
14283
14284     case CONST_INT:
14285       if (mode != VOIDmode && mode != BLKmode)
14286         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14287                                                     INTVAL (rtl));
14288       break;
14289
14290     case CONST_DOUBLE:
14291       if (mode == VOIDmode)
14292         mode = GET_MODE (rtl);
14293
14294       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14295         {
14296           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14297
14298           /* Note that a CONST_DOUBLE rtx could represent either an integer
14299              or a floating-point constant.  A CONST_DOUBLE is used whenever
14300              the constant requires more than one word in order to be
14301              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14302           loc_result = new_loc_descr (DW_OP_implicit_value,
14303                                       GET_MODE_SIZE (mode), 0);
14304           if (SCALAR_FLOAT_MODE_P (mode))
14305             {
14306               unsigned int length = GET_MODE_SIZE (mode);
14307               unsigned char *array
14308                   = (unsigned char*) ggc_alloc_atomic (length);
14309
14310               insert_float (rtl, array);
14311               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14312               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14313               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14314               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14315             }
14316           else
14317             {
14318               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14319               loc_result->dw_loc_oprnd2.v.val_double
14320                 = rtx_to_double_int (rtl);
14321             }
14322         }
14323       break;
14324
14325     case CONST_VECTOR:
14326       if (mode == VOIDmode)
14327         mode = GET_MODE (rtl);
14328
14329       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14330         {
14331           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14332           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14333           unsigned char *array = (unsigned char *)
14334             ggc_alloc_atomic (length * elt_size);
14335           unsigned int i;
14336           unsigned char *p;
14337
14338           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14339           switch (GET_MODE_CLASS (mode))
14340             {
14341             case MODE_VECTOR_INT:
14342               for (i = 0, p = array; i < length; i++, p += elt_size)
14343                 {
14344                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14345                   double_int val = rtx_to_double_int (elt);
14346
14347                   if (elt_size <= sizeof (HOST_WIDE_INT))
14348                     insert_int (double_int_to_shwi (val), elt_size, p);
14349                   else
14350                     {
14351                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14352                       insert_double (val, p);
14353                     }
14354                 }
14355               break;
14356
14357             case MODE_VECTOR_FLOAT:
14358               for (i = 0, p = array; i < length; i++, p += elt_size)
14359                 {
14360                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14361                   insert_float (elt, p);
14362                 }
14363               break;
14364
14365             default:
14366               gcc_unreachable ();
14367             }
14368
14369           loc_result = new_loc_descr (DW_OP_implicit_value,
14370                                       length * elt_size, 0);
14371           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14372           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14373           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14374           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14375         }
14376       break;
14377
14378     case CONST:
14379       if (mode == VOIDmode
14380           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14381           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14382           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14383         {
14384           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14385           break;
14386         }
14387       /* FALLTHROUGH */
14388     case SYMBOL_REF:
14389       if (!const_ok_for_output (rtl))
14390         break;
14391     case LABEL_REF:
14392       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14393           && (dwarf_version >= 4 || !dwarf_strict))
14394         {
14395           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14396           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14397           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14398           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14399           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14400         }
14401       break;
14402
14403     default:
14404       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14405           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14406           && (dwarf_version >= 4 || !dwarf_strict))
14407         {
14408           /* Value expression.  */
14409           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14410           if (loc_result)
14411             add_loc_descr (&loc_result,
14412                            new_loc_descr (DW_OP_stack_value, 0, 0));
14413         }
14414       break;
14415     }
14416
14417   return loc_result;
14418 }
14419
14420 /* We need to figure out what section we should use as the base for the
14421    address ranges where a given location is valid.
14422    1. If this particular DECL has a section associated with it, use that.
14423    2. If this function has a section associated with it, use that.
14424    3. Otherwise, use the text section.
14425    XXX: If you split a variable across multiple sections, we won't notice.  */
14426
14427 static const char *
14428 secname_for_decl (const_tree decl)
14429 {
14430   const char *secname;
14431
14432   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14433     {
14434       tree sectree = DECL_SECTION_NAME (decl);
14435       secname = TREE_STRING_POINTER (sectree);
14436     }
14437   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14438     {
14439       tree sectree = DECL_SECTION_NAME (current_function_decl);
14440       secname = TREE_STRING_POINTER (sectree);
14441     }
14442   else if (cfun && in_cold_section_p)
14443     secname = crtl->subsections.cold_section_label;
14444   else
14445     secname = text_section_label;
14446
14447   return secname;
14448 }
14449
14450 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14451
14452 static bool
14453 decl_by_reference_p (tree decl)
14454 {
14455   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14456            || TREE_CODE (decl) == VAR_DECL)
14457           && DECL_BY_REFERENCE (decl));
14458 }
14459
14460 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14461    for VARLOC.  */
14462
14463 static dw_loc_descr_ref
14464 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14465                enum var_init_status initialized)
14466 {
14467   int have_address = 0;
14468   dw_loc_descr_ref descr;
14469   enum machine_mode mode;
14470
14471   if (want_address != 2)
14472     {
14473       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14474       /* Single part.  */
14475       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14476         {
14477           varloc = PAT_VAR_LOCATION_LOC (varloc);
14478           if (GET_CODE (varloc) == EXPR_LIST)
14479             varloc = XEXP (varloc, 0);
14480           mode = GET_MODE (varloc);
14481           if (MEM_P (varloc))
14482             {
14483               rtx addr = XEXP (varloc, 0);
14484               descr = mem_loc_descriptor (addr, mode, initialized);
14485               if (descr)
14486                 have_address = 1;
14487               else
14488                 {
14489                   rtx x = avoid_constant_pool_reference (varloc);
14490                   if (x != varloc)
14491                     descr = mem_loc_descriptor (x, mode, initialized);
14492                 }
14493             }
14494           else
14495             descr = mem_loc_descriptor (varloc, mode, initialized);
14496         }
14497       else
14498         return 0;
14499     }
14500   else
14501     {
14502       if (GET_CODE (varloc) == VAR_LOCATION)
14503         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14504       else
14505         mode = DECL_MODE (loc);
14506       descr = loc_descriptor (varloc, mode, initialized);
14507       have_address = 1;
14508     }
14509
14510   if (!descr)
14511     return 0;
14512
14513   if (want_address == 2 && !have_address
14514       && (dwarf_version >= 4 || !dwarf_strict))
14515     {
14516       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14517         {
14518           expansion_failed (loc, NULL_RTX,
14519                             "DWARF address size mismatch");
14520           return 0;
14521         }
14522       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14523       have_address = 1;
14524     }
14525   /* Show if we can't fill the request for an address.  */
14526   if (want_address && !have_address)
14527     {
14528       expansion_failed (loc, NULL_RTX,
14529                         "Want address and only have value");
14530       return 0;
14531     }
14532
14533   /* If we've got an address and don't want one, dereference.  */
14534   if (!want_address && have_address)
14535     {
14536       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14537       enum dwarf_location_atom op;
14538
14539       if (size > DWARF2_ADDR_SIZE || size == -1)
14540         {
14541           expansion_failed (loc, NULL_RTX,
14542                             "DWARF address size mismatch");
14543           return 0;
14544         }
14545       else if (size == DWARF2_ADDR_SIZE)
14546         op = DW_OP_deref;
14547       else
14548         op = DW_OP_deref_size;
14549
14550       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14551     }
14552
14553   return descr;
14554 }
14555
14556 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14557    if it is not possible.  */
14558
14559 static dw_loc_descr_ref
14560 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14561 {
14562   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14563     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14564   else if (dwarf_version >= 3 || !dwarf_strict)
14565     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14566   else
14567     return NULL;
14568 }
14569
14570 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14571    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14572
14573 static dw_loc_descr_ref
14574 dw_sra_loc_expr (tree decl, rtx loc)
14575 {
14576   rtx p;
14577   unsigned int padsize = 0;
14578   dw_loc_descr_ref descr, *descr_tail;
14579   unsigned HOST_WIDE_INT decl_size;
14580   rtx varloc;
14581   enum var_init_status initialized;
14582
14583   if (DECL_SIZE (decl) == NULL
14584       || !host_integerp (DECL_SIZE (decl), 1))
14585     return NULL;
14586
14587   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14588   descr = NULL;
14589   descr_tail = &descr;
14590
14591   for (p = loc; p; p = XEXP (p, 1))
14592     {
14593       unsigned int bitsize = decl_piece_bitsize (p);
14594       rtx loc_note = *decl_piece_varloc_ptr (p);
14595       dw_loc_descr_ref cur_descr;
14596       dw_loc_descr_ref *tail, last = NULL;
14597       unsigned int opsize = 0;
14598
14599       if (loc_note == NULL_RTX
14600           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14601         {
14602           padsize += bitsize;
14603           continue;
14604         }
14605       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14606       varloc = NOTE_VAR_LOCATION (loc_note);
14607       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14608       if (cur_descr == NULL)
14609         {
14610           padsize += bitsize;
14611           continue;
14612         }
14613
14614       /* Check that cur_descr either doesn't use
14615          DW_OP_*piece operations, or their sum is equal
14616          to bitsize.  Otherwise we can't embed it.  */
14617       for (tail = &cur_descr; *tail != NULL;
14618            tail = &(*tail)->dw_loc_next)
14619         if ((*tail)->dw_loc_opc == DW_OP_piece)
14620           {
14621             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14622                       * BITS_PER_UNIT;
14623             last = *tail;
14624           }
14625         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14626           {
14627             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14628             last = *tail;
14629           }
14630
14631       if (last != NULL && opsize != bitsize)
14632         {
14633           padsize += bitsize;
14634           continue;
14635         }
14636
14637       /* If there is a hole, add DW_OP_*piece after empty DWARF
14638          expression, which means that those bits are optimized out.  */
14639       if (padsize)
14640         {
14641           if (padsize > decl_size)
14642             return NULL;
14643           decl_size -= padsize;
14644           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14645           if (*descr_tail == NULL)
14646             return NULL;
14647           descr_tail = &(*descr_tail)->dw_loc_next;
14648           padsize = 0;
14649         }
14650       *descr_tail = cur_descr;
14651       descr_tail = tail;
14652       if (bitsize > decl_size)
14653         return NULL;
14654       decl_size -= bitsize;
14655       if (last == NULL)
14656         {
14657           HOST_WIDE_INT offset = 0;
14658           if (GET_CODE (varloc) == VAR_LOCATION
14659               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14660             {
14661               varloc = PAT_VAR_LOCATION_LOC (varloc);
14662               if (GET_CODE (varloc) == EXPR_LIST)
14663                 varloc = XEXP (varloc, 0);
14664             }
14665           do 
14666             {
14667               if (GET_CODE (varloc) == CONST
14668                   || GET_CODE (varloc) == SIGN_EXTEND
14669                   || GET_CODE (varloc) == ZERO_EXTEND)
14670                 varloc = XEXP (varloc, 0);
14671               else if (GET_CODE (varloc) == SUBREG)
14672                 varloc = SUBREG_REG (varloc);
14673               else
14674                 break;
14675             }
14676           while (1);
14677           /* DW_OP_bit_size offset should be zero for register
14678              or implicit location descriptions and empty location
14679              descriptions, but for memory addresses needs big endian
14680              adjustment.  */
14681           if (MEM_P (varloc))
14682             {
14683               unsigned HOST_WIDE_INT memsize
14684                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14685               if (memsize != bitsize)
14686                 {
14687                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14688                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14689                     return NULL;
14690                   if (memsize < bitsize)
14691                     return NULL;
14692                   if (BITS_BIG_ENDIAN)
14693                     offset = memsize - bitsize;
14694                 }
14695             }
14696
14697           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14698           if (*descr_tail == NULL)
14699             return NULL;
14700           descr_tail = &(*descr_tail)->dw_loc_next;
14701         }
14702     }
14703
14704   /* If there were any non-empty expressions, add padding till the end of
14705      the decl.  */
14706   if (descr != NULL && decl_size != 0)
14707     {
14708       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14709       if (*descr_tail == NULL)
14710         return NULL;
14711     }
14712   return descr;
14713 }
14714
14715 /* Return the dwarf representation of the location list LOC_LIST of
14716    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14717    function.  */
14718
14719 static dw_loc_list_ref
14720 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14721 {
14722   const char *endname, *secname;
14723   rtx varloc;
14724   enum var_init_status initialized;
14725   struct var_loc_node *node;
14726   dw_loc_descr_ref descr;
14727   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14728   dw_loc_list_ref list = NULL;
14729   dw_loc_list_ref *listp = &list;
14730
14731   /* Now that we know what section we are using for a base,
14732      actually construct the list of locations.
14733      The first location information is what is passed to the
14734      function that creates the location list, and the remaining
14735      locations just get added on to that list.
14736      Note that we only know the start address for a location
14737      (IE location changes), so to build the range, we use
14738      the range [current location start, next location start].
14739      This means we have to special case the last node, and generate
14740      a range of [last location start, end of function label].  */
14741
14742   secname = secname_for_decl (decl);
14743
14744   for (node = loc_list->first; node; node = node->next)
14745     if (GET_CODE (node->loc) == EXPR_LIST
14746         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14747       {
14748         if (GET_CODE (node->loc) == EXPR_LIST)
14749           {
14750             /* This requires DW_OP_{,bit_}piece, which is not usable
14751                inside DWARF expressions.  */
14752             if (want_address != 2)
14753               continue;
14754             descr = dw_sra_loc_expr (decl, node->loc);
14755             if (descr == NULL)
14756               continue;
14757           }
14758         else
14759           {
14760             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14761             varloc = NOTE_VAR_LOCATION (node->loc);
14762             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14763           }
14764         if (descr)
14765           {
14766             /* The variable has a location between NODE->LABEL and
14767                NODE->NEXT->LABEL.  */
14768             if (node->next)
14769               endname = node->next->label;
14770             /* If the variable has a location at the last label
14771                it keeps its location until the end of function.  */
14772             else if (!current_function_decl)
14773               endname = text_end_label;
14774             else
14775               {
14776                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14777                                              current_function_funcdef_no);
14778                 endname = ggc_strdup (label_id);
14779               }
14780
14781             *listp = new_loc_list (descr, node->label, endname, secname);
14782             listp = &(*listp)->dw_loc_next;
14783           }
14784       }
14785
14786   /* Try to avoid the overhead of a location list emitting a location
14787      expression instead, but only if we didn't have more than one
14788      location entry in the first place.  If some entries were not
14789      representable, we don't want to pretend a single entry that was
14790      applies to the entire scope in which the variable is
14791      available.  */
14792   if (list && loc_list->first->next)
14793     gen_llsym (list);
14794
14795   return list;
14796 }
14797
14798 /* Return if the loc_list has only single element and thus can be represented
14799    as location description.   */
14800
14801 static bool
14802 single_element_loc_list_p (dw_loc_list_ref list)
14803 {
14804   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14805   return !list->ll_symbol;
14806 }
14807
14808 /* To each location in list LIST add loc descr REF.  */
14809
14810 static void
14811 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14812 {
14813   dw_loc_descr_ref copy;
14814   add_loc_descr (&list->expr, ref);
14815   list = list->dw_loc_next;
14816   while (list)
14817     {
14818       copy = ggc_alloc_dw_loc_descr_node ();
14819       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14820       add_loc_descr (&list->expr, copy);
14821       while (copy->dw_loc_next)
14822         {
14823           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14824           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14825           copy->dw_loc_next = new_copy;
14826           copy = new_copy;
14827         }
14828       list = list->dw_loc_next;
14829     }
14830 }
14831
14832 /* Given two lists RET and LIST
14833    produce location list that is result of adding expression in LIST
14834    to expression in RET on each possition in program.
14835    Might be destructive on both RET and LIST.
14836
14837    TODO: We handle only simple cases of RET or LIST having at most one
14838    element. General case would inolve sorting the lists in program order
14839    and merging them that will need some additional work.
14840    Adding that will improve quality of debug info especially for SRA-ed
14841    structures.  */
14842
14843 static void
14844 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14845 {
14846   if (!list)
14847     return;
14848   if (!*ret)
14849     {
14850       *ret = list;
14851       return;
14852     }
14853   if (!list->dw_loc_next)
14854     {
14855       add_loc_descr_to_each (*ret, list->expr);
14856       return;
14857     }
14858   if (!(*ret)->dw_loc_next)
14859     {
14860       add_loc_descr_to_each (list, (*ret)->expr);
14861       *ret = list;
14862       return;
14863     }
14864   expansion_failed (NULL_TREE, NULL_RTX,
14865                     "Don't know how to merge two non-trivial"
14866                     " location lists.\n");
14867   *ret = NULL;
14868   return;
14869 }
14870
14871 /* LOC is constant expression.  Try a luck, look it up in constant
14872    pool and return its loc_descr of its address.  */
14873
14874 static dw_loc_descr_ref
14875 cst_pool_loc_descr (tree loc)
14876 {
14877   /* Get an RTL for this, if something has been emitted.  */
14878   rtx rtl = lookup_constant_def (loc);
14879   enum machine_mode mode;
14880
14881   if (!rtl || !MEM_P (rtl))
14882     {
14883       gcc_assert (!rtl);
14884       return 0;
14885     }
14886   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14887
14888   /* TODO: We might get more coverage if we was actually delaying expansion
14889      of all expressions till end of compilation when constant pools are fully
14890      populated.  */
14891   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14892     {
14893       expansion_failed (loc, NULL_RTX,
14894                         "CST value in contant pool but not marked.");
14895       return 0;
14896     }
14897   mode = GET_MODE (rtl);
14898   rtl = XEXP (rtl, 0);
14899   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14900 }
14901
14902 /* Return dw_loc_list representing address of addr_expr LOC
14903    by looking for innder INDIRECT_REF expression and turing it
14904    into simple arithmetics.  */
14905
14906 static dw_loc_list_ref
14907 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14908 {
14909   tree obj, offset;
14910   HOST_WIDE_INT bitsize, bitpos, bytepos;
14911   enum machine_mode mode;
14912   int volatilep;
14913   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14914   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14915
14916   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14917                              &bitsize, &bitpos, &offset, &mode,
14918                              &unsignedp, &volatilep, false);
14919   STRIP_NOPS (obj);
14920   if (bitpos % BITS_PER_UNIT)
14921     {
14922       expansion_failed (loc, NULL_RTX, "bitfield access");
14923       return 0;
14924     }
14925   if (!INDIRECT_REF_P (obj))
14926     {
14927       expansion_failed (obj,
14928                         NULL_RTX, "no indirect ref in inner refrence");
14929       return 0;
14930     }
14931   if (!offset && !bitpos)
14932     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14933   else if (toplev
14934            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14935            && (dwarf_version >= 4 || !dwarf_strict))
14936     {
14937       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14938       if (!list_ret)
14939         return 0;
14940       if (offset)
14941         {
14942           /* Variable offset.  */
14943           list_ret1 = loc_list_from_tree (offset, 0);
14944           if (list_ret1 == 0)
14945             return 0;
14946           add_loc_list (&list_ret, list_ret1);
14947           if (!list_ret)
14948             return 0;
14949           add_loc_descr_to_each (list_ret,
14950                                  new_loc_descr (DW_OP_plus, 0, 0));
14951         }
14952       bytepos = bitpos / BITS_PER_UNIT;
14953       if (bytepos > 0)
14954         add_loc_descr_to_each (list_ret,
14955                                new_loc_descr (DW_OP_plus_uconst,
14956                                               bytepos, 0));
14957       else if (bytepos < 0)
14958         loc_list_plus_const (list_ret, bytepos);
14959       add_loc_descr_to_each (list_ret,
14960                              new_loc_descr (DW_OP_stack_value, 0, 0));
14961     }
14962   return list_ret;
14963 }
14964
14965
14966 /* Generate Dwarf location list representing LOC.
14967    If WANT_ADDRESS is false, expression computing LOC will be computed
14968    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14969    if WANT_ADDRESS is 2, expression computing address useable in location
14970      will be returned (i.e. DW_OP_reg can be used
14971      to refer to register values).  */
14972
14973 static dw_loc_list_ref
14974 loc_list_from_tree (tree loc, int want_address)
14975 {
14976   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14977   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14978   int have_address = 0;
14979   enum dwarf_location_atom op;
14980
14981   /* ??? Most of the time we do not take proper care for sign/zero
14982      extending the values properly.  Hopefully this won't be a real
14983      problem...  */
14984
14985   switch (TREE_CODE (loc))
14986     {
14987     case ERROR_MARK:
14988       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14989       return 0;
14990
14991     case PLACEHOLDER_EXPR:
14992       /* This case involves extracting fields from an object to determine the
14993          position of other fields.  We don't try to encode this here.  The
14994          only user of this is Ada, which encodes the needed information using
14995          the names of types.  */
14996       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14997       return 0;
14998
14999     case CALL_EXPR:
15000       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15001       /* There are no opcodes for these operations.  */
15002       return 0;
15003
15004     case PREINCREMENT_EXPR:
15005     case PREDECREMENT_EXPR:
15006     case POSTINCREMENT_EXPR:
15007     case POSTDECREMENT_EXPR:
15008       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15009       /* There are no opcodes for these operations.  */
15010       return 0;
15011
15012     case ADDR_EXPR:
15013       /* If we already want an address, see if there is INDIRECT_REF inside
15014          e.g. for &this->field.  */
15015       if (want_address)
15016         {
15017           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15018                        (loc, want_address == 2);
15019           if (list_ret)
15020             have_address = 1;
15021           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15022                    && (ret = cst_pool_loc_descr (loc)))
15023             have_address = 1;
15024         }
15025         /* Otherwise, process the argument and look for the address.  */
15026       if (!list_ret && !ret)
15027         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15028       else
15029         {
15030           if (want_address)
15031             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15032           return NULL;
15033         }
15034       break;
15035
15036     case VAR_DECL:
15037       if (DECL_THREAD_LOCAL_P (loc))
15038         {
15039           rtx rtl;
15040           enum dwarf_location_atom first_op;
15041           enum dwarf_location_atom second_op;
15042           bool dtprel = false;
15043
15044           if (targetm.have_tls)
15045             {
15046               /* If this is not defined, we have no way to emit the
15047                  data.  */
15048               if (!targetm.asm_out.output_dwarf_dtprel)
15049                 return 0;
15050
15051                /* The way DW_OP_GNU_push_tls_address is specified, we
15052                   can only look up addresses of objects in the current
15053                   module.  */
15054               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15055                 return 0;
15056               first_op = DW_OP_addr;
15057               dtprel = true;
15058               second_op = DW_OP_GNU_push_tls_address;
15059             }
15060           else
15061             {
15062               if (!targetm.emutls.debug_form_tls_address
15063                   || !(dwarf_version >= 3 || !dwarf_strict))
15064                 return 0;
15065               loc = emutls_decl (loc);
15066               first_op = DW_OP_addr;
15067               second_op = DW_OP_form_tls_address;
15068             }
15069
15070           rtl = rtl_for_decl_location (loc);
15071           if (rtl == NULL_RTX)
15072             return 0;
15073
15074           if (!MEM_P (rtl))
15075             return 0;
15076           rtl = XEXP (rtl, 0);
15077           if (! CONSTANT_P (rtl))
15078             return 0;
15079
15080           ret = new_loc_descr (first_op, 0, 0);
15081           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15082           ret->dw_loc_oprnd1.v.val_addr = rtl;
15083           ret->dtprel = dtprel;
15084
15085           ret1 = new_loc_descr (second_op, 0, 0);
15086           add_loc_descr (&ret, ret1);
15087
15088           have_address = 1;
15089           break;
15090         }
15091       /* FALLTHRU */
15092
15093     case PARM_DECL:
15094       if (DECL_HAS_VALUE_EXPR_P (loc))
15095         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15096                                    want_address);
15097       /* FALLTHRU */
15098
15099     case RESULT_DECL:
15100     case FUNCTION_DECL:
15101       {
15102         rtx rtl;
15103         var_loc_list *loc_list = lookup_decl_loc (loc);
15104
15105         if (loc_list && loc_list->first)
15106           {
15107             list_ret = dw_loc_list (loc_list, loc, want_address);
15108             have_address = want_address != 0;
15109             break;
15110           }
15111         rtl = rtl_for_decl_location (loc);
15112         if (rtl == NULL_RTX)
15113           {
15114             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15115             return 0;
15116           }
15117         else if (CONST_INT_P (rtl))
15118           {
15119             HOST_WIDE_INT val = INTVAL (rtl);
15120             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15121               val &= GET_MODE_MASK (DECL_MODE (loc));
15122             ret = int_loc_descriptor (val);
15123           }
15124         else if (GET_CODE (rtl) == CONST_STRING)
15125           {
15126             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15127             return 0;
15128           }
15129         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15130           {
15131             ret = new_loc_descr (DW_OP_addr, 0, 0);
15132             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15133             ret->dw_loc_oprnd1.v.val_addr = rtl;
15134           }
15135         else
15136           {
15137             enum machine_mode mode;
15138
15139             /* Certain constructs can only be represented at top-level.  */
15140             if (want_address == 2)
15141               {
15142                 ret = loc_descriptor (rtl, VOIDmode,
15143                                       VAR_INIT_STATUS_INITIALIZED);
15144                 have_address = 1;
15145               }
15146             else
15147               {
15148                 mode = GET_MODE (rtl);
15149                 if (MEM_P (rtl))
15150                   {
15151                     rtl = XEXP (rtl, 0);
15152                     have_address = 1;
15153                   }
15154                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15155               }
15156             if (!ret)
15157               expansion_failed (loc, rtl,
15158                                 "failed to produce loc descriptor for rtl");
15159           }
15160       }
15161       break;
15162
15163     case MEM_REF:
15164       /* ??? FIXME.  */
15165       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15166         return 0;
15167       /* Fallthru.  */
15168     case INDIRECT_REF:
15169     case ALIGN_INDIRECT_REF:
15170     case MISALIGNED_INDIRECT_REF:
15171       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15172       have_address = 1;
15173       break;
15174
15175     case COMPOUND_EXPR:
15176       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15177
15178     CASE_CONVERT:
15179     case VIEW_CONVERT_EXPR:
15180     case SAVE_EXPR:
15181     case MODIFY_EXPR:
15182       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15183
15184     case COMPONENT_REF:
15185     case BIT_FIELD_REF:
15186     case ARRAY_REF:
15187     case ARRAY_RANGE_REF:
15188     case REALPART_EXPR:
15189     case IMAGPART_EXPR:
15190       {
15191         tree obj, offset;
15192         HOST_WIDE_INT bitsize, bitpos, bytepos;
15193         enum machine_mode mode;
15194         int volatilep;
15195         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15196
15197         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15198                                    &unsignedp, &volatilep, false);
15199
15200         gcc_assert (obj != loc);
15201
15202         list_ret = loc_list_from_tree (obj,
15203                                        want_address == 2
15204                                        && !bitpos && !offset ? 2 : 1);
15205         /* TODO: We can extract value of the small expression via shifting even
15206            for nonzero bitpos.  */
15207         if (list_ret == 0)
15208           return 0;
15209         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15210           {
15211             expansion_failed (loc, NULL_RTX,
15212                               "bitfield access");
15213             return 0;
15214           }
15215
15216         if (offset != NULL_TREE)
15217           {
15218             /* Variable offset.  */
15219             list_ret1 = loc_list_from_tree (offset, 0);
15220             if (list_ret1 == 0)
15221               return 0;
15222             add_loc_list (&list_ret, list_ret1);
15223             if (!list_ret)
15224               return 0;
15225             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15226           }
15227
15228         bytepos = bitpos / BITS_PER_UNIT;
15229         if (bytepos > 0)
15230           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15231         else if (bytepos < 0)
15232           loc_list_plus_const (list_ret, bytepos);
15233
15234         have_address = 1;
15235         break;
15236       }
15237
15238     case INTEGER_CST:
15239       if ((want_address || !host_integerp (loc, 0))
15240           && (ret = cst_pool_loc_descr (loc)))
15241         have_address = 1;
15242       else if (want_address == 2
15243                && host_integerp (loc, 0)
15244                && (ret = address_of_int_loc_descriptor
15245                            (int_size_in_bytes (TREE_TYPE (loc)),
15246                             tree_low_cst (loc, 0))))
15247         have_address = 1;
15248       else if (host_integerp (loc, 0))
15249         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15250       else
15251         {
15252           expansion_failed (loc, NULL_RTX,
15253                             "Integer operand is not host integer");
15254           return 0;
15255         }
15256       break;
15257
15258     case CONSTRUCTOR:
15259     case REAL_CST:
15260     case STRING_CST:
15261     case COMPLEX_CST:
15262       if ((ret = cst_pool_loc_descr (loc)))
15263         have_address = 1;
15264       else
15265       /* We can construct small constants here using int_loc_descriptor.  */
15266         expansion_failed (loc, NULL_RTX,
15267                           "constructor or constant not in constant pool");
15268       break;
15269
15270     case TRUTH_AND_EXPR:
15271     case TRUTH_ANDIF_EXPR:
15272     case BIT_AND_EXPR:
15273       op = DW_OP_and;
15274       goto do_binop;
15275
15276     case TRUTH_XOR_EXPR:
15277     case BIT_XOR_EXPR:
15278       op = DW_OP_xor;
15279       goto do_binop;
15280
15281     case TRUTH_OR_EXPR:
15282     case TRUTH_ORIF_EXPR:
15283     case BIT_IOR_EXPR:
15284       op = DW_OP_or;
15285       goto do_binop;
15286
15287     case FLOOR_DIV_EXPR:
15288     case CEIL_DIV_EXPR:
15289     case ROUND_DIV_EXPR:
15290     case TRUNC_DIV_EXPR:
15291       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15292         return 0;
15293       op = DW_OP_div;
15294       goto do_binop;
15295
15296     case MINUS_EXPR:
15297       op = DW_OP_minus;
15298       goto do_binop;
15299
15300     case FLOOR_MOD_EXPR:
15301     case CEIL_MOD_EXPR:
15302     case ROUND_MOD_EXPR:
15303     case TRUNC_MOD_EXPR:
15304       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15305         {
15306           op = DW_OP_mod;
15307           goto do_binop;
15308         }
15309       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15310       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15311       if (list_ret == 0 || list_ret1 == 0)
15312         return 0;
15313
15314       add_loc_list (&list_ret, list_ret1);
15315       if (list_ret == 0)
15316         return 0;
15317       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15318       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15319       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15320       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15321       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15322       break;
15323
15324     case MULT_EXPR:
15325       op = DW_OP_mul;
15326       goto do_binop;
15327
15328     case LSHIFT_EXPR:
15329       op = DW_OP_shl;
15330       goto do_binop;
15331
15332     case RSHIFT_EXPR:
15333       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15334       goto do_binop;
15335
15336     case POINTER_PLUS_EXPR:
15337     case PLUS_EXPR:
15338       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15339         {
15340           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15341           if (list_ret == 0)
15342             return 0;
15343
15344           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15345           break;
15346         }
15347
15348       op = DW_OP_plus;
15349       goto do_binop;
15350
15351     case LE_EXPR:
15352       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15353         return 0;
15354
15355       op = DW_OP_le;
15356       goto do_binop;
15357
15358     case GE_EXPR:
15359       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15360         return 0;
15361
15362       op = DW_OP_ge;
15363       goto do_binop;
15364
15365     case LT_EXPR:
15366       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15367         return 0;
15368
15369       op = DW_OP_lt;
15370       goto do_binop;
15371
15372     case GT_EXPR:
15373       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15374         return 0;
15375
15376       op = DW_OP_gt;
15377       goto do_binop;
15378
15379     case EQ_EXPR:
15380       op = DW_OP_eq;
15381       goto do_binop;
15382
15383     case NE_EXPR:
15384       op = DW_OP_ne;
15385       goto do_binop;
15386
15387     do_binop:
15388       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15389       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15390       if (list_ret == 0 || list_ret1 == 0)
15391         return 0;
15392
15393       add_loc_list (&list_ret, list_ret1);
15394       if (list_ret == 0)
15395         return 0;
15396       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15397       break;
15398
15399     case TRUTH_NOT_EXPR:
15400     case BIT_NOT_EXPR:
15401       op = DW_OP_not;
15402       goto do_unop;
15403
15404     case ABS_EXPR:
15405       op = DW_OP_abs;
15406       goto do_unop;
15407
15408     case NEGATE_EXPR:
15409       op = DW_OP_neg;
15410       goto do_unop;
15411
15412     do_unop:
15413       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15414       if (list_ret == 0)
15415         return 0;
15416
15417       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15418       break;
15419
15420     case MIN_EXPR:
15421     case MAX_EXPR:
15422       {
15423         const enum tree_code code =
15424           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15425
15426         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15427                       build2 (code, integer_type_node,
15428                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15429                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15430       }
15431
15432       /* ... fall through ...  */
15433
15434     case COND_EXPR:
15435       {
15436         dw_loc_descr_ref lhs
15437           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15438         dw_loc_list_ref rhs
15439           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15440         dw_loc_descr_ref bra_node, jump_node, tmp;
15441
15442         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15443         if (list_ret == 0 || lhs == 0 || rhs == 0)
15444           return 0;
15445
15446         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15447         add_loc_descr_to_each (list_ret, bra_node);
15448
15449         add_loc_list (&list_ret, rhs);
15450         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15451         add_loc_descr_to_each (list_ret, jump_node);
15452
15453         add_loc_descr_to_each (list_ret, lhs);
15454         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15455         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15456
15457         /* ??? Need a node to point the skip at.  Use a nop.  */
15458         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15459         add_loc_descr_to_each (list_ret, tmp);
15460         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15461         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15462       }
15463       break;
15464
15465     case FIX_TRUNC_EXPR:
15466       return 0;
15467
15468     default:
15469       /* Leave front-end specific codes as simply unknown.  This comes
15470          up, for instance, with the C STMT_EXPR.  */
15471       if ((unsigned int) TREE_CODE (loc)
15472           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15473         {
15474           expansion_failed (loc, NULL_RTX,
15475                             "language specific tree node");
15476           return 0;
15477         }
15478
15479 #ifdef ENABLE_CHECKING
15480       /* Otherwise this is a generic code; we should just lists all of
15481          these explicitly.  We forgot one.  */
15482       gcc_unreachable ();
15483 #else
15484       /* In a release build, we want to degrade gracefully: better to
15485          generate incomplete debugging information than to crash.  */
15486       return NULL;
15487 #endif
15488     }
15489
15490   if (!ret && !list_ret)
15491     return 0;
15492
15493   if (want_address == 2 && !have_address
15494       && (dwarf_version >= 4 || !dwarf_strict))
15495     {
15496       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15497         {
15498           expansion_failed (loc, NULL_RTX,
15499                             "DWARF address size mismatch");
15500           return 0;
15501         }
15502       if (ret)
15503         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15504       else
15505         add_loc_descr_to_each (list_ret,
15506                                new_loc_descr (DW_OP_stack_value, 0, 0));
15507       have_address = 1;
15508     }
15509   /* Show if we can't fill the request for an address.  */
15510   if (want_address && !have_address)
15511     {
15512       expansion_failed (loc, NULL_RTX,
15513                         "Want address and only have value");
15514       return 0;
15515     }
15516
15517   gcc_assert (!ret || !list_ret);
15518
15519   /* If we've got an address and don't want one, dereference.  */
15520   if (!want_address && have_address)
15521     {
15522       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15523
15524       if (size > DWARF2_ADDR_SIZE || size == -1)
15525         {
15526           expansion_failed (loc, NULL_RTX,
15527                             "DWARF address size mismatch");
15528           return 0;
15529         }
15530       else if (size == DWARF2_ADDR_SIZE)
15531         op = DW_OP_deref;
15532       else
15533         op = DW_OP_deref_size;
15534
15535       if (ret)
15536         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15537       else
15538         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15539     }
15540   if (ret)
15541     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15542
15543   return list_ret;
15544 }
15545
15546 /* Same as above but return only single location expression.  */
15547 static dw_loc_descr_ref
15548 loc_descriptor_from_tree (tree loc, int want_address)
15549 {
15550   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15551   if (!ret)
15552     return NULL;
15553   if (ret->dw_loc_next)
15554     {
15555       expansion_failed (loc, NULL_RTX,
15556                         "Location list where only loc descriptor needed");
15557       return NULL;
15558     }
15559   return ret->expr;
15560 }
15561
15562 /* Given a value, round it up to the lowest multiple of `boundary'
15563    which is not less than the value itself.  */
15564
15565 static inline HOST_WIDE_INT
15566 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15567 {
15568   return (((value + boundary - 1) / boundary) * boundary);
15569 }
15570
15571 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15572    pointer to the declared type for the relevant field variable, or return
15573    `integer_type_node' if the given node turns out to be an
15574    ERROR_MARK node.  */
15575
15576 static inline tree
15577 field_type (const_tree decl)
15578 {
15579   tree type;
15580
15581   if (TREE_CODE (decl) == ERROR_MARK)
15582     return integer_type_node;
15583
15584   type = DECL_BIT_FIELD_TYPE (decl);
15585   if (type == NULL_TREE)
15586     type = TREE_TYPE (decl);
15587
15588   return type;
15589 }
15590
15591 /* Given a pointer to a tree node, return the alignment in bits for
15592    it, or else return BITS_PER_WORD if the node actually turns out to
15593    be an ERROR_MARK node.  */
15594
15595 static inline unsigned
15596 simple_type_align_in_bits (const_tree type)
15597 {
15598   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15599 }
15600
15601 static inline unsigned
15602 simple_decl_align_in_bits (const_tree decl)
15603 {
15604   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15605 }
15606
15607 /* Return the result of rounding T up to ALIGN.  */
15608
15609 static inline double_int
15610 round_up_to_align (double_int t, unsigned int align)
15611 {
15612   double_int alignd = uhwi_to_double_int (align);
15613   t = double_int_add (t, alignd);
15614   t = double_int_add (t, double_int_minus_one);
15615   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15616   t = double_int_mul (t, alignd);
15617   return t;
15618 }
15619
15620 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15621    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15622    or return 0 if we are unable to determine what that offset is, either
15623    because the argument turns out to be a pointer to an ERROR_MARK node, or
15624    because the offset is actually variable.  (We can't handle the latter case
15625    just yet).  */
15626
15627 static HOST_WIDE_INT
15628 field_byte_offset (const_tree decl)
15629 {
15630   double_int object_offset_in_bits;
15631   double_int object_offset_in_bytes;
15632   double_int bitpos_int;
15633
15634   if (TREE_CODE (decl) == ERROR_MARK)
15635     return 0;
15636
15637   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15638
15639   /* We cannot yet cope with fields whose positions are variable, so
15640      for now, when we see such things, we simply return 0.  Someday, we may
15641      be able to handle such cases, but it will be damn difficult.  */
15642   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15643     return 0;
15644
15645   bitpos_int = tree_to_double_int (bit_position (decl));
15646
15647 #ifdef PCC_BITFIELD_TYPE_MATTERS
15648   if (PCC_BITFIELD_TYPE_MATTERS)
15649     {
15650       tree type;
15651       tree field_size_tree;
15652       double_int deepest_bitpos;
15653       double_int field_size_in_bits;
15654       unsigned int type_align_in_bits;
15655       unsigned int decl_align_in_bits;
15656       double_int type_size_in_bits;
15657
15658       type = field_type (decl);
15659       type_size_in_bits = double_int_type_size_in_bits (type);
15660       type_align_in_bits = simple_type_align_in_bits (type);
15661
15662       field_size_tree = DECL_SIZE (decl);
15663
15664       /* The size could be unspecified if there was an error, or for
15665          a flexible array member.  */
15666       if (!field_size_tree)
15667         field_size_tree = bitsize_zero_node;
15668
15669       /* If the size of the field is not constant, use the type size.  */
15670       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15671         field_size_in_bits = tree_to_double_int (field_size_tree);
15672       else
15673         field_size_in_bits = type_size_in_bits;
15674
15675       decl_align_in_bits = simple_decl_align_in_bits (decl);
15676
15677       /* The GCC front-end doesn't make any attempt to keep track of the
15678          starting bit offset (relative to the start of the containing
15679          structure type) of the hypothetical "containing object" for a
15680          bit-field.  Thus, when computing the byte offset value for the
15681          start of the "containing object" of a bit-field, we must deduce
15682          this information on our own. This can be rather tricky to do in
15683          some cases.  For example, handling the following structure type
15684          definition when compiling for an i386/i486 target (which only
15685          aligns long long's to 32-bit boundaries) can be very tricky:
15686
15687          struct S { int field1; long long field2:31; };
15688
15689          Fortunately, there is a simple rule-of-thumb which can be used
15690          in such cases.  When compiling for an i386/i486, GCC will
15691          allocate 8 bytes for the structure shown above.  It decides to
15692          do this based upon one simple rule for bit-field allocation.
15693          GCC allocates each "containing object" for each bit-field at
15694          the first (i.e. lowest addressed) legitimate alignment boundary
15695          (based upon the required minimum alignment for the declared
15696          type of the field) which it can possibly use, subject to the
15697          condition that there is still enough available space remaining
15698          in the containing object (when allocated at the selected point)
15699          to fully accommodate all of the bits of the bit-field itself.
15700
15701          This simple rule makes it obvious why GCC allocates 8 bytes for
15702          each object of the structure type shown above.  When looking
15703          for a place to allocate the "containing object" for `field2',
15704          the compiler simply tries to allocate a 64-bit "containing
15705          object" at each successive 32-bit boundary (starting at zero)
15706          until it finds a place to allocate that 64- bit field such that
15707          at least 31 contiguous (and previously unallocated) bits remain
15708          within that selected 64 bit field.  (As it turns out, for the
15709          example above, the compiler finds it is OK to allocate the
15710          "containing object" 64-bit field at bit-offset zero within the
15711          structure type.)
15712
15713          Here we attempt to work backwards from the limited set of facts
15714          we're given, and we try to deduce from those facts, where GCC
15715          must have believed that the containing object started (within
15716          the structure type). The value we deduce is then used (by the
15717          callers of this routine) to generate DW_AT_location and
15718          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15719          the case of DW_AT_location, regular fields as well).  */
15720
15721       /* Figure out the bit-distance from the start of the structure to
15722          the "deepest" bit of the bit-field.  */
15723       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15724
15725       /* This is the tricky part.  Use some fancy footwork to deduce
15726          where the lowest addressed bit of the containing object must
15727          be.  */
15728       object_offset_in_bits
15729         = double_int_add (deepest_bitpos, double_int_neg (type_size_in_bits));
15730
15731       /* Round up to type_align by default.  This works best for
15732          bitfields.  */
15733       object_offset_in_bits
15734         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15735
15736       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15737         {
15738           object_offset_in_bits
15739             = double_int_add (deepest_bitpos,
15740                               double_int_neg (type_size_in_bits));
15741
15742           /* Round up to decl_align instead.  */
15743           object_offset_in_bits
15744             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15745         }
15746     }
15747   else
15748 #endif
15749     object_offset_in_bits = bitpos_int;
15750
15751   object_offset_in_bytes
15752     = double_int_div (object_offset_in_bits,
15753                       uhwi_to_double_int (BITS_PER_UNIT), true,
15754                       TRUNC_DIV_EXPR);
15755   return double_int_to_shwi (object_offset_in_bytes);
15756 }
15757 \f
15758 /* The following routines define various Dwarf attributes and any data
15759    associated with them.  */
15760
15761 /* Add a location description attribute value to a DIE.
15762
15763    This emits location attributes suitable for whole variables and
15764    whole parameters.  Note that the location attributes for struct fields are
15765    generated by the routine `data_member_location_attribute' below.  */
15766
15767 static inline void
15768 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15769                              dw_loc_list_ref descr)
15770 {
15771   if (descr == 0)
15772     return;
15773   if (single_element_loc_list_p (descr))
15774     add_AT_loc (die, attr_kind, descr->expr);
15775   else
15776     add_AT_loc_list (die, attr_kind, descr);
15777 }
15778
15779 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15780
15781 static void
15782 add_accessibility_attribute (dw_die_ref die, tree decl)
15783 {
15784   if (TREE_PROTECTED (decl))
15785     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15786   else if (TREE_PRIVATE (decl))
15787     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15788 }
15789
15790 /* Attach the specialized form of location attribute used for data members of
15791    struct and union types.  In the special case of a FIELD_DECL node which
15792    represents a bit-field, the "offset" part of this special location
15793    descriptor must indicate the distance in bytes from the lowest-addressed
15794    byte of the containing struct or union type to the lowest-addressed byte of
15795    the "containing object" for the bit-field.  (See the `field_byte_offset'
15796    function above).
15797
15798    For any given bit-field, the "containing object" is a hypothetical object
15799    (of some integral or enum type) within which the given bit-field lives.  The
15800    type of this hypothetical "containing object" is always the same as the
15801    declared type of the individual bit-field itself (for GCC anyway... the
15802    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15803    bytes) of the hypothetical "containing object" which will be given in the
15804    DW_AT_byte_size attribute for this bit-field.  (See the
15805    `byte_size_attribute' function below.)  It is also used when calculating the
15806    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15807    function below.)  */
15808
15809 static void
15810 add_data_member_location_attribute (dw_die_ref die, tree decl)
15811 {
15812   HOST_WIDE_INT offset;
15813   dw_loc_descr_ref loc_descr = 0;
15814
15815   if (TREE_CODE (decl) == TREE_BINFO)
15816     {
15817       /* We're working on the TAG_inheritance for a base class.  */
15818       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15819         {
15820           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15821              aren't at a fixed offset from all (sub)objects of the same
15822              type.  We need to extract the appropriate offset from our
15823              vtable.  The following dwarf expression means
15824
15825                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15826
15827              This is specific to the V3 ABI, of course.  */
15828
15829           dw_loc_descr_ref tmp;
15830
15831           /* Make a copy of the object address.  */
15832           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15833           add_loc_descr (&loc_descr, tmp);
15834
15835           /* Extract the vtable address.  */
15836           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15837           add_loc_descr (&loc_descr, tmp);
15838
15839           /* Calculate the address of the offset.  */
15840           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15841           gcc_assert (offset < 0);
15842
15843           tmp = int_loc_descriptor (-offset);
15844           add_loc_descr (&loc_descr, tmp);
15845           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15846           add_loc_descr (&loc_descr, tmp);
15847
15848           /* Extract the offset.  */
15849           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15850           add_loc_descr (&loc_descr, tmp);
15851
15852           /* Add it to the object address.  */
15853           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15854           add_loc_descr (&loc_descr, tmp);
15855         }
15856       else
15857         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15858     }
15859   else
15860     offset = field_byte_offset (decl);
15861
15862   if (! loc_descr)
15863     {
15864       if (dwarf_version > 2)
15865         {
15866           /* Don't need to output a location expression, just the constant. */
15867           add_AT_int (die, DW_AT_data_member_location, offset);
15868           return;
15869         }
15870       else
15871         {
15872           enum dwarf_location_atom op;
15873
15874           /* The DWARF2 standard says that we should assume that the structure
15875              address is already on the stack, so we can specify a structure
15876              field address by using DW_OP_plus_uconst.  */
15877
15878 #ifdef MIPS_DEBUGGING_INFO
15879           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15880              operator correctly.  It works only if we leave the offset on the
15881              stack.  */
15882           op = DW_OP_constu;
15883 #else
15884           op = DW_OP_plus_uconst;
15885 #endif
15886
15887           loc_descr = new_loc_descr (op, offset, 0);
15888         }
15889     }
15890
15891   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15892 }
15893
15894 /* Writes integer values to dw_vec_const array.  */
15895
15896 static void
15897 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15898 {
15899   while (size != 0)
15900     {
15901       *dest++ = val & 0xff;
15902       val >>= 8;
15903       --size;
15904     }
15905 }
15906
15907 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15908
15909 static HOST_WIDE_INT
15910 extract_int (const unsigned char *src, unsigned int size)
15911 {
15912   HOST_WIDE_INT val = 0;
15913
15914   src += size;
15915   while (size != 0)
15916     {
15917       val <<= 8;
15918       val |= *--src & 0xff;
15919       --size;
15920     }
15921   return val;
15922 }
15923
15924 /* Writes double_int values to dw_vec_const array.  */
15925
15926 static void
15927 insert_double (double_int val, unsigned char *dest)
15928 {
15929   unsigned char *p0 = dest;
15930   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15931
15932   if (WORDS_BIG_ENDIAN)
15933     {
15934       p0 = p1;
15935       p1 = dest;
15936     }
15937
15938   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15939   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15940 }
15941
15942 /* Writes floating point values to dw_vec_const array.  */
15943
15944 static void
15945 insert_float (const_rtx rtl, unsigned char *array)
15946 {
15947   REAL_VALUE_TYPE rv;
15948   long val[4];
15949   int i;
15950
15951   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15952   real_to_target (val, &rv, GET_MODE (rtl));
15953
15954   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15955   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15956     {
15957       insert_int (val[i], 4, array);
15958       array += 4;
15959     }
15960 }
15961
15962 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15963    does not have a "location" either in memory or in a register.  These
15964    things can arise in GNU C when a constant is passed as an actual parameter
15965    to an inlined function.  They can also arise in C++ where declared
15966    constants do not necessarily get memory "homes".  */
15967
15968 static bool
15969 add_const_value_attribute (dw_die_ref die, rtx rtl)
15970 {
15971   switch (GET_CODE (rtl))
15972     {
15973     case CONST_INT:
15974       {
15975         HOST_WIDE_INT val = INTVAL (rtl);
15976
15977         if (val < 0)
15978           add_AT_int (die, DW_AT_const_value, val);
15979         else
15980           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15981       }
15982       return true;
15983
15984     case CONST_DOUBLE:
15985       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15986          floating-point constant.  A CONST_DOUBLE is used whenever the
15987          constant requires more than one word in order to be adequately
15988          represented.  */
15989       {
15990         enum machine_mode mode = GET_MODE (rtl);
15991
15992         if (SCALAR_FLOAT_MODE_P (mode))
15993           {
15994             unsigned int length = GET_MODE_SIZE (mode);
15995             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15996
15997             insert_float (rtl, array);
15998             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15999           }
16000         else
16001           add_AT_double (die, DW_AT_const_value,
16002                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16003       }
16004       return true;
16005
16006     case CONST_VECTOR:
16007       {
16008         enum machine_mode mode = GET_MODE (rtl);
16009         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16010         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16011         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16012           (length * elt_size);
16013         unsigned int i;
16014         unsigned char *p;
16015
16016         switch (GET_MODE_CLASS (mode))
16017           {
16018           case MODE_VECTOR_INT:
16019             for (i = 0, p = array; i < length; i++, p += elt_size)
16020               {
16021                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16022                 double_int val = rtx_to_double_int (elt);
16023
16024                 if (elt_size <= sizeof (HOST_WIDE_INT))
16025                   insert_int (double_int_to_shwi (val), elt_size, p);
16026                 else
16027                   {
16028                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16029                     insert_double (val, p);
16030                   }
16031               }
16032             break;
16033
16034           case MODE_VECTOR_FLOAT:
16035             for (i = 0, p = array; i < length; i++, p += elt_size)
16036               {
16037                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16038                 insert_float (elt, p);
16039               }
16040             break;
16041
16042           default:
16043             gcc_unreachable ();
16044           }
16045
16046         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16047       }
16048       return true;
16049
16050     case CONST_STRING:
16051       if (dwarf_version >= 4 || !dwarf_strict)
16052         {
16053           dw_loc_descr_ref loc_result;
16054           resolve_one_addr (&rtl, NULL);
16055         rtl_addr:
16056           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16057           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16058           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16059           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16060           add_AT_loc (die, DW_AT_location, loc_result);
16061           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16062           return true;
16063         }
16064       return false;
16065
16066     case CONST:
16067       if (CONSTANT_P (XEXP (rtl, 0)))
16068         return add_const_value_attribute (die, XEXP (rtl, 0));
16069       /* FALLTHROUGH */
16070     case SYMBOL_REF:
16071       if (!const_ok_for_output (rtl))
16072         return false;
16073     case LABEL_REF:
16074       if (dwarf_version >= 4 || !dwarf_strict)
16075         goto rtl_addr;
16076       return false;
16077
16078     case PLUS:
16079       /* In cases where an inlined instance of an inline function is passed
16080          the address of an `auto' variable (which is local to the caller) we
16081          can get a situation where the DECL_RTL of the artificial local
16082          variable (for the inlining) which acts as a stand-in for the
16083          corresponding formal parameter (of the inline function) will look
16084          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16085          exactly a compile-time constant expression, but it isn't the address
16086          of the (artificial) local variable either.  Rather, it represents the
16087          *value* which the artificial local variable always has during its
16088          lifetime.  We currently have no way to represent such quasi-constant
16089          values in Dwarf, so for now we just punt and generate nothing.  */
16090       return false;
16091
16092     case HIGH:
16093     case CONST_FIXED:
16094       return false;
16095
16096     case MEM:
16097       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16098           && MEM_READONLY_P (rtl)
16099           && GET_MODE (rtl) == BLKmode)
16100         {
16101           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16102           return true;
16103         }
16104       return false;
16105
16106     default:
16107       /* No other kinds of rtx should be possible here.  */
16108       gcc_unreachable ();
16109     }
16110   return false;
16111 }
16112
16113 /* Determine whether the evaluation of EXPR references any variables
16114    or functions which aren't otherwise used (and therefore may not be
16115    output).  */
16116 static tree
16117 reference_to_unused (tree * tp, int * walk_subtrees,
16118                      void * data ATTRIBUTE_UNUSED)
16119 {
16120   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16121     *walk_subtrees = 0;
16122
16123   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16124       && ! TREE_ASM_WRITTEN (*tp))
16125     return *tp;
16126   /* ???  The C++ FE emits debug information for using decls, so
16127      putting gcc_unreachable here falls over.  See PR31899.  For now
16128      be conservative.  */
16129   else if (!cgraph_global_info_ready
16130            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16131     return *tp;
16132   else if (TREE_CODE (*tp) == VAR_DECL)
16133     {
16134       struct varpool_node *node = varpool_get_node (*tp);
16135       if (!node || !node->needed)
16136         return *tp;
16137     }
16138   else if (TREE_CODE (*tp) == FUNCTION_DECL
16139            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16140     {
16141       /* The call graph machinery must have finished analyzing,
16142          optimizing and gimplifying the CU by now.
16143          So if *TP has no call graph node associated
16144          to it, it means *TP will not be emitted.  */
16145       if (!cgraph_get_node (*tp))
16146         return *tp;
16147     }
16148   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16149     return *tp;
16150
16151   return NULL_TREE;
16152 }
16153
16154 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16155    for use in a later add_const_value_attribute call.  */
16156
16157 static rtx
16158 rtl_for_decl_init (tree init, tree type)
16159 {
16160   rtx rtl = NULL_RTX;
16161
16162   /* If a variable is initialized with a string constant without embedded
16163      zeros, build CONST_STRING.  */
16164   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16165     {
16166       tree enttype = TREE_TYPE (type);
16167       tree domain = TYPE_DOMAIN (type);
16168       enum machine_mode mode = TYPE_MODE (enttype);
16169
16170       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16171           && domain
16172           && integer_zerop (TYPE_MIN_VALUE (domain))
16173           && compare_tree_int (TYPE_MAX_VALUE (domain),
16174                                TREE_STRING_LENGTH (init) - 1) == 0
16175           && ((size_t) TREE_STRING_LENGTH (init)
16176               == strlen (TREE_STRING_POINTER (init)) + 1))
16177         {
16178           rtl = gen_rtx_CONST_STRING (VOIDmode,
16179                                       ggc_strdup (TREE_STRING_POINTER (init)));
16180           rtl = gen_rtx_MEM (BLKmode, rtl);
16181           MEM_READONLY_P (rtl) = 1;
16182         }
16183     }
16184   /* Other aggregates, and complex values, could be represented using
16185      CONCAT: FIXME!  */
16186   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16187     ;
16188   /* Vectors only work if their mode is supported by the target.
16189      FIXME: generic vectors ought to work too.  */
16190   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16191     ;
16192   /* If the initializer is something that we know will expand into an
16193      immediate RTL constant, expand it now.  We must be careful not to
16194      reference variables which won't be output.  */
16195   else if (initializer_constant_valid_p (init, type)
16196            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16197     {
16198       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16199          possible.  */
16200       if (TREE_CODE (type) == VECTOR_TYPE)
16201         switch (TREE_CODE (init))
16202           {
16203           case VECTOR_CST:
16204             break;
16205           case CONSTRUCTOR:
16206             if (TREE_CONSTANT (init))
16207               {
16208                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16209                 bool constant_p = true;
16210                 tree value;
16211                 unsigned HOST_WIDE_INT ix;
16212
16213                 /* Even when ctor is constant, it might contain non-*_CST
16214                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16215                    belong into VECTOR_CST nodes.  */
16216                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16217                   if (!CONSTANT_CLASS_P (value))
16218                     {
16219                       constant_p = false;
16220                       break;
16221                     }
16222
16223                 if (constant_p)
16224                   {
16225                     init = build_vector_from_ctor (type, elts);
16226                     break;
16227                   }
16228               }
16229             /* FALLTHRU */
16230
16231           default:
16232             return NULL;
16233           }
16234
16235       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16236
16237       /* If expand_expr returns a MEM, it wasn't immediate.  */
16238       gcc_assert (!rtl || !MEM_P (rtl));
16239     }
16240
16241   return rtl;
16242 }
16243
16244 /* Generate RTL for the variable DECL to represent its location.  */
16245
16246 static rtx
16247 rtl_for_decl_location (tree decl)
16248 {
16249   rtx rtl;
16250
16251   /* Here we have to decide where we are going to say the parameter "lives"
16252      (as far as the debugger is concerned).  We only have a couple of
16253      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16254
16255      DECL_RTL normally indicates where the parameter lives during most of the
16256      activation of the function.  If optimization is enabled however, this
16257      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16258      that the parameter doesn't really live anywhere (as far as the code
16259      generation parts of GCC are concerned) during most of the function's
16260      activation.  That will happen (for example) if the parameter is never
16261      referenced within the function.
16262
16263      We could just generate a location descriptor here for all non-NULL
16264      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16265      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16266      where DECL_RTL is NULL or is a pseudo-reg.
16267
16268      Note however that we can only get away with using DECL_INCOMING_RTL as
16269      a backup substitute for DECL_RTL in certain limited cases.  In cases
16270      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16271      we can be sure that the parameter was passed using the same type as it is
16272      declared to have within the function, and that its DECL_INCOMING_RTL
16273      points us to a place where a value of that type is passed.
16274
16275      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16276      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16277      because in these cases DECL_INCOMING_RTL points us to a value of some
16278      type which is *different* from the type of the parameter itself.  Thus,
16279      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16280      such cases, the debugger would end up (for example) trying to fetch a
16281      `float' from a place which actually contains the first part of a
16282      `double'.  That would lead to really incorrect and confusing
16283      output at debug-time.
16284
16285      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16286      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16287      are a couple of exceptions however.  On little-endian machines we can
16288      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16289      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16290      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16291      when (on a little-endian machine) a non-prototyped function has a
16292      parameter declared to be of type `short' or `char'.  In such cases,
16293      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16294      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16295      passed `int' value.  If the debugger then uses that address to fetch
16296      a `short' or a `char' (on a little-endian machine) the result will be
16297      the correct data, so we allow for such exceptional cases below.
16298
16299      Note that our goal here is to describe the place where the given formal
16300      parameter lives during most of the function's activation (i.e. between the
16301      end of the prologue and the start of the epilogue).  We'll do that as best
16302      as we can. Note however that if the given formal parameter is modified
16303      sometime during the execution of the function, then a stack backtrace (at
16304      debug-time) will show the function as having been called with the *new*
16305      value rather than the value which was originally passed in.  This happens
16306      rarely enough that it is not a major problem, but it *is* a problem, and
16307      I'd like to fix it.
16308
16309      A future version of dwarf2out.c may generate two additional attributes for
16310      any given DW_TAG_formal_parameter DIE which will describe the "passed
16311      type" and the "passed location" for the given formal parameter in addition
16312      to the attributes we now generate to indicate the "declared type" and the
16313      "active location" for each parameter.  This additional set of attributes
16314      could be used by debuggers for stack backtraces. Separately, note that
16315      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16316      This happens (for example) for inlined-instances of inline function formal
16317      parameters which are never referenced.  This really shouldn't be
16318      happening.  All PARM_DECL nodes should get valid non-NULL
16319      DECL_INCOMING_RTL values.  FIXME.  */
16320
16321   /* Use DECL_RTL as the "location" unless we find something better.  */
16322   rtl = DECL_RTL_IF_SET (decl);
16323
16324   /* When generating abstract instances, ignore everything except
16325      constants, symbols living in memory, and symbols living in
16326      fixed registers.  */
16327   if (! reload_completed)
16328     {
16329       if (rtl
16330           && (CONSTANT_P (rtl)
16331               || (MEM_P (rtl)
16332                   && CONSTANT_P (XEXP (rtl, 0)))
16333               || (REG_P (rtl)
16334                   && TREE_CODE (decl) == VAR_DECL
16335                   && TREE_STATIC (decl))))
16336         {
16337           rtl = targetm.delegitimize_address (rtl);
16338           return rtl;
16339         }
16340       rtl = NULL_RTX;
16341     }
16342   else if (TREE_CODE (decl) == PARM_DECL)
16343     {
16344       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16345         {
16346           tree declared_type = TREE_TYPE (decl);
16347           tree passed_type = DECL_ARG_TYPE (decl);
16348           enum machine_mode dmode = TYPE_MODE (declared_type);
16349           enum machine_mode pmode = TYPE_MODE (passed_type);
16350
16351           /* This decl represents a formal parameter which was optimized out.
16352              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16353              all cases where (rtl == NULL_RTX) just below.  */
16354           if (dmode == pmode)
16355             rtl = DECL_INCOMING_RTL (decl);
16356           else if (SCALAR_INT_MODE_P (dmode)
16357                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16358                    && DECL_INCOMING_RTL (decl))
16359             {
16360               rtx inc = DECL_INCOMING_RTL (decl);
16361               if (REG_P (inc))
16362                 rtl = inc;
16363               else if (MEM_P (inc))
16364                 {
16365                   if (BYTES_BIG_ENDIAN)
16366                     rtl = adjust_address_nv (inc, dmode,
16367                                              GET_MODE_SIZE (pmode)
16368                                              - GET_MODE_SIZE (dmode));
16369                   else
16370                     rtl = inc;
16371                 }
16372             }
16373         }
16374
16375       /* If the parm was passed in registers, but lives on the stack, then
16376          make a big endian correction if the mode of the type of the
16377          parameter is not the same as the mode of the rtl.  */
16378       /* ??? This is the same series of checks that are made in dbxout.c before
16379          we reach the big endian correction code there.  It isn't clear if all
16380          of these checks are necessary here, but keeping them all is the safe
16381          thing to do.  */
16382       else if (MEM_P (rtl)
16383                && XEXP (rtl, 0) != const0_rtx
16384                && ! CONSTANT_P (XEXP (rtl, 0))
16385                /* Not passed in memory.  */
16386                && !MEM_P (DECL_INCOMING_RTL (decl))
16387                /* Not passed by invisible reference.  */
16388                && (!REG_P (XEXP (rtl, 0))
16389                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16390                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16391 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16392                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16393 #endif
16394                      )
16395                /* Big endian correction check.  */
16396                && BYTES_BIG_ENDIAN
16397                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16398                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16399                    < UNITS_PER_WORD))
16400         {
16401           int offset = (UNITS_PER_WORD
16402                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16403
16404           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16405                              plus_constant (XEXP (rtl, 0), offset));
16406         }
16407     }
16408   else if (TREE_CODE (decl) == VAR_DECL
16409            && rtl
16410            && MEM_P (rtl)
16411            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16412            && BYTES_BIG_ENDIAN)
16413     {
16414       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16415       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16416
16417       /* If a variable is declared "register" yet is smaller than
16418          a register, then if we store the variable to memory, it
16419          looks like we're storing a register-sized value, when in
16420          fact we are not.  We need to adjust the offset of the
16421          storage location to reflect the actual value's bytes,
16422          else gdb will not be able to display it.  */
16423       if (rsize > dsize)
16424         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16425                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16426     }
16427
16428   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16429      and will have been substituted directly into all expressions that use it.
16430      C does not have such a concept, but C++ and other languages do.  */
16431   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16432     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16433
16434   if (rtl)
16435     rtl = targetm.delegitimize_address (rtl);
16436
16437   /* If we don't look past the constant pool, we risk emitting a
16438      reference to a constant pool entry that isn't referenced from
16439      code, and thus is not emitted.  */
16440   if (rtl)
16441     rtl = avoid_constant_pool_reference (rtl);
16442
16443   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16444      in the current CU, resolve_addr will remove the expression referencing
16445      it.  */
16446   if (rtl == NULL_RTX
16447       && TREE_CODE (decl) == VAR_DECL
16448       && !DECL_EXTERNAL (decl)
16449       && TREE_STATIC (decl)
16450       && DECL_NAME (decl)
16451       && !DECL_HARD_REGISTER (decl)
16452       && DECL_MODE (decl) != VOIDmode)
16453     {
16454       rtl = make_decl_rtl_for_debug (decl);
16455       if (!MEM_P (rtl)
16456           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16457           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16458         rtl = NULL_RTX;
16459     }
16460
16461   return rtl;
16462 }
16463
16464 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16465    returned.  If so, the decl for the COMMON block is returned, and the
16466    value is the offset into the common block for the symbol.  */
16467
16468 static tree
16469 fortran_common (tree decl, HOST_WIDE_INT *value)
16470 {
16471   tree val_expr, cvar;
16472   enum machine_mode mode;
16473   HOST_WIDE_INT bitsize, bitpos;
16474   tree offset;
16475   int volatilep = 0, unsignedp = 0;
16476
16477   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16478      it does not have a value (the offset into the common area), or if it
16479      is thread local (as opposed to global) then it isn't common, and shouldn't
16480      be handled as such.  */
16481   if (TREE_CODE (decl) != VAR_DECL
16482       || !TREE_STATIC (decl)
16483       || !DECL_HAS_VALUE_EXPR_P (decl)
16484       || !is_fortran ())
16485     return NULL_TREE;
16486
16487   val_expr = DECL_VALUE_EXPR (decl);
16488   if (TREE_CODE (val_expr) != COMPONENT_REF)
16489     return NULL_TREE;
16490
16491   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16492                               &mode, &unsignedp, &volatilep, true);
16493
16494   if (cvar == NULL_TREE
16495       || TREE_CODE (cvar) != VAR_DECL
16496       || DECL_ARTIFICIAL (cvar)
16497       || !TREE_PUBLIC (cvar))
16498     return NULL_TREE;
16499
16500   *value = 0;
16501   if (offset != NULL)
16502     {
16503       if (!host_integerp (offset, 0))
16504         return NULL_TREE;
16505       *value = tree_low_cst (offset, 0);
16506     }
16507   if (bitpos != 0)
16508     *value += bitpos / BITS_PER_UNIT;
16509
16510   return cvar;
16511 }
16512
16513 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16514    data attribute for a variable or a parameter.  We generate the
16515    DW_AT_const_value attribute only in those cases where the given variable
16516    or parameter does not have a true "location" either in memory or in a
16517    register.  This can happen (for example) when a constant is passed as an
16518    actual argument in a call to an inline function.  (It's possible that
16519    these things can crop up in other ways also.)  Note that one type of
16520    constant value which can be passed into an inlined function is a constant
16521    pointer.  This can happen for example if an actual argument in an inlined
16522    function call evaluates to a compile-time constant address.  */
16523
16524 static bool
16525 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16526                                        enum dwarf_attribute attr)
16527 {
16528   rtx rtl;
16529   dw_loc_list_ref list;
16530   var_loc_list *loc_list;
16531
16532   if (TREE_CODE (decl) == ERROR_MARK)
16533     return false;
16534
16535   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16536               || TREE_CODE (decl) == RESULT_DECL);
16537
16538   /* Try to get some constant RTL for this decl, and use that as the value of
16539      the location.  */
16540
16541   rtl = rtl_for_decl_location (decl);
16542   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16543       && add_const_value_attribute (die, rtl))
16544     return true;
16545
16546   /* See if we have single element location list that is equivalent to
16547      a constant value.  That way we are better to use add_const_value_attribute
16548      rather than expanding constant value equivalent.  */
16549   loc_list = lookup_decl_loc (decl);
16550   if (loc_list
16551       && loc_list->first
16552       && loc_list->first->next == NULL
16553       && NOTE_P (loc_list->first->loc)
16554       && NOTE_VAR_LOCATION (loc_list->first->loc)
16555       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16556     {
16557       struct var_loc_node *node;
16558
16559       node = loc_list->first;
16560       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16561       if (GET_CODE (rtl) == EXPR_LIST)
16562         rtl = XEXP (rtl, 0);
16563       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16564           && add_const_value_attribute (die, rtl))
16565          return true;
16566     }
16567   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16568   if (list)
16569     {
16570       add_AT_location_description (die, attr, list);
16571       return true;
16572     }
16573   /* None of that worked, so it must not really have a location;
16574      try adding a constant value attribute from the DECL_INITIAL.  */
16575   return tree_add_const_value_attribute_for_decl (die, decl);
16576 }
16577
16578 /* Add VARIABLE and DIE into deferred locations list.  */
16579
16580 static void
16581 defer_location (tree variable, dw_die_ref die)
16582 {
16583   deferred_locations entry;
16584   entry.variable = variable;
16585   entry.die = die;
16586   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16587 }
16588
16589 /* Helper function for tree_add_const_value_attribute.  Natively encode
16590    initializer INIT into an array.  Return true if successful.  */
16591
16592 static bool
16593 native_encode_initializer (tree init, unsigned char *array, int size)
16594 {
16595   tree type;
16596
16597   if (init == NULL_TREE)
16598     return false;
16599
16600   STRIP_NOPS (init);
16601   switch (TREE_CODE (init))
16602     {
16603     case STRING_CST:
16604       type = TREE_TYPE (init);
16605       if (TREE_CODE (type) == ARRAY_TYPE)
16606         {
16607           tree enttype = TREE_TYPE (type);
16608           enum machine_mode mode = TYPE_MODE (enttype);
16609
16610           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16611             return false;
16612           if (int_size_in_bytes (type) != size)
16613             return false;
16614           if (size > TREE_STRING_LENGTH (init))
16615             {
16616               memcpy (array, TREE_STRING_POINTER (init),
16617                       TREE_STRING_LENGTH (init));
16618               memset (array + TREE_STRING_LENGTH (init),
16619                       '\0', size - TREE_STRING_LENGTH (init));
16620             }
16621           else
16622             memcpy (array, TREE_STRING_POINTER (init), size);
16623           return true;
16624         }
16625       return false;
16626     case CONSTRUCTOR:
16627       type = TREE_TYPE (init);
16628       if (int_size_in_bytes (type) != size)
16629         return false;
16630       if (TREE_CODE (type) == ARRAY_TYPE)
16631         {
16632           HOST_WIDE_INT min_index;
16633           unsigned HOST_WIDE_INT cnt;
16634           int curpos = 0, fieldsize;
16635           constructor_elt *ce;
16636
16637           if (TYPE_DOMAIN (type) == NULL_TREE
16638               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16639             return false;
16640
16641           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16642           if (fieldsize <= 0)
16643             return false;
16644
16645           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16646           memset (array, '\0', size);
16647           for (cnt = 0;
16648                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16649                cnt++)
16650             {
16651               tree val = ce->value;
16652               tree index = ce->index;
16653               int pos = curpos;
16654               if (index && TREE_CODE (index) == RANGE_EXPR)
16655                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16656                       * fieldsize;
16657               else if (index)
16658                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16659
16660               if (val)
16661                 {
16662                   STRIP_NOPS (val);
16663                   if (!native_encode_initializer (val, array + pos, fieldsize))
16664                     return false;
16665                 }
16666               curpos = pos + fieldsize;
16667               if (index && TREE_CODE (index) == RANGE_EXPR)
16668                 {
16669                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16670                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16671                   while (count > 0)
16672                     {
16673                       if (val)
16674                         memcpy (array + curpos, array + pos, fieldsize);
16675                       curpos += fieldsize;
16676                     }
16677                 }
16678               gcc_assert (curpos <= size);
16679             }
16680           return true;
16681         }
16682       else if (TREE_CODE (type) == RECORD_TYPE
16683                || TREE_CODE (type) == UNION_TYPE)
16684         {
16685           tree field = NULL_TREE;
16686           unsigned HOST_WIDE_INT cnt;
16687           constructor_elt *ce;
16688
16689           if (int_size_in_bytes (type) != size)
16690             return false;
16691
16692           if (TREE_CODE (type) == RECORD_TYPE)
16693             field = TYPE_FIELDS (type);
16694
16695           for (cnt = 0;
16696                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16697                cnt++, field = field ? TREE_CHAIN (field) : 0)
16698             {
16699               tree val = ce->value;
16700               int pos, fieldsize;
16701
16702               if (ce->index != 0)
16703                 field = ce->index;
16704
16705               if (val)
16706                 STRIP_NOPS (val);
16707
16708               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16709                 return false;
16710
16711               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16712                   && TYPE_DOMAIN (TREE_TYPE (field))
16713                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16714                 return false;
16715               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16716                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16717                 return false;
16718               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16719               pos = int_byte_position (field);
16720               gcc_assert (pos + fieldsize <= size);
16721               if (val
16722                   && !native_encode_initializer (val, array + pos, fieldsize))
16723                 return false;
16724             }
16725           return true;
16726         }
16727       return false;
16728     case VIEW_CONVERT_EXPR:
16729     case NON_LVALUE_EXPR:
16730       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16731     default:
16732       return native_encode_expr (init, array, size) == size;
16733     }
16734 }
16735
16736 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16737    attribute is the const value T.  */
16738
16739 static bool
16740 tree_add_const_value_attribute (dw_die_ref die, tree t)
16741 {
16742   tree init;
16743   tree type = TREE_TYPE (t);
16744   rtx rtl;
16745
16746   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16747     return false;
16748
16749   init = t;
16750   gcc_assert (!DECL_P (init));
16751
16752   rtl = rtl_for_decl_init (init, type);
16753   if (rtl)
16754     return add_const_value_attribute (die, rtl);
16755   /* If the host and target are sane, try harder.  */
16756   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16757            && initializer_constant_valid_p (init, type))
16758     {
16759       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16760       if (size > 0 && (int) size == size)
16761         {
16762           unsigned char *array = (unsigned char *)
16763             ggc_alloc_cleared_atomic (size);
16764
16765           if (native_encode_initializer (init, array, size))
16766             {
16767               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16768               return true;
16769             }
16770         }
16771     }
16772   return false;
16773 }
16774
16775 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16776    attribute is the const value of T, where T is an integral constant
16777    variable with static storage duration
16778    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16779
16780 static bool
16781 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16782 {
16783
16784   if (!decl
16785       || (TREE_CODE (decl) != VAR_DECL
16786           && TREE_CODE (decl) != CONST_DECL))
16787     return false;
16788
16789     if (TREE_READONLY (decl)
16790         && ! TREE_THIS_VOLATILE (decl)
16791         && DECL_INITIAL (decl))
16792       /* OK */;
16793     else
16794       return false;
16795
16796   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16797   if (get_AT (var_die, DW_AT_const_value))
16798     return false;
16799
16800   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16801 }
16802
16803 /* Convert the CFI instructions for the current function into a
16804    location list.  This is used for DW_AT_frame_base when we targeting
16805    a dwarf2 consumer that does not support the dwarf3
16806    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16807    expressions.  */
16808
16809 static dw_loc_list_ref
16810 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16811 {
16812   dw_fde_ref fde;
16813   dw_loc_list_ref list, *list_tail;
16814   dw_cfi_ref cfi;
16815   dw_cfa_location last_cfa, next_cfa;
16816   const char *start_label, *last_label, *section;
16817   dw_cfa_location remember;
16818
16819   fde = current_fde ();
16820   gcc_assert (fde != NULL);
16821
16822   section = secname_for_decl (current_function_decl);
16823   list_tail = &list;
16824   list = NULL;
16825
16826   memset (&next_cfa, 0, sizeof (next_cfa));
16827   next_cfa.reg = INVALID_REGNUM;
16828   remember = next_cfa;
16829
16830   start_label = fde->dw_fde_begin;
16831
16832   /* ??? Bald assumption that the CIE opcode list does not contain
16833      advance opcodes.  */
16834   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16835     lookup_cfa_1 (cfi, &next_cfa, &remember);
16836
16837   last_cfa = next_cfa;
16838   last_label = start_label;
16839
16840   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16841     switch (cfi->dw_cfi_opc)
16842       {
16843       case DW_CFA_set_loc:
16844       case DW_CFA_advance_loc1:
16845       case DW_CFA_advance_loc2:
16846       case DW_CFA_advance_loc4:
16847         if (!cfa_equal_p (&last_cfa, &next_cfa))
16848           {
16849             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16850                                        start_label, last_label, section);
16851
16852             list_tail = &(*list_tail)->dw_loc_next;
16853             last_cfa = next_cfa;
16854             start_label = last_label;
16855           }
16856         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16857         break;
16858
16859       case DW_CFA_advance_loc:
16860         /* The encoding is complex enough that we should never emit this.  */
16861         gcc_unreachable ();
16862
16863       default:
16864         lookup_cfa_1 (cfi, &next_cfa, &remember);
16865         break;
16866       }
16867
16868   if (!cfa_equal_p (&last_cfa, &next_cfa))
16869     {
16870       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16871                                  start_label, last_label, section);
16872       list_tail = &(*list_tail)->dw_loc_next;
16873       start_label = last_label;
16874     }
16875
16876   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16877                              start_label, fde->dw_fde_end, section);
16878
16879   if (list && list->dw_loc_next)
16880     gen_llsym (list);
16881
16882   return list;
16883 }
16884
16885 /* Compute a displacement from the "steady-state frame pointer" to the
16886    frame base (often the same as the CFA), and store it in
16887    frame_pointer_fb_offset.  OFFSET is added to the displacement
16888    before the latter is negated.  */
16889
16890 static void
16891 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16892 {
16893   rtx reg, elim;
16894
16895 #ifdef FRAME_POINTER_CFA_OFFSET
16896   reg = frame_pointer_rtx;
16897   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16898 #else
16899   reg = arg_pointer_rtx;
16900   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16901 #endif
16902
16903   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16904   if (GET_CODE (elim) == PLUS)
16905     {
16906       offset += INTVAL (XEXP (elim, 1));
16907       elim = XEXP (elim, 0);
16908     }
16909
16910   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16911                && (elim == hard_frame_pointer_rtx
16912                    || elim == stack_pointer_rtx))
16913               || elim == (frame_pointer_needed
16914                           ? hard_frame_pointer_rtx
16915                           : stack_pointer_rtx));
16916
16917   frame_pointer_fb_offset = -offset;
16918 }
16919
16920 /* Generate a DW_AT_name attribute given some string value to be included as
16921    the value of the attribute.  */
16922
16923 static void
16924 add_name_attribute (dw_die_ref die, const char *name_string)
16925 {
16926   if (name_string != NULL && *name_string != 0)
16927     {
16928       if (demangle_name_func)
16929         name_string = (*demangle_name_func) (name_string);
16930
16931       add_AT_string (die, DW_AT_name, name_string);
16932     }
16933 }
16934
16935 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16936
16937 static void
16938 add_comp_dir_attribute (dw_die_ref die)
16939 {
16940   const char *wd = get_src_pwd ();
16941   char *wd1;
16942
16943   if (wd == NULL)
16944     return;
16945
16946   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16947     {
16948       int wdlen;
16949
16950       wdlen = strlen (wd);
16951       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16952       strcpy (wd1, wd);
16953       wd1 [wdlen] = DIR_SEPARATOR;
16954       wd1 [wdlen + 1] = 0;
16955       wd = wd1;
16956     }
16957
16958     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16959 }
16960
16961 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16962    default.  */
16963
16964 static int
16965 lower_bound_default (void)
16966 {
16967   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
16968     {
16969     case DW_LANG_C:
16970     case DW_LANG_C89:
16971     case DW_LANG_C99:
16972     case DW_LANG_C_plus_plus:
16973     case DW_LANG_ObjC:
16974     case DW_LANG_ObjC_plus_plus:
16975     case DW_LANG_Java:
16976       return 0;
16977     case DW_LANG_Fortran77:
16978     case DW_LANG_Fortran90:
16979     case DW_LANG_Fortran95:
16980       return 1;
16981     case DW_LANG_UPC:
16982     case DW_LANG_D:
16983     case DW_LANG_Python:
16984       return dwarf_version >= 4 ? 0 : -1;
16985     case DW_LANG_Ada95:
16986     case DW_LANG_Ada83:
16987     case DW_LANG_Cobol74:
16988     case DW_LANG_Cobol85:
16989     case DW_LANG_Pascal83:
16990     case DW_LANG_Modula2:
16991     case DW_LANG_PLI:
16992       return dwarf_version >= 4 ? 1 : -1;
16993     default:
16994       return -1;
16995     }
16996 }
16997
16998 /* Given a tree node describing an array bound (either lower or upper) output
16999    a representation for that bound.  */
17000
17001 static void
17002 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17003 {
17004   switch (TREE_CODE (bound))
17005     {
17006     case ERROR_MARK:
17007       return;
17008
17009     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17010     case INTEGER_CST:
17011       {
17012         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17013         int dflt;
17014
17015         /* Use the default if possible.  */
17016         if (bound_attr == DW_AT_lower_bound
17017             && host_integerp (bound, 0)
17018             && (dflt = lower_bound_default ()) != -1
17019             && tree_low_cst (bound, 0) == dflt)
17020           ;
17021
17022         /* Otherwise represent the bound as an unsigned value with the
17023            precision of its type.  The precision and signedness of the
17024            type will be necessary to re-interpret it unambiguously.  */
17025         else if (prec < HOST_BITS_PER_WIDE_INT)
17026           {
17027             unsigned HOST_WIDE_INT mask
17028               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17029             add_AT_unsigned (subrange_die, bound_attr,
17030                              TREE_INT_CST_LOW (bound) & mask);
17031           }
17032         else if (prec == HOST_BITS_PER_WIDE_INT
17033                  || TREE_INT_CST_HIGH (bound) == 0)
17034           add_AT_unsigned (subrange_die, bound_attr,
17035                            TREE_INT_CST_LOW (bound));
17036         else
17037           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17038                          TREE_INT_CST_LOW (bound));
17039       }
17040       break;
17041
17042     CASE_CONVERT:
17043     case VIEW_CONVERT_EXPR:
17044       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17045       break;
17046
17047     case SAVE_EXPR:
17048       break;
17049
17050     case VAR_DECL:
17051     case PARM_DECL:
17052     case RESULT_DECL:
17053       {
17054         dw_die_ref decl_die = lookup_decl_die (bound);
17055
17056         /* ??? Can this happen, or should the variable have been bound
17057            first?  Probably it can, since I imagine that we try to create
17058            the types of parameters in the order in which they exist in
17059            the list, and won't have created a forward reference to a
17060            later parameter.  */
17061         if (decl_die != NULL)
17062           {
17063             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17064             break;
17065           }
17066       }
17067       /* FALLTHRU */
17068
17069     default:
17070       {
17071         /* Otherwise try to create a stack operation procedure to
17072            evaluate the value of the array bound.  */
17073
17074         dw_die_ref ctx, decl_die;
17075         dw_loc_list_ref list;
17076
17077         list = loc_list_from_tree (bound, 2);
17078         if (list == NULL || single_element_loc_list_p (list))
17079           {
17080             /* If DW_AT_*bound is not a reference nor constant, it is
17081                a DWARF expression rather than location description.
17082                For that loc_list_from_tree (bound, 0) is needed.
17083                If that fails to give a single element list,
17084                fall back to outputting this as a reference anyway.  */
17085             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17086             if (list2 && single_element_loc_list_p (list2))
17087               {
17088                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17089                 break;
17090               }
17091           }
17092         if (list == NULL)
17093           break;
17094
17095         if (current_function_decl == 0)
17096           ctx = comp_unit_die;
17097         else
17098           ctx = lookup_decl_die (current_function_decl);
17099
17100         decl_die = new_die (DW_TAG_variable, ctx, bound);
17101         add_AT_flag (decl_die, DW_AT_artificial, 1);
17102         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17103         add_AT_location_description (decl_die, DW_AT_location, list);
17104         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17105         break;
17106       }
17107     }
17108 }
17109
17110 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17111    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17112    Note that the block of subscript information for an array type also
17113    includes information about the element type of the given array type.  */
17114
17115 static void
17116 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17117 {
17118   unsigned dimension_number;
17119   tree lower, upper;
17120   dw_die_ref subrange_die;
17121
17122   for (dimension_number = 0;
17123        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17124        type = TREE_TYPE (type), dimension_number++)
17125     {
17126       tree domain = TYPE_DOMAIN (type);
17127
17128       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17129         break;
17130
17131       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17132          and (in GNU C only) variable bounds.  Handle all three forms
17133          here.  */
17134       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17135       if (domain)
17136         {
17137           /* We have an array type with specified bounds.  */
17138           lower = TYPE_MIN_VALUE (domain);
17139           upper = TYPE_MAX_VALUE (domain);
17140
17141           /* Define the index type.  */
17142           if (TREE_TYPE (domain))
17143             {
17144               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17145                  TREE_TYPE field.  We can't emit debug info for this
17146                  because it is an unnamed integral type.  */
17147               if (TREE_CODE (domain) == INTEGER_TYPE
17148                   && TYPE_NAME (domain) == NULL_TREE
17149                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17150                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17151                 ;
17152               else
17153                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17154                                     type_die);
17155             }
17156
17157           /* ??? If upper is NULL, the array has unspecified length,
17158              but it does have a lower bound.  This happens with Fortran
17159                dimension arr(N:*)
17160              Since the debugger is definitely going to need to know N
17161              to produce useful results, go ahead and output the lower
17162              bound solo, and hope the debugger can cope.  */
17163
17164           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17165           if (upper)
17166             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17167         }
17168
17169       /* Otherwise we have an array type with an unspecified length.  The
17170          DWARF-2 spec does not say how to handle this; let's just leave out the
17171          bounds.  */
17172     }
17173 }
17174
17175 static void
17176 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17177 {
17178   unsigned size;
17179
17180   switch (TREE_CODE (tree_node))
17181     {
17182     case ERROR_MARK:
17183       size = 0;
17184       break;
17185     case ENUMERAL_TYPE:
17186     case RECORD_TYPE:
17187     case UNION_TYPE:
17188     case QUAL_UNION_TYPE:
17189       size = int_size_in_bytes (tree_node);
17190       break;
17191     case FIELD_DECL:
17192       /* For a data member of a struct or union, the DW_AT_byte_size is
17193          generally given as the number of bytes normally allocated for an
17194          object of the *declared* type of the member itself.  This is true
17195          even for bit-fields.  */
17196       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17197       break;
17198     default:
17199       gcc_unreachable ();
17200     }
17201
17202   /* Note that `size' might be -1 when we get to this point.  If it is, that
17203      indicates that the byte size of the entity in question is variable.  We
17204      have no good way of expressing this fact in Dwarf at the present time,
17205      so just let the -1 pass on through.  */
17206   add_AT_unsigned (die, DW_AT_byte_size, size);
17207 }
17208
17209 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17210    which specifies the distance in bits from the highest order bit of the
17211    "containing object" for the bit-field to the highest order bit of the
17212    bit-field itself.
17213
17214    For any given bit-field, the "containing object" is a hypothetical object
17215    (of some integral or enum type) within which the given bit-field lives.  The
17216    type of this hypothetical "containing object" is always the same as the
17217    declared type of the individual bit-field itself.  The determination of the
17218    exact location of the "containing object" for a bit-field is rather
17219    complicated.  It's handled by the `field_byte_offset' function (above).
17220
17221    Note that it is the size (in bytes) of the hypothetical "containing object"
17222    which will be given in the DW_AT_byte_size attribute for this bit-field.
17223    (See `byte_size_attribute' above).  */
17224
17225 static inline void
17226 add_bit_offset_attribute (dw_die_ref die, tree decl)
17227 {
17228   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17229   tree type = DECL_BIT_FIELD_TYPE (decl);
17230   HOST_WIDE_INT bitpos_int;
17231   HOST_WIDE_INT highest_order_object_bit_offset;
17232   HOST_WIDE_INT highest_order_field_bit_offset;
17233   HOST_WIDE_INT unsigned bit_offset;
17234
17235   /* Must be a field and a bit field.  */
17236   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17237
17238   /* We can't yet handle bit-fields whose offsets are variable, so if we
17239      encounter such things, just return without generating any attribute
17240      whatsoever.  Likewise for variable or too large size.  */
17241   if (! host_integerp (bit_position (decl), 0)
17242       || ! host_integerp (DECL_SIZE (decl), 1))
17243     return;
17244
17245   bitpos_int = int_bit_position (decl);
17246
17247   /* Note that the bit offset is always the distance (in bits) from the
17248      highest-order bit of the "containing object" to the highest-order bit of
17249      the bit-field itself.  Since the "high-order end" of any object or field
17250      is different on big-endian and little-endian machines, the computation
17251      below must take account of these differences.  */
17252   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17253   highest_order_field_bit_offset = bitpos_int;
17254
17255   if (! BYTES_BIG_ENDIAN)
17256     {
17257       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17258       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17259     }
17260
17261   bit_offset
17262     = (! BYTES_BIG_ENDIAN
17263        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17264        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17265
17266   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17267 }
17268
17269 /* For a FIELD_DECL node which represents a bit field, output an attribute
17270    which specifies the length in bits of the given field.  */
17271
17272 static inline void
17273 add_bit_size_attribute (dw_die_ref die, tree decl)
17274 {
17275   /* Must be a field and a bit field.  */
17276   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17277               && DECL_BIT_FIELD_TYPE (decl));
17278
17279   if (host_integerp (DECL_SIZE (decl), 1))
17280     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17281 }
17282
17283 /* If the compiled language is ANSI C, then add a 'prototyped'
17284    attribute, if arg types are given for the parameters of a function.  */
17285
17286 static inline void
17287 add_prototyped_attribute (dw_die_ref die, tree func_type)
17288 {
17289   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17290       && TYPE_ARG_TYPES (func_type) != NULL)
17291     add_AT_flag (die, DW_AT_prototyped, 1);
17292 }
17293
17294 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17295    by looking in either the type declaration or object declaration
17296    equate table.  */
17297
17298 static inline dw_die_ref
17299 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17300 {
17301   dw_die_ref origin_die = NULL;
17302
17303   if (TREE_CODE (origin) != FUNCTION_DECL)
17304     {
17305       /* We may have gotten separated from the block for the inlined
17306          function, if we're in an exception handler or some such; make
17307          sure that the abstract function has been written out.
17308
17309          Doing this for nested functions is wrong, however; functions are
17310          distinct units, and our context might not even be inline.  */
17311       tree fn = origin;
17312
17313       if (TYPE_P (fn))
17314         fn = TYPE_STUB_DECL (fn);
17315
17316       fn = decl_function_context (fn);
17317       if (fn)
17318         dwarf2out_abstract_function (fn);
17319     }
17320
17321   if (DECL_P (origin))
17322     origin_die = lookup_decl_die (origin);
17323   else if (TYPE_P (origin))
17324     origin_die = lookup_type_die (origin);
17325
17326   /* XXX: Functions that are never lowered don't always have correct block
17327      trees (in the case of java, they simply have no block tree, in some other
17328      languages).  For these functions, there is nothing we can really do to
17329      output correct debug info for inlined functions in all cases.  Rather
17330      than die, we'll just produce deficient debug info now, in that we will
17331      have variables without a proper abstract origin.  In the future, when all
17332      functions are lowered, we should re-add a gcc_assert (origin_die)
17333      here.  */
17334
17335   if (origin_die)
17336     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17337   return origin_die;
17338 }
17339
17340 /* We do not currently support the pure_virtual attribute.  */
17341
17342 static inline void
17343 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17344 {
17345   if (DECL_VINDEX (func_decl))
17346     {
17347       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17348
17349       if (host_integerp (DECL_VINDEX (func_decl), 0))
17350         add_AT_loc (die, DW_AT_vtable_elem_location,
17351                     new_loc_descr (DW_OP_constu,
17352                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17353                                    0));
17354
17355       /* GNU extension: Record what type this method came from originally.  */
17356       if (debug_info_level > DINFO_LEVEL_TERSE
17357           && DECL_CONTEXT (func_decl))
17358         add_AT_die_ref (die, DW_AT_containing_type,
17359                         lookup_type_die (DECL_CONTEXT (func_decl)));
17360     }
17361 }
17362 \f
17363 /* Add source coordinate attributes for the given decl.  */
17364
17365 static void
17366 add_src_coords_attributes (dw_die_ref die, tree decl)
17367 {
17368   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17369
17370   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17371   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17372 }
17373
17374 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17375
17376 static void
17377 add_linkage_name (dw_die_ref die, tree decl)
17378 {
17379   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17380        && TREE_PUBLIC (decl)
17381        && !DECL_ABSTRACT (decl)
17382        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17383        && die->die_tag != DW_TAG_member)
17384     {
17385       /* Defer until we have an assembler name set.  */
17386       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17387         {
17388           limbo_die_node *asm_name;
17389
17390           asm_name = ggc_alloc_cleared_limbo_die_node ();
17391           asm_name->die = die;
17392           asm_name->created_for = decl;
17393           asm_name->next = deferred_asm_name;
17394           deferred_asm_name = asm_name;
17395         }
17396       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17397         add_AT_string (die, AT_linkage_name,
17398                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17399     }
17400 }
17401
17402 /* Add a DW_AT_name attribute and source coordinate attribute for the
17403    given decl, but only if it actually has a name.  */
17404
17405 static void
17406 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17407 {
17408   tree decl_name;
17409
17410   decl_name = DECL_NAME (decl);
17411   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17412     {
17413       const char *name = dwarf2_name (decl, 0);
17414       if (name)
17415         add_name_attribute (die, name);
17416       if (! DECL_ARTIFICIAL (decl))
17417         add_src_coords_attributes (die, decl);
17418
17419       add_linkage_name (die, decl);
17420     }
17421
17422 #ifdef VMS_DEBUGGING_INFO
17423   /* Get the function's name, as described by its RTL.  This may be different
17424      from the DECL_NAME name used in the source file.  */
17425   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17426     {
17427       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17428                    XEXP (DECL_RTL (decl), 0));
17429       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17430     }
17431 #endif
17432 }
17433
17434 #ifdef VMS_DEBUGGING_INFO
17435
17436 /* Output the debug main pointer die for VMS */
17437
17438 void
17439 dwarf2out_vms_debug_main_pointer (void)
17440 {
17441   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17442   dw_die_ref die;
17443
17444   /* Allocate the VMS debug main subprogram die.  */
17445   die = ggc_alloc_cleared_die_node ();
17446   die->die_tag = DW_TAG_subprogram;
17447   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17448   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17449                                current_function_funcdef_no);
17450   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17451
17452   /* Make it the first child of comp_unit_die.  */
17453   die->die_parent = comp_unit_die;
17454   if (comp_unit_die->die_child)
17455     {
17456       die->die_sib = comp_unit_die->die_child->die_sib;
17457       comp_unit_die->die_child->die_sib = die;
17458     }
17459   else
17460     {
17461       die->die_sib = die;
17462       comp_unit_die->die_child = die;
17463     }
17464 }
17465 #endif
17466
17467 /* Push a new declaration scope.  */
17468
17469 static void
17470 push_decl_scope (tree scope)
17471 {
17472   VEC_safe_push (tree, gc, decl_scope_table, scope);
17473 }
17474
17475 /* Pop a declaration scope.  */
17476
17477 static inline void
17478 pop_decl_scope (void)
17479 {
17480   VEC_pop (tree, decl_scope_table);
17481 }
17482
17483 /* Return the DIE for the scope that immediately contains this type.
17484    Non-named types get global scope.  Named types nested in other
17485    types get their containing scope if it's open, or global scope
17486    otherwise.  All other types (i.e. function-local named types) get
17487    the current active scope.  */
17488
17489 static dw_die_ref
17490 scope_die_for (tree t, dw_die_ref context_die)
17491 {
17492   dw_die_ref scope_die = NULL;
17493   tree containing_scope;
17494   int i;
17495
17496   /* Non-types always go in the current scope.  */
17497   gcc_assert (TYPE_P (t));
17498
17499   containing_scope = TYPE_CONTEXT (t);
17500
17501   /* Use the containing namespace if it was passed in (for a declaration).  */
17502   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17503     {
17504       if (context_die == lookup_decl_die (containing_scope))
17505         /* OK */;
17506       else
17507         containing_scope = NULL_TREE;
17508     }
17509
17510   /* Ignore function type "scopes" from the C frontend.  They mean that
17511      a tagged type is local to a parmlist of a function declarator, but
17512      that isn't useful to DWARF.  */
17513   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17514     containing_scope = NULL_TREE;
17515
17516   if (containing_scope == NULL_TREE)
17517     scope_die = comp_unit_die;
17518   else if (TYPE_P (containing_scope))
17519     {
17520       /* For types, we can just look up the appropriate DIE.  But
17521          first we check to see if we're in the middle of emitting it
17522          so we know where the new DIE should go.  */
17523       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17524         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17525           break;
17526
17527       if (i < 0)
17528         {
17529           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17530                       || TREE_ASM_WRITTEN (containing_scope));
17531
17532           /* If none of the current dies are suitable, we get file scope.  */
17533           scope_die = comp_unit_die;
17534         }
17535       else
17536         scope_die = lookup_type_die (containing_scope);
17537     }
17538   else
17539     scope_die = context_die;
17540
17541   return scope_die;
17542 }
17543
17544 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17545
17546 static inline int
17547 local_scope_p (dw_die_ref context_die)
17548 {
17549   for (; context_die; context_die = context_die->die_parent)
17550     if (context_die->die_tag == DW_TAG_inlined_subroutine
17551         || context_die->die_tag == DW_TAG_subprogram)
17552       return 1;
17553
17554   return 0;
17555 }
17556
17557 /* Returns nonzero if CONTEXT_DIE is a class.  */
17558
17559 static inline int
17560 class_scope_p (dw_die_ref context_die)
17561 {
17562   return (context_die
17563           && (context_die->die_tag == DW_TAG_structure_type
17564               || context_die->die_tag == DW_TAG_class_type
17565               || context_die->die_tag == DW_TAG_interface_type
17566               || context_die->die_tag == DW_TAG_union_type));
17567 }
17568
17569 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17570    whether or not to treat a DIE in this context as a declaration.  */
17571
17572 static inline int
17573 class_or_namespace_scope_p (dw_die_ref context_die)
17574 {
17575   return (class_scope_p (context_die)
17576           || (context_die && context_die->die_tag == DW_TAG_namespace));
17577 }
17578
17579 /* Many forms of DIEs require a "type description" attribute.  This
17580    routine locates the proper "type descriptor" die for the type given
17581    by 'type', and adds a DW_AT_type attribute below the given die.  */
17582
17583 static void
17584 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17585                     int decl_volatile, dw_die_ref context_die)
17586 {
17587   enum tree_code code  = TREE_CODE (type);
17588   dw_die_ref type_die  = NULL;
17589
17590   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17591      or fixed-point type, use the inner type.  This is because we have no
17592      support for unnamed types in base_type_die.  This can happen if this is
17593      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17594   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17595       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17596     type = TREE_TYPE (type), code = TREE_CODE (type);
17597
17598   if (code == ERROR_MARK
17599       /* Handle a special case.  For functions whose return type is void, we
17600          generate *no* type attribute.  (Note that no object may have type
17601          `void', so this only applies to function return types).  */
17602       || code == VOID_TYPE)
17603     return;
17604
17605   type_die = modified_type_die (type,
17606                                 decl_const || TYPE_READONLY (type),
17607                                 decl_volatile || TYPE_VOLATILE (type),
17608                                 context_die);
17609
17610   if (type_die != NULL)
17611     add_AT_die_ref (object_die, DW_AT_type, type_die);
17612 }
17613
17614 /* Given an object die, add the calling convention attribute for the
17615    function call type.  */
17616 static void
17617 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17618 {
17619   enum dwarf_calling_convention value = DW_CC_normal;
17620
17621   value = ((enum dwarf_calling_convention)
17622            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17623
17624   /* DWARF doesn't provide a way to identify a program's source-level
17625      entry point.  DW_AT_calling_convention attributes are only meant
17626      to describe functions' calling conventions.  However, lacking a
17627      better way to signal the Fortran main program, we use this for the
17628      time being, following existing custom.  */
17629   if (is_fortran ()
17630       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17631     value = DW_CC_program;
17632
17633   /* Only add the attribute if the backend requests it, and
17634      is not DW_CC_normal.  */
17635   if (value && (value != DW_CC_normal))
17636     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17637 }
17638
17639 /* Given a tree pointer to a struct, class, union, or enum type node, return
17640    a pointer to the (string) tag name for the given type, or zero if the type
17641    was declared without a tag.  */
17642
17643 static const char *
17644 type_tag (const_tree type)
17645 {
17646   const char *name = 0;
17647
17648   if (TYPE_NAME (type) != 0)
17649     {
17650       tree t = 0;
17651
17652       /* Find the IDENTIFIER_NODE for the type name.  */
17653       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
17654         t = TYPE_NAME (type);
17655
17656       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17657          a TYPE_DECL node, regardless of whether or not a `typedef' was
17658          involved.  */
17659       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17660                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17661         {
17662           /* We want to be extra verbose.  Don't call dwarf_name if
17663              DECL_NAME isn't set.  The default hook for decl_printable_name
17664              doesn't like that, and in this context it's correct to return
17665              0, instead of "<anonymous>" or the like.  */
17666           if (DECL_NAME (TYPE_NAME (type)))
17667             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17668         }
17669
17670       /* Now get the name as a string, or invent one.  */
17671       if (!name && t != 0)
17672         name = IDENTIFIER_POINTER (t);
17673     }
17674
17675   return (name == 0 || *name == '\0') ? 0 : name;
17676 }
17677
17678 /* Return the type associated with a data member, make a special check
17679    for bit field types.  */
17680
17681 static inline tree
17682 member_declared_type (const_tree member)
17683 {
17684   return (DECL_BIT_FIELD_TYPE (member)
17685           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17686 }
17687
17688 /* Get the decl's label, as described by its RTL. This may be different
17689    from the DECL_NAME name used in the source file.  */
17690
17691 #if 0
17692 static const char *
17693 decl_start_label (tree decl)
17694 {
17695   rtx x;
17696   const char *fnname;
17697
17698   x = DECL_RTL (decl);
17699   gcc_assert (MEM_P (x));
17700
17701   x = XEXP (x, 0);
17702   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17703
17704   fnname = XSTR (x, 0);
17705   return fnname;
17706 }
17707 #endif
17708 \f
17709 /* These routines generate the internal representation of the DIE's for
17710    the compilation unit.  Debugging information is collected by walking
17711    the declaration trees passed in from dwarf2out_decl().  */
17712
17713 static void
17714 gen_array_type_die (tree type, dw_die_ref context_die)
17715 {
17716   dw_die_ref scope_die = scope_die_for (type, context_die);
17717   dw_die_ref array_die;
17718
17719   /* GNU compilers represent multidimensional array types as sequences of one
17720      dimensional array types whose element types are themselves array types.
17721      We sometimes squish that down to a single array_type DIE with multiple
17722      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17723      say that we are allowed to do this kind of compression in C, because
17724      there is no difference between an array of arrays and a multidimensional
17725      array.  We don't do this for Ada to remain as close as possible to the
17726      actual representation, which is especially important against the language
17727      flexibilty wrt arrays of variable size.  */
17728
17729   bool collapse_nested_arrays = !is_ada ();
17730   tree element_type;
17731
17732   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17733      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17734   if (TYPE_STRING_FLAG (type)
17735       && TREE_CODE (type) == ARRAY_TYPE
17736       && is_fortran ()
17737       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17738     {
17739       HOST_WIDE_INT size;
17740
17741       array_die = new_die (DW_TAG_string_type, scope_die, type);
17742       add_name_attribute (array_die, type_tag (type));
17743       equate_type_number_to_die (type, array_die);
17744       size = int_size_in_bytes (type);
17745       if (size >= 0)
17746         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17747       else if (TYPE_DOMAIN (type) != NULL_TREE
17748                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17749                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17750         {
17751           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17752           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17753
17754           size = int_size_in_bytes (TREE_TYPE (szdecl));
17755           if (loc && size > 0)
17756             {
17757               add_AT_location_description (array_die, DW_AT_string_length, loc);
17758               if (size != DWARF2_ADDR_SIZE)
17759                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17760             }
17761         }
17762       return;
17763     }
17764
17765   /* ??? The SGI dwarf reader fails for array of array of enum types
17766      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17767      array type comes before the outer array type.  We thus call gen_type_die
17768      before we new_die and must prevent nested array types collapsing for this
17769      target.  */
17770
17771 #ifdef MIPS_DEBUGGING_INFO
17772   gen_type_die (TREE_TYPE (type), context_die);
17773   collapse_nested_arrays = false;
17774 #endif
17775
17776   array_die = new_die (DW_TAG_array_type, scope_die, type);
17777   add_name_attribute (array_die, type_tag (type));
17778   equate_type_number_to_die (type, array_die);
17779
17780   if (TREE_CODE (type) == VECTOR_TYPE)
17781     {
17782       /* The frontend feeds us a representation for the vector as a struct
17783          containing an array.  Pull out the array type.  */
17784       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17785       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17786     }
17787
17788   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17789   if (is_fortran ()
17790       && TREE_CODE (type) == ARRAY_TYPE
17791       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17792       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17793     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17794
17795 #if 0
17796   /* We default the array ordering.  SDB will probably do
17797      the right things even if DW_AT_ordering is not present.  It's not even
17798      an issue until we start to get into multidimensional arrays anyway.  If
17799      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17800      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17801      and when we find out that we need to put these in, we will only do so
17802      for multidimensional arrays.  */
17803   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17804 #endif
17805
17806 #ifdef MIPS_DEBUGGING_INFO
17807   /* The SGI compilers handle arrays of unknown bound by setting
17808      AT_declaration and not emitting any subrange DIEs.  */
17809   if (! TYPE_DOMAIN (type))
17810     add_AT_flag (array_die, DW_AT_declaration, 1);
17811   else
17812 #endif
17813     add_subscript_info (array_die, type, collapse_nested_arrays);
17814
17815   /* Add representation of the type of the elements of this array type and
17816      emit the corresponding DIE if we haven't done it already.  */
17817   element_type = TREE_TYPE (type);
17818   if (collapse_nested_arrays)
17819     while (TREE_CODE (element_type) == ARRAY_TYPE)
17820       {
17821         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17822           break;
17823         element_type = TREE_TYPE (element_type);
17824       }
17825
17826 #ifndef MIPS_DEBUGGING_INFO
17827   gen_type_die (element_type, context_die);
17828 #endif
17829
17830   add_type_attribute (array_die, element_type, 0, 0, context_die);
17831
17832   if (get_AT (array_die, DW_AT_name))
17833     add_pubtype (type, array_die);
17834 }
17835
17836 static dw_loc_descr_ref
17837 descr_info_loc (tree val, tree base_decl)
17838 {
17839   HOST_WIDE_INT size;
17840   dw_loc_descr_ref loc, loc2;
17841   enum dwarf_location_atom op;
17842
17843   if (val == base_decl)
17844     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17845
17846   switch (TREE_CODE (val))
17847     {
17848     CASE_CONVERT:
17849       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17850     case VAR_DECL:
17851       return loc_descriptor_from_tree (val, 0);
17852     case INTEGER_CST:
17853       if (host_integerp (val, 0))
17854         return int_loc_descriptor (tree_low_cst (val, 0));
17855       break;
17856     case INDIRECT_REF:
17857       size = int_size_in_bytes (TREE_TYPE (val));
17858       if (size < 0)
17859         break;
17860       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17861       if (!loc)
17862         break;
17863       if (size == DWARF2_ADDR_SIZE)
17864         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17865       else
17866         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17867       return loc;
17868     case POINTER_PLUS_EXPR:
17869     case PLUS_EXPR:
17870       if (host_integerp (TREE_OPERAND (val, 1), 1)
17871           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17872              < 16384)
17873         {
17874           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17875           if (!loc)
17876             break;
17877           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17878         }
17879       else
17880         {
17881           op = DW_OP_plus;
17882         do_binop:
17883           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17884           if (!loc)
17885             break;
17886           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17887           if (!loc2)
17888             break;
17889           add_loc_descr (&loc, loc2);
17890           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17891         }
17892       return loc;
17893     case MINUS_EXPR:
17894       op = DW_OP_minus;
17895       goto do_binop;
17896     case MULT_EXPR:
17897       op = DW_OP_mul;
17898       goto do_binop;
17899     case EQ_EXPR:
17900       op = DW_OP_eq;
17901       goto do_binop;
17902     case NE_EXPR:
17903       op = DW_OP_ne;
17904       goto do_binop;
17905     default:
17906       break;
17907     }
17908   return NULL;
17909 }
17910
17911 static void
17912 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17913                       tree val, tree base_decl)
17914 {
17915   dw_loc_descr_ref loc;
17916
17917   if (host_integerp (val, 0))
17918     {
17919       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17920       return;
17921     }
17922
17923   loc = descr_info_loc (val, base_decl);
17924   if (!loc)
17925     return;
17926
17927   add_AT_loc (die, attr, loc);
17928 }
17929
17930 /* This routine generates DIE for array with hidden descriptor, details
17931    are filled into *info by a langhook.  */
17932
17933 static void
17934 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17935                           dw_die_ref context_die)
17936 {
17937   dw_die_ref scope_die = scope_die_for (type, context_die);
17938   dw_die_ref array_die;
17939   int dim;
17940
17941   array_die = new_die (DW_TAG_array_type, scope_die, type);
17942   add_name_attribute (array_die, type_tag (type));
17943   equate_type_number_to_die (type, array_die);
17944
17945   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17946   if (is_fortran ()
17947       && info->ndimensions >= 2)
17948     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17949
17950   if (info->data_location)
17951     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17952                           info->base_decl);
17953   if (info->associated)
17954     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17955                           info->base_decl);
17956   if (info->allocated)
17957     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17958                           info->base_decl);
17959
17960   for (dim = 0; dim < info->ndimensions; dim++)
17961     {
17962       dw_die_ref subrange_die
17963         = new_die (DW_TAG_subrange_type, array_die, NULL);
17964
17965       if (info->dimen[dim].lower_bound)
17966         {
17967           /* If it is the default value, omit it.  */
17968           int dflt;
17969
17970           if (host_integerp (info->dimen[dim].lower_bound, 0)
17971               && (dflt = lower_bound_default ()) != -1
17972               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17973             ;
17974           else
17975             add_descr_info_field (subrange_die, DW_AT_lower_bound,
17976                                   info->dimen[dim].lower_bound,
17977                                   info->base_decl);
17978         }
17979       if (info->dimen[dim].upper_bound)
17980         add_descr_info_field (subrange_die, DW_AT_upper_bound,
17981                               info->dimen[dim].upper_bound,
17982                               info->base_decl);
17983       if (info->dimen[dim].stride)
17984         add_descr_info_field (subrange_die, DW_AT_byte_stride,
17985                               info->dimen[dim].stride,
17986                               info->base_decl);
17987     }
17988
17989   gen_type_die (info->element_type, context_die);
17990   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17991
17992   if (get_AT (array_die, DW_AT_name))
17993     add_pubtype (type, array_die);
17994 }
17995
17996 #if 0
17997 static void
17998 gen_entry_point_die (tree decl, dw_die_ref context_die)
17999 {
18000   tree origin = decl_ultimate_origin (decl);
18001   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18002
18003   if (origin != NULL)
18004     add_abstract_origin_attribute (decl_die, origin);
18005   else
18006     {
18007       add_name_and_src_coords_attributes (decl_die, decl);
18008       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18009                           0, 0, context_die);
18010     }
18011
18012   if (DECL_ABSTRACT (decl))
18013     equate_decl_number_to_die (decl, decl_die);
18014   else
18015     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18016 }
18017 #endif
18018
18019 /* Walk through the list of incomplete types again, trying once more to
18020    emit full debugging info for them.  */
18021
18022 static void
18023 retry_incomplete_types (void)
18024 {
18025   int i;
18026
18027   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18028     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18029                                   DINFO_USAGE_DIR_USE))
18030       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18031 }
18032
18033 /* Determine what tag to use for a record type.  */
18034
18035 static enum dwarf_tag
18036 record_type_tag (tree type)
18037 {
18038   if (! lang_hooks.types.classify_record)
18039     return DW_TAG_structure_type;
18040
18041   switch (lang_hooks.types.classify_record (type))
18042     {
18043     case RECORD_IS_STRUCT:
18044       return DW_TAG_structure_type;
18045
18046     case RECORD_IS_CLASS:
18047       return DW_TAG_class_type;
18048
18049     case RECORD_IS_INTERFACE:
18050       if (dwarf_version >= 3 || !dwarf_strict)
18051         return DW_TAG_interface_type;
18052       return DW_TAG_structure_type;
18053
18054     default:
18055       gcc_unreachable ();
18056     }
18057 }
18058
18059 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18060    include all of the information about the enumeration values also. Each
18061    enumerated type name/value is listed as a child of the enumerated type
18062    DIE.  */
18063
18064 static dw_die_ref
18065 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18066 {
18067   dw_die_ref type_die = lookup_type_die (type);
18068
18069   if (type_die == NULL)
18070     {
18071       type_die = new_die (DW_TAG_enumeration_type,
18072                           scope_die_for (type, context_die), type);
18073       equate_type_number_to_die (type, type_die);
18074       add_name_attribute (type_die, type_tag (type));
18075       if ((dwarf_version >= 4 || !dwarf_strict)
18076           && ENUM_IS_SCOPED (type))
18077         add_AT_flag (type_die, DW_AT_enum_class, 1);
18078     }
18079   else if (! TYPE_SIZE (type))
18080     return type_die;
18081   else
18082     remove_AT (type_die, DW_AT_declaration);
18083
18084   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18085      given enum type is incomplete, do not generate the DW_AT_byte_size
18086      attribute or the DW_AT_element_list attribute.  */
18087   if (TYPE_SIZE (type))
18088     {
18089       tree link;
18090
18091       TREE_ASM_WRITTEN (type) = 1;
18092       add_byte_size_attribute (type_die, type);
18093       if (TYPE_STUB_DECL (type) != NULL_TREE)
18094         {
18095           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18096           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18097         }
18098
18099       /* If the first reference to this type was as the return type of an
18100          inline function, then it may not have a parent.  Fix this now.  */
18101       if (type_die->die_parent == NULL)
18102         add_child_die (scope_die_for (type, context_die), type_die);
18103
18104       for (link = TYPE_VALUES (type);
18105            link != NULL; link = TREE_CHAIN (link))
18106         {
18107           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18108           tree value = TREE_VALUE (link);
18109
18110           add_name_attribute (enum_die,
18111                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18112
18113           if (TREE_CODE (value) == CONST_DECL)
18114             value = DECL_INITIAL (value);
18115
18116           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18117             /* DWARF2 does not provide a way of indicating whether or
18118                not enumeration constants are signed or unsigned.  GDB
18119                always assumes the values are signed, so we output all
18120                values as if they were signed.  That means that
18121                enumeration constants with very large unsigned values
18122                will appear to have negative values in the debugger.  */
18123             add_AT_int (enum_die, DW_AT_const_value,
18124                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18125         }
18126     }
18127   else
18128     add_AT_flag (type_die, DW_AT_declaration, 1);
18129
18130   if (get_AT (type_die, DW_AT_name))
18131     add_pubtype (type, type_die);
18132
18133   return type_die;
18134 }
18135
18136 /* Generate a DIE to represent either a real live formal parameter decl or to
18137    represent just the type of some formal parameter position in some function
18138    type.
18139
18140    Note that this routine is a bit unusual because its argument may be a
18141    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18142    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18143    node.  If it's the former then this function is being called to output a
18144    DIE to represent a formal parameter object (or some inlining thereof).  If
18145    it's the latter, then this function is only being called to output a
18146    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18147    argument type of some subprogram type.
18148    If EMIT_NAME_P is true, name and source coordinate attributes
18149    are emitted.  */
18150
18151 static dw_die_ref
18152 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18153                           dw_die_ref context_die)
18154 {
18155   tree node_or_origin = node ? node : origin;
18156   tree ultimate_origin;
18157   dw_die_ref parm_die
18158     = new_die (DW_TAG_formal_parameter, context_die, node);
18159
18160   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18161     {
18162     case tcc_declaration:
18163       ultimate_origin = decl_ultimate_origin (node_or_origin);
18164       if (node || ultimate_origin)
18165         origin = ultimate_origin;
18166       if (origin != NULL)
18167         add_abstract_origin_attribute (parm_die, origin);
18168       else if (emit_name_p)
18169         add_name_and_src_coords_attributes (parm_die, node);
18170       if (origin == NULL
18171           || (! DECL_ABSTRACT (node_or_origin)
18172               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18173                                            decl_function_context
18174                                                             (node_or_origin))))
18175         {
18176           tree type = TREE_TYPE (node_or_origin);
18177           if (decl_by_reference_p (node_or_origin))
18178             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18179                                 context_die);
18180           else
18181             add_type_attribute (parm_die, type,
18182                                 TREE_READONLY (node_or_origin),
18183                                 TREE_THIS_VOLATILE (node_or_origin),
18184                                 context_die);
18185         }
18186       if (origin == NULL && DECL_ARTIFICIAL (node))
18187         add_AT_flag (parm_die, DW_AT_artificial, 1);
18188
18189       if (node && node != origin)
18190         equate_decl_number_to_die (node, parm_die);
18191       if (! DECL_ABSTRACT (node_or_origin))
18192         add_location_or_const_value_attribute (parm_die, node_or_origin,
18193                                                DW_AT_location);
18194
18195       break;
18196
18197     case tcc_type:
18198       /* We were called with some kind of a ..._TYPE node.  */
18199       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18200       break;
18201
18202     default:
18203       gcc_unreachable ();
18204     }
18205
18206   return parm_die;
18207 }
18208
18209 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18210    children DW_TAG_formal_parameter DIEs representing the arguments of the
18211    parameter pack.
18212
18213    PARM_PACK must be a function parameter pack.
18214    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18215    must point to the subsequent arguments of the function PACK_ARG belongs to.
18216    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18217    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18218    following the last one for which a DIE was generated.  */
18219
18220 static dw_die_ref
18221 gen_formal_parameter_pack_die  (tree parm_pack,
18222                                 tree pack_arg,
18223                                 dw_die_ref subr_die,
18224                                 tree *next_arg)
18225 {
18226   tree arg;
18227   dw_die_ref parm_pack_die;
18228
18229   gcc_assert (parm_pack
18230               && lang_hooks.function_parameter_pack_p (parm_pack)
18231               && subr_die);
18232
18233   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18234   add_src_coords_attributes (parm_pack_die, parm_pack);
18235
18236   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
18237     {
18238       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18239                                                                  parm_pack))
18240         break;
18241       gen_formal_parameter_die (arg, NULL,
18242                                 false /* Don't emit name attribute.  */,
18243                                 parm_pack_die);
18244     }
18245   if (next_arg)
18246     *next_arg = arg;
18247   return parm_pack_die;
18248 }
18249
18250 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18251    at the end of an (ANSI prototyped) formal parameters list.  */
18252
18253 static void
18254 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18255 {
18256   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18257 }
18258
18259 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18260    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18261    parameters as specified in some function type specification (except for
18262    those which appear as part of a function *definition*).  */
18263
18264 static void
18265 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18266 {
18267   tree link;
18268   tree formal_type = NULL;
18269   tree first_parm_type;
18270   tree arg;
18271
18272   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18273     {
18274       arg = DECL_ARGUMENTS (function_or_method_type);
18275       function_or_method_type = TREE_TYPE (function_or_method_type);
18276     }
18277   else
18278     arg = NULL_TREE;
18279
18280   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18281
18282   /* Make our first pass over the list of formal parameter types and output a
18283      DW_TAG_formal_parameter DIE for each one.  */
18284   for (link = first_parm_type; link; )
18285     {
18286       dw_die_ref parm_die;
18287
18288       formal_type = TREE_VALUE (link);
18289       if (formal_type == void_type_node)
18290         break;
18291
18292       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18293       parm_die = gen_formal_parameter_die (formal_type, NULL,
18294                                            true /* Emit name attribute.  */,
18295                                            context_die);
18296       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
18297            && link == first_parm_type)
18298           || (arg && DECL_ARTIFICIAL (arg)))
18299         add_AT_flag (parm_die, DW_AT_artificial, 1);
18300
18301       link = TREE_CHAIN (link);
18302       if (arg)
18303         arg = TREE_CHAIN (arg);
18304     }
18305
18306   /* If this function type has an ellipsis, add a
18307      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18308   if (formal_type != void_type_node)
18309     gen_unspecified_parameters_die (function_or_method_type, context_die);
18310
18311   /* Make our second (and final) pass over the list of formal parameter types
18312      and output DIEs to represent those types (as necessary).  */
18313   for (link = TYPE_ARG_TYPES (function_or_method_type);
18314        link && TREE_VALUE (link);
18315        link = TREE_CHAIN (link))
18316     gen_type_die (TREE_VALUE (link), context_die);
18317 }
18318
18319 /* We want to generate the DIE for TYPE so that we can generate the
18320    die for MEMBER, which has been defined; we will need to refer back
18321    to the member declaration nested within TYPE.  If we're trying to
18322    generate minimal debug info for TYPE, processing TYPE won't do the
18323    trick; we need to attach the member declaration by hand.  */
18324
18325 static void
18326 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18327 {
18328   gen_type_die (type, context_die);
18329
18330   /* If we're trying to avoid duplicate debug info, we may not have
18331      emitted the member decl for this function.  Emit it now.  */
18332   if (TYPE_STUB_DECL (type)
18333       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18334       && ! lookup_decl_die (member))
18335     {
18336       dw_die_ref type_die;
18337       gcc_assert (!decl_ultimate_origin (member));
18338
18339       push_decl_scope (type);
18340       type_die = lookup_type_die (type);
18341       if (TREE_CODE (member) == FUNCTION_DECL)
18342         gen_subprogram_die (member, type_die);
18343       else if (TREE_CODE (member) == FIELD_DECL)
18344         {
18345           /* Ignore the nameless fields that are used to skip bits but handle
18346              C++ anonymous unions and structs.  */
18347           if (DECL_NAME (member) != NULL_TREE
18348               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18349               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18350             {
18351               gen_type_die (member_declared_type (member), type_die);
18352               gen_field_die (member, type_die);
18353             }
18354         }
18355       else
18356         gen_variable_die (member, NULL_TREE, type_die);
18357
18358       pop_decl_scope ();
18359     }
18360 }
18361
18362 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18363    may later generate inlined and/or out-of-line instances of.  */
18364
18365 static void
18366 dwarf2out_abstract_function (tree decl)
18367 {
18368   dw_die_ref old_die;
18369   tree save_fn;
18370   tree context;
18371   int was_abstract;
18372   htab_t old_decl_loc_table;
18373
18374   /* Make sure we have the actual abstract inline, not a clone.  */
18375   decl = DECL_ORIGIN (decl);
18376
18377   old_die = lookup_decl_die (decl);
18378   if (old_die && get_AT (old_die, DW_AT_inline))
18379     /* We've already generated the abstract instance.  */
18380     return;
18381
18382   /* We can be called while recursively when seeing block defining inlined subroutine
18383      DIE.  Be sure to not clobber the outer location table nor use it or we would
18384      get locations in abstract instantces.  */
18385   old_decl_loc_table = decl_loc_table;
18386   decl_loc_table = NULL;
18387
18388   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18389      we don't get confused by DECL_ABSTRACT.  */
18390   if (debug_info_level > DINFO_LEVEL_TERSE)
18391     {
18392       context = decl_class_context (decl);
18393       if (context)
18394         gen_type_die_for_member
18395           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18396     }
18397
18398   /* Pretend we've just finished compiling this function.  */
18399   save_fn = current_function_decl;
18400   current_function_decl = decl;
18401   push_cfun (DECL_STRUCT_FUNCTION (decl));
18402
18403   was_abstract = DECL_ABSTRACT (decl);
18404   set_decl_abstract_flags (decl, 1);
18405   dwarf2out_decl (decl);
18406   if (! was_abstract)
18407     set_decl_abstract_flags (decl, 0);
18408
18409   current_function_decl = save_fn;
18410   decl_loc_table = old_decl_loc_table;
18411   pop_cfun ();
18412 }
18413
18414 /* Helper function of premark_used_types() which gets called through
18415    htab_traverse.
18416
18417    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18418    marked as unused by prune_unused_types.  */
18419
18420 static int
18421 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18422 {
18423   tree type;
18424   dw_die_ref die;
18425
18426   type = (tree) *slot;
18427   die = lookup_type_die (type);
18428   if (die != NULL)
18429     die->die_perennial_p = 1;
18430   return 1;
18431 }
18432
18433 /* Helper function of premark_types_used_by_global_vars which gets called
18434    through htab_traverse.
18435
18436    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18437    marked as unused by prune_unused_types. The DIE of the type is marked
18438    only if the global variable using the type will actually be emitted.  */
18439
18440 static int
18441 premark_types_used_by_global_vars_helper (void **slot,
18442                                           void *data ATTRIBUTE_UNUSED)
18443 {
18444   struct types_used_by_vars_entry *entry;
18445   dw_die_ref die;
18446
18447   entry = (struct types_used_by_vars_entry *) *slot;
18448   gcc_assert (entry->type != NULL
18449               && entry->var_decl != NULL);
18450   die = lookup_type_die (entry->type);
18451   if (die)
18452     {
18453       /* Ask cgraph if the global variable really is to be emitted.
18454          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18455       struct varpool_node *node = varpool_get_node (entry->var_decl);
18456       if (node && node->needed)
18457         {
18458           die->die_perennial_p = 1;
18459           /* Keep the parent DIEs as well.  */
18460           while ((die = die->die_parent) && die->die_perennial_p == 0)
18461             die->die_perennial_p = 1;
18462         }
18463     }
18464   return 1;
18465 }
18466
18467 /* Mark all members of used_types_hash as perennial.  */
18468
18469 static void
18470 premark_used_types (void)
18471 {
18472   if (cfun && cfun->used_types_hash)
18473     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18474 }
18475
18476 /* Mark all members of types_used_by_vars_entry as perennial.  */
18477
18478 static void
18479 premark_types_used_by_global_vars (void)
18480 {
18481   if (types_used_by_vars_hash)
18482     htab_traverse (types_used_by_vars_hash,
18483                    premark_types_used_by_global_vars_helper, NULL);
18484 }
18485
18486 /* Generate a DIE to represent a declared function (either file-scope or
18487    block-local).  */
18488
18489 static void
18490 gen_subprogram_die (tree decl, dw_die_ref context_die)
18491 {
18492   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18493   tree origin = decl_ultimate_origin (decl);
18494   dw_die_ref subr_die;
18495   tree fn_arg_types;
18496   tree outer_scope;
18497   dw_die_ref old_die = lookup_decl_die (decl);
18498   int declaration = (current_function_decl != decl
18499                      || class_or_namespace_scope_p (context_die));
18500
18501   premark_used_types ();
18502
18503   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18504      started to generate the abstract instance of an inline, decided to output
18505      its containing class, and proceeded to emit the declaration of the inline
18506      from the member list for the class.  If so, DECLARATION takes priority;
18507      we'll get back to the abstract instance when done with the class.  */
18508
18509   /* The class-scope declaration DIE must be the primary DIE.  */
18510   if (origin && declaration && class_or_namespace_scope_p (context_die))
18511     {
18512       origin = NULL;
18513       gcc_assert (!old_die);
18514     }
18515
18516   /* Now that the C++ front end lazily declares artificial member fns, we
18517      might need to retrofit the declaration into its class.  */
18518   if (!declaration && !origin && !old_die
18519       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18520       && !class_or_namespace_scope_p (context_die)
18521       && debug_info_level > DINFO_LEVEL_TERSE)
18522     old_die = force_decl_die (decl);
18523
18524   if (origin != NULL)
18525     {
18526       gcc_assert (!declaration || local_scope_p (context_die));
18527
18528       /* Fixup die_parent for the abstract instance of a nested
18529          inline function.  */
18530       if (old_die && old_die->die_parent == NULL)
18531         add_child_die (context_die, old_die);
18532
18533       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18534       add_abstract_origin_attribute (subr_die, origin);
18535     }
18536   else if (old_die)
18537     {
18538       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18539       struct dwarf_file_data * file_index = lookup_filename (s.file);
18540
18541       if (!get_AT_flag (old_die, DW_AT_declaration)
18542           /* We can have a normal definition following an inline one in the
18543              case of redefinition of GNU C extern inlines.
18544              It seems reasonable to use AT_specification in this case.  */
18545           && !get_AT (old_die, DW_AT_inline))
18546         {
18547           /* Detect and ignore this case, where we are trying to output
18548              something we have already output.  */
18549           return;
18550         }
18551
18552       /* If the definition comes from the same place as the declaration,
18553          maybe use the old DIE.  We always want the DIE for this function
18554          that has the *_pc attributes to be under comp_unit_die so the
18555          debugger can find it.  We also need to do this for abstract
18556          instances of inlines, since the spec requires the out-of-line copy
18557          to have the same parent.  For local class methods, this doesn't
18558          apply; we just use the old DIE.  */
18559       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18560           && (DECL_ARTIFICIAL (decl)
18561               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18562                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18563                       == (unsigned) s.line))))
18564         {
18565           subr_die = old_die;
18566
18567           /* Clear out the declaration attribute and the formal parameters.
18568              Do not remove all children, because it is possible that this
18569              declaration die was forced using force_decl_die(). In such
18570              cases die that forced declaration die (e.g. TAG_imported_module)
18571              is one of the children that we do not want to remove.  */
18572           remove_AT (subr_die, DW_AT_declaration);
18573           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18574         }
18575       else
18576         {
18577           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18578           add_AT_specification (subr_die, old_die);
18579           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18580             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18581           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18582             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18583         }
18584     }
18585   else
18586     {
18587       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18588
18589       if (TREE_PUBLIC (decl))
18590         add_AT_flag (subr_die, DW_AT_external, 1);
18591
18592       add_name_and_src_coords_attributes (subr_die, decl);
18593       if (debug_info_level > DINFO_LEVEL_TERSE)
18594         {
18595           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18596           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18597                               0, 0, context_die);
18598         }
18599
18600       add_pure_or_virtual_attribute (subr_die, decl);
18601       if (DECL_ARTIFICIAL (decl))
18602         add_AT_flag (subr_die, DW_AT_artificial, 1);
18603
18604       add_accessibility_attribute (subr_die, decl);
18605     }
18606
18607   if (declaration)
18608     {
18609       if (!old_die || !get_AT (old_die, DW_AT_inline))
18610         {
18611           add_AT_flag (subr_die, DW_AT_declaration, 1);
18612
18613           /* If this is an explicit function declaration then generate
18614              a DW_AT_explicit attribute.  */
18615           if (lang_hooks.decls.function_decl_explicit_p (decl)
18616               && (dwarf_version >= 3 || !dwarf_strict))
18617             add_AT_flag (subr_die, DW_AT_explicit, 1);
18618
18619           /* The first time we see a member function, it is in the context of
18620              the class to which it belongs.  We make sure of this by emitting
18621              the class first.  The next time is the definition, which is
18622              handled above.  The two may come from the same source text.
18623
18624              Note that force_decl_die() forces function declaration die. It is
18625              later reused to represent definition.  */
18626           equate_decl_number_to_die (decl, subr_die);
18627         }
18628     }
18629   else if (DECL_ABSTRACT (decl))
18630     {
18631       if (DECL_DECLARED_INLINE_P (decl))
18632         {
18633           if (cgraph_function_possibly_inlined_p (decl))
18634             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18635           else
18636             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18637         }
18638       else
18639         {
18640           if (cgraph_function_possibly_inlined_p (decl))
18641             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18642           else
18643             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18644         }
18645
18646       if (DECL_DECLARED_INLINE_P (decl)
18647           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18648         add_AT_flag (subr_die, DW_AT_artificial, 1);
18649
18650       equate_decl_number_to_die (decl, subr_die);
18651     }
18652   else if (!DECL_EXTERNAL (decl))
18653     {
18654       HOST_WIDE_INT cfa_fb_offset;
18655
18656       if (!old_die || !get_AT (old_die, DW_AT_inline))
18657         equate_decl_number_to_die (decl, subr_die);
18658
18659       if (!flag_reorder_blocks_and_partition)
18660         {
18661           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18662                                        current_function_funcdef_no);
18663           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18664           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18665                                        current_function_funcdef_no);
18666           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18667
18668 #if VMS_DEBUGGING_INFO
18669       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18670          Section 2.3 Prologue and Epilogue Attributes:
18671          When a breakpoint is set on entry to a function, it is generally
18672          desirable for execution to be suspended, not on the very first
18673          instruction of the function, but rather at a point after the
18674          function's frame has been set up, after any language defined local
18675          declaration processing has been completed, and before execution of
18676          the first statement of the function begins. Debuggers generally
18677          cannot properly determine where this point is.  Similarly for a
18678          breakpoint set on exit from a function. The prologue and epilogue
18679          attributes allow a compiler to communicate the location(s) to use.  */
18680
18681       {
18682         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18683
18684         if (fde->dw_fde_vms_end_prologue)
18685           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18686             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18687
18688         if (fde->dw_fde_vms_begin_epilogue)
18689           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18690             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18691       }
18692 #endif
18693
18694           add_pubname (decl, subr_die);
18695           add_arange (decl, subr_die);
18696         }
18697       else
18698         {  /* Do nothing for now; maybe need to duplicate die, one for
18699               hot section and one for cold section, then use the hot/cold
18700               section begin/end labels to generate the aranges...  */
18701           /*
18702             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18703             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18704             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18705             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18706
18707             add_pubname (decl, subr_die);
18708             add_arange (decl, subr_die);
18709             add_arange (decl, subr_die);
18710            */
18711         }
18712
18713 #ifdef MIPS_DEBUGGING_INFO
18714       /* Add a reference to the FDE for this routine.  */
18715       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18716 #endif
18717
18718       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18719
18720       /* We define the "frame base" as the function's CFA.  This is more
18721          convenient for several reasons: (1) It's stable across the prologue
18722          and epilogue, which makes it better than just a frame pointer,
18723          (2) With dwarf3, there exists a one-byte encoding that allows us
18724          to reference the .debug_frame data by proxy, but failing that,
18725          (3) We can at least reuse the code inspection and interpretation
18726          code that determines the CFA position at various points in the
18727          function.  */
18728       if (dwarf_version >= 3)
18729         {
18730           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18731           add_AT_loc (subr_die, DW_AT_frame_base, op);
18732         }
18733       else
18734         {
18735           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18736           if (list->dw_loc_next)
18737             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18738           else
18739             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18740         }
18741
18742       /* Compute a displacement from the "steady-state frame pointer" to
18743          the CFA.  The former is what all stack slots and argument slots
18744          will reference in the rtl; the later is what we've told the
18745          debugger about.  We'll need to adjust all frame_base references
18746          by this displacement.  */
18747       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18748
18749       if (cfun->static_chain_decl)
18750         add_AT_location_description (subr_die, DW_AT_static_link,
18751                  loc_list_from_tree (cfun->static_chain_decl, 2));
18752     }
18753
18754   /* Generate child dies for template paramaters.  */
18755   if (debug_info_level > DINFO_LEVEL_TERSE)
18756     gen_generic_params_dies (decl);
18757
18758   /* Now output descriptions of the arguments for this function. This gets
18759      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18760      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18761      `...' at the end of the formal parameter list.  In order to find out if
18762      there was a trailing ellipsis or not, we must instead look at the type
18763      associated with the FUNCTION_DECL.  This will be a node of type
18764      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18765      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18766      an ellipsis at the end.  */
18767
18768   /* In the case where we are describing a mere function declaration, all we
18769      need to do here (and all we *can* do here) is to describe the *types* of
18770      its formal parameters.  */
18771   if (debug_info_level <= DINFO_LEVEL_TERSE)
18772     ;
18773   else if (declaration)
18774     gen_formal_types_die (decl, subr_die);
18775   else
18776     {
18777       /* Generate DIEs to represent all known formal parameters.  */
18778       tree parm = DECL_ARGUMENTS (decl);
18779       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18780       tree generic_decl_parm = generic_decl
18781                                 ? DECL_ARGUMENTS (generic_decl)
18782                                 : NULL;
18783
18784       /* Now we want to walk the list of parameters of the function and
18785          emit their relevant DIEs.
18786
18787          We consider the case of DECL being an instance of a generic function
18788          as well as it being a normal function.
18789
18790          If DECL is an instance of a generic function we walk the
18791          parameters of the generic function declaration _and_ the parameters of
18792          DECL itself. This is useful because we want to emit specific DIEs for
18793          function parameter packs and those are declared as part of the
18794          generic function declaration. In that particular case,
18795          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18796          That DIE has children DIEs representing the set of arguments
18797          of the pack. Note that the set of pack arguments can be empty.
18798          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18799          children DIE.
18800
18801          Otherwise, we just consider the parameters of DECL.  */
18802       while (generic_decl_parm || parm)
18803         {
18804           if (generic_decl_parm
18805               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18806             gen_formal_parameter_pack_die (generic_decl_parm,
18807                                            parm, subr_die,
18808                                            &parm);
18809           else if (parm)
18810             {
18811               gen_decl_die (parm, NULL, subr_die);
18812               parm = TREE_CHAIN (parm);
18813             }
18814
18815           if (generic_decl_parm)
18816             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
18817         }
18818
18819       /* Decide whether we need an unspecified_parameters DIE at the end.
18820          There are 2 more cases to do this for: 1) the ansi ... declaration -
18821          this is detectable when the end of the arg list is not a
18822          void_type_node 2) an unprototyped function declaration (not a
18823          definition).  This just means that we have no info about the
18824          parameters at all.  */
18825       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18826       if (fn_arg_types != NULL)
18827         {
18828           /* This is the prototyped case, check for....  */
18829           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18830             gen_unspecified_parameters_die (decl, subr_die);
18831         }
18832       else if (DECL_INITIAL (decl) == NULL_TREE)
18833         gen_unspecified_parameters_die (decl, subr_die);
18834     }
18835
18836   /* Output Dwarf info for all of the stuff within the body of the function
18837      (if it has one - it may be just a declaration).  */
18838   outer_scope = DECL_INITIAL (decl);
18839
18840   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18841      a function.  This BLOCK actually represents the outermost binding contour
18842      for the function, i.e. the contour in which the function's formal
18843      parameters and labels get declared. Curiously, it appears that the front
18844      end doesn't actually put the PARM_DECL nodes for the current function onto
18845      the BLOCK_VARS list for this outer scope, but are strung off of the
18846      DECL_ARGUMENTS list for the function instead.
18847
18848      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18849      the LABEL_DECL nodes for the function however, and we output DWARF info
18850      for those in decls_for_scope.  Just within the `outer_scope' there will be
18851      a BLOCK node representing the function's outermost pair of curly braces,
18852      and any blocks used for the base and member initializers of a C++
18853      constructor function.  */
18854   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18855     {
18856       /* Emit a DW_TAG_variable DIE for a named return value.  */
18857       if (DECL_NAME (DECL_RESULT (decl)))
18858         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18859
18860       current_function_has_inlines = 0;
18861       decls_for_scope (outer_scope, subr_die, 0);
18862
18863 #if 0 && defined (MIPS_DEBUGGING_INFO)
18864       if (current_function_has_inlines)
18865         {
18866           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18867           if (! comp_unit_has_inlines)
18868             {
18869               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18870               comp_unit_has_inlines = 1;
18871             }
18872         }
18873 #endif
18874     }
18875   /* Add the calling convention attribute if requested.  */
18876   add_calling_convention_attribute (subr_die, decl);
18877
18878 }
18879
18880 /* Returns a hash value for X (which really is a die_struct).  */
18881
18882 static hashval_t
18883 common_block_die_table_hash (const void *x)
18884 {
18885   const_dw_die_ref d = (const_dw_die_ref) x;
18886   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18887 }
18888
18889 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18890    as decl_id and die_parent of die_struct Y.  */
18891
18892 static int
18893 common_block_die_table_eq (const void *x, const void *y)
18894 {
18895   const_dw_die_ref d = (const_dw_die_ref) x;
18896   const_dw_die_ref e = (const_dw_die_ref) y;
18897   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18898 }
18899
18900 /* Generate a DIE to represent a declared data object.
18901    Either DECL or ORIGIN must be non-null.  */
18902
18903 static void
18904 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18905 {
18906   HOST_WIDE_INT off;
18907   tree com_decl;
18908   tree decl_or_origin = decl ? decl : origin;
18909   tree ultimate_origin;
18910   dw_die_ref var_die;
18911   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18912   dw_die_ref origin_die;
18913   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18914                       || class_or_namespace_scope_p (context_die));
18915   bool specialization_p = false;
18916
18917   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18918   if (decl || ultimate_origin)
18919     origin = ultimate_origin;
18920   com_decl = fortran_common (decl_or_origin, &off);
18921
18922   /* Symbol in common gets emitted as a child of the common block, in the form
18923      of a data member.  */
18924   if (com_decl)
18925     {
18926       dw_die_ref com_die;
18927       dw_loc_list_ref loc;
18928       die_node com_die_arg;
18929
18930       var_die = lookup_decl_die (decl_or_origin);
18931       if (var_die)
18932         {
18933           if (get_AT (var_die, DW_AT_location) == NULL)
18934             {
18935               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18936               if (loc)
18937                 {
18938                   if (off)
18939                     {
18940                       /* Optimize the common case.  */
18941                       if (single_element_loc_list_p (loc)
18942                           && loc->expr->dw_loc_opc == DW_OP_addr
18943                           && loc->expr->dw_loc_next == NULL
18944                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18945                              == SYMBOL_REF)
18946                         loc->expr->dw_loc_oprnd1.v.val_addr
18947                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18948                         else
18949                           loc_list_plus_const (loc, off);
18950                     }
18951                   add_AT_location_description (var_die, DW_AT_location, loc);
18952                   remove_AT (var_die, DW_AT_declaration);
18953                 }
18954             }
18955           return;
18956         }
18957
18958       if (common_block_die_table == NULL)
18959         common_block_die_table
18960           = htab_create_ggc (10, common_block_die_table_hash,
18961                              common_block_die_table_eq, NULL);
18962
18963       com_die_arg.decl_id = DECL_UID (com_decl);
18964       com_die_arg.die_parent = context_die;
18965       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18966       loc = loc_list_from_tree (com_decl, 2);
18967       if (com_die == NULL)
18968         {
18969           const char *cnam
18970             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18971           void **slot;
18972
18973           com_die = new_die (DW_TAG_common_block, context_die, decl);
18974           add_name_and_src_coords_attributes (com_die, com_decl);
18975           if (loc)
18976             {
18977               add_AT_location_description (com_die, DW_AT_location, loc);
18978               /* Avoid sharing the same loc descriptor between
18979                  DW_TAG_common_block and DW_TAG_variable.  */
18980               loc = loc_list_from_tree (com_decl, 2);
18981             }
18982           else if (DECL_EXTERNAL (decl))
18983             add_AT_flag (com_die, DW_AT_declaration, 1);
18984           add_pubname_string (cnam, com_die); /* ??? needed? */
18985           com_die->decl_id = DECL_UID (com_decl);
18986           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18987           *slot = (void *) com_die;
18988         }
18989       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18990         {
18991           add_AT_location_description (com_die, DW_AT_location, loc);
18992           loc = loc_list_from_tree (com_decl, 2);
18993           remove_AT (com_die, DW_AT_declaration);
18994         }
18995       var_die = new_die (DW_TAG_variable, com_die, decl);
18996       add_name_and_src_coords_attributes (var_die, decl);
18997       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18998                           TREE_THIS_VOLATILE (decl), context_die);
18999       add_AT_flag (var_die, DW_AT_external, 1);
19000       if (loc)
19001         {
19002           if (off)
19003             {
19004               /* Optimize the common case.  */
19005               if (single_element_loc_list_p (loc)
19006                   && loc->expr->dw_loc_opc == DW_OP_addr
19007                   && loc->expr->dw_loc_next == NULL
19008                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19009                 loc->expr->dw_loc_oprnd1.v.val_addr
19010                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19011               else
19012                 loc_list_plus_const (loc, off);
19013             }
19014           add_AT_location_description (var_die, DW_AT_location, loc);
19015         }
19016       else if (DECL_EXTERNAL (decl))
19017         add_AT_flag (var_die, DW_AT_declaration, 1);
19018       equate_decl_number_to_die (decl, var_die);
19019       return;
19020     }
19021
19022   /* If the compiler emitted a definition for the DECL declaration
19023      and if we already emitted a DIE for it, don't emit a second
19024      DIE for it again. Allow re-declarations of DECLs that are
19025      inside functions, though.  */
19026   if (old_die && declaration && !local_scope_p (context_die))
19027     return;
19028
19029   /* For static data members, the declaration in the class is supposed
19030      to have DW_TAG_member tag; the specification should still be
19031      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19032   if (declaration && class_scope_p (context_die))
19033     var_die = new_die (DW_TAG_member, context_die, decl);
19034   else
19035     var_die = new_die (DW_TAG_variable, context_die, decl);
19036
19037   origin_die = NULL;
19038   if (origin != NULL)
19039     origin_die = add_abstract_origin_attribute (var_die, origin);
19040
19041   /* Loop unrolling can create multiple blocks that refer to the same
19042      static variable, so we must test for the DW_AT_declaration flag.
19043
19044      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19045      copy decls and set the DECL_ABSTRACT flag on them instead of
19046      sharing them.
19047
19048      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19049
19050      ??? The declare_in_namespace support causes us to get two DIEs for one
19051      variable, both of which are declarations.  We want to avoid considering
19052      one to be a specification, so we must test that this DIE is not a
19053      declaration.  */
19054   else if (old_die && TREE_STATIC (decl) && ! declaration
19055            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19056     {
19057       /* This is a definition of a C++ class level static.  */
19058       add_AT_specification (var_die, old_die);
19059       specialization_p = true;
19060       if (DECL_NAME (decl))
19061         {
19062           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19063           struct dwarf_file_data * file_index = lookup_filename (s.file);
19064
19065           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19066             add_AT_file (var_die, DW_AT_decl_file, file_index);
19067
19068           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19069             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19070
19071           if (old_die->die_tag == DW_TAG_member)
19072             add_linkage_name (var_die, decl);
19073         }
19074     }
19075   else
19076     add_name_and_src_coords_attributes (var_die, decl);
19077
19078   if ((origin == NULL && !specialization_p)
19079       || (origin != NULL
19080           && !DECL_ABSTRACT (decl_or_origin)
19081           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19082                                        decl_function_context
19083                                                         (decl_or_origin))))
19084     {
19085       tree type = TREE_TYPE (decl_or_origin);
19086
19087       if (decl_by_reference_p (decl_or_origin))
19088         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19089       else
19090         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19091                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19092     }
19093
19094   if (origin == NULL && !specialization_p)
19095     {
19096       if (TREE_PUBLIC (decl))
19097         add_AT_flag (var_die, DW_AT_external, 1);
19098
19099       if (DECL_ARTIFICIAL (decl))
19100         add_AT_flag (var_die, DW_AT_artificial, 1);
19101
19102       add_accessibility_attribute (var_die, decl);
19103     }
19104
19105   if (declaration)
19106     add_AT_flag (var_die, DW_AT_declaration, 1);
19107
19108   if (decl && (DECL_ABSTRACT (decl) || declaration))
19109     equate_decl_number_to_die (decl, var_die);
19110
19111   if (! declaration
19112       && (! DECL_ABSTRACT (decl_or_origin)
19113           /* Local static vars are shared between all clones/inlines,
19114              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19115              already set.  */
19116           || (TREE_CODE (decl_or_origin) == VAR_DECL
19117               && TREE_STATIC (decl_or_origin)
19118               && DECL_RTL_SET_P (decl_or_origin)))
19119       /* When abstract origin already has DW_AT_location attribute, no need
19120          to add it again.  */
19121       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19122     {
19123       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19124           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19125         defer_location (decl_or_origin, var_die);
19126       else
19127         add_location_or_const_value_attribute (var_die,
19128                                                decl_or_origin,
19129                                                DW_AT_location);
19130       add_pubname (decl_or_origin, var_die);
19131     }
19132   else
19133     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19134 }
19135
19136 /* Generate a DIE to represent a named constant.  */
19137
19138 static void
19139 gen_const_die (tree decl, dw_die_ref context_die)
19140 {
19141   dw_die_ref const_die;
19142   tree type = TREE_TYPE (decl);
19143
19144   const_die = new_die (DW_TAG_constant, context_die, decl);
19145   add_name_and_src_coords_attributes (const_die, decl);
19146   add_type_attribute (const_die, type, 1, 0, context_die);
19147   if (TREE_PUBLIC (decl))
19148     add_AT_flag (const_die, DW_AT_external, 1);
19149   if (DECL_ARTIFICIAL (decl))
19150     add_AT_flag (const_die, DW_AT_artificial, 1);
19151   tree_add_const_value_attribute_for_decl (const_die, decl);
19152 }
19153
19154 /* Generate a DIE to represent a label identifier.  */
19155
19156 static void
19157 gen_label_die (tree decl, dw_die_ref context_die)
19158 {
19159   tree origin = decl_ultimate_origin (decl);
19160   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19161   rtx insn;
19162   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19163
19164   if (origin != NULL)
19165     add_abstract_origin_attribute (lbl_die, origin);
19166   else
19167     add_name_and_src_coords_attributes (lbl_die, decl);
19168
19169   if (DECL_ABSTRACT (decl))
19170     equate_decl_number_to_die (decl, lbl_die);
19171   else
19172     {
19173       insn = DECL_RTL_IF_SET (decl);
19174
19175       /* Deleted labels are programmer specified labels which have been
19176          eliminated because of various optimizations.  We still emit them
19177          here so that it is possible to put breakpoints on them.  */
19178       if (insn
19179           && (LABEL_P (insn)
19180               || ((NOTE_P (insn)
19181                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19182         {
19183           /* When optimization is enabled (via -O) some parts of the compiler
19184              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19185              represent source-level labels which were explicitly declared by
19186              the user.  This really shouldn't be happening though, so catch
19187              it if it ever does happen.  */
19188           gcc_assert (!INSN_DELETED_P (insn));
19189
19190           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19191           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19192         }
19193     }
19194 }
19195
19196 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19197    attributes to the DIE for a block STMT, to describe where the inlined
19198    function was called from.  This is similar to add_src_coords_attributes.  */
19199
19200 static inline void
19201 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19202 {
19203   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19204
19205   if (dwarf_version >= 3 || !dwarf_strict)
19206     {
19207       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19208       add_AT_unsigned (die, DW_AT_call_line, s.line);
19209     }
19210 }
19211
19212
19213 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19214    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19215
19216 static inline void
19217 add_high_low_attributes (tree stmt, dw_die_ref die)
19218 {
19219   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19220
19221   if (BLOCK_FRAGMENT_CHAIN (stmt)
19222       && (dwarf_version >= 3 || !dwarf_strict))
19223     {
19224       tree chain;
19225
19226       if (inlined_function_outer_scope_p (stmt))
19227         {
19228           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19229                                        BLOCK_NUMBER (stmt));
19230           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19231         }
19232
19233       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19234
19235       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19236       do
19237         {
19238           add_ranges (chain);
19239           chain = BLOCK_FRAGMENT_CHAIN (chain);
19240         }
19241       while (chain);
19242       add_ranges (NULL);
19243     }
19244   else
19245     {
19246       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19247                                    BLOCK_NUMBER (stmt));
19248       add_AT_lbl_id (die, DW_AT_low_pc, label);
19249       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19250                                    BLOCK_NUMBER (stmt));
19251       add_AT_lbl_id (die, DW_AT_high_pc, label);
19252     }
19253 }
19254
19255 /* Generate a DIE for a lexical block.  */
19256
19257 static void
19258 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19259 {
19260   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19261
19262   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19263     add_high_low_attributes (stmt, stmt_die);
19264
19265   decls_for_scope (stmt, stmt_die, depth);
19266 }
19267
19268 /* Generate a DIE for an inlined subprogram.  */
19269
19270 static void
19271 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19272 {
19273   tree decl;
19274
19275   /* The instance of function that is effectively being inlined shall not
19276      be abstract.  */
19277   gcc_assert (! BLOCK_ABSTRACT (stmt));
19278
19279   decl = block_ultimate_origin (stmt);
19280
19281   /* Emit info for the abstract instance first, if we haven't yet.  We
19282      must emit this even if the block is abstract, otherwise when we
19283      emit the block below (or elsewhere), we may end up trying to emit
19284      a die whose origin die hasn't been emitted, and crashing.  */
19285   dwarf2out_abstract_function (decl);
19286
19287   if (! BLOCK_ABSTRACT (stmt))
19288     {
19289       dw_die_ref subr_die
19290         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19291
19292       add_abstract_origin_attribute (subr_die, decl);
19293       if (TREE_ASM_WRITTEN (stmt))
19294         add_high_low_attributes (stmt, subr_die);
19295       add_call_src_coords_attributes (stmt, subr_die);
19296
19297       decls_for_scope (stmt, subr_die, depth);
19298       current_function_has_inlines = 1;
19299     }
19300 }
19301
19302 /* Generate a DIE for a field in a record, or structure.  */
19303
19304 static void
19305 gen_field_die (tree decl, dw_die_ref context_die)
19306 {
19307   dw_die_ref decl_die;
19308
19309   if (TREE_TYPE (decl) == error_mark_node)
19310     return;
19311
19312   decl_die = new_die (DW_TAG_member, context_die, decl);
19313   add_name_and_src_coords_attributes (decl_die, decl);
19314   add_type_attribute (decl_die, member_declared_type (decl),
19315                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19316                       context_die);
19317
19318   if (DECL_BIT_FIELD_TYPE (decl))
19319     {
19320       add_byte_size_attribute (decl_die, decl);
19321       add_bit_size_attribute (decl_die, decl);
19322       add_bit_offset_attribute (decl_die, decl);
19323     }
19324
19325   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19326     add_data_member_location_attribute (decl_die, decl);
19327
19328   if (DECL_ARTIFICIAL (decl))
19329     add_AT_flag (decl_die, DW_AT_artificial, 1);
19330
19331   add_accessibility_attribute (decl_die, decl);
19332
19333   /* Equate decl number to die, so that we can look up this decl later on.  */
19334   equate_decl_number_to_die (decl, decl_die);
19335 }
19336
19337 #if 0
19338 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19339    Use modified_type_die instead.
19340    We keep this code here just in case these types of DIEs may be needed to
19341    represent certain things in other languages (e.g. Pascal) someday.  */
19342
19343 static void
19344 gen_pointer_type_die (tree type, dw_die_ref context_die)
19345 {
19346   dw_die_ref ptr_die
19347     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19348
19349   equate_type_number_to_die (type, ptr_die);
19350   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19351   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19352 }
19353
19354 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19355    Use modified_type_die instead.
19356    We keep this code here just in case these types of DIEs may be needed to
19357    represent certain things in other languages (e.g. Pascal) someday.  */
19358
19359 static void
19360 gen_reference_type_die (tree type, dw_die_ref context_die)
19361 {
19362   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19363
19364   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19365     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19366   else
19367     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19368
19369   equate_type_number_to_die (type, ref_die);
19370   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19371   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19372 }
19373 #endif
19374
19375 /* Generate a DIE for a pointer to a member type.  */
19376
19377 static void
19378 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19379 {
19380   dw_die_ref ptr_die
19381     = new_die (DW_TAG_ptr_to_member_type,
19382                scope_die_for (type, context_die), type);
19383
19384   equate_type_number_to_die (type, ptr_die);
19385   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19386                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19387   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19388 }
19389
19390 /* Generate the DIE for the compilation unit.  */
19391
19392 static dw_die_ref
19393 gen_compile_unit_die (const char *filename)
19394 {
19395   dw_die_ref die;
19396   char producer[250];
19397   const char *language_string = lang_hooks.name;
19398   int language;
19399
19400   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19401
19402   if (filename)
19403     {
19404       add_name_attribute (die, filename);
19405       /* Don't add cwd for <built-in>.  */
19406       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19407         add_comp_dir_attribute (die);
19408     }
19409
19410   sprintf (producer, "%s %s", language_string, version_string);
19411
19412 #ifdef MIPS_DEBUGGING_INFO
19413   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19414      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19415      not appear in the producer string, the debugger reaches the conclusion
19416      that the object file is stripped and has no debugging information.
19417      To get the MIPS/SGI debugger to believe that there is debugging
19418      information in the object file, we add a -g to the producer string.  */
19419   if (debug_info_level > DINFO_LEVEL_TERSE)
19420     strcat (producer, " -g");
19421 #endif
19422
19423   add_AT_string (die, DW_AT_producer, producer);
19424
19425   language = DW_LANG_C89;
19426   if (strcmp (language_string, "GNU C++") == 0)
19427     language = DW_LANG_C_plus_plus;
19428   else if (strcmp (language_string, "GNU F77") == 0)
19429     language = DW_LANG_Fortran77;
19430   else if (strcmp (language_string, "GNU Pascal") == 0)
19431     language = DW_LANG_Pascal83;
19432   else if (dwarf_version >= 3 || !dwarf_strict)
19433     {
19434       if (strcmp (language_string, "GNU Ada") == 0)
19435         language = DW_LANG_Ada95;
19436       else if (strcmp (language_string, "GNU Fortran") == 0)
19437         language = DW_LANG_Fortran95;
19438       else if (strcmp (language_string, "GNU Java") == 0)
19439         language = DW_LANG_Java;
19440       else if (strcmp (language_string, "GNU Objective-C") == 0)
19441         language = DW_LANG_ObjC;
19442       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19443         language = DW_LANG_ObjC_plus_plus;
19444     }
19445
19446   add_AT_unsigned (die, DW_AT_language, language);
19447
19448   switch (language)
19449     {
19450     case DW_LANG_Fortran77:
19451     case DW_LANG_Fortran90:
19452     case DW_LANG_Fortran95:
19453       /* Fortran has case insensitive identifiers and the front-end
19454          lowercases everything.  */
19455       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19456       break;
19457     default:
19458       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19459       break;
19460     }
19461   return die;
19462 }
19463
19464 /* Generate the DIE for a base class.  */
19465
19466 static void
19467 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19468 {
19469   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19470
19471   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19472   add_data_member_location_attribute (die, binfo);
19473
19474   if (BINFO_VIRTUAL_P (binfo))
19475     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19476
19477   if (access == access_public_node)
19478     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19479   else if (access == access_protected_node)
19480     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19481 }
19482
19483 /* Generate a DIE for a class member.  */
19484
19485 static void
19486 gen_member_die (tree type, dw_die_ref context_die)
19487 {
19488   tree member;
19489   tree binfo = TYPE_BINFO (type);
19490   dw_die_ref child;
19491
19492   /* If this is not an incomplete type, output descriptions of each of its
19493      members. Note that as we output the DIEs necessary to represent the
19494      members of this record or union type, we will also be trying to output
19495      DIEs to represent the *types* of those members. However the `type'
19496      function (above) will specifically avoid generating type DIEs for member
19497      types *within* the list of member DIEs for this (containing) type except
19498      for those types (of members) which are explicitly marked as also being
19499      members of this (containing) type themselves.  The g++ front- end can
19500      force any given type to be treated as a member of some other (containing)
19501      type by setting the TYPE_CONTEXT of the given (member) type to point to
19502      the TREE node representing the appropriate (containing) type.  */
19503
19504   /* First output info about the base classes.  */
19505   if (binfo)
19506     {
19507       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19508       int i;
19509       tree base;
19510
19511       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19512         gen_inheritance_die (base,
19513                              (accesses ? VEC_index (tree, accesses, i)
19514                               : access_public_node), context_die);
19515     }
19516
19517   /* Now output info about the data members and type members.  */
19518   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
19519     {
19520       /* If we thought we were generating minimal debug info for TYPE
19521          and then changed our minds, some of the member declarations
19522          may have already been defined.  Don't define them again, but
19523          do put them in the right order.  */
19524
19525       child = lookup_decl_die (member);
19526       if (child)
19527         splice_child_die (context_die, child);
19528       else
19529         gen_decl_die (member, NULL, context_die);
19530     }
19531
19532   /* Now output info about the function members (if any).  */
19533   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
19534     {
19535       /* Don't include clones in the member list.  */
19536       if (DECL_ABSTRACT_ORIGIN (member))
19537         continue;
19538
19539       child = lookup_decl_die (member);
19540       if (child)
19541         splice_child_die (context_die, child);
19542       else
19543         gen_decl_die (member, NULL, context_die);
19544     }
19545 }
19546
19547 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19548    is set, we pretend that the type was never defined, so we only get the
19549    member DIEs needed by later specification DIEs.  */
19550
19551 static void
19552 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19553                                 enum debug_info_usage usage)
19554 {
19555   dw_die_ref type_die = lookup_type_die (type);
19556   dw_die_ref scope_die = 0;
19557   int nested = 0;
19558   int complete = (TYPE_SIZE (type)
19559                   && (! TYPE_STUB_DECL (type)
19560                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19561   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19562   complete = complete && should_emit_struct_debug (type, usage);
19563
19564   if (type_die && ! complete)
19565     return;
19566
19567   if (TYPE_CONTEXT (type) != NULL_TREE
19568       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19569           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19570     nested = 1;
19571
19572   scope_die = scope_die_for (type, context_die);
19573
19574   if (! type_die || (nested && scope_die == comp_unit_die))
19575     /* First occurrence of type or toplevel definition of nested class.  */
19576     {
19577       dw_die_ref old_die = type_die;
19578
19579       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19580                           ? record_type_tag (type) : DW_TAG_union_type,
19581                           scope_die, type);
19582       equate_type_number_to_die (type, type_die);
19583       if (old_die)
19584         add_AT_specification (type_die, old_die);
19585       else
19586         add_name_attribute (type_die, type_tag (type));
19587     }
19588   else
19589     remove_AT (type_die, DW_AT_declaration);
19590
19591   /* Generate child dies for template paramaters.  */
19592   if (debug_info_level > DINFO_LEVEL_TERSE
19593       && COMPLETE_TYPE_P (type))
19594     gen_generic_params_dies (type);
19595
19596   /* If this type has been completed, then give it a byte_size attribute and
19597      then give a list of members.  */
19598   if (complete && !ns_decl)
19599     {
19600       /* Prevent infinite recursion in cases where the type of some member of
19601          this type is expressed in terms of this type itself.  */
19602       TREE_ASM_WRITTEN (type) = 1;
19603       add_byte_size_attribute (type_die, type);
19604       if (TYPE_STUB_DECL (type) != NULL_TREE)
19605         {
19606           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19607           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19608         }
19609
19610       /* If the first reference to this type was as the return type of an
19611          inline function, then it may not have a parent.  Fix this now.  */
19612       if (type_die->die_parent == NULL)
19613         add_child_die (scope_die, type_die);
19614
19615       push_decl_scope (type);
19616       gen_member_die (type, type_die);
19617       pop_decl_scope ();
19618
19619       /* GNU extension: Record what type our vtable lives in.  */
19620       if (TYPE_VFIELD (type))
19621         {
19622           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19623
19624           gen_type_die (vtype, context_die);
19625           add_AT_die_ref (type_die, DW_AT_containing_type,
19626                           lookup_type_die (vtype));
19627         }
19628     }
19629   else
19630     {
19631       add_AT_flag (type_die, DW_AT_declaration, 1);
19632
19633       /* We don't need to do this for function-local types.  */
19634       if (TYPE_STUB_DECL (type)
19635           && ! decl_function_context (TYPE_STUB_DECL (type)))
19636         VEC_safe_push (tree, gc, incomplete_types, type);
19637     }
19638
19639   if (get_AT (type_die, DW_AT_name))
19640     add_pubtype (type, type_die);
19641 }
19642
19643 /* Generate a DIE for a subroutine _type_.  */
19644
19645 static void
19646 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19647 {
19648   tree return_type = TREE_TYPE (type);
19649   dw_die_ref subr_die
19650     = new_die (DW_TAG_subroutine_type,
19651                scope_die_for (type, context_die), type);
19652
19653   equate_type_number_to_die (type, subr_die);
19654   add_prototyped_attribute (subr_die, type);
19655   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19656   gen_formal_types_die (type, subr_die);
19657
19658   if (get_AT (subr_die, DW_AT_name))
19659     add_pubtype (type, subr_die);
19660 }
19661
19662 /* Generate a DIE for a type definition.  */
19663
19664 static void
19665 gen_typedef_die (tree decl, dw_die_ref context_die)
19666 {
19667   dw_die_ref type_die;
19668   tree origin;
19669
19670   if (TREE_ASM_WRITTEN (decl))
19671     return;
19672
19673   TREE_ASM_WRITTEN (decl) = 1;
19674   type_die = new_die (DW_TAG_typedef, context_die, decl);
19675   origin = decl_ultimate_origin (decl);
19676   if (origin != NULL)
19677     add_abstract_origin_attribute (type_die, origin);
19678   else
19679     {
19680       tree type;
19681
19682       add_name_and_src_coords_attributes (type_die, decl);
19683       if (DECL_ORIGINAL_TYPE (decl))
19684         {
19685           type = DECL_ORIGINAL_TYPE (decl);
19686
19687           gcc_assert (type != TREE_TYPE (decl));
19688           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19689         }
19690       else
19691         {
19692           type = TREE_TYPE (decl);
19693
19694           if (is_naming_typedef_decl (TYPE_NAME (type)))
19695             /* 
19696                Here, we are in the case of decl being a typedef naming
19697                an anonymous type, e.g:
19698                      typedef struct {...} foo;
19699                In that case TREE_TYPE (decl) is not a typedef variant
19700                type and TYPE_NAME of the anonymous type is set to the
19701                TYPE_DECL of the typedef. This construct is emitted by
19702                the C++ FE.
19703
19704                TYPE is the anonymous struct named by the typedef
19705                DECL. As we need the DW_AT_type attribute of the
19706                DW_TAG_typedef to point to the DIE of TYPE, let's
19707                generate that DIE right away. add_type_attribute
19708                called below will then pick (via lookup_type_die) that
19709                anonymous struct DIE.  */
19710             gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19711         }
19712
19713       add_type_attribute (type_die, type, TREE_READONLY (decl),
19714                           TREE_THIS_VOLATILE (decl), context_die);
19715
19716       if (is_naming_typedef_decl (decl))
19717         /* We want that all subsequent calls to lookup_type_die with
19718            TYPE in argument yield the DW_TAG_typedef we have just
19719            created.  */
19720         equate_type_number_to_die (type, type_die);
19721
19722       add_accessibility_attribute (type_die, decl);
19723     }
19724
19725   if (DECL_ABSTRACT (decl))
19726     equate_decl_number_to_die (decl, type_die);
19727
19728   if (get_AT (type_die, DW_AT_name))
19729     add_pubtype (decl, type_die);
19730 }
19731
19732 /* Generate a DIE for a struct, class, enum or union type.  */
19733
19734 static void
19735 gen_tagged_type_die (tree type,
19736                      dw_die_ref context_die,
19737                      enum debug_info_usage usage)
19738 {
19739   int need_pop;
19740
19741   if (type == NULL_TREE
19742       || !is_tagged_type (type))
19743     return;
19744
19745   /* If this is a nested type whose containing class hasn't been written
19746      out yet, writing it out will cover this one, too.  This does not apply
19747      to instantiations of member class templates; they need to be added to
19748      the containing class as they are generated.  FIXME: This hurts the
19749      idea of combining type decls from multiple TUs, since we can't predict
19750      what set of template instantiations we'll get.  */
19751   if (TYPE_CONTEXT (type)
19752       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19753       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19754     {
19755       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19756
19757       if (TREE_ASM_WRITTEN (type))
19758         return;
19759
19760       /* If that failed, attach ourselves to the stub.  */
19761       push_decl_scope (TYPE_CONTEXT (type));
19762       context_die = lookup_type_die (TYPE_CONTEXT (type));
19763       need_pop = 1;
19764     }
19765   else if (TYPE_CONTEXT (type) != NULL_TREE
19766            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19767     {
19768       /* If this type is local to a function that hasn't been written
19769          out yet, use a NULL context for now; it will be fixed up in
19770          decls_for_scope.  */
19771       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19772       need_pop = 0;
19773     }
19774   else
19775     {
19776       context_die = declare_in_namespace (type, context_die);
19777       need_pop = 0;
19778     }
19779
19780   if (TREE_CODE (type) == ENUMERAL_TYPE)
19781     {
19782       /* This might have been written out by the call to
19783          declare_in_namespace.  */
19784       if (!TREE_ASM_WRITTEN (type))
19785         gen_enumeration_type_die (type, context_die);
19786     }
19787   else
19788     gen_struct_or_union_type_die (type, context_die, usage);
19789
19790   if (need_pop)
19791     pop_decl_scope ();
19792
19793   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19794      it up if it is ever completed.  gen_*_type_die will set it for us
19795      when appropriate.  */
19796 }
19797
19798 /* Generate a type description DIE.  */
19799
19800 static void
19801 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19802                                 enum debug_info_usage usage)
19803 {
19804   struct array_descr_info info;
19805
19806   if (type == NULL_TREE || type == error_mark_node)
19807     return;
19808
19809   /* If TYPE is a typedef type variant, let's generate debug info
19810      for the parent typedef which TYPE is a type of.  */
19811   if (typedef_variant_p (type))
19812     {
19813       if (TREE_ASM_WRITTEN (type))
19814         return;
19815
19816       /* Prevent broken recursion; we can't hand off to the same type.  */
19817       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19818
19819       /* Use the DIE of the containing namespace as the parent DIE of
19820          the type description DIE we want to generate.  */
19821       if (DECL_CONTEXT (TYPE_NAME (type))
19822           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19823         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19824
19825       TREE_ASM_WRITTEN (type) = 1;
19826
19827       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19828       return;
19829     }
19830
19831   /* If type is an anonymous tagged type named by a typedef, let's
19832      generate debug info for the typedef.  */
19833   if (is_naming_typedef_decl (TYPE_NAME (type)))
19834     {
19835       /* Use the DIE of the containing namespace as the parent DIE of
19836          the type description DIE we want to generate.  */
19837       if (DECL_CONTEXT (TYPE_NAME (type))
19838           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19839         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19840       
19841       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19842       return;
19843     }
19844
19845   /* If this is an array type with hidden descriptor, handle it first.  */
19846   if (!TREE_ASM_WRITTEN (type)
19847       && lang_hooks.types.get_array_descr_info
19848       && lang_hooks.types.get_array_descr_info (type, &info)
19849       && (dwarf_version >= 3 || !dwarf_strict))
19850     {
19851       gen_descr_array_type_die (type, &info, context_die);
19852       TREE_ASM_WRITTEN (type) = 1;
19853       return;
19854     }
19855
19856   /* We are going to output a DIE to represent the unqualified version
19857      of this type (i.e. without any const or volatile qualifiers) so
19858      get the main variant (i.e. the unqualified version) of this type
19859      now.  (Vectors are special because the debugging info is in the
19860      cloned type itself).  */
19861   if (TREE_CODE (type) != VECTOR_TYPE)
19862     type = type_main_variant (type);
19863
19864   if (TREE_ASM_WRITTEN (type))
19865     return;
19866
19867   switch (TREE_CODE (type))
19868     {
19869     case ERROR_MARK:
19870       break;
19871
19872     case POINTER_TYPE:
19873     case REFERENCE_TYPE:
19874       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19875          ensures that the gen_type_die recursion will terminate even if the
19876          type is recursive.  Recursive types are possible in Ada.  */
19877       /* ??? We could perhaps do this for all types before the switch
19878          statement.  */
19879       TREE_ASM_WRITTEN (type) = 1;
19880
19881       /* For these types, all that is required is that we output a DIE (or a
19882          set of DIEs) to represent the "basis" type.  */
19883       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19884                                 DINFO_USAGE_IND_USE);
19885       break;
19886
19887     case OFFSET_TYPE:
19888       /* This code is used for C++ pointer-to-data-member types.
19889          Output a description of the relevant class type.  */
19890       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19891                                         DINFO_USAGE_IND_USE);
19892
19893       /* Output a description of the type of the object pointed to.  */
19894       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19895                                         DINFO_USAGE_IND_USE);
19896
19897       /* Now output a DIE to represent this pointer-to-data-member type
19898          itself.  */
19899       gen_ptr_to_mbr_type_die (type, context_die);
19900       break;
19901
19902     case FUNCTION_TYPE:
19903       /* Force out return type (in case it wasn't forced out already).  */
19904       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19905                                         DINFO_USAGE_DIR_USE);
19906       gen_subroutine_type_die (type, context_die);
19907       break;
19908
19909     case METHOD_TYPE:
19910       /* Force out return type (in case it wasn't forced out already).  */
19911       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19912                                         DINFO_USAGE_DIR_USE);
19913       gen_subroutine_type_die (type, context_die);
19914       break;
19915
19916     case ARRAY_TYPE:
19917       gen_array_type_die (type, context_die);
19918       break;
19919
19920     case VECTOR_TYPE:
19921       gen_array_type_die (type, context_die);
19922       break;
19923
19924     case ENUMERAL_TYPE:
19925     case RECORD_TYPE:
19926     case UNION_TYPE:
19927     case QUAL_UNION_TYPE:
19928       gen_tagged_type_die (type, context_die, usage);
19929       return;
19930
19931     case VOID_TYPE:
19932     case INTEGER_TYPE:
19933     case REAL_TYPE:
19934     case FIXED_POINT_TYPE:
19935     case COMPLEX_TYPE:
19936     case BOOLEAN_TYPE:
19937       /* No DIEs needed for fundamental types.  */
19938       break;
19939
19940     case LANG_TYPE:
19941       /* Just use DW_TAG_unspecified_type.  */
19942       {
19943         dw_die_ref type_die = lookup_type_die (type);
19944         if (type_die == NULL)
19945           {
19946             tree name = TYPE_NAME (type);
19947             if (TREE_CODE (name) == TYPE_DECL)
19948               name = DECL_NAME (name);
19949             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
19950             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19951             equate_type_number_to_die (type, type_die);
19952           }
19953       }
19954       break;
19955
19956     default:
19957       gcc_unreachable ();
19958     }
19959
19960   TREE_ASM_WRITTEN (type) = 1;
19961 }
19962
19963 static void
19964 gen_type_die (tree type, dw_die_ref context_die)
19965 {
19966   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19967 }
19968
19969 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19970    things which are local to the given block.  */
19971
19972 static void
19973 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19974 {
19975   int must_output_die = 0;
19976   bool inlined_func;
19977
19978   /* Ignore blocks that are NULL.  */
19979   if (stmt == NULL_TREE)
19980     return;
19981
19982   inlined_func = inlined_function_outer_scope_p (stmt);
19983
19984   /* If the block is one fragment of a non-contiguous block, do not
19985      process the variables, since they will have been done by the
19986      origin block.  Do process subblocks.  */
19987   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19988     {
19989       tree sub;
19990
19991       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19992         gen_block_die (sub, context_die, depth + 1);
19993
19994       return;
19995     }
19996
19997   /* Determine if we need to output any Dwarf DIEs at all to represent this
19998      block.  */
19999   if (inlined_func)
20000     /* The outer scopes for inlinings *must* always be represented.  We
20001        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20002     must_output_die = 1;
20003   else
20004     {
20005       /* Determine if this block directly contains any "significant"
20006          local declarations which we will need to output DIEs for.  */
20007       if (debug_info_level > DINFO_LEVEL_TERSE)
20008         /* We are not in terse mode so *any* local declaration counts
20009            as being a "significant" one.  */
20010         must_output_die = ((BLOCK_VARS (stmt) != NULL
20011                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20012                            && (TREE_USED (stmt)
20013                                || TREE_ASM_WRITTEN (stmt)
20014                                || BLOCK_ABSTRACT (stmt)));
20015       else if ((TREE_USED (stmt)
20016                 || TREE_ASM_WRITTEN (stmt)
20017                 || BLOCK_ABSTRACT (stmt))
20018                && !dwarf2out_ignore_block (stmt))
20019         must_output_die = 1;
20020     }
20021
20022   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20023      DIE for any block which contains no significant local declarations at
20024      all.  Rather, in such cases we just call `decls_for_scope' so that any
20025      needed Dwarf info for any sub-blocks will get properly generated. Note
20026      that in terse mode, our definition of what constitutes a "significant"
20027      local declaration gets restricted to include only inlined function
20028      instances and local (nested) function definitions.  */
20029   if (must_output_die)
20030     {
20031       if (inlined_func)
20032         {
20033           /* If STMT block is abstract, that means we have been called
20034              indirectly from dwarf2out_abstract_function.
20035              That function rightfully marks the descendent blocks (of
20036              the abstract function it is dealing with) as being abstract,
20037              precisely to prevent us from emitting any
20038              DW_TAG_inlined_subroutine DIE as a descendent
20039              of an abstract function instance. So in that case, we should
20040              not call gen_inlined_subroutine_die.
20041
20042              Later though, when cgraph asks dwarf2out to emit info
20043              for the concrete instance of the function decl into which
20044              the concrete instance of STMT got inlined, the later will lead
20045              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20046           if (! BLOCK_ABSTRACT (stmt))
20047             gen_inlined_subroutine_die (stmt, context_die, depth);
20048         }
20049       else
20050         gen_lexical_block_die (stmt, context_die, depth);
20051     }
20052   else
20053     decls_for_scope (stmt, context_die, depth);
20054 }
20055
20056 /* Process variable DECL (or variable with origin ORIGIN) within
20057    block STMT and add it to CONTEXT_DIE.  */
20058 static void
20059 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20060 {
20061   dw_die_ref die;
20062   tree decl_or_origin = decl ? decl : origin;
20063
20064   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20065     die = lookup_decl_die (decl_or_origin);
20066   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20067            && TYPE_DECL_IS_STUB (decl_or_origin))
20068     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20069   else
20070     die = NULL;
20071
20072   if (die != NULL && die->die_parent == NULL)
20073     add_child_die (context_die, die);
20074   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20075     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20076                                          stmt, context_die);
20077   else
20078     gen_decl_die (decl, origin, context_die);
20079 }
20080
20081 /* Generate all of the decls declared within a given scope and (recursively)
20082    all of its sub-blocks.  */
20083
20084 static void
20085 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20086 {
20087   tree decl;
20088   unsigned int i;
20089   tree subblocks;
20090
20091   /* Ignore NULL blocks.  */
20092   if (stmt == NULL_TREE)
20093     return;
20094
20095   /* Output the DIEs to represent all of the data objects and typedefs
20096      declared directly within this block but not within any nested
20097      sub-blocks.  Also, nested function and tag DIEs have been
20098      generated with a parent of NULL; fix that up now.  */
20099   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
20100     process_scope_var (stmt, decl, NULL_TREE, context_die);
20101   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20102     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20103                        context_die);
20104
20105   /* If we're at -g1, we're not interested in subblocks.  */
20106   if (debug_info_level <= DINFO_LEVEL_TERSE)
20107     return;
20108
20109   /* Output the DIEs to represent all sub-blocks (and the items declared
20110      therein) of this block.  */
20111   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20112        subblocks != NULL;
20113        subblocks = BLOCK_CHAIN (subblocks))
20114     gen_block_die (subblocks, context_die, depth + 1);
20115 }
20116
20117 /* Is this a typedef we can avoid emitting?  */
20118
20119 static inline int
20120 is_redundant_typedef (const_tree decl)
20121 {
20122   if (TYPE_DECL_IS_STUB (decl))
20123     return 1;
20124
20125   if (DECL_ARTIFICIAL (decl)
20126       && DECL_CONTEXT (decl)
20127       && is_tagged_type (DECL_CONTEXT (decl))
20128       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20129       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20130     /* Also ignore the artificial member typedef for the class name.  */
20131     return 1;
20132
20133   return 0;
20134 }
20135
20136 /* Return TRUE if TYPE is a typedef that names a type for linkage
20137    purposes. This kind of typedefs is produced by the C++ FE for
20138    constructs like:
20139
20140    typedef struct {...} foo;
20141
20142    In that case, there is no typedef variant type produced for foo.
20143    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20144    struct type.  */
20145
20146 static bool
20147 is_naming_typedef_decl (const_tree decl)
20148 {
20149   if (decl == NULL_TREE
20150       || TREE_CODE (decl) != TYPE_DECL
20151       || !is_tagged_type (TREE_TYPE (decl))
20152       || DECL_IS_BUILTIN (decl)
20153       || is_redundant_typedef (decl)
20154       /* It looks like Ada produces TYPE_DECLs that are very similar
20155          to C++ naming typedefs but that have different
20156          semantics. Let's be specific to c++ for now.  */
20157       || !is_cxx ())
20158     return FALSE;
20159
20160   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20161           && TYPE_NAME (TREE_TYPE (decl)) == decl
20162           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20163               != TYPE_NAME (TREE_TYPE (decl))));
20164 }
20165
20166 /* Returns the DIE for a context.  */
20167
20168 static inline dw_die_ref
20169 get_context_die (tree context)
20170 {
20171   if (context)
20172     {
20173       /* Find die that represents this context.  */
20174       if (TYPE_P (context))
20175         return force_type_die (TYPE_MAIN_VARIANT (context));
20176       else
20177         return force_decl_die (context);
20178     }
20179   return comp_unit_die;
20180 }
20181
20182 /* Returns the DIE for decl.  A DIE will always be returned.  */
20183
20184 static dw_die_ref
20185 force_decl_die (tree decl)
20186 {
20187   dw_die_ref decl_die;
20188   unsigned saved_external_flag;
20189   tree save_fn = NULL_TREE;
20190   decl_die = lookup_decl_die (decl);
20191   if (!decl_die)
20192     {
20193       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20194
20195       decl_die = lookup_decl_die (decl);
20196       if (decl_die)
20197         return decl_die;
20198
20199       switch (TREE_CODE (decl))
20200         {
20201         case FUNCTION_DECL:
20202           /* Clear current_function_decl, so that gen_subprogram_die thinks
20203              that this is a declaration. At this point, we just want to force
20204              declaration die.  */
20205           save_fn = current_function_decl;
20206           current_function_decl = NULL_TREE;
20207           gen_subprogram_die (decl, context_die);
20208           current_function_decl = save_fn;
20209           break;
20210
20211         case VAR_DECL:
20212           /* Set external flag to force declaration die. Restore it after
20213            gen_decl_die() call.  */
20214           saved_external_flag = DECL_EXTERNAL (decl);
20215           DECL_EXTERNAL (decl) = 1;
20216           gen_decl_die (decl, NULL, context_die);
20217           DECL_EXTERNAL (decl) = saved_external_flag;
20218           break;
20219
20220         case NAMESPACE_DECL:
20221           if (dwarf_version >= 3 || !dwarf_strict)
20222             dwarf2out_decl (decl);
20223           else
20224             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20225             decl_die = comp_unit_die;
20226           break;
20227
20228         default:
20229           gcc_unreachable ();
20230         }
20231
20232       /* We should be able to find the DIE now.  */
20233       if (!decl_die)
20234         decl_die = lookup_decl_die (decl);
20235       gcc_assert (decl_die);
20236     }
20237
20238   return decl_die;
20239 }
20240
20241 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20242    always returned.  */
20243
20244 static dw_die_ref
20245 force_type_die (tree type)
20246 {
20247   dw_die_ref type_die;
20248
20249   type_die = lookup_type_die (type);
20250   if (!type_die)
20251     {
20252       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20253
20254       type_die = modified_type_die (type, TYPE_READONLY (type),
20255                                     TYPE_VOLATILE (type), context_die);
20256       gcc_assert (type_die);
20257     }
20258   return type_die;
20259 }
20260
20261 /* Force out any required namespaces to be able to output DECL,
20262    and return the new context_die for it, if it's changed.  */
20263
20264 static dw_die_ref
20265 setup_namespace_context (tree thing, dw_die_ref context_die)
20266 {
20267   tree context = (DECL_P (thing)
20268                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20269   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20270     /* Force out the namespace.  */
20271     context_die = force_decl_die (context);
20272
20273   return context_die;
20274 }
20275
20276 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20277    type) within its namespace, if appropriate.
20278
20279    For compatibility with older debuggers, namespace DIEs only contain
20280    declarations; all definitions are emitted at CU scope.  */
20281
20282 static dw_die_ref
20283 declare_in_namespace (tree thing, dw_die_ref context_die)
20284 {
20285   dw_die_ref ns_context;
20286
20287   if (debug_info_level <= DINFO_LEVEL_TERSE)
20288     return context_die;
20289
20290   /* If this decl is from an inlined function, then don't try to emit it in its
20291      namespace, as we will get confused.  It would have already been emitted
20292      when the abstract instance of the inline function was emitted anyways.  */
20293   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20294     return context_die;
20295
20296   ns_context = setup_namespace_context (thing, context_die);
20297
20298   if (ns_context != context_die)
20299     {
20300       if (is_fortran ())
20301         return ns_context;
20302       if (DECL_P (thing))
20303         gen_decl_die (thing, NULL, ns_context);
20304       else
20305         gen_type_die (thing, ns_context);
20306     }
20307   return context_die;
20308 }
20309
20310 /* Generate a DIE for a namespace or namespace alias.  */
20311
20312 static void
20313 gen_namespace_die (tree decl, dw_die_ref context_die)
20314 {
20315   dw_die_ref namespace_die;
20316
20317   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20318      they are an alias of.  */
20319   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20320     {
20321       /* Output a real namespace or module.  */
20322       context_die = setup_namespace_context (decl, comp_unit_die);
20323       namespace_die = new_die (is_fortran ()
20324                                ? DW_TAG_module : DW_TAG_namespace,
20325                                context_die, decl);
20326       /* For Fortran modules defined in different CU don't add src coords.  */
20327       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20328         {
20329           const char *name = dwarf2_name (decl, 0);
20330           if (name)
20331             add_name_attribute (namespace_die, name);
20332         }
20333       else
20334         add_name_and_src_coords_attributes (namespace_die, decl);
20335       if (DECL_EXTERNAL (decl))
20336         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20337       equate_decl_number_to_die (decl, namespace_die);
20338     }
20339   else
20340     {
20341       /* Output a namespace alias.  */
20342
20343       /* Force out the namespace we are an alias of, if necessary.  */
20344       dw_die_ref origin_die
20345         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20346
20347       if (DECL_CONTEXT (decl) == NULL_TREE
20348           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20349         context_die = setup_namespace_context (decl, comp_unit_die);
20350       /* Now create the namespace alias DIE.  */
20351       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20352       add_name_and_src_coords_attributes (namespace_die, decl);
20353       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20354       equate_decl_number_to_die (decl, namespace_die);
20355     }
20356 }
20357
20358 /* Generate Dwarf debug information for a decl described by DECL.  */
20359
20360 static void
20361 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20362 {
20363   tree decl_or_origin = decl ? decl : origin;
20364   tree class_origin = NULL, ultimate_origin;
20365
20366   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20367     return;
20368
20369   switch (TREE_CODE (decl_or_origin))
20370     {
20371     case ERROR_MARK:
20372       break;
20373
20374     case CONST_DECL:
20375       if (!is_fortran ())
20376         {
20377           /* The individual enumerators of an enum type get output when we output
20378              the Dwarf representation of the relevant enum type itself.  */
20379           break;
20380         }
20381
20382       /* Emit its type.  */
20383       gen_type_die (TREE_TYPE (decl), context_die);
20384
20385       /* And its containing namespace.  */
20386       context_die = declare_in_namespace (decl, context_die);
20387
20388       gen_const_die (decl, context_die);
20389       break;
20390
20391     case FUNCTION_DECL:
20392       /* Don't output any DIEs to represent mere function declarations,
20393          unless they are class members or explicit block externs.  */
20394       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20395           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20396           && (current_function_decl == NULL_TREE
20397               || DECL_ARTIFICIAL (decl_or_origin)))
20398         break;
20399
20400 #if 0
20401       /* FIXME */
20402       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20403          on local redeclarations of global functions.  That seems broken.  */
20404       if (current_function_decl != decl)
20405         /* This is only a declaration.  */;
20406 #endif
20407
20408       /* If we're emitting a clone, emit info for the abstract instance.  */
20409       if (origin || DECL_ORIGIN (decl) != decl)
20410         dwarf2out_abstract_function (origin
20411                                      ? DECL_ORIGIN (origin)
20412                                      : DECL_ABSTRACT_ORIGIN (decl));
20413
20414       /* If we're emitting an out-of-line copy of an inline function,
20415          emit info for the abstract instance and set up to refer to it.  */
20416       else if (cgraph_function_possibly_inlined_p (decl)
20417                && ! DECL_ABSTRACT (decl)
20418                && ! class_or_namespace_scope_p (context_die)
20419                /* dwarf2out_abstract_function won't emit a die if this is just
20420                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20421                   that case, because that works only if we have a die.  */
20422                && DECL_INITIAL (decl) != NULL_TREE)
20423         {
20424           dwarf2out_abstract_function (decl);
20425           set_decl_origin_self (decl);
20426         }
20427
20428       /* Otherwise we're emitting the primary DIE for this decl.  */
20429       else if (debug_info_level > DINFO_LEVEL_TERSE)
20430         {
20431           /* Before we describe the FUNCTION_DECL itself, make sure that we
20432              have described its return type.  */
20433           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20434
20435           /* And its virtual context.  */
20436           if (DECL_VINDEX (decl) != NULL_TREE)
20437             gen_type_die (DECL_CONTEXT (decl), context_die);
20438
20439           /* And its containing type.  */
20440           if (!origin)
20441             origin = decl_class_context (decl);
20442           if (origin != NULL_TREE)
20443             gen_type_die_for_member (origin, decl, context_die);
20444
20445           /* And its containing namespace.  */
20446           context_die = declare_in_namespace (decl, context_die);
20447         }
20448
20449       /* Now output a DIE to represent the function itself.  */
20450       if (decl)
20451         gen_subprogram_die (decl, context_die);
20452       break;
20453
20454     case TYPE_DECL:
20455       /* If we are in terse mode, don't generate any DIEs to represent any
20456          actual typedefs.  */
20457       if (debug_info_level <= DINFO_LEVEL_TERSE)
20458         break;
20459
20460       /* In the special case of a TYPE_DECL node representing the declaration
20461          of some type tag, if the given TYPE_DECL is marked as having been
20462          instantiated from some other (original) TYPE_DECL node (e.g. one which
20463          was generated within the original definition of an inline function) we
20464          used to generate a special (abbreviated) DW_TAG_structure_type,
20465          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20466          should be actually referencing those DIEs, as variable DIEs with that
20467          type would be emitted already in the abstract origin, so it was always
20468          removed during unused type prunning.  Don't add anything in this
20469          case.  */
20470       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20471         break;
20472
20473       if (is_redundant_typedef (decl))
20474         gen_type_die (TREE_TYPE (decl), context_die);
20475       else
20476         /* Output a DIE to represent the typedef itself.  */
20477         gen_typedef_die (decl, context_die);
20478       break;
20479
20480     case LABEL_DECL:
20481       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20482         gen_label_die (decl, context_die);
20483       break;
20484
20485     case VAR_DECL:
20486     case RESULT_DECL:
20487       /* If we are in terse mode, don't generate any DIEs to represent any
20488          variable declarations or definitions.  */
20489       if (debug_info_level <= DINFO_LEVEL_TERSE)
20490         break;
20491
20492       /* Output any DIEs that are needed to specify the type of this data
20493          object.  */
20494       if (decl_by_reference_p (decl_or_origin))
20495         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20496       else
20497         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20498
20499       /* And its containing type.  */
20500       class_origin = decl_class_context (decl_or_origin);
20501       if (class_origin != NULL_TREE)
20502         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20503
20504       /* And its containing namespace.  */
20505       context_die = declare_in_namespace (decl_or_origin, context_die);
20506
20507       /* Now output the DIE to represent the data object itself.  This gets
20508          complicated because of the possibility that the VAR_DECL really
20509          represents an inlined instance of a formal parameter for an inline
20510          function.  */
20511       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20512       if (ultimate_origin != NULL_TREE
20513           && TREE_CODE (ultimate_origin) == PARM_DECL)
20514         gen_formal_parameter_die (decl, origin,
20515                                   true /* Emit name attribute.  */,
20516                                   context_die);
20517       else
20518         gen_variable_die (decl, origin, context_die);
20519       break;
20520
20521     case FIELD_DECL:
20522       /* Ignore the nameless fields that are used to skip bits but handle C++
20523          anonymous unions and structs.  */
20524       if (DECL_NAME (decl) != NULL_TREE
20525           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20526           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20527         {
20528           gen_type_die (member_declared_type (decl), context_die);
20529           gen_field_die (decl, context_die);
20530         }
20531       break;
20532
20533     case PARM_DECL:
20534       if (DECL_BY_REFERENCE (decl_or_origin))
20535         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20536       else
20537         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20538       gen_formal_parameter_die (decl, origin,
20539                                 true /* Emit name attribute.  */,
20540                                 context_die);
20541       break;
20542
20543     case NAMESPACE_DECL:
20544     case IMPORTED_DECL:
20545       if (dwarf_version >= 3 || !dwarf_strict)
20546         gen_namespace_die (decl, context_die);
20547       break;
20548
20549     default:
20550       /* Probably some frontend-internal decl.  Assume we don't care.  */
20551       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20552       break;
20553     }
20554 }
20555 \f
20556 /* Output debug information for global decl DECL.  Called from toplev.c after
20557    compilation proper has finished.  */
20558
20559 static void
20560 dwarf2out_global_decl (tree decl)
20561 {
20562   /* Output DWARF2 information for file-scope tentative data object
20563      declarations, file-scope (extern) function declarations (which
20564      had no corresponding body) and file-scope tagged type declarations
20565      and definitions which have not yet been forced out.  */
20566   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20567     dwarf2out_decl (decl);
20568 }
20569
20570 /* Output debug information for type decl DECL.  Called from toplev.c
20571    and from language front ends (to record built-in types).  */
20572 static void
20573 dwarf2out_type_decl (tree decl, int local)
20574 {
20575   if (!local)
20576     dwarf2out_decl (decl);
20577 }
20578
20579 /* Output debug information for imported module or decl DECL.
20580    NAME is non-NULL name in the lexical block if the decl has been renamed.
20581    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20582    that DECL belongs to.
20583    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20584 static void
20585 dwarf2out_imported_module_or_decl_1 (tree decl,
20586                                      tree name,
20587                                      tree lexical_block,
20588                                      dw_die_ref lexical_block_die)
20589 {
20590   expanded_location xloc;
20591   dw_die_ref imported_die = NULL;
20592   dw_die_ref at_import_die;
20593
20594   if (TREE_CODE (decl) == IMPORTED_DECL)
20595     {
20596       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20597       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20598       gcc_assert (decl);
20599     }
20600   else
20601     xloc = expand_location (input_location);
20602
20603   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20604     {
20605       at_import_die = force_type_die (TREE_TYPE (decl));
20606       /* For namespace N { typedef void T; } using N::T; base_type_die
20607          returns NULL, but DW_TAG_imported_declaration requires
20608          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20609       if (!at_import_die)
20610         {
20611           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20612           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20613           at_import_die = lookup_type_die (TREE_TYPE (decl));
20614           gcc_assert (at_import_die);
20615         }
20616     }
20617   else
20618     {
20619       at_import_die = lookup_decl_die (decl);
20620       if (!at_import_die)
20621         {
20622           /* If we're trying to avoid duplicate debug info, we may not have
20623              emitted the member decl for this field.  Emit it now.  */
20624           if (TREE_CODE (decl) == FIELD_DECL)
20625             {
20626               tree type = DECL_CONTEXT (decl);
20627
20628               if (TYPE_CONTEXT (type)
20629                   && TYPE_P (TYPE_CONTEXT (type))
20630                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20631                                                 DINFO_USAGE_DIR_USE))
20632                 return;
20633               gen_type_die_for_member (type, decl,
20634                                        get_context_die (TYPE_CONTEXT (type)));
20635             }
20636           at_import_die = force_decl_die (decl);
20637         }
20638     }
20639
20640   if (TREE_CODE (decl) == NAMESPACE_DECL)
20641     {
20642       if (dwarf_version >= 3 || !dwarf_strict)
20643         imported_die = new_die (DW_TAG_imported_module,
20644                                 lexical_block_die,
20645                                 lexical_block);
20646       else
20647         return;
20648     }
20649   else
20650     imported_die = new_die (DW_TAG_imported_declaration,
20651                             lexical_block_die,
20652                             lexical_block);
20653
20654   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20655   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20656   if (name)
20657     add_AT_string (imported_die, DW_AT_name,
20658                    IDENTIFIER_POINTER (name));
20659   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20660 }
20661
20662 /* Output debug information for imported module or decl DECL.
20663    NAME is non-NULL name in context if the decl has been renamed.
20664    CHILD is true if decl is one of the renamed decls as part of
20665    importing whole module.  */
20666
20667 static void
20668 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20669                                    bool child)
20670 {
20671   /* dw_die_ref at_import_die;  */
20672   dw_die_ref scope_die;
20673
20674   if (debug_info_level <= DINFO_LEVEL_TERSE)
20675     return;
20676
20677   gcc_assert (decl);
20678
20679   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20680      We need decl DIE for reference and scope die. First, get DIE for the decl
20681      itself.  */
20682
20683   /* Get the scope die for decl context. Use comp_unit_die for global module
20684      or decl. If die is not found for non globals, force new die.  */
20685   if (context
20686       && TYPE_P (context)
20687       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20688     return;
20689
20690   if (!(dwarf_version >= 3 || !dwarf_strict))
20691     return;
20692
20693   scope_die = get_context_die (context);
20694
20695   if (child)
20696     {
20697       gcc_assert (scope_die->die_child);
20698       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20699       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20700       scope_die = scope_die->die_child;
20701     }
20702
20703   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20704   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20705
20706 }
20707
20708 /* Write the debugging output for DECL.  */
20709
20710 void
20711 dwarf2out_decl (tree decl)
20712 {
20713   dw_die_ref context_die = comp_unit_die;
20714
20715   switch (TREE_CODE (decl))
20716     {
20717     case ERROR_MARK:
20718       return;
20719
20720     case FUNCTION_DECL:
20721       /* What we would really like to do here is to filter out all mere
20722          file-scope declarations of file-scope functions which are never
20723          referenced later within this translation unit (and keep all of ones
20724          that *are* referenced later on) but we aren't clairvoyant, so we have
20725          no idea which functions will be referenced in the future (i.e. later
20726          on within the current translation unit). So here we just ignore all
20727          file-scope function declarations which are not also definitions.  If
20728          and when the debugger needs to know something about these functions,
20729          it will have to hunt around and find the DWARF information associated
20730          with the definition of the function.
20731
20732          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20733          nodes represent definitions and which ones represent mere
20734          declarations.  We have to check DECL_INITIAL instead. That's because
20735          the C front-end supports some weird semantics for "extern inline"
20736          function definitions.  These can get inlined within the current
20737          translation unit (and thus, we need to generate Dwarf info for their
20738          abstract instances so that the Dwarf info for the concrete inlined
20739          instances can have something to refer to) but the compiler never
20740          generates any out-of-lines instances of such things (despite the fact
20741          that they *are* definitions).
20742
20743          The important point is that the C front-end marks these "extern
20744          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20745          them anyway. Note that the C++ front-end also plays some similar games
20746          for inline function definitions appearing within include files which
20747          also contain `#pragma interface' pragmas.  */
20748       if (DECL_INITIAL (decl) == NULL_TREE)
20749         return;
20750
20751       /* If we're a nested function, initially use a parent of NULL; if we're
20752          a plain function, this will be fixed up in decls_for_scope.  If
20753          we're a method, it will be ignored, since we already have a DIE.  */
20754       if (decl_function_context (decl)
20755           /* But if we're in terse mode, we don't care about scope.  */
20756           && debug_info_level > DINFO_LEVEL_TERSE)
20757         context_die = NULL;
20758       break;
20759
20760     case VAR_DECL:
20761       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20762          declaration and if the declaration was never even referenced from
20763          within this entire compilation unit.  We suppress these DIEs in
20764          order to save space in the .debug section (by eliminating entries
20765          which are probably useless).  Note that we must not suppress
20766          block-local extern declarations (whether used or not) because that
20767          would screw-up the debugger's name lookup mechanism and cause it to
20768          miss things which really ought to be in scope at a given point.  */
20769       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20770         return;
20771
20772       /* For local statics lookup proper context die.  */
20773       if (TREE_STATIC (decl) && decl_function_context (decl))
20774         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20775
20776       /* If we are in terse mode, don't generate any DIEs to represent any
20777          variable declarations or definitions.  */
20778       if (debug_info_level <= DINFO_LEVEL_TERSE)
20779         return;
20780       break;
20781
20782     case CONST_DECL:
20783       if (debug_info_level <= DINFO_LEVEL_TERSE)
20784         return;
20785       if (!is_fortran ())
20786         return;
20787       if (TREE_STATIC (decl) && decl_function_context (decl))
20788         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20789       break;
20790
20791     case NAMESPACE_DECL:
20792     case IMPORTED_DECL:
20793       if (debug_info_level <= DINFO_LEVEL_TERSE)
20794         return;
20795       if (lookup_decl_die (decl) != NULL)
20796         return;
20797       break;
20798
20799     case TYPE_DECL:
20800       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20801       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20802         return;
20803
20804       /* Don't bother trying to generate any DIEs to represent any of the
20805          normal built-in types for the language we are compiling.  */
20806       if (DECL_IS_BUILTIN (decl))
20807         {
20808           /* OK, we need to generate one for `bool' so GDB knows what type
20809              comparisons have.  */
20810           if (is_cxx ()
20811               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20812               && ! DECL_IGNORED_P (decl))
20813             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20814
20815           return;
20816         }
20817
20818       /* If we are in terse mode, don't generate any DIEs for types.  */
20819       if (debug_info_level <= DINFO_LEVEL_TERSE)
20820         return;
20821
20822       /* If we're a function-scope tag, initially use a parent of NULL;
20823          this will be fixed up in decls_for_scope.  */
20824       if (decl_function_context (decl))
20825         context_die = NULL;
20826
20827       break;
20828
20829     default:
20830       return;
20831     }
20832
20833   gen_decl_die (decl, NULL, context_die);
20834 }
20835
20836 /* Write the debugging output for DECL.  */
20837
20838 static void
20839 dwarf2out_function_decl (tree decl)
20840 {
20841   dwarf2out_decl (decl);
20842
20843   htab_empty (decl_loc_table);
20844 }
20845
20846 /* Output a marker (i.e. a label) for the beginning of the generated code for
20847    a lexical block.  */
20848
20849 static void
20850 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20851                        unsigned int blocknum)
20852 {
20853   switch_to_section (current_function_section ());
20854   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20855 }
20856
20857 /* Output a marker (i.e. a label) for the end of the generated code for a
20858    lexical block.  */
20859
20860 static void
20861 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20862 {
20863   switch_to_section (current_function_section ());
20864   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20865 }
20866
20867 /* Returns nonzero if it is appropriate not to emit any debugging
20868    information for BLOCK, because it doesn't contain any instructions.
20869
20870    Don't allow this for blocks with nested functions or local classes
20871    as we would end up with orphans, and in the presence of scheduling
20872    we may end up calling them anyway.  */
20873
20874 static bool
20875 dwarf2out_ignore_block (const_tree block)
20876 {
20877   tree decl;
20878   unsigned int i;
20879
20880   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
20881     if (TREE_CODE (decl) == FUNCTION_DECL
20882         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20883       return 0;
20884   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20885     {
20886       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20887       if (TREE_CODE (decl) == FUNCTION_DECL
20888           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20889       return 0;
20890     }
20891
20892   return 1;
20893 }
20894
20895 /* Hash table routines for file_hash.  */
20896
20897 static int
20898 file_table_eq (const void *p1_p, const void *p2_p)
20899 {
20900   const struct dwarf_file_data *const p1 =
20901     (const struct dwarf_file_data *) p1_p;
20902   const char *const p2 = (const char *) p2_p;
20903   return strcmp (p1->filename, p2) == 0;
20904 }
20905
20906 static hashval_t
20907 file_table_hash (const void *p_p)
20908 {
20909   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20910   return htab_hash_string (p->filename);
20911 }
20912
20913 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20914    dwarf2out.c) and return its "index".  The index of each (known) filename is
20915    just a unique number which is associated with only that one filename.  We
20916    need such numbers for the sake of generating labels (in the .debug_sfnames
20917    section) and references to those files numbers (in the .debug_srcinfo
20918    and.debug_macinfo sections).  If the filename given as an argument is not
20919    found in our current list, add it to the list and assign it the next
20920    available unique index number.  In order to speed up searches, we remember
20921    the index of the filename was looked up last.  This handles the majority of
20922    all searches.  */
20923
20924 static struct dwarf_file_data *
20925 lookup_filename (const char *file_name)
20926 {
20927   void ** slot;
20928   struct dwarf_file_data * created;
20929
20930   /* Check to see if the file name that was searched on the previous
20931      call matches this file name.  If so, return the index.  */
20932   if (file_table_last_lookup
20933       && (file_name == file_table_last_lookup->filename
20934           || strcmp (file_table_last_lookup->filename, file_name) == 0))
20935     return file_table_last_lookup;
20936
20937   /* Didn't match the previous lookup, search the table.  */
20938   slot = htab_find_slot_with_hash (file_table, file_name,
20939                                    htab_hash_string (file_name), INSERT);
20940   if (*slot)
20941     return (struct dwarf_file_data *) *slot;
20942
20943   created = ggc_alloc_dwarf_file_data ();
20944   created->filename = file_name;
20945   created->emitted_number = 0;
20946   *slot = created;
20947   return created;
20948 }
20949
20950 /* If the assembler will construct the file table, then translate the compiler
20951    internal file table number into the assembler file table number, and emit
20952    a .file directive if we haven't already emitted one yet.  The file table
20953    numbers are different because we prune debug info for unused variables and
20954    types, which may include filenames.  */
20955
20956 static int
20957 maybe_emit_file (struct dwarf_file_data * fd)
20958 {
20959   if (! fd->emitted_number)
20960     {
20961       if (last_emitted_file)
20962         fd->emitted_number = last_emitted_file->emitted_number + 1;
20963       else
20964         fd->emitted_number = 1;
20965       last_emitted_file = fd;
20966
20967       if (DWARF2_ASM_LINE_DEBUG_INFO)
20968         {
20969           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20970           output_quoted_string (asm_out_file,
20971                                 remap_debug_filename (fd->filename));
20972           fputc ('\n', asm_out_file);
20973         }
20974     }
20975
20976   return fd->emitted_number;
20977 }
20978
20979 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20980    That generation should happen after function debug info has been
20981    generated. The value of the attribute is the constant value of ARG.  */
20982
20983 static void
20984 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20985 {
20986   die_arg_entry entry;
20987
20988   if (!die || !arg)
20989     return;
20990
20991   if (!tmpl_value_parm_die_table)
20992     tmpl_value_parm_die_table
20993       = VEC_alloc (die_arg_entry, gc, 32);
20994
20995   entry.die = die;
20996   entry.arg = arg;
20997   VEC_safe_push (die_arg_entry, gc,
20998                  tmpl_value_parm_die_table,
20999                  &entry);
21000 }
21001
21002 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21003    by append_entry_to_tmpl_value_parm_die_table. This function must
21004    be called after function DIEs have been generated.  */
21005
21006 static void
21007 gen_remaining_tmpl_value_param_die_attribute (void)
21008 {
21009   if (tmpl_value_parm_die_table)
21010     {
21011       unsigned i;
21012       die_arg_entry *e;
21013
21014       for (i = 0;
21015            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
21016            i++)
21017         tree_add_const_value_attribute (e->die, e->arg);
21018     }
21019 }
21020
21021
21022 /* Replace DW_AT_name for the decl with name.  */
21023
21024 static void
21025 dwarf2out_set_name (tree decl, tree name)
21026 {
21027   dw_die_ref die;
21028   dw_attr_ref attr;
21029   const char *dname;
21030
21031   die = TYPE_SYMTAB_DIE (decl);
21032   if (!die)
21033     return;
21034
21035   dname = dwarf2_name (name, 0);
21036   if (!dname)
21037     return;
21038
21039   attr = get_AT (die, DW_AT_name);
21040   if (attr)
21041     {
21042       struct indirect_string_node *node;
21043
21044       node = find_AT_string (dname);
21045       /* replace the string.  */
21046       attr->dw_attr_val.v.val_str = node;
21047     }
21048
21049   else
21050     add_name_attribute (die, dname);
21051 }
21052
21053 /* Called by the final INSN scan whenever we see a direct function call.
21054    Make an entry into the direct call table, recording the point of call
21055    and a reference to the target function's debug entry.  */
21056
21057 static void
21058 dwarf2out_direct_call (tree targ)
21059 {
21060   dcall_entry e;
21061   tree origin = decl_ultimate_origin (targ);
21062
21063   /* If this is a clone, use the abstract origin as the target.  */
21064   if (origin)
21065     targ = origin;
21066
21067   e.poc_label_num = poc_label_num++;
21068   e.poc_decl = current_function_decl;
21069   e.targ_die = force_decl_die (targ);
21070   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21071
21072   /* Drop a label at the return point to mark the point of call.  */
21073   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21074 }
21075
21076 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21077
21078 static hashval_t
21079 vcall_insn_table_hash (const void *x)
21080 {
21081   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21082 }
21083
21084 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21085    insnd_uid of *Y.  */
21086
21087 static int
21088 vcall_insn_table_eq (const void *x, const void *y)
21089 {
21090   return (((const struct vcall_insn *) x)->insn_uid
21091           == ((const struct vcall_insn *) y)->insn_uid);
21092 }
21093
21094 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21095
21096 static void
21097 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21098 {
21099   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21100   struct vcall_insn **slot;
21101
21102   gcc_assert (item);
21103   item->insn_uid = insn_uid;
21104   item->vtable_slot = vtable_slot;
21105   slot = (struct vcall_insn **)
21106       htab_find_slot_with_hash (vcall_insn_table, &item,
21107                                 (hashval_t) insn_uid, INSERT);
21108   *slot = item;
21109 }
21110
21111 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21112
21113 static unsigned int
21114 lookup_vcall_insn (unsigned int insn_uid)
21115 {
21116   struct vcall_insn item;
21117   struct vcall_insn *p;
21118
21119   item.insn_uid = insn_uid;
21120   item.vtable_slot = 0;
21121   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21122                                                  (void *) &item,
21123                                                  (hashval_t) insn_uid);
21124   if (p == NULL)
21125     return (unsigned int) -1;
21126   return p->vtable_slot;
21127 }
21128
21129
21130 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21131    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21132    is the vtable slot index that we will need to put in the virtual call
21133    table later.  */
21134
21135 static void
21136 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21137 {
21138   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21139     {
21140       tree token = OBJ_TYPE_REF_TOKEN (addr);
21141       if (TREE_CODE (token) == INTEGER_CST)
21142         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21143     }
21144 }
21145
21146 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21147    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21148    with NEW_INSN.  */
21149
21150 static void
21151 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21152 {
21153   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21154
21155   if (vtable_slot != (unsigned int) -1)
21156     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21157 }
21158
21159 /* Called by the final INSN scan whenever we see a virtual function call.
21160    Make an entry into the virtual call table, recording the point of call
21161    and the slot index of the vtable entry used to call the virtual member
21162    function.  The slot index was associated with the INSN_UID during the
21163    lowering to RTL.  */
21164
21165 static void
21166 dwarf2out_virtual_call (int insn_uid)
21167 {
21168   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21169   vcall_entry e;
21170
21171   if (vtable_slot == (unsigned int) -1)
21172     return;
21173
21174   e.poc_label_num = poc_label_num++;
21175   e.vtable_slot = vtable_slot;
21176   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21177
21178   /* Drop a label at the return point to mark the point of call.  */
21179   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21180 }
21181
21182 /* Called by the final INSN scan whenever we see a var location.  We
21183    use it to drop labels in the right places, and throw the location in
21184    our lookup table.  */
21185
21186 static void
21187 dwarf2out_var_location (rtx loc_note)
21188 {
21189   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21190   struct var_loc_node *newloc;
21191   rtx next_real;
21192   static const char *last_label;
21193   static const char *last_postcall_label;
21194   static bool last_in_cold_section_p;
21195   tree decl;
21196
21197   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21198     return;
21199
21200   next_real = next_real_insn (loc_note);
21201   /* If there are no instructions which would be affected by this note,
21202      don't do anything.  */
21203   if (next_real == NULL_RTX)
21204     return;
21205
21206   /* If there were any real insns between note we processed last time
21207      and this note (or if it is the first note), clear
21208      last_{,postcall_}label so that they are not reused this time.  */
21209   if (last_var_location_insn == NULL_RTX
21210       || last_var_location_insn != next_real
21211       || last_in_cold_section_p != in_cold_section_p)
21212     {
21213       last_label = NULL;
21214       last_postcall_label = NULL;
21215     }
21216
21217   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21218   newloc = add_var_loc_to_decl (decl, loc_note,
21219                                 NOTE_DURING_CALL_P (loc_note)
21220                                 ? last_postcall_label : last_label);
21221   if (newloc == NULL)
21222     return;
21223
21224   /* If there were no real insns between note we processed last time
21225      and this note, use the label we emitted last time.  Otherwise
21226      create a new label and emit it.  */
21227   if (last_label == NULL)
21228     {
21229       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21230       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21231       loclabel_num++;
21232       last_label = ggc_strdup (loclabel);
21233     }
21234
21235   if (!NOTE_DURING_CALL_P (loc_note))
21236     newloc->label = last_label;
21237   else
21238     {
21239       if (!last_postcall_label)
21240         {
21241           sprintf (loclabel, "%s-1", last_label);
21242           last_postcall_label = ggc_strdup (loclabel);
21243         }
21244       newloc->label = last_postcall_label;
21245     }
21246
21247   last_var_location_insn = next_real;
21248   last_in_cold_section_p = in_cold_section_p;
21249 }
21250
21251 /* We need to reset the locations at the beginning of each
21252    function. We can't do this in the end_function hook, because the
21253    declarations that use the locations won't have been output when
21254    that hook is called.  Also compute have_multiple_function_sections here.  */
21255
21256 static void
21257 dwarf2out_begin_function (tree fun)
21258 {
21259   if (function_section (fun) != text_section)
21260     have_multiple_function_sections = true;
21261
21262   dwarf2out_note_section_used ();
21263 }
21264
21265 /* Output a label to mark the beginning of a source code line entry
21266    and record information relating to this source line, in
21267    'line_info_table' for later output of the .debug_line section.  */
21268
21269 static void
21270 dwarf2out_source_line (unsigned int line, const char *filename,
21271                        int discriminator, bool is_stmt)
21272 {
21273   static bool last_is_stmt = true;
21274
21275   if (debug_info_level >= DINFO_LEVEL_NORMAL
21276       && line != 0)
21277     {
21278       int file_num = maybe_emit_file (lookup_filename (filename));
21279
21280       switch_to_section (current_function_section ());
21281
21282       /* If requested, emit something human-readable.  */
21283       if (flag_debug_asm)
21284         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21285                  filename, line);
21286
21287       if (DWARF2_ASM_LINE_DEBUG_INFO)
21288         {
21289           /* Emit the .loc directive understood by GNU as.  */
21290           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21291           if (is_stmt != last_is_stmt)
21292             {
21293               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21294               last_is_stmt = is_stmt;
21295             }
21296           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21297             fprintf (asm_out_file, " discriminator %d", discriminator);
21298           fputc ('\n', asm_out_file);
21299
21300           /* Indicate that line number info exists.  */
21301           line_info_table_in_use++;
21302         }
21303       else if (function_section (current_function_decl) != text_section)
21304         {
21305           dw_separate_line_info_ref line_info;
21306           targetm.asm_out.internal_label (asm_out_file,
21307                                           SEPARATE_LINE_CODE_LABEL,
21308                                           separate_line_info_table_in_use);
21309
21310           /* Expand the line info table if necessary.  */
21311           if (separate_line_info_table_in_use
21312               == separate_line_info_table_allocated)
21313             {
21314               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21315               separate_line_info_table
21316                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21317                                  separate_line_info_table,
21318                                  separate_line_info_table_allocated);
21319               memset (separate_line_info_table
21320                        + separate_line_info_table_in_use,
21321                       0,
21322                       (LINE_INFO_TABLE_INCREMENT
21323                        * sizeof (dw_separate_line_info_entry)));
21324             }
21325
21326           /* Add the new entry at the end of the line_info_table.  */
21327           line_info
21328             = &separate_line_info_table[separate_line_info_table_in_use++];
21329           line_info->dw_file_num = file_num;
21330           line_info->dw_line_num = line;
21331           line_info->function = current_function_funcdef_no;
21332         }
21333       else
21334         {
21335           dw_line_info_ref line_info;
21336
21337           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21338                                      line_info_table_in_use);
21339
21340           /* Expand the line info table if necessary.  */
21341           if (line_info_table_in_use == line_info_table_allocated)
21342             {
21343               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21344               line_info_table
21345                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21346                                  line_info_table_allocated);
21347               memset (line_info_table + line_info_table_in_use, 0,
21348                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21349             }
21350
21351           /* Add the new entry at the end of the line_info_table.  */
21352           line_info = &line_info_table[line_info_table_in_use++];
21353           line_info->dw_file_num = file_num;
21354           line_info->dw_line_num = line;
21355         }
21356     }
21357 }
21358
21359 /* Record the beginning of a new source file.  */
21360
21361 static void
21362 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21363 {
21364   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21365     {
21366       /* Record the beginning of the file for break_out_includes.  */
21367       dw_die_ref bincl_die;
21368
21369       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21370       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21371     }
21372
21373   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21374     {
21375       int file_num = maybe_emit_file (lookup_filename (filename));
21376
21377       switch_to_section (debug_macinfo_section);
21378       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21379       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21380                                    lineno);
21381
21382       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21383     }
21384 }
21385
21386 /* Record the end of a source file.  */
21387
21388 static void
21389 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21390 {
21391   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21392     /* Record the end of the file for break_out_includes.  */
21393     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21394
21395   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21396     {
21397       switch_to_section (debug_macinfo_section);
21398       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21399     }
21400 }
21401
21402 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21403    the tail part of the directive line, i.e. the part which is past the
21404    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21405
21406 static void
21407 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21408                   const char *buffer ATTRIBUTE_UNUSED)
21409 {
21410   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21411     {
21412       switch_to_section (debug_macinfo_section);
21413       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21414       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21415       dw2_asm_output_nstring (buffer, -1, "The macro");
21416     }
21417 }
21418
21419 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21420    the tail part of the directive line, i.e. the part which is past the
21421    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21422
21423 static void
21424 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21425                  const char *buffer ATTRIBUTE_UNUSED)
21426 {
21427   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21428     {
21429       switch_to_section (debug_macinfo_section);
21430       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21431       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21432       dw2_asm_output_nstring (buffer, -1, "The macro");
21433     }
21434 }
21435
21436 /* Set up for Dwarf output at the start of compilation.  */
21437
21438 static void
21439 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21440 {
21441   /* Allocate the file_table.  */
21442   file_table = htab_create_ggc (50, file_table_hash,
21443                                 file_table_eq, NULL);
21444
21445   /* Allocate the decl_die_table.  */
21446   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21447                                     decl_die_table_eq, NULL);
21448
21449   /* Allocate the decl_loc_table.  */
21450   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21451                                     decl_loc_table_eq, NULL);
21452
21453   /* Allocate the initial hunk of the decl_scope_table.  */
21454   decl_scope_table = VEC_alloc (tree, gc, 256);
21455
21456   /* Allocate the initial hunk of the abbrev_die_table.  */
21457   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21458     (ABBREV_DIE_TABLE_INCREMENT);
21459   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21460   /* Zero-th entry is allocated, but unused.  */
21461   abbrev_die_table_in_use = 1;
21462
21463   /* Allocate the initial hunk of the line_info_table.  */
21464   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21465     (LINE_INFO_TABLE_INCREMENT);
21466   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21467
21468   /* Zero-th entry is allocated, but unused.  */
21469   line_info_table_in_use = 1;
21470
21471   /* Allocate the pubtypes and pubnames vectors.  */
21472   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21473   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21474
21475   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21476   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21477                                       vcall_insn_table_eq, NULL);
21478
21479   /* Generate the initial DIE for the .debug section.  Note that the (string)
21480      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21481      will (typically) be a relative pathname and that this pathname should be
21482      taken as being relative to the directory from which the compiler was
21483      invoked when the given (base) source file was compiled.  We will fill
21484      in this value in dwarf2out_finish.  */
21485   comp_unit_die = gen_compile_unit_die (NULL);
21486
21487   incomplete_types = VEC_alloc (tree, gc, 64);
21488
21489   used_rtx_array = VEC_alloc (rtx, gc, 32);
21490
21491   debug_info_section = get_section (DEBUG_INFO_SECTION,
21492                                     SECTION_DEBUG, NULL);
21493   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21494                                       SECTION_DEBUG, NULL);
21495   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21496                                        SECTION_DEBUG, NULL);
21497   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21498                                        SECTION_DEBUG, NULL);
21499   debug_line_section = get_section (DEBUG_LINE_SECTION,
21500                                     SECTION_DEBUG, NULL);
21501   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21502                                    SECTION_DEBUG, NULL);
21503   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21504                                         SECTION_DEBUG, NULL);
21505   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21506                                         SECTION_DEBUG, NULL);
21507   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21508                                      SECTION_DEBUG, NULL);
21509   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21510                                      SECTION_DEBUG, NULL);
21511   debug_str_section = get_section (DEBUG_STR_SECTION,
21512                                    DEBUG_STR_SECTION_FLAGS, NULL);
21513   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21514                                       SECTION_DEBUG, NULL);
21515   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21516                                      SECTION_DEBUG, NULL);
21517
21518   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21519   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21520                                DEBUG_ABBREV_SECTION_LABEL, 0);
21521   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21522   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21523                                COLD_TEXT_SECTION_LABEL, 0);
21524   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21525
21526   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21527                                DEBUG_INFO_SECTION_LABEL, 0);
21528   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21529                                DEBUG_LINE_SECTION_LABEL, 0);
21530   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21531                                DEBUG_RANGES_SECTION_LABEL, 0);
21532   switch_to_section (debug_abbrev_section);
21533   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21534   switch_to_section (debug_info_section);
21535   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21536   switch_to_section (debug_line_section);
21537   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21538
21539   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21540     {
21541       switch_to_section (debug_macinfo_section);
21542       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21543                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21544       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21545     }
21546
21547   switch_to_section (text_section);
21548   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21549   if (flag_reorder_blocks_and_partition)
21550     {
21551       cold_text_section = unlikely_text_section ();
21552       switch_to_section (cold_text_section);
21553       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21554     }
21555
21556 }
21557
21558 /* Called before cgraph_optimize starts outputtting functions, variables
21559    and toplevel asms into assembly.  */
21560
21561 static void
21562 dwarf2out_assembly_start (void)
21563 {
21564   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21565     {
21566 #ifndef TARGET_UNWIND_INFO
21567       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21568 #endif
21569         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21570     }
21571 }
21572
21573 /* A helper function for dwarf2out_finish called through
21574    htab_traverse.  Emit one queued .debug_str string.  */
21575
21576 static int
21577 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21578 {
21579   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21580
21581   if (node->label && node->refcount)
21582     {
21583       switch_to_section (debug_str_section);
21584       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21585       assemble_string (node->str, strlen (node->str) + 1);
21586     }
21587
21588   return 1;
21589 }
21590
21591 #if ENABLE_ASSERT_CHECKING
21592 /* Verify that all marks are clear.  */
21593
21594 static void
21595 verify_marks_clear (dw_die_ref die)
21596 {
21597   dw_die_ref c;
21598
21599   gcc_assert (! die->die_mark);
21600   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21601 }
21602 #endif /* ENABLE_ASSERT_CHECKING */
21603
21604 /* Clear the marks for a die and its children.
21605    Be cool if the mark isn't set.  */
21606
21607 static void
21608 prune_unmark_dies (dw_die_ref die)
21609 {
21610   dw_die_ref c;
21611
21612   if (die->die_mark)
21613     die->die_mark = 0;
21614   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21615 }
21616
21617 /* Given DIE that we're marking as used, find any other dies
21618    it references as attributes and mark them as used.  */
21619
21620 static void
21621 prune_unused_types_walk_attribs (dw_die_ref die)
21622 {
21623   dw_attr_ref a;
21624   unsigned ix;
21625
21626   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21627     {
21628       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21629         {
21630           /* A reference to another DIE.
21631              Make sure that it will get emitted.
21632              If it was broken out into a comdat group, don't follow it.  */
21633           if (dwarf_version < 4
21634               || a->dw_attr == DW_AT_specification
21635               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21636             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21637         }
21638       /* Set the string's refcount to 0 so that prune_unused_types_mark
21639          accounts properly for it.  */
21640       if (AT_class (a) == dw_val_class_str)
21641         a->dw_attr_val.v.val_str->refcount = 0;
21642     }
21643 }
21644
21645
21646 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21647    to DIE's children.  */
21648
21649 static void
21650 prune_unused_types_mark (dw_die_ref die, int dokids)
21651 {
21652   dw_die_ref c;
21653
21654   if (die->die_mark == 0)
21655     {
21656       /* We haven't done this node yet.  Mark it as used.  */
21657       die->die_mark = 1;
21658
21659       /* We also have to mark its parents as used.
21660          (But we don't want to mark our parents' kids due to this.)  */
21661       if (die->die_parent)
21662         prune_unused_types_mark (die->die_parent, 0);
21663
21664       /* Mark any referenced nodes.  */
21665       prune_unused_types_walk_attribs (die);
21666
21667       /* If this node is a specification,
21668          also mark the definition, if it exists.  */
21669       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21670         prune_unused_types_mark (die->die_definition, 1);
21671     }
21672
21673   if (dokids && die->die_mark != 2)
21674     {
21675       /* We need to walk the children, but haven't done so yet.
21676          Remember that we've walked the kids.  */
21677       die->die_mark = 2;
21678
21679       /* If this is an array type, we need to make sure our
21680          kids get marked, even if they're types.  If we're
21681          breaking out types into comdat sections, do this
21682          for all type definitions.  */
21683       if (die->die_tag == DW_TAG_array_type
21684           || (dwarf_version >= 4
21685               && is_type_die (die) && ! is_declaration_die (die)))
21686         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21687       else
21688         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21689     }
21690 }
21691
21692 /* For local classes, look if any static member functions were emitted
21693    and if so, mark them.  */
21694
21695 static void
21696 prune_unused_types_walk_local_classes (dw_die_ref die)
21697 {
21698   dw_die_ref c;
21699
21700   if (die->die_mark == 2)
21701     return;
21702
21703   switch (die->die_tag)
21704     {
21705     case DW_TAG_structure_type:
21706     case DW_TAG_union_type:
21707     case DW_TAG_class_type:
21708       break;
21709
21710     case DW_TAG_subprogram:
21711       if (!get_AT_flag (die, DW_AT_declaration)
21712           || die->die_definition != NULL)
21713         prune_unused_types_mark (die, 1);
21714       return;
21715
21716     default:
21717       return;
21718     }
21719
21720   /* Mark children.  */
21721   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21722 }
21723
21724 /* Walk the tree DIE and mark types that we actually use.  */
21725
21726 static void
21727 prune_unused_types_walk (dw_die_ref die)
21728 {
21729   dw_die_ref c;
21730
21731   /* Don't do anything if this node is already marked and
21732      children have been marked as well.  */
21733   if (die->die_mark == 2)
21734     return;
21735
21736   switch (die->die_tag)
21737     {
21738     case DW_TAG_structure_type:
21739     case DW_TAG_union_type:
21740     case DW_TAG_class_type:
21741       if (die->die_perennial_p)
21742         break;
21743
21744       for (c = die->die_parent; c; c = c->die_parent)
21745         if (c->die_tag == DW_TAG_subprogram)
21746           break;
21747
21748       /* Finding used static member functions inside of classes
21749          is needed just for local classes, because for other classes
21750          static member function DIEs with DW_AT_specification
21751          are emitted outside of the DW_TAG_*_type.  If we ever change
21752          it, we'd need to call this even for non-local classes.  */
21753       if (c)
21754         prune_unused_types_walk_local_classes (die);
21755
21756       /* It's a type node --- don't mark it.  */
21757       return;
21758
21759     case DW_TAG_const_type:
21760     case DW_TAG_packed_type:
21761     case DW_TAG_pointer_type:
21762     case DW_TAG_reference_type:
21763     case DW_TAG_rvalue_reference_type:
21764     case DW_TAG_volatile_type:
21765     case DW_TAG_typedef:
21766     case DW_TAG_array_type:
21767     case DW_TAG_interface_type:
21768     case DW_TAG_friend:
21769     case DW_TAG_variant_part:
21770     case DW_TAG_enumeration_type:
21771     case DW_TAG_subroutine_type:
21772     case DW_TAG_string_type:
21773     case DW_TAG_set_type:
21774     case DW_TAG_subrange_type:
21775     case DW_TAG_ptr_to_member_type:
21776     case DW_TAG_file_type:
21777       if (die->die_perennial_p)
21778         break;
21779
21780       /* It's a type node --- don't mark it.  */
21781       return;
21782
21783     default:
21784       /* Mark everything else.  */
21785       break;
21786   }
21787
21788   if (die->die_mark == 0)
21789     {
21790       die->die_mark = 1;
21791
21792       /* Now, mark any dies referenced from here.  */
21793       prune_unused_types_walk_attribs (die);
21794     }
21795
21796   die->die_mark = 2;
21797
21798   /* Mark children.  */
21799   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21800 }
21801
21802 /* Increment the string counts on strings referred to from DIE's
21803    attributes.  */
21804
21805 static void
21806 prune_unused_types_update_strings (dw_die_ref die)
21807 {
21808   dw_attr_ref a;
21809   unsigned ix;
21810
21811   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21812     if (AT_class (a) == dw_val_class_str)
21813       {
21814         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21815         s->refcount++;
21816         /* Avoid unnecessarily putting strings that are used less than
21817            twice in the hash table.  */
21818         if (s->refcount
21819             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21820           {
21821             void ** slot;
21822             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21823                                              htab_hash_string (s->str),
21824                                              INSERT);
21825             gcc_assert (*slot == NULL);
21826             *slot = s;
21827           }
21828       }
21829 }
21830
21831 /* Remove from the tree DIE any dies that aren't marked.  */
21832
21833 static void
21834 prune_unused_types_prune (dw_die_ref die)
21835 {
21836   dw_die_ref c;
21837
21838   gcc_assert (die->die_mark);
21839   prune_unused_types_update_strings (die);
21840
21841   if (! die->die_child)
21842     return;
21843
21844   c = die->die_child;
21845   do {
21846     dw_die_ref prev = c;
21847     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21848       if (c == die->die_child)
21849         {
21850           /* No marked children between 'prev' and the end of the list.  */
21851           if (prev == c)
21852             /* No marked children at all.  */
21853             die->die_child = NULL;
21854           else
21855             {
21856               prev->die_sib = c->die_sib;
21857               die->die_child = prev;
21858             }
21859           return;
21860         }
21861
21862     if (c != prev->die_sib)
21863       prev->die_sib = c;
21864     prune_unused_types_prune (c);
21865   } while (c != die->die_child);
21866 }
21867
21868 /* A helper function for dwarf2out_finish called through
21869    htab_traverse.  Clear .debug_str strings that we haven't already
21870    decided to emit.  */
21871
21872 static int
21873 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21874 {
21875   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21876
21877   if (!node->label || !node->refcount)
21878     htab_clear_slot (debug_str_hash, h);
21879
21880   return 1;
21881 }
21882
21883 /* Remove dies representing declarations that we never use.  */
21884
21885 static void
21886 prune_unused_types (void)
21887 {
21888   unsigned int i;
21889   limbo_die_node *node;
21890   comdat_type_node *ctnode;
21891   pubname_ref pub;
21892   dcall_entry *dcall;
21893
21894 #if ENABLE_ASSERT_CHECKING
21895   /* All the marks should already be clear.  */
21896   verify_marks_clear (comp_unit_die);
21897   for (node = limbo_die_list; node; node = node->next)
21898     verify_marks_clear (node->die);
21899   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21900     verify_marks_clear (ctnode->root_die);
21901 #endif /* ENABLE_ASSERT_CHECKING */
21902
21903   /* Mark types that are used in global variables.  */
21904   premark_types_used_by_global_vars ();
21905
21906   /* Set the mark on nodes that are actually used.  */
21907   prune_unused_types_walk (comp_unit_die);
21908   for (node = limbo_die_list; node; node = node->next)
21909     prune_unused_types_walk (node->die);
21910   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21911     {
21912       prune_unused_types_walk (ctnode->root_die);
21913       prune_unused_types_mark (ctnode->type_die, 1);
21914     }
21915
21916   /* Also set the mark on nodes referenced from the
21917      pubname_table or arange_table.  */
21918   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
21919     prune_unused_types_mark (pub->die, 1);
21920   for (i = 0; i < arange_table_in_use; i++)
21921     prune_unused_types_mark (arange_table[i], 1);
21922
21923   /* Mark nodes referenced from the direct call table.  */
21924   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
21925     prune_unused_types_mark (dcall->targ_die, 1);
21926
21927   /* Get rid of nodes that aren't marked; and update the string counts.  */
21928   if (debug_str_hash && debug_str_hash_forced)
21929     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
21930   else if (debug_str_hash)
21931     htab_empty (debug_str_hash);
21932   prune_unused_types_prune (comp_unit_die);
21933   for (node = limbo_die_list; node; node = node->next)
21934     prune_unused_types_prune (node->die);
21935   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21936     prune_unused_types_prune (ctnode->root_die);
21937
21938   /* Leave the marks clear.  */
21939   prune_unmark_dies (comp_unit_die);
21940   for (node = limbo_die_list; node; node = node->next)
21941     prune_unmark_dies (node->die);
21942   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21943     prune_unmark_dies (ctnode->root_die);
21944 }
21945
21946 /* Set the parameter to true if there are any relative pathnames in
21947    the file table.  */
21948 static int
21949 file_table_relative_p (void ** slot, void *param)
21950 {
21951   bool *p = (bool *) param;
21952   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21953   if (!IS_ABSOLUTE_PATH (d->filename))
21954     {
21955       *p = true;
21956       return 0;
21957     }
21958   return 1;
21959 }
21960
21961 /* Routines to manipulate hash table of comdat type units.  */
21962
21963 static hashval_t
21964 htab_ct_hash (const void *of)
21965 {
21966   hashval_t h;
21967   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21968
21969   memcpy (&h, type_node->signature, sizeof (h));
21970   return h;
21971 }
21972
21973 static int
21974 htab_ct_eq (const void *of1, const void *of2)
21975 {
21976   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21977   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21978
21979   return (! memcmp (type_node_1->signature, type_node_2->signature,
21980                     DWARF_TYPE_SIGNATURE_SIZE));
21981 }
21982
21983 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21984    to the location it would have been added, should we know its
21985    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21986    probably improve compactness of debug info, removing equivalent
21987    abbrevs, and hide any differences caused by deferring the
21988    computation of the assembler name, triggered by e.g. PCH.  */
21989
21990 static inline void
21991 move_linkage_attr (dw_die_ref die)
21992 {
21993   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21994   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21995
21996   gcc_assert (linkage.dw_attr == AT_linkage_name);
21997
21998   while (--ix > 0)
21999     {
22000       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22001
22002       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22003         break;
22004     }
22005
22006   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22007     {
22008       VEC_pop (dw_attr_node, die->die_attr);
22009       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22010     }
22011 }
22012
22013 /* Helper function for resolve_addr, attempt to resolve
22014    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22015    SYMBOL_REFs refer to variables emitted in the current CU.  */
22016
22017 static int
22018 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22019 {
22020   rtx rtl = *addr;
22021
22022   if (GET_CODE (rtl) == CONST_STRING)
22023     {
22024       size_t len = strlen (XSTR (rtl, 0)) + 1;
22025       tree t = build_string (len, XSTR (rtl, 0));
22026       tree tlen = build_int_cst (NULL_TREE, len - 1);
22027       TREE_TYPE (t)
22028         = build_array_type (char_type_node, build_index_type (tlen));
22029       rtl = lookup_constant_def (t);
22030       if (!rtl || !MEM_P (rtl))
22031         return 1;
22032       rtl = XEXP (rtl, 0);
22033       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22034       *addr = rtl;
22035       return 0;
22036     }
22037
22038   if (GET_CODE (rtl) == SYMBOL_REF
22039       && SYMBOL_REF_DECL (rtl)
22040       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22041     return 1;
22042
22043   if (GET_CODE (rtl) == CONST
22044       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22045     return 1;
22046
22047   return 0;
22048 }
22049
22050 /* Helper function for resolve_addr, handle one location
22051    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22052    the location list couldn't be resolved.  */
22053
22054 static bool
22055 resolve_addr_in_expr (dw_loc_descr_ref loc)
22056 {
22057   for (; loc; loc = loc->dw_loc_next)
22058     if ((loc->dw_loc_opc == DW_OP_addr
22059          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22060         || (loc->dw_loc_opc == DW_OP_implicit_value
22061             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22062             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22063       return false;
22064   return true;
22065 }
22066
22067 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22068    an address in .rodata section if the string literal is emitted there,
22069    or remove the containing location list or replace DW_AT_const_value
22070    with DW_AT_location and empty location expression, if it isn't found
22071    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22072    to something that has been emitted in the current CU.  */
22073
22074 static void
22075 resolve_addr (dw_die_ref die)
22076 {
22077   dw_die_ref c;
22078   dw_attr_ref a;
22079   dw_loc_list_ref *curr;
22080   unsigned ix;
22081
22082   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
22083     switch (AT_class (a))
22084       {
22085       case dw_val_class_loc_list:
22086         curr = AT_loc_list_ptr (a);
22087         while (*curr)
22088           {
22089             if (!resolve_addr_in_expr ((*curr)->expr))
22090               {
22091                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22092                 if (next && (*curr)->ll_symbol)
22093                   {
22094                     gcc_assert (!next->ll_symbol);
22095                     next->ll_symbol = (*curr)->ll_symbol;
22096                   }
22097                 *curr = next;
22098               }
22099             else
22100               curr = &(*curr)->dw_loc_next;
22101           }
22102         if (!AT_loc_list (a))
22103           {
22104             remove_AT (die, a->dw_attr);
22105             ix--;
22106           }
22107         break;
22108       case dw_val_class_loc:
22109         if (!resolve_addr_in_expr (AT_loc (a)))
22110           {
22111             remove_AT (die, a->dw_attr);
22112             ix--;
22113           }
22114         break;
22115       case dw_val_class_addr:
22116         if (a->dw_attr == DW_AT_const_value
22117             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22118           {
22119             remove_AT (die, a->dw_attr);
22120             ix--;
22121           }
22122         break;
22123       default:
22124         break;
22125       }
22126
22127   FOR_EACH_CHILD (die, c, resolve_addr (c));
22128 }
22129
22130 /* Output stuff that dwarf requires at the end of every file,
22131    and generate the DWARF-2 debugging info.  */
22132
22133 static void
22134 dwarf2out_finish (const char *filename)
22135 {
22136   limbo_die_node *node, *next_node;
22137   comdat_type_node *ctnode;
22138   htab_t comdat_type_table;
22139   dw_die_ref die = 0;
22140   unsigned int i;
22141
22142   gen_remaining_tmpl_value_param_die_attribute ();
22143
22144   /* Add the name for the main input file now.  We delayed this from
22145      dwarf2out_init to avoid complications with PCH.  */
22146   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22147   if (!IS_ABSOLUTE_PATH (filename))
22148     add_comp_dir_attribute (comp_unit_die);
22149   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22150     {
22151       bool p = false;
22152       htab_traverse (file_table, file_table_relative_p, &p);
22153       if (p)
22154         add_comp_dir_attribute (comp_unit_die);
22155     }
22156
22157   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22158     {
22159       add_location_or_const_value_attribute (
22160         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22161         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22162         DW_AT_location);
22163     }
22164
22165   /* Traverse the limbo die list, and add parent/child links.  The only
22166      dies without parents that should be here are concrete instances of
22167      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22168      For concrete instances, we can get the parent die from the abstract
22169      instance.  */
22170   for (node = limbo_die_list; node; node = next_node)
22171     {
22172       next_node = node->next;
22173       die = node->die;
22174
22175       if (die->die_parent == NULL)
22176         {
22177           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22178
22179           if (origin)
22180             add_child_die (origin->die_parent, die);
22181           else if (die == comp_unit_die)
22182             ;
22183           else if (seen_error ())
22184             /* It's OK to be confused by errors in the input.  */
22185             add_child_die (comp_unit_die, die);
22186           else
22187             {
22188               /* In certain situations, the lexical block containing a
22189                  nested function can be optimized away, which results
22190                  in the nested function die being orphaned.  Likewise
22191                  with the return type of that nested function.  Force
22192                  this to be a child of the containing function.
22193
22194                  It may happen that even the containing function got fully
22195                  inlined and optimized out.  In that case we are lost and
22196                  assign the empty child.  This should not be big issue as
22197                  the function is likely unreachable too.  */
22198               tree context = NULL_TREE;
22199
22200               gcc_assert (node->created_for);
22201
22202               if (DECL_P (node->created_for))
22203                 context = DECL_CONTEXT (node->created_for);
22204               else if (TYPE_P (node->created_for))
22205                 context = TYPE_CONTEXT (node->created_for);
22206
22207               gcc_assert (context
22208                           && (TREE_CODE (context) == FUNCTION_DECL
22209                               || TREE_CODE (context) == NAMESPACE_DECL));
22210
22211               origin = lookup_decl_die (context);
22212               if (origin)
22213                 add_child_die (origin, die);
22214               else
22215                 add_child_die (comp_unit_die, die);
22216             }
22217         }
22218     }
22219
22220   limbo_die_list = NULL;
22221
22222   resolve_addr (comp_unit_die);
22223
22224   for (node = deferred_asm_name; node; node = node->next)
22225     {
22226       tree decl = node->created_for;
22227       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22228         {
22229           add_AT_string (node->die, AT_linkage_name,
22230                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
22231           move_linkage_attr (node->die);
22232         }
22233     }
22234
22235   deferred_asm_name = NULL;
22236
22237   /* Walk through the list of incomplete types again, trying once more to
22238      emit full debugging info for them.  */
22239   retry_incomplete_types ();
22240
22241   if (flag_eliminate_unused_debug_types)
22242     prune_unused_types ();
22243
22244   /* Generate separate CUs for each of the include files we've seen.
22245      They will go into limbo_die_list.  */
22246   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22247     break_out_includes (comp_unit_die);
22248
22249   /* Generate separate COMDAT sections for type DIEs. */
22250   if (dwarf_version >= 4)
22251     {
22252       break_out_comdat_types (comp_unit_die);
22253
22254       /* Each new type_unit DIE was added to the limbo die list when created.
22255          Since these have all been added to comdat_type_list, clear the
22256          limbo die list.  */
22257       limbo_die_list = NULL;
22258
22259       /* For each new comdat type unit, copy declarations for incomplete
22260          types to make the new unit self-contained (i.e., no direct
22261          references to the main compile unit).  */
22262       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22263         copy_decls_for_unworthy_types (ctnode->root_die);
22264       copy_decls_for_unworthy_types (comp_unit_die);
22265
22266       /* In the process of copying declarations from one unit to another,
22267          we may have left some declarations behind that are no longer
22268          referenced.  Prune them.  */
22269       prune_unused_types ();
22270     }
22271
22272   /* Traverse the DIE's and add add sibling attributes to those DIE's
22273      that have children.  */
22274   add_sibling_attributes (comp_unit_die);
22275   for (node = limbo_die_list; node; node = node->next)
22276     add_sibling_attributes (node->die);
22277   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22278     add_sibling_attributes (ctnode->root_die);
22279
22280   /* Output a terminator label for the .text section.  */
22281   switch_to_section (text_section);
22282   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22283   if (flag_reorder_blocks_and_partition)
22284     {
22285       switch_to_section (unlikely_text_section ());
22286       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22287     }
22288
22289   /* We can only use the low/high_pc attributes if all of the code was
22290      in .text.  */
22291   if (!have_multiple_function_sections
22292       || !(dwarf_version >= 3 || !dwarf_strict))
22293     {
22294       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22295       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22296     }
22297
22298   else
22299     {
22300       unsigned fde_idx = 0;
22301       bool range_list_added = false;
22302
22303       /* We need to give .debug_loc and .debug_ranges an appropriate
22304          "base address".  Use zero so that these addresses become
22305          absolute.  Historically, we've emitted the unexpected
22306          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22307          Emit both to give time for other tools to adapt.  */
22308       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22309       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22310
22311       if (text_section_used)
22312         add_ranges_by_labels (comp_unit_die, text_section_label,
22313                               text_end_label, &range_list_added);
22314       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22315         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22316                               cold_end_label, &range_list_added);
22317
22318       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22319         {
22320           dw_fde_ref fde = &fde_table[fde_idx];
22321
22322           if (fde->dw_fde_switched_sections)
22323             {
22324               if (!fde->in_std_section)
22325                 add_ranges_by_labels (comp_unit_die,
22326                                       fde->dw_fde_hot_section_label,
22327                                       fde->dw_fde_hot_section_end_label,
22328                                       &range_list_added);
22329               if (!fde->cold_in_std_section)
22330                 add_ranges_by_labels (comp_unit_die,
22331                                       fde->dw_fde_unlikely_section_label,
22332                                       fde->dw_fde_unlikely_section_end_label,
22333                                       &range_list_added);
22334             }
22335           else if (!fde->in_std_section)
22336             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22337                                   fde->dw_fde_end, &range_list_added);
22338         }
22339
22340       if (range_list_added)
22341         add_ranges (NULL);
22342     }
22343
22344   /* Output location list section if necessary.  */
22345   if (have_location_lists)
22346     {
22347       /* Output the location lists info.  */
22348       switch_to_section (debug_loc_section);
22349       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22350                                    DEBUG_LOC_SECTION_LABEL, 0);
22351       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22352       output_location_lists (die);
22353     }
22354
22355   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22356     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22357                     debug_line_section_label);
22358
22359   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22360     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22361
22362   /* Output all of the compilation units.  We put the main one last so that
22363      the offsets are available to output_pubnames.  */
22364   for (node = limbo_die_list; node; node = node->next)
22365     output_comp_unit (node->die, 0);
22366
22367   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22368   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22369     {
22370       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22371
22372       /* Don't output duplicate types.  */
22373       if (*slot != HTAB_EMPTY_ENTRY)
22374         continue;
22375
22376       /* Add a pointer to the line table for the main compilation unit
22377          so that the debugger can make sense of DW_AT_decl_file
22378          attributes.  */
22379       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22380         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22381                         debug_line_section_label);
22382
22383       output_comdat_type_unit (ctnode);
22384       *slot = ctnode;
22385     }
22386   htab_delete (comdat_type_table);
22387
22388   /* Output the main compilation unit if non-empty or if .debug_macinfo
22389      has been emitted.  */
22390   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22391
22392   /* Output the abbreviation table.  */
22393   switch_to_section (debug_abbrev_section);
22394   output_abbrev_section ();
22395
22396   /* Output public names table if necessary.  */
22397   if (!VEC_empty (pubname_entry, pubname_table))
22398     {
22399       switch_to_section (debug_pubnames_section);
22400       output_pubnames (pubname_table);
22401     }
22402
22403   /* Output public types table if necessary.  */
22404   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22405      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22406      simply won't look for the section.  */
22407   if (!VEC_empty (pubname_entry, pubtype_table))
22408     {
22409       switch_to_section (debug_pubtypes_section);
22410       output_pubnames (pubtype_table);
22411     }
22412
22413   /* Output direct and virtual call tables if necessary.  */
22414   if (!VEC_empty (dcall_entry, dcall_table))
22415     {
22416       switch_to_section (debug_dcall_section);
22417       output_dcall_table ();
22418     }
22419   if (!VEC_empty (vcall_entry, vcall_table))
22420     {
22421       switch_to_section (debug_vcall_section);
22422       output_vcall_table ();
22423     }
22424
22425   /* Output the address range information.  We only put functions in the arange
22426      table, so don't write it out if we don't have any.  */
22427   if (fde_table_in_use)
22428     {
22429       switch_to_section (debug_aranges_section);
22430       output_aranges ();
22431     }
22432
22433   /* Output ranges section if necessary.  */
22434   if (ranges_table_in_use)
22435     {
22436       switch_to_section (debug_ranges_section);
22437       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22438       output_ranges ();
22439     }
22440
22441   /* Output the source line correspondence table.  We must do this
22442      even if there is no line information.  Otherwise, on an empty
22443      translation unit, we will generate a present, but empty,
22444      .debug_info section.  IRIX 6.5 `nm' will then complain when
22445      examining the file.  This is done late so that any filenames
22446      used by the debug_info section are marked as 'used'.  */
22447   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22448     {
22449       switch_to_section (debug_line_section);
22450       output_line_info ();
22451     }
22452
22453   /* Have to end the macro section.  */
22454   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22455     {
22456       switch_to_section (debug_macinfo_section);
22457       dw2_asm_output_data (1, 0, "End compilation unit");
22458     }
22459
22460   /* If we emitted any DW_FORM_strp form attribute, output the string
22461      table too.  */
22462   if (debug_str_hash)
22463     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22464 }
22465 #else
22466
22467 /* This should never be used, but its address is needed for comparisons.  */
22468 const struct gcc_debug_hooks dwarf2_debug_hooks =
22469 {
22470   0,            /* init */
22471   0,            /* finish */
22472   0,            /* assembly_start */
22473   0,            /* define */
22474   0,            /* undef */
22475   0,            /* start_source_file */
22476   0,            /* end_source_file */
22477   0,            /* begin_block */
22478   0,            /* end_block */
22479   0,            /* ignore_block */
22480   0,            /* source_line */
22481   0,            /* begin_prologue */
22482   0,            /* end_prologue */
22483   0,            /* begin_epilogue */
22484   0,            /* end_epilogue */
22485   0,            /* begin_function */
22486   0,            /* end_function */
22487   0,            /* function_decl */
22488   0,            /* global_decl */
22489   0,            /* type_decl */
22490   0,            /* imported_module_or_decl */
22491   0,            /* deferred_inline_function */
22492   0,            /* outlining_inline_function */
22493   0,            /* label */
22494   0,            /* handle_pch */
22495   0,            /* var_location */
22496   0,            /* switch_text_section */
22497   0,            /* direct_call */
22498   0,            /* virtual_call_token */
22499   0,            /* copy_call_info */
22500   0,            /* virtual_call */
22501   0,            /* set_name */
22502   0             /* start_end_main_source_file */
22503 };
22504
22505 #endif /* DWARF2_DEBUGGING_INFO */
22506
22507 #include "gt-dwarf2out.h"