OSDN Git Service

IA64 uses // instead of # for comments in its assembly file.
[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       if (loc->dtprel)
4932         {
4933           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4934           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4935                                                val1->v.val_addr);
4936           fputc ('\n', asm_out_file);
4937           break;
4938         }
4939       /* FALLTHRU */
4940     case DW_OP_const4s:
4941       dw2_asm_output_data (4, val1->v.val_int, NULL);
4942       break;
4943     case DW_OP_const8u:
4944       if (loc->dtprel)
4945         {
4946           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4947           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4948                                                val1->v.val_addr);
4949           fputc ('\n', asm_out_file);
4950           break;
4951         }
4952       /* FALLTHRU */
4953     case DW_OP_const8s:
4954       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4955       dw2_asm_output_data (8, val1->v.val_int, NULL);
4956       break;
4957     case DW_OP_skip:
4958     case DW_OP_bra:
4959       {
4960         int offset;
4961
4962         gcc_assert (val1->val_class == dw_val_class_loc);
4963         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4964
4965         dw2_asm_output_data (2, offset, NULL);
4966       }
4967       break;
4968     case DW_OP_implicit_value:
4969       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4970       switch (val2->val_class)
4971         {
4972         case dw_val_class_const:
4973           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4974           break;
4975         case dw_val_class_vec:
4976           {
4977             unsigned int elt_size = val2->v.val_vec.elt_size;
4978             unsigned int len = val2->v.val_vec.length;
4979             unsigned int i;
4980             unsigned char *p;
4981
4982             if (elt_size > sizeof (HOST_WIDE_INT))
4983               {
4984                 elt_size /= 2;
4985                 len *= 2;
4986               }
4987             for (i = 0, p = val2->v.val_vec.array;
4988                  i < len;
4989                  i++, p += elt_size)
4990               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4991                                    "fp or vector constant word %u", i);
4992           }
4993           break;
4994         case dw_val_class_const_double:
4995           {
4996             unsigned HOST_WIDE_INT first, second;
4997
4998             if (WORDS_BIG_ENDIAN)
4999               {
5000                 first = val2->v.val_double.high;
5001                 second = val2->v.val_double.low;
5002               }
5003             else
5004               {
5005                 first = val2->v.val_double.low;
5006                 second = val2->v.val_double.high;
5007               }
5008             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5009                                  first, NULL);
5010             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5011                                  second, NULL);
5012           }
5013           break;
5014         case dw_val_class_addr:
5015           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5016           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5017           break;
5018         default:
5019           gcc_unreachable ();
5020         }
5021       break;
5022 #else
5023     case DW_OP_const2u:
5024     case DW_OP_const2s:
5025     case DW_OP_const4u:
5026     case DW_OP_const4s:
5027     case DW_OP_const8u:
5028     case DW_OP_const8s:
5029     case DW_OP_skip:
5030     case DW_OP_bra:
5031     case DW_OP_implicit_value:
5032       /* We currently don't make any attempt to make sure these are
5033          aligned properly like we do for the main unwind info, so
5034          don't support emitting things larger than a byte if we're
5035          only doing unwinding.  */
5036       gcc_unreachable ();
5037 #endif
5038     case DW_OP_const1u:
5039     case DW_OP_const1s:
5040       dw2_asm_output_data (1, val1->v.val_int, NULL);
5041       break;
5042     case DW_OP_constu:
5043       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5044       break;
5045     case DW_OP_consts:
5046       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5047       break;
5048     case DW_OP_pick:
5049       dw2_asm_output_data (1, val1->v.val_int, NULL);
5050       break;
5051     case DW_OP_plus_uconst:
5052       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5053       break;
5054     case DW_OP_breg0:
5055     case DW_OP_breg1:
5056     case DW_OP_breg2:
5057     case DW_OP_breg3:
5058     case DW_OP_breg4:
5059     case DW_OP_breg5:
5060     case DW_OP_breg6:
5061     case DW_OP_breg7:
5062     case DW_OP_breg8:
5063     case DW_OP_breg9:
5064     case DW_OP_breg10:
5065     case DW_OP_breg11:
5066     case DW_OP_breg12:
5067     case DW_OP_breg13:
5068     case DW_OP_breg14:
5069     case DW_OP_breg15:
5070     case DW_OP_breg16:
5071     case DW_OP_breg17:
5072     case DW_OP_breg18:
5073     case DW_OP_breg19:
5074     case DW_OP_breg20:
5075     case DW_OP_breg21:
5076     case DW_OP_breg22:
5077     case DW_OP_breg23:
5078     case DW_OP_breg24:
5079     case DW_OP_breg25:
5080     case DW_OP_breg26:
5081     case DW_OP_breg27:
5082     case DW_OP_breg28:
5083     case DW_OP_breg29:
5084     case DW_OP_breg30:
5085     case DW_OP_breg31:
5086       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5087       break;
5088     case DW_OP_regx:
5089       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5090       break;
5091     case DW_OP_fbreg:
5092       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5093       break;
5094     case DW_OP_bregx:
5095       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5096       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5097       break;
5098     case DW_OP_piece:
5099       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5100       break;
5101     case DW_OP_bit_piece:
5102       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5103       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5104       break;
5105     case DW_OP_deref_size:
5106     case DW_OP_xderef_size:
5107       dw2_asm_output_data (1, val1->v.val_int, NULL);
5108       break;
5109
5110     case DW_OP_addr:
5111       if (loc->dtprel)
5112         {
5113           if (targetm.asm_out.output_dwarf_dtprel)
5114             {
5115               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5116                                                    DWARF2_ADDR_SIZE,
5117                                                    val1->v.val_addr);
5118               fputc ('\n', asm_out_file);
5119             }
5120           else
5121             gcc_unreachable ();
5122         }
5123       else
5124         {
5125 #ifdef DWARF2_DEBUGGING_INFO
5126           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5127 #else
5128           gcc_unreachable ();
5129 #endif
5130         }
5131       break;
5132
5133     default:
5134       /* Other codes have no operands.  */
5135       break;
5136     }
5137 }
5138
5139 /* Output a sequence of location operations.  */
5140
5141 static void
5142 output_loc_sequence (dw_loc_descr_ref loc)
5143 {
5144   for (; loc != NULL; loc = loc->dw_loc_next)
5145     {
5146       /* Output the opcode.  */
5147       dw2_asm_output_data (1, loc->dw_loc_opc,
5148                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5149
5150       /* Output the operand(s) (if any).  */
5151       output_loc_operands (loc);
5152     }
5153 }
5154
5155 /* Output location description stack opcode's operands (if any).
5156    The output is single bytes on a line, suitable for .cfi_escape.  */
5157
5158 static void
5159 output_loc_operands_raw (dw_loc_descr_ref loc)
5160 {
5161   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5162   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5163
5164   switch (loc->dw_loc_opc)
5165     {
5166     case DW_OP_addr:
5167     case DW_OP_implicit_value:
5168       /* We cannot output addresses in .cfi_escape, only bytes.  */
5169       gcc_unreachable ();
5170
5171     case DW_OP_const1u:
5172     case DW_OP_const1s:
5173     case DW_OP_pick:
5174     case DW_OP_deref_size:
5175     case DW_OP_xderef_size:
5176       fputc (',', asm_out_file);
5177       dw2_asm_output_data_raw (1, val1->v.val_int);
5178       break;
5179
5180     case DW_OP_const2u:
5181     case DW_OP_const2s:
5182       fputc (',', asm_out_file);
5183       dw2_asm_output_data_raw (2, val1->v.val_int);
5184       break;
5185
5186     case DW_OP_const4u:
5187     case DW_OP_const4s:
5188       fputc (',', asm_out_file);
5189       dw2_asm_output_data_raw (4, val1->v.val_int);
5190       break;
5191
5192     case DW_OP_const8u:
5193     case DW_OP_const8s:
5194       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5195       fputc (',', asm_out_file);
5196       dw2_asm_output_data_raw (8, val1->v.val_int);
5197       break;
5198
5199     case DW_OP_skip:
5200     case DW_OP_bra:
5201       {
5202         int offset;
5203
5204         gcc_assert (val1->val_class == dw_val_class_loc);
5205         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5206
5207         fputc (',', asm_out_file);
5208         dw2_asm_output_data_raw (2, offset);
5209       }
5210       break;
5211
5212     case DW_OP_constu:
5213     case DW_OP_plus_uconst:
5214     case DW_OP_regx:
5215     case DW_OP_piece:
5216       fputc (',', asm_out_file);
5217       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5218       break;
5219
5220     case DW_OP_bit_piece:
5221       fputc (',', asm_out_file);
5222       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5223       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5224       break;
5225
5226     case DW_OP_consts:
5227     case DW_OP_breg0:
5228     case DW_OP_breg1:
5229     case DW_OP_breg2:
5230     case DW_OP_breg3:
5231     case DW_OP_breg4:
5232     case DW_OP_breg5:
5233     case DW_OP_breg6:
5234     case DW_OP_breg7:
5235     case DW_OP_breg8:
5236     case DW_OP_breg9:
5237     case DW_OP_breg10:
5238     case DW_OP_breg11:
5239     case DW_OP_breg12:
5240     case DW_OP_breg13:
5241     case DW_OP_breg14:
5242     case DW_OP_breg15:
5243     case DW_OP_breg16:
5244     case DW_OP_breg17:
5245     case DW_OP_breg18:
5246     case DW_OP_breg19:
5247     case DW_OP_breg20:
5248     case DW_OP_breg21:
5249     case DW_OP_breg22:
5250     case DW_OP_breg23:
5251     case DW_OP_breg24:
5252     case DW_OP_breg25:
5253     case DW_OP_breg26:
5254     case DW_OP_breg27:
5255     case DW_OP_breg28:
5256     case DW_OP_breg29:
5257     case DW_OP_breg30:
5258     case DW_OP_breg31:
5259     case DW_OP_fbreg:
5260       fputc (',', asm_out_file);
5261       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5262       break;
5263
5264     case DW_OP_bregx:
5265       fputc (',', asm_out_file);
5266       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5267       fputc (',', asm_out_file);
5268       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5269       break;
5270
5271     default:
5272       /* Other codes have no operands.  */
5273       break;
5274     }
5275 }
5276
5277 static void
5278 output_loc_sequence_raw (dw_loc_descr_ref loc)
5279 {
5280   while (1)
5281     {
5282       /* Output the opcode.  */
5283       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5284       output_loc_operands_raw (loc);
5285
5286       if (!loc->dw_loc_next)
5287         break;
5288       loc = loc->dw_loc_next;
5289
5290       fputc (',', asm_out_file);
5291     }
5292 }
5293
5294 /* This routine will generate the correct assembly data for a location
5295    description based on a cfi entry with a complex address.  */
5296
5297 static void
5298 output_cfa_loc (dw_cfi_ref cfi)
5299 {
5300   dw_loc_descr_ref loc;
5301   unsigned long size;
5302
5303   if (cfi->dw_cfi_opc == DW_CFA_expression)
5304     {
5305       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5306       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5307     }
5308   else
5309     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5310
5311   /* Output the size of the block.  */
5312   size = size_of_locs (loc);
5313   dw2_asm_output_data_uleb128 (size, NULL);
5314
5315   /* Now output the operations themselves.  */
5316   output_loc_sequence (loc);
5317 }
5318
5319 /* Similar, but used for .cfi_escape.  */
5320
5321 static void
5322 output_cfa_loc_raw (dw_cfi_ref cfi)
5323 {
5324   dw_loc_descr_ref loc;
5325   unsigned long size;
5326
5327   if (cfi->dw_cfi_opc == DW_CFA_expression)
5328     {
5329       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5330       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5331     }
5332   else
5333     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5334
5335   /* Output the size of the block.  */
5336   size = size_of_locs (loc);
5337   dw2_asm_output_data_uleb128_raw (size);
5338   fputc (',', asm_out_file);
5339
5340   /* Now output the operations themselves.  */
5341   output_loc_sequence_raw (loc);
5342 }
5343
5344 /* This function builds a dwarf location descriptor sequence from a
5345    dw_cfa_location, adding the given OFFSET to the result of the
5346    expression.  */
5347
5348 static struct dw_loc_descr_struct *
5349 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5350 {
5351   struct dw_loc_descr_struct *head, *tmp;
5352
5353   offset += cfa->offset;
5354
5355   if (cfa->indirect)
5356     {
5357       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5358       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5359       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5360       add_loc_descr (&head, tmp);
5361       if (offset != 0)
5362         {
5363           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5364           add_loc_descr (&head, tmp);
5365         }
5366     }
5367   else
5368     head = new_reg_loc_descr (cfa->reg, offset);
5369
5370   return head;
5371 }
5372
5373 /* This function builds a dwarf location descriptor sequence for
5374    the address at OFFSET from the CFA when stack is aligned to
5375    ALIGNMENT byte.  */
5376
5377 static struct dw_loc_descr_struct *
5378 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5379 {
5380   struct dw_loc_descr_struct *head;
5381   unsigned int dwarf_fp
5382     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5383
5384  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5385   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5386     {
5387       head = new_reg_loc_descr (dwarf_fp, 0);
5388       add_loc_descr (&head, int_loc_descriptor (alignment));
5389       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5390       loc_descr_plus_const (&head, offset);
5391     }
5392   else
5393     head = new_reg_loc_descr (dwarf_fp, offset);
5394   return head;
5395 }
5396
5397 /* This function fills in aa dw_cfa_location structure from a dwarf location
5398    descriptor sequence.  */
5399
5400 static void
5401 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5402 {
5403   struct dw_loc_descr_struct *ptr;
5404   cfa->offset = 0;
5405   cfa->base_offset = 0;
5406   cfa->indirect = 0;
5407   cfa->reg = -1;
5408
5409   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5410     {
5411       enum dwarf_location_atom op = ptr->dw_loc_opc;
5412
5413       switch (op)
5414         {
5415         case DW_OP_reg0:
5416         case DW_OP_reg1:
5417         case DW_OP_reg2:
5418         case DW_OP_reg3:
5419         case DW_OP_reg4:
5420         case DW_OP_reg5:
5421         case DW_OP_reg6:
5422         case DW_OP_reg7:
5423         case DW_OP_reg8:
5424         case DW_OP_reg9:
5425         case DW_OP_reg10:
5426         case DW_OP_reg11:
5427         case DW_OP_reg12:
5428         case DW_OP_reg13:
5429         case DW_OP_reg14:
5430         case DW_OP_reg15:
5431         case DW_OP_reg16:
5432         case DW_OP_reg17:
5433         case DW_OP_reg18:
5434         case DW_OP_reg19:
5435         case DW_OP_reg20:
5436         case DW_OP_reg21:
5437         case DW_OP_reg22:
5438         case DW_OP_reg23:
5439         case DW_OP_reg24:
5440         case DW_OP_reg25:
5441         case DW_OP_reg26:
5442         case DW_OP_reg27:
5443         case DW_OP_reg28:
5444         case DW_OP_reg29:
5445         case DW_OP_reg30:
5446         case DW_OP_reg31:
5447           cfa->reg = op - DW_OP_reg0;
5448           break;
5449         case DW_OP_regx:
5450           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5451           break;
5452         case DW_OP_breg0:
5453         case DW_OP_breg1:
5454         case DW_OP_breg2:
5455         case DW_OP_breg3:
5456         case DW_OP_breg4:
5457         case DW_OP_breg5:
5458         case DW_OP_breg6:
5459         case DW_OP_breg7:
5460         case DW_OP_breg8:
5461         case DW_OP_breg9:
5462         case DW_OP_breg10:
5463         case DW_OP_breg11:
5464         case DW_OP_breg12:
5465         case DW_OP_breg13:
5466         case DW_OP_breg14:
5467         case DW_OP_breg15:
5468         case DW_OP_breg16:
5469         case DW_OP_breg17:
5470         case DW_OP_breg18:
5471         case DW_OP_breg19:
5472         case DW_OP_breg20:
5473         case DW_OP_breg21:
5474         case DW_OP_breg22:
5475         case DW_OP_breg23:
5476         case DW_OP_breg24:
5477         case DW_OP_breg25:
5478         case DW_OP_breg26:
5479         case DW_OP_breg27:
5480         case DW_OP_breg28:
5481         case DW_OP_breg29:
5482         case DW_OP_breg30:
5483         case DW_OP_breg31:
5484           cfa->reg = op - DW_OP_breg0;
5485           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5486           break;
5487         case DW_OP_bregx:
5488           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5489           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5490           break;
5491         case DW_OP_deref:
5492           cfa->indirect = 1;
5493           break;
5494         case DW_OP_plus_uconst:
5495           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5496           break;
5497         default:
5498           internal_error ("DW_LOC_OP %s not implemented",
5499                           dwarf_stack_op_name (ptr->dw_loc_opc));
5500         }
5501     }
5502 }
5503 #endif /* .debug_frame support */
5504 \f
5505 /* And now, the support for symbolic debugging information.  */
5506 #ifdef DWARF2_DEBUGGING_INFO
5507
5508 /* .debug_str support.  */
5509 static int output_indirect_string (void **, void *);
5510
5511 static void dwarf2out_init (const char *);
5512 static void dwarf2out_finish (const char *);
5513 static void dwarf2out_assembly_start (void);
5514 static void dwarf2out_define (unsigned int, const char *);
5515 static void dwarf2out_undef (unsigned int, const char *);
5516 static void dwarf2out_start_source_file (unsigned, const char *);
5517 static void dwarf2out_end_source_file (unsigned);
5518 static void dwarf2out_function_decl (tree);
5519 static void dwarf2out_begin_block (unsigned, unsigned);
5520 static void dwarf2out_end_block (unsigned, unsigned);
5521 static bool dwarf2out_ignore_block (const_tree);
5522 static void dwarf2out_global_decl (tree);
5523 static void dwarf2out_type_decl (tree, int);
5524 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5525 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5526                                                  dw_die_ref);
5527 static void dwarf2out_abstract_function (tree);
5528 static void dwarf2out_var_location (rtx);
5529 static void dwarf2out_direct_call (tree);
5530 static void dwarf2out_virtual_call_token (tree, int);
5531 static void dwarf2out_copy_call_info (rtx, rtx);
5532 static void dwarf2out_virtual_call (int);
5533 static void dwarf2out_begin_function (tree);
5534 static void dwarf2out_set_name (tree, tree);
5535
5536 /* The debug hooks structure.  */
5537
5538 const struct gcc_debug_hooks dwarf2_debug_hooks =
5539 {
5540   dwarf2out_init,
5541   dwarf2out_finish,
5542   dwarf2out_assembly_start,
5543   dwarf2out_define,
5544   dwarf2out_undef,
5545   dwarf2out_start_source_file,
5546   dwarf2out_end_source_file,
5547   dwarf2out_begin_block,
5548   dwarf2out_end_block,
5549   dwarf2out_ignore_block,
5550   dwarf2out_source_line,
5551   dwarf2out_begin_prologue,
5552 #if VMS_DEBUGGING_INFO
5553   dwarf2out_vms_end_prologue,
5554   dwarf2out_vms_begin_epilogue,
5555 #else
5556   debug_nothing_int_charstar,
5557   debug_nothing_int_charstar,
5558 #endif
5559   dwarf2out_end_epilogue,
5560   dwarf2out_begin_function,
5561   debug_nothing_int,            /* end_function */
5562   dwarf2out_function_decl,      /* function_decl */
5563   dwarf2out_global_decl,
5564   dwarf2out_type_decl,          /* type_decl */
5565   dwarf2out_imported_module_or_decl,
5566   debug_nothing_tree,           /* deferred_inline_function */
5567   /* The DWARF 2 backend tries to reduce debugging bloat by not
5568      emitting the abstract description of inline functions until
5569      something tries to reference them.  */
5570   dwarf2out_abstract_function,  /* outlining_inline_function */
5571   debug_nothing_rtx,            /* label */
5572   debug_nothing_int,            /* handle_pch */
5573   dwarf2out_var_location,
5574   dwarf2out_switch_text_section,
5575   dwarf2out_direct_call,
5576   dwarf2out_virtual_call_token,
5577   dwarf2out_copy_call_info,
5578   dwarf2out_virtual_call,
5579   dwarf2out_set_name,
5580   1                             /* start_end_main_source_file */
5581 };
5582 #endif
5583 \f
5584 /* NOTE: In the comments in this file, many references are made to
5585    "Debugging Information Entries".  This term is abbreviated as `DIE'
5586    throughout the remainder of this file.  */
5587
5588 /* An internal representation of the DWARF output is built, and then
5589    walked to generate the DWARF debugging info.  The walk of the internal
5590    representation is done after the entire program has been compiled.
5591    The types below are used to describe the internal representation.  */
5592
5593 /* Various DIE's use offsets relative to the beginning of the
5594    .debug_info section to refer to each other.  */
5595
5596 typedef long int dw_offset;
5597
5598 /* Define typedefs here to avoid circular dependencies.  */
5599
5600 typedef struct dw_attr_struct *dw_attr_ref;
5601 typedef struct dw_line_info_struct *dw_line_info_ref;
5602 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5603 typedef struct pubname_struct *pubname_ref;
5604 typedef struct dw_ranges_struct *dw_ranges_ref;
5605 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5606 typedef struct comdat_type_struct *comdat_type_node_ref;
5607
5608 /* Each entry in the line_info_table maintains the file and
5609    line number associated with the label generated for that
5610    entry.  The label gives the PC value associated with
5611    the line number entry.  */
5612
5613 typedef struct GTY(()) dw_line_info_struct {
5614   unsigned long dw_file_num;
5615   unsigned long dw_line_num;
5616 }
5617 dw_line_info_entry;
5618
5619 /* Line information for functions in separate sections; each one gets its
5620    own sequence.  */
5621 typedef struct GTY(()) dw_separate_line_info_struct {
5622   unsigned long dw_file_num;
5623   unsigned long dw_line_num;
5624   unsigned long function;
5625 }
5626 dw_separate_line_info_entry;
5627
5628 /* Each DIE attribute has a field specifying the attribute kind,
5629    a link to the next attribute in the chain, and an attribute value.
5630    Attributes are typically linked below the DIE they modify.  */
5631
5632 typedef struct GTY(()) dw_attr_struct {
5633   enum dwarf_attribute dw_attr;
5634   dw_val_node dw_attr_val;
5635 }
5636 dw_attr_node;
5637
5638 DEF_VEC_O(dw_attr_node);
5639 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5640
5641 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5642    The children of each node form a circular list linked by
5643    die_sib.  die_child points to the node *before* the "first" child node.  */
5644
5645 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5646   enum dwarf_tag die_tag;
5647   union die_symbol_or_type_node
5648     {
5649       char * GTY ((tag ("0"))) die_symbol;
5650       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5651     }
5652   GTY ((desc ("dwarf_version >= 4"))) die_id;
5653   VEC(dw_attr_node,gc) * die_attr;
5654   dw_die_ref die_parent;
5655   dw_die_ref die_child;
5656   dw_die_ref die_sib;
5657   dw_die_ref die_definition; /* ref from a specification to its definition */
5658   dw_offset die_offset;
5659   unsigned long die_abbrev;
5660   int die_mark;
5661   /* Die is used and must not be pruned as unused.  */
5662   int die_perennial_p;
5663   unsigned int decl_id;
5664 }
5665 die_node;
5666
5667 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5668 #define FOR_EACH_CHILD(die, c, expr) do {       \
5669   c = die->die_child;                           \
5670   if (c) do {                                   \
5671     c = c->die_sib;                             \
5672     expr;                                       \
5673   } while (c != die->die_child);                \
5674 } while (0)
5675
5676 /* The pubname structure */
5677
5678 typedef struct GTY(()) pubname_struct {
5679   dw_die_ref die;
5680   const char *name;
5681 }
5682 pubname_entry;
5683
5684 DEF_VEC_O(pubname_entry);
5685 DEF_VEC_ALLOC_O(pubname_entry, gc);
5686
5687 struct GTY(()) dw_ranges_struct {
5688   /* If this is positive, it's a block number, otherwise it's a
5689      bitwise-negated index into dw_ranges_by_label.  */
5690   int num;
5691 };
5692
5693 struct GTY(()) dw_ranges_by_label_struct {
5694   const char *begin;
5695   const char *end;
5696 };
5697
5698 /* The comdat type node structure.  */
5699 typedef struct GTY(()) comdat_type_struct
5700 {
5701   dw_die_ref root_die;
5702   dw_die_ref type_die;
5703   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5704   struct comdat_type_struct *next;
5705 }
5706 comdat_type_node;
5707
5708 /* The limbo die list structure.  */
5709 typedef struct GTY(()) limbo_die_struct {
5710   dw_die_ref die;
5711   tree created_for;
5712   struct limbo_die_struct *next;
5713 }
5714 limbo_die_node;
5715
5716 typedef struct GTY(()) skeleton_chain_struct
5717 {
5718   dw_die_ref old_die;
5719   dw_die_ref new_die;
5720   struct skeleton_chain_struct *parent;
5721 }
5722 skeleton_chain_node;
5723
5724 /* How to start an assembler comment.  */
5725 #ifndef ASM_COMMENT_START
5726 #define ASM_COMMENT_START ";#"
5727 #endif
5728
5729 /* Define a macro which returns nonzero for a TYPE_DECL which was
5730    implicitly generated for a tagged type.
5731
5732    Note that unlike the gcc front end (which generates a NULL named
5733    TYPE_DECL node for each complete tagged type, each array type, and
5734    each function type node created) the g++ front end generates a
5735    _named_ TYPE_DECL node for each tagged type node created.
5736    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5737    generate a DW_TAG_typedef DIE for them.  */
5738
5739 #define TYPE_DECL_IS_STUB(decl)                         \
5740   (DECL_NAME (decl) == NULL_TREE                        \
5741    || (DECL_ARTIFICIAL (decl)                           \
5742        && is_tagged_type (TREE_TYPE (decl))             \
5743        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5744            /* This is necessary for stub decls that     \
5745               appear in nested inline functions.  */    \
5746            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5747                && (decl_ultimate_origin (decl)          \
5748                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5749
5750 /* Information concerning the compilation unit's programming
5751    language, and compiler version.  */
5752
5753 /* Fixed size portion of the DWARF compilation unit header.  */
5754 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5755   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5756
5757 /* Fixed size portion of the DWARF comdat type unit header.  */
5758 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5759   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5760    + DWARF_OFFSET_SIZE)
5761
5762 /* Fixed size portion of public names info.  */
5763 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5764
5765 /* Fixed size portion of the address range info.  */
5766 #define DWARF_ARANGES_HEADER_SIZE                                       \
5767   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5768                 DWARF2_ADDR_SIZE * 2)                                   \
5769    - DWARF_INITIAL_LENGTH_SIZE)
5770
5771 /* Size of padding portion in the address range info.  It must be
5772    aligned to twice the pointer size.  */
5773 #define DWARF_ARANGES_PAD_SIZE \
5774   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5775                 DWARF2_ADDR_SIZE * 2)                              \
5776    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5777
5778 /* Use assembler line directives if available.  */
5779 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5780 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5781 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5782 #else
5783 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5784 #endif
5785 #endif
5786
5787 /* Minimum line offset in a special line info. opcode.
5788    This value was chosen to give a reasonable range of values.  */
5789 #define DWARF_LINE_BASE  -10
5790
5791 /* First special line opcode - leave room for the standard opcodes.  */
5792 #define DWARF_LINE_OPCODE_BASE  10
5793
5794 /* Range of line offsets in a special line info. opcode.  */
5795 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5796
5797 /* Flag that indicates the initial value of the is_stmt_start flag.
5798    In the present implementation, we do not mark any lines as
5799    the beginning of a source statement, because that information
5800    is not made available by the GCC front-end.  */
5801 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5802
5803 /* Maximum number of operations per instruction bundle.  */
5804 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5805 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5806 #endif
5807
5808 #ifdef DWARF2_DEBUGGING_INFO
5809 /* This location is used by calc_die_sizes() to keep track
5810    the offset of each DIE within the .debug_info section.  */
5811 static unsigned long next_die_offset;
5812 #endif
5813
5814 /* Record the root of the DIE's built for the current compilation unit.  */
5815 static GTY(()) dw_die_ref comp_unit_die;
5816
5817 /* A list of type DIEs that have been separated into comdat sections.  */
5818 static GTY(()) comdat_type_node *comdat_type_list;
5819
5820 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5821 static GTY(()) limbo_die_node *limbo_die_list;
5822
5823 /* A list of DIEs for which we may have to generate
5824    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5825 static GTY(()) limbo_die_node *deferred_asm_name;
5826
5827 /* Filenames referenced by this compilation unit.  */
5828 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5829
5830 /* A hash table of references to DIE's that describe declarations.
5831    The key is a DECL_UID() which is a unique number identifying each decl.  */
5832 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5833
5834 /* A hash table of references to DIE's that describe COMMON blocks.
5835    The key is DECL_UID() ^ die_parent.  */
5836 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5837
5838 typedef struct GTY(()) die_arg_entry_struct {
5839     dw_die_ref die;
5840     tree arg;
5841 } die_arg_entry;
5842
5843 DEF_VEC_O(die_arg_entry);
5844 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5845
5846 /* Node of the variable location list.  */
5847 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5848   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5849      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5850      in mode of the EXPR_LIST node and first EXPR_LIST operand
5851      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5852      location or NULL for padding.  For larger bitsizes,
5853      mode is 0 and first operand is a CONCAT with bitsize
5854      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5855      NULL as second operand.  */
5856   rtx GTY (()) loc;
5857   const char * GTY (()) label;
5858   struct var_loc_node * GTY (()) next;
5859 };
5860
5861 /* Variable location list.  */
5862 struct GTY (()) var_loc_list_def {
5863   struct var_loc_node * GTY (()) first;
5864
5865   /* Pointer to the last but one or last element of the
5866      chained list.  If the list is empty, both first and
5867      last are NULL, if the list contains just one node
5868      or the last node certainly is not redundant, it points
5869      to the last node, otherwise points to the last but one.
5870      Do not mark it for GC because it is marked through the chain.  */
5871   struct var_loc_node * GTY ((skip ("%h"))) last;
5872
5873   /* DECL_UID of the variable decl.  */
5874   unsigned int decl_id;
5875 };
5876 typedef struct var_loc_list_def var_loc_list;
5877
5878
5879 /* Table of decl location linked lists.  */
5880 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5881
5882 /* A pointer to the base of a list of references to DIE's that
5883    are uniquely identified by their tag, presence/absence of
5884    children DIE's, and list of attribute/value pairs.  */
5885 static GTY((length ("abbrev_die_table_allocated")))
5886   dw_die_ref *abbrev_die_table;
5887
5888 /* Number of elements currently allocated for abbrev_die_table.  */
5889 static GTY(()) unsigned abbrev_die_table_allocated;
5890
5891 /* Number of elements in type_die_table currently in use.  */
5892 static GTY(()) unsigned abbrev_die_table_in_use;
5893
5894 /* Size (in elements) of increments by which we may expand the
5895    abbrev_die_table.  */
5896 #define ABBREV_DIE_TABLE_INCREMENT 256
5897
5898 /* A pointer to the base of a table that contains line information
5899    for each source code line in .text in the compilation unit.  */
5900 static GTY((length ("line_info_table_allocated")))
5901      dw_line_info_ref line_info_table;
5902
5903 /* Number of elements currently allocated for line_info_table.  */
5904 static GTY(()) unsigned line_info_table_allocated;
5905
5906 /* Number of elements in line_info_table currently in use.  */
5907 static GTY(()) unsigned line_info_table_in_use;
5908
5909 /* A pointer to the base of a table that contains line information
5910    for each source code line outside of .text in the compilation unit.  */
5911 static GTY ((length ("separate_line_info_table_allocated")))
5912      dw_separate_line_info_ref separate_line_info_table;
5913
5914 /* Number of elements currently allocated for separate_line_info_table.  */
5915 static GTY(()) unsigned separate_line_info_table_allocated;
5916
5917 /* Number of elements in separate_line_info_table currently in use.  */
5918 static GTY(()) unsigned separate_line_info_table_in_use;
5919
5920 /* Size (in elements) of increments by which we may expand the
5921    line_info_table.  */
5922 #define LINE_INFO_TABLE_INCREMENT 1024
5923
5924 /* A pointer to the base of a table that contains a list of publicly
5925    accessible names.  */
5926 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5927
5928 /* A pointer to the base of a table that contains a list of publicly
5929    accessible types.  */
5930 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5931
5932 /* Array of dies for which we should generate .debug_arange info.  */
5933 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5934
5935 /* Number of elements currently allocated for arange_table.  */
5936 static GTY(()) unsigned arange_table_allocated;
5937
5938 /* Number of elements in arange_table currently in use.  */
5939 static GTY(()) unsigned arange_table_in_use;
5940
5941 /* Size (in elements) of increments by which we may expand the
5942    arange_table.  */
5943 #define ARANGE_TABLE_INCREMENT 64
5944
5945 /* Array of dies for which we should generate .debug_ranges info.  */
5946 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5947
5948 /* Number of elements currently allocated for ranges_table.  */
5949 static GTY(()) unsigned ranges_table_allocated;
5950
5951 /* Number of elements in ranges_table currently in use.  */
5952 static GTY(()) unsigned ranges_table_in_use;
5953
5954 /* Array of pairs of labels referenced in ranges_table.  */
5955 static GTY ((length ("ranges_by_label_allocated")))
5956      dw_ranges_by_label_ref ranges_by_label;
5957
5958 /* Number of elements currently allocated for ranges_by_label.  */
5959 static GTY(()) unsigned ranges_by_label_allocated;
5960
5961 /* Number of elements in ranges_by_label currently in use.  */
5962 static GTY(()) unsigned ranges_by_label_in_use;
5963
5964 /* Size (in elements) of increments by which we may expand the
5965    ranges_table.  */
5966 #define RANGES_TABLE_INCREMENT 64
5967
5968 /* Whether we have location lists that need outputting */
5969 static GTY(()) bool have_location_lists;
5970
5971 /* Unique label counter.  */
5972 static GTY(()) unsigned int loclabel_num;
5973
5974 /* Unique label counter for point-of-call tables.  */
5975 static GTY(()) unsigned int poc_label_num;
5976
5977 /* The direct call table structure.  */
5978
5979 typedef struct GTY(()) dcall_struct {
5980   unsigned int poc_label_num;
5981   tree poc_decl;
5982   dw_die_ref targ_die;
5983 }
5984 dcall_entry;
5985
5986 DEF_VEC_O(dcall_entry);
5987 DEF_VEC_ALLOC_O(dcall_entry, gc);
5988
5989 /* The virtual call table structure.  */
5990
5991 typedef struct GTY(()) vcall_struct {
5992   unsigned int poc_label_num;
5993   unsigned int vtable_slot;
5994 }
5995 vcall_entry;
5996
5997 DEF_VEC_O(vcall_entry);
5998 DEF_VEC_ALLOC_O(vcall_entry, gc);
5999
6000 /* Pointers to the direct and virtual call tables.  */
6001 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6002 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6003
6004 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6005
6006 struct GTY (()) vcall_insn {
6007   int insn_uid;
6008   unsigned int vtable_slot;
6009 };
6010
6011 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6012
6013 #ifdef DWARF2_DEBUGGING_INFO
6014 /* Record whether the function being analyzed contains inlined functions.  */
6015 static int current_function_has_inlines;
6016 #endif
6017 #if 0 && defined (MIPS_DEBUGGING_INFO)
6018 static int comp_unit_has_inlines;
6019 #endif
6020
6021 /* The last file entry emitted by maybe_emit_file().  */
6022 static GTY(()) struct dwarf_file_data * last_emitted_file;
6023
6024 /* Number of internal labels generated by gen_internal_sym().  */
6025 static GTY(()) int label_num;
6026
6027 /* Cached result of previous call to lookup_filename.  */
6028 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6029
6030 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6031
6032 #ifdef DWARF2_DEBUGGING_INFO
6033
6034 /* Offset from the "steady-state frame pointer" to the frame base,
6035    within the current function.  */
6036 static HOST_WIDE_INT frame_pointer_fb_offset;
6037
6038 /* Forward declarations for functions defined in this file.  */
6039
6040 static int is_pseudo_reg (const_rtx);
6041 static tree type_main_variant (tree);
6042 static int is_tagged_type (const_tree);
6043 static const char *dwarf_tag_name (unsigned);
6044 static const char *dwarf_attr_name (unsigned);
6045 static const char *dwarf_form_name (unsigned);
6046 static tree decl_ultimate_origin (const_tree);
6047 static tree decl_class_context (tree);
6048 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6049 static inline enum dw_val_class AT_class (dw_attr_ref);
6050 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6051 static inline unsigned AT_flag (dw_attr_ref);
6052 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6053 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6054 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6055 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6056 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6057                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6058 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6059                                unsigned int, unsigned char *);
6060 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6061 static hashval_t debug_str_do_hash (const void *);
6062 static int debug_str_eq (const void *, const void *);
6063 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6064 static inline const char *AT_string (dw_attr_ref);
6065 static enum dwarf_form AT_string_form (dw_attr_ref);
6066 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6067 static void add_AT_specification (dw_die_ref, dw_die_ref);
6068 static inline dw_die_ref AT_ref (dw_attr_ref);
6069 static inline int AT_ref_external (dw_attr_ref);
6070 static inline void set_AT_ref_external (dw_attr_ref, int);
6071 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6072 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6073 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6074 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6075                              dw_loc_list_ref);
6076 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6077 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6078 static inline rtx AT_addr (dw_attr_ref);
6079 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6080 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6081 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6082 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6083                            unsigned HOST_WIDE_INT);
6084 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6085                                unsigned long);
6086 static inline const char *AT_lbl (dw_attr_ref);
6087 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6088 static const char *get_AT_low_pc (dw_die_ref);
6089 static const char *get_AT_hi_pc (dw_die_ref);
6090 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6091 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6092 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6093 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6094 static bool is_cxx (void);
6095 static bool is_fortran (void);
6096 static bool is_ada (void);
6097 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6098 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6099 static void add_child_die (dw_die_ref, dw_die_ref);
6100 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6101 static dw_die_ref lookup_type_die (tree);
6102 static void equate_type_number_to_die (tree, dw_die_ref);
6103 static hashval_t decl_die_table_hash (const void *);
6104 static int decl_die_table_eq (const void *, const void *);
6105 static dw_die_ref lookup_decl_die (tree);
6106 static hashval_t common_block_die_table_hash (const void *);
6107 static int common_block_die_table_eq (const void *, const void *);
6108 static hashval_t decl_loc_table_hash (const void *);
6109 static int decl_loc_table_eq (const void *, const void *);
6110 static var_loc_list *lookup_decl_loc (const_tree);
6111 static void equate_decl_number_to_die (tree, dw_die_ref);
6112 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6113 static void print_spaces (FILE *);
6114 static void print_die (dw_die_ref, FILE *);
6115 static void print_dwarf_line_table (FILE *);
6116 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6117 static dw_die_ref pop_compile_unit (dw_die_ref);
6118 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6119 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6120 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6121 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6122 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6123 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6124 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6125                                    struct md5_ctx *, int *);
6126 struct checksum_attributes;
6127 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6128 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6129 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6130 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6131 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6132 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6133 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6134 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6135 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6136 static void compute_section_prefix (dw_die_ref);
6137 static int is_type_die (dw_die_ref);
6138 static int is_comdat_die (dw_die_ref);
6139 static int is_symbol_die (dw_die_ref);
6140 static void assign_symbol_names (dw_die_ref);
6141 static void break_out_includes (dw_die_ref);
6142 static int is_declaration_die (dw_die_ref);
6143 static int should_move_die_to_comdat (dw_die_ref);
6144 static dw_die_ref clone_as_declaration (dw_die_ref);
6145 static dw_die_ref clone_die (dw_die_ref);
6146 static dw_die_ref clone_tree (dw_die_ref);
6147 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6148 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6149 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6150 static dw_die_ref generate_skeleton (dw_die_ref);
6151 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6152                                                          dw_die_ref);
6153 static void break_out_comdat_types (dw_die_ref);
6154 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6155 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6156 static void copy_decls_for_unworthy_types (dw_die_ref);
6157
6158 static hashval_t htab_cu_hash (const void *);
6159 static int htab_cu_eq (const void *, const void *);
6160 static void htab_cu_del (void *);
6161 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6162 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6163 static void add_sibling_attributes (dw_die_ref);
6164 static void build_abbrev_table (dw_die_ref);
6165 static void output_location_lists (dw_die_ref);
6166 static int constant_size (unsigned HOST_WIDE_INT);
6167 static unsigned long size_of_die (dw_die_ref);
6168 static void calc_die_sizes (dw_die_ref);
6169 static void mark_dies (dw_die_ref);
6170 static void unmark_dies (dw_die_ref);
6171 static void unmark_all_dies (dw_die_ref);
6172 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6173 static unsigned long size_of_aranges (void);
6174 static enum dwarf_form value_format (dw_attr_ref);
6175 static void output_value_format (dw_attr_ref);
6176 static void output_abbrev_section (void);
6177 static void output_die_symbol (dw_die_ref);
6178 static void output_die (dw_die_ref);
6179 static void output_compilation_unit_header (void);
6180 static void output_comp_unit (dw_die_ref, int);
6181 static void output_comdat_type_unit (comdat_type_node *);
6182 static const char *dwarf2_name (tree, int);
6183 static void add_pubname (tree, dw_die_ref);
6184 static void add_pubname_string (const char *, dw_die_ref);
6185 static void add_pubtype (tree, dw_die_ref);
6186 static void output_pubnames (VEC (pubname_entry,gc) *);
6187 static void add_arange (tree, dw_die_ref);
6188 static void output_aranges (void);
6189 static unsigned int add_ranges_num (int);
6190 static unsigned int add_ranges (const_tree);
6191 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6192                                   bool *);
6193 static void output_ranges (void);
6194 static void output_line_info (void);
6195 static void output_file_names (void);
6196 static dw_die_ref base_type_die (tree);
6197 static int is_base_type (tree);
6198 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6199 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6200 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6201 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6202 static int type_is_enum (const_tree);
6203 static unsigned int dbx_reg_number (const_rtx);
6204 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6205 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6206 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6207                                                 enum var_init_status);
6208 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6209                                                      enum var_init_status);
6210 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6211                                          enum var_init_status);
6212 static int is_based_loc (const_rtx);
6213 static int resolve_one_addr (rtx *, void *);
6214 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6215                                             enum var_init_status);
6216 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6217                                                enum var_init_status);
6218 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6219                                         enum var_init_status);
6220 static dw_loc_list_ref loc_list_from_tree (tree, int);
6221 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6222 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6223 static tree field_type (const_tree);
6224 static unsigned int simple_type_align_in_bits (const_tree);
6225 static unsigned int simple_decl_align_in_bits (const_tree);
6226 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6227 static HOST_WIDE_INT field_byte_offset (const_tree);
6228 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6229                                          dw_loc_list_ref);
6230 static void add_data_member_location_attribute (dw_die_ref, tree);
6231 static bool add_const_value_attribute (dw_die_ref, rtx);
6232 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6233 static void insert_double (double_int, unsigned char *);
6234 static void insert_float (const_rtx, unsigned char *);
6235 static rtx rtl_for_decl_location (tree);
6236 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6237                                                    enum dwarf_attribute);
6238 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6239 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6240 static void add_name_attribute (dw_die_ref, const char *);
6241 static void add_comp_dir_attribute (dw_die_ref);
6242 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6243 static void add_subscript_info (dw_die_ref, tree, bool);
6244 static void add_byte_size_attribute (dw_die_ref, tree);
6245 static void add_bit_offset_attribute (dw_die_ref, tree);
6246 static void add_bit_size_attribute (dw_die_ref, tree);
6247 static void add_prototyped_attribute (dw_die_ref, tree);
6248 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6249 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6250 static void add_src_coords_attributes (dw_die_ref, tree);
6251 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6252 static void push_decl_scope (tree);
6253 static void pop_decl_scope (void);
6254 static dw_die_ref scope_die_for (tree, dw_die_ref);
6255 static inline int local_scope_p (dw_die_ref);
6256 static inline int class_scope_p (dw_die_ref);
6257 static inline int class_or_namespace_scope_p (dw_die_ref);
6258 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6259 static void add_calling_convention_attribute (dw_die_ref, tree);
6260 static const char *type_tag (const_tree);
6261 static tree member_declared_type (const_tree);
6262 #if 0
6263 static const char *decl_start_label (tree);
6264 #endif
6265 static void gen_array_type_die (tree, dw_die_ref);
6266 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6267 #if 0
6268 static void gen_entry_point_die (tree, dw_die_ref);
6269 #endif
6270 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6271 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6272 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6273 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6274 static void gen_formal_types_die (tree, dw_die_ref);
6275 static void gen_subprogram_die (tree, dw_die_ref);
6276 static void gen_variable_die (tree, tree, dw_die_ref);
6277 static void gen_const_die (tree, dw_die_ref);
6278 static void gen_label_die (tree, dw_die_ref);
6279 static void gen_lexical_block_die (tree, dw_die_ref, int);
6280 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6281 static void gen_field_die (tree, dw_die_ref);
6282 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6283 static dw_die_ref gen_compile_unit_die (const char *);
6284 static void gen_inheritance_die (tree, tree, dw_die_ref);
6285 static void gen_member_die (tree, dw_die_ref);
6286 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6287                                                 enum debug_info_usage);
6288 static void gen_subroutine_type_die (tree, dw_die_ref);
6289 static void gen_typedef_die (tree, dw_die_ref);
6290 static void gen_type_die (tree, dw_die_ref);
6291 static void gen_block_die (tree, dw_die_ref, int);
6292 static void decls_for_scope (tree, dw_die_ref, int);
6293 static int is_redundant_typedef (const_tree);
6294 static bool is_naming_typedef_decl (const_tree);
6295 static inline dw_die_ref get_context_die (tree);
6296 static void gen_namespace_die (tree, dw_die_ref);
6297 static void gen_decl_die (tree, tree, dw_die_ref);
6298 static dw_die_ref force_decl_die (tree);
6299 static dw_die_ref force_type_die (tree);
6300 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6301 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6302 static struct dwarf_file_data * lookup_filename (const char *);
6303 static void retry_incomplete_types (void);
6304 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6305 static void gen_generic_params_dies (tree);
6306 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6307 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6308 static void splice_child_die (dw_die_ref, dw_die_ref);
6309 static int file_info_cmp (const void *, const void *);
6310 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6311                                      const char *, const char *);
6312 static void output_loc_list (dw_loc_list_ref);
6313 static char *gen_internal_sym (const char *);
6314
6315 static void prune_unmark_dies (dw_die_ref);
6316 static void prune_unused_types_mark (dw_die_ref, int);
6317 static void prune_unused_types_walk (dw_die_ref);
6318 static void prune_unused_types_walk_attribs (dw_die_ref);
6319 static void prune_unused_types_prune (dw_die_ref);
6320 static void prune_unused_types (void);
6321 static int maybe_emit_file (struct dwarf_file_data *fd);
6322 static inline const char *AT_vms_delta1 (dw_attr_ref);
6323 static inline const char *AT_vms_delta2 (dw_attr_ref);
6324 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6325                                      const char *, const char *);
6326 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6327 static void gen_remaining_tmpl_value_param_die_attribute (void);
6328
6329 /* Section names used to hold DWARF debugging information.  */
6330 #ifndef DEBUG_INFO_SECTION
6331 #define DEBUG_INFO_SECTION      ".debug_info"
6332 #endif
6333 #ifndef DEBUG_ABBREV_SECTION
6334 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6335 #endif
6336 #ifndef DEBUG_ARANGES_SECTION
6337 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6338 #endif
6339 #ifndef DEBUG_MACINFO_SECTION
6340 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6341 #endif
6342 #ifndef DEBUG_LINE_SECTION
6343 #define DEBUG_LINE_SECTION      ".debug_line"
6344 #endif
6345 #ifndef DEBUG_LOC_SECTION
6346 #define DEBUG_LOC_SECTION       ".debug_loc"
6347 #endif
6348 #ifndef DEBUG_PUBNAMES_SECTION
6349 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6350 #endif
6351 #ifndef DEBUG_PUBTYPES_SECTION
6352 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6353 #endif
6354 #ifndef DEBUG_DCALL_SECTION
6355 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6356 #endif
6357 #ifndef DEBUG_VCALL_SECTION
6358 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6359 #endif
6360 #ifndef DEBUG_STR_SECTION
6361 #define DEBUG_STR_SECTION       ".debug_str"
6362 #endif
6363 #ifndef DEBUG_RANGES_SECTION
6364 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6365 #endif
6366
6367 /* Standard ELF section names for compiled code and data.  */
6368 #ifndef TEXT_SECTION_NAME
6369 #define TEXT_SECTION_NAME       ".text"
6370 #endif
6371
6372 /* Section flags for .debug_str section.  */
6373 #define DEBUG_STR_SECTION_FLAGS \
6374   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6375    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6376    : SECTION_DEBUG)
6377
6378 /* Labels we insert at beginning sections we can reference instead of
6379    the section names themselves.  */
6380
6381 #ifndef TEXT_SECTION_LABEL
6382 #define TEXT_SECTION_LABEL              "Ltext"
6383 #endif
6384 #ifndef COLD_TEXT_SECTION_LABEL
6385 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6386 #endif
6387 #ifndef DEBUG_LINE_SECTION_LABEL
6388 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6389 #endif
6390 #ifndef DEBUG_INFO_SECTION_LABEL
6391 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6392 #endif
6393 #ifndef DEBUG_ABBREV_SECTION_LABEL
6394 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6395 #endif
6396 #ifndef DEBUG_LOC_SECTION_LABEL
6397 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6398 #endif
6399 #ifndef DEBUG_RANGES_SECTION_LABEL
6400 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6401 #endif
6402 #ifndef DEBUG_MACINFO_SECTION_LABEL
6403 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6404 #endif
6405
6406
6407 /* Definitions of defaults for formats and names of various special
6408    (artificial) labels which may be generated within this file (when the -g
6409    options is used and DWARF2_DEBUGGING_INFO is in effect.
6410    If necessary, these may be overridden from within the tm.h file, but
6411    typically, overriding these defaults is unnecessary.  */
6412
6413 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6414 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6415 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6416 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6417 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6418 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6419 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6420 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6421 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6422 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6423
6424 #ifndef TEXT_END_LABEL
6425 #define TEXT_END_LABEL          "Letext"
6426 #endif
6427 #ifndef COLD_END_LABEL
6428 #define COLD_END_LABEL          "Letext_cold"
6429 #endif
6430 #ifndef BLOCK_BEGIN_LABEL
6431 #define BLOCK_BEGIN_LABEL       "LBB"
6432 #endif
6433 #ifndef BLOCK_END_LABEL
6434 #define BLOCK_END_LABEL         "LBE"
6435 #endif
6436 #ifndef LINE_CODE_LABEL
6437 #define LINE_CODE_LABEL         "LM"
6438 #endif
6439 #ifndef SEPARATE_LINE_CODE_LABEL
6440 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6441 #endif
6442
6443 \f
6444 /* We allow a language front-end to designate a function that is to be
6445    called to "demangle" any name before it is put into a DIE.  */
6446
6447 static const char *(*demangle_name_func) (const char *);
6448
6449 void
6450 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6451 {
6452   demangle_name_func = func;
6453 }
6454
6455 /* Test if rtl node points to a pseudo register.  */
6456
6457 static inline int
6458 is_pseudo_reg (const_rtx rtl)
6459 {
6460   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6461           || (GET_CODE (rtl) == SUBREG
6462               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6463 }
6464
6465 /* Return a reference to a type, with its const and volatile qualifiers
6466    removed.  */
6467
6468 static inline tree
6469 type_main_variant (tree type)
6470 {
6471   type = TYPE_MAIN_VARIANT (type);
6472
6473   /* ??? There really should be only one main variant among any group of
6474      variants of a given type (and all of the MAIN_VARIANT values for all
6475      members of the group should point to that one type) but sometimes the C
6476      front-end messes this up for array types, so we work around that bug
6477      here.  */
6478   if (TREE_CODE (type) == ARRAY_TYPE)
6479     while (type != TYPE_MAIN_VARIANT (type))
6480       type = TYPE_MAIN_VARIANT (type);
6481
6482   return type;
6483 }
6484
6485 /* Return nonzero if the given type node represents a tagged type.  */
6486
6487 static inline int
6488 is_tagged_type (const_tree type)
6489 {
6490   enum tree_code code = TREE_CODE (type);
6491
6492   return (code == RECORD_TYPE || code == UNION_TYPE
6493           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6494 }
6495
6496 /* Convert a DIE tag into its string name.  */
6497
6498 static const char *
6499 dwarf_tag_name (unsigned int tag)
6500 {
6501   switch (tag)
6502     {
6503     case DW_TAG_padding:
6504       return "DW_TAG_padding";
6505     case DW_TAG_array_type:
6506       return "DW_TAG_array_type";
6507     case DW_TAG_class_type:
6508       return "DW_TAG_class_type";
6509     case DW_TAG_entry_point:
6510       return "DW_TAG_entry_point";
6511     case DW_TAG_enumeration_type:
6512       return "DW_TAG_enumeration_type";
6513     case DW_TAG_formal_parameter:
6514       return "DW_TAG_formal_parameter";
6515     case DW_TAG_imported_declaration:
6516       return "DW_TAG_imported_declaration";
6517     case DW_TAG_label:
6518       return "DW_TAG_label";
6519     case DW_TAG_lexical_block:
6520       return "DW_TAG_lexical_block";
6521     case DW_TAG_member:
6522       return "DW_TAG_member";
6523     case DW_TAG_pointer_type:
6524       return "DW_TAG_pointer_type";
6525     case DW_TAG_reference_type:
6526       return "DW_TAG_reference_type";
6527     case DW_TAG_compile_unit:
6528       return "DW_TAG_compile_unit";
6529     case DW_TAG_string_type:
6530       return "DW_TAG_string_type";
6531     case DW_TAG_structure_type:
6532       return "DW_TAG_structure_type";
6533     case DW_TAG_subroutine_type:
6534       return "DW_TAG_subroutine_type";
6535     case DW_TAG_typedef:
6536       return "DW_TAG_typedef";
6537     case DW_TAG_union_type:
6538       return "DW_TAG_union_type";
6539     case DW_TAG_unspecified_parameters:
6540       return "DW_TAG_unspecified_parameters";
6541     case DW_TAG_variant:
6542       return "DW_TAG_variant";
6543     case DW_TAG_common_block:
6544       return "DW_TAG_common_block";
6545     case DW_TAG_common_inclusion:
6546       return "DW_TAG_common_inclusion";
6547     case DW_TAG_inheritance:
6548       return "DW_TAG_inheritance";
6549     case DW_TAG_inlined_subroutine:
6550       return "DW_TAG_inlined_subroutine";
6551     case DW_TAG_module:
6552       return "DW_TAG_module";
6553     case DW_TAG_ptr_to_member_type:
6554       return "DW_TAG_ptr_to_member_type";
6555     case DW_TAG_set_type:
6556       return "DW_TAG_set_type";
6557     case DW_TAG_subrange_type:
6558       return "DW_TAG_subrange_type";
6559     case DW_TAG_with_stmt:
6560       return "DW_TAG_with_stmt";
6561     case DW_TAG_access_declaration:
6562       return "DW_TAG_access_declaration";
6563     case DW_TAG_base_type:
6564       return "DW_TAG_base_type";
6565     case DW_TAG_catch_block:
6566       return "DW_TAG_catch_block";
6567     case DW_TAG_const_type:
6568       return "DW_TAG_const_type";
6569     case DW_TAG_constant:
6570       return "DW_TAG_constant";
6571     case DW_TAG_enumerator:
6572       return "DW_TAG_enumerator";
6573     case DW_TAG_file_type:
6574       return "DW_TAG_file_type";
6575     case DW_TAG_friend:
6576       return "DW_TAG_friend";
6577     case DW_TAG_namelist:
6578       return "DW_TAG_namelist";
6579     case DW_TAG_namelist_item:
6580       return "DW_TAG_namelist_item";
6581     case DW_TAG_packed_type:
6582       return "DW_TAG_packed_type";
6583     case DW_TAG_subprogram:
6584       return "DW_TAG_subprogram";
6585     case DW_TAG_template_type_param:
6586       return "DW_TAG_template_type_param";
6587     case DW_TAG_template_value_param:
6588       return "DW_TAG_template_value_param";
6589     case DW_TAG_thrown_type:
6590       return "DW_TAG_thrown_type";
6591     case DW_TAG_try_block:
6592       return "DW_TAG_try_block";
6593     case DW_TAG_variant_part:
6594       return "DW_TAG_variant_part";
6595     case DW_TAG_variable:
6596       return "DW_TAG_variable";
6597     case DW_TAG_volatile_type:
6598       return "DW_TAG_volatile_type";
6599     case DW_TAG_dwarf_procedure:
6600       return "DW_TAG_dwarf_procedure";
6601     case DW_TAG_restrict_type:
6602       return "DW_TAG_restrict_type";
6603     case DW_TAG_interface_type:
6604       return "DW_TAG_interface_type";
6605     case DW_TAG_namespace:
6606       return "DW_TAG_namespace";
6607     case DW_TAG_imported_module:
6608       return "DW_TAG_imported_module";
6609     case DW_TAG_unspecified_type:
6610       return "DW_TAG_unspecified_type";
6611     case DW_TAG_partial_unit:
6612       return "DW_TAG_partial_unit";
6613     case DW_TAG_imported_unit:
6614       return "DW_TAG_imported_unit";
6615     case DW_TAG_condition:
6616       return "DW_TAG_condition";
6617     case DW_TAG_shared_type:
6618       return "DW_TAG_shared_type";
6619     case DW_TAG_type_unit:
6620       return "DW_TAG_type_unit";
6621     case DW_TAG_rvalue_reference_type:
6622       return "DW_TAG_rvalue_reference_type";
6623     case DW_TAG_template_alias:
6624       return "DW_TAG_template_alias";
6625     case DW_TAG_GNU_template_parameter_pack:
6626       return "DW_TAG_GNU_template_parameter_pack";
6627     case DW_TAG_GNU_formal_parameter_pack:
6628       return "DW_TAG_GNU_formal_parameter_pack";
6629     case DW_TAG_MIPS_loop:
6630       return "DW_TAG_MIPS_loop";
6631     case DW_TAG_format_label:
6632       return "DW_TAG_format_label";
6633     case DW_TAG_function_template:
6634       return "DW_TAG_function_template";
6635     case DW_TAG_class_template:
6636       return "DW_TAG_class_template";
6637     case DW_TAG_GNU_BINCL:
6638       return "DW_TAG_GNU_BINCL";
6639     case DW_TAG_GNU_EINCL:
6640       return "DW_TAG_GNU_EINCL";
6641     case DW_TAG_GNU_template_template_param:
6642       return "DW_TAG_GNU_template_template_param";
6643     default:
6644       return "DW_TAG_<unknown>";
6645     }
6646 }
6647
6648 /* Convert a DWARF attribute code into its string name.  */
6649
6650 static const char *
6651 dwarf_attr_name (unsigned int attr)
6652 {
6653   switch (attr)
6654     {
6655     case DW_AT_sibling:
6656       return "DW_AT_sibling";
6657     case DW_AT_location:
6658       return "DW_AT_location";
6659     case DW_AT_name:
6660       return "DW_AT_name";
6661     case DW_AT_ordering:
6662       return "DW_AT_ordering";
6663     case DW_AT_subscr_data:
6664       return "DW_AT_subscr_data";
6665     case DW_AT_byte_size:
6666       return "DW_AT_byte_size";
6667     case DW_AT_bit_offset:
6668       return "DW_AT_bit_offset";
6669     case DW_AT_bit_size:
6670       return "DW_AT_bit_size";
6671     case DW_AT_element_list:
6672       return "DW_AT_element_list";
6673     case DW_AT_stmt_list:
6674       return "DW_AT_stmt_list";
6675     case DW_AT_low_pc:
6676       return "DW_AT_low_pc";
6677     case DW_AT_high_pc:
6678       return "DW_AT_high_pc";
6679     case DW_AT_language:
6680       return "DW_AT_language";
6681     case DW_AT_member:
6682       return "DW_AT_member";
6683     case DW_AT_discr:
6684       return "DW_AT_discr";
6685     case DW_AT_discr_value:
6686       return "DW_AT_discr_value";
6687     case DW_AT_visibility:
6688       return "DW_AT_visibility";
6689     case DW_AT_import:
6690       return "DW_AT_import";
6691     case DW_AT_string_length:
6692       return "DW_AT_string_length";
6693     case DW_AT_common_reference:
6694       return "DW_AT_common_reference";
6695     case DW_AT_comp_dir:
6696       return "DW_AT_comp_dir";
6697     case DW_AT_const_value:
6698       return "DW_AT_const_value";
6699     case DW_AT_containing_type:
6700       return "DW_AT_containing_type";
6701     case DW_AT_default_value:
6702       return "DW_AT_default_value";
6703     case DW_AT_inline:
6704       return "DW_AT_inline";
6705     case DW_AT_is_optional:
6706       return "DW_AT_is_optional";
6707     case DW_AT_lower_bound:
6708       return "DW_AT_lower_bound";
6709     case DW_AT_producer:
6710       return "DW_AT_producer";
6711     case DW_AT_prototyped:
6712       return "DW_AT_prototyped";
6713     case DW_AT_return_addr:
6714       return "DW_AT_return_addr";
6715     case DW_AT_start_scope:
6716       return "DW_AT_start_scope";
6717     case DW_AT_bit_stride:
6718       return "DW_AT_bit_stride";
6719     case DW_AT_upper_bound:
6720       return "DW_AT_upper_bound";
6721     case DW_AT_abstract_origin:
6722       return "DW_AT_abstract_origin";
6723     case DW_AT_accessibility:
6724       return "DW_AT_accessibility";
6725     case DW_AT_address_class:
6726       return "DW_AT_address_class";
6727     case DW_AT_artificial:
6728       return "DW_AT_artificial";
6729     case DW_AT_base_types:
6730       return "DW_AT_base_types";
6731     case DW_AT_calling_convention:
6732       return "DW_AT_calling_convention";
6733     case DW_AT_count:
6734       return "DW_AT_count";
6735     case DW_AT_data_member_location:
6736       return "DW_AT_data_member_location";
6737     case DW_AT_decl_column:
6738       return "DW_AT_decl_column";
6739     case DW_AT_decl_file:
6740       return "DW_AT_decl_file";
6741     case DW_AT_decl_line:
6742       return "DW_AT_decl_line";
6743     case DW_AT_declaration:
6744       return "DW_AT_declaration";
6745     case DW_AT_discr_list:
6746       return "DW_AT_discr_list";
6747     case DW_AT_encoding:
6748       return "DW_AT_encoding";
6749     case DW_AT_external:
6750       return "DW_AT_external";
6751     case DW_AT_explicit:
6752       return "DW_AT_explicit";
6753     case DW_AT_frame_base:
6754       return "DW_AT_frame_base";
6755     case DW_AT_friend:
6756       return "DW_AT_friend";
6757     case DW_AT_identifier_case:
6758       return "DW_AT_identifier_case";
6759     case DW_AT_macro_info:
6760       return "DW_AT_macro_info";
6761     case DW_AT_namelist_items:
6762       return "DW_AT_namelist_items";
6763     case DW_AT_priority:
6764       return "DW_AT_priority";
6765     case DW_AT_segment:
6766       return "DW_AT_segment";
6767     case DW_AT_specification:
6768       return "DW_AT_specification";
6769     case DW_AT_static_link:
6770       return "DW_AT_static_link";
6771     case DW_AT_type:
6772       return "DW_AT_type";
6773     case DW_AT_use_location:
6774       return "DW_AT_use_location";
6775     case DW_AT_variable_parameter:
6776       return "DW_AT_variable_parameter";
6777     case DW_AT_virtuality:
6778       return "DW_AT_virtuality";
6779     case DW_AT_vtable_elem_location:
6780       return "DW_AT_vtable_elem_location";
6781
6782     case DW_AT_allocated:
6783       return "DW_AT_allocated";
6784     case DW_AT_associated:
6785       return "DW_AT_associated";
6786     case DW_AT_data_location:
6787       return "DW_AT_data_location";
6788     case DW_AT_byte_stride:
6789       return "DW_AT_byte_stride";
6790     case DW_AT_entry_pc:
6791       return "DW_AT_entry_pc";
6792     case DW_AT_use_UTF8:
6793       return "DW_AT_use_UTF8";
6794     case DW_AT_extension:
6795       return "DW_AT_extension";
6796     case DW_AT_ranges:
6797       return "DW_AT_ranges";
6798     case DW_AT_trampoline:
6799       return "DW_AT_trampoline";
6800     case DW_AT_call_column:
6801       return "DW_AT_call_column";
6802     case DW_AT_call_file:
6803       return "DW_AT_call_file";
6804     case DW_AT_call_line:
6805       return "DW_AT_call_line";
6806
6807     case DW_AT_signature:
6808       return "DW_AT_signature";
6809     case DW_AT_main_subprogram:
6810       return "DW_AT_main_subprogram";
6811     case DW_AT_data_bit_offset:
6812       return "DW_AT_data_bit_offset";
6813     case DW_AT_const_expr:
6814       return "DW_AT_const_expr";
6815     case DW_AT_enum_class:
6816       return "DW_AT_enum_class";
6817     case DW_AT_linkage_name:
6818       return "DW_AT_linkage_name";
6819
6820     case DW_AT_MIPS_fde:
6821       return "DW_AT_MIPS_fde";
6822     case DW_AT_MIPS_loop_begin:
6823       return "DW_AT_MIPS_loop_begin";
6824     case DW_AT_MIPS_tail_loop_begin:
6825       return "DW_AT_MIPS_tail_loop_begin";
6826     case DW_AT_MIPS_epilog_begin:
6827       return "DW_AT_MIPS_epilog_begin";
6828 #if VMS_DEBUGGING_INFO
6829     case DW_AT_HP_prologue:
6830       return "DW_AT_HP_prologue";
6831 #else
6832     case DW_AT_MIPS_loop_unroll_factor:
6833       return "DW_AT_MIPS_loop_unroll_factor";
6834 #endif
6835     case DW_AT_MIPS_software_pipeline_depth:
6836       return "DW_AT_MIPS_software_pipeline_depth";
6837     case DW_AT_MIPS_linkage_name:
6838       return "DW_AT_MIPS_linkage_name";
6839 #if VMS_DEBUGGING_INFO
6840     case DW_AT_HP_epilogue:
6841       return "DW_AT_HP_epilogue";
6842 #else
6843     case DW_AT_MIPS_stride:
6844       return "DW_AT_MIPS_stride";
6845 #endif
6846     case DW_AT_MIPS_abstract_name:
6847       return "DW_AT_MIPS_abstract_name";
6848     case DW_AT_MIPS_clone_origin:
6849       return "DW_AT_MIPS_clone_origin";
6850     case DW_AT_MIPS_has_inlines:
6851       return "DW_AT_MIPS_has_inlines";
6852
6853     case DW_AT_sf_names:
6854       return "DW_AT_sf_names";
6855     case DW_AT_src_info:
6856       return "DW_AT_src_info";
6857     case DW_AT_mac_info:
6858       return "DW_AT_mac_info";
6859     case DW_AT_src_coords:
6860       return "DW_AT_src_coords";
6861     case DW_AT_body_begin:
6862       return "DW_AT_body_begin";
6863     case DW_AT_body_end:
6864       return "DW_AT_body_end";
6865     case DW_AT_GNU_vector:
6866       return "DW_AT_GNU_vector";
6867     case DW_AT_GNU_guarded_by:
6868       return "DW_AT_GNU_guarded_by";
6869     case DW_AT_GNU_pt_guarded_by:
6870       return "DW_AT_GNU_pt_guarded_by";
6871     case DW_AT_GNU_guarded:
6872       return "DW_AT_GNU_guarded";
6873     case DW_AT_GNU_pt_guarded:
6874       return "DW_AT_GNU_pt_guarded";
6875     case DW_AT_GNU_locks_excluded:
6876       return "DW_AT_GNU_locks_excluded";
6877     case DW_AT_GNU_exclusive_locks_required:
6878       return "DW_AT_GNU_exclusive_locks_required";
6879     case DW_AT_GNU_shared_locks_required:
6880       return "DW_AT_GNU_shared_locks_required";
6881     case DW_AT_GNU_odr_signature:
6882       return "DW_AT_GNU_odr_signature";
6883     case DW_AT_GNU_template_name:
6884       return "DW_AT_GNU_template_name";
6885
6886     case DW_AT_VMS_rtnbeg_pd_address:
6887       return "DW_AT_VMS_rtnbeg_pd_address";
6888
6889     default:
6890       return "DW_AT_<unknown>";
6891     }
6892 }
6893
6894 /* Convert a DWARF value form code into its string name.  */
6895
6896 static const char *
6897 dwarf_form_name (unsigned int form)
6898 {
6899   switch (form)
6900     {
6901     case DW_FORM_addr:
6902       return "DW_FORM_addr";
6903     case DW_FORM_block2:
6904       return "DW_FORM_block2";
6905     case DW_FORM_block4:
6906       return "DW_FORM_block4";
6907     case DW_FORM_data2:
6908       return "DW_FORM_data2";
6909     case DW_FORM_data4:
6910       return "DW_FORM_data4";
6911     case DW_FORM_data8:
6912       return "DW_FORM_data8";
6913     case DW_FORM_string:
6914       return "DW_FORM_string";
6915     case DW_FORM_block:
6916       return "DW_FORM_block";
6917     case DW_FORM_block1:
6918       return "DW_FORM_block1";
6919     case DW_FORM_data1:
6920       return "DW_FORM_data1";
6921     case DW_FORM_flag:
6922       return "DW_FORM_flag";
6923     case DW_FORM_sdata:
6924       return "DW_FORM_sdata";
6925     case DW_FORM_strp:
6926       return "DW_FORM_strp";
6927     case DW_FORM_udata:
6928       return "DW_FORM_udata";
6929     case DW_FORM_ref_addr:
6930       return "DW_FORM_ref_addr";
6931     case DW_FORM_ref1:
6932       return "DW_FORM_ref1";
6933     case DW_FORM_ref2:
6934       return "DW_FORM_ref2";
6935     case DW_FORM_ref4:
6936       return "DW_FORM_ref4";
6937     case DW_FORM_ref8:
6938       return "DW_FORM_ref8";
6939     case DW_FORM_ref_udata:
6940       return "DW_FORM_ref_udata";
6941     case DW_FORM_indirect:
6942       return "DW_FORM_indirect";
6943     case DW_FORM_sec_offset:
6944       return "DW_FORM_sec_offset";
6945     case DW_FORM_exprloc:
6946       return "DW_FORM_exprloc";
6947     case DW_FORM_flag_present:
6948       return "DW_FORM_flag_present";
6949     case DW_FORM_ref_sig8:
6950       return "DW_FORM_ref_sig8";
6951     default:
6952       return "DW_FORM_<unknown>";
6953     }
6954 }
6955 \f
6956 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6957    instance of an inlined instance of a decl which is local to an inline
6958    function, so we have to trace all of the way back through the origin chain
6959    to find out what sort of node actually served as the original seed for the
6960    given block.  */
6961
6962 static tree
6963 decl_ultimate_origin (const_tree decl)
6964 {
6965   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6966     return NULL_TREE;
6967
6968   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6969      nodes in the function to point to themselves; ignore that if
6970      we're trying to output the abstract instance of this function.  */
6971   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6972     return NULL_TREE;
6973
6974   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6975      most distant ancestor, this should never happen.  */
6976   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6977
6978   return DECL_ABSTRACT_ORIGIN (decl);
6979 }
6980
6981 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6982    of a virtual function may refer to a base class, so we check the 'this'
6983    parameter.  */
6984
6985 static tree
6986 decl_class_context (tree decl)
6987 {
6988   tree context = NULL_TREE;
6989
6990   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6991     context = DECL_CONTEXT (decl);
6992   else
6993     context = TYPE_MAIN_VARIANT
6994       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6995
6996   if (context && !TYPE_P (context))
6997     context = NULL_TREE;
6998
6999   return context;
7000 }
7001 \f
7002 /* Add an attribute/value pair to a DIE.  */
7003
7004 static inline void
7005 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7006 {
7007   /* Maybe this should be an assert?  */
7008   if (die == NULL)
7009     return;
7010
7011   if (die->die_attr == NULL)
7012     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7013   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7014 }
7015
7016 static inline enum dw_val_class
7017 AT_class (dw_attr_ref a)
7018 {
7019   return a->dw_attr_val.val_class;
7020 }
7021
7022 /* Add a flag value attribute to a DIE.  */
7023
7024 static inline void
7025 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7026 {
7027   dw_attr_node attr;
7028
7029   attr.dw_attr = attr_kind;
7030   attr.dw_attr_val.val_class = dw_val_class_flag;
7031   attr.dw_attr_val.v.val_flag = flag;
7032   add_dwarf_attr (die, &attr);
7033 }
7034
7035 static inline unsigned
7036 AT_flag (dw_attr_ref a)
7037 {
7038   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7039   return a->dw_attr_val.v.val_flag;
7040 }
7041
7042 /* Add a signed integer attribute value to a DIE.  */
7043
7044 static inline void
7045 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7046 {
7047   dw_attr_node attr;
7048
7049   attr.dw_attr = attr_kind;
7050   attr.dw_attr_val.val_class = dw_val_class_const;
7051   attr.dw_attr_val.v.val_int = int_val;
7052   add_dwarf_attr (die, &attr);
7053 }
7054
7055 static inline HOST_WIDE_INT
7056 AT_int (dw_attr_ref a)
7057 {
7058   gcc_assert (a && AT_class (a) == dw_val_class_const);
7059   return a->dw_attr_val.v.val_int;
7060 }
7061
7062 /* Add an unsigned integer attribute value to a DIE.  */
7063
7064 static inline void
7065 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7066                  unsigned HOST_WIDE_INT unsigned_val)
7067 {
7068   dw_attr_node attr;
7069
7070   attr.dw_attr = attr_kind;
7071   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7072   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7073   add_dwarf_attr (die, &attr);
7074 }
7075
7076 static inline unsigned HOST_WIDE_INT
7077 AT_unsigned (dw_attr_ref a)
7078 {
7079   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7080   return a->dw_attr_val.v.val_unsigned;
7081 }
7082
7083 /* Add an unsigned double integer attribute value to a DIE.  */
7084
7085 static inline void
7086 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7087                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7088 {
7089   dw_attr_node attr;
7090
7091   attr.dw_attr = attr_kind;
7092   attr.dw_attr_val.val_class = dw_val_class_const_double;
7093   attr.dw_attr_val.v.val_double.high = high;
7094   attr.dw_attr_val.v.val_double.low = low;
7095   add_dwarf_attr (die, &attr);
7096 }
7097
7098 /* Add a floating point attribute value to a DIE and return it.  */
7099
7100 static inline void
7101 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7102             unsigned int length, unsigned int elt_size, unsigned char *array)
7103 {
7104   dw_attr_node attr;
7105
7106   attr.dw_attr = attr_kind;
7107   attr.dw_attr_val.val_class = dw_val_class_vec;
7108   attr.dw_attr_val.v.val_vec.length = length;
7109   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7110   attr.dw_attr_val.v.val_vec.array = array;
7111   add_dwarf_attr (die, &attr);
7112 }
7113
7114 /* Add an 8-byte data attribute value to a DIE.  */
7115
7116 static inline void
7117 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7118               unsigned char data8[8])
7119 {
7120   dw_attr_node attr;
7121
7122   attr.dw_attr = attr_kind;
7123   attr.dw_attr_val.val_class = dw_val_class_data8;
7124   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7125   add_dwarf_attr (die, &attr);
7126 }
7127
7128 /* Hash and equality functions for debug_str_hash.  */
7129
7130 static hashval_t
7131 debug_str_do_hash (const void *x)
7132 {
7133   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7134 }
7135
7136 static int
7137 debug_str_eq (const void *x1, const void *x2)
7138 {
7139   return strcmp ((((const struct indirect_string_node *)x1)->str),
7140                  (const char *)x2) == 0;
7141 }
7142
7143 /* Add STR to the indirect string hash table.  */
7144
7145 static struct indirect_string_node *
7146 find_AT_string (const char *str)
7147 {
7148   struct indirect_string_node *node;
7149   void **slot;
7150
7151   if (! debug_str_hash)
7152     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7153                                       debug_str_eq, NULL);
7154
7155   slot = htab_find_slot_with_hash (debug_str_hash, str,
7156                                    htab_hash_string (str), INSERT);
7157   if (*slot == NULL)
7158     {
7159       node = ggc_alloc_cleared_indirect_string_node ();
7160       node->str = ggc_strdup (str);
7161       *slot = node;
7162     }
7163   else
7164     node = (struct indirect_string_node *) *slot;
7165
7166   node->refcount++;
7167   return node;
7168 }
7169
7170 /* Add a string attribute value to a DIE.  */
7171
7172 static inline void
7173 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7174 {
7175   dw_attr_node attr;
7176   struct indirect_string_node *node;
7177
7178   node = find_AT_string (str);
7179
7180   attr.dw_attr = attr_kind;
7181   attr.dw_attr_val.val_class = dw_val_class_str;
7182   attr.dw_attr_val.v.val_str = node;
7183   add_dwarf_attr (die, &attr);
7184 }
7185
7186 /* Create a label for an indirect string node, ensuring it is going to
7187    be output, unless its reference count goes down to zero.  */
7188
7189 static inline void
7190 gen_label_for_indirect_string (struct indirect_string_node *node)
7191 {
7192   char label[32];
7193
7194   if (node->label)
7195     return;
7196
7197   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7198   ++dw2_string_counter;
7199   node->label = xstrdup (label);
7200 }
7201
7202 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7203    debug string STR.  */
7204
7205 static inline rtx
7206 get_debug_string_label (const char *str)
7207 {
7208   struct indirect_string_node *node = find_AT_string (str);
7209
7210   debug_str_hash_forced = true;
7211
7212   gen_label_for_indirect_string (node);
7213
7214   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7215 }
7216
7217 static inline const char *
7218 AT_string (dw_attr_ref a)
7219 {
7220   gcc_assert (a && AT_class (a) == dw_val_class_str);
7221   return a->dw_attr_val.v.val_str->str;
7222 }
7223
7224 /* Find out whether a string should be output inline in DIE
7225    or out-of-line in .debug_str section.  */
7226
7227 static enum dwarf_form
7228 AT_string_form (dw_attr_ref a)
7229 {
7230   struct indirect_string_node *node;
7231   unsigned int len;
7232
7233   gcc_assert (a && AT_class (a) == dw_val_class_str);
7234
7235   node = a->dw_attr_val.v.val_str;
7236   if (node->form)
7237     return node->form;
7238
7239   len = strlen (node->str) + 1;
7240
7241   /* If the string is shorter or equal to the size of the reference, it is
7242      always better to put it inline.  */
7243   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7244     return node->form = DW_FORM_string;
7245
7246   /* If we cannot expect the linker to merge strings in .debug_str
7247      section, only put it into .debug_str if it is worth even in this
7248      single module.  */
7249   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7250       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7251       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7252     return node->form = DW_FORM_string;
7253
7254   gen_label_for_indirect_string (node);
7255
7256   return node->form = DW_FORM_strp;
7257 }
7258
7259 /* Add a DIE reference attribute value to a DIE.  */
7260
7261 static inline void
7262 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7263 {
7264   dw_attr_node attr;
7265
7266   attr.dw_attr = attr_kind;
7267   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7268   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7269   attr.dw_attr_val.v.val_die_ref.external = 0;
7270   add_dwarf_attr (die, &attr);
7271 }
7272
7273 /* Add an AT_specification attribute to a DIE, and also make the back
7274    pointer from the specification to the definition.  */
7275
7276 static inline void
7277 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7278 {
7279   add_AT_die_ref (die, DW_AT_specification, targ_die);
7280   gcc_assert (!targ_die->die_definition);
7281   targ_die->die_definition = die;
7282 }
7283
7284 static inline dw_die_ref
7285 AT_ref (dw_attr_ref a)
7286 {
7287   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7288   return a->dw_attr_val.v.val_die_ref.die;
7289 }
7290
7291 static inline int
7292 AT_ref_external (dw_attr_ref a)
7293 {
7294   if (a && AT_class (a) == dw_val_class_die_ref)
7295     return a->dw_attr_val.v.val_die_ref.external;
7296
7297   return 0;
7298 }
7299
7300 static inline void
7301 set_AT_ref_external (dw_attr_ref a, int i)
7302 {
7303   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7304   a->dw_attr_val.v.val_die_ref.external = i;
7305 }
7306
7307 /* Add an FDE reference attribute value to a DIE.  */
7308
7309 static inline void
7310 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7311 {
7312   dw_attr_node attr;
7313
7314   attr.dw_attr = attr_kind;
7315   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7316   attr.dw_attr_val.v.val_fde_index = targ_fde;
7317   add_dwarf_attr (die, &attr);
7318 }
7319
7320 /* Add a location description attribute value to a DIE.  */
7321
7322 static inline void
7323 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7324 {
7325   dw_attr_node attr;
7326
7327   attr.dw_attr = attr_kind;
7328   attr.dw_attr_val.val_class = dw_val_class_loc;
7329   attr.dw_attr_val.v.val_loc = loc;
7330   add_dwarf_attr (die, &attr);
7331 }
7332
7333 static inline dw_loc_descr_ref
7334 AT_loc (dw_attr_ref a)
7335 {
7336   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7337   return a->dw_attr_val.v.val_loc;
7338 }
7339
7340 static inline void
7341 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7342 {
7343   dw_attr_node attr;
7344
7345   attr.dw_attr = attr_kind;
7346   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7347   attr.dw_attr_val.v.val_loc_list = loc_list;
7348   add_dwarf_attr (die, &attr);
7349   have_location_lists = true;
7350 }
7351
7352 static inline dw_loc_list_ref
7353 AT_loc_list (dw_attr_ref a)
7354 {
7355   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7356   return a->dw_attr_val.v.val_loc_list;
7357 }
7358
7359 static inline dw_loc_list_ref *
7360 AT_loc_list_ptr (dw_attr_ref a)
7361 {
7362   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7363   return &a->dw_attr_val.v.val_loc_list;
7364 }
7365
7366 /* Add an address constant attribute value to a DIE.  */
7367
7368 static inline void
7369 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7370 {
7371   dw_attr_node attr;
7372
7373   attr.dw_attr = attr_kind;
7374   attr.dw_attr_val.val_class = dw_val_class_addr;
7375   attr.dw_attr_val.v.val_addr = addr;
7376   add_dwarf_attr (die, &attr);
7377 }
7378
7379 /* Get the RTX from to an address DIE attribute.  */
7380
7381 static inline rtx
7382 AT_addr (dw_attr_ref a)
7383 {
7384   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7385   return a->dw_attr_val.v.val_addr;
7386 }
7387
7388 /* Add a file attribute value to a DIE.  */
7389
7390 static inline void
7391 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7392              struct dwarf_file_data *fd)
7393 {
7394   dw_attr_node attr;
7395
7396   attr.dw_attr = attr_kind;
7397   attr.dw_attr_val.val_class = dw_val_class_file;
7398   attr.dw_attr_val.v.val_file = fd;
7399   add_dwarf_attr (die, &attr);
7400 }
7401
7402 /* Get the dwarf_file_data from a file DIE attribute.  */
7403
7404 static inline struct dwarf_file_data *
7405 AT_file (dw_attr_ref a)
7406 {
7407   gcc_assert (a && AT_class (a) == dw_val_class_file);
7408   return a->dw_attr_val.v.val_file;
7409 }
7410
7411 /* Add a vms delta attribute value to a DIE.  */
7412
7413 static inline void
7414 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7415                   const char *lbl1, const char *lbl2)
7416 {
7417   dw_attr_node attr;
7418
7419   attr.dw_attr = attr_kind;
7420   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7421   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7422   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7423   add_dwarf_attr (die, &attr);
7424 }
7425
7426 /* Add a label identifier attribute value to a DIE.  */
7427
7428 static inline void
7429 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7430 {
7431   dw_attr_node attr;
7432
7433   attr.dw_attr = attr_kind;
7434   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7435   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7436   add_dwarf_attr (die, &attr);
7437 }
7438
7439 /* Add a section offset attribute value to a DIE, an offset into the
7440    debug_line section.  */
7441
7442 static inline void
7443 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7444                 const char *label)
7445 {
7446   dw_attr_node attr;
7447
7448   attr.dw_attr = attr_kind;
7449   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7450   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7451   add_dwarf_attr (die, &attr);
7452 }
7453
7454 /* Add a section offset attribute value to a DIE, an offset into the
7455    debug_macinfo section.  */
7456
7457 static inline void
7458 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7459                const char *label)
7460 {
7461   dw_attr_node attr;
7462
7463   attr.dw_attr = attr_kind;
7464   attr.dw_attr_val.val_class = dw_val_class_macptr;
7465   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7466   add_dwarf_attr (die, &attr);
7467 }
7468
7469 /* Add an offset attribute value to a DIE.  */
7470
7471 static inline void
7472 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7473                unsigned HOST_WIDE_INT offset)
7474 {
7475   dw_attr_node attr;
7476
7477   attr.dw_attr = attr_kind;
7478   attr.dw_attr_val.val_class = dw_val_class_offset;
7479   attr.dw_attr_val.v.val_offset = offset;
7480   add_dwarf_attr (die, &attr);
7481 }
7482
7483 /* Add an range_list attribute value to a DIE.  */
7484
7485 static void
7486 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7487                    long unsigned int offset)
7488 {
7489   dw_attr_node attr;
7490
7491   attr.dw_attr = attr_kind;
7492   attr.dw_attr_val.val_class = dw_val_class_range_list;
7493   attr.dw_attr_val.v.val_offset = offset;
7494   add_dwarf_attr (die, &attr);
7495 }
7496
7497 /* Return the start label of a delta attribute.  */
7498
7499 static inline const char *
7500 AT_vms_delta1 (dw_attr_ref a)
7501 {
7502   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7503   return a->dw_attr_val.v.val_vms_delta.lbl1;
7504 }
7505
7506 /* Return the end label of a delta attribute.  */
7507
7508 static inline const char *
7509 AT_vms_delta2 (dw_attr_ref a)
7510 {
7511   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7512   return a->dw_attr_val.v.val_vms_delta.lbl2;
7513 }
7514
7515 static inline const char *
7516 AT_lbl (dw_attr_ref a)
7517 {
7518   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7519                     || AT_class (a) == dw_val_class_lineptr
7520                     || AT_class (a) == dw_val_class_macptr));
7521   return a->dw_attr_val.v.val_lbl_id;
7522 }
7523
7524 /* Get the attribute of type attr_kind.  */
7525
7526 static dw_attr_ref
7527 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7528 {
7529   dw_attr_ref a;
7530   unsigned ix;
7531   dw_die_ref spec = NULL;
7532
7533   if (! die)
7534     return NULL;
7535
7536   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7537     if (a->dw_attr == attr_kind)
7538       return a;
7539     else if (a->dw_attr == DW_AT_specification
7540              || a->dw_attr == DW_AT_abstract_origin)
7541       spec = AT_ref (a);
7542
7543   if (spec)
7544     return get_AT (spec, attr_kind);
7545
7546   return NULL;
7547 }
7548
7549 /* Return the "low pc" attribute value, typically associated with a subprogram
7550    DIE.  Return null if the "low pc" attribute is either not present, or if it
7551    cannot be represented as an assembler label identifier.  */
7552
7553 static inline const char *
7554 get_AT_low_pc (dw_die_ref die)
7555 {
7556   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7557
7558   return a ? AT_lbl (a) : NULL;
7559 }
7560
7561 /* Return the "high pc" attribute value, typically associated with a subprogram
7562    DIE.  Return null if the "high pc" attribute is either not present, or if it
7563    cannot be represented as an assembler label identifier.  */
7564
7565 static inline const char *
7566 get_AT_hi_pc (dw_die_ref die)
7567 {
7568   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7569
7570   return a ? AT_lbl (a) : NULL;
7571 }
7572
7573 /* Return the value of the string attribute designated by ATTR_KIND, or
7574    NULL if it is not present.  */
7575
7576 static inline const char *
7577 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7578 {
7579   dw_attr_ref a = get_AT (die, attr_kind);
7580
7581   return a ? AT_string (a) : NULL;
7582 }
7583
7584 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7585    if it is not present.  */
7586
7587 static inline int
7588 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7589 {
7590   dw_attr_ref a = get_AT (die, attr_kind);
7591
7592   return a ? AT_flag (a) : 0;
7593 }
7594
7595 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7596    if it is not present.  */
7597
7598 static inline unsigned
7599 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7600 {
7601   dw_attr_ref a = get_AT (die, attr_kind);
7602
7603   return a ? AT_unsigned (a) : 0;
7604 }
7605
7606 static inline dw_die_ref
7607 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7608 {
7609   dw_attr_ref a = get_AT (die, attr_kind);
7610
7611   return a ? AT_ref (a) : NULL;
7612 }
7613
7614 static inline struct dwarf_file_data *
7615 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7616 {
7617   dw_attr_ref a = get_AT (die, attr_kind);
7618
7619   return a ? AT_file (a) : NULL;
7620 }
7621
7622 /* Return TRUE if the language is C++.  */
7623
7624 static inline bool
7625 is_cxx (void)
7626 {
7627   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7628
7629   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7630 }
7631
7632 /* Return TRUE if the language is Fortran.  */
7633
7634 static inline bool
7635 is_fortran (void)
7636 {
7637   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7638
7639   return (lang == DW_LANG_Fortran77
7640           || lang == DW_LANG_Fortran90
7641           || lang == DW_LANG_Fortran95);
7642 }
7643
7644 /* Return TRUE if the language is Ada.  */
7645
7646 static inline bool
7647 is_ada (void)
7648 {
7649   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7650
7651   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7652 }
7653
7654 /* Remove the specified attribute if present.  */
7655
7656 static void
7657 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7658 {
7659   dw_attr_ref a;
7660   unsigned ix;
7661
7662   if (! die)
7663     return;
7664
7665   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7666     if (a->dw_attr == attr_kind)
7667       {
7668         if (AT_class (a) == dw_val_class_str)
7669           if (a->dw_attr_val.v.val_str->refcount)
7670             a->dw_attr_val.v.val_str->refcount--;
7671
7672         /* VEC_ordered_remove should help reduce the number of abbrevs
7673            that are needed.  */
7674         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7675         return;
7676       }
7677 }
7678
7679 /* Remove CHILD from its parent.  PREV must have the property that
7680    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7681
7682 static void
7683 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7684 {
7685   gcc_assert (child->die_parent == prev->die_parent);
7686   gcc_assert (prev->die_sib == child);
7687   if (prev == child)
7688     {
7689       gcc_assert (child->die_parent->die_child == child);
7690       prev = NULL;
7691     }
7692   else
7693     prev->die_sib = child->die_sib;
7694   if (child->die_parent->die_child == child)
7695     child->die_parent->die_child = prev;
7696 }
7697
7698 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7699    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7700
7701 static void
7702 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7703 {
7704   dw_die_ref parent = old_child->die_parent;
7705
7706   gcc_assert (parent == prev->die_parent);
7707   gcc_assert (prev->die_sib == old_child);
7708
7709   new_child->die_parent = parent;
7710   if (prev == old_child)
7711     {
7712       gcc_assert (parent->die_child == old_child);
7713       new_child->die_sib = new_child;
7714     }
7715   else
7716     {
7717       prev->die_sib = new_child;
7718       new_child->die_sib = old_child->die_sib;
7719     }
7720   if (old_child->die_parent->die_child == old_child)
7721     old_child->die_parent->die_child = new_child;
7722 }
7723
7724 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7725
7726 static void
7727 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7728 {
7729   dw_die_ref c;
7730   new_parent->die_child = old_parent->die_child;
7731   old_parent->die_child = NULL;
7732   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7733 }
7734
7735 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7736    matches TAG.  */
7737
7738 static void
7739 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7740 {
7741   dw_die_ref c;
7742
7743   c = die->die_child;
7744   if (c) do {
7745     dw_die_ref prev = c;
7746     c = c->die_sib;
7747     while (c->die_tag == tag)
7748       {
7749         remove_child_with_prev (c, prev);
7750         /* Might have removed every child.  */
7751         if (c == c->die_sib)
7752           return;
7753         c = c->die_sib;
7754       }
7755   } while (c != die->die_child);
7756 }
7757
7758 /* Add a CHILD_DIE as the last child of DIE.  */
7759
7760 static void
7761 add_child_die (dw_die_ref die, dw_die_ref child_die)
7762 {
7763   /* FIXME this should probably be an assert.  */
7764   if (! die || ! child_die)
7765     return;
7766   gcc_assert (die != child_die);
7767
7768   child_die->die_parent = die;
7769   if (die->die_child)
7770     {
7771       child_die->die_sib = die->die_child->die_sib;
7772       die->die_child->die_sib = child_die;
7773     }
7774   else
7775     child_die->die_sib = child_die;
7776   die->die_child = child_die;
7777 }
7778
7779 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7780    is the specification, to the end of PARENT's list of children.
7781    This is done by removing and re-adding it.  */
7782
7783 static void
7784 splice_child_die (dw_die_ref parent, dw_die_ref child)
7785 {
7786   dw_die_ref p;
7787
7788   /* We want the declaration DIE from inside the class, not the
7789      specification DIE at toplevel.  */
7790   if (child->die_parent != parent)
7791     {
7792       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7793
7794       if (tmp)
7795         child = tmp;
7796     }
7797
7798   gcc_assert (child->die_parent == parent
7799               || (child->die_parent
7800                   == get_AT_ref (parent, DW_AT_specification)));
7801
7802   for (p = child->die_parent->die_child; ; p = p->die_sib)
7803     if (p->die_sib == child)
7804       {
7805         remove_child_with_prev (child, p);
7806         break;
7807       }
7808
7809   add_child_die (parent, child);
7810 }
7811
7812 /* Return a pointer to a newly created DIE node.  */
7813
7814 static inline dw_die_ref
7815 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7816 {
7817   dw_die_ref die = ggc_alloc_cleared_die_node ();
7818
7819   die->die_tag = tag_value;
7820
7821   if (parent_die != NULL)
7822     add_child_die (parent_die, die);
7823   else
7824     {
7825       limbo_die_node *limbo_node;
7826
7827       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7828       limbo_node->die = die;
7829       limbo_node->created_for = t;
7830       limbo_node->next = limbo_die_list;
7831       limbo_die_list = limbo_node;
7832     }
7833
7834   return die;
7835 }
7836
7837 /* Return the DIE associated with the given type specifier.  */
7838
7839 static inline dw_die_ref
7840 lookup_type_die (tree type)
7841 {
7842   return TYPE_SYMTAB_DIE (type);
7843 }
7844
7845 /* Equate a DIE to a given type specifier.  */
7846
7847 static inline void
7848 equate_type_number_to_die (tree type, dw_die_ref type_die)
7849 {
7850   TYPE_SYMTAB_DIE (type) = type_die;
7851 }
7852
7853 /* Returns a hash value for X (which really is a die_struct).  */
7854
7855 static hashval_t
7856 decl_die_table_hash (const void *x)
7857 {
7858   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7859 }
7860
7861 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7862
7863 static int
7864 decl_die_table_eq (const void *x, const void *y)
7865 {
7866   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7867 }
7868
7869 /* Return the DIE associated with a given declaration.  */
7870
7871 static inline dw_die_ref
7872 lookup_decl_die (tree decl)
7873 {
7874   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7875 }
7876
7877 /* Returns a hash value for X (which really is a var_loc_list).  */
7878
7879 static hashval_t
7880 decl_loc_table_hash (const void *x)
7881 {
7882   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7883 }
7884
7885 /* Return nonzero if decl_id of var_loc_list X is the same as
7886    UID of decl *Y.  */
7887
7888 static int
7889 decl_loc_table_eq (const void *x, const void *y)
7890 {
7891   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7892 }
7893
7894 /* Return the var_loc list associated with a given declaration.  */
7895
7896 static inline var_loc_list *
7897 lookup_decl_loc (const_tree decl)
7898 {
7899   if (!decl_loc_table)
7900     return NULL;
7901   return (var_loc_list *)
7902     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7903 }
7904
7905 /* Equate a DIE to a particular declaration.  */
7906
7907 static void
7908 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7909 {
7910   unsigned int decl_id = DECL_UID (decl);
7911   void **slot;
7912
7913   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7914   *slot = decl_die;
7915   decl_die->decl_id = decl_id;
7916 }
7917
7918 /* Return how many bits covers PIECE EXPR_LIST.  */
7919
7920 static int
7921 decl_piece_bitsize (rtx piece)
7922 {
7923   int ret = (int) GET_MODE (piece);
7924   if (ret)
7925     return ret;
7926   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7927               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7928   return INTVAL (XEXP (XEXP (piece, 0), 0));
7929 }
7930
7931 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7932
7933 static rtx *
7934 decl_piece_varloc_ptr (rtx piece)
7935 {
7936   if ((int) GET_MODE (piece))
7937     return &XEXP (piece, 0);
7938   else
7939     return &XEXP (XEXP (piece, 0), 1);
7940 }
7941
7942 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7943    Next is the chain of following piece nodes.  */
7944
7945 static rtx
7946 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7947 {
7948   if (bitsize <= (int) MAX_MACHINE_MODE)
7949     return alloc_EXPR_LIST (bitsize, loc_note, next);
7950   else
7951     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7952                                                GEN_INT (bitsize),
7953                                                loc_note), next);
7954 }
7955
7956 /* Return rtx that should be stored into loc field for
7957    LOC_NOTE and BITPOS/BITSIZE.  */
7958
7959 static rtx
7960 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7961                       HOST_WIDE_INT bitsize)
7962 {
7963   if (bitsize != -1)
7964     {
7965       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7966       if (bitpos != 0)
7967         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7968     }
7969   return loc_note;
7970 }
7971
7972 /* This function either modifies location piece list *DEST in
7973    place (if SRC and INNER is NULL), or copies location piece list
7974    *SRC to *DEST while modifying it.  Location BITPOS is modified
7975    to contain LOC_NOTE, any pieces overlapping it are removed resp.
7976    not copied and if needed some padding around it is added.
7977    When modifying in place, DEST should point to EXPR_LIST where
7978    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
7979    to the start of the whole list and INNER points to the EXPR_LIST
7980    where earlier pieces cover PIECE_BITPOS bits.  */
7981
7982 static void
7983 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
7984                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
7985                    HOST_WIDE_INT bitsize, rtx loc_note)
7986 {
7987   int diff;
7988   bool copy = inner != NULL;
7989
7990   if (copy)
7991     {
7992       /* First copy all nodes preceeding the current bitpos.  */
7993       while (src != inner)
7994         {
7995           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
7996                                    decl_piece_bitsize (*src), NULL_RTX);
7997           dest = &XEXP (*dest, 1);
7998           src = &XEXP (*src, 1);
7999         }
8000     }
8001   /* Add padding if needed.  */
8002   if (bitpos != piece_bitpos)
8003     {
8004       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8005                                copy ? NULL_RTX : *dest);
8006       dest = &XEXP (*dest, 1);
8007     }
8008   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8009     {
8010       gcc_assert (!copy);
8011       /* A piece with correct bitpos and bitsize already exist,
8012          just update the location for it and return.  */
8013       *decl_piece_varloc_ptr (*dest) = loc_note;
8014       return;
8015     }
8016   /* Add the piece that changed.  */
8017   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8018   dest = &XEXP (*dest, 1);
8019   /* Skip over pieces that overlap it.  */
8020   diff = bitpos - piece_bitpos + bitsize;
8021   if (!copy)
8022     src = dest;
8023   while (diff > 0 && *src)
8024     {
8025       rtx piece = *src;
8026       diff -= decl_piece_bitsize (piece);
8027       if (copy)
8028         src = &XEXP (piece, 1);
8029       else
8030         {
8031           *src = XEXP (piece, 1);
8032           free_EXPR_LIST_node (piece);
8033         }
8034     }
8035   /* Add padding if needed.  */
8036   if (diff < 0 && *src)
8037     {
8038       if (!copy)
8039         dest = src;
8040       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8041       dest = &XEXP (*dest, 1);
8042     }
8043   if (!copy)
8044     return;
8045   /* Finally copy all nodes following it.  */
8046   while (*src)
8047     {
8048       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8049                                decl_piece_bitsize (*src), NULL_RTX);
8050       dest = &XEXP (*dest, 1);
8051       src = &XEXP (*src, 1);
8052     }
8053 }
8054
8055 /* Add a variable location node to the linked list for DECL.  */
8056
8057 static struct var_loc_node *
8058 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8059 {
8060   unsigned int decl_id;
8061   var_loc_list *temp;
8062   void **slot;
8063   struct var_loc_node *loc = NULL;
8064   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8065
8066   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8067     {
8068       tree realdecl = DECL_DEBUG_EXPR (decl);
8069       if (realdecl && handled_component_p (realdecl))
8070         {
8071           HOST_WIDE_INT maxsize;
8072           tree innerdecl;
8073           innerdecl
8074             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8075           if (!DECL_P (innerdecl)
8076               || DECL_IGNORED_P (innerdecl)
8077               || TREE_STATIC (innerdecl)
8078               || bitsize <= 0
8079               || bitpos + bitsize > 256
8080               || bitsize != maxsize)
8081             return NULL;
8082           decl = innerdecl;
8083         }
8084     }
8085
8086   decl_id = DECL_UID (decl);
8087   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8088   if (*slot == NULL)
8089     {
8090       temp = ggc_alloc_cleared_var_loc_list ();
8091       temp->decl_id = decl_id;
8092       *slot = temp;
8093     }
8094   else
8095     temp = (var_loc_list *) *slot;
8096
8097   if (temp->last)
8098     {
8099       struct var_loc_node *last = temp->last, *unused = NULL;
8100       rtx *piece_loc = NULL, last_loc_note;
8101       int piece_bitpos = 0;
8102       if (last->next)
8103         {
8104           last = last->next;
8105           gcc_assert (last->next == NULL);
8106         }
8107       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8108         {
8109           piece_loc = &last->loc;
8110           do
8111             {
8112               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8113               if (piece_bitpos + cur_bitsize > bitpos)
8114                 break;
8115               piece_bitpos += cur_bitsize;
8116               piece_loc = &XEXP (*piece_loc, 1);
8117             }
8118           while (*piece_loc);
8119         }
8120       /* TEMP->LAST here is either pointer to the last but one or
8121          last element in the chained list, LAST is pointer to the
8122          last element.  */
8123       if (label && strcmp (last->label, label) == 0)
8124         {
8125           /* For SRA optimized variables if there weren't any real
8126              insns since last note, just modify the last node.  */
8127           if (piece_loc != NULL)
8128             {
8129               adjust_piece_list (piece_loc, NULL, NULL,
8130                                  bitpos, piece_bitpos, bitsize, loc_note);
8131               return NULL;
8132             }
8133           /* If the last note doesn't cover any instructions, remove it.  */
8134           if (temp->last != last)
8135             {
8136               temp->last->next = NULL;
8137               unused = last;
8138               last = temp->last;
8139               gcc_assert (strcmp (last->label, label) != 0);
8140             }
8141           else
8142             {
8143               gcc_assert (temp->first == temp->last);
8144               memset (temp->last, '\0', sizeof (*temp->last));
8145               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8146               return temp->last;
8147             }
8148         }
8149       if (bitsize == -1 && NOTE_P (last->loc))
8150         last_loc_note = last->loc;
8151       else if (piece_loc != NULL
8152                && *piece_loc != NULL_RTX
8153                && piece_bitpos == bitpos
8154                && decl_piece_bitsize (*piece_loc) == bitsize)
8155         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8156       else
8157         last_loc_note = NULL_RTX;
8158       /* If the current location is the same as the end of the list,
8159          and either both or neither of the locations is uninitialized,
8160          we have nothing to do.  */
8161       if (last_loc_note == NULL_RTX
8162           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8163                             NOTE_VAR_LOCATION_LOC (loc_note)))
8164           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8165                != NOTE_VAR_LOCATION_STATUS (loc_note))
8166               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8167                    == VAR_INIT_STATUS_UNINITIALIZED)
8168                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8169                       == VAR_INIT_STATUS_UNINITIALIZED))))
8170         {
8171           /* Add LOC to the end of list and update LAST.  If the last
8172              element of the list has been removed above, reuse its
8173              memory for the new node, otherwise allocate a new one.  */
8174           if (unused)
8175             {
8176               loc = unused;
8177               memset (loc, '\0', sizeof (*loc));
8178             }
8179           else
8180             loc = ggc_alloc_cleared_var_loc_node ();
8181           if (bitsize == -1 || piece_loc == NULL)
8182             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8183           else
8184             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8185                                bitpos, piece_bitpos, bitsize, loc_note);
8186           last->next = loc;
8187           /* Ensure TEMP->LAST will point either to the new last but one
8188              element of the chain, or to the last element in it.  */
8189           if (last != temp->last)
8190             temp->last = last;
8191         }
8192       else if (unused)
8193         ggc_free (unused);
8194     }
8195   else
8196     {
8197       loc = ggc_alloc_cleared_var_loc_node ();
8198       temp->first = loc;
8199       temp->last = loc;
8200       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8201     }
8202   return loc;
8203 }
8204 \f
8205 /* Keep track of the number of spaces used to indent the
8206    output of the debugging routines that print the structure of
8207    the DIE internal representation.  */
8208 static int print_indent;
8209
8210 /* Indent the line the number of spaces given by print_indent.  */
8211
8212 static inline void
8213 print_spaces (FILE *outfile)
8214 {
8215   fprintf (outfile, "%*s", print_indent, "");
8216 }
8217
8218 /* Print a type signature in hex.  */
8219
8220 static inline void
8221 print_signature (FILE *outfile, char *sig)
8222 {
8223   int i;
8224
8225   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8226     fprintf (outfile, "%02x", sig[i] & 0xff);
8227 }
8228
8229 /* Print the information associated with a given DIE, and its children.
8230    This routine is a debugging aid only.  */
8231
8232 static void
8233 print_die (dw_die_ref die, FILE *outfile)
8234 {
8235   dw_attr_ref a;
8236   dw_die_ref c;
8237   unsigned ix;
8238
8239   print_spaces (outfile);
8240   fprintf (outfile, "DIE %4ld: %s\n",
8241            die->die_offset, dwarf_tag_name (die->die_tag));
8242   print_spaces (outfile);
8243   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8244   fprintf (outfile, " offset: %ld\n", die->die_offset);
8245   if (dwarf_version >= 4 && die->die_id.die_type_node)
8246     {
8247       print_spaces (outfile);
8248       fprintf (outfile, "  signature: ");
8249       print_signature (outfile, die->die_id.die_type_node->signature);
8250       fprintf (outfile, "\n");
8251     }
8252
8253   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8254     {
8255       print_spaces (outfile);
8256       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8257
8258       switch (AT_class (a))
8259         {
8260         case dw_val_class_addr:
8261           fprintf (outfile, "address");
8262           break;
8263         case dw_val_class_offset:
8264           fprintf (outfile, "offset");
8265           break;
8266         case dw_val_class_loc:
8267           fprintf (outfile, "location descriptor");
8268           break;
8269         case dw_val_class_loc_list:
8270           fprintf (outfile, "location list -> label:%s",
8271                    AT_loc_list (a)->ll_symbol);
8272           break;
8273         case dw_val_class_range_list:
8274           fprintf (outfile, "range list");
8275           break;
8276         case dw_val_class_const:
8277           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8278           break;
8279         case dw_val_class_unsigned_const:
8280           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8281           break;
8282         case dw_val_class_const_double:
8283           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8284                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8285                    a->dw_attr_val.v.val_double.high,
8286                    a->dw_attr_val.v.val_double.low);
8287           break;
8288         case dw_val_class_vec:
8289           fprintf (outfile, "floating-point or vector constant");
8290           break;
8291         case dw_val_class_flag:
8292           fprintf (outfile, "%u", AT_flag (a));
8293           break;
8294         case dw_val_class_die_ref:
8295           if (AT_ref (a) != NULL)
8296             {
8297               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8298                 {
8299                   fprintf (outfile, "die -> signature: ");
8300                   print_signature (outfile,
8301                                    AT_ref (a)->die_id.die_type_node->signature);
8302                 }
8303               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8304                 fprintf (outfile, "die -> label: %s",
8305                          AT_ref (a)->die_id.die_symbol);
8306               else
8307                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8308             }
8309           else
8310             fprintf (outfile, "die -> <null>");
8311           break;
8312         case dw_val_class_vms_delta:
8313           fprintf (outfile, "delta: @slotcount(%s-%s)",
8314                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8315           break;
8316         case dw_val_class_lbl_id:
8317         case dw_val_class_lineptr:
8318         case dw_val_class_macptr:
8319           fprintf (outfile, "label: %s", AT_lbl (a));
8320           break;
8321         case dw_val_class_str:
8322           if (AT_string (a) != NULL)
8323             fprintf (outfile, "\"%s\"", AT_string (a));
8324           else
8325             fprintf (outfile, "<null>");
8326           break;
8327         case dw_val_class_file:
8328           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8329                    AT_file (a)->emitted_number);
8330           break;
8331         case dw_val_class_data8:
8332           {
8333             int i;
8334
8335             for (i = 0; i < 8; i++)
8336               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8337             break;
8338           }
8339         default:
8340           break;
8341         }
8342
8343       fprintf (outfile, "\n");
8344     }
8345
8346   if (die->die_child != NULL)
8347     {
8348       print_indent += 4;
8349       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8350       print_indent -= 4;
8351     }
8352   if (print_indent == 0)
8353     fprintf (outfile, "\n");
8354 }
8355
8356 /* Print the contents of the source code line number correspondence table.
8357    This routine is a debugging aid only.  */
8358
8359 static void
8360 print_dwarf_line_table (FILE *outfile)
8361 {
8362   unsigned i;
8363   dw_line_info_ref line_info;
8364
8365   fprintf (outfile, "\n\nDWARF source line information\n");
8366   for (i = 1; i < line_info_table_in_use; i++)
8367     {
8368       line_info = &line_info_table[i];
8369       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8370                line_info->dw_file_num,
8371                line_info->dw_line_num);
8372     }
8373
8374   fprintf (outfile, "\n\n");
8375 }
8376
8377 /* Print the information collected for a given DIE.  */
8378
8379 DEBUG_FUNCTION void
8380 debug_dwarf_die (dw_die_ref die)
8381 {
8382   print_die (die, stderr);
8383 }
8384
8385 /* Print all DWARF information collected for the compilation unit.
8386    This routine is a debugging aid only.  */
8387
8388 DEBUG_FUNCTION void
8389 debug_dwarf (void)
8390 {
8391   print_indent = 0;
8392   print_die (comp_unit_die, stderr);
8393   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8394     print_dwarf_line_table (stderr);
8395 }
8396 \f
8397 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8398    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8399    DIE that marks the start of the DIEs for this include file.  */
8400
8401 static dw_die_ref
8402 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8403 {
8404   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8405   dw_die_ref new_unit = gen_compile_unit_die (filename);
8406
8407   new_unit->die_sib = old_unit;
8408   return new_unit;
8409 }
8410
8411 /* Close an include-file CU and reopen the enclosing one.  */
8412
8413 static dw_die_ref
8414 pop_compile_unit (dw_die_ref old_unit)
8415 {
8416   dw_die_ref new_unit = old_unit->die_sib;
8417
8418   old_unit->die_sib = NULL;
8419   return new_unit;
8420 }
8421
8422 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8423 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8424
8425 /* Calculate the checksum of a location expression.  */
8426
8427 static inline void
8428 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8429 {
8430   int tem;
8431
8432   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8433   CHECKSUM (tem);
8434   CHECKSUM (loc->dw_loc_oprnd1);
8435   CHECKSUM (loc->dw_loc_oprnd2);
8436 }
8437
8438 /* Calculate the checksum of an attribute.  */
8439
8440 static void
8441 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8442 {
8443   dw_loc_descr_ref loc;
8444   rtx r;
8445
8446   CHECKSUM (at->dw_attr);
8447
8448   /* We don't care that this was compiled with a different compiler
8449      snapshot; if the output is the same, that's what matters.  */
8450   if (at->dw_attr == DW_AT_producer)
8451     return;
8452
8453   switch (AT_class (at))
8454     {
8455     case dw_val_class_const:
8456       CHECKSUM (at->dw_attr_val.v.val_int);
8457       break;
8458     case dw_val_class_unsigned_const:
8459       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8460       break;
8461     case dw_val_class_const_double:
8462       CHECKSUM (at->dw_attr_val.v.val_double);
8463       break;
8464     case dw_val_class_vec:
8465       CHECKSUM (at->dw_attr_val.v.val_vec);
8466       break;
8467     case dw_val_class_flag:
8468       CHECKSUM (at->dw_attr_val.v.val_flag);
8469       break;
8470     case dw_val_class_str:
8471       CHECKSUM_STRING (AT_string (at));
8472       break;
8473
8474     case dw_val_class_addr:
8475       r = AT_addr (at);
8476       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8477       CHECKSUM_STRING (XSTR (r, 0));
8478       break;
8479
8480     case dw_val_class_offset:
8481       CHECKSUM (at->dw_attr_val.v.val_offset);
8482       break;
8483
8484     case dw_val_class_loc:
8485       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8486         loc_checksum (loc, ctx);
8487       break;
8488
8489     case dw_val_class_die_ref:
8490       die_checksum (AT_ref (at), ctx, mark);
8491       break;
8492
8493     case dw_val_class_fde_ref:
8494     case dw_val_class_vms_delta:
8495     case dw_val_class_lbl_id:
8496     case dw_val_class_lineptr:
8497     case dw_val_class_macptr:
8498       break;
8499
8500     case dw_val_class_file:
8501       CHECKSUM_STRING (AT_file (at)->filename);
8502       break;
8503
8504     case dw_val_class_data8:
8505       CHECKSUM (at->dw_attr_val.v.val_data8);
8506       break;
8507
8508     default:
8509       break;
8510     }
8511 }
8512
8513 /* Calculate the checksum of a DIE.  */
8514
8515 static void
8516 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8517 {
8518   dw_die_ref c;
8519   dw_attr_ref a;
8520   unsigned ix;
8521
8522   /* To avoid infinite recursion.  */
8523   if (die->die_mark)
8524     {
8525       CHECKSUM (die->die_mark);
8526       return;
8527     }
8528   die->die_mark = ++(*mark);
8529
8530   CHECKSUM (die->die_tag);
8531
8532   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8533     attr_checksum (a, ctx, mark);
8534
8535   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8536 }
8537
8538 #undef CHECKSUM
8539 #undef CHECKSUM_STRING
8540
8541 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8542 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8543 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8544 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8545 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8546 #define CHECKSUM_ATTR(FOO) \
8547   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8548
8549 /* Calculate the checksum of a number in signed LEB128 format.  */
8550
8551 static void
8552 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8553 {
8554   unsigned char byte;
8555   bool more;
8556
8557   while (1)
8558     {
8559       byte = (value & 0x7f);
8560       value >>= 7;
8561       more = !((value == 0 && (byte & 0x40) == 0)
8562                 || (value == -1 && (byte & 0x40) != 0));
8563       if (more)
8564         byte |= 0x80;
8565       CHECKSUM (byte);
8566       if (!more)
8567         break;
8568     }
8569 }
8570
8571 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8572
8573 static void
8574 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8575 {
8576   while (1)
8577     {
8578       unsigned char byte = (value & 0x7f);
8579       value >>= 7;
8580       if (value != 0)
8581         /* More bytes to follow.  */
8582         byte |= 0x80;
8583       CHECKSUM (byte);
8584       if (value == 0)
8585         break;
8586     }
8587 }
8588
8589 /* Checksum the context of the DIE.  This adds the names of any
8590    surrounding namespaces or structures to the checksum.  */
8591
8592 static void
8593 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8594 {
8595   const char *name;
8596   dw_die_ref spec;
8597   int tag = die->die_tag;
8598
8599   if (tag != DW_TAG_namespace
8600       && tag != DW_TAG_structure_type
8601       && tag != DW_TAG_class_type)
8602     return;
8603
8604   name = get_AT_string (die, DW_AT_name);
8605
8606   spec = get_AT_ref (die, DW_AT_specification);
8607   if (spec != NULL)
8608     die = spec;
8609
8610   if (die->die_parent != NULL)
8611     checksum_die_context (die->die_parent, ctx);
8612
8613   CHECKSUM_ULEB128 ('C');
8614   CHECKSUM_ULEB128 (tag);
8615   if (name != NULL)
8616     CHECKSUM_STRING (name);
8617 }
8618
8619 /* Calculate the checksum of a location expression.  */
8620
8621 static inline void
8622 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8623 {
8624   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8625      were emitted as a DW_FORM_sdata instead of a location expression.  */
8626   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8627     {
8628       CHECKSUM_ULEB128 (DW_FORM_sdata);
8629       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8630       return;
8631     }
8632
8633   /* Otherwise, just checksum the raw location expression.  */
8634   while (loc != NULL)
8635     {
8636       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8637       CHECKSUM (loc->dw_loc_oprnd1);
8638       CHECKSUM (loc->dw_loc_oprnd2);
8639       loc = loc->dw_loc_next;
8640     }
8641 }
8642
8643 /* Calculate the checksum of an attribute.  */
8644
8645 static void
8646 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8647                        struct md5_ctx *ctx, int *mark)
8648 {
8649   dw_loc_descr_ref loc;
8650   rtx r;
8651
8652   if (AT_class (at) == dw_val_class_die_ref)
8653     {
8654       dw_die_ref target_die = AT_ref (at);
8655
8656       /* For pointer and reference types, we checksum only the (qualified)
8657          name of the target type (if there is a name).  For friend entries,
8658          we checksum only the (qualified) name of the target type or function.
8659          This allows the checksum to remain the same whether the target type
8660          is complete or not.  */
8661       if ((at->dw_attr == DW_AT_type
8662            && (tag == DW_TAG_pointer_type
8663                || tag == DW_TAG_reference_type
8664                || tag == DW_TAG_rvalue_reference_type
8665                || tag == DW_TAG_ptr_to_member_type))
8666           || (at->dw_attr == DW_AT_friend
8667               && tag == DW_TAG_friend))
8668         {
8669           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8670
8671           if (name_attr != NULL)
8672             {
8673               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8674
8675               if (decl == NULL)
8676                 decl = target_die;
8677               CHECKSUM_ULEB128 ('N');
8678               CHECKSUM_ULEB128 (at->dw_attr);
8679               if (decl->die_parent != NULL)
8680                 checksum_die_context (decl->die_parent, ctx);
8681               CHECKSUM_ULEB128 ('E');
8682               CHECKSUM_STRING (AT_string (name_attr));
8683               return;
8684             }
8685         }
8686
8687       /* For all other references to another DIE, we check to see if the
8688          target DIE has already been visited.  If it has, we emit a
8689          backward reference; if not, we descend recursively.  */
8690       if (target_die->die_mark > 0)
8691         {
8692           CHECKSUM_ULEB128 ('R');
8693           CHECKSUM_ULEB128 (at->dw_attr);
8694           CHECKSUM_ULEB128 (target_die->die_mark);
8695         }
8696       else
8697         {
8698           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8699
8700           if (decl == NULL)
8701             decl = target_die;
8702           target_die->die_mark = ++(*mark);
8703           CHECKSUM_ULEB128 ('T');
8704           CHECKSUM_ULEB128 (at->dw_attr);
8705           if (decl->die_parent != NULL)
8706             checksum_die_context (decl->die_parent, ctx);
8707           die_checksum_ordered (target_die, ctx, mark);
8708         }
8709       return;
8710     }
8711
8712   CHECKSUM_ULEB128 ('A');
8713   CHECKSUM_ULEB128 (at->dw_attr);
8714
8715   switch (AT_class (at))
8716     {
8717     case dw_val_class_const:
8718       CHECKSUM_ULEB128 (DW_FORM_sdata);
8719       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8720       break;
8721
8722     case dw_val_class_unsigned_const:
8723       CHECKSUM_ULEB128 (DW_FORM_sdata);
8724       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8725       break;
8726
8727     case dw_val_class_const_double:
8728       CHECKSUM_ULEB128 (DW_FORM_block);
8729       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8730       CHECKSUM (at->dw_attr_val.v.val_double);
8731       break;
8732
8733     case dw_val_class_vec:
8734       CHECKSUM_ULEB128 (DW_FORM_block);
8735       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8736       CHECKSUM (at->dw_attr_val.v.val_vec);
8737       break;
8738
8739     case dw_val_class_flag:
8740       CHECKSUM_ULEB128 (DW_FORM_flag);
8741       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8742       break;
8743
8744     case dw_val_class_str:
8745       CHECKSUM_ULEB128 (DW_FORM_string);
8746       CHECKSUM_STRING (AT_string (at));
8747       break;
8748
8749     case dw_val_class_addr:
8750       r = AT_addr (at);
8751       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8752       CHECKSUM_ULEB128 (DW_FORM_string);
8753       CHECKSUM_STRING (XSTR (r, 0));
8754       break;
8755
8756     case dw_val_class_offset:
8757       CHECKSUM_ULEB128 (DW_FORM_sdata);
8758       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8759       break;
8760
8761     case dw_val_class_loc:
8762       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8763         loc_checksum_ordered (loc, ctx);
8764       break;
8765
8766     case dw_val_class_fde_ref:
8767     case dw_val_class_lbl_id:
8768     case dw_val_class_lineptr:
8769     case dw_val_class_macptr:
8770       break;
8771
8772     case dw_val_class_file:
8773       CHECKSUM_ULEB128 (DW_FORM_string);
8774       CHECKSUM_STRING (AT_file (at)->filename);
8775       break;
8776
8777     case dw_val_class_data8:
8778       CHECKSUM (at->dw_attr_val.v.val_data8);
8779       break;
8780
8781     default:
8782       break;
8783     }
8784 }
8785
8786 struct checksum_attributes
8787 {
8788   dw_attr_ref at_name;
8789   dw_attr_ref at_type;
8790   dw_attr_ref at_friend;
8791   dw_attr_ref at_accessibility;
8792   dw_attr_ref at_address_class;
8793   dw_attr_ref at_allocated;
8794   dw_attr_ref at_artificial;
8795   dw_attr_ref at_associated;
8796   dw_attr_ref at_binary_scale;
8797   dw_attr_ref at_bit_offset;
8798   dw_attr_ref at_bit_size;
8799   dw_attr_ref at_bit_stride;
8800   dw_attr_ref at_byte_size;
8801   dw_attr_ref at_byte_stride;
8802   dw_attr_ref at_const_value;
8803   dw_attr_ref at_containing_type;
8804   dw_attr_ref at_count;
8805   dw_attr_ref at_data_location;
8806   dw_attr_ref at_data_member_location;
8807   dw_attr_ref at_decimal_scale;
8808   dw_attr_ref at_decimal_sign;
8809   dw_attr_ref at_default_value;
8810   dw_attr_ref at_digit_count;
8811   dw_attr_ref at_discr;
8812   dw_attr_ref at_discr_list;
8813   dw_attr_ref at_discr_value;
8814   dw_attr_ref at_encoding;
8815   dw_attr_ref at_endianity;
8816   dw_attr_ref at_explicit;
8817   dw_attr_ref at_is_optional;
8818   dw_attr_ref at_location;
8819   dw_attr_ref at_lower_bound;
8820   dw_attr_ref at_mutable;
8821   dw_attr_ref at_ordering;
8822   dw_attr_ref at_picture_string;
8823   dw_attr_ref at_prototyped;
8824   dw_attr_ref at_small;
8825   dw_attr_ref at_segment;
8826   dw_attr_ref at_string_length;
8827   dw_attr_ref at_threads_scaled;
8828   dw_attr_ref at_upper_bound;
8829   dw_attr_ref at_use_location;
8830   dw_attr_ref at_use_UTF8;
8831   dw_attr_ref at_variable_parameter;
8832   dw_attr_ref at_virtuality;
8833   dw_attr_ref at_visibility;
8834   dw_attr_ref at_vtable_elem_location;
8835 };
8836
8837 /* Collect the attributes that we will want to use for the checksum.  */
8838
8839 static void
8840 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8841 {
8842   dw_attr_ref a;
8843   unsigned ix;
8844
8845   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8846     {
8847       switch (a->dw_attr)
8848         {
8849         case DW_AT_name:
8850           attrs->at_name = a;
8851           break;
8852         case DW_AT_type:
8853           attrs->at_type = a;
8854           break;
8855         case DW_AT_friend:
8856           attrs->at_friend = a;
8857           break;
8858         case DW_AT_accessibility:
8859           attrs->at_accessibility = a;
8860           break;
8861         case DW_AT_address_class:
8862           attrs->at_address_class = a;
8863           break;
8864         case DW_AT_allocated:
8865           attrs->at_allocated = a;
8866           break;
8867         case DW_AT_artificial:
8868           attrs->at_artificial = a;
8869           break;
8870         case DW_AT_associated:
8871           attrs->at_associated = a;
8872           break;
8873         case DW_AT_binary_scale:
8874           attrs->at_binary_scale = a;
8875           break;
8876         case DW_AT_bit_offset:
8877           attrs->at_bit_offset = a;
8878           break;
8879         case DW_AT_bit_size:
8880           attrs->at_bit_size = a;
8881           break;
8882         case DW_AT_bit_stride:
8883           attrs->at_bit_stride = a;
8884           break;
8885         case DW_AT_byte_size:
8886           attrs->at_byte_size = a;
8887           break;
8888         case DW_AT_byte_stride:
8889           attrs->at_byte_stride = a;
8890           break;
8891         case DW_AT_const_value:
8892           attrs->at_const_value = a;
8893           break;
8894         case DW_AT_containing_type:
8895           attrs->at_containing_type = a;
8896           break;
8897         case DW_AT_count:
8898           attrs->at_count = a;
8899           break;
8900         case DW_AT_data_location:
8901           attrs->at_data_location = a;
8902           break;
8903         case DW_AT_data_member_location:
8904           attrs->at_data_member_location = a;
8905           break;
8906         case DW_AT_decimal_scale:
8907           attrs->at_decimal_scale = a;
8908           break;
8909         case DW_AT_decimal_sign:
8910           attrs->at_decimal_sign = a;
8911           break;
8912         case DW_AT_default_value:
8913           attrs->at_default_value = a;
8914           break;
8915         case DW_AT_digit_count:
8916           attrs->at_digit_count = a;
8917           break;
8918         case DW_AT_discr:
8919           attrs->at_discr = a;
8920           break;
8921         case DW_AT_discr_list:
8922           attrs->at_discr_list = a;
8923           break;
8924         case DW_AT_discr_value:
8925           attrs->at_discr_value = a;
8926           break;
8927         case DW_AT_encoding:
8928           attrs->at_encoding = a;
8929           break;
8930         case DW_AT_endianity:
8931           attrs->at_endianity = a;
8932           break;
8933         case DW_AT_explicit:
8934           attrs->at_explicit = a;
8935           break;
8936         case DW_AT_is_optional:
8937           attrs->at_is_optional = a;
8938           break;
8939         case DW_AT_location:
8940           attrs->at_location = a;
8941           break;
8942         case DW_AT_lower_bound:
8943           attrs->at_lower_bound = a;
8944           break;
8945         case DW_AT_mutable:
8946           attrs->at_mutable = a;
8947           break;
8948         case DW_AT_ordering:
8949           attrs->at_ordering = a;
8950           break;
8951         case DW_AT_picture_string:
8952           attrs->at_picture_string = a;
8953           break;
8954         case DW_AT_prototyped:
8955           attrs->at_prototyped = a;
8956           break;
8957         case DW_AT_small:
8958           attrs->at_small = a;
8959           break;
8960         case DW_AT_segment:
8961           attrs->at_segment = a;
8962           break;
8963         case DW_AT_string_length:
8964           attrs->at_string_length = a;
8965           break;
8966         case DW_AT_threads_scaled:
8967           attrs->at_threads_scaled = a;
8968           break;
8969         case DW_AT_upper_bound:
8970           attrs->at_upper_bound = a;
8971           break;
8972         case DW_AT_use_location:
8973           attrs->at_use_location = a;
8974           break;
8975         case DW_AT_use_UTF8:
8976           attrs->at_use_UTF8 = a;
8977           break;
8978         case DW_AT_variable_parameter:
8979           attrs->at_variable_parameter = a;
8980           break;
8981         case DW_AT_virtuality:
8982           attrs->at_virtuality = a;
8983           break;
8984         case DW_AT_visibility:
8985           attrs->at_visibility = a;
8986           break;
8987         case DW_AT_vtable_elem_location:
8988           attrs->at_vtable_elem_location = a;
8989           break;
8990         default:
8991           break;
8992         }
8993     }
8994 }
8995
8996 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8997
8998 static void
8999 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9000 {
9001   dw_die_ref c;
9002   dw_die_ref decl;
9003   struct checksum_attributes attrs;
9004
9005   CHECKSUM_ULEB128 ('D');
9006   CHECKSUM_ULEB128 (die->die_tag);
9007
9008   memset (&attrs, 0, sizeof (attrs));
9009
9010   decl = get_AT_ref (die, DW_AT_specification);
9011   if (decl != NULL)
9012     collect_checksum_attributes (&attrs, decl);
9013   collect_checksum_attributes (&attrs, die);
9014
9015   CHECKSUM_ATTR (attrs.at_name);
9016   CHECKSUM_ATTR (attrs.at_accessibility);
9017   CHECKSUM_ATTR (attrs.at_address_class);
9018   CHECKSUM_ATTR (attrs.at_allocated);
9019   CHECKSUM_ATTR (attrs.at_artificial);
9020   CHECKSUM_ATTR (attrs.at_associated);
9021   CHECKSUM_ATTR (attrs.at_binary_scale);
9022   CHECKSUM_ATTR (attrs.at_bit_offset);
9023   CHECKSUM_ATTR (attrs.at_bit_size);
9024   CHECKSUM_ATTR (attrs.at_bit_stride);
9025   CHECKSUM_ATTR (attrs.at_byte_size);
9026   CHECKSUM_ATTR (attrs.at_byte_stride);
9027   CHECKSUM_ATTR (attrs.at_const_value);
9028   CHECKSUM_ATTR (attrs.at_containing_type);
9029   CHECKSUM_ATTR (attrs.at_count);
9030   CHECKSUM_ATTR (attrs.at_data_location);
9031   CHECKSUM_ATTR (attrs.at_data_member_location);
9032   CHECKSUM_ATTR (attrs.at_decimal_scale);
9033   CHECKSUM_ATTR (attrs.at_decimal_sign);
9034   CHECKSUM_ATTR (attrs.at_default_value);
9035   CHECKSUM_ATTR (attrs.at_digit_count);
9036   CHECKSUM_ATTR (attrs.at_discr);
9037   CHECKSUM_ATTR (attrs.at_discr_list);
9038   CHECKSUM_ATTR (attrs.at_discr_value);
9039   CHECKSUM_ATTR (attrs.at_encoding);
9040   CHECKSUM_ATTR (attrs.at_endianity);
9041   CHECKSUM_ATTR (attrs.at_explicit);
9042   CHECKSUM_ATTR (attrs.at_is_optional);
9043   CHECKSUM_ATTR (attrs.at_location);
9044   CHECKSUM_ATTR (attrs.at_lower_bound);
9045   CHECKSUM_ATTR (attrs.at_mutable);
9046   CHECKSUM_ATTR (attrs.at_ordering);
9047   CHECKSUM_ATTR (attrs.at_picture_string);
9048   CHECKSUM_ATTR (attrs.at_prototyped);
9049   CHECKSUM_ATTR (attrs.at_small);
9050   CHECKSUM_ATTR (attrs.at_segment);
9051   CHECKSUM_ATTR (attrs.at_string_length);
9052   CHECKSUM_ATTR (attrs.at_threads_scaled);
9053   CHECKSUM_ATTR (attrs.at_upper_bound);
9054   CHECKSUM_ATTR (attrs.at_use_location);
9055   CHECKSUM_ATTR (attrs.at_use_UTF8);
9056   CHECKSUM_ATTR (attrs.at_variable_parameter);
9057   CHECKSUM_ATTR (attrs.at_virtuality);
9058   CHECKSUM_ATTR (attrs.at_visibility);
9059   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9060   CHECKSUM_ATTR (attrs.at_type);
9061   CHECKSUM_ATTR (attrs.at_friend);
9062
9063   /* Checksum the child DIEs, except for nested types and member functions.  */
9064   c = die->die_child;
9065   if (c) do {
9066     dw_attr_ref name_attr;
9067
9068     c = c->die_sib;
9069     name_attr = get_AT (c, DW_AT_name);
9070     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9071         && name_attr != NULL)
9072       {
9073         CHECKSUM_ULEB128 ('S');
9074         CHECKSUM_ULEB128 (c->die_tag);
9075         CHECKSUM_STRING (AT_string (name_attr));
9076       }
9077     else
9078       {
9079         /* Mark this DIE so it gets processed when unmarking.  */
9080         if (c->die_mark == 0)
9081           c->die_mark = -1;
9082         die_checksum_ordered (c, ctx, mark);
9083       }
9084   } while (c != die->die_child);
9085
9086   CHECKSUM_ULEB128 (0);
9087 }
9088
9089 #undef CHECKSUM
9090 #undef CHECKSUM_STRING
9091 #undef CHECKSUM_ATTR
9092 #undef CHECKSUM_LEB128
9093 #undef CHECKSUM_ULEB128
9094
9095 /* Generate the type signature for DIE.  This is computed by generating an
9096    MD5 checksum over the DIE's tag, its relevant attributes, and its
9097    children.  Attributes that are references to other DIEs are processed
9098    by recursion, using the MARK field to prevent infinite recursion.
9099    If the DIE is nested inside a namespace or another type, we also
9100    need to include that context in the signature.  The lower 64 bits
9101    of the resulting MD5 checksum comprise the signature.  */
9102
9103 static void
9104 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9105 {
9106   int mark;
9107   const char *name;
9108   unsigned char checksum[16];
9109   struct md5_ctx ctx;
9110   dw_die_ref decl;
9111
9112   name = get_AT_string (die, DW_AT_name);
9113   decl = get_AT_ref (die, DW_AT_specification);
9114
9115   /* First, compute a signature for just the type name (and its surrounding
9116      context, if any.  This is stored in the type unit DIE for link-time
9117      ODR (one-definition rule) checking.  */
9118
9119   if (is_cxx() && name != NULL)
9120     {
9121       md5_init_ctx (&ctx);
9122
9123       /* Checksum the names of surrounding namespaces and structures.  */
9124       if (decl != NULL && decl->die_parent != NULL)
9125         checksum_die_context (decl->die_parent, &ctx);
9126
9127       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9128       md5_process_bytes (name, strlen (name) + 1, &ctx);
9129       md5_finish_ctx (&ctx, checksum);
9130
9131       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9132     }
9133
9134   /* Next, compute the complete type signature.  */
9135
9136   md5_init_ctx (&ctx);
9137   mark = 1;
9138   die->die_mark = mark;
9139
9140   /* Checksum the names of surrounding namespaces and structures.  */
9141   if (decl != NULL && decl->die_parent != NULL)
9142     checksum_die_context (decl->die_parent, &ctx);
9143
9144   /* Checksum the DIE and its children.  */
9145   die_checksum_ordered (die, &ctx, &mark);
9146   unmark_all_dies (die);
9147   md5_finish_ctx (&ctx, checksum);
9148
9149   /* Store the signature in the type node and link the type DIE and the
9150      type node together.  */
9151   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9152           DWARF_TYPE_SIGNATURE_SIZE);
9153   die->die_id.die_type_node = type_node;
9154   type_node->type_die = die;
9155
9156   /* If the DIE is a specification, link its declaration to the type node
9157      as well.  */
9158   if (decl != NULL)
9159     decl->die_id.die_type_node = type_node;
9160 }
9161
9162 /* Do the location expressions look same?  */
9163 static inline int
9164 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9165 {
9166   return loc1->dw_loc_opc == loc2->dw_loc_opc
9167          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9168          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9169 }
9170
9171 /* Do the values look the same?  */
9172 static int
9173 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9174 {
9175   dw_loc_descr_ref loc1, loc2;
9176   rtx r1, r2;
9177
9178   if (v1->val_class != v2->val_class)
9179     return 0;
9180
9181   switch (v1->val_class)
9182     {
9183     case dw_val_class_const:
9184       return v1->v.val_int == v2->v.val_int;
9185     case dw_val_class_unsigned_const:
9186       return v1->v.val_unsigned == v2->v.val_unsigned;
9187     case dw_val_class_const_double:
9188       return v1->v.val_double.high == v2->v.val_double.high
9189              && v1->v.val_double.low == v2->v.val_double.low;
9190     case dw_val_class_vec:
9191       if (v1->v.val_vec.length != v2->v.val_vec.length
9192           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9193         return 0;
9194       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9195                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9196         return 0;
9197       return 1;
9198     case dw_val_class_flag:
9199       return v1->v.val_flag == v2->v.val_flag;
9200     case dw_val_class_str:
9201       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9202
9203     case dw_val_class_addr:
9204       r1 = v1->v.val_addr;
9205       r2 = v2->v.val_addr;
9206       if (GET_CODE (r1) != GET_CODE (r2))
9207         return 0;
9208       return !rtx_equal_p (r1, r2);
9209
9210     case dw_val_class_offset:
9211       return v1->v.val_offset == v2->v.val_offset;
9212
9213     case dw_val_class_loc:
9214       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9215            loc1 && loc2;
9216            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9217         if (!same_loc_p (loc1, loc2, mark))
9218           return 0;
9219       return !loc1 && !loc2;
9220
9221     case dw_val_class_die_ref:
9222       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9223
9224     case dw_val_class_fde_ref:
9225     case dw_val_class_vms_delta:
9226     case dw_val_class_lbl_id:
9227     case dw_val_class_lineptr:
9228     case dw_val_class_macptr:
9229       return 1;
9230
9231     case dw_val_class_file:
9232       return v1->v.val_file == v2->v.val_file;
9233
9234     case dw_val_class_data8:
9235       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9236
9237     default:
9238       return 1;
9239     }
9240 }
9241
9242 /* Do the attributes look the same?  */
9243
9244 static int
9245 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9246 {
9247   if (at1->dw_attr != at2->dw_attr)
9248     return 0;
9249
9250   /* We don't care that this was compiled with a different compiler
9251      snapshot; if the output is the same, that's what matters. */
9252   if (at1->dw_attr == DW_AT_producer)
9253     return 1;
9254
9255   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9256 }
9257
9258 /* Do the dies look the same?  */
9259
9260 static int
9261 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9262 {
9263   dw_die_ref c1, c2;
9264   dw_attr_ref a1;
9265   unsigned ix;
9266
9267   /* To avoid infinite recursion.  */
9268   if (die1->die_mark)
9269     return die1->die_mark == die2->die_mark;
9270   die1->die_mark = die2->die_mark = ++(*mark);
9271
9272   if (die1->die_tag != die2->die_tag)
9273     return 0;
9274
9275   if (VEC_length (dw_attr_node, die1->die_attr)
9276       != VEC_length (dw_attr_node, die2->die_attr))
9277     return 0;
9278
9279   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9280     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9281       return 0;
9282
9283   c1 = die1->die_child;
9284   c2 = die2->die_child;
9285   if (! c1)
9286     {
9287       if (c2)
9288         return 0;
9289     }
9290   else
9291     for (;;)
9292       {
9293         if (!same_die_p (c1, c2, mark))
9294           return 0;
9295         c1 = c1->die_sib;
9296         c2 = c2->die_sib;
9297         if (c1 == die1->die_child)
9298           {
9299             if (c2 == die2->die_child)
9300               break;
9301             else
9302               return 0;
9303           }
9304     }
9305
9306   return 1;
9307 }
9308
9309 /* Do the dies look the same?  Wrapper around same_die_p.  */
9310
9311 static int
9312 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9313 {
9314   int mark = 0;
9315   int ret = same_die_p (die1, die2, &mark);
9316
9317   unmark_all_dies (die1);
9318   unmark_all_dies (die2);
9319
9320   return ret;
9321 }
9322
9323 /* The prefix to attach to symbols on DIEs in the current comdat debug
9324    info section.  */
9325 static char *comdat_symbol_id;
9326
9327 /* The index of the current symbol within the current comdat CU.  */
9328 static unsigned int comdat_symbol_number;
9329
9330 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9331    children, and set comdat_symbol_id accordingly.  */
9332
9333 static void
9334 compute_section_prefix (dw_die_ref unit_die)
9335 {
9336   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9337   const char *base = die_name ? lbasename (die_name) : "anonymous";
9338   char *name = XALLOCAVEC (char, strlen (base) + 64);
9339   char *p;
9340   int i, mark;
9341   unsigned char checksum[16];
9342   struct md5_ctx ctx;
9343
9344   /* Compute the checksum of the DIE, then append part of it as hex digits to
9345      the name filename of the unit.  */
9346
9347   md5_init_ctx (&ctx);
9348   mark = 0;
9349   die_checksum (unit_die, &ctx, &mark);
9350   unmark_all_dies (unit_die);
9351   md5_finish_ctx (&ctx, checksum);
9352
9353   sprintf (name, "%s.", base);
9354   clean_symbol_name (name);
9355
9356   p = name + strlen (name);
9357   for (i = 0; i < 4; i++)
9358     {
9359       sprintf (p, "%.2x", checksum[i]);
9360       p += 2;
9361     }
9362
9363   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9364   comdat_symbol_number = 0;
9365 }
9366
9367 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9368
9369 static int
9370 is_type_die (dw_die_ref die)
9371 {
9372   switch (die->die_tag)
9373     {
9374     case DW_TAG_array_type:
9375     case DW_TAG_class_type:
9376     case DW_TAG_interface_type:
9377     case DW_TAG_enumeration_type:
9378     case DW_TAG_pointer_type:
9379     case DW_TAG_reference_type:
9380     case DW_TAG_rvalue_reference_type:
9381     case DW_TAG_string_type:
9382     case DW_TAG_structure_type:
9383     case DW_TAG_subroutine_type:
9384     case DW_TAG_union_type:
9385     case DW_TAG_ptr_to_member_type:
9386     case DW_TAG_set_type:
9387     case DW_TAG_subrange_type:
9388     case DW_TAG_base_type:
9389     case DW_TAG_const_type:
9390     case DW_TAG_file_type:
9391     case DW_TAG_packed_type:
9392     case DW_TAG_volatile_type:
9393     case DW_TAG_typedef:
9394       return 1;
9395     default:
9396       return 0;
9397     }
9398 }
9399
9400 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9401    Basically, we want to choose the bits that are likely to be shared between
9402    compilations (types) and leave out the bits that are specific to individual
9403    compilations (functions).  */
9404
9405 static int
9406 is_comdat_die (dw_die_ref c)
9407 {
9408   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9409      we do for stabs.  The advantage is a greater likelihood of sharing between
9410      objects that don't include headers in the same order (and therefore would
9411      put the base types in a different comdat).  jason 8/28/00 */
9412
9413   if (c->die_tag == DW_TAG_base_type)
9414     return 0;
9415
9416   if (c->die_tag == DW_TAG_pointer_type
9417       || c->die_tag == DW_TAG_reference_type
9418       || c->die_tag == DW_TAG_rvalue_reference_type
9419       || c->die_tag == DW_TAG_const_type
9420       || c->die_tag == DW_TAG_volatile_type)
9421     {
9422       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9423
9424       return t ? is_comdat_die (t) : 0;
9425     }
9426
9427   return is_type_die (c);
9428 }
9429
9430 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9431    compilation unit.  */
9432
9433 static int
9434 is_symbol_die (dw_die_ref c)
9435 {
9436   return (is_type_die (c)
9437           || is_declaration_die (c)
9438           || c->die_tag == DW_TAG_namespace
9439           || c->die_tag == DW_TAG_module);
9440 }
9441
9442 static char *
9443 gen_internal_sym (const char *prefix)
9444 {
9445   char buf[256];
9446
9447   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9448   return xstrdup (buf);
9449 }
9450
9451 /* Assign symbols to all worthy DIEs under DIE.  */
9452
9453 static void
9454 assign_symbol_names (dw_die_ref die)
9455 {
9456   dw_die_ref c;
9457
9458   if (is_symbol_die (die))
9459     {
9460       if (comdat_symbol_id)
9461         {
9462           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9463
9464           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9465                    comdat_symbol_id, comdat_symbol_number++);
9466           die->die_id.die_symbol = xstrdup (p);
9467         }
9468       else
9469         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9470     }
9471
9472   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9473 }
9474
9475 struct cu_hash_table_entry
9476 {
9477   dw_die_ref cu;
9478   unsigned min_comdat_num, max_comdat_num;
9479   struct cu_hash_table_entry *next;
9480 };
9481
9482 /* Routines to manipulate hash table of CUs.  */
9483 static hashval_t
9484 htab_cu_hash (const void *of)
9485 {
9486   const struct cu_hash_table_entry *const entry =
9487     (const struct cu_hash_table_entry *) of;
9488
9489   return htab_hash_string (entry->cu->die_id.die_symbol);
9490 }
9491
9492 static int
9493 htab_cu_eq (const void *of1, const void *of2)
9494 {
9495   const struct cu_hash_table_entry *const entry1 =
9496     (const struct cu_hash_table_entry *) of1;
9497   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9498
9499   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9500 }
9501
9502 static void
9503 htab_cu_del (void *what)
9504 {
9505   struct cu_hash_table_entry *next,
9506     *entry = (struct cu_hash_table_entry *) what;
9507
9508   while (entry)
9509     {
9510       next = entry->next;
9511       free (entry);
9512       entry = next;
9513     }
9514 }
9515
9516 /* Check whether we have already seen this CU and set up SYM_NUM
9517    accordingly.  */
9518 static int
9519 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9520 {
9521   struct cu_hash_table_entry dummy;
9522   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9523
9524   dummy.max_comdat_num = 0;
9525
9526   slot = (struct cu_hash_table_entry **)
9527     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9528         INSERT);
9529   entry = *slot;
9530
9531   for (; entry; last = entry, entry = entry->next)
9532     {
9533       if (same_die_p_wrap (cu, entry->cu))
9534         break;
9535     }
9536
9537   if (entry)
9538     {
9539       *sym_num = entry->min_comdat_num;
9540       return 1;
9541     }
9542
9543   entry = XCNEW (struct cu_hash_table_entry);
9544   entry->cu = cu;
9545   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9546   entry->next = *slot;
9547   *slot = entry;
9548
9549   return 0;
9550 }
9551
9552 /* Record SYM_NUM to record of CU in HTABLE.  */
9553 static void
9554 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9555 {
9556   struct cu_hash_table_entry **slot, *entry;
9557
9558   slot = (struct cu_hash_table_entry **)
9559     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9560         NO_INSERT);
9561   entry = *slot;
9562
9563   entry->max_comdat_num = sym_num;
9564 }
9565
9566 /* Traverse the DIE (which is always comp_unit_die), and set up
9567    additional compilation units for each of the include files we see
9568    bracketed by BINCL/EINCL.  */
9569
9570 static void
9571 break_out_includes (dw_die_ref die)
9572 {
9573   dw_die_ref c;
9574   dw_die_ref unit = NULL;
9575   limbo_die_node *node, **pnode;
9576   htab_t cu_hash_table;
9577
9578   c = die->die_child;
9579   if (c) do {
9580     dw_die_ref prev = c;
9581     c = c->die_sib;
9582     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9583            || (unit && is_comdat_die (c)))
9584       {
9585         dw_die_ref next = c->die_sib;
9586
9587         /* This DIE is for a secondary CU; remove it from the main one.  */
9588         remove_child_with_prev (c, prev);
9589
9590         if (c->die_tag == DW_TAG_GNU_BINCL)
9591           unit = push_new_compile_unit (unit, c);
9592         else if (c->die_tag == DW_TAG_GNU_EINCL)
9593           unit = pop_compile_unit (unit);
9594         else
9595           add_child_die (unit, c);
9596         c = next;
9597         if (c == die->die_child)
9598           break;
9599       }
9600   } while (c != die->die_child);
9601
9602 #if 0
9603   /* We can only use this in debugging, since the frontend doesn't check
9604      to make sure that we leave every include file we enter.  */
9605   gcc_assert (!unit);
9606 #endif
9607
9608   assign_symbol_names (die);
9609   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9610   for (node = limbo_die_list, pnode = &limbo_die_list;
9611        node;
9612        node = node->next)
9613     {
9614       int is_dupl;
9615
9616       compute_section_prefix (node->die);
9617       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9618                         &comdat_symbol_number);
9619       assign_symbol_names (node->die);
9620       if (is_dupl)
9621         *pnode = node->next;
9622       else
9623         {
9624           pnode = &node->next;
9625           record_comdat_symbol_number (node->die, cu_hash_table,
9626                 comdat_symbol_number);
9627         }
9628     }
9629   htab_delete (cu_hash_table);
9630 }
9631
9632 /* Return non-zero if this DIE is a declaration.  */
9633
9634 static int
9635 is_declaration_die (dw_die_ref die)
9636 {
9637   dw_attr_ref a;
9638   unsigned ix;
9639
9640   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9641     if (a->dw_attr == DW_AT_declaration)
9642       return 1;
9643
9644   return 0;
9645 }
9646
9647 /* Return non-zero if this DIE is nested inside a subprogram.  */
9648
9649 static int
9650 is_nested_in_subprogram (dw_die_ref die)
9651 {
9652   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9653
9654   if (decl == NULL)
9655     decl = die;
9656   return local_scope_p (decl);
9657 }
9658
9659 /* Return non-zero if this is a type DIE that should be moved to a
9660    COMDAT .debug_types section.  */
9661
9662 static int
9663 should_move_die_to_comdat (dw_die_ref die)
9664 {
9665   switch (die->die_tag)
9666     {
9667     case DW_TAG_class_type:
9668     case DW_TAG_structure_type:
9669     case DW_TAG_enumeration_type:
9670     case DW_TAG_union_type:
9671       /* Don't move declarations, inlined instances, or types nested in a
9672          subprogram.  */
9673       if (is_declaration_die (die)
9674           || get_AT (die, DW_AT_abstract_origin)
9675           || is_nested_in_subprogram (die))
9676         return 0;
9677       return 1;
9678     case DW_TAG_array_type:
9679     case DW_TAG_interface_type:
9680     case DW_TAG_pointer_type:
9681     case DW_TAG_reference_type:
9682     case DW_TAG_rvalue_reference_type:
9683     case DW_TAG_string_type:
9684     case DW_TAG_subroutine_type:
9685     case DW_TAG_ptr_to_member_type:
9686     case DW_TAG_set_type:
9687     case DW_TAG_subrange_type:
9688     case DW_TAG_base_type:
9689     case DW_TAG_const_type:
9690     case DW_TAG_file_type:
9691     case DW_TAG_packed_type:
9692     case DW_TAG_volatile_type:
9693     case DW_TAG_typedef:
9694     default:
9695       return 0;
9696     }
9697 }
9698
9699 /* Make a clone of DIE.  */
9700
9701 static dw_die_ref
9702 clone_die (dw_die_ref die)
9703 {
9704   dw_die_ref clone;
9705   dw_attr_ref a;
9706   unsigned ix;
9707
9708   clone = ggc_alloc_cleared_die_node ();
9709   clone->die_tag = die->die_tag;
9710
9711   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9712     add_dwarf_attr (clone, a);
9713
9714   return clone;
9715 }
9716
9717 /* Make a clone of the tree rooted at DIE.  */
9718
9719 static dw_die_ref
9720 clone_tree (dw_die_ref die)
9721 {
9722   dw_die_ref c;
9723   dw_die_ref clone = clone_die (die);
9724
9725   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9726
9727   return clone;
9728 }
9729
9730 /* Make a clone of DIE as a declaration.  */
9731
9732 static dw_die_ref
9733 clone_as_declaration (dw_die_ref die)
9734 {
9735   dw_die_ref clone;
9736   dw_die_ref decl;
9737   dw_attr_ref a;
9738   unsigned ix;
9739
9740   /* If the DIE is already a declaration, just clone it.  */
9741   if (is_declaration_die (die))
9742     return clone_die (die);
9743
9744   /* If the DIE is a specification, just clone its declaration DIE.  */
9745   decl = get_AT_ref (die, DW_AT_specification);
9746   if (decl != NULL)
9747     return clone_die (decl);
9748
9749   clone = ggc_alloc_cleared_die_node ();
9750   clone->die_tag = die->die_tag;
9751
9752   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9753     {
9754       /* We don't want to copy over all attributes.
9755          For example we don't want DW_AT_byte_size because otherwise we will no
9756          longer have a declaration and GDB will treat it as a definition.  */
9757
9758       switch (a->dw_attr)
9759         {
9760         case DW_AT_artificial:
9761         case DW_AT_containing_type:
9762         case DW_AT_external:
9763         case DW_AT_name:
9764         case DW_AT_type:
9765         case DW_AT_virtuality:
9766         case DW_AT_linkage_name:
9767         case DW_AT_MIPS_linkage_name:
9768           add_dwarf_attr (clone, a);
9769           break;
9770         case DW_AT_byte_size:
9771         default:
9772           break;
9773         }
9774     }
9775
9776   if (die->die_id.die_type_node)
9777     add_AT_die_ref (clone, DW_AT_signature, die);
9778
9779   add_AT_flag (clone, DW_AT_declaration, 1);
9780   return clone;
9781 }
9782
9783 /* Copy the declaration context to the new compile unit DIE.  This includes
9784    any surrounding namespace or type declarations.  If the DIE has an
9785    AT_specification attribute, it also includes attributes and children
9786    attached to the specification.  */
9787
9788 static void
9789 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9790 {
9791   dw_die_ref decl;
9792   dw_die_ref new_decl;
9793
9794   decl = get_AT_ref (die, DW_AT_specification);
9795   if (decl == NULL)
9796     decl = die;
9797   else
9798     {
9799       unsigned ix;
9800       dw_die_ref c;
9801       dw_attr_ref a;
9802
9803       /* Copy the type node pointer from the new DIE to the original
9804          declaration DIE so we can forward references later.  */
9805       decl->die_id.die_type_node = die->die_id.die_type_node;
9806
9807       remove_AT (die, DW_AT_specification);
9808
9809       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9810         {
9811           if (a->dw_attr != DW_AT_name
9812               && a->dw_attr != DW_AT_declaration
9813               && a->dw_attr != DW_AT_external)
9814             add_dwarf_attr (die, a);
9815         }
9816
9817       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9818     }
9819
9820   if (decl->die_parent != NULL
9821       && decl->die_parent->die_tag != DW_TAG_compile_unit
9822       && decl->die_parent->die_tag != DW_TAG_type_unit)
9823     {
9824       new_decl = copy_ancestor_tree (unit, decl, NULL);
9825       if (new_decl != NULL)
9826         {
9827           remove_AT (new_decl, DW_AT_signature);
9828           add_AT_specification (die, new_decl);
9829         }
9830     }
9831 }
9832
9833 /* Generate the skeleton ancestor tree for the given NODE, then clone
9834    the DIE and add the clone into the tree.  */
9835
9836 static void
9837 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9838 {
9839   if (node->new_die != NULL)
9840     return;
9841
9842   node->new_die = clone_as_declaration (node->old_die);
9843
9844   if (node->parent != NULL)
9845     {
9846       generate_skeleton_ancestor_tree (node->parent);
9847       add_child_die (node->parent->new_die, node->new_die);
9848     }
9849 }
9850
9851 /* Generate a skeleton tree of DIEs containing any declarations that are
9852    found in the original tree.  We traverse the tree looking for declaration
9853    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9854
9855 static void
9856 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9857 {
9858   skeleton_chain_node node;
9859   dw_die_ref c;
9860   dw_die_ref first;
9861   dw_die_ref prev = NULL;
9862   dw_die_ref next = NULL;
9863
9864   node.parent = parent;
9865
9866   first = c = parent->old_die->die_child;
9867   if (c)
9868     next = c->die_sib;
9869   if (c) do {
9870     if (prev == NULL || prev->die_sib == c)
9871       prev = c;
9872     c = next;
9873     next = (c == first ? NULL : c->die_sib);
9874     node.old_die = c;
9875     node.new_die = NULL;
9876     if (is_declaration_die (c))
9877       {
9878         /* Clone the existing DIE, move the original to the skeleton
9879            tree (which is in the main CU), and put the clone, with
9880            all the original's children, where the original came from.  */
9881         dw_die_ref clone = clone_die (c);
9882         move_all_children (c, clone);
9883
9884         replace_child (c, clone, prev);
9885         generate_skeleton_ancestor_tree (parent);
9886         add_child_die (parent->new_die, c);
9887         node.new_die = c;
9888         c = clone;
9889       }
9890     generate_skeleton_bottom_up (&node);
9891   } while (next != NULL);
9892 }
9893
9894 /* Wrapper function for generate_skeleton_bottom_up.  */
9895
9896 static dw_die_ref
9897 generate_skeleton (dw_die_ref die)
9898 {
9899   skeleton_chain_node node;
9900
9901   node.old_die = die;
9902   node.new_die = NULL;
9903   node.parent = NULL;
9904
9905   /* If this type definition is nested inside another type,
9906      always leave at least a declaration in its place.  */
9907   if (die->die_parent != NULL && is_type_die (die->die_parent))
9908     node.new_die = clone_as_declaration (die);
9909
9910   generate_skeleton_bottom_up (&node);
9911   return node.new_die;
9912 }
9913
9914 /* Remove the DIE from its parent, possibly replacing it with a cloned
9915    declaration.  The original DIE will be moved to a new compile unit
9916    so that existing references to it follow it to the new location.  If
9917    any of the original DIE's descendants is a declaration, we need to
9918    replace the original DIE with a skeleton tree and move the
9919    declarations back into the skeleton tree.  */
9920
9921 static dw_die_ref
9922 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9923 {
9924   dw_die_ref skeleton;
9925
9926   skeleton = generate_skeleton (child);
9927   if (skeleton == NULL)
9928     remove_child_with_prev (child, prev);
9929   else
9930     {
9931       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9932       replace_child (child, skeleton, prev);
9933     }
9934
9935   return skeleton;
9936 }
9937
9938 /* Traverse the DIE and set up additional .debug_types sections for each
9939    type worthy of being placed in a COMDAT section.  */
9940
9941 static void
9942 break_out_comdat_types (dw_die_ref die)
9943 {
9944   dw_die_ref c;
9945   dw_die_ref first;
9946   dw_die_ref prev = NULL;
9947   dw_die_ref next = NULL;
9948   dw_die_ref unit = NULL;
9949
9950   first = c = die->die_child;
9951   if (c)
9952     next = c->die_sib;
9953   if (c) do {
9954     if (prev == NULL || prev->die_sib == c)
9955       prev = c;
9956     c = next;
9957     next = (c == first ? NULL : c->die_sib);
9958     if (should_move_die_to_comdat (c))
9959       {
9960         dw_die_ref replacement;
9961         comdat_type_node_ref type_node;
9962
9963         /* Create a new type unit DIE as the root for the new tree, and
9964            add it to the list of comdat types.  */
9965         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9966         add_AT_unsigned (unit, DW_AT_language,
9967                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9968         type_node = ggc_alloc_cleared_comdat_type_node ();
9969         type_node->root_die = unit;
9970         type_node->next = comdat_type_list;
9971         comdat_type_list = type_node;
9972
9973         /* Generate the type signature.  */
9974         generate_type_signature (c, type_node);
9975
9976         /* Copy the declaration context, attributes, and children of the
9977            declaration into the new compile unit DIE.  */
9978         copy_declaration_context (unit, c);
9979
9980         /* Remove this DIE from the main CU.  */
9981         replacement = remove_child_or_replace_with_skeleton (c, prev);
9982
9983         /* Break out nested types into their own type units.  */
9984         break_out_comdat_types (c);
9985
9986         /* Add the DIE to the new compunit.  */
9987         add_child_die (unit, c);
9988
9989         if (replacement != NULL)
9990           c = replacement;
9991       }
9992     else if (c->die_tag == DW_TAG_namespace
9993              || c->die_tag == DW_TAG_class_type
9994              || c->die_tag == DW_TAG_structure_type
9995              || c->die_tag == DW_TAG_union_type)
9996       {
9997         /* Look for nested types that can be broken out.  */
9998         break_out_comdat_types (c);
9999       }
10000   } while (next != NULL);
10001 }
10002
10003 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10004
10005 struct decl_table_entry
10006 {
10007   dw_die_ref orig;
10008   dw_die_ref copy;
10009 };
10010
10011 /* Routines to manipulate hash table of copied declarations.  */
10012
10013 static hashval_t
10014 htab_decl_hash (const void *of)
10015 {
10016   const struct decl_table_entry *const entry =
10017     (const struct decl_table_entry *) of;
10018
10019   return htab_hash_pointer (entry->orig);
10020 }
10021
10022 static int
10023 htab_decl_eq (const void *of1, const void *of2)
10024 {
10025   const struct decl_table_entry *const entry1 =
10026     (const struct decl_table_entry *) of1;
10027   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10028
10029   return entry1->orig == entry2;
10030 }
10031
10032 static void
10033 htab_decl_del (void *what)
10034 {
10035   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10036
10037   free (entry);
10038 }
10039
10040 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10041    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10042    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10043    to check if the ancestor has already been copied into UNIT.  */
10044
10045 static dw_die_ref
10046 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10047 {
10048   dw_die_ref parent = die->die_parent;
10049   dw_die_ref new_parent = unit;
10050   dw_die_ref copy;
10051   void **slot = NULL;
10052   struct decl_table_entry *entry = NULL;
10053
10054   if (decl_table)
10055     {
10056       /* Check if the entry has already been copied to UNIT.  */
10057       slot = htab_find_slot_with_hash (decl_table, die,
10058                                        htab_hash_pointer (die), INSERT);
10059       if (*slot != HTAB_EMPTY_ENTRY)
10060         {
10061           entry = (struct decl_table_entry *) *slot;
10062           return entry->copy;
10063         }
10064
10065       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10066       entry = XCNEW (struct decl_table_entry);
10067       entry->orig = die;
10068       entry->copy = NULL;
10069       *slot = entry;
10070     }
10071
10072   if (parent != NULL)
10073     {
10074       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10075       if (spec != NULL)
10076         parent = spec;
10077       if (parent->die_tag != DW_TAG_compile_unit
10078           && parent->die_tag != DW_TAG_type_unit)
10079         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10080     }
10081
10082   copy = clone_as_declaration (die);
10083   add_child_die (new_parent, copy);
10084
10085   if (decl_table != NULL)
10086     {
10087       /* Record the pointer to the copy.  */
10088       entry->copy = copy;
10089     }
10090
10091   return copy;
10092 }
10093
10094 /* Walk the DIE and its children, looking for references to incomplete
10095    or trivial types that are unmarked (i.e., that are not in the current
10096    type_unit).  */
10097
10098 static void
10099 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10100 {
10101   dw_die_ref c;
10102   dw_attr_ref a;
10103   unsigned ix;
10104
10105   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10106     {
10107       if (AT_class (a) == dw_val_class_die_ref)
10108         {
10109           dw_die_ref targ = AT_ref (a);
10110           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10111           void **slot;
10112           struct decl_table_entry *entry;
10113
10114           if (targ->die_mark != 0 || type_node != NULL)
10115             continue;
10116
10117           slot = htab_find_slot_with_hash (decl_table, targ,
10118                                            htab_hash_pointer (targ), INSERT);
10119
10120           if (*slot != HTAB_EMPTY_ENTRY)
10121             {
10122               /* TARG has already been copied, so we just need to
10123                  modify the reference to point to the copy.  */
10124               entry = (struct decl_table_entry *) *slot;
10125               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10126             }
10127           else
10128             {
10129               dw_die_ref parent = unit;
10130               dw_die_ref copy = clone_tree (targ);
10131
10132               /* Make sure the cloned tree is marked as part of the
10133                  type unit.  */
10134               mark_dies (copy);
10135
10136               /* Record in DECL_TABLE that TARG has been copied.
10137                  Need to do this now, before the recursive call,
10138                  because DECL_TABLE may be expanded and SLOT
10139                  would no longer be a valid pointer.  */
10140               entry = XCNEW (struct decl_table_entry);
10141               entry->orig = targ;
10142               entry->copy = copy;
10143               *slot = entry;
10144
10145               /* If TARG has surrounding context, copy its ancestor tree
10146                  into the new type unit.  */
10147               if (targ->die_parent != NULL
10148                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10149                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10150                 parent = copy_ancestor_tree (unit, targ->die_parent,
10151                                              decl_table);
10152
10153               add_child_die (parent, copy);
10154               a->dw_attr_val.v.val_die_ref.die = copy;
10155
10156               /* Make sure the newly-copied DIE is walked.  If it was
10157                  installed in a previously-added context, it won't
10158                  get visited otherwise.  */
10159               if (parent != unit)
10160                 {
10161                   /* Find the highest point of the newly-added tree,
10162                      mark each node along the way, and walk from there.  */
10163                   parent->die_mark = 1;
10164                   while (parent->die_parent
10165                          && parent->die_parent->die_mark == 0)
10166                     {
10167                       parent = parent->die_parent;
10168                       parent->die_mark = 1;
10169                     }
10170                   copy_decls_walk (unit, parent, decl_table);
10171                 }
10172             }
10173         }
10174     }
10175
10176   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10177 }
10178
10179 /* Copy declarations for "unworthy" types into the new comdat section.
10180    Incomplete types, modified types, and certain other types aren't broken
10181    out into comdat sections of their own, so they don't have a signature,
10182    and we need to copy the declaration into the same section so that we
10183    don't have an external reference.  */
10184
10185 static void
10186 copy_decls_for_unworthy_types (dw_die_ref unit)
10187 {
10188   htab_t decl_table;
10189
10190   mark_dies (unit);
10191   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10192   copy_decls_walk (unit, unit, decl_table);
10193   htab_delete (decl_table);
10194   unmark_dies (unit);
10195 }
10196
10197 /* Traverse the DIE and add a sibling attribute if it may have the
10198    effect of speeding up access to siblings.  To save some space,
10199    avoid generating sibling attributes for DIE's without children.  */
10200
10201 static void
10202 add_sibling_attributes (dw_die_ref die)
10203 {
10204   dw_die_ref c;
10205
10206   if (! die->die_child)
10207     return;
10208
10209   if (die->die_parent && die != die->die_parent->die_child)
10210     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10211
10212   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10213 }
10214
10215 /* Output all location lists for the DIE and its children.  */
10216
10217 static void
10218 output_location_lists (dw_die_ref die)
10219 {
10220   dw_die_ref c;
10221   dw_attr_ref a;
10222   unsigned ix;
10223
10224   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10225     if (AT_class (a) == dw_val_class_loc_list)
10226       output_loc_list (AT_loc_list (a));
10227
10228   FOR_EACH_CHILD (die, c, output_location_lists (c));
10229 }
10230
10231 /* The format of each DIE (and its attribute value pairs) is encoded in an
10232    abbreviation table.  This routine builds the abbreviation table and assigns
10233    a unique abbreviation id for each abbreviation entry.  The children of each
10234    die are visited recursively.  */
10235
10236 static void
10237 build_abbrev_table (dw_die_ref die)
10238 {
10239   unsigned long abbrev_id;
10240   unsigned int n_alloc;
10241   dw_die_ref c;
10242   dw_attr_ref a;
10243   unsigned ix;
10244
10245   /* Scan the DIE references, and mark as external any that refer to
10246      DIEs from other CUs (i.e. those which are not marked).  */
10247   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10248     if (AT_class (a) == dw_val_class_die_ref
10249         && AT_ref (a)->die_mark == 0)
10250       {
10251         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10252         set_AT_ref_external (a, 1);
10253       }
10254
10255   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10256     {
10257       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10258       dw_attr_ref die_a, abbrev_a;
10259       unsigned ix;
10260       bool ok = true;
10261
10262       if (abbrev->die_tag != die->die_tag)
10263         continue;
10264       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10265         continue;
10266
10267       if (VEC_length (dw_attr_node, abbrev->die_attr)
10268           != VEC_length (dw_attr_node, die->die_attr))
10269         continue;
10270
10271       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10272         {
10273           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10274           if ((abbrev_a->dw_attr != die_a->dw_attr)
10275               || (value_format (abbrev_a) != value_format (die_a)))
10276             {
10277               ok = false;
10278               break;
10279             }
10280         }
10281       if (ok)
10282         break;
10283     }
10284
10285   if (abbrev_id >= abbrev_die_table_in_use)
10286     {
10287       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10288         {
10289           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10290           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10291                                             n_alloc);
10292
10293           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10294                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10295           abbrev_die_table_allocated = n_alloc;
10296         }
10297
10298       ++abbrev_die_table_in_use;
10299       abbrev_die_table[abbrev_id] = die;
10300     }
10301
10302   die->die_abbrev = abbrev_id;
10303   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10304 }
10305 \f
10306 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10307
10308 static int
10309 constant_size (unsigned HOST_WIDE_INT value)
10310 {
10311   int log;
10312
10313   if (value == 0)
10314     log = 0;
10315   else
10316     log = floor_log2 (value);
10317
10318   log = log / 8;
10319   log = 1 << (floor_log2 (log) + 1);
10320
10321   return log;
10322 }
10323
10324 /* Return the size of a DIE as it is represented in the
10325    .debug_info section.  */
10326
10327 static unsigned long
10328 size_of_die (dw_die_ref die)
10329 {
10330   unsigned long size = 0;
10331   dw_attr_ref a;
10332   unsigned ix;
10333
10334   size += size_of_uleb128 (die->die_abbrev);
10335   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10336     {
10337       switch (AT_class (a))
10338         {
10339         case dw_val_class_addr:
10340           size += DWARF2_ADDR_SIZE;
10341           break;
10342         case dw_val_class_offset:
10343           size += DWARF_OFFSET_SIZE;
10344           break;
10345         case dw_val_class_loc:
10346           {
10347             unsigned long lsize = size_of_locs (AT_loc (a));
10348
10349             /* Block length.  */
10350             if (dwarf_version >= 4)
10351               size += size_of_uleb128 (lsize);
10352             else
10353               size += constant_size (lsize);
10354             size += lsize;
10355           }
10356           break;
10357         case dw_val_class_loc_list:
10358           size += DWARF_OFFSET_SIZE;
10359           break;
10360         case dw_val_class_range_list:
10361           size += DWARF_OFFSET_SIZE;
10362           break;
10363         case dw_val_class_const:
10364           size += size_of_sleb128 (AT_int (a));
10365           break;
10366         case dw_val_class_unsigned_const:
10367           size += constant_size (AT_unsigned (a));
10368           break;
10369         case dw_val_class_const_double:
10370           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10371           if (HOST_BITS_PER_WIDE_INT >= 64)
10372             size++; /* block */
10373           break;
10374         case dw_val_class_vec:
10375           size += constant_size (a->dw_attr_val.v.val_vec.length
10376                                  * a->dw_attr_val.v.val_vec.elt_size)
10377                   + a->dw_attr_val.v.val_vec.length
10378                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10379           break;
10380         case dw_val_class_flag:
10381           if (dwarf_version >= 4)
10382             /* Currently all add_AT_flag calls pass in 1 as last argument,
10383                so DW_FORM_flag_present can be used.  If that ever changes,
10384                we'll need to use DW_FORM_flag and have some optimization
10385                in build_abbrev_table that will change those to
10386                DW_FORM_flag_present if it is set to 1 in all DIEs using
10387                the same abbrev entry.  */
10388             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10389           else
10390             size += 1;
10391           break;
10392         case dw_val_class_die_ref:
10393           if (AT_ref_external (a))
10394             {
10395               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10396                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10397                  is sized by target address length, whereas in DWARF3
10398                  it's always sized as an offset.  */
10399               if (dwarf_version >= 4)
10400                 size += DWARF_TYPE_SIGNATURE_SIZE;
10401               else if (dwarf_version == 2)
10402                 size += DWARF2_ADDR_SIZE;
10403               else
10404                 size += DWARF_OFFSET_SIZE;
10405             }
10406           else
10407             size += DWARF_OFFSET_SIZE;
10408           break;
10409         case dw_val_class_fde_ref:
10410           size += DWARF_OFFSET_SIZE;
10411           break;
10412         case dw_val_class_lbl_id:
10413           size += DWARF2_ADDR_SIZE;
10414           break;
10415         case dw_val_class_lineptr:
10416         case dw_val_class_macptr:
10417           size += DWARF_OFFSET_SIZE;
10418           break;
10419         case dw_val_class_str:
10420           if (AT_string_form (a) == DW_FORM_strp)
10421             size += DWARF_OFFSET_SIZE;
10422           else
10423             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10424           break;
10425         case dw_val_class_file:
10426           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10427           break;
10428         case dw_val_class_data8:
10429           size += 8;
10430           break;
10431         case dw_val_class_vms_delta:
10432           size += DWARF_OFFSET_SIZE;
10433           break;
10434         default:
10435           gcc_unreachable ();
10436         }
10437     }
10438
10439   return size;
10440 }
10441
10442 /* Size the debugging information associated with a given DIE.  Visits the
10443    DIE's children recursively.  Updates the global variable next_die_offset, on
10444    each time through.  Uses the current value of next_die_offset to update the
10445    die_offset field in each DIE.  */
10446
10447 static void
10448 calc_die_sizes (dw_die_ref die)
10449 {
10450   dw_die_ref c;
10451
10452   die->die_offset = next_die_offset;
10453   next_die_offset += size_of_die (die);
10454
10455   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10456
10457   if (die->die_child != NULL)
10458     /* Count the null byte used to terminate sibling lists.  */
10459     next_die_offset += 1;
10460 }
10461
10462 /* Set the marks for a die and its children.  We do this so
10463    that we know whether or not a reference needs to use FORM_ref_addr; only
10464    DIEs in the same CU will be marked.  We used to clear out the offset
10465    and use that as the flag, but ran into ordering problems.  */
10466
10467 static void
10468 mark_dies (dw_die_ref die)
10469 {
10470   dw_die_ref c;
10471
10472   gcc_assert (!die->die_mark);
10473
10474   die->die_mark = 1;
10475   FOR_EACH_CHILD (die, c, mark_dies (c));
10476 }
10477
10478 /* Clear the marks for a die and its children.  */
10479
10480 static void
10481 unmark_dies (dw_die_ref die)
10482 {
10483   dw_die_ref c;
10484
10485   if (dwarf_version < 4)
10486     gcc_assert (die->die_mark);
10487
10488   die->die_mark = 0;
10489   FOR_EACH_CHILD (die, c, unmark_dies (c));
10490 }
10491
10492 /* Clear the marks for a die, its children and referred dies.  */
10493
10494 static void
10495 unmark_all_dies (dw_die_ref die)
10496 {
10497   dw_die_ref c;
10498   dw_attr_ref a;
10499   unsigned ix;
10500
10501   if (!die->die_mark)
10502     return;
10503   die->die_mark = 0;
10504
10505   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10506
10507   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10508     if (AT_class (a) == dw_val_class_die_ref)
10509       unmark_all_dies (AT_ref (a));
10510 }
10511
10512 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10513    generated for the compilation unit.  */
10514
10515 static unsigned long
10516 size_of_pubnames (VEC (pubname_entry, gc) * names)
10517 {
10518   unsigned long size;
10519   unsigned i;
10520   pubname_ref p;
10521
10522   size = DWARF_PUBNAMES_HEADER_SIZE;
10523   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10524     if (names != pubtype_table
10525         || p->die->die_offset != 0
10526         || !flag_eliminate_unused_debug_types)
10527       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10528
10529   size += DWARF_OFFSET_SIZE;
10530   return size;
10531 }
10532
10533 /* Return the size of the information in the .debug_aranges section.  */
10534
10535 static unsigned long
10536 size_of_aranges (void)
10537 {
10538   unsigned long size;
10539
10540   size = DWARF_ARANGES_HEADER_SIZE;
10541
10542   /* Count the address/length pair for this compilation unit.  */
10543   if (text_section_used)
10544     size += 2 * DWARF2_ADDR_SIZE;
10545   if (cold_text_section_used)
10546     size += 2 * DWARF2_ADDR_SIZE;
10547   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10548
10549   /* Count the two zero words used to terminated the address range table.  */
10550   size += 2 * DWARF2_ADDR_SIZE;
10551   return size;
10552 }
10553 \f
10554 /* Select the encoding of an attribute value.  */
10555
10556 static enum dwarf_form
10557 value_format (dw_attr_ref a)
10558 {
10559   switch (a->dw_attr_val.val_class)
10560     {
10561     case dw_val_class_addr:
10562       /* Only very few attributes allow DW_FORM_addr.  */
10563       switch (a->dw_attr)
10564         {
10565         case DW_AT_low_pc:
10566         case DW_AT_high_pc:
10567         case DW_AT_entry_pc:
10568         case DW_AT_trampoline:
10569           return DW_FORM_addr;
10570         default:
10571           break;
10572         }
10573       switch (DWARF2_ADDR_SIZE)
10574         {
10575         case 1:
10576           return DW_FORM_data1;
10577         case 2:
10578           return DW_FORM_data2;
10579         case 4:
10580           return DW_FORM_data4;
10581         case 8:
10582           return DW_FORM_data8;
10583         default:
10584           gcc_unreachable ();
10585         }
10586     case dw_val_class_range_list:
10587     case dw_val_class_loc_list:
10588       if (dwarf_version >= 4)
10589         return DW_FORM_sec_offset;
10590       /* FALLTHRU */
10591     case dw_val_class_vms_delta:
10592     case dw_val_class_offset:
10593       switch (DWARF_OFFSET_SIZE)
10594         {
10595         case 4:
10596           return DW_FORM_data4;
10597         case 8:
10598           return DW_FORM_data8;
10599         default:
10600           gcc_unreachable ();
10601         }
10602     case dw_val_class_loc:
10603       if (dwarf_version >= 4)
10604         return DW_FORM_exprloc;
10605       switch (constant_size (size_of_locs (AT_loc (a))))
10606         {
10607         case 1:
10608           return DW_FORM_block1;
10609         case 2:
10610           return DW_FORM_block2;
10611         default:
10612           gcc_unreachable ();
10613         }
10614     case dw_val_class_const:
10615       return DW_FORM_sdata;
10616     case dw_val_class_unsigned_const:
10617       switch (constant_size (AT_unsigned (a)))
10618         {
10619         case 1:
10620           return DW_FORM_data1;
10621         case 2:
10622           return DW_FORM_data2;
10623         case 4:
10624           return DW_FORM_data4;
10625         case 8:
10626           return DW_FORM_data8;
10627         default:
10628           gcc_unreachable ();
10629         }
10630     case dw_val_class_const_double:
10631       switch (HOST_BITS_PER_WIDE_INT)
10632         {
10633         case 8:
10634           return DW_FORM_data2;
10635         case 16:
10636           return DW_FORM_data4;
10637         case 32:
10638           return DW_FORM_data8;
10639         case 64:
10640         default:
10641           return DW_FORM_block1;
10642         }
10643     case dw_val_class_vec:
10644       switch (constant_size (a->dw_attr_val.v.val_vec.length
10645                              * a->dw_attr_val.v.val_vec.elt_size))
10646         {
10647         case 1:
10648           return DW_FORM_block1;
10649         case 2:
10650           return DW_FORM_block2;
10651         case 4:
10652           return DW_FORM_block4;
10653         default:
10654           gcc_unreachable ();
10655         }
10656     case dw_val_class_flag:
10657       if (dwarf_version >= 4)
10658         {
10659           /* Currently all add_AT_flag calls pass in 1 as last argument,
10660              so DW_FORM_flag_present can be used.  If that ever changes,
10661              we'll need to use DW_FORM_flag and have some optimization
10662              in build_abbrev_table that will change those to
10663              DW_FORM_flag_present if it is set to 1 in all DIEs using
10664              the same abbrev entry.  */
10665           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10666           return DW_FORM_flag_present;
10667         }
10668       return DW_FORM_flag;
10669     case dw_val_class_die_ref:
10670       if (AT_ref_external (a))
10671         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10672       else
10673         return DW_FORM_ref;
10674     case dw_val_class_fde_ref:
10675       return DW_FORM_data;
10676     case dw_val_class_lbl_id:
10677       return DW_FORM_addr;
10678     case dw_val_class_lineptr:
10679     case dw_val_class_macptr:
10680       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10681     case dw_val_class_str:
10682       return AT_string_form (a);
10683     case dw_val_class_file:
10684       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10685         {
10686         case 1:
10687           return DW_FORM_data1;
10688         case 2:
10689           return DW_FORM_data2;
10690         case 4:
10691           return DW_FORM_data4;
10692         default:
10693           gcc_unreachable ();
10694         }
10695
10696     case dw_val_class_data8:
10697       return DW_FORM_data8;
10698
10699     default:
10700       gcc_unreachable ();
10701     }
10702 }
10703
10704 /* Output the encoding of an attribute value.  */
10705
10706 static void
10707 output_value_format (dw_attr_ref a)
10708 {
10709   enum dwarf_form form = value_format (a);
10710
10711   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10712 }
10713
10714 /* Output the .debug_abbrev section which defines the DIE abbreviation
10715    table.  */
10716
10717 static void
10718 output_abbrev_section (void)
10719 {
10720   unsigned long abbrev_id;
10721
10722   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10723     {
10724       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10725       unsigned ix;
10726       dw_attr_ref a_attr;
10727
10728       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10729       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10730                                    dwarf_tag_name (abbrev->die_tag));
10731
10732       if (abbrev->die_child != NULL)
10733         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10734       else
10735         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10736
10737       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10738            ix++)
10739         {
10740           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10741                                        dwarf_attr_name (a_attr->dw_attr));
10742           output_value_format (a_attr);
10743         }
10744
10745       dw2_asm_output_data (1, 0, NULL);
10746       dw2_asm_output_data (1, 0, NULL);
10747     }
10748
10749   /* Terminate the table.  */
10750   dw2_asm_output_data (1, 0, NULL);
10751 }
10752
10753 /* Output a symbol we can use to refer to this DIE from another CU.  */
10754
10755 static inline void
10756 output_die_symbol (dw_die_ref die)
10757 {
10758   char *sym = die->die_id.die_symbol;
10759
10760   if (sym == 0)
10761     return;
10762
10763   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10764     /* We make these global, not weak; if the target doesn't support
10765        .linkonce, it doesn't support combining the sections, so debugging
10766        will break.  */
10767     targetm.asm_out.globalize_label (asm_out_file, sym);
10768
10769   ASM_OUTPUT_LABEL (asm_out_file, sym);
10770 }
10771
10772 /* Return a new location list, given the begin and end range, and the
10773    expression.  */
10774
10775 static inline dw_loc_list_ref
10776 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10777               const char *section)
10778 {
10779   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10780
10781   retlist->begin = begin;
10782   retlist->end = end;
10783   retlist->expr = expr;
10784   retlist->section = section;
10785
10786   return retlist;
10787 }
10788
10789 /* Generate a new internal symbol for this location list node, if it
10790    hasn't got one yet.  */
10791
10792 static inline void
10793 gen_llsym (dw_loc_list_ref list)
10794 {
10795   gcc_assert (!list->ll_symbol);
10796   list->ll_symbol = gen_internal_sym ("LLST");
10797 }
10798
10799 /* Output the location list given to us.  */
10800
10801 static void
10802 output_loc_list (dw_loc_list_ref list_head)
10803 {
10804   dw_loc_list_ref curr = list_head;
10805
10806   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10807
10808   /* Walk the location list, and output each range + expression.  */
10809   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10810     {
10811       unsigned long size;
10812       /* Don't output an entry that starts and ends at the same address.  */
10813       if (strcmp (curr->begin, curr->end) == 0)
10814         continue;
10815       if (!have_multiple_function_sections)
10816         {
10817           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10818                                 "Location list begin address (%s)",
10819                                 list_head->ll_symbol);
10820           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10821                                 "Location list end address (%s)",
10822                                 list_head->ll_symbol);
10823         }
10824       else
10825         {
10826           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10827                                "Location list begin address (%s)",
10828                                list_head->ll_symbol);
10829           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10830                                "Location list end address (%s)",
10831                                list_head->ll_symbol);
10832         }
10833       size = size_of_locs (curr->expr);
10834
10835       /* Output the block length for this list of location operations.  */
10836       gcc_assert (size <= 0xffff);
10837       dw2_asm_output_data (2, size, "%s", "Location expression size");
10838
10839       output_loc_sequence (curr->expr);
10840     }
10841
10842   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10843                        "Location list terminator begin (%s)",
10844                        list_head->ll_symbol);
10845   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10846                        "Location list terminator end (%s)",
10847                        list_head->ll_symbol);
10848 }
10849
10850 /* Output a type signature.  */
10851
10852 static inline void
10853 output_signature (const char *sig, const char *name)
10854 {
10855   int i;
10856
10857   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10858     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10859 }
10860
10861 /* Output the DIE and its attributes.  Called recursively to generate
10862    the definitions of each child DIE.  */
10863
10864 static void
10865 output_die (dw_die_ref die)
10866 {
10867   dw_attr_ref a;
10868   dw_die_ref c;
10869   unsigned long size;
10870   unsigned ix;
10871
10872   /* If someone in another CU might refer to us, set up a symbol for
10873      them to point to.  */
10874   if (dwarf_version < 4 && die->die_id.die_symbol)
10875     output_die_symbol (die);
10876
10877   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10878                                (unsigned long)die->die_offset,
10879                                dwarf_tag_name (die->die_tag));
10880
10881   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10882     {
10883       const char *name = dwarf_attr_name (a->dw_attr);
10884
10885       switch (AT_class (a))
10886         {
10887         case dw_val_class_addr:
10888           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10889           break;
10890
10891         case dw_val_class_offset:
10892           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10893                                "%s", name);
10894           break;
10895
10896         case dw_val_class_range_list:
10897           {
10898             char *p = strchr (ranges_section_label, '\0');
10899
10900             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10901                      a->dw_attr_val.v.val_offset);
10902             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10903                                    debug_ranges_section, "%s", name);
10904             *p = '\0';
10905           }
10906           break;
10907
10908         case dw_val_class_loc:
10909           size = size_of_locs (AT_loc (a));
10910
10911           /* Output the block length for this list of location operations.  */
10912           if (dwarf_version >= 4)
10913             dw2_asm_output_data_uleb128 (size, "%s", name);
10914           else
10915             dw2_asm_output_data (constant_size (size), size, "%s", name);
10916
10917           output_loc_sequence (AT_loc (a));
10918           break;
10919
10920         case dw_val_class_const:
10921           /* ??? It would be slightly more efficient to use a scheme like is
10922              used for unsigned constants below, but gdb 4.x does not sign
10923              extend.  Gdb 5.x does sign extend.  */
10924           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10925           break;
10926
10927         case dw_val_class_unsigned_const:
10928           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10929                                AT_unsigned (a), "%s", name);
10930           break;
10931
10932         case dw_val_class_const_double:
10933           {
10934             unsigned HOST_WIDE_INT first, second;
10935
10936             if (HOST_BITS_PER_WIDE_INT >= 64)
10937               dw2_asm_output_data (1,
10938                                    2 * HOST_BITS_PER_WIDE_INT
10939                                    / HOST_BITS_PER_CHAR,
10940                                    NULL);
10941
10942             if (WORDS_BIG_ENDIAN)
10943               {
10944                 first = a->dw_attr_val.v.val_double.high;
10945                 second = a->dw_attr_val.v.val_double.low;
10946               }
10947             else
10948               {
10949                 first = a->dw_attr_val.v.val_double.low;
10950                 second = a->dw_attr_val.v.val_double.high;
10951               }
10952
10953             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10954                                  first, name);
10955             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10956                                  second, NULL);
10957           }
10958           break;
10959
10960         case dw_val_class_vec:
10961           {
10962             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10963             unsigned int len = a->dw_attr_val.v.val_vec.length;
10964             unsigned int i;
10965             unsigned char *p;
10966
10967             dw2_asm_output_data (constant_size (len * elt_size),
10968                                  len * elt_size, "%s", name);
10969             if (elt_size > sizeof (HOST_WIDE_INT))
10970               {
10971                 elt_size /= 2;
10972                 len *= 2;
10973               }
10974             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10975                  i < len;
10976                  i++, p += elt_size)
10977               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10978                                    "fp or vector constant word %u", i);
10979             break;
10980           }
10981
10982         case dw_val_class_flag:
10983           if (dwarf_version >= 4)
10984             {
10985               /* Currently all add_AT_flag calls pass in 1 as last argument,
10986                  so DW_FORM_flag_present can be used.  If that ever changes,
10987                  we'll need to use DW_FORM_flag and have some optimization
10988                  in build_abbrev_table that will change those to
10989                  DW_FORM_flag_present if it is set to 1 in all DIEs using
10990                  the same abbrev entry.  */
10991               gcc_assert (AT_flag (a) == 1);
10992               if (flag_debug_asm)
10993                 fprintf (asm_out_file, "\t\t\t%s %s\n",
10994                          ASM_COMMENT_START, name);
10995               break;
10996             }
10997           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10998           break;
10999
11000         case dw_val_class_loc_list:
11001           {
11002             char *sym = AT_loc_list (a)->ll_symbol;
11003
11004             gcc_assert (sym);
11005             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11006                                    "%s", name);
11007           }
11008           break;
11009
11010         case dw_val_class_die_ref:
11011           if (AT_ref_external (a))
11012             {
11013               if (dwarf_version >= 4)
11014                 {
11015                   comdat_type_node_ref type_node =
11016                     AT_ref (a)->die_id.die_type_node;
11017
11018                   gcc_assert (type_node);
11019                   output_signature (type_node->signature, name);
11020                 }
11021               else
11022                 {
11023                   char *sym = AT_ref (a)->die_id.die_symbol;
11024                   int size;
11025
11026                   gcc_assert (sym);
11027                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11028                      length, whereas in DWARF3 it's always sized as an
11029                      offset.  */
11030                   if (dwarf_version == 2)
11031                     size = DWARF2_ADDR_SIZE;
11032                   else
11033                     size = DWARF_OFFSET_SIZE;
11034                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11035                                          name);
11036                 }
11037             }
11038           else
11039             {
11040               gcc_assert (AT_ref (a)->die_offset);
11041               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11042                                    "%s", name);
11043             }
11044           break;
11045
11046         case dw_val_class_fde_ref:
11047           {
11048             char l1[20];
11049
11050             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11051                                          a->dw_attr_val.v.val_fde_index * 2);
11052             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11053                                    "%s", name);
11054           }
11055           break;
11056
11057         case dw_val_class_vms_delta:
11058           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11059                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11060                                     "%s", name);
11061           break;
11062
11063         case dw_val_class_lbl_id:
11064           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11065           break;
11066
11067         case dw_val_class_lineptr:
11068           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11069                                  debug_line_section, "%s", name);
11070           break;
11071
11072         case dw_val_class_macptr:
11073           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11074                                  debug_macinfo_section, "%s", name);
11075           break;
11076
11077         case dw_val_class_str:
11078           if (AT_string_form (a) == DW_FORM_strp)
11079             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11080                                    a->dw_attr_val.v.val_str->label,
11081                                    debug_str_section,
11082                                    "%s: \"%s\"", name, AT_string (a));
11083           else
11084             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11085           break;
11086
11087         case dw_val_class_file:
11088           {
11089             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11090
11091             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11092                                  a->dw_attr_val.v.val_file->filename);
11093             break;
11094           }
11095
11096         case dw_val_class_data8:
11097           {
11098             int i;
11099
11100             for (i = 0; i < 8; i++)
11101               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11102                                    i == 0 ? "%s" : NULL, name);
11103             break;
11104           }
11105
11106         default:
11107           gcc_unreachable ();
11108         }
11109     }
11110
11111   FOR_EACH_CHILD (die, c, output_die (c));
11112
11113   /* Add null byte to terminate sibling list.  */
11114   if (die->die_child != NULL)
11115     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11116                          (unsigned long) die->die_offset);
11117 }
11118
11119 /* Output the compilation unit that appears at the beginning of the
11120    .debug_info section, and precedes the DIE descriptions.  */
11121
11122 static void
11123 output_compilation_unit_header (void)
11124 {
11125   int ver = dwarf_version;
11126
11127   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11128     dw2_asm_output_data (4, 0xffffffff,
11129       "Initial length escape value indicating 64-bit DWARF extension");
11130   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11131                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11132                        "Length of Compilation Unit Info");
11133   dw2_asm_output_data (2, ver, "DWARF version number");
11134   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11135                          debug_abbrev_section,
11136                          "Offset Into Abbrev. Section");
11137   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11138 }
11139
11140 /* Output the compilation unit DIE and its children.  */
11141
11142 static void
11143 output_comp_unit (dw_die_ref die, int output_if_empty)
11144 {
11145   const char *secname;
11146   char *oldsym, *tmp;
11147
11148   /* Unless we are outputting main CU, we may throw away empty ones.  */
11149   if (!output_if_empty && die->die_child == NULL)
11150     return;
11151
11152   /* Even if there are no children of this DIE, we must output the information
11153      about the compilation unit.  Otherwise, on an empty translation unit, we
11154      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11155      will then complain when examining the file.  First mark all the DIEs in
11156      this CU so we know which get local refs.  */
11157   mark_dies (die);
11158
11159   build_abbrev_table (die);
11160
11161   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11162   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11163   calc_die_sizes (die);
11164
11165   oldsym = die->die_id.die_symbol;
11166   if (oldsym)
11167     {
11168       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11169
11170       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11171       secname = tmp;
11172       die->die_id.die_symbol = NULL;
11173       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11174     }
11175   else
11176     switch_to_section (debug_info_section);
11177
11178   /* Output debugging information.  */
11179   output_compilation_unit_header ();
11180   output_die (die);
11181
11182   /* Leave the marks on the main CU, so we can check them in
11183      output_pubnames.  */
11184   if (oldsym)
11185     {
11186       unmark_dies (die);
11187       die->die_id.die_symbol = oldsym;
11188     }
11189 }
11190
11191 /* Output a comdat type unit DIE and its children.  */
11192
11193 static void
11194 output_comdat_type_unit (comdat_type_node *node)
11195 {
11196   const char *secname;
11197   char *tmp;
11198   int i;
11199 #if defined (OBJECT_FORMAT_ELF)
11200   tree comdat_key;
11201 #endif
11202
11203   /* First mark all the DIEs in this CU so we know which get local refs.  */
11204   mark_dies (node->root_die);
11205
11206   build_abbrev_table (node->root_die);
11207
11208   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11209   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11210   calc_die_sizes (node->root_die);
11211
11212 #if defined (OBJECT_FORMAT_ELF)
11213   secname = ".debug_types";
11214   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11215   sprintf (tmp, "wt.");
11216   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11217     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11218   comdat_key = get_identifier (tmp);
11219   targetm.asm_out.named_section (secname,
11220                                  SECTION_DEBUG | SECTION_LINKONCE,
11221                                  comdat_key);
11222 #else
11223   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11224   sprintf (tmp, ".gnu.linkonce.wt.");
11225   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11226     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11227   secname = tmp;
11228   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11229 #endif
11230
11231   /* Output debugging information.  */
11232   output_compilation_unit_header ();
11233   output_signature (node->signature, "Type Signature");
11234   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11235                        "Offset to Type DIE");
11236   output_die (node->root_die);
11237
11238   unmark_dies (node->root_die);
11239 }
11240
11241 /* Return the DWARF2/3 pubname associated with a decl.  */
11242
11243 static const char *
11244 dwarf2_name (tree decl, int scope)
11245 {
11246   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11247 }
11248
11249 /* Add a new entry to .debug_pubnames if appropriate.  */
11250
11251 static void
11252 add_pubname_string (const char *str, dw_die_ref die)
11253 {
11254   if (targetm.want_debug_pub_sections)
11255     {
11256       pubname_entry e;
11257
11258       e.die = die;
11259       e.name = xstrdup (str);
11260       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11261     }
11262 }
11263
11264 static void
11265 add_pubname (tree decl, dw_die_ref die)
11266 {
11267   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11268     {
11269       const char *name = dwarf2_name (decl, 1);
11270       if (name)
11271         add_pubname_string (name, die);
11272     }
11273 }
11274
11275 /* Add a new entry to .debug_pubtypes if appropriate.  */
11276
11277 static void
11278 add_pubtype (tree decl, dw_die_ref die)
11279 {
11280   pubname_entry e;
11281
11282   if (!targetm.want_debug_pub_sections)
11283     return;
11284
11285   e.name = NULL;
11286   if ((TREE_PUBLIC (decl)
11287        || die->die_parent == comp_unit_die)
11288       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11289     {
11290       e.die = die;
11291       if (TYPE_P (decl))
11292         {
11293           if (TYPE_NAME (decl))
11294             {
11295               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11296                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11297               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11298                        && DECL_NAME (TYPE_NAME (decl)))
11299                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11300               else
11301                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11302             }
11303         }
11304       else
11305         {
11306           e.name = dwarf2_name (decl, 1);
11307           if (e.name)
11308             e.name = xstrdup (e.name);
11309         }
11310
11311       /* If we don't have a name for the type, there's no point in adding
11312          it to the table.  */
11313       if (e.name && e.name[0] != '\0')
11314         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11315     }
11316 }
11317
11318 /* Output the public names table used to speed up access to externally
11319    visible names; or the public types table used to find type definitions.  */
11320
11321 static void
11322 output_pubnames (VEC (pubname_entry, gc) * names)
11323 {
11324   unsigned i;
11325   unsigned long pubnames_length = size_of_pubnames (names);
11326   pubname_ref pub;
11327
11328   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11329     dw2_asm_output_data (4, 0xffffffff,
11330       "Initial length escape value indicating 64-bit DWARF extension");
11331   if (names == pubname_table)
11332     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11333                          "Length of Public Names Info");
11334   else
11335     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11336                          "Length of Public Type Names Info");
11337   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11338   dw2_asm_output_data (2, 2, "DWARF Version");
11339   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11340                          debug_info_section,
11341                          "Offset of Compilation Unit Info");
11342   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11343                        "Compilation Unit Length");
11344
11345   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11346     {
11347       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11348       if (names == pubname_table)
11349         gcc_assert (pub->die->die_mark);
11350
11351       if (names != pubtype_table
11352           || pub->die->die_offset != 0
11353           || !flag_eliminate_unused_debug_types)
11354         {
11355           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11356                                "DIE offset");
11357
11358           dw2_asm_output_nstring (pub->name, -1, "external name");
11359         }
11360     }
11361
11362   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11363 }
11364
11365 /* Add a new entry to .debug_aranges if appropriate.  */
11366
11367 static void
11368 add_arange (tree decl, dw_die_ref die)
11369 {
11370   if (! DECL_SECTION_NAME (decl))
11371     return;
11372
11373   if (arange_table_in_use == arange_table_allocated)
11374     {
11375       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11376       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11377                                     arange_table_allocated);
11378       memset (arange_table + arange_table_in_use, 0,
11379               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11380     }
11381
11382   arange_table[arange_table_in_use++] = die;
11383 }
11384
11385 /* Output the information that goes into the .debug_aranges table.
11386    Namely, define the beginning and ending address range of the
11387    text section generated for this compilation unit.  */
11388
11389 static void
11390 output_aranges (void)
11391 {
11392   unsigned i;
11393   unsigned long aranges_length = size_of_aranges ();
11394
11395   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11396     dw2_asm_output_data (4, 0xffffffff,
11397       "Initial length escape value indicating 64-bit DWARF extension");
11398   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11399                        "Length of Address Ranges Info");
11400   /* Version number for aranges is still 2, even in DWARF3.  */
11401   dw2_asm_output_data (2, 2, "DWARF Version");
11402   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11403                          debug_info_section,
11404                          "Offset of Compilation Unit Info");
11405   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11406   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11407
11408   /* We need to align to twice the pointer size here.  */
11409   if (DWARF_ARANGES_PAD_SIZE)
11410     {
11411       /* Pad using a 2 byte words so that padding is correct for any
11412          pointer size.  */
11413       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11414                            2 * DWARF2_ADDR_SIZE);
11415       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11416         dw2_asm_output_data (2, 0, NULL);
11417     }
11418
11419   /* It is necessary not to output these entries if the sections were
11420      not used; if the sections were not used, the length will be 0 and
11421      the address may end up as 0 if the section is discarded by ld
11422      --gc-sections, leaving an invalid (0, 0) entry that can be
11423      confused with the terminator.  */
11424   if (text_section_used)
11425     {
11426       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11427       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11428                             text_section_label, "Length");
11429     }
11430   if (cold_text_section_used)
11431     {
11432       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11433                            "Address");
11434       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11435                             cold_text_section_label, "Length");
11436     }
11437
11438   for (i = 0; i < arange_table_in_use; i++)
11439     {
11440       dw_die_ref die = arange_table[i];
11441
11442       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11443       gcc_assert (die->die_mark);
11444
11445       if (die->die_tag == DW_TAG_subprogram)
11446         {
11447           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11448                                "Address");
11449           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11450                                 get_AT_low_pc (die), "Length");
11451         }
11452       else
11453         {
11454           /* A static variable; extract the symbol from DW_AT_location.
11455              Note that this code isn't currently hit, as we only emit
11456              aranges for functions (jason 9/23/99).  */
11457           dw_attr_ref a = get_AT (die, DW_AT_location);
11458           dw_loc_descr_ref loc;
11459
11460           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11461
11462           loc = AT_loc (a);
11463           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11464
11465           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11466                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11467           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11468                                get_AT_unsigned (die, DW_AT_byte_size),
11469                                "Length");
11470         }
11471     }
11472
11473   /* Output the terminator words.  */
11474   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11475   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11476 }
11477
11478 /* Add a new entry to .debug_ranges.  Return the offset at which it
11479    was placed.  */
11480
11481 static unsigned int
11482 add_ranges_num (int num)
11483 {
11484   unsigned int in_use = ranges_table_in_use;
11485
11486   if (in_use == ranges_table_allocated)
11487     {
11488       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11489       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11490                                     ranges_table_allocated);
11491       memset (ranges_table + ranges_table_in_use, 0,
11492               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11493     }
11494
11495   ranges_table[in_use].num = num;
11496   ranges_table_in_use = in_use + 1;
11497
11498   return in_use * 2 * DWARF2_ADDR_SIZE;
11499 }
11500
11501 /* Add a new entry to .debug_ranges corresponding to a block, or a
11502    range terminator if BLOCK is NULL.  */
11503
11504 static unsigned int
11505 add_ranges (const_tree block)
11506 {
11507   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11508 }
11509
11510 /* Add a new entry to .debug_ranges corresponding to a pair of
11511    labels.  */
11512
11513 static void
11514 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11515                       bool *added)
11516 {
11517   unsigned int in_use = ranges_by_label_in_use;
11518   unsigned int offset;
11519
11520   if (in_use == ranges_by_label_allocated)
11521     {
11522       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11523       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11524                                        ranges_by_label,
11525                                        ranges_by_label_allocated);
11526       memset (ranges_by_label + ranges_by_label_in_use, 0,
11527               RANGES_TABLE_INCREMENT
11528               * sizeof (struct dw_ranges_by_label_struct));
11529     }
11530
11531   ranges_by_label[in_use].begin = begin;
11532   ranges_by_label[in_use].end = end;
11533   ranges_by_label_in_use = in_use + 1;
11534
11535   offset = add_ranges_num (-(int)in_use - 1);
11536   if (!*added)
11537     {
11538       add_AT_range_list (die, DW_AT_ranges, offset);
11539       *added = true;
11540     }
11541 }
11542
11543 static void
11544 output_ranges (void)
11545 {
11546   unsigned i;
11547   static const char *const start_fmt = "Offset %#x";
11548   const char *fmt = start_fmt;
11549
11550   for (i = 0; i < ranges_table_in_use; i++)
11551     {
11552       int block_num = ranges_table[i].num;
11553
11554       if (block_num > 0)
11555         {
11556           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11557           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11558
11559           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11560           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11561
11562           /* If all code is in the text section, then the compilation
11563              unit base address defaults to DW_AT_low_pc, which is the
11564              base of the text section.  */
11565           if (!have_multiple_function_sections)
11566             {
11567               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11568                                     text_section_label,
11569                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11570               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11571                                     text_section_label, NULL);
11572             }
11573
11574           /* Otherwise, the compilation unit base address is zero,
11575              which allows us to use absolute addresses, and not worry
11576              about whether the target supports cross-section
11577              arithmetic.  */
11578           else
11579             {
11580               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11581                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11582               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11583             }
11584
11585           fmt = NULL;
11586         }
11587
11588       /* Negative block_num stands for an index into ranges_by_label.  */
11589       else if (block_num < 0)
11590         {
11591           int lab_idx = - block_num - 1;
11592
11593           if (!have_multiple_function_sections)
11594             {
11595               gcc_unreachable ();
11596 #if 0
11597               /* If we ever use add_ranges_by_labels () for a single
11598                  function section, all we have to do is to take out
11599                  the #if 0 above.  */
11600               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11601                                     ranges_by_label[lab_idx].begin,
11602                                     text_section_label,
11603                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11604               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11605                                     ranges_by_label[lab_idx].end,
11606                                     text_section_label, NULL);
11607 #endif
11608             }
11609           else
11610             {
11611               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11612                                    ranges_by_label[lab_idx].begin,
11613                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11614               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11615                                    ranges_by_label[lab_idx].end,
11616                                    NULL);
11617             }
11618         }
11619       else
11620         {
11621           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11622           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11623           fmt = start_fmt;
11624         }
11625     }
11626 }
11627
11628 /* Data structure containing information about input files.  */
11629 struct file_info
11630 {
11631   const char *path;     /* Complete file name.  */
11632   const char *fname;    /* File name part.  */
11633   int length;           /* Length of entire string.  */
11634   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11635   int dir_idx;          /* Index in directory table.  */
11636 };
11637
11638 /* Data structure containing information about directories with source
11639    files.  */
11640 struct dir_info
11641 {
11642   const char *path;     /* Path including directory name.  */
11643   int length;           /* Path length.  */
11644   int prefix;           /* Index of directory entry which is a prefix.  */
11645   int count;            /* Number of files in this directory.  */
11646   int dir_idx;          /* Index of directory used as base.  */
11647 };
11648
11649 /* Callback function for file_info comparison.  We sort by looking at
11650    the directories in the path.  */
11651
11652 static int
11653 file_info_cmp (const void *p1, const void *p2)
11654 {
11655   const struct file_info *const s1 = (const struct file_info *) p1;
11656   const struct file_info *const s2 = (const struct file_info *) p2;
11657   const unsigned char *cp1;
11658   const unsigned char *cp2;
11659
11660   /* Take care of file names without directories.  We need to make sure that
11661      we return consistent values to qsort since some will get confused if
11662      we return the same value when identical operands are passed in opposite
11663      orders.  So if neither has a directory, return 0 and otherwise return
11664      1 or -1 depending on which one has the directory.  */
11665   if ((s1->path == s1->fname || s2->path == s2->fname))
11666     return (s2->path == s2->fname) - (s1->path == s1->fname);
11667
11668   cp1 = (const unsigned char *) s1->path;
11669   cp2 = (const unsigned char *) s2->path;
11670
11671   while (1)
11672     {
11673       ++cp1;
11674       ++cp2;
11675       /* Reached the end of the first path?  If so, handle like above.  */
11676       if ((cp1 == (const unsigned char *) s1->fname)
11677           || (cp2 == (const unsigned char *) s2->fname))
11678         return ((cp2 == (const unsigned char *) s2->fname)
11679                 - (cp1 == (const unsigned char *) s1->fname));
11680
11681       /* Character of current path component the same?  */
11682       else if (*cp1 != *cp2)
11683         return *cp1 - *cp2;
11684     }
11685 }
11686
11687 struct file_name_acquire_data
11688 {
11689   struct file_info *files;
11690   int used_files;
11691   int max_files;
11692 };
11693
11694 /* Traversal function for the hash table.  */
11695
11696 static int
11697 file_name_acquire (void ** slot, void *data)
11698 {
11699   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11700   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11701   struct file_info *fi;
11702   const char *f;
11703
11704   gcc_assert (fnad->max_files >= d->emitted_number);
11705
11706   if (! d->emitted_number)
11707     return 1;
11708
11709   gcc_assert (fnad->max_files != fnad->used_files);
11710
11711   fi = fnad->files + fnad->used_files++;
11712
11713   /* Skip all leading "./".  */
11714   f = d->filename;
11715   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11716     f += 2;
11717
11718   /* Create a new array entry.  */
11719   fi->path = f;
11720   fi->length = strlen (f);
11721   fi->file_idx = d;
11722
11723   /* Search for the file name part.  */
11724   f = strrchr (f, DIR_SEPARATOR);
11725 #if defined (DIR_SEPARATOR_2)
11726   {
11727     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11728
11729     if (g != NULL)
11730       {
11731         if (f == NULL || f < g)
11732           f = g;
11733       }
11734   }
11735 #endif
11736
11737   fi->fname = f == NULL ? fi->path : f + 1;
11738   return 1;
11739 }
11740
11741 /* Output the directory table and the file name table.  We try to minimize
11742    the total amount of memory needed.  A heuristic is used to avoid large
11743    slowdowns with many input files.  */
11744
11745 static void
11746 output_file_names (void)
11747 {
11748   struct file_name_acquire_data fnad;
11749   int numfiles;
11750   struct file_info *files;
11751   struct dir_info *dirs;
11752   int *saved;
11753   int *savehere;
11754   int *backmap;
11755   int ndirs;
11756   int idx_offset;
11757   int i;
11758
11759   if (!last_emitted_file)
11760     {
11761       dw2_asm_output_data (1, 0, "End directory table");
11762       dw2_asm_output_data (1, 0, "End file name table");
11763       return;
11764     }
11765
11766   numfiles = last_emitted_file->emitted_number;
11767
11768   /* Allocate the various arrays we need.  */
11769   files = XALLOCAVEC (struct file_info, numfiles);
11770   dirs = XALLOCAVEC (struct dir_info, numfiles);
11771
11772   fnad.files = files;
11773   fnad.used_files = 0;
11774   fnad.max_files = numfiles;
11775   htab_traverse (file_table, file_name_acquire, &fnad);
11776   gcc_assert (fnad.used_files == fnad.max_files);
11777
11778   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11779
11780   /* Find all the different directories used.  */
11781   dirs[0].path = files[0].path;
11782   dirs[0].length = files[0].fname - files[0].path;
11783   dirs[0].prefix = -1;
11784   dirs[0].count = 1;
11785   dirs[0].dir_idx = 0;
11786   files[0].dir_idx = 0;
11787   ndirs = 1;
11788
11789   for (i = 1; i < numfiles; i++)
11790     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11791         && memcmp (dirs[ndirs - 1].path, files[i].path,
11792                    dirs[ndirs - 1].length) == 0)
11793       {
11794         /* Same directory as last entry.  */
11795         files[i].dir_idx = ndirs - 1;
11796         ++dirs[ndirs - 1].count;
11797       }
11798     else
11799       {
11800         int j;
11801
11802         /* This is a new directory.  */
11803         dirs[ndirs].path = files[i].path;
11804         dirs[ndirs].length = files[i].fname - files[i].path;
11805         dirs[ndirs].count = 1;
11806         dirs[ndirs].dir_idx = ndirs;
11807         files[i].dir_idx = ndirs;
11808
11809         /* Search for a prefix.  */
11810         dirs[ndirs].prefix = -1;
11811         for (j = 0; j < ndirs; j++)
11812           if (dirs[j].length < dirs[ndirs].length
11813               && dirs[j].length > 1
11814               && (dirs[ndirs].prefix == -1
11815                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11816               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11817             dirs[ndirs].prefix = j;
11818
11819         ++ndirs;
11820       }
11821
11822   /* Now to the actual work.  We have to find a subset of the directories which
11823      allow expressing the file name using references to the directory table
11824      with the least amount of characters.  We do not do an exhaustive search
11825      where we would have to check out every combination of every single
11826      possible prefix.  Instead we use a heuristic which provides nearly optimal
11827      results in most cases and never is much off.  */
11828   saved = XALLOCAVEC (int, ndirs);
11829   savehere = XALLOCAVEC (int, ndirs);
11830
11831   memset (saved, '\0', ndirs * sizeof (saved[0]));
11832   for (i = 0; i < ndirs; i++)
11833     {
11834       int j;
11835       int total;
11836
11837       /* We can always save some space for the current directory.  But this
11838          does not mean it will be enough to justify adding the directory.  */
11839       savehere[i] = dirs[i].length;
11840       total = (savehere[i] - saved[i]) * dirs[i].count;
11841
11842       for (j = i + 1; j < ndirs; j++)
11843         {
11844           savehere[j] = 0;
11845           if (saved[j] < dirs[i].length)
11846             {
11847               /* Determine whether the dirs[i] path is a prefix of the
11848                  dirs[j] path.  */
11849               int k;
11850
11851               k = dirs[j].prefix;
11852               while (k != -1 && k != (int) i)
11853                 k = dirs[k].prefix;
11854
11855               if (k == (int) i)
11856                 {
11857                   /* Yes it is.  We can possibly save some memory by
11858                      writing the filenames in dirs[j] relative to
11859                      dirs[i].  */
11860                   savehere[j] = dirs[i].length;
11861                   total += (savehere[j] - saved[j]) * dirs[j].count;
11862                 }
11863             }
11864         }
11865
11866       /* Check whether we can save enough to justify adding the dirs[i]
11867          directory.  */
11868       if (total > dirs[i].length + 1)
11869         {
11870           /* It's worthwhile adding.  */
11871           for (j = i; j < ndirs; j++)
11872             if (savehere[j] > 0)
11873               {
11874                 /* Remember how much we saved for this directory so far.  */
11875                 saved[j] = savehere[j];
11876
11877                 /* Remember the prefix directory.  */
11878                 dirs[j].dir_idx = i;
11879               }
11880         }
11881     }
11882
11883   /* Emit the directory name table.  */
11884   idx_offset = dirs[0].length > 0 ? 1 : 0;
11885   for (i = 1 - idx_offset; i < ndirs; i++)
11886     dw2_asm_output_nstring (dirs[i].path,
11887                             dirs[i].length
11888                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11889                             "Directory Entry: %#x", i + idx_offset);
11890
11891   dw2_asm_output_data (1, 0, "End directory table");
11892
11893   /* We have to emit them in the order of emitted_number since that's
11894      used in the debug info generation.  To do this efficiently we
11895      generate a back-mapping of the indices first.  */
11896   backmap = XALLOCAVEC (int, numfiles);
11897   for (i = 0; i < numfiles; i++)
11898     backmap[files[i].file_idx->emitted_number - 1] = i;
11899
11900   /* Now write all the file names.  */
11901   for (i = 0; i < numfiles; i++)
11902     {
11903       int file_idx = backmap[i];
11904       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11905
11906 #ifdef VMS_DEBUGGING_INFO
11907 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11908
11909       /* Setting these fields can lead to debugger miscomparisons,
11910          but VMS Debug requires them to be set correctly.  */
11911
11912       int ver;
11913       long long cdt;
11914       long siz;
11915       int maxfilelen = strlen (files[file_idx].path)
11916                                + dirs[dir_idx].length
11917                                + MAX_VMS_VERSION_LEN + 1;
11918       char *filebuf = XALLOCAVEC (char, maxfilelen);
11919
11920       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11921       snprintf (filebuf, maxfilelen, "%s;%d",
11922                 files[file_idx].path + dirs[dir_idx].length, ver);
11923
11924       dw2_asm_output_nstring
11925         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11926
11927       /* Include directory index.  */
11928       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11929
11930       /* Modification time.  */
11931       dw2_asm_output_data_uleb128
11932         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11933           ? cdt : 0,
11934          NULL);
11935
11936       /* File length in bytes.  */
11937       dw2_asm_output_data_uleb128
11938         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11939           ? siz : 0,
11940          NULL);
11941 #else
11942       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11943                               "File Entry: %#x", (unsigned) i + 1);
11944
11945       /* Include directory index.  */
11946       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11947
11948       /* Modification time.  */
11949       dw2_asm_output_data_uleb128 (0, NULL);
11950
11951       /* File length in bytes.  */
11952       dw2_asm_output_data_uleb128 (0, NULL);
11953 #endif
11954     }
11955
11956   dw2_asm_output_data (1, 0, "End file name table");
11957 }
11958
11959
11960 /* Output the source line number correspondence information.  This
11961    information goes into the .debug_line section.  */
11962
11963 static void
11964 output_line_info (void)
11965 {
11966   char l1[20], l2[20], p1[20], p2[20];
11967   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11968   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11969   unsigned opc;
11970   unsigned n_op_args;
11971   unsigned long lt_index;
11972   unsigned long current_line;
11973   long line_offset;
11974   long line_delta;
11975   unsigned long current_file;
11976   unsigned long function;
11977   int ver = dwarf_version;
11978
11979   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11980   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11981   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11982   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11983
11984   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11985     dw2_asm_output_data (4, 0xffffffff,
11986       "Initial length escape value indicating 64-bit DWARF extension");
11987   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11988                         "Length of Source Line Info");
11989   ASM_OUTPUT_LABEL (asm_out_file, l1);
11990
11991   dw2_asm_output_data (2, ver, "DWARF Version");
11992   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11993   ASM_OUTPUT_LABEL (asm_out_file, p1);
11994
11995   /* Define the architecture-dependent minimum instruction length (in
11996    bytes).  In this implementation of DWARF, this field is used for
11997    information purposes only.  Since GCC generates assembly language,
11998    we have no a priori knowledge of how many instruction bytes are
11999    generated for each source line, and therefore can use only the
12000    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12001    commands.  Accordingly, we fix this as `1', which is "correct
12002    enough" for all architectures, and don't let the target override.  */
12003   dw2_asm_output_data (1, 1,
12004                        "Minimum Instruction Length");
12005
12006   if (ver >= 4)
12007     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12008                          "Maximum Operations Per Instruction");
12009   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12010                        "Default is_stmt_start flag");
12011   dw2_asm_output_data (1, DWARF_LINE_BASE,
12012                        "Line Base Value (Special Opcodes)");
12013   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12014                        "Line Range Value (Special Opcodes)");
12015   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12016                        "Special Opcode Base");
12017
12018   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12019     {
12020       switch (opc)
12021         {
12022         case DW_LNS_advance_pc:
12023         case DW_LNS_advance_line:
12024         case DW_LNS_set_file:
12025         case DW_LNS_set_column:
12026         case DW_LNS_fixed_advance_pc:
12027           n_op_args = 1;
12028           break;
12029         default:
12030           n_op_args = 0;
12031           break;
12032         }
12033
12034       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12035                            opc, n_op_args);
12036     }
12037
12038   /* Write out the information about the files we use.  */
12039   output_file_names ();
12040   ASM_OUTPUT_LABEL (asm_out_file, p2);
12041
12042   /* We used to set the address register to the first location in the text
12043      section here, but that didn't accomplish anything since we already
12044      have a line note for the opening brace of the first function.  */
12045
12046   /* Generate the line number to PC correspondence table, encoded as
12047      a series of state machine operations.  */
12048   current_file = 1;
12049   current_line = 1;
12050
12051   if (cfun && in_cold_section_p)
12052     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12053   else
12054     strcpy (prev_line_label, text_section_label);
12055   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12056     {
12057       dw_line_info_ref line_info = &line_info_table[lt_index];
12058
12059 #if 0
12060       /* Disable this optimization for now; GDB wants to see two line notes
12061          at the beginning of a function so it can find the end of the
12062          prologue.  */
12063
12064       /* Don't emit anything for redundant notes.  Just updating the
12065          address doesn't accomplish anything, because we already assume
12066          that anything after the last address is this line.  */
12067       if (line_info->dw_line_num == current_line
12068           && line_info->dw_file_num == current_file)
12069         continue;
12070 #endif
12071
12072       /* Emit debug info for the address of the current line.
12073
12074          Unfortunately, we have little choice here currently, and must always
12075          use the most general form.  GCC does not know the address delta
12076          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12077          attributes which will give an upper bound on the address range.  We
12078          could perhaps use length attributes to determine when it is safe to
12079          use DW_LNS_fixed_advance_pc.  */
12080
12081       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12082       if (0)
12083         {
12084           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12085           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12086                                "DW_LNS_fixed_advance_pc");
12087           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12088         }
12089       else
12090         {
12091           /* This can handle any delta.  This takes
12092              4+DWARF2_ADDR_SIZE bytes.  */
12093           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12094           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12095           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12096           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12097         }
12098
12099       strcpy (prev_line_label, line_label);
12100
12101       /* Emit debug info for the source file of the current line, if
12102          different from the previous line.  */
12103       if (line_info->dw_file_num != current_file)
12104         {
12105           current_file = line_info->dw_file_num;
12106           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12107           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12108         }
12109
12110       /* Emit debug info for the current line number, choosing the encoding
12111          that uses the least amount of space.  */
12112       if (line_info->dw_line_num != current_line)
12113         {
12114           line_offset = line_info->dw_line_num - current_line;
12115           line_delta = line_offset - DWARF_LINE_BASE;
12116           current_line = line_info->dw_line_num;
12117           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12118             /* This can handle deltas from -10 to 234, using the current
12119                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12120                takes 1 byte.  */
12121             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12122                                  "line %lu", current_line);
12123           else
12124             {
12125               /* This can handle any delta.  This takes at least 4 bytes,
12126                  depending on the value being encoded.  */
12127               dw2_asm_output_data (1, DW_LNS_advance_line,
12128                                    "advance to line %lu", current_line);
12129               dw2_asm_output_data_sleb128 (line_offset, NULL);
12130               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12131             }
12132         }
12133       else
12134         /* We still need to start a new row, so output a copy insn.  */
12135         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12136     }
12137
12138   /* Emit debug info for the address of the end of the function.  */
12139   if (0)
12140     {
12141       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12142                            "DW_LNS_fixed_advance_pc");
12143       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12144     }
12145   else
12146     {
12147       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12148       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12149       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12150       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12151     }
12152
12153   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12154   dw2_asm_output_data_uleb128 (1, NULL);
12155   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12156
12157   function = 0;
12158   current_file = 1;
12159   current_line = 1;
12160   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12161     {
12162       dw_separate_line_info_ref line_info
12163         = &separate_line_info_table[lt_index];
12164
12165 #if 0
12166       /* Don't emit anything for redundant notes.  */
12167       if (line_info->dw_line_num == current_line
12168           && line_info->dw_file_num == current_file
12169           && line_info->function == function)
12170         goto cont;
12171 #endif
12172
12173       /* Emit debug info for the address of the current line.  If this is
12174          a new function, or the first line of a function, then we need
12175          to handle it differently.  */
12176       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12177                                    lt_index);
12178       if (function != line_info->function)
12179         {
12180           function = line_info->function;
12181
12182           /* Set the address register to the first line in the function.  */
12183           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12184           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12185           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12186           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12187         }
12188       else
12189         {
12190           /* ??? See the DW_LNS_advance_pc comment above.  */
12191           if (0)
12192             {
12193               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12194                                    "DW_LNS_fixed_advance_pc");
12195               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12196             }
12197           else
12198             {
12199               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12200               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12201               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12202               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12203             }
12204         }
12205
12206       strcpy (prev_line_label, line_label);
12207
12208       /* Emit debug info for the source file of the current line, if
12209          different from the previous line.  */
12210       if (line_info->dw_file_num != current_file)
12211         {
12212           current_file = line_info->dw_file_num;
12213           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12214           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12215         }
12216
12217       /* Emit debug info for the current line number, choosing the encoding
12218          that uses the least amount of space.  */
12219       if (line_info->dw_line_num != current_line)
12220         {
12221           line_offset = line_info->dw_line_num - current_line;
12222           line_delta = line_offset - DWARF_LINE_BASE;
12223           current_line = line_info->dw_line_num;
12224           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12225             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12226                                  "line %lu", current_line);
12227           else
12228             {
12229               dw2_asm_output_data (1, DW_LNS_advance_line,
12230                                    "advance to line %lu", current_line);
12231               dw2_asm_output_data_sleb128 (line_offset, NULL);
12232               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12233             }
12234         }
12235       else
12236         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12237
12238 #if 0
12239     cont:
12240 #endif
12241
12242       lt_index++;
12243
12244       /* If we're done with a function, end its sequence.  */
12245       if (lt_index == separate_line_info_table_in_use
12246           || separate_line_info_table[lt_index].function != function)
12247         {
12248           current_file = 1;
12249           current_line = 1;
12250
12251           /* Emit debug info for the address of the end of the function.  */
12252           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12253           if (0)
12254             {
12255               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12256                                    "DW_LNS_fixed_advance_pc");
12257               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12258             }
12259           else
12260             {
12261               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12262               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12263               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12264               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12265             }
12266
12267           /* Output the marker for the end of this sequence.  */
12268           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12269           dw2_asm_output_data_uleb128 (1, NULL);
12270           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12271         }
12272     }
12273
12274   /* Output the marker for the end of the line number info.  */
12275   ASM_OUTPUT_LABEL (asm_out_file, l2);
12276 }
12277
12278 /* Return the size of the .debug_dcall table for the compilation unit.  */
12279
12280 static unsigned long
12281 size_of_dcall_table (void)
12282 {
12283   unsigned long size;
12284   unsigned int i;
12285   dcall_entry *p;
12286   tree last_poc_decl = NULL;
12287
12288   /* Header:  version + debug info section pointer + pointer size.  */
12289   size = 2 + DWARF_OFFSET_SIZE + 1;
12290
12291   /* Each entry:  code label + DIE offset.  */
12292   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12293     {
12294       gcc_assert (p->targ_die != NULL);
12295       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12296       if (p->poc_decl != last_poc_decl)
12297         {
12298           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12299           gcc_assert (poc_die);
12300           last_poc_decl = p->poc_decl;
12301           if (poc_die)
12302             size += (DWARF_OFFSET_SIZE
12303                      + size_of_uleb128 (poc_die->die_offset));
12304         }
12305       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12306     }
12307
12308   return size;
12309 }
12310
12311 /* Output the direct call table used to disambiguate PC values when
12312    identical function have been merged.  */
12313
12314 static void
12315 output_dcall_table (void)
12316 {
12317   unsigned i;
12318   unsigned long dcall_length = size_of_dcall_table ();
12319   dcall_entry *p;
12320   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12321   tree last_poc_decl = NULL;
12322
12323   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12324     dw2_asm_output_data (4, 0xffffffff,
12325       "Initial length escape value indicating 64-bit DWARF extension");
12326   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12327                        "Length of Direct Call Table");
12328   dw2_asm_output_data (2, 4, "Version number");
12329   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12330                          debug_info_section,
12331                          "Offset of Compilation Unit Info");
12332   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12333
12334   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12335     {
12336       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12337       if (p->poc_decl != last_poc_decl)
12338         {
12339           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12340           last_poc_decl = p->poc_decl;
12341           if (poc_die)
12342             {
12343               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12344               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12345                                            "Caller DIE offset");
12346             }
12347         }
12348       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12349       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12350       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12351                                    "Callee DIE offset");
12352     }
12353 }
12354 \f
12355 /* Return the size of the .debug_vcall table for the compilation unit.  */
12356
12357 static unsigned long
12358 size_of_vcall_table (void)
12359 {
12360   unsigned long size;
12361   unsigned int i;
12362   vcall_entry *p;
12363
12364   /* Header:  version + pointer size.  */
12365   size = 2 + 1;
12366
12367   /* Each entry:  code label + vtable slot index.  */
12368   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12369     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12370
12371   return size;
12372 }
12373
12374 /* Output the virtual call table used to disambiguate PC values when
12375    identical function have been merged.  */
12376
12377 static void
12378 output_vcall_table (void)
12379 {
12380   unsigned i;
12381   unsigned long vcall_length = size_of_vcall_table ();
12382   vcall_entry *p;
12383   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12384
12385   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12386     dw2_asm_output_data (4, 0xffffffff,
12387       "Initial length escape value indicating 64-bit DWARF extension");
12388   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12389                        "Length of Virtual Call Table");
12390   dw2_asm_output_data (2, 4, "Version number");
12391   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12392
12393   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12394     {
12395       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12396       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12397       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12398     }
12399 }
12400 \f
12401 /* Given a pointer to a tree node for some base type, return a pointer to
12402    a DIE that describes the given type.
12403
12404    This routine must only be called for GCC type nodes that correspond to
12405    Dwarf base (fundamental) types.  */
12406
12407 static dw_die_ref
12408 base_type_die (tree type)
12409 {
12410   dw_die_ref base_type_result;
12411   enum dwarf_type encoding;
12412
12413   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12414     return 0;
12415
12416   /* If this is a subtype that should not be emitted as a subrange type,
12417      use the base type.  See subrange_type_for_debug_p.  */
12418   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12419     type = TREE_TYPE (type);
12420
12421   switch (TREE_CODE (type))
12422     {
12423     case INTEGER_TYPE:
12424       if ((dwarf_version >= 4 || !dwarf_strict)
12425           && TYPE_NAME (type)
12426           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12427           && DECL_IS_BUILTIN (TYPE_NAME (type))
12428           && DECL_NAME (TYPE_NAME (type)))
12429         {
12430           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12431           if (strcmp (name, "char16_t") == 0
12432               || strcmp (name, "char32_t") == 0)
12433             {
12434               encoding = DW_ATE_UTF;
12435               break;
12436             }
12437         }
12438       if (TYPE_STRING_FLAG (type))
12439         {
12440           if (TYPE_UNSIGNED (type))
12441             encoding = DW_ATE_unsigned_char;
12442           else
12443             encoding = DW_ATE_signed_char;
12444         }
12445       else if (TYPE_UNSIGNED (type))
12446         encoding = DW_ATE_unsigned;
12447       else
12448         encoding = DW_ATE_signed;
12449       break;
12450
12451     case REAL_TYPE:
12452       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12453         {
12454           if (dwarf_version >= 3 || !dwarf_strict)
12455             encoding = DW_ATE_decimal_float;
12456           else
12457             encoding = DW_ATE_lo_user;
12458         }
12459       else
12460         encoding = DW_ATE_float;
12461       break;
12462
12463     case FIXED_POINT_TYPE:
12464       if (!(dwarf_version >= 3 || !dwarf_strict))
12465         encoding = DW_ATE_lo_user;
12466       else if (TYPE_UNSIGNED (type))
12467         encoding = DW_ATE_unsigned_fixed;
12468       else
12469         encoding = DW_ATE_signed_fixed;
12470       break;
12471
12472       /* Dwarf2 doesn't know anything about complex ints, so use
12473          a user defined type for it.  */
12474     case COMPLEX_TYPE:
12475       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12476         encoding = DW_ATE_complex_float;
12477       else
12478         encoding = DW_ATE_lo_user;
12479       break;
12480
12481     case BOOLEAN_TYPE:
12482       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12483       encoding = DW_ATE_boolean;
12484       break;
12485
12486     default:
12487       /* No other TREE_CODEs are Dwarf fundamental types.  */
12488       gcc_unreachable ();
12489     }
12490
12491   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12492
12493   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12494                    int_size_in_bytes (type));
12495   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12496
12497   return base_type_result;
12498 }
12499
12500 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12501    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12502
12503 static inline int
12504 is_base_type (tree type)
12505 {
12506   switch (TREE_CODE (type))
12507     {
12508     case ERROR_MARK:
12509     case VOID_TYPE:
12510     case INTEGER_TYPE:
12511     case REAL_TYPE:
12512     case FIXED_POINT_TYPE:
12513     case COMPLEX_TYPE:
12514     case BOOLEAN_TYPE:
12515       return 1;
12516
12517     case ARRAY_TYPE:
12518     case RECORD_TYPE:
12519     case UNION_TYPE:
12520     case QUAL_UNION_TYPE:
12521     case ENUMERAL_TYPE:
12522     case FUNCTION_TYPE:
12523     case METHOD_TYPE:
12524     case POINTER_TYPE:
12525     case REFERENCE_TYPE:
12526     case OFFSET_TYPE:
12527     case LANG_TYPE:
12528     case VECTOR_TYPE:
12529       return 0;
12530
12531     default:
12532       gcc_unreachable ();
12533     }
12534
12535   return 0;
12536 }
12537
12538 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12539    node, return the size in bits for the type if it is a constant, or else
12540    return the alignment for the type if the type's size is not constant, or
12541    else return BITS_PER_WORD if the type actually turns out to be an
12542    ERROR_MARK node.  */
12543
12544 static inline unsigned HOST_WIDE_INT
12545 simple_type_size_in_bits (const_tree type)
12546 {
12547   if (TREE_CODE (type) == ERROR_MARK)
12548     return BITS_PER_WORD;
12549   else if (TYPE_SIZE (type) == NULL_TREE)
12550     return 0;
12551   else if (host_integerp (TYPE_SIZE (type), 1))
12552     return tree_low_cst (TYPE_SIZE (type), 1);
12553   else
12554     return TYPE_ALIGN (type);
12555 }
12556
12557 /* Similarly, but return a double_int instead of UHWI.  */
12558
12559 static inline double_int
12560 double_int_type_size_in_bits (const_tree type)
12561 {
12562   if (TREE_CODE (type) == ERROR_MARK)
12563     return uhwi_to_double_int (BITS_PER_WORD);
12564   else if (TYPE_SIZE (type) == NULL_TREE)
12565     return double_int_zero;
12566   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12567     return tree_to_double_int (TYPE_SIZE (type));
12568   else
12569     return uhwi_to_double_int (TYPE_ALIGN (type));
12570 }
12571
12572 /*  Given a pointer to a tree node for a subrange type, return a pointer
12573     to a DIE that describes the given type.  */
12574
12575 static dw_die_ref
12576 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12577 {
12578   dw_die_ref subrange_die;
12579   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12580
12581   if (context_die == NULL)
12582     context_die = comp_unit_die;
12583
12584   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12585
12586   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12587     {
12588       /* The size of the subrange type and its base type do not match,
12589          so we need to generate a size attribute for the subrange type.  */
12590       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12591     }
12592
12593   if (low)
12594     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12595   if (high)
12596     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12597
12598   return subrange_die;
12599 }
12600
12601 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12602    entry that chains various modifiers in front of the given type.  */
12603
12604 static dw_die_ref
12605 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12606                    dw_die_ref context_die)
12607 {
12608   enum tree_code code = TREE_CODE (type);
12609   dw_die_ref mod_type_die;
12610   dw_die_ref sub_die = NULL;
12611   tree item_type = NULL;
12612   tree qualified_type;
12613   tree name, low, high;
12614
12615   if (code == ERROR_MARK)
12616     return NULL;
12617
12618   /* See if we already have the appropriately qualified variant of
12619      this type.  */
12620   qualified_type
12621     = get_qualified_type (type,
12622                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12623                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12624
12625   if (qualified_type == sizetype
12626       && TYPE_NAME (qualified_type)
12627       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12628     {
12629 #ifdef ENABLE_CHECKING
12630       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12631                   == INTEGER_TYPE
12632                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12633                      == TYPE_PRECISION (qualified_type)
12634                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12635                      == TYPE_UNSIGNED (qualified_type));
12636 #endif
12637       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12638     }
12639
12640   /* If we do, then we can just use its DIE, if it exists.  */
12641   if (qualified_type)
12642     {
12643       mod_type_die = lookup_type_die (qualified_type);
12644       if (mod_type_die)
12645         return mod_type_die;
12646     }
12647
12648   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12649
12650   /* Handle C typedef types.  */
12651   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12652       && !DECL_ARTIFICIAL (name))
12653     {
12654       tree dtype = TREE_TYPE (name);
12655
12656       if (qualified_type == dtype)
12657         {
12658           /* For a named type, use the typedef.  */
12659           gen_type_die (qualified_type, context_die);
12660           return lookup_type_die (qualified_type);
12661         }
12662       else if (is_const_type < TYPE_READONLY (dtype)
12663                || is_volatile_type < TYPE_VOLATILE (dtype)
12664                || (is_const_type <= TYPE_READONLY (dtype)
12665                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12666                    && DECL_ORIGINAL_TYPE (name) != type))
12667         /* cv-unqualified version of named type.  Just use the unnamed
12668            type to which it refers.  */
12669         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12670                                   is_const_type, is_volatile_type,
12671                                   context_die);
12672       /* Else cv-qualified version of named type; fall through.  */
12673     }
12674
12675   if (is_const_type)
12676     {
12677       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12678       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12679     }
12680   else if (is_volatile_type)
12681     {
12682       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12683       sub_die = modified_type_die (type, 0, 0, context_die);
12684     }
12685   else if (code == POINTER_TYPE)
12686     {
12687       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12688       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12689                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12690       item_type = TREE_TYPE (type);
12691       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12692         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12693                          TYPE_ADDR_SPACE (item_type));
12694     }
12695   else if (code == REFERENCE_TYPE)
12696     {
12697       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12698         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12699                                 type);
12700       else
12701         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12702       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12703                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12704       item_type = TREE_TYPE (type);
12705       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12706         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12707                          TYPE_ADDR_SPACE (item_type));
12708     }
12709   else if (code == INTEGER_TYPE
12710            && TREE_TYPE (type) != NULL_TREE
12711            && subrange_type_for_debug_p (type, &low, &high))
12712     {
12713       mod_type_die = subrange_type_die (type, low, high, context_die);
12714       item_type = TREE_TYPE (type);
12715     }
12716   else if (is_base_type (type))
12717     mod_type_die = base_type_die (type);
12718   else
12719     {
12720       gen_type_die (type, context_die);
12721
12722       /* We have to get the type_main_variant here (and pass that to the
12723          `lookup_type_die' routine) because the ..._TYPE node we have
12724          might simply be a *copy* of some original type node (where the
12725          copy was created to help us keep track of typedef names) and
12726          that copy might have a different TYPE_UID from the original
12727          ..._TYPE node.  */
12728       if (TREE_CODE (type) != VECTOR_TYPE)
12729         return lookup_type_die (type_main_variant (type));
12730       else
12731         /* Vectors have the debugging information in the type,
12732            not the main variant.  */
12733         return lookup_type_die (type);
12734     }
12735
12736   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12737      don't output a DW_TAG_typedef, since there isn't one in the
12738      user's program; just attach a DW_AT_name to the type.
12739      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12740      if the base type already has the same name.  */
12741   if (name
12742       && ((TREE_CODE (name) != TYPE_DECL
12743            && (qualified_type == TYPE_MAIN_VARIANT (type)
12744                || (!is_const_type && !is_volatile_type)))
12745           || (TREE_CODE (name) == TYPE_DECL
12746               && TREE_TYPE (name) == qualified_type
12747               && DECL_NAME (name))))
12748     {
12749       if (TREE_CODE (name) == TYPE_DECL)
12750         /* Could just call add_name_and_src_coords_attributes here,
12751            but since this is a builtin type it doesn't have any
12752            useful source coordinates anyway.  */
12753         name = DECL_NAME (name);
12754       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12755     }
12756   /* This probably indicates a bug.  */
12757   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12758     add_name_attribute (mod_type_die, "__unknown__");
12759
12760   if (qualified_type)
12761     equate_type_number_to_die (qualified_type, mod_type_die);
12762
12763   if (item_type)
12764     /* We must do this after the equate_type_number_to_die call, in case
12765        this is a recursive type.  This ensures that the modified_type_die
12766        recursion will terminate even if the type is recursive.  Recursive
12767        types are possible in Ada.  */
12768     sub_die = modified_type_die (item_type,
12769                                  TYPE_READONLY (item_type),
12770                                  TYPE_VOLATILE (item_type),
12771                                  context_die);
12772
12773   if (sub_die != NULL)
12774     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12775
12776   return mod_type_die;
12777 }
12778
12779 /* Generate DIEs for the generic parameters of T.
12780    T must be either a generic type or a generic function.
12781    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12782
12783 static void
12784 gen_generic_params_dies (tree t)
12785 {
12786   tree parms, args;
12787   int parms_num, i;
12788   dw_die_ref die = NULL;
12789
12790   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12791     return;
12792
12793   if (TYPE_P (t))
12794     die = lookup_type_die (t);
12795   else if (DECL_P (t))
12796     die = lookup_decl_die (t);
12797
12798   gcc_assert (die);
12799
12800   parms = lang_hooks.get_innermost_generic_parms (t);
12801   if (!parms)
12802     /* T has no generic parameter. It means T is neither a generic type
12803        or function. End of story.  */
12804     return;
12805
12806   parms_num = TREE_VEC_LENGTH (parms);
12807   args = lang_hooks.get_innermost_generic_args (t);
12808   for (i = 0; i < parms_num; i++)
12809     {
12810       tree parm, arg, arg_pack_elems;
12811
12812       parm = TREE_VEC_ELT (parms, i);
12813       arg = TREE_VEC_ELT (args, i);
12814       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12815       gcc_assert (parm && TREE_VALUE (parm) && arg);
12816
12817       if (parm && TREE_VALUE (parm) && arg)
12818         {
12819           /* If PARM represents a template parameter pack,
12820              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12821              by DW_TAG_template_*_parameter DIEs for the argument
12822              pack elements of ARG. Note that ARG would then be
12823              an argument pack.  */
12824           if (arg_pack_elems)
12825             template_parameter_pack_die (TREE_VALUE (parm),
12826                                          arg_pack_elems,
12827                                          die);
12828           else
12829             generic_parameter_die (TREE_VALUE (parm), arg,
12830                                    true /* Emit DW_AT_name */, die);
12831         }
12832     }
12833 }
12834
12835 /* Create and return a DIE for PARM which should be
12836    the representation of a generic type parameter.
12837    For instance, in the C++ front end, PARM would be a template parameter.
12838    ARG is the argument to PARM.
12839    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12840    name of the PARM.
12841    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12842    as a child node.  */
12843
12844 static dw_die_ref
12845 generic_parameter_die (tree parm, tree arg,
12846                        bool emit_name_p,
12847                        dw_die_ref parent_die)
12848 {
12849   dw_die_ref tmpl_die = NULL;
12850   const char *name = NULL;
12851
12852   if (!parm || !DECL_NAME (parm) || !arg)
12853     return NULL;
12854
12855   /* We support non-type generic parameters and arguments,
12856      type generic parameters and arguments, as well as
12857      generic generic parameters (a.k.a. template template parameters in C++)
12858      and arguments.  */
12859   if (TREE_CODE (parm) == PARM_DECL)
12860     /* PARM is a nontype generic parameter  */
12861     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12862   else if (TREE_CODE (parm) == TYPE_DECL)
12863     /* PARM is a type generic parameter.  */
12864     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12865   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12866     /* PARM is a generic generic parameter.
12867        Its DIE is a GNU extension. It shall have a
12868        DW_AT_name attribute to represent the name of the template template
12869        parameter, and a DW_AT_GNU_template_name attribute to represent the
12870        name of the template template argument.  */
12871     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12872                         parent_die, parm);
12873   else
12874     gcc_unreachable ();
12875
12876   if (tmpl_die)
12877     {
12878       tree tmpl_type;
12879
12880       /* If PARM is a generic parameter pack, it means we are
12881          emitting debug info for a template argument pack element.
12882          In other terms, ARG is a template argument pack element.
12883          In that case, we don't emit any DW_AT_name attribute for
12884          the die.  */
12885       if (emit_name_p)
12886         {
12887           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12888           gcc_assert (name);
12889           add_AT_string (tmpl_die, DW_AT_name, name);
12890         }
12891
12892       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12893         {
12894           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12895              TMPL_DIE should have a child DW_AT_type attribute that is set
12896              to the type of the argument to PARM, which is ARG.
12897              If PARM is a type generic parameter, TMPL_DIE should have a
12898              child DW_AT_type that is set to ARG.  */
12899           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12900           add_type_attribute (tmpl_die, tmpl_type, 0,
12901                               TREE_THIS_VOLATILE (tmpl_type),
12902                               parent_die);
12903         }
12904       else
12905         {
12906           /* So TMPL_DIE is a DIE representing a
12907              a generic generic template parameter, a.k.a template template
12908              parameter in C++ and arg is a template.  */
12909
12910           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12911              to the name of the argument.  */
12912           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12913           if (name)
12914             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12915         }
12916
12917       if (TREE_CODE (parm) == PARM_DECL)
12918         /* So PARM is a non-type generic parameter.
12919            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12920            attribute of TMPL_DIE which value represents the value
12921            of ARG.
12922            We must be careful here:
12923            The value of ARG might reference some function decls.
12924            We might currently be emitting debug info for a generic
12925            type and types are emitted before function decls, we don't
12926            know if the function decls referenced by ARG will actually be
12927            emitted after cgraph computations.
12928            So must defer the generation of the DW_AT_const_value to
12929            after cgraph is ready.  */
12930         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12931     }
12932
12933   return tmpl_die;
12934 }
12935
12936 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12937    PARM_PACK must be a template parameter pack. The returned DIE
12938    will be child DIE of PARENT_DIE.  */
12939
12940 static dw_die_ref
12941 template_parameter_pack_die (tree parm_pack,
12942                              tree parm_pack_args,
12943                              dw_die_ref parent_die)
12944 {
12945   dw_die_ref die;
12946   int j;
12947
12948   gcc_assert (parent_die && parm_pack);
12949
12950   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12951   add_name_and_src_coords_attributes (die, parm_pack);
12952   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12953     generic_parameter_die (parm_pack,
12954                            TREE_VEC_ELT (parm_pack_args, j),
12955                            false /* Don't emit DW_AT_name */,
12956                            die);
12957   return die;
12958 }
12959
12960 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12961    an enumerated type.  */
12962
12963 static inline int
12964 type_is_enum (const_tree type)
12965 {
12966   return TREE_CODE (type) == ENUMERAL_TYPE;
12967 }
12968
12969 /* Return the DBX register number described by a given RTL node.  */
12970
12971 static unsigned int
12972 dbx_reg_number (const_rtx rtl)
12973 {
12974   unsigned regno = REGNO (rtl);
12975
12976   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12977
12978 #ifdef LEAF_REG_REMAP
12979   if (current_function_uses_only_leaf_regs)
12980     {
12981       int leaf_reg = LEAF_REG_REMAP (regno);
12982       if (leaf_reg != -1)
12983         regno = (unsigned) leaf_reg;
12984     }
12985 #endif
12986
12987   return DBX_REGISTER_NUMBER (regno);
12988 }
12989
12990 /* Optionally add a DW_OP_piece term to a location description expression.
12991    DW_OP_piece is only added if the location description expression already
12992    doesn't end with DW_OP_piece.  */
12993
12994 static void
12995 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12996 {
12997   dw_loc_descr_ref loc;
12998
12999   if (*list_head != NULL)
13000     {
13001       /* Find the end of the chain.  */
13002       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13003         ;
13004
13005       if (loc->dw_loc_opc != DW_OP_piece)
13006         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13007     }
13008 }
13009
13010 /* Return a location descriptor that designates a machine register or
13011    zero if there is none.  */
13012
13013 static dw_loc_descr_ref
13014 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13015 {
13016   rtx regs;
13017
13018   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13019     return 0;
13020
13021   /* We only use "frame base" when we're sure we're talking about the
13022      post-prologue local stack frame.  We do this by *not* running
13023      register elimination until this point, and recognizing the special
13024      argument pointer and soft frame pointer rtx's.
13025      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13026   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13027       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13028     {
13029       dw_loc_descr_ref result = NULL;
13030
13031       if (dwarf_version >= 4 || !dwarf_strict)
13032         {
13033           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13034           if (result)
13035             add_loc_descr (&result,
13036                            new_loc_descr (DW_OP_stack_value, 0, 0));
13037         }
13038       return result;
13039     }
13040
13041   regs = targetm.dwarf_register_span (rtl);
13042
13043   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13044     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13045   else
13046     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13047 }
13048
13049 /* Return a location descriptor that designates a machine register for
13050    a given hard register number.  */
13051
13052 static dw_loc_descr_ref
13053 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13054 {
13055   dw_loc_descr_ref reg_loc_descr;
13056
13057   if (regno <= 31)
13058     reg_loc_descr
13059       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13060   else
13061     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13062
13063   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13064     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13065
13066   return reg_loc_descr;
13067 }
13068
13069 /* Given an RTL of a register, return a location descriptor that
13070    designates a value that spans more than one register.  */
13071
13072 static dw_loc_descr_ref
13073 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13074                              enum var_init_status initialized)
13075 {
13076   int nregs, size, i;
13077   unsigned reg;
13078   dw_loc_descr_ref loc_result = NULL;
13079
13080   reg = REGNO (rtl);
13081 #ifdef LEAF_REG_REMAP
13082   if (current_function_uses_only_leaf_regs)
13083     {
13084       int leaf_reg = LEAF_REG_REMAP (reg);
13085       if (leaf_reg != -1)
13086         reg = (unsigned) leaf_reg;
13087     }
13088 #endif
13089   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13090   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13091
13092   /* Simple, contiguous registers.  */
13093   if (regs == NULL_RTX)
13094     {
13095       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13096
13097       loc_result = NULL;
13098       while (nregs--)
13099         {
13100           dw_loc_descr_ref t;
13101
13102           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13103                                       VAR_INIT_STATUS_INITIALIZED);
13104           add_loc_descr (&loc_result, t);
13105           add_loc_descr_op_piece (&loc_result, size);
13106           ++reg;
13107         }
13108       return loc_result;
13109     }
13110
13111   /* Now onto stupid register sets in non contiguous locations.  */
13112
13113   gcc_assert (GET_CODE (regs) == PARALLEL);
13114
13115   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13116   loc_result = NULL;
13117
13118   for (i = 0; i < XVECLEN (regs, 0); ++i)
13119     {
13120       dw_loc_descr_ref t;
13121
13122       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13123                                   VAR_INIT_STATUS_INITIALIZED);
13124       add_loc_descr (&loc_result, t);
13125       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13126       add_loc_descr_op_piece (&loc_result, size);
13127     }
13128
13129   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13130     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13131   return loc_result;
13132 }
13133
13134 #endif /* DWARF2_DEBUGGING_INFO */
13135
13136 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
13137
13138 /* Return a location descriptor that designates a constant.  */
13139
13140 static dw_loc_descr_ref
13141 int_loc_descriptor (HOST_WIDE_INT i)
13142 {
13143   enum dwarf_location_atom op;
13144
13145   /* Pick the smallest representation of a constant, rather than just
13146      defaulting to the LEB encoding.  */
13147   if (i >= 0)
13148     {
13149       if (i <= 31)
13150         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13151       else if (i <= 0xff)
13152         op = DW_OP_const1u;
13153       else if (i <= 0xffff)
13154         op = DW_OP_const2u;
13155       else if (HOST_BITS_PER_WIDE_INT == 32
13156                || i <= 0xffffffff)
13157         op = DW_OP_const4u;
13158       else
13159         op = DW_OP_constu;
13160     }
13161   else
13162     {
13163       if (i >= -0x80)
13164         op = DW_OP_const1s;
13165       else if (i >= -0x8000)
13166         op = DW_OP_const2s;
13167       else if (HOST_BITS_PER_WIDE_INT == 32
13168                || i >= -0x80000000)
13169         op = DW_OP_const4s;
13170       else
13171         op = DW_OP_consts;
13172     }
13173
13174   return new_loc_descr (op, i, 0);
13175 }
13176 #endif
13177
13178 #ifdef DWARF2_DEBUGGING_INFO
13179 /* Return loc description representing "address" of integer value.
13180    This can appear only as toplevel expression.  */
13181
13182 static dw_loc_descr_ref
13183 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13184 {
13185   int litsize;
13186   dw_loc_descr_ref loc_result = NULL;
13187
13188   if (!(dwarf_version >= 4 || !dwarf_strict))
13189     return NULL;
13190
13191   if (i >= 0)
13192     {
13193       if (i <= 31)
13194         litsize = 1;
13195       else if (i <= 0xff)
13196         litsize = 2;
13197       else if (i <= 0xffff)
13198         litsize = 3;
13199       else if (HOST_BITS_PER_WIDE_INT == 32
13200                || i <= 0xffffffff)
13201         litsize = 5;
13202       else
13203         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13204     }
13205   else
13206     {
13207       if (i >= -0x80)
13208         litsize = 2;
13209       else if (i >= -0x8000)
13210         litsize = 3;
13211       else if (HOST_BITS_PER_WIDE_INT == 32
13212                || i >= -0x80000000)
13213         litsize = 5;
13214       else
13215         litsize = 1 + size_of_sleb128 (i);
13216     }
13217   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13218      is more compact.  For DW_OP_stack_value we need:
13219      litsize + 1 (DW_OP_stack_value)
13220      and for DW_OP_implicit_value:
13221      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13222   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13223     {
13224       loc_result = int_loc_descriptor (i);
13225       add_loc_descr (&loc_result,
13226                      new_loc_descr (DW_OP_stack_value, 0, 0));
13227       return loc_result;
13228     }
13229
13230   loc_result = new_loc_descr (DW_OP_implicit_value,
13231                               size, 0);
13232   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13233   loc_result->dw_loc_oprnd2.v.val_int = i;
13234   return loc_result;
13235 }
13236
13237 /* Return a location descriptor that designates a base+offset location.  */
13238
13239 static dw_loc_descr_ref
13240 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13241                  enum var_init_status initialized)
13242 {
13243   unsigned int regno;
13244   dw_loc_descr_ref result;
13245   dw_fde_ref fde = current_fde ();
13246
13247   /* We only use "frame base" when we're sure we're talking about the
13248      post-prologue local stack frame.  We do this by *not* running
13249      register elimination until this point, and recognizing the special
13250      argument pointer and soft frame pointer rtx's.  */
13251   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13252     {
13253       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13254
13255       if (elim != reg)
13256         {
13257           if (GET_CODE (elim) == PLUS)
13258             {
13259               offset += INTVAL (XEXP (elim, 1));
13260               elim = XEXP (elim, 0);
13261             }
13262           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13263                        && (elim == hard_frame_pointer_rtx
13264                            || elim == stack_pointer_rtx))
13265                       || elim == (frame_pointer_needed
13266                                   ? hard_frame_pointer_rtx
13267                                   : stack_pointer_rtx));
13268
13269           /* If drap register is used to align stack, use frame
13270              pointer + offset to access stack variables.  If stack
13271              is aligned without drap, use stack pointer + offset to
13272              access stack variables.  */
13273           if (crtl->stack_realign_tried
13274               && reg == frame_pointer_rtx)
13275             {
13276               int base_reg
13277                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13278                                       ? HARD_FRAME_POINTER_REGNUM
13279                                       : STACK_POINTER_REGNUM);
13280               return new_reg_loc_descr (base_reg, offset);
13281             }
13282
13283           offset += frame_pointer_fb_offset;
13284           return new_loc_descr (DW_OP_fbreg, offset, 0);
13285         }
13286     }
13287   else if (!optimize
13288            && fde
13289            && (fde->drap_reg == REGNO (reg)
13290                || fde->vdrap_reg == REGNO (reg)))
13291     {
13292       /* Use cfa+offset to represent the location of arguments passed
13293          on the stack when drap is used to align stack.
13294          Only do this when not optimizing, for optimized code var-tracking
13295          is supposed to track where the arguments live and the register
13296          used as vdrap or drap in some spot might be used for something
13297          else in other part of the routine.  */
13298       return new_loc_descr (DW_OP_fbreg, offset, 0);
13299     }
13300
13301   regno = dbx_reg_number (reg);
13302   if (regno <= 31)
13303     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13304                             offset, 0);
13305   else
13306     result = new_loc_descr (DW_OP_bregx, regno, offset);
13307
13308   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13309     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13310
13311   return result;
13312 }
13313
13314 /* Return true if this RTL expression describes a base+offset calculation.  */
13315
13316 static inline int
13317 is_based_loc (const_rtx rtl)
13318 {
13319   return (GET_CODE (rtl) == PLUS
13320           && ((REG_P (XEXP (rtl, 0))
13321                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13322                && CONST_INT_P (XEXP (rtl, 1)))));
13323 }
13324
13325 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13326    failed.  */
13327
13328 static dw_loc_descr_ref
13329 tls_mem_loc_descriptor (rtx mem)
13330 {
13331   tree base;
13332   dw_loc_descr_ref loc_result;
13333
13334   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13335     return NULL;
13336
13337   base = get_base_address (MEM_EXPR (mem));
13338   if (base == NULL
13339       || TREE_CODE (base) != VAR_DECL
13340       || !DECL_THREAD_LOCAL_P (base))
13341     return NULL;
13342
13343   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13344   if (loc_result == NULL)
13345     return NULL;
13346
13347   if (INTVAL (MEM_OFFSET (mem)))
13348     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13349
13350   return loc_result;
13351 }
13352
13353 /* Output debug info about reason why we failed to expand expression as dwarf
13354    expression.  */
13355
13356 static void
13357 expansion_failed (tree expr, rtx rtl, char const *reason)
13358 {
13359   if (dump_file && (dump_flags & TDF_DETAILS))
13360     {
13361       fprintf (dump_file, "Failed to expand as dwarf: ");
13362       if (expr)
13363         print_generic_expr (dump_file, expr, dump_flags);
13364       if (rtl)
13365         {
13366           fprintf (dump_file, "\n");
13367           print_rtl (dump_file, rtl);
13368         }
13369       fprintf (dump_file, "\nReason: %s\n", reason);
13370     }
13371 }
13372
13373 /* Helper function for const_ok_for_output, called either directly
13374    or via for_each_rtx.  */
13375
13376 static int
13377 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13378 {
13379   rtx rtl = *rtlp;
13380
13381   if (GET_CODE (rtl) == UNSPEC)
13382     {
13383       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13384          we can't express it in the debug info.  */
13385 #ifdef ENABLE_CHECKING
13386       inform (current_function_decl
13387               ? DECL_SOURCE_LOCATION (current_function_decl)
13388               : UNKNOWN_LOCATION,
13389               "non-delegitimized UNSPEC %d found in variable location",
13390               XINT (rtl, 1));
13391 #endif
13392       expansion_failed (NULL_TREE, rtl,
13393                         "UNSPEC hasn't been delegitimized.\n");
13394       return 1;
13395     }
13396
13397   if (GET_CODE (rtl) != SYMBOL_REF)
13398     return 0;
13399
13400   if (CONSTANT_POOL_ADDRESS_P (rtl))
13401     {
13402       bool marked;
13403       get_pool_constant_mark (rtl, &marked);
13404       /* If all references to this pool constant were optimized away,
13405          it was not output and thus we can't represent it.  */
13406       if (!marked)
13407         {
13408           expansion_failed (NULL_TREE, rtl,
13409                             "Constant was removed from constant pool.\n");
13410           return 1;
13411         }
13412     }
13413
13414   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13415     return 1;
13416
13417   /* Avoid references to external symbols in debug info, on several targets
13418      the linker might even refuse to link when linking a shared library,
13419      and in many other cases the relocations for .debug_info/.debug_loc are
13420      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13421      to be defined within the same shared library or executable are fine.  */
13422   if (SYMBOL_REF_EXTERNAL_P (rtl))
13423     {
13424       tree decl = SYMBOL_REF_DECL (rtl);
13425
13426       if (decl == NULL || !targetm.binds_local_p (decl))
13427         {
13428           expansion_failed (NULL_TREE, rtl,
13429                             "Symbol not defined in current TU.\n");
13430           return 1;
13431         }
13432     }
13433
13434   return 0;
13435 }
13436
13437 /* Return true if constant RTL can be emitted in DW_OP_addr or
13438    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13439    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13440
13441 static bool
13442 const_ok_for_output (rtx rtl)
13443 {
13444   if (GET_CODE (rtl) == SYMBOL_REF)
13445     return const_ok_for_output_1 (&rtl, NULL) == 0;
13446
13447   if (GET_CODE (rtl) == CONST)
13448     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13449
13450   return true;
13451 }
13452
13453 /* The following routine converts the RTL for a variable or parameter
13454    (resident in memory) into an equivalent Dwarf representation of a
13455    mechanism for getting the address of that same variable onto the top of a
13456    hypothetical "address evaluation" stack.
13457
13458    When creating memory location descriptors, we are effectively transforming
13459    the RTL for a memory-resident object into its Dwarf postfix expression
13460    equivalent.  This routine recursively descends an RTL tree, turning
13461    it into Dwarf postfix code as it goes.
13462
13463    MODE is the mode of the memory reference, needed to handle some
13464    autoincrement addressing modes.
13465
13466    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13467    location list for RTL.
13468
13469    Return 0 if we can't represent the location.  */
13470
13471 static dw_loc_descr_ref
13472 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13473                     enum var_init_status initialized)
13474 {
13475   dw_loc_descr_ref mem_loc_result = NULL;
13476   enum dwarf_location_atom op;
13477   dw_loc_descr_ref op0, op1;
13478
13479   /* Note that for a dynamically sized array, the location we will generate a
13480      description of here will be the lowest numbered location which is
13481      actually within the array.  That's *not* necessarily the same as the
13482      zeroth element of the array.  */
13483
13484   rtl = targetm.delegitimize_address (rtl);
13485
13486   switch (GET_CODE (rtl))
13487     {
13488     case POST_INC:
13489     case POST_DEC:
13490     case POST_MODIFY:
13491       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13492
13493     case SUBREG:
13494       /* The case of a subreg may arise when we have a local (register)
13495          variable or a formal (register) parameter which doesn't quite fill
13496          up an entire register.  For now, just assume that it is
13497          legitimate to make the Dwarf info refer to the whole register which
13498          contains the given subreg.  */
13499       if (!subreg_lowpart_p (rtl))
13500         break;
13501       rtl = SUBREG_REG (rtl);
13502       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13503         break;
13504       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13505         break;
13506       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13507       break;
13508
13509     case REG:
13510       /* Whenever a register number forms a part of the description of the
13511          method for calculating the (dynamic) address of a memory resident
13512          object, DWARF rules require the register number be referred to as
13513          a "base register".  This distinction is not based in any way upon
13514          what category of register the hardware believes the given register
13515          belongs to.  This is strictly DWARF terminology we're dealing with
13516          here. Note that in cases where the location of a memory-resident
13517          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13518          OP_CONST (0)) the actual DWARF location descriptor that we generate
13519          may just be OP_BASEREG (basereg).  This may look deceptively like
13520          the object in question was allocated to a register (rather than in
13521          memory) so DWARF consumers need to be aware of the subtle
13522          distinction between OP_REG and OP_BASEREG.  */
13523       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13524         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13525       else if (stack_realign_drap
13526                && crtl->drap_reg
13527                && crtl->args.internal_arg_pointer == rtl
13528                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13529         {
13530           /* If RTL is internal_arg_pointer, which has been optimized
13531              out, use DRAP instead.  */
13532           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13533                                             VAR_INIT_STATUS_INITIALIZED);
13534         }
13535       break;
13536
13537     case SIGN_EXTEND:
13538     case ZERO_EXTEND:
13539       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13540                                 VAR_INIT_STATUS_INITIALIZED);
13541       if (op0 == 0)
13542         break;
13543       else
13544         {
13545           int shift = DWARF2_ADDR_SIZE
13546                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13547           shift *= BITS_PER_UNIT;
13548           if (GET_CODE (rtl) == SIGN_EXTEND)
13549             op = DW_OP_shra;
13550           else
13551             op = DW_OP_shr;
13552           mem_loc_result = op0;
13553           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13554           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13555           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13556           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13557         }
13558       break;
13559
13560     case MEM:
13561       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13562                                            VAR_INIT_STATUS_INITIALIZED);
13563       if (mem_loc_result == NULL)
13564         mem_loc_result = tls_mem_loc_descriptor (rtl);
13565       if (mem_loc_result != 0)
13566         {
13567           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13568             {
13569               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13570               return 0;
13571             }
13572           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13573             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13574           else
13575             add_loc_descr (&mem_loc_result,
13576                            new_loc_descr (DW_OP_deref_size,
13577                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13578         }
13579       else
13580         {
13581           rtx new_rtl = avoid_constant_pool_reference (rtl);
13582           if (new_rtl != rtl)
13583             return mem_loc_descriptor (new_rtl, mode, initialized);
13584         }
13585       break;
13586
13587     case LO_SUM:
13588          rtl = XEXP (rtl, 1);
13589
13590       /* ... fall through ...  */
13591
13592     case LABEL_REF:
13593       /* Some ports can transform a symbol ref into a label ref, because
13594          the symbol ref is too far away and has to be dumped into a constant
13595          pool.  */
13596     case CONST:
13597     case SYMBOL_REF:
13598       if (GET_CODE (rtl) == SYMBOL_REF
13599           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13600         {
13601           dw_loc_descr_ref temp;
13602
13603           /* If this is not defined, we have no way to emit the data.  */
13604           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13605             break;
13606
13607           /* We used to emit DW_OP_addr here, but that's wrong, since
13608              DW_OP_addr should be relocated by the debug info consumer,
13609              while DW_OP_GNU_push_tls_address operand should not.  */
13610           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13611                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13612           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13613           temp->dw_loc_oprnd1.v.val_addr = rtl;
13614           temp->dtprel = true;
13615
13616           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13617           add_loc_descr (&mem_loc_result, temp);
13618
13619           break;
13620         }
13621
13622       if (!const_ok_for_output (rtl))
13623         break;
13624
13625     symref:
13626       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13627       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13628       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13629       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13630       break;
13631
13632     case CONCAT:
13633     case CONCATN:
13634     case VAR_LOCATION:
13635       expansion_failed (NULL_TREE, rtl,
13636                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13637       return 0;
13638
13639     case PRE_MODIFY:
13640       /* Extract the PLUS expression nested inside and fall into
13641          PLUS code below.  */
13642       rtl = XEXP (rtl, 1);
13643       goto plus;
13644
13645     case PRE_INC:
13646     case PRE_DEC:
13647       /* Turn these into a PLUS expression and fall into the PLUS code
13648          below.  */
13649       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13650                           GEN_INT (GET_CODE (rtl) == PRE_INC
13651                                    ? GET_MODE_UNIT_SIZE (mode)
13652                                    : -GET_MODE_UNIT_SIZE (mode)));
13653
13654       /* ... fall through ...  */
13655
13656     case PLUS:
13657     plus:
13658       if (is_based_loc (rtl))
13659         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13660                                           INTVAL (XEXP (rtl, 1)),
13661                                           VAR_INIT_STATUS_INITIALIZED);
13662       else
13663         {
13664           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13665                                                VAR_INIT_STATUS_INITIALIZED);
13666           if (mem_loc_result == 0)
13667             break;
13668
13669           if (CONST_INT_P (XEXP (rtl, 1)))
13670             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13671           else
13672             {
13673               dw_loc_descr_ref mem_loc_result2
13674                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13675                                       VAR_INIT_STATUS_INITIALIZED);
13676               if (mem_loc_result2 == 0)
13677                 break;
13678               add_loc_descr (&mem_loc_result, mem_loc_result2);
13679               add_loc_descr (&mem_loc_result,
13680                              new_loc_descr (DW_OP_plus, 0, 0));
13681             }
13682         }
13683       break;
13684
13685     /* If a pseudo-reg is optimized away, it is possible for it to
13686        be replaced with a MEM containing a multiply or shift.  */
13687     case MINUS:
13688       op = DW_OP_minus;
13689       goto do_binop;
13690
13691     case MULT:
13692       op = DW_OP_mul;
13693       goto do_binop;
13694
13695     case DIV:
13696       op = DW_OP_div;
13697       goto do_binop;
13698
13699     case UMOD:
13700       op = DW_OP_mod;
13701       goto do_binop;
13702
13703     case ASHIFT:
13704       op = DW_OP_shl;
13705       goto do_binop;
13706
13707     case ASHIFTRT:
13708       op = DW_OP_shra;
13709       goto do_binop;
13710
13711     case LSHIFTRT:
13712       op = DW_OP_shr;
13713       goto do_binop;
13714
13715     case AND:
13716       op = DW_OP_and;
13717       goto do_binop;
13718
13719     case IOR:
13720       op = DW_OP_or;
13721       goto do_binop;
13722
13723     case XOR:
13724       op = DW_OP_xor;
13725       goto do_binop;
13726
13727     do_binop:
13728       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13729                                 VAR_INIT_STATUS_INITIALIZED);
13730       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13731                                 VAR_INIT_STATUS_INITIALIZED);
13732
13733       if (op0 == 0 || op1 == 0)
13734         break;
13735
13736       mem_loc_result = op0;
13737       add_loc_descr (&mem_loc_result, op1);
13738       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13739       break;
13740
13741     case MOD:
13742       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13743                                 VAR_INIT_STATUS_INITIALIZED);
13744       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13745                                 VAR_INIT_STATUS_INITIALIZED);
13746
13747       if (op0 == 0 || op1 == 0)
13748         break;
13749
13750       mem_loc_result = op0;
13751       add_loc_descr (&mem_loc_result, op1);
13752       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13753       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13754       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13755       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13756       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13757       break;
13758
13759     case NOT:
13760       op = DW_OP_not;
13761       goto do_unop;
13762
13763     case ABS:
13764       op = DW_OP_abs;
13765       goto do_unop;
13766
13767     case NEG:
13768       op = DW_OP_neg;
13769       goto do_unop;
13770
13771     do_unop:
13772       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13773                                 VAR_INIT_STATUS_INITIALIZED);
13774
13775       if (op0 == 0)
13776         break;
13777
13778       mem_loc_result = op0;
13779       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13780       break;
13781
13782     case CONST_INT:
13783       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13784       break;
13785
13786     case EQ:
13787       op = DW_OP_eq;
13788       goto do_scompare;
13789
13790     case GE:
13791       op = DW_OP_ge;
13792       goto do_scompare;
13793
13794     case GT:
13795       op = DW_OP_gt;
13796       goto do_scompare;
13797
13798     case LE:
13799       op = DW_OP_le;
13800       goto do_scompare;
13801
13802     case LT:
13803       op = DW_OP_lt;
13804       goto do_scompare;
13805
13806     case NE:
13807       op = DW_OP_ne;
13808       goto do_scompare;
13809
13810     do_scompare:
13811       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13812           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13813         break;
13814       else
13815         {
13816           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13817
13818           if (op_mode == VOIDmode)
13819             op_mode = GET_MODE (XEXP (rtl, 1));
13820           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13821             break;
13822
13823           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13824                                     VAR_INIT_STATUS_INITIALIZED);
13825           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13826                                     VAR_INIT_STATUS_INITIALIZED);
13827
13828           if (op0 == 0 || op1 == 0)
13829             break;
13830
13831           if (op_mode != VOIDmode
13832               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13833             {
13834               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13835               shift *= BITS_PER_UNIT;
13836               /* For eq/ne, if the operands are known to be zero-extended,
13837                  there is no need to do the fancy shifting up.  */
13838               if (op == DW_OP_eq || op == DW_OP_ne)
13839                 {
13840                   dw_loc_descr_ref last0, last1;
13841                   for (last0 = op0;
13842                        last0->dw_loc_next != NULL;
13843                        last0 = last0->dw_loc_next)
13844                     ;
13845                   for (last1 = op1;
13846                        last1->dw_loc_next != NULL;
13847                        last1 = last1->dw_loc_next)
13848                     ;
13849                   /* deref_size zero extends, and for constants we can check
13850                      whether they are zero extended or not.  */
13851                   if (((last0->dw_loc_opc == DW_OP_deref_size
13852                         && last0->dw_loc_oprnd1.v.val_int
13853                            <= GET_MODE_SIZE (op_mode))
13854                        || (CONST_INT_P (XEXP (rtl, 0))
13855                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13856                                == (INTVAL (XEXP (rtl, 0))
13857                                    & GET_MODE_MASK (op_mode))))
13858                       && ((last1->dw_loc_opc == DW_OP_deref_size
13859                            && last1->dw_loc_oprnd1.v.val_int
13860                               <= GET_MODE_SIZE (op_mode))
13861                           || (CONST_INT_P (XEXP (rtl, 1))
13862                               && (unsigned HOST_WIDE_INT)
13863                                  INTVAL (XEXP (rtl, 1))
13864                                  == (INTVAL (XEXP (rtl, 1))
13865                                      & GET_MODE_MASK (op_mode)))))
13866                     goto do_compare;
13867                 }
13868               add_loc_descr (&op0, int_loc_descriptor (shift));
13869               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13870               if (CONST_INT_P (XEXP (rtl, 1)))
13871                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13872               else
13873                 {
13874                   add_loc_descr (&op1, int_loc_descriptor (shift));
13875                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13876                 }
13877             }
13878         }
13879
13880     do_compare:
13881       mem_loc_result = op0;
13882       add_loc_descr (&mem_loc_result, op1);
13883       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13884       if (STORE_FLAG_VALUE != 1)
13885         {
13886           add_loc_descr (&mem_loc_result,
13887                          int_loc_descriptor (STORE_FLAG_VALUE));
13888           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13889         }
13890       break;
13891
13892     case GEU:
13893       op = DW_OP_ge;
13894       goto do_ucompare;
13895
13896     case GTU:
13897       op = DW_OP_gt;
13898       goto do_ucompare;
13899
13900     case LEU:
13901       op = DW_OP_le;
13902       goto do_ucompare;
13903
13904     case LTU:
13905       op = DW_OP_lt;
13906       goto do_ucompare;
13907
13908     do_ucompare:
13909       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13910           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13911         break;
13912       else
13913         {
13914           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13915
13916           if (op_mode == VOIDmode)
13917             op_mode = GET_MODE (XEXP (rtl, 1));
13918           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13919             break;
13920
13921           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13922                                     VAR_INIT_STATUS_INITIALIZED);
13923           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13924                                     VAR_INIT_STATUS_INITIALIZED);
13925
13926           if (op0 == 0 || op1 == 0)
13927             break;
13928
13929           if (op_mode != VOIDmode
13930               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13931             {
13932               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13933               dw_loc_descr_ref last0, last1;
13934               for (last0 = op0;
13935                    last0->dw_loc_next != NULL;
13936                    last0 = last0->dw_loc_next)
13937                 ;
13938               for (last1 = op1;
13939                    last1->dw_loc_next != NULL;
13940                    last1 = last1->dw_loc_next)
13941                 ;
13942               if (CONST_INT_P (XEXP (rtl, 0)))
13943                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13944               /* deref_size zero extends, so no need to mask it again.  */
13945               else if (last0->dw_loc_opc != DW_OP_deref_size
13946                        || last0->dw_loc_oprnd1.v.val_int
13947                           > GET_MODE_SIZE (op_mode))
13948                 {
13949                   add_loc_descr (&op0, int_loc_descriptor (mask));
13950                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13951                 }
13952               if (CONST_INT_P (XEXP (rtl, 1)))
13953                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13954               /* deref_size zero extends, so no need to mask it again.  */
13955               else if (last1->dw_loc_opc != DW_OP_deref_size
13956                        || last1->dw_loc_oprnd1.v.val_int
13957                           > GET_MODE_SIZE (op_mode))
13958                 {
13959                   add_loc_descr (&op1, int_loc_descriptor (mask));
13960                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13961                 }
13962             }
13963           else
13964             {
13965               HOST_WIDE_INT bias = 1;
13966               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13967               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13968               if (CONST_INT_P (XEXP (rtl, 1)))
13969                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13970                                           + INTVAL (XEXP (rtl, 1)));
13971               else
13972                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13973                                                     bias, 0));
13974             }
13975         }
13976       goto do_compare;
13977
13978     case SMIN:
13979     case SMAX:
13980     case UMIN:
13981     case UMAX:
13982       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13983           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13984           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13985         break;
13986
13987       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13988                                 VAR_INIT_STATUS_INITIALIZED);
13989       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13990                                 VAR_INIT_STATUS_INITIALIZED);
13991
13992       if (op0 == 0 || op1 == 0)
13993         break;
13994
13995       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13996       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13997       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13998       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13999         {
14000           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14001             {
14002               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14003               add_loc_descr (&op0, int_loc_descriptor (mask));
14004               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14005               add_loc_descr (&op1, int_loc_descriptor (mask));
14006               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14007             }
14008           else
14009             {
14010               HOST_WIDE_INT bias = 1;
14011               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14012               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14013               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14014             }
14015         }
14016       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14017         {
14018           int shift = DWARF2_ADDR_SIZE
14019                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14020           shift *= BITS_PER_UNIT;
14021           add_loc_descr (&op0, int_loc_descriptor (shift));
14022           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14023           add_loc_descr (&op1, int_loc_descriptor (shift));
14024           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14025         }
14026
14027       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14028         op = DW_OP_lt;
14029       else
14030         op = DW_OP_gt;
14031       mem_loc_result = op0;
14032       add_loc_descr (&mem_loc_result, op1);
14033       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14034       {
14035         dw_loc_descr_ref bra_node, drop_node;
14036
14037         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14038         add_loc_descr (&mem_loc_result, bra_node);
14039         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14040         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14041         add_loc_descr (&mem_loc_result, drop_node);
14042         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14043         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14044       }
14045       break;
14046
14047     case ZERO_EXTRACT:
14048     case SIGN_EXTRACT:
14049       if (CONST_INT_P (XEXP (rtl, 1))
14050           && CONST_INT_P (XEXP (rtl, 2))
14051           && ((unsigned) INTVAL (XEXP (rtl, 1))
14052               + (unsigned) INTVAL (XEXP (rtl, 2))
14053               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14054           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14055           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14056         {
14057           int shift, size;
14058           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14059                                     VAR_INIT_STATUS_INITIALIZED);
14060           if (op0 == 0)
14061             break;
14062           if (GET_CODE (rtl) == SIGN_EXTRACT)
14063             op = DW_OP_shra;
14064           else
14065             op = DW_OP_shr;
14066           mem_loc_result = op0;
14067           size = INTVAL (XEXP (rtl, 1));
14068           shift = INTVAL (XEXP (rtl, 2));
14069           if (BITS_BIG_ENDIAN)
14070             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14071                     - shift - size;
14072           if (shift + size != (int) DWARF2_ADDR_SIZE)
14073             {
14074               add_loc_descr (&mem_loc_result,
14075                              int_loc_descriptor (DWARF2_ADDR_SIZE
14076                                                  - shift - size));
14077               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14078             }
14079           if (size != (int) DWARF2_ADDR_SIZE)
14080             {
14081               add_loc_descr (&mem_loc_result,
14082                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14083               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14084             }
14085         }
14086       break;
14087
14088     case COMPARE:
14089     case IF_THEN_ELSE:
14090     case ROTATE:
14091     case ROTATERT:
14092     case TRUNCATE:
14093       /* In theory, we could implement the above.  */
14094       /* DWARF cannot represent the unsigned compare operations
14095          natively.  */
14096     case SS_MULT:
14097     case US_MULT:
14098     case SS_DIV:
14099     case US_DIV:
14100     case SS_PLUS:
14101     case US_PLUS:
14102     case SS_MINUS:
14103     case US_MINUS:
14104     case SS_NEG:
14105     case US_NEG:
14106     case SS_ABS:
14107     case SS_ASHIFT:
14108     case US_ASHIFT:
14109     case SS_TRUNCATE:
14110     case US_TRUNCATE:
14111     case UDIV:
14112     case UNORDERED:
14113     case ORDERED:
14114     case UNEQ:
14115     case UNGE:
14116     case UNGT:
14117     case UNLE:
14118     case UNLT:
14119     case LTGT:
14120     case FLOAT_EXTEND:
14121     case FLOAT_TRUNCATE:
14122     case FLOAT:
14123     case UNSIGNED_FLOAT:
14124     case FIX:
14125     case UNSIGNED_FIX:
14126     case FRACT_CONVERT:
14127     case UNSIGNED_FRACT_CONVERT:
14128     case SAT_FRACT:
14129     case UNSIGNED_SAT_FRACT:
14130     case SQRT:
14131     case BSWAP:
14132     case FFS:
14133     case CLZ:
14134     case CTZ:
14135     case POPCOUNT:
14136     case PARITY:
14137     case ASM_OPERANDS:
14138     case VEC_MERGE:
14139     case VEC_SELECT:
14140     case VEC_CONCAT:
14141     case VEC_DUPLICATE:
14142     case UNSPEC:
14143     case HIGH:
14144       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14145          can't express it in the debug info.  This can happen e.g. with some
14146          TLS UNSPECs.  */
14147       break;
14148
14149     case CONST_STRING:
14150       resolve_one_addr (&rtl, NULL);
14151       goto symref;
14152
14153     default:
14154 #ifdef ENABLE_CHECKING
14155       print_rtl (stderr, rtl);
14156       gcc_unreachable ();
14157 #else
14158       break;
14159 #endif
14160     }
14161
14162   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14163     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14164
14165   return mem_loc_result;
14166 }
14167
14168 /* Return a descriptor that describes the concatenation of two locations.
14169    This is typically a complex variable.  */
14170
14171 static dw_loc_descr_ref
14172 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14173 {
14174   dw_loc_descr_ref cc_loc_result = NULL;
14175   dw_loc_descr_ref x0_ref
14176     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14177   dw_loc_descr_ref x1_ref
14178     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14179
14180   if (x0_ref == 0 || x1_ref == 0)
14181     return 0;
14182
14183   cc_loc_result = x0_ref;
14184   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14185
14186   add_loc_descr (&cc_loc_result, x1_ref);
14187   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14188
14189   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14190     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14191
14192   return cc_loc_result;
14193 }
14194
14195 /* Return a descriptor that describes the concatenation of N
14196    locations.  */
14197
14198 static dw_loc_descr_ref
14199 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14200 {
14201   unsigned int i;
14202   dw_loc_descr_ref cc_loc_result = NULL;
14203   unsigned int n = XVECLEN (concatn, 0);
14204
14205   for (i = 0; i < n; ++i)
14206     {
14207       dw_loc_descr_ref ref;
14208       rtx x = XVECEXP (concatn, 0, i);
14209
14210       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14211       if (ref == NULL)
14212         return NULL;
14213
14214       add_loc_descr (&cc_loc_result, ref);
14215       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14216     }
14217
14218   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14219     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14220
14221   return cc_loc_result;
14222 }
14223
14224 /* Output a proper Dwarf location descriptor for a variable or parameter
14225    which is either allocated in a register or in a memory location.  For a
14226    register, we just generate an OP_REG and the register number.  For a
14227    memory location we provide a Dwarf postfix expression describing how to
14228    generate the (dynamic) address of the object onto the address stack.
14229
14230    MODE is mode of the decl if this loc_descriptor is going to be used in
14231    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14232    allowed, VOIDmode otherwise.
14233
14234    If we don't know how to describe it, return 0.  */
14235
14236 static dw_loc_descr_ref
14237 loc_descriptor (rtx rtl, enum machine_mode mode,
14238                 enum var_init_status initialized)
14239 {
14240   dw_loc_descr_ref loc_result = NULL;
14241
14242   switch (GET_CODE (rtl))
14243     {
14244     case SUBREG:
14245       /* The case of a subreg may arise when we have a local (register)
14246          variable or a formal (register) parameter which doesn't quite fill
14247          up an entire register.  For now, just assume that it is
14248          legitimate to make the Dwarf info refer to the whole register which
14249          contains the given subreg.  */
14250       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14251       break;
14252
14253     case REG:
14254       loc_result = reg_loc_descriptor (rtl, initialized);
14255       break;
14256
14257     case MEM:
14258       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14259                                        initialized);
14260       if (loc_result == NULL)
14261         loc_result = tls_mem_loc_descriptor (rtl);
14262       if (loc_result == NULL)
14263         {
14264           rtx new_rtl = avoid_constant_pool_reference (rtl);
14265           if (new_rtl != rtl)
14266             loc_result = loc_descriptor (new_rtl, mode, initialized);
14267         }
14268       break;
14269
14270     case CONCAT:
14271       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14272                                           initialized);
14273       break;
14274
14275     case CONCATN:
14276       loc_result = concatn_loc_descriptor (rtl, initialized);
14277       break;
14278
14279     case VAR_LOCATION:
14280       /* Single part.  */
14281       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14282         {
14283           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14284           if (GET_CODE (loc) == EXPR_LIST)
14285             loc = XEXP (loc, 0);
14286           loc_result = loc_descriptor (loc, mode, initialized);
14287           break;
14288         }
14289
14290       rtl = XEXP (rtl, 1);
14291       /* FALLTHRU */
14292
14293     case PARALLEL:
14294       {
14295         rtvec par_elems = XVEC (rtl, 0);
14296         int num_elem = GET_NUM_ELEM (par_elems);
14297         enum machine_mode mode;
14298         int i;
14299
14300         /* Create the first one, so we have something to add to.  */
14301         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14302                                      VOIDmode, initialized);
14303         if (loc_result == NULL)
14304           return NULL;
14305         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14306         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14307         for (i = 1; i < num_elem; i++)
14308           {
14309             dw_loc_descr_ref temp;
14310
14311             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14312                                    VOIDmode, initialized);
14313             if (temp == NULL)
14314               return NULL;
14315             add_loc_descr (&loc_result, temp);
14316             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14317             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14318           }
14319       }
14320       break;
14321
14322     case CONST_INT:
14323       if (mode != VOIDmode && mode != BLKmode)
14324         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14325                                                     INTVAL (rtl));
14326       break;
14327
14328     case CONST_DOUBLE:
14329       if (mode == VOIDmode)
14330         mode = GET_MODE (rtl);
14331
14332       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14333         {
14334           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14335
14336           /* Note that a CONST_DOUBLE rtx could represent either an integer
14337              or a floating-point constant.  A CONST_DOUBLE is used whenever
14338              the constant requires more than one word in order to be
14339              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14340           loc_result = new_loc_descr (DW_OP_implicit_value,
14341                                       GET_MODE_SIZE (mode), 0);
14342           if (SCALAR_FLOAT_MODE_P (mode))
14343             {
14344               unsigned int length = GET_MODE_SIZE (mode);
14345               unsigned char *array
14346                   = (unsigned char*) ggc_alloc_atomic (length);
14347
14348               insert_float (rtl, array);
14349               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14350               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14351               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14352               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14353             }
14354           else
14355             {
14356               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14357               loc_result->dw_loc_oprnd2.v.val_double
14358                 = rtx_to_double_int (rtl);
14359             }
14360         }
14361       break;
14362
14363     case CONST_VECTOR:
14364       if (mode == VOIDmode)
14365         mode = GET_MODE (rtl);
14366
14367       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14368         {
14369           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14370           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14371           unsigned char *array = (unsigned char *)
14372             ggc_alloc_atomic (length * elt_size);
14373           unsigned int i;
14374           unsigned char *p;
14375
14376           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14377           switch (GET_MODE_CLASS (mode))
14378             {
14379             case MODE_VECTOR_INT:
14380               for (i = 0, p = array; i < length; i++, p += elt_size)
14381                 {
14382                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14383                   double_int val = rtx_to_double_int (elt);
14384
14385                   if (elt_size <= sizeof (HOST_WIDE_INT))
14386                     insert_int (double_int_to_shwi (val), elt_size, p);
14387                   else
14388                     {
14389                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14390                       insert_double (val, p);
14391                     }
14392                 }
14393               break;
14394
14395             case MODE_VECTOR_FLOAT:
14396               for (i = 0, p = array; i < length; i++, p += elt_size)
14397                 {
14398                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14399                   insert_float (elt, p);
14400                 }
14401               break;
14402
14403             default:
14404               gcc_unreachable ();
14405             }
14406
14407           loc_result = new_loc_descr (DW_OP_implicit_value,
14408                                       length * elt_size, 0);
14409           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14410           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14411           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14412           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14413         }
14414       break;
14415
14416     case CONST:
14417       if (mode == VOIDmode
14418           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14419           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14420           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14421         {
14422           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14423           break;
14424         }
14425       /* FALLTHROUGH */
14426     case SYMBOL_REF:
14427       if (!const_ok_for_output (rtl))
14428         break;
14429     case LABEL_REF:
14430       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14431           && (dwarf_version >= 4 || !dwarf_strict))
14432         {
14433           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14434           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14435           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14436           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14437           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14438         }
14439       break;
14440
14441     default:
14442       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14443           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14444           && (dwarf_version >= 4 || !dwarf_strict))
14445         {
14446           /* Value expression.  */
14447           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14448           if (loc_result)
14449             add_loc_descr (&loc_result,
14450                            new_loc_descr (DW_OP_stack_value, 0, 0));
14451         }
14452       break;
14453     }
14454
14455   return loc_result;
14456 }
14457
14458 /* We need to figure out what section we should use as the base for the
14459    address ranges where a given location is valid.
14460    1. If this particular DECL has a section associated with it, use that.
14461    2. If this function has a section associated with it, use that.
14462    3. Otherwise, use the text section.
14463    XXX: If you split a variable across multiple sections, we won't notice.  */
14464
14465 static const char *
14466 secname_for_decl (const_tree decl)
14467 {
14468   const char *secname;
14469
14470   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14471     {
14472       tree sectree = DECL_SECTION_NAME (decl);
14473       secname = TREE_STRING_POINTER (sectree);
14474     }
14475   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14476     {
14477       tree sectree = DECL_SECTION_NAME (current_function_decl);
14478       secname = TREE_STRING_POINTER (sectree);
14479     }
14480   else if (cfun && in_cold_section_p)
14481     secname = crtl->subsections.cold_section_label;
14482   else
14483     secname = text_section_label;
14484
14485   return secname;
14486 }
14487
14488 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14489
14490 static bool
14491 decl_by_reference_p (tree decl)
14492 {
14493   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14494            || TREE_CODE (decl) == VAR_DECL)
14495           && DECL_BY_REFERENCE (decl));
14496 }
14497
14498 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14499    for VARLOC.  */
14500
14501 static dw_loc_descr_ref
14502 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14503                enum var_init_status initialized)
14504 {
14505   int have_address = 0;
14506   dw_loc_descr_ref descr;
14507   enum machine_mode mode;
14508
14509   if (want_address != 2)
14510     {
14511       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14512       /* Single part.  */
14513       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14514         {
14515           varloc = PAT_VAR_LOCATION_LOC (varloc);
14516           if (GET_CODE (varloc) == EXPR_LIST)
14517             varloc = XEXP (varloc, 0);
14518           mode = GET_MODE (varloc);
14519           if (MEM_P (varloc))
14520             {
14521               rtx addr = XEXP (varloc, 0);
14522               descr = mem_loc_descriptor (addr, mode, initialized);
14523               if (descr)
14524                 have_address = 1;
14525               else
14526                 {
14527                   rtx x = avoid_constant_pool_reference (varloc);
14528                   if (x != varloc)
14529                     descr = mem_loc_descriptor (x, mode, initialized);
14530                 }
14531             }
14532           else
14533             descr = mem_loc_descriptor (varloc, mode, initialized);
14534         }
14535       else
14536         return 0;
14537     }
14538   else
14539     {
14540       if (GET_CODE (varloc) == VAR_LOCATION)
14541         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14542       else
14543         mode = DECL_MODE (loc);
14544       descr = loc_descriptor (varloc, mode, initialized);
14545       have_address = 1;
14546     }
14547
14548   if (!descr)
14549     return 0;
14550
14551   if (want_address == 2 && !have_address
14552       && (dwarf_version >= 4 || !dwarf_strict))
14553     {
14554       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14555         {
14556           expansion_failed (loc, NULL_RTX,
14557                             "DWARF address size mismatch");
14558           return 0;
14559         }
14560       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14561       have_address = 1;
14562     }
14563   /* Show if we can't fill the request for an address.  */
14564   if (want_address && !have_address)
14565     {
14566       expansion_failed (loc, NULL_RTX,
14567                         "Want address and only have value");
14568       return 0;
14569     }
14570
14571   /* If we've got an address and don't want one, dereference.  */
14572   if (!want_address && have_address)
14573     {
14574       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14575       enum dwarf_location_atom op;
14576
14577       if (size > DWARF2_ADDR_SIZE || size == -1)
14578         {
14579           expansion_failed (loc, NULL_RTX,
14580                             "DWARF address size mismatch");
14581           return 0;
14582         }
14583       else if (size == DWARF2_ADDR_SIZE)
14584         op = DW_OP_deref;
14585       else
14586         op = DW_OP_deref_size;
14587
14588       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14589     }
14590
14591   return descr;
14592 }
14593
14594 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14595    if it is not possible.  */
14596
14597 static dw_loc_descr_ref
14598 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14599 {
14600   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14601     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14602   else if (dwarf_version >= 3 || !dwarf_strict)
14603     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14604   else
14605     return NULL;
14606 }
14607
14608 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14609    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14610
14611 static dw_loc_descr_ref
14612 dw_sra_loc_expr (tree decl, rtx loc)
14613 {
14614   rtx p;
14615   unsigned int padsize = 0;
14616   dw_loc_descr_ref descr, *descr_tail;
14617   unsigned HOST_WIDE_INT decl_size;
14618   rtx varloc;
14619   enum var_init_status initialized;
14620
14621   if (DECL_SIZE (decl) == NULL
14622       || !host_integerp (DECL_SIZE (decl), 1))
14623     return NULL;
14624
14625   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14626   descr = NULL;
14627   descr_tail = &descr;
14628
14629   for (p = loc; p; p = XEXP (p, 1))
14630     {
14631       unsigned int bitsize = decl_piece_bitsize (p);
14632       rtx loc_note = *decl_piece_varloc_ptr (p);
14633       dw_loc_descr_ref cur_descr;
14634       dw_loc_descr_ref *tail, last = NULL;
14635       unsigned int opsize = 0;
14636
14637       if (loc_note == NULL_RTX
14638           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14639         {
14640           padsize += bitsize;
14641           continue;
14642         }
14643       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14644       varloc = NOTE_VAR_LOCATION (loc_note);
14645       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14646       if (cur_descr == NULL)
14647         {
14648           padsize += bitsize;
14649           continue;
14650         }
14651
14652       /* Check that cur_descr either doesn't use
14653          DW_OP_*piece operations, or their sum is equal
14654          to bitsize.  Otherwise we can't embed it.  */
14655       for (tail = &cur_descr; *tail != NULL;
14656            tail = &(*tail)->dw_loc_next)
14657         if ((*tail)->dw_loc_opc == DW_OP_piece)
14658           {
14659             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14660                       * BITS_PER_UNIT;
14661             last = *tail;
14662           }
14663         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14664           {
14665             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14666             last = *tail;
14667           }
14668
14669       if (last != NULL && opsize != bitsize)
14670         {
14671           padsize += bitsize;
14672           continue;
14673         }
14674
14675       /* If there is a hole, add DW_OP_*piece after empty DWARF
14676          expression, which means that those bits are optimized out.  */
14677       if (padsize)
14678         {
14679           if (padsize > decl_size)
14680             return NULL;
14681           decl_size -= padsize;
14682           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14683           if (*descr_tail == NULL)
14684             return NULL;
14685           descr_tail = &(*descr_tail)->dw_loc_next;
14686           padsize = 0;
14687         }
14688       *descr_tail = cur_descr;
14689       descr_tail = tail;
14690       if (bitsize > decl_size)
14691         return NULL;
14692       decl_size -= bitsize;
14693       if (last == NULL)
14694         {
14695           HOST_WIDE_INT offset = 0;
14696           if (GET_CODE (varloc) == VAR_LOCATION
14697               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14698             {
14699               varloc = PAT_VAR_LOCATION_LOC (varloc);
14700               if (GET_CODE (varloc) == EXPR_LIST)
14701                 varloc = XEXP (varloc, 0);
14702             }
14703           do 
14704             {
14705               if (GET_CODE (varloc) == CONST
14706                   || GET_CODE (varloc) == SIGN_EXTEND
14707                   || GET_CODE (varloc) == ZERO_EXTEND)
14708                 varloc = XEXP (varloc, 0);
14709               else if (GET_CODE (varloc) == SUBREG)
14710                 varloc = SUBREG_REG (varloc);
14711               else
14712                 break;
14713             }
14714           while (1);
14715           /* DW_OP_bit_size offset should be zero for register
14716              or implicit location descriptions and empty location
14717              descriptions, but for memory addresses needs big endian
14718              adjustment.  */
14719           if (MEM_P (varloc))
14720             {
14721               unsigned HOST_WIDE_INT memsize
14722                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14723               if (memsize != bitsize)
14724                 {
14725                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14726                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14727                     return NULL;
14728                   if (memsize < bitsize)
14729                     return NULL;
14730                   if (BITS_BIG_ENDIAN)
14731                     offset = memsize - bitsize;
14732                 }
14733             }
14734
14735           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14736           if (*descr_tail == NULL)
14737             return NULL;
14738           descr_tail = &(*descr_tail)->dw_loc_next;
14739         }
14740     }
14741
14742   /* If there were any non-empty expressions, add padding till the end of
14743      the decl.  */
14744   if (descr != NULL && decl_size != 0)
14745     {
14746       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14747       if (*descr_tail == NULL)
14748         return NULL;
14749     }
14750   return descr;
14751 }
14752
14753 /* Return the dwarf representation of the location list LOC_LIST of
14754    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14755    function.  */
14756
14757 static dw_loc_list_ref
14758 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14759 {
14760   const char *endname, *secname;
14761   rtx varloc;
14762   enum var_init_status initialized;
14763   struct var_loc_node *node;
14764   dw_loc_descr_ref descr;
14765   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14766   dw_loc_list_ref list = NULL;
14767   dw_loc_list_ref *listp = &list;
14768
14769   /* Now that we know what section we are using for a base,
14770      actually construct the list of locations.
14771      The first location information is what is passed to the
14772      function that creates the location list, and the remaining
14773      locations just get added on to that list.
14774      Note that we only know the start address for a location
14775      (IE location changes), so to build the range, we use
14776      the range [current location start, next location start].
14777      This means we have to special case the last node, and generate
14778      a range of [last location start, end of function label].  */
14779
14780   secname = secname_for_decl (decl);
14781
14782   for (node = loc_list->first; node; node = node->next)
14783     if (GET_CODE (node->loc) == EXPR_LIST
14784         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14785       {
14786         if (GET_CODE (node->loc) == EXPR_LIST)
14787           {
14788             /* This requires DW_OP_{,bit_}piece, which is not usable
14789                inside DWARF expressions.  */
14790             if (want_address != 2)
14791               continue;
14792             descr = dw_sra_loc_expr (decl, node->loc);
14793             if (descr == NULL)
14794               continue;
14795           }
14796         else
14797           {
14798             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14799             varloc = NOTE_VAR_LOCATION (node->loc);
14800             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14801           }
14802         if (descr)
14803           {
14804             /* The variable has a location between NODE->LABEL and
14805                NODE->NEXT->LABEL.  */
14806             if (node->next)
14807               endname = node->next->label;
14808             /* If the variable has a location at the last label
14809                it keeps its location until the end of function.  */
14810             else if (!current_function_decl)
14811               endname = text_end_label;
14812             else
14813               {
14814                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14815                                              current_function_funcdef_no);
14816                 endname = ggc_strdup (label_id);
14817               }
14818
14819             *listp = new_loc_list (descr, node->label, endname, secname);
14820             listp = &(*listp)->dw_loc_next;
14821           }
14822       }
14823
14824   /* Try to avoid the overhead of a location list emitting a location
14825      expression instead, but only if we didn't have more than one
14826      location entry in the first place.  If some entries were not
14827      representable, we don't want to pretend a single entry that was
14828      applies to the entire scope in which the variable is
14829      available.  */
14830   if (list && loc_list->first->next)
14831     gen_llsym (list);
14832
14833   return list;
14834 }
14835
14836 /* Return if the loc_list has only single element and thus can be represented
14837    as location description.   */
14838
14839 static bool
14840 single_element_loc_list_p (dw_loc_list_ref list)
14841 {
14842   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14843   return !list->ll_symbol;
14844 }
14845
14846 /* To each location in list LIST add loc descr REF.  */
14847
14848 static void
14849 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14850 {
14851   dw_loc_descr_ref copy;
14852   add_loc_descr (&list->expr, ref);
14853   list = list->dw_loc_next;
14854   while (list)
14855     {
14856       copy = ggc_alloc_dw_loc_descr_node ();
14857       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14858       add_loc_descr (&list->expr, copy);
14859       while (copy->dw_loc_next)
14860         {
14861           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14862           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14863           copy->dw_loc_next = new_copy;
14864           copy = new_copy;
14865         }
14866       list = list->dw_loc_next;
14867     }
14868 }
14869
14870 /* Given two lists RET and LIST
14871    produce location list that is result of adding expression in LIST
14872    to expression in RET on each possition in program.
14873    Might be destructive on both RET and LIST.
14874
14875    TODO: We handle only simple cases of RET or LIST having at most one
14876    element. General case would inolve sorting the lists in program order
14877    and merging them that will need some additional work.
14878    Adding that will improve quality of debug info especially for SRA-ed
14879    structures.  */
14880
14881 static void
14882 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14883 {
14884   if (!list)
14885     return;
14886   if (!*ret)
14887     {
14888       *ret = list;
14889       return;
14890     }
14891   if (!list->dw_loc_next)
14892     {
14893       add_loc_descr_to_each (*ret, list->expr);
14894       return;
14895     }
14896   if (!(*ret)->dw_loc_next)
14897     {
14898       add_loc_descr_to_each (list, (*ret)->expr);
14899       *ret = list;
14900       return;
14901     }
14902   expansion_failed (NULL_TREE, NULL_RTX,
14903                     "Don't know how to merge two non-trivial"
14904                     " location lists.\n");
14905   *ret = NULL;
14906   return;
14907 }
14908
14909 /* LOC is constant expression.  Try a luck, look it up in constant
14910    pool and return its loc_descr of its address.  */
14911
14912 static dw_loc_descr_ref
14913 cst_pool_loc_descr (tree loc)
14914 {
14915   /* Get an RTL for this, if something has been emitted.  */
14916   rtx rtl = lookup_constant_def (loc);
14917   enum machine_mode mode;
14918
14919   if (!rtl || !MEM_P (rtl))
14920     {
14921       gcc_assert (!rtl);
14922       return 0;
14923     }
14924   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14925
14926   /* TODO: We might get more coverage if we was actually delaying expansion
14927      of all expressions till end of compilation when constant pools are fully
14928      populated.  */
14929   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14930     {
14931       expansion_failed (loc, NULL_RTX,
14932                         "CST value in contant pool but not marked.");
14933       return 0;
14934     }
14935   mode = GET_MODE (rtl);
14936   rtl = XEXP (rtl, 0);
14937   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14938 }
14939
14940 /* Return dw_loc_list representing address of addr_expr LOC
14941    by looking for innder INDIRECT_REF expression and turing it
14942    into simple arithmetics.  */
14943
14944 static dw_loc_list_ref
14945 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14946 {
14947   tree obj, offset;
14948   HOST_WIDE_INT bitsize, bitpos, bytepos;
14949   enum machine_mode mode;
14950   int volatilep;
14951   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14952   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14953
14954   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14955                              &bitsize, &bitpos, &offset, &mode,
14956                              &unsignedp, &volatilep, false);
14957   STRIP_NOPS (obj);
14958   if (bitpos % BITS_PER_UNIT)
14959     {
14960       expansion_failed (loc, NULL_RTX, "bitfield access");
14961       return 0;
14962     }
14963   if (!INDIRECT_REF_P (obj))
14964     {
14965       expansion_failed (obj,
14966                         NULL_RTX, "no indirect ref in inner refrence");
14967       return 0;
14968     }
14969   if (!offset && !bitpos)
14970     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14971   else if (toplev
14972            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14973            && (dwarf_version >= 4 || !dwarf_strict))
14974     {
14975       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14976       if (!list_ret)
14977         return 0;
14978       if (offset)
14979         {
14980           /* Variable offset.  */
14981           list_ret1 = loc_list_from_tree (offset, 0);
14982           if (list_ret1 == 0)
14983             return 0;
14984           add_loc_list (&list_ret, list_ret1);
14985           if (!list_ret)
14986             return 0;
14987           add_loc_descr_to_each (list_ret,
14988                                  new_loc_descr (DW_OP_plus, 0, 0));
14989         }
14990       bytepos = bitpos / BITS_PER_UNIT;
14991       if (bytepos > 0)
14992         add_loc_descr_to_each (list_ret,
14993                                new_loc_descr (DW_OP_plus_uconst,
14994                                               bytepos, 0));
14995       else if (bytepos < 0)
14996         loc_list_plus_const (list_ret, bytepos);
14997       add_loc_descr_to_each (list_ret,
14998                              new_loc_descr (DW_OP_stack_value, 0, 0));
14999     }
15000   return list_ret;
15001 }
15002
15003
15004 /* Generate Dwarf location list representing LOC.
15005    If WANT_ADDRESS is false, expression computing LOC will be computed
15006    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15007    if WANT_ADDRESS is 2, expression computing address useable in location
15008      will be returned (i.e. DW_OP_reg can be used
15009      to refer to register values).  */
15010
15011 static dw_loc_list_ref
15012 loc_list_from_tree (tree loc, int want_address)
15013 {
15014   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15015   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15016   int have_address = 0;
15017   enum dwarf_location_atom op;
15018
15019   /* ??? Most of the time we do not take proper care for sign/zero
15020      extending the values properly.  Hopefully this won't be a real
15021      problem...  */
15022
15023   switch (TREE_CODE (loc))
15024     {
15025     case ERROR_MARK:
15026       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15027       return 0;
15028
15029     case PLACEHOLDER_EXPR:
15030       /* This case involves extracting fields from an object to determine the
15031          position of other fields.  We don't try to encode this here.  The
15032          only user of this is Ada, which encodes the needed information using
15033          the names of types.  */
15034       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15035       return 0;
15036
15037     case CALL_EXPR:
15038       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15039       /* There are no opcodes for these operations.  */
15040       return 0;
15041
15042     case PREINCREMENT_EXPR:
15043     case PREDECREMENT_EXPR:
15044     case POSTINCREMENT_EXPR:
15045     case POSTDECREMENT_EXPR:
15046       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15047       /* There are no opcodes for these operations.  */
15048       return 0;
15049
15050     case ADDR_EXPR:
15051       /* If we already want an address, see if there is INDIRECT_REF inside
15052          e.g. for &this->field.  */
15053       if (want_address)
15054         {
15055           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15056                        (loc, want_address == 2);
15057           if (list_ret)
15058             have_address = 1;
15059           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15060                    && (ret = cst_pool_loc_descr (loc)))
15061             have_address = 1;
15062         }
15063         /* Otherwise, process the argument and look for the address.  */
15064       if (!list_ret && !ret)
15065         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15066       else
15067         {
15068           if (want_address)
15069             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15070           return NULL;
15071         }
15072       break;
15073
15074     case VAR_DECL:
15075       if (DECL_THREAD_LOCAL_P (loc))
15076         {
15077           rtx rtl;
15078           enum dwarf_location_atom first_op;
15079           enum dwarf_location_atom second_op;
15080           bool dtprel = false;
15081
15082           if (targetm.have_tls)
15083             {
15084               /* If this is not defined, we have no way to emit the
15085                  data.  */
15086               if (!targetm.asm_out.output_dwarf_dtprel)
15087                 return 0;
15088
15089                /* The way DW_OP_GNU_push_tls_address is specified, we
15090                   can only look up addresses of objects in the current
15091                   module.  We used DW_OP_addr as first op, but that's
15092                   wrong, because DW_OP_addr is relocated by the debug
15093                   info consumer, while DW_OP_GNU_push_tls_address
15094                   operand shouldn't be.  */
15095               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15096                 return 0;
15097               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15098               dtprel = true;
15099               second_op = DW_OP_GNU_push_tls_address;
15100             }
15101           else
15102             {
15103               if (!targetm.emutls.debug_form_tls_address
15104                   || !(dwarf_version >= 3 || !dwarf_strict))
15105                 return 0;
15106               loc = emutls_decl (loc);
15107               first_op = DW_OP_addr;
15108               second_op = DW_OP_form_tls_address;
15109             }
15110
15111           rtl = rtl_for_decl_location (loc);
15112           if (rtl == NULL_RTX)
15113             return 0;
15114
15115           if (!MEM_P (rtl))
15116             return 0;
15117           rtl = XEXP (rtl, 0);
15118           if (! CONSTANT_P (rtl))
15119             return 0;
15120
15121           ret = new_loc_descr (first_op, 0, 0);
15122           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15123           ret->dw_loc_oprnd1.v.val_addr = rtl;
15124           ret->dtprel = dtprel;
15125
15126           ret1 = new_loc_descr (second_op, 0, 0);
15127           add_loc_descr (&ret, ret1);
15128
15129           have_address = 1;
15130           break;
15131         }
15132       /* FALLTHRU */
15133
15134     case PARM_DECL:
15135       if (DECL_HAS_VALUE_EXPR_P (loc))
15136         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15137                                    want_address);
15138       /* FALLTHRU */
15139
15140     case RESULT_DECL:
15141     case FUNCTION_DECL:
15142       {
15143         rtx rtl;
15144         var_loc_list *loc_list = lookup_decl_loc (loc);
15145
15146         if (loc_list && loc_list->first)
15147           {
15148             list_ret = dw_loc_list (loc_list, loc, want_address);
15149             have_address = want_address != 0;
15150             break;
15151           }
15152         rtl = rtl_for_decl_location (loc);
15153         if (rtl == NULL_RTX)
15154           {
15155             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15156             return 0;
15157           }
15158         else if (CONST_INT_P (rtl))
15159           {
15160             HOST_WIDE_INT val = INTVAL (rtl);
15161             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15162               val &= GET_MODE_MASK (DECL_MODE (loc));
15163             ret = int_loc_descriptor (val);
15164           }
15165         else if (GET_CODE (rtl) == CONST_STRING)
15166           {
15167             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15168             return 0;
15169           }
15170         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15171           {
15172             ret = new_loc_descr (DW_OP_addr, 0, 0);
15173             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15174             ret->dw_loc_oprnd1.v.val_addr = rtl;
15175           }
15176         else
15177           {
15178             enum machine_mode mode;
15179
15180             /* Certain constructs can only be represented at top-level.  */
15181             if (want_address == 2)
15182               {
15183                 ret = loc_descriptor (rtl, VOIDmode,
15184                                       VAR_INIT_STATUS_INITIALIZED);
15185                 have_address = 1;
15186               }
15187             else
15188               {
15189                 mode = GET_MODE (rtl);
15190                 if (MEM_P (rtl))
15191                   {
15192                     rtl = XEXP (rtl, 0);
15193                     have_address = 1;
15194                   }
15195                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15196               }
15197             if (!ret)
15198               expansion_failed (loc, rtl,
15199                                 "failed to produce loc descriptor for rtl");
15200           }
15201       }
15202       break;
15203
15204     case MEM_REF:
15205       /* ??? FIXME.  */
15206       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15207         return 0;
15208       /* Fallthru.  */
15209     case INDIRECT_REF:
15210     case MISALIGNED_INDIRECT_REF:
15211       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15212       have_address = 1;
15213       break;
15214
15215     case COMPOUND_EXPR:
15216       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15217
15218     CASE_CONVERT:
15219     case VIEW_CONVERT_EXPR:
15220     case SAVE_EXPR:
15221     case MODIFY_EXPR:
15222       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15223
15224     case COMPONENT_REF:
15225     case BIT_FIELD_REF:
15226     case ARRAY_REF:
15227     case ARRAY_RANGE_REF:
15228     case REALPART_EXPR:
15229     case IMAGPART_EXPR:
15230       {
15231         tree obj, offset;
15232         HOST_WIDE_INT bitsize, bitpos, bytepos;
15233         enum machine_mode mode;
15234         int volatilep;
15235         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15236
15237         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15238                                    &unsignedp, &volatilep, false);
15239
15240         gcc_assert (obj != loc);
15241
15242         list_ret = loc_list_from_tree (obj,
15243                                        want_address == 2
15244                                        && !bitpos && !offset ? 2 : 1);
15245         /* TODO: We can extract value of the small expression via shifting even
15246            for nonzero bitpos.  */
15247         if (list_ret == 0)
15248           return 0;
15249         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15250           {
15251             expansion_failed (loc, NULL_RTX,
15252                               "bitfield access");
15253             return 0;
15254           }
15255
15256         if (offset != NULL_TREE)
15257           {
15258             /* Variable offset.  */
15259             list_ret1 = loc_list_from_tree (offset, 0);
15260             if (list_ret1 == 0)
15261               return 0;
15262             add_loc_list (&list_ret, list_ret1);
15263             if (!list_ret)
15264               return 0;
15265             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15266           }
15267
15268         bytepos = bitpos / BITS_PER_UNIT;
15269         if (bytepos > 0)
15270           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15271         else if (bytepos < 0)
15272           loc_list_plus_const (list_ret, bytepos);
15273
15274         have_address = 1;
15275         break;
15276       }
15277
15278     case INTEGER_CST:
15279       if ((want_address || !host_integerp (loc, 0))
15280           && (ret = cst_pool_loc_descr (loc)))
15281         have_address = 1;
15282       else if (want_address == 2
15283                && host_integerp (loc, 0)
15284                && (ret = address_of_int_loc_descriptor
15285                            (int_size_in_bytes (TREE_TYPE (loc)),
15286                             tree_low_cst (loc, 0))))
15287         have_address = 1;
15288       else if (host_integerp (loc, 0))
15289         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15290       else
15291         {
15292           expansion_failed (loc, NULL_RTX,
15293                             "Integer operand is not host integer");
15294           return 0;
15295         }
15296       break;
15297
15298     case CONSTRUCTOR:
15299     case REAL_CST:
15300     case STRING_CST:
15301     case COMPLEX_CST:
15302       if ((ret = cst_pool_loc_descr (loc)))
15303         have_address = 1;
15304       else
15305       /* We can construct small constants here using int_loc_descriptor.  */
15306         expansion_failed (loc, NULL_RTX,
15307                           "constructor or constant not in constant pool");
15308       break;
15309
15310     case TRUTH_AND_EXPR:
15311     case TRUTH_ANDIF_EXPR:
15312     case BIT_AND_EXPR:
15313       op = DW_OP_and;
15314       goto do_binop;
15315
15316     case TRUTH_XOR_EXPR:
15317     case BIT_XOR_EXPR:
15318       op = DW_OP_xor;
15319       goto do_binop;
15320
15321     case TRUTH_OR_EXPR:
15322     case TRUTH_ORIF_EXPR:
15323     case BIT_IOR_EXPR:
15324       op = DW_OP_or;
15325       goto do_binop;
15326
15327     case FLOOR_DIV_EXPR:
15328     case CEIL_DIV_EXPR:
15329     case ROUND_DIV_EXPR:
15330     case TRUNC_DIV_EXPR:
15331       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15332         return 0;
15333       op = DW_OP_div;
15334       goto do_binop;
15335
15336     case MINUS_EXPR:
15337       op = DW_OP_minus;
15338       goto do_binop;
15339
15340     case FLOOR_MOD_EXPR:
15341     case CEIL_MOD_EXPR:
15342     case ROUND_MOD_EXPR:
15343     case TRUNC_MOD_EXPR:
15344       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15345         {
15346           op = DW_OP_mod;
15347           goto do_binop;
15348         }
15349       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15350       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15351       if (list_ret == 0 || list_ret1 == 0)
15352         return 0;
15353
15354       add_loc_list (&list_ret, list_ret1);
15355       if (list_ret == 0)
15356         return 0;
15357       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15358       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15359       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15360       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15361       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15362       break;
15363
15364     case MULT_EXPR:
15365       op = DW_OP_mul;
15366       goto do_binop;
15367
15368     case LSHIFT_EXPR:
15369       op = DW_OP_shl;
15370       goto do_binop;
15371
15372     case RSHIFT_EXPR:
15373       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15374       goto do_binop;
15375
15376     case POINTER_PLUS_EXPR:
15377     case PLUS_EXPR:
15378       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15379         {
15380           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15381           if (list_ret == 0)
15382             return 0;
15383
15384           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15385           break;
15386         }
15387
15388       op = DW_OP_plus;
15389       goto do_binop;
15390
15391     case LE_EXPR:
15392       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15393         return 0;
15394
15395       op = DW_OP_le;
15396       goto do_binop;
15397
15398     case GE_EXPR:
15399       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15400         return 0;
15401
15402       op = DW_OP_ge;
15403       goto do_binop;
15404
15405     case LT_EXPR:
15406       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15407         return 0;
15408
15409       op = DW_OP_lt;
15410       goto do_binop;
15411
15412     case GT_EXPR:
15413       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15414         return 0;
15415
15416       op = DW_OP_gt;
15417       goto do_binop;
15418
15419     case EQ_EXPR:
15420       op = DW_OP_eq;
15421       goto do_binop;
15422
15423     case NE_EXPR:
15424       op = DW_OP_ne;
15425       goto do_binop;
15426
15427     do_binop:
15428       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15429       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15430       if (list_ret == 0 || list_ret1 == 0)
15431         return 0;
15432
15433       add_loc_list (&list_ret, list_ret1);
15434       if (list_ret == 0)
15435         return 0;
15436       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15437       break;
15438
15439     case TRUTH_NOT_EXPR:
15440     case BIT_NOT_EXPR:
15441       op = DW_OP_not;
15442       goto do_unop;
15443
15444     case ABS_EXPR:
15445       op = DW_OP_abs;
15446       goto do_unop;
15447
15448     case NEGATE_EXPR:
15449       op = DW_OP_neg;
15450       goto do_unop;
15451
15452     do_unop:
15453       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15454       if (list_ret == 0)
15455         return 0;
15456
15457       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15458       break;
15459
15460     case MIN_EXPR:
15461     case MAX_EXPR:
15462       {
15463         const enum tree_code code =
15464           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15465
15466         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15467                       build2 (code, integer_type_node,
15468                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15469                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15470       }
15471
15472       /* ... fall through ...  */
15473
15474     case COND_EXPR:
15475       {
15476         dw_loc_descr_ref lhs
15477           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15478         dw_loc_list_ref rhs
15479           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15480         dw_loc_descr_ref bra_node, jump_node, tmp;
15481
15482         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15483         if (list_ret == 0 || lhs == 0 || rhs == 0)
15484           return 0;
15485
15486         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15487         add_loc_descr_to_each (list_ret, bra_node);
15488
15489         add_loc_list (&list_ret, rhs);
15490         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15491         add_loc_descr_to_each (list_ret, jump_node);
15492
15493         add_loc_descr_to_each (list_ret, lhs);
15494         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15495         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15496
15497         /* ??? Need a node to point the skip at.  Use a nop.  */
15498         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15499         add_loc_descr_to_each (list_ret, tmp);
15500         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15501         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15502       }
15503       break;
15504
15505     case FIX_TRUNC_EXPR:
15506       return 0;
15507
15508     default:
15509       /* Leave front-end specific codes as simply unknown.  This comes
15510          up, for instance, with the C STMT_EXPR.  */
15511       if ((unsigned int) TREE_CODE (loc)
15512           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15513         {
15514           expansion_failed (loc, NULL_RTX,
15515                             "language specific tree node");
15516           return 0;
15517         }
15518
15519 #ifdef ENABLE_CHECKING
15520       /* Otherwise this is a generic code; we should just lists all of
15521          these explicitly.  We forgot one.  */
15522       gcc_unreachable ();
15523 #else
15524       /* In a release build, we want to degrade gracefully: better to
15525          generate incomplete debugging information than to crash.  */
15526       return NULL;
15527 #endif
15528     }
15529
15530   if (!ret && !list_ret)
15531     return 0;
15532
15533   if (want_address == 2 && !have_address
15534       && (dwarf_version >= 4 || !dwarf_strict))
15535     {
15536       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15537         {
15538           expansion_failed (loc, NULL_RTX,
15539                             "DWARF address size mismatch");
15540           return 0;
15541         }
15542       if (ret)
15543         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15544       else
15545         add_loc_descr_to_each (list_ret,
15546                                new_loc_descr (DW_OP_stack_value, 0, 0));
15547       have_address = 1;
15548     }
15549   /* Show if we can't fill the request for an address.  */
15550   if (want_address && !have_address)
15551     {
15552       expansion_failed (loc, NULL_RTX,
15553                         "Want address and only have value");
15554       return 0;
15555     }
15556
15557   gcc_assert (!ret || !list_ret);
15558
15559   /* If we've got an address and don't want one, dereference.  */
15560   if (!want_address && have_address)
15561     {
15562       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15563
15564       if (size > DWARF2_ADDR_SIZE || size == -1)
15565         {
15566           expansion_failed (loc, NULL_RTX,
15567                             "DWARF address size mismatch");
15568           return 0;
15569         }
15570       else if (size == DWARF2_ADDR_SIZE)
15571         op = DW_OP_deref;
15572       else
15573         op = DW_OP_deref_size;
15574
15575       if (ret)
15576         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15577       else
15578         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15579     }
15580   if (ret)
15581     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15582
15583   return list_ret;
15584 }
15585
15586 /* Same as above but return only single location expression.  */
15587 static dw_loc_descr_ref
15588 loc_descriptor_from_tree (tree loc, int want_address)
15589 {
15590   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15591   if (!ret)
15592     return NULL;
15593   if (ret->dw_loc_next)
15594     {
15595       expansion_failed (loc, NULL_RTX,
15596                         "Location list where only loc descriptor needed");
15597       return NULL;
15598     }
15599   return ret->expr;
15600 }
15601
15602 /* Given a value, round it up to the lowest multiple of `boundary'
15603    which is not less than the value itself.  */
15604
15605 static inline HOST_WIDE_INT
15606 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15607 {
15608   return (((value + boundary - 1) / boundary) * boundary);
15609 }
15610
15611 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15612    pointer to the declared type for the relevant field variable, or return
15613    `integer_type_node' if the given node turns out to be an
15614    ERROR_MARK node.  */
15615
15616 static inline tree
15617 field_type (const_tree decl)
15618 {
15619   tree type;
15620
15621   if (TREE_CODE (decl) == ERROR_MARK)
15622     return integer_type_node;
15623
15624   type = DECL_BIT_FIELD_TYPE (decl);
15625   if (type == NULL_TREE)
15626     type = TREE_TYPE (decl);
15627
15628   return type;
15629 }
15630
15631 /* Given a pointer to a tree node, return the alignment in bits for
15632    it, or else return BITS_PER_WORD if the node actually turns out to
15633    be an ERROR_MARK node.  */
15634
15635 static inline unsigned
15636 simple_type_align_in_bits (const_tree type)
15637 {
15638   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15639 }
15640
15641 static inline unsigned
15642 simple_decl_align_in_bits (const_tree decl)
15643 {
15644   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15645 }
15646
15647 /* Return the result of rounding T up to ALIGN.  */
15648
15649 static inline double_int
15650 round_up_to_align (double_int t, unsigned int align)
15651 {
15652   double_int alignd = uhwi_to_double_int (align);
15653   t = double_int_add (t, alignd);
15654   t = double_int_add (t, double_int_minus_one);
15655   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15656   t = double_int_mul (t, alignd);
15657   return t;
15658 }
15659
15660 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15661    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15662    or return 0 if we are unable to determine what that offset is, either
15663    because the argument turns out to be a pointer to an ERROR_MARK node, or
15664    because the offset is actually variable.  (We can't handle the latter case
15665    just yet).  */
15666
15667 static HOST_WIDE_INT
15668 field_byte_offset (const_tree decl)
15669 {
15670   double_int object_offset_in_bits;
15671   double_int object_offset_in_bytes;
15672   double_int bitpos_int;
15673
15674   if (TREE_CODE (decl) == ERROR_MARK)
15675     return 0;
15676
15677   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15678
15679   /* We cannot yet cope with fields whose positions are variable, so
15680      for now, when we see such things, we simply return 0.  Someday, we may
15681      be able to handle such cases, but it will be damn difficult.  */
15682   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15683     return 0;
15684
15685   bitpos_int = tree_to_double_int (bit_position (decl));
15686
15687 #ifdef PCC_BITFIELD_TYPE_MATTERS
15688   if (PCC_BITFIELD_TYPE_MATTERS)
15689     {
15690       tree type;
15691       tree field_size_tree;
15692       double_int deepest_bitpos;
15693       double_int field_size_in_bits;
15694       unsigned int type_align_in_bits;
15695       unsigned int decl_align_in_bits;
15696       double_int type_size_in_bits;
15697
15698       type = field_type (decl);
15699       type_size_in_bits = double_int_type_size_in_bits (type);
15700       type_align_in_bits = simple_type_align_in_bits (type);
15701
15702       field_size_tree = DECL_SIZE (decl);
15703
15704       /* The size could be unspecified if there was an error, or for
15705          a flexible array member.  */
15706       if (!field_size_tree)
15707         field_size_tree = bitsize_zero_node;
15708
15709       /* If the size of the field is not constant, use the type size.  */
15710       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15711         field_size_in_bits = tree_to_double_int (field_size_tree);
15712       else
15713         field_size_in_bits = type_size_in_bits;
15714
15715       decl_align_in_bits = simple_decl_align_in_bits (decl);
15716
15717       /* The GCC front-end doesn't make any attempt to keep track of the
15718          starting bit offset (relative to the start of the containing
15719          structure type) of the hypothetical "containing object" for a
15720          bit-field.  Thus, when computing the byte offset value for the
15721          start of the "containing object" of a bit-field, we must deduce
15722          this information on our own. This can be rather tricky to do in
15723          some cases.  For example, handling the following structure type
15724          definition when compiling for an i386/i486 target (which only
15725          aligns long long's to 32-bit boundaries) can be very tricky:
15726
15727          struct S { int field1; long long field2:31; };
15728
15729          Fortunately, there is a simple rule-of-thumb which can be used
15730          in such cases.  When compiling for an i386/i486, GCC will
15731          allocate 8 bytes for the structure shown above.  It decides to
15732          do this based upon one simple rule for bit-field allocation.
15733          GCC allocates each "containing object" for each bit-field at
15734          the first (i.e. lowest addressed) legitimate alignment boundary
15735          (based upon the required minimum alignment for the declared
15736          type of the field) which it can possibly use, subject to the
15737          condition that there is still enough available space remaining
15738          in the containing object (when allocated at the selected point)
15739          to fully accommodate all of the bits of the bit-field itself.
15740
15741          This simple rule makes it obvious why GCC allocates 8 bytes for
15742          each object of the structure type shown above.  When looking
15743          for a place to allocate the "containing object" for `field2',
15744          the compiler simply tries to allocate a 64-bit "containing
15745          object" at each successive 32-bit boundary (starting at zero)
15746          until it finds a place to allocate that 64- bit field such that
15747          at least 31 contiguous (and previously unallocated) bits remain
15748          within that selected 64 bit field.  (As it turns out, for the
15749          example above, the compiler finds it is OK to allocate the
15750          "containing object" 64-bit field at bit-offset zero within the
15751          structure type.)
15752
15753          Here we attempt to work backwards from the limited set of facts
15754          we're given, and we try to deduce from those facts, where GCC
15755          must have believed that the containing object started (within
15756          the structure type). The value we deduce is then used (by the
15757          callers of this routine) to generate DW_AT_location and
15758          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15759          the case of DW_AT_location, regular fields as well).  */
15760
15761       /* Figure out the bit-distance from the start of the structure to
15762          the "deepest" bit of the bit-field.  */
15763       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15764
15765       /* This is the tricky part.  Use some fancy footwork to deduce
15766          where the lowest addressed bit of the containing object must
15767          be.  */
15768       object_offset_in_bits
15769         = double_int_sub (deepest_bitpos, type_size_in_bits);
15770
15771       /* Round up to type_align by default.  This works best for
15772          bitfields.  */
15773       object_offset_in_bits
15774         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15775
15776       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15777         {
15778           object_offset_in_bits
15779             = double_int_sub (deepest_bitpos, type_size_in_bits);
15780
15781           /* Round up to decl_align instead.  */
15782           object_offset_in_bits
15783             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15784         }
15785     }
15786   else
15787 #endif
15788     object_offset_in_bits = bitpos_int;
15789
15790   object_offset_in_bytes
15791     = double_int_div (object_offset_in_bits,
15792                       uhwi_to_double_int (BITS_PER_UNIT), true,
15793                       TRUNC_DIV_EXPR);
15794   return double_int_to_shwi (object_offset_in_bytes);
15795 }
15796 \f
15797 /* The following routines define various Dwarf attributes and any data
15798    associated with them.  */
15799
15800 /* Add a location description attribute value to a DIE.
15801
15802    This emits location attributes suitable for whole variables and
15803    whole parameters.  Note that the location attributes for struct fields are
15804    generated by the routine `data_member_location_attribute' below.  */
15805
15806 static inline void
15807 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15808                              dw_loc_list_ref descr)
15809 {
15810   if (descr == 0)
15811     return;
15812   if (single_element_loc_list_p (descr))
15813     add_AT_loc (die, attr_kind, descr->expr);
15814   else
15815     add_AT_loc_list (die, attr_kind, descr);
15816 }
15817
15818 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15819
15820 static void
15821 add_accessibility_attribute (dw_die_ref die, tree decl)
15822 {
15823   if (TREE_PROTECTED (decl))
15824     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15825   else if (TREE_PRIVATE (decl))
15826     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15827 }
15828
15829 /* Attach the specialized form of location attribute used for data members of
15830    struct and union types.  In the special case of a FIELD_DECL node which
15831    represents a bit-field, the "offset" part of this special location
15832    descriptor must indicate the distance in bytes from the lowest-addressed
15833    byte of the containing struct or union type to the lowest-addressed byte of
15834    the "containing object" for the bit-field.  (See the `field_byte_offset'
15835    function above).
15836
15837    For any given bit-field, the "containing object" is a hypothetical object
15838    (of some integral or enum type) within which the given bit-field lives.  The
15839    type of this hypothetical "containing object" is always the same as the
15840    declared type of the individual bit-field itself (for GCC anyway... the
15841    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15842    bytes) of the hypothetical "containing object" which will be given in the
15843    DW_AT_byte_size attribute for this bit-field.  (See the
15844    `byte_size_attribute' function below.)  It is also used when calculating the
15845    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15846    function below.)  */
15847
15848 static void
15849 add_data_member_location_attribute (dw_die_ref die, tree decl)
15850 {
15851   HOST_WIDE_INT offset;
15852   dw_loc_descr_ref loc_descr = 0;
15853
15854   if (TREE_CODE (decl) == TREE_BINFO)
15855     {
15856       /* We're working on the TAG_inheritance for a base class.  */
15857       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15858         {
15859           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15860              aren't at a fixed offset from all (sub)objects of the same
15861              type.  We need to extract the appropriate offset from our
15862              vtable.  The following dwarf expression means
15863
15864                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15865
15866              This is specific to the V3 ABI, of course.  */
15867
15868           dw_loc_descr_ref tmp;
15869
15870           /* Make a copy of the object address.  */
15871           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15872           add_loc_descr (&loc_descr, tmp);
15873
15874           /* Extract the vtable address.  */
15875           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15876           add_loc_descr (&loc_descr, tmp);
15877
15878           /* Calculate the address of the offset.  */
15879           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15880           gcc_assert (offset < 0);
15881
15882           tmp = int_loc_descriptor (-offset);
15883           add_loc_descr (&loc_descr, tmp);
15884           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15885           add_loc_descr (&loc_descr, tmp);
15886
15887           /* Extract the offset.  */
15888           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15889           add_loc_descr (&loc_descr, tmp);
15890
15891           /* Add it to the object address.  */
15892           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15893           add_loc_descr (&loc_descr, tmp);
15894         }
15895       else
15896         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15897     }
15898   else
15899     offset = field_byte_offset (decl);
15900
15901   if (! loc_descr)
15902     {
15903       if (dwarf_version > 2)
15904         {
15905           /* Don't need to output a location expression, just the constant. */
15906           add_AT_int (die, DW_AT_data_member_location, offset);
15907           return;
15908         }
15909       else
15910         {
15911           enum dwarf_location_atom op;
15912
15913           /* The DWARF2 standard says that we should assume that the structure
15914              address is already on the stack, so we can specify a structure
15915              field address by using DW_OP_plus_uconst.  */
15916
15917 #ifdef MIPS_DEBUGGING_INFO
15918           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15919              operator correctly.  It works only if we leave the offset on the
15920              stack.  */
15921           op = DW_OP_constu;
15922 #else
15923           op = DW_OP_plus_uconst;
15924 #endif
15925
15926           loc_descr = new_loc_descr (op, offset, 0);
15927         }
15928     }
15929
15930   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15931 }
15932
15933 /* Writes integer values to dw_vec_const array.  */
15934
15935 static void
15936 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15937 {
15938   while (size != 0)
15939     {
15940       *dest++ = val & 0xff;
15941       val >>= 8;
15942       --size;
15943     }
15944 }
15945
15946 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15947
15948 static HOST_WIDE_INT
15949 extract_int (const unsigned char *src, unsigned int size)
15950 {
15951   HOST_WIDE_INT val = 0;
15952
15953   src += size;
15954   while (size != 0)
15955     {
15956       val <<= 8;
15957       val |= *--src & 0xff;
15958       --size;
15959     }
15960   return val;
15961 }
15962
15963 /* Writes double_int values to dw_vec_const array.  */
15964
15965 static void
15966 insert_double (double_int val, unsigned char *dest)
15967 {
15968   unsigned char *p0 = dest;
15969   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15970
15971   if (WORDS_BIG_ENDIAN)
15972     {
15973       p0 = p1;
15974       p1 = dest;
15975     }
15976
15977   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15978   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15979 }
15980
15981 /* Writes floating point values to dw_vec_const array.  */
15982
15983 static void
15984 insert_float (const_rtx rtl, unsigned char *array)
15985 {
15986   REAL_VALUE_TYPE rv;
15987   long val[4];
15988   int i;
15989
15990   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15991   real_to_target (val, &rv, GET_MODE (rtl));
15992
15993   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15994   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15995     {
15996       insert_int (val[i], 4, array);
15997       array += 4;
15998     }
15999 }
16000
16001 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16002    does not have a "location" either in memory or in a register.  These
16003    things can arise in GNU C when a constant is passed as an actual parameter
16004    to an inlined function.  They can also arise in C++ where declared
16005    constants do not necessarily get memory "homes".  */
16006
16007 static bool
16008 add_const_value_attribute (dw_die_ref die, rtx rtl)
16009 {
16010   switch (GET_CODE (rtl))
16011     {
16012     case CONST_INT:
16013       {
16014         HOST_WIDE_INT val = INTVAL (rtl);
16015
16016         if (val < 0)
16017           add_AT_int (die, DW_AT_const_value, val);
16018         else
16019           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16020       }
16021       return true;
16022
16023     case CONST_DOUBLE:
16024       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16025          floating-point constant.  A CONST_DOUBLE is used whenever the
16026          constant requires more than one word in order to be adequately
16027          represented.  */
16028       {
16029         enum machine_mode mode = GET_MODE (rtl);
16030
16031         if (SCALAR_FLOAT_MODE_P (mode))
16032           {
16033             unsigned int length = GET_MODE_SIZE (mode);
16034             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16035
16036             insert_float (rtl, array);
16037             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16038           }
16039         else
16040           add_AT_double (die, DW_AT_const_value,
16041                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16042       }
16043       return true;
16044
16045     case CONST_VECTOR:
16046       {
16047         enum machine_mode mode = GET_MODE (rtl);
16048         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16049         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16050         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16051           (length * elt_size);
16052         unsigned int i;
16053         unsigned char *p;
16054
16055         switch (GET_MODE_CLASS (mode))
16056           {
16057           case MODE_VECTOR_INT:
16058             for (i = 0, p = array; i < length; i++, p += elt_size)
16059               {
16060                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16061                 double_int val = rtx_to_double_int (elt);
16062
16063                 if (elt_size <= sizeof (HOST_WIDE_INT))
16064                   insert_int (double_int_to_shwi (val), elt_size, p);
16065                 else
16066                   {
16067                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16068                     insert_double (val, p);
16069                   }
16070               }
16071             break;
16072
16073           case MODE_VECTOR_FLOAT:
16074             for (i = 0, p = array; i < length; i++, p += elt_size)
16075               {
16076                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16077                 insert_float (elt, p);
16078               }
16079             break;
16080
16081           default:
16082             gcc_unreachable ();
16083           }
16084
16085         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16086       }
16087       return true;
16088
16089     case CONST_STRING:
16090       if (dwarf_version >= 4 || !dwarf_strict)
16091         {
16092           dw_loc_descr_ref loc_result;
16093           resolve_one_addr (&rtl, NULL);
16094         rtl_addr:
16095           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16096           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16097           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16098           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16099           add_AT_loc (die, DW_AT_location, loc_result);
16100           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16101           return true;
16102         }
16103       return false;
16104
16105     case CONST:
16106       if (CONSTANT_P (XEXP (rtl, 0)))
16107         return add_const_value_attribute (die, XEXP (rtl, 0));
16108       /* FALLTHROUGH */
16109     case SYMBOL_REF:
16110       if (!const_ok_for_output (rtl))
16111         return false;
16112     case LABEL_REF:
16113       if (dwarf_version >= 4 || !dwarf_strict)
16114         goto rtl_addr;
16115       return false;
16116
16117     case PLUS:
16118       /* In cases where an inlined instance of an inline function is passed
16119          the address of an `auto' variable (which is local to the caller) we
16120          can get a situation where the DECL_RTL of the artificial local
16121          variable (for the inlining) which acts as a stand-in for the
16122          corresponding formal parameter (of the inline function) will look
16123          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16124          exactly a compile-time constant expression, but it isn't the address
16125          of the (artificial) local variable either.  Rather, it represents the
16126          *value* which the artificial local variable always has during its
16127          lifetime.  We currently have no way to represent such quasi-constant
16128          values in Dwarf, so for now we just punt and generate nothing.  */
16129       return false;
16130
16131     case HIGH:
16132     case CONST_FIXED:
16133       return false;
16134
16135     case MEM:
16136       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16137           && MEM_READONLY_P (rtl)
16138           && GET_MODE (rtl) == BLKmode)
16139         {
16140           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16141           return true;
16142         }
16143       return false;
16144
16145     default:
16146       /* No other kinds of rtx should be possible here.  */
16147       gcc_unreachable ();
16148     }
16149   return false;
16150 }
16151
16152 /* Determine whether the evaluation of EXPR references any variables
16153    or functions which aren't otherwise used (and therefore may not be
16154    output).  */
16155 static tree
16156 reference_to_unused (tree * tp, int * walk_subtrees,
16157                      void * data ATTRIBUTE_UNUSED)
16158 {
16159   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16160     *walk_subtrees = 0;
16161
16162   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16163       && ! TREE_ASM_WRITTEN (*tp))
16164     return *tp;
16165   /* ???  The C++ FE emits debug information for using decls, so
16166      putting gcc_unreachable here falls over.  See PR31899.  For now
16167      be conservative.  */
16168   else if (!cgraph_global_info_ready
16169            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16170     return *tp;
16171   else if (TREE_CODE (*tp) == VAR_DECL)
16172     {
16173       struct varpool_node *node = varpool_get_node (*tp);
16174       if (!node || !node->needed)
16175         return *tp;
16176     }
16177   else if (TREE_CODE (*tp) == FUNCTION_DECL
16178            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16179     {
16180       /* The call graph machinery must have finished analyzing,
16181          optimizing and gimplifying the CU by now.
16182          So if *TP has no call graph node associated
16183          to it, it means *TP will not be emitted.  */
16184       if (!cgraph_get_node (*tp))
16185         return *tp;
16186     }
16187   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16188     return *tp;
16189
16190   return NULL_TREE;
16191 }
16192
16193 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16194    for use in a later add_const_value_attribute call.  */
16195
16196 static rtx
16197 rtl_for_decl_init (tree init, tree type)
16198 {
16199   rtx rtl = NULL_RTX;
16200
16201   /* If a variable is initialized with a string constant without embedded
16202      zeros, build CONST_STRING.  */
16203   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16204     {
16205       tree enttype = TREE_TYPE (type);
16206       tree domain = TYPE_DOMAIN (type);
16207       enum machine_mode mode = TYPE_MODE (enttype);
16208
16209       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16210           && domain
16211           && integer_zerop (TYPE_MIN_VALUE (domain))
16212           && compare_tree_int (TYPE_MAX_VALUE (domain),
16213                                TREE_STRING_LENGTH (init) - 1) == 0
16214           && ((size_t) TREE_STRING_LENGTH (init)
16215               == strlen (TREE_STRING_POINTER (init)) + 1))
16216         {
16217           rtl = gen_rtx_CONST_STRING (VOIDmode,
16218                                       ggc_strdup (TREE_STRING_POINTER (init)));
16219           rtl = gen_rtx_MEM (BLKmode, rtl);
16220           MEM_READONLY_P (rtl) = 1;
16221         }
16222     }
16223   /* Other aggregates, and complex values, could be represented using
16224      CONCAT: FIXME!  */
16225   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16226     ;
16227   /* Vectors only work if their mode is supported by the target.
16228      FIXME: generic vectors ought to work too.  */
16229   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16230     ;
16231   /* If the initializer is something that we know will expand into an
16232      immediate RTL constant, expand it now.  We must be careful not to
16233      reference variables which won't be output.  */
16234   else if (initializer_constant_valid_p (init, type)
16235            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16236     {
16237       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16238          possible.  */
16239       if (TREE_CODE (type) == VECTOR_TYPE)
16240         switch (TREE_CODE (init))
16241           {
16242           case VECTOR_CST:
16243             break;
16244           case CONSTRUCTOR:
16245             if (TREE_CONSTANT (init))
16246               {
16247                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16248                 bool constant_p = true;
16249                 tree value;
16250                 unsigned HOST_WIDE_INT ix;
16251
16252                 /* Even when ctor is constant, it might contain non-*_CST
16253                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16254                    belong into VECTOR_CST nodes.  */
16255                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16256                   if (!CONSTANT_CLASS_P (value))
16257                     {
16258                       constant_p = false;
16259                       break;
16260                     }
16261
16262                 if (constant_p)
16263                   {
16264                     init = build_vector_from_ctor (type, elts);
16265                     break;
16266                   }
16267               }
16268             /* FALLTHRU */
16269
16270           default:
16271             return NULL;
16272           }
16273
16274       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16275
16276       /* If expand_expr returns a MEM, it wasn't immediate.  */
16277       gcc_assert (!rtl || !MEM_P (rtl));
16278     }
16279
16280   return rtl;
16281 }
16282
16283 /* Generate RTL for the variable DECL to represent its location.  */
16284
16285 static rtx
16286 rtl_for_decl_location (tree decl)
16287 {
16288   rtx rtl;
16289
16290   /* Here we have to decide where we are going to say the parameter "lives"
16291      (as far as the debugger is concerned).  We only have a couple of
16292      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16293
16294      DECL_RTL normally indicates where the parameter lives during most of the
16295      activation of the function.  If optimization is enabled however, this
16296      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16297      that the parameter doesn't really live anywhere (as far as the code
16298      generation parts of GCC are concerned) during most of the function's
16299      activation.  That will happen (for example) if the parameter is never
16300      referenced within the function.
16301
16302      We could just generate a location descriptor here for all non-NULL
16303      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16304      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16305      where DECL_RTL is NULL or is a pseudo-reg.
16306
16307      Note however that we can only get away with using DECL_INCOMING_RTL as
16308      a backup substitute for DECL_RTL in certain limited cases.  In cases
16309      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16310      we can be sure that the parameter was passed using the same type as it is
16311      declared to have within the function, and that its DECL_INCOMING_RTL
16312      points us to a place where a value of that type is passed.
16313
16314      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16315      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16316      because in these cases DECL_INCOMING_RTL points us to a value of some
16317      type which is *different* from the type of the parameter itself.  Thus,
16318      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16319      such cases, the debugger would end up (for example) trying to fetch a
16320      `float' from a place which actually contains the first part of a
16321      `double'.  That would lead to really incorrect and confusing
16322      output at debug-time.
16323
16324      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16325      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16326      are a couple of exceptions however.  On little-endian machines we can
16327      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16328      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16329      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16330      when (on a little-endian machine) a non-prototyped function has a
16331      parameter declared to be of type `short' or `char'.  In such cases,
16332      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16333      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16334      passed `int' value.  If the debugger then uses that address to fetch
16335      a `short' or a `char' (on a little-endian machine) the result will be
16336      the correct data, so we allow for such exceptional cases below.
16337
16338      Note that our goal here is to describe the place where the given formal
16339      parameter lives during most of the function's activation (i.e. between the
16340      end of the prologue and the start of the epilogue).  We'll do that as best
16341      as we can. Note however that if the given formal parameter is modified
16342      sometime during the execution of the function, then a stack backtrace (at
16343      debug-time) will show the function as having been called with the *new*
16344      value rather than the value which was originally passed in.  This happens
16345      rarely enough that it is not a major problem, but it *is* a problem, and
16346      I'd like to fix it.
16347
16348      A future version of dwarf2out.c may generate two additional attributes for
16349      any given DW_TAG_formal_parameter DIE which will describe the "passed
16350      type" and the "passed location" for the given formal parameter in addition
16351      to the attributes we now generate to indicate the "declared type" and the
16352      "active location" for each parameter.  This additional set of attributes
16353      could be used by debuggers for stack backtraces. Separately, note that
16354      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16355      This happens (for example) for inlined-instances of inline function formal
16356      parameters which are never referenced.  This really shouldn't be
16357      happening.  All PARM_DECL nodes should get valid non-NULL
16358      DECL_INCOMING_RTL values.  FIXME.  */
16359
16360   /* Use DECL_RTL as the "location" unless we find something better.  */
16361   rtl = DECL_RTL_IF_SET (decl);
16362
16363   /* When generating abstract instances, ignore everything except
16364      constants, symbols living in memory, and symbols living in
16365      fixed registers.  */
16366   if (! reload_completed)
16367     {
16368       if (rtl
16369           && (CONSTANT_P (rtl)
16370               || (MEM_P (rtl)
16371                   && CONSTANT_P (XEXP (rtl, 0)))
16372               || (REG_P (rtl)
16373                   && TREE_CODE (decl) == VAR_DECL
16374                   && TREE_STATIC (decl))))
16375         {
16376           rtl = targetm.delegitimize_address (rtl);
16377           return rtl;
16378         }
16379       rtl = NULL_RTX;
16380     }
16381   else if (TREE_CODE (decl) == PARM_DECL)
16382     {
16383       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16384         {
16385           tree declared_type = TREE_TYPE (decl);
16386           tree passed_type = DECL_ARG_TYPE (decl);
16387           enum machine_mode dmode = TYPE_MODE (declared_type);
16388           enum machine_mode pmode = TYPE_MODE (passed_type);
16389
16390           /* This decl represents a formal parameter which was optimized out.
16391              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16392              all cases where (rtl == NULL_RTX) just below.  */
16393           if (dmode == pmode)
16394             rtl = DECL_INCOMING_RTL (decl);
16395           else if (SCALAR_INT_MODE_P (dmode)
16396                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16397                    && DECL_INCOMING_RTL (decl))
16398             {
16399               rtx inc = DECL_INCOMING_RTL (decl);
16400               if (REG_P (inc))
16401                 rtl = inc;
16402               else if (MEM_P (inc))
16403                 {
16404                   if (BYTES_BIG_ENDIAN)
16405                     rtl = adjust_address_nv (inc, dmode,
16406                                              GET_MODE_SIZE (pmode)
16407                                              - GET_MODE_SIZE (dmode));
16408                   else
16409                     rtl = inc;
16410                 }
16411             }
16412         }
16413
16414       /* If the parm was passed in registers, but lives on the stack, then
16415          make a big endian correction if the mode of the type of the
16416          parameter is not the same as the mode of the rtl.  */
16417       /* ??? This is the same series of checks that are made in dbxout.c before
16418          we reach the big endian correction code there.  It isn't clear if all
16419          of these checks are necessary here, but keeping them all is the safe
16420          thing to do.  */
16421       else if (MEM_P (rtl)
16422                && XEXP (rtl, 0) != const0_rtx
16423                && ! CONSTANT_P (XEXP (rtl, 0))
16424                /* Not passed in memory.  */
16425                && !MEM_P (DECL_INCOMING_RTL (decl))
16426                /* Not passed by invisible reference.  */
16427                && (!REG_P (XEXP (rtl, 0))
16428                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16429                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16430 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16431                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16432 #endif
16433                      )
16434                /* Big endian correction check.  */
16435                && BYTES_BIG_ENDIAN
16436                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16437                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16438                    < UNITS_PER_WORD))
16439         {
16440           int offset = (UNITS_PER_WORD
16441                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16442
16443           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16444                              plus_constant (XEXP (rtl, 0), offset));
16445         }
16446     }
16447   else if (TREE_CODE (decl) == VAR_DECL
16448            && rtl
16449            && MEM_P (rtl)
16450            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16451            && BYTES_BIG_ENDIAN)
16452     {
16453       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16454       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16455
16456       /* If a variable is declared "register" yet is smaller than
16457          a register, then if we store the variable to memory, it
16458          looks like we're storing a register-sized value, when in
16459          fact we are not.  We need to adjust the offset of the
16460          storage location to reflect the actual value's bytes,
16461          else gdb will not be able to display it.  */
16462       if (rsize > dsize)
16463         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16464                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16465     }
16466
16467   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16468      and will have been substituted directly into all expressions that use it.
16469      C does not have such a concept, but C++ and other languages do.  */
16470   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16471     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16472
16473   if (rtl)
16474     rtl = targetm.delegitimize_address (rtl);
16475
16476   /* If we don't look past the constant pool, we risk emitting a
16477      reference to a constant pool entry that isn't referenced from
16478      code, and thus is not emitted.  */
16479   if (rtl)
16480     rtl = avoid_constant_pool_reference (rtl);
16481
16482   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16483      in the current CU, resolve_addr will remove the expression referencing
16484      it.  */
16485   if (rtl == NULL_RTX
16486       && TREE_CODE (decl) == VAR_DECL
16487       && !DECL_EXTERNAL (decl)
16488       && TREE_STATIC (decl)
16489       && DECL_NAME (decl)
16490       && !DECL_HARD_REGISTER (decl)
16491       && DECL_MODE (decl) != VOIDmode)
16492     {
16493       rtl = make_decl_rtl_for_debug (decl);
16494       if (!MEM_P (rtl)
16495           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16496           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16497         rtl = NULL_RTX;
16498     }
16499
16500   return rtl;
16501 }
16502
16503 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16504    returned.  If so, the decl for the COMMON block is returned, and the
16505    value is the offset into the common block for the symbol.  */
16506
16507 static tree
16508 fortran_common (tree decl, HOST_WIDE_INT *value)
16509 {
16510   tree val_expr, cvar;
16511   enum machine_mode mode;
16512   HOST_WIDE_INT bitsize, bitpos;
16513   tree offset;
16514   int volatilep = 0, unsignedp = 0;
16515
16516   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16517      it does not have a value (the offset into the common area), or if it
16518      is thread local (as opposed to global) then it isn't common, and shouldn't
16519      be handled as such.  */
16520   if (TREE_CODE (decl) != VAR_DECL
16521       || !TREE_STATIC (decl)
16522       || !DECL_HAS_VALUE_EXPR_P (decl)
16523       || !is_fortran ())
16524     return NULL_TREE;
16525
16526   val_expr = DECL_VALUE_EXPR (decl);
16527   if (TREE_CODE (val_expr) != COMPONENT_REF)
16528     return NULL_TREE;
16529
16530   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16531                               &mode, &unsignedp, &volatilep, true);
16532
16533   if (cvar == NULL_TREE
16534       || TREE_CODE (cvar) != VAR_DECL
16535       || DECL_ARTIFICIAL (cvar)
16536       || !TREE_PUBLIC (cvar))
16537     return NULL_TREE;
16538
16539   *value = 0;
16540   if (offset != NULL)
16541     {
16542       if (!host_integerp (offset, 0))
16543         return NULL_TREE;
16544       *value = tree_low_cst (offset, 0);
16545     }
16546   if (bitpos != 0)
16547     *value += bitpos / BITS_PER_UNIT;
16548
16549   return cvar;
16550 }
16551
16552 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16553    data attribute for a variable or a parameter.  We generate the
16554    DW_AT_const_value attribute only in those cases where the given variable
16555    or parameter does not have a true "location" either in memory or in a
16556    register.  This can happen (for example) when a constant is passed as an
16557    actual argument in a call to an inline function.  (It's possible that
16558    these things can crop up in other ways also.)  Note that one type of
16559    constant value which can be passed into an inlined function is a constant
16560    pointer.  This can happen for example if an actual argument in an inlined
16561    function call evaluates to a compile-time constant address.  */
16562
16563 static bool
16564 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16565                                        enum dwarf_attribute attr)
16566 {
16567   rtx rtl;
16568   dw_loc_list_ref list;
16569   var_loc_list *loc_list;
16570
16571   if (TREE_CODE (decl) == ERROR_MARK)
16572     return false;
16573
16574   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16575               || TREE_CODE (decl) == RESULT_DECL);
16576
16577   /* Try to get some constant RTL for this decl, and use that as the value of
16578      the location.  */
16579
16580   rtl = rtl_for_decl_location (decl);
16581   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16582       && add_const_value_attribute (die, rtl))
16583     return true;
16584
16585   /* See if we have single element location list that is equivalent to
16586      a constant value.  That way we are better to use add_const_value_attribute
16587      rather than expanding constant value equivalent.  */
16588   loc_list = lookup_decl_loc (decl);
16589   if (loc_list
16590       && loc_list->first
16591       && loc_list->first->next == NULL
16592       && NOTE_P (loc_list->first->loc)
16593       && NOTE_VAR_LOCATION (loc_list->first->loc)
16594       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16595     {
16596       struct var_loc_node *node;
16597
16598       node = loc_list->first;
16599       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16600       if (GET_CODE (rtl) == EXPR_LIST)
16601         rtl = XEXP (rtl, 0);
16602       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16603           && add_const_value_attribute (die, rtl))
16604          return true;
16605     }
16606   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16607   if (list)
16608     {
16609       add_AT_location_description (die, attr, list);
16610       return true;
16611     }
16612   /* None of that worked, so it must not really have a location;
16613      try adding a constant value attribute from the DECL_INITIAL.  */
16614   return tree_add_const_value_attribute_for_decl (die, decl);
16615 }
16616
16617 /* Add VARIABLE and DIE into deferred locations list.  */
16618
16619 static void
16620 defer_location (tree variable, dw_die_ref die)
16621 {
16622   deferred_locations entry;
16623   entry.variable = variable;
16624   entry.die = die;
16625   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16626 }
16627
16628 /* Helper function for tree_add_const_value_attribute.  Natively encode
16629    initializer INIT into an array.  Return true if successful.  */
16630
16631 static bool
16632 native_encode_initializer (tree init, unsigned char *array, int size)
16633 {
16634   tree type;
16635
16636   if (init == NULL_TREE)
16637     return false;
16638
16639   STRIP_NOPS (init);
16640   switch (TREE_CODE (init))
16641     {
16642     case STRING_CST:
16643       type = TREE_TYPE (init);
16644       if (TREE_CODE (type) == ARRAY_TYPE)
16645         {
16646           tree enttype = TREE_TYPE (type);
16647           enum machine_mode mode = TYPE_MODE (enttype);
16648
16649           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16650             return false;
16651           if (int_size_in_bytes (type) != size)
16652             return false;
16653           if (size > TREE_STRING_LENGTH (init))
16654             {
16655               memcpy (array, TREE_STRING_POINTER (init),
16656                       TREE_STRING_LENGTH (init));
16657               memset (array + TREE_STRING_LENGTH (init),
16658                       '\0', size - TREE_STRING_LENGTH (init));
16659             }
16660           else
16661             memcpy (array, TREE_STRING_POINTER (init), size);
16662           return true;
16663         }
16664       return false;
16665     case CONSTRUCTOR:
16666       type = TREE_TYPE (init);
16667       if (int_size_in_bytes (type) != size)
16668         return false;
16669       if (TREE_CODE (type) == ARRAY_TYPE)
16670         {
16671           HOST_WIDE_INT min_index;
16672           unsigned HOST_WIDE_INT cnt;
16673           int curpos = 0, fieldsize;
16674           constructor_elt *ce;
16675
16676           if (TYPE_DOMAIN (type) == NULL_TREE
16677               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16678             return false;
16679
16680           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16681           if (fieldsize <= 0)
16682             return false;
16683
16684           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16685           memset (array, '\0', size);
16686           for (cnt = 0;
16687                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16688                cnt++)
16689             {
16690               tree val = ce->value;
16691               tree index = ce->index;
16692               int pos = curpos;
16693               if (index && TREE_CODE (index) == RANGE_EXPR)
16694                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16695                       * fieldsize;
16696               else if (index)
16697                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16698
16699               if (val)
16700                 {
16701                   STRIP_NOPS (val);
16702                   if (!native_encode_initializer (val, array + pos, fieldsize))
16703                     return false;
16704                 }
16705               curpos = pos + fieldsize;
16706               if (index && TREE_CODE (index) == RANGE_EXPR)
16707                 {
16708                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16709                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16710                   while (count > 0)
16711                     {
16712                       if (val)
16713                         memcpy (array + curpos, array + pos, fieldsize);
16714                       curpos += fieldsize;
16715                     }
16716                 }
16717               gcc_assert (curpos <= size);
16718             }
16719           return true;
16720         }
16721       else if (TREE_CODE (type) == RECORD_TYPE
16722                || TREE_CODE (type) == UNION_TYPE)
16723         {
16724           tree field = NULL_TREE;
16725           unsigned HOST_WIDE_INT cnt;
16726           constructor_elt *ce;
16727
16728           if (int_size_in_bytes (type) != size)
16729             return false;
16730
16731           if (TREE_CODE (type) == RECORD_TYPE)
16732             field = TYPE_FIELDS (type);
16733
16734           for (cnt = 0;
16735                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16736                cnt++, field = field ? DECL_CHAIN (field) : 0)
16737             {
16738               tree val = ce->value;
16739               int pos, fieldsize;
16740
16741               if (ce->index != 0)
16742                 field = ce->index;
16743
16744               if (val)
16745                 STRIP_NOPS (val);
16746
16747               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16748                 return false;
16749
16750               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16751                   && TYPE_DOMAIN (TREE_TYPE (field))
16752                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16753                 return false;
16754               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16755                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16756                 return false;
16757               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16758               pos = int_byte_position (field);
16759               gcc_assert (pos + fieldsize <= size);
16760               if (val
16761                   && !native_encode_initializer (val, array + pos, fieldsize))
16762                 return false;
16763             }
16764           return true;
16765         }
16766       return false;
16767     case VIEW_CONVERT_EXPR:
16768     case NON_LVALUE_EXPR:
16769       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16770     default:
16771       return native_encode_expr (init, array, size) == size;
16772     }
16773 }
16774
16775 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16776    attribute is the const value T.  */
16777
16778 static bool
16779 tree_add_const_value_attribute (dw_die_ref die, tree t)
16780 {
16781   tree init;
16782   tree type = TREE_TYPE (t);
16783   rtx rtl;
16784
16785   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16786     return false;
16787
16788   init = t;
16789   gcc_assert (!DECL_P (init));
16790
16791   rtl = rtl_for_decl_init (init, type);
16792   if (rtl)
16793     return add_const_value_attribute (die, rtl);
16794   /* If the host and target are sane, try harder.  */
16795   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16796            && initializer_constant_valid_p (init, type))
16797     {
16798       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16799       if (size > 0 && (int) size == size)
16800         {
16801           unsigned char *array = (unsigned char *)
16802             ggc_alloc_cleared_atomic (size);
16803
16804           if (native_encode_initializer (init, array, size))
16805             {
16806               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16807               return true;
16808             }
16809         }
16810     }
16811   return false;
16812 }
16813
16814 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16815    attribute is the const value of T, where T is an integral constant
16816    variable with static storage duration
16817    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16818
16819 static bool
16820 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16821 {
16822
16823   if (!decl
16824       || (TREE_CODE (decl) != VAR_DECL
16825           && TREE_CODE (decl) != CONST_DECL))
16826     return false;
16827
16828     if (TREE_READONLY (decl)
16829         && ! TREE_THIS_VOLATILE (decl)
16830         && DECL_INITIAL (decl))
16831       /* OK */;
16832     else
16833       return false;
16834
16835   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16836   if (get_AT (var_die, DW_AT_const_value))
16837     return false;
16838
16839   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16840 }
16841
16842 /* Convert the CFI instructions for the current function into a
16843    location list.  This is used for DW_AT_frame_base when we targeting
16844    a dwarf2 consumer that does not support the dwarf3
16845    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16846    expressions.  */
16847
16848 static dw_loc_list_ref
16849 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16850 {
16851   dw_fde_ref fde;
16852   dw_loc_list_ref list, *list_tail;
16853   dw_cfi_ref cfi;
16854   dw_cfa_location last_cfa, next_cfa;
16855   const char *start_label, *last_label, *section;
16856   dw_cfa_location remember;
16857
16858   fde = current_fde ();
16859   gcc_assert (fde != NULL);
16860
16861   section = secname_for_decl (current_function_decl);
16862   list_tail = &list;
16863   list = NULL;
16864
16865   memset (&next_cfa, 0, sizeof (next_cfa));
16866   next_cfa.reg = INVALID_REGNUM;
16867   remember = next_cfa;
16868
16869   start_label = fde->dw_fde_begin;
16870
16871   /* ??? Bald assumption that the CIE opcode list does not contain
16872      advance opcodes.  */
16873   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16874     lookup_cfa_1 (cfi, &next_cfa, &remember);
16875
16876   last_cfa = next_cfa;
16877   last_label = start_label;
16878
16879   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16880     switch (cfi->dw_cfi_opc)
16881       {
16882       case DW_CFA_set_loc:
16883       case DW_CFA_advance_loc1:
16884       case DW_CFA_advance_loc2:
16885       case DW_CFA_advance_loc4:
16886         if (!cfa_equal_p (&last_cfa, &next_cfa))
16887           {
16888             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16889                                        start_label, last_label, section);
16890
16891             list_tail = &(*list_tail)->dw_loc_next;
16892             last_cfa = next_cfa;
16893             start_label = last_label;
16894           }
16895         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16896         break;
16897
16898       case DW_CFA_advance_loc:
16899         /* The encoding is complex enough that we should never emit this.  */
16900         gcc_unreachable ();
16901
16902       default:
16903         lookup_cfa_1 (cfi, &next_cfa, &remember);
16904         break;
16905       }
16906
16907   if (!cfa_equal_p (&last_cfa, &next_cfa))
16908     {
16909       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16910                                  start_label, last_label, section);
16911       list_tail = &(*list_tail)->dw_loc_next;
16912       start_label = last_label;
16913     }
16914
16915   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16916                              start_label, fde->dw_fde_end, section);
16917
16918   if (list && list->dw_loc_next)
16919     gen_llsym (list);
16920
16921   return list;
16922 }
16923
16924 /* Compute a displacement from the "steady-state frame pointer" to the
16925    frame base (often the same as the CFA), and store it in
16926    frame_pointer_fb_offset.  OFFSET is added to the displacement
16927    before the latter is negated.  */
16928
16929 static void
16930 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16931 {
16932   rtx reg, elim;
16933
16934 #ifdef FRAME_POINTER_CFA_OFFSET
16935   reg = frame_pointer_rtx;
16936   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16937 #else
16938   reg = arg_pointer_rtx;
16939   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16940 #endif
16941
16942   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16943   if (GET_CODE (elim) == PLUS)
16944     {
16945       offset += INTVAL (XEXP (elim, 1));
16946       elim = XEXP (elim, 0);
16947     }
16948
16949   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16950                && (elim == hard_frame_pointer_rtx
16951                    || elim == stack_pointer_rtx))
16952               || elim == (frame_pointer_needed
16953                           ? hard_frame_pointer_rtx
16954                           : stack_pointer_rtx));
16955
16956   frame_pointer_fb_offset = -offset;
16957 }
16958
16959 /* Generate a DW_AT_name attribute given some string value to be included as
16960    the value of the attribute.  */
16961
16962 static void
16963 add_name_attribute (dw_die_ref die, const char *name_string)
16964 {
16965   if (name_string != NULL && *name_string != 0)
16966     {
16967       if (demangle_name_func)
16968         name_string = (*demangle_name_func) (name_string);
16969
16970       add_AT_string (die, DW_AT_name, name_string);
16971     }
16972 }
16973
16974 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16975
16976 static void
16977 add_comp_dir_attribute (dw_die_ref die)
16978 {
16979   const char *wd = get_src_pwd ();
16980   char *wd1;
16981
16982   if (wd == NULL)
16983     return;
16984
16985   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16986     {
16987       int wdlen;
16988
16989       wdlen = strlen (wd);
16990       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16991       strcpy (wd1, wd);
16992       wd1 [wdlen] = DIR_SEPARATOR;
16993       wd1 [wdlen + 1] = 0;
16994       wd = wd1;
16995     }
16996
16997     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16998 }
16999
17000 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17001    default.  */
17002
17003 static int
17004 lower_bound_default (void)
17005 {
17006   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17007     {
17008     case DW_LANG_C:
17009     case DW_LANG_C89:
17010     case DW_LANG_C99:
17011     case DW_LANG_C_plus_plus:
17012     case DW_LANG_ObjC:
17013     case DW_LANG_ObjC_plus_plus:
17014     case DW_LANG_Java:
17015       return 0;
17016     case DW_LANG_Fortran77:
17017     case DW_LANG_Fortran90:
17018     case DW_LANG_Fortran95:
17019       return 1;
17020     case DW_LANG_UPC:
17021     case DW_LANG_D:
17022     case DW_LANG_Python:
17023       return dwarf_version >= 4 ? 0 : -1;
17024     case DW_LANG_Ada95:
17025     case DW_LANG_Ada83:
17026     case DW_LANG_Cobol74:
17027     case DW_LANG_Cobol85:
17028     case DW_LANG_Pascal83:
17029     case DW_LANG_Modula2:
17030     case DW_LANG_PLI:
17031       return dwarf_version >= 4 ? 1 : -1;
17032     default:
17033       return -1;
17034     }
17035 }
17036
17037 /* Given a tree node describing an array bound (either lower or upper) output
17038    a representation for that bound.  */
17039
17040 static void
17041 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17042 {
17043   switch (TREE_CODE (bound))
17044     {
17045     case ERROR_MARK:
17046       return;
17047
17048     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17049     case INTEGER_CST:
17050       {
17051         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17052         int dflt;
17053
17054         /* Use the default if possible.  */
17055         if (bound_attr == DW_AT_lower_bound
17056             && host_integerp (bound, 0)
17057             && (dflt = lower_bound_default ()) != -1
17058             && tree_low_cst (bound, 0) == dflt)
17059           ;
17060
17061         /* Otherwise represent the bound as an unsigned value with the
17062            precision of its type.  The precision and signedness of the
17063            type will be necessary to re-interpret it unambiguously.  */
17064         else if (prec < HOST_BITS_PER_WIDE_INT)
17065           {
17066             unsigned HOST_WIDE_INT mask
17067               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17068             add_AT_unsigned (subrange_die, bound_attr,
17069                              TREE_INT_CST_LOW (bound) & mask);
17070           }
17071         else if (prec == HOST_BITS_PER_WIDE_INT
17072                  || TREE_INT_CST_HIGH (bound) == 0)
17073           add_AT_unsigned (subrange_die, bound_attr,
17074                            TREE_INT_CST_LOW (bound));
17075         else
17076           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17077                          TREE_INT_CST_LOW (bound));
17078       }
17079       break;
17080
17081     CASE_CONVERT:
17082     case VIEW_CONVERT_EXPR:
17083       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17084       break;
17085
17086     case SAVE_EXPR:
17087       break;
17088
17089     case VAR_DECL:
17090     case PARM_DECL:
17091     case RESULT_DECL:
17092       {
17093         dw_die_ref decl_die = lookup_decl_die (bound);
17094
17095         /* ??? Can this happen, or should the variable have been bound
17096            first?  Probably it can, since I imagine that we try to create
17097            the types of parameters in the order in which they exist in
17098            the list, and won't have created a forward reference to a
17099            later parameter.  */
17100         if (decl_die != NULL)
17101           {
17102             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17103             break;
17104           }
17105       }
17106       /* FALLTHRU */
17107
17108     default:
17109       {
17110         /* Otherwise try to create a stack operation procedure to
17111            evaluate the value of the array bound.  */
17112
17113         dw_die_ref ctx, decl_die;
17114         dw_loc_list_ref list;
17115
17116         list = loc_list_from_tree (bound, 2);
17117         if (list == NULL || single_element_loc_list_p (list))
17118           {
17119             /* If DW_AT_*bound is not a reference nor constant, it is
17120                a DWARF expression rather than location description.
17121                For that loc_list_from_tree (bound, 0) is needed.
17122                If that fails to give a single element list,
17123                fall back to outputting this as a reference anyway.  */
17124             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17125             if (list2 && single_element_loc_list_p (list2))
17126               {
17127                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17128                 break;
17129               }
17130           }
17131         if (list == NULL)
17132           break;
17133
17134         if (current_function_decl == 0)
17135           ctx = comp_unit_die;
17136         else
17137           ctx = lookup_decl_die (current_function_decl);
17138
17139         decl_die = new_die (DW_TAG_variable, ctx, bound);
17140         add_AT_flag (decl_die, DW_AT_artificial, 1);
17141         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17142         add_AT_location_description (decl_die, DW_AT_location, list);
17143         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17144         break;
17145       }
17146     }
17147 }
17148
17149 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17150    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17151    Note that the block of subscript information for an array type also
17152    includes information about the element type of the given array type.  */
17153
17154 static void
17155 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17156 {
17157   unsigned dimension_number;
17158   tree lower, upper;
17159   dw_die_ref subrange_die;
17160
17161   for (dimension_number = 0;
17162        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17163        type = TREE_TYPE (type), dimension_number++)
17164     {
17165       tree domain = TYPE_DOMAIN (type);
17166
17167       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17168         break;
17169
17170       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17171          and (in GNU C only) variable bounds.  Handle all three forms
17172          here.  */
17173       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17174       if (domain)
17175         {
17176           /* We have an array type with specified bounds.  */
17177           lower = TYPE_MIN_VALUE (domain);
17178           upper = TYPE_MAX_VALUE (domain);
17179
17180           /* Define the index type.  */
17181           if (TREE_TYPE (domain))
17182             {
17183               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17184                  TREE_TYPE field.  We can't emit debug info for this
17185                  because it is an unnamed integral type.  */
17186               if (TREE_CODE (domain) == INTEGER_TYPE
17187                   && TYPE_NAME (domain) == NULL_TREE
17188                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17189                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17190                 ;
17191               else
17192                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17193                                     type_die);
17194             }
17195
17196           /* ??? If upper is NULL, the array has unspecified length,
17197              but it does have a lower bound.  This happens with Fortran
17198                dimension arr(N:*)
17199              Since the debugger is definitely going to need to know N
17200              to produce useful results, go ahead and output the lower
17201              bound solo, and hope the debugger can cope.  */
17202
17203           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17204           if (upper)
17205             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17206         }
17207
17208       /* Otherwise we have an array type with an unspecified length.  The
17209          DWARF-2 spec does not say how to handle this; let's just leave out the
17210          bounds.  */
17211     }
17212 }
17213
17214 static void
17215 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17216 {
17217   unsigned size;
17218
17219   switch (TREE_CODE (tree_node))
17220     {
17221     case ERROR_MARK:
17222       size = 0;
17223       break;
17224     case ENUMERAL_TYPE:
17225     case RECORD_TYPE:
17226     case UNION_TYPE:
17227     case QUAL_UNION_TYPE:
17228       size = int_size_in_bytes (tree_node);
17229       break;
17230     case FIELD_DECL:
17231       /* For a data member of a struct or union, the DW_AT_byte_size is
17232          generally given as the number of bytes normally allocated for an
17233          object of the *declared* type of the member itself.  This is true
17234          even for bit-fields.  */
17235       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17236       break;
17237     default:
17238       gcc_unreachable ();
17239     }
17240
17241   /* Note that `size' might be -1 when we get to this point.  If it is, that
17242      indicates that the byte size of the entity in question is variable.  We
17243      have no good way of expressing this fact in Dwarf at the present time,
17244      so just let the -1 pass on through.  */
17245   add_AT_unsigned (die, DW_AT_byte_size, size);
17246 }
17247
17248 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17249    which specifies the distance in bits from the highest order bit of the
17250    "containing object" for the bit-field to the highest order bit of the
17251    bit-field itself.
17252
17253    For any given bit-field, the "containing object" is a hypothetical object
17254    (of some integral or enum type) within which the given bit-field lives.  The
17255    type of this hypothetical "containing object" is always the same as the
17256    declared type of the individual bit-field itself.  The determination of the
17257    exact location of the "containing object" for a bit-field is rather
17258    complicated.  It's handled by the `field_byte_offset' function (above).
17259
17260    Note that it is the size (in bytes) of the hypothetical "containing object"
17261    which will be given in the DW_AT_byte_size attribute for this bit-field.
17262    (See `byte_size_attribute' above).  */
17263
17264 static inline void
17265 add_bit_offset_attribute (dw_die_ref die, tree decl)
17266 {
17267   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17268   tree type = DECL_BIT_FIELD_TYPE (decl);
17269   HOST_WIDE_INT bitpos_int;
17270   HOST_WIDE_INT highest_order_object_bit_offset;
17271   HOST_WIDE_INT highest_order_field_bit_offset;
17272   HOST_WIDE_INT unsigned bit_offset;
17273
17274   /* Must be a field and a bit field.  */
17275   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17276
17277   /* We can't yet handle bit-fields whose offsets are variable, so if we
17278      encounter such things, just return without generating any attribute
17279      whatsoever.  Likewise for variable or too large size.  */
17280   if (! host_integerp (bit_position (decl), 0)
17281       || ! host_integerp (DECL_SIZE (decl), 1))
17282     return;
17283
17284   bitpos_int = int_bit_position (decl);
17285
17286   /* Note that the bit offset is always the distance (in bits) from the
17287      highest-order bit of the "containing object" to the highest-order bit of
17288      the bit-field itself.  Since the "high-order end" of any object or field
17289      is different on big-endian and little-endian machines, the computation
17290      below must take account of these differences.  */
17291   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17292   highest_order_field_bit_offset = bitpos_int;
17293
17294   if (! BYTES_BIG_ENDIAN)
17295     {
17296       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17297       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17298     }
17299
17300   bit_offset
17301     = (! BYTES_BIG_ENDIAN
17302        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17303        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17304
17305   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17306 }
17307
17308 /* For a FIELD_DECL node which represents a bit field, output an attribute
17309    which specifies the length in bits of the given field.  */
17310
17311 static inline void
17312 add_bit_size_attribute (dw_die_ref die, tree decl)
17313 {
17314   /* Must be a field and a bit field.  */
17315   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17316               && DECL_BIT_FIELD_TYPE (decl));
17317
17318   if (host_integerp (DECL_SIZE (decl), 1))
17319     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17320 }
17321
17322 /* If the compiled language is ANSI C, then add a 'prototyped'
17323    attribute, if arg types are given for the parameters of a function.  */
17324
17325 static inline void
17326 add_prototyped_attribute (dw_die_ref die, tree func_type)
17327 {
17328   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17329       && TYPE_ARG_TYPES (func_type) != NULL)
17330     add_AT_flag (die, DW_AT_prototyped, 1);
17331 }
17332
17333 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17334    by looking in either the type declaration or object declaration
17335    equate table.  */
17336
17337 static inline dw_die_ref
17338 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17339 {
17340   dw_die_ref origin_die = NULL;
17341
17342   if (TREE_CODE (origin) != FUNCTION_DECL)
17343     {
17344       /* We may have gotten separated from the block for the inlined
17345          function, if we're in an exception handler or some such; make
17346          sure that the abstract function has been written out.
17347
17348          Doing this for nested functions is wrong, however; functions are
17349          distinct units, and our context might not even be inline.  */
17350       tree fn = origin;
17351
17352       if (TYPE_P (fn))
17353         fn = TYPE_STUB_DECL (fn);
17354
17355       fn = decl_function_context (fn);
17356       if (fn)
17357         dwarf2out_abstract_function (fn);
17358     }
17359
17360   if (DECL_P (origin))
17361     origin_die = lookup_decl_die (origin);
17362   else if (TYPE_P (origin))
17363     origin_die = lookup_type_die (origin);
17364
17365   /* XXX: Functions that are never lowered don't always have correct block
17366      trees (in the case of java, they simply have no block tree, in some other
17367      languages).  For these functions, there is nothing we can really do to
17368      output correct debug info for inlined functions in all cases.  Rather
17369      than die, we'll just produce deficient debug info now, in that we will
17370      have variables without a proper abstract origin.  In the future, when all
17371      functions are lowered, we should re-add a gcc_assert (origin_die)
17372      here.  */
17373
17374   if (origin_die)
17375     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17376   return origin_die;
17377 }
17378
17379 /* We do not currently support the pure_virtual attribute.  */
17380
17381 static inline void
17382 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17383 {
17384   if (DECL_VINDEX (func_decl))
17385     {
17386       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17387
17388       if (host_integerp (DECL_VINDEX (func_decl), 0))
17389         add_AT_loc (die, DW_AT_vtable_elem_location,
17390                     new_loc_descr (DW_OP_constu,
17391                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17392                                    0));
17393
17394       /* GNU extension: Record what type this method came from originally.  */
17395       if (debug_info_level > DINFO_LEVEL_TERSE
17396           && DECL_CONTEXT (func_decl))
17397         add_AT_die_ref (die, DW_AT_containing_type,
17398                         lookup_type_die (DECL_CONTEXT (func_decl)));
17399     }
17400 }
17401 \f
17402 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17403    given decl.  This used to be a vendor extension until after DWARF 4
17404    standardized it.  */
17405
17406 static void
17407 add_linkage_attr (dw_die_ref die, tree decl)
17408 {
17409   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17410
17411   /* Mimic what assemble_name_raw does with a leading '*'.  */
17412   if (name[0] == '*')
17413     name = &name[1];
17414
17415   if (dwarf_version >= 4)
17416     add_AT_string (die, DW_AT_linkage_name, name);
17417   else
17418     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17419 }
17420
17421 /* Add source coordinate attributes for the given decl.  */
17422
17423 static void
17424 add_src_coords_attributes (dw_die_ref die, tree decl)
17425 {
17426   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17427
17428   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17429   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17430 }
17431
17432 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17433
17434 static void
17435 add_linkage_name (dw_die_ref die, tree decl)
17436 {
17437   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17438        && TREE_PUBLIC (decl)
17439        && !DECL_ABSTRACT (decl)
17440        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17441        && die->die_tag != DW_TAG_member)
17442     {
17443       /* Defer until we have an assembler name set.  */
17444       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17445         {
17446           limbo_die_node *asm_name;
17447
17448           asm_name = ggc_alloc_cleared_limbo_die_node ();
17449           asm_name->die = die;
17450           asm_name->created_for = decl;
17451           asm_name->next = deferred_asm_name;
17452           deferred_asm_name = asm_name;
17453         }
17454       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17455         add_linkage_attr (die, decl);
17456     }
17457 }
17458
17459 /* Add a DW_AT_name attribute and source coordinate attribute for the
17460    given decl, but only if it actually has a name.  */
17461
17462 static void
17463 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17464 {
17465   tree decl_name;
17466
17467   decl_name = DECL_NAME (decl);
17468   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17469     {
17470       const char *name = dwarf2_name (decl, 0);
17471       if (name)
17472         add_name_attribute (die, name);
17473       if (! DECL_ARTIFICIAL (decl))
17474         add_src_coords_attributes (die, decl);
17475
17476       add_linkage_name (die, decl);
17477     }
17478
17479 #ifdef VMS_DEBUGGING_INFO
17480   /* Get the function's name, as described by its RTL.  This may be different
17481      from the DECL_NAME name used in the source file.  */
17482   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17483     {
17484       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17485                    XEXP (DECL_RTL (decl), 0));
17486       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17487     }
17488 #endif
17489 }
17490
17491 #ifdef VMS_DEBUGGING_INFO
17492
17493 /* Output the debug main pointer die for VMS */
17494
17495 void
17496 dwarf2out_vms_debug_main_pointer (void)
17497 {
17498   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17499   dw_die_ref die;
17500
17501   /* Allocate the VMS debug main subprogram die.  */
17502   die = ggc_alloc_cleared_die_node ();
17503   die->die_tag = DW_TAG_subprogram;
17504   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17505   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17506                                current_function_funcdef_no);
17507   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17508
17509   /* Make it the first child of comp_unit_die.  */
17510   die->die_parent = comp_unit_die;
17511   if (comp_unit_die->die_child)
17512     {
17513       die->die_sib = comp_unit_die->die_child->die_sib;
17514       comp_unit_die->die_child->die_sib = die;
17515     }
17516   else
17517     {
17518       die->die_sib = die;
17519       comp_unit_die->die_child = die;
17520     }
17521 }
17522 #endif
17523
17524 /* Push a new declaration scope.  */
17525
17526 static void
17527 push_decl_scope (tree scope)
17528 {
17529   VEC_safe_push (tree, gc, decl_scope_table, scope);
17530 }
17531
17532 /* Pop a declaration scope.  */
17533
17534 static inline void
17535 pop_decl_scope (void)
17536 {
17537   VEC_pop (tree, decl_scope_table);
17538 }
17539
17540 /* Return the DIE for the scope that immediately contains this type.
17541    Non-named types get global scope.  Named types nested in other
17542    types get their containing scope if it's open, or global scope
17543    otherwise.  All other types (i.e. function-local named types) get
17544    the current active scope.  */
17545
17546 static dw_die_ref
17547 scope_die_for (tree t, dw_die_ref context_die)
17548 {
17549   dw_die_ref scope_die = NULL;
17550   tree containing_scope;
17551   int i;
17552
17553   /* Non-types always go in the current scope.  */
17554   gcc_assert (TYPE_P (t));
17555
17556   containing_scope = TYPE_CONTEXT (t);
17557
17558   /* Use the containing namespace if it was passed in (for a declaration).  */
17559   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17560     {
17561       if (context_die == lookup_decl_die (containing_scope))
17562         /* OK */;
17563       else
17564         containing_scope = NULL_TREE;
17565     }
17566
17567   /* Ignore function type "scopes" from the C frontend.  They mean that
17568      a tagged type is local to a parmlist of a function declarator, but
17569      that isn't useful to DWARF.  */
17570   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17571     containing_scope = NULL_TREE;
17572
17573   if (containing_scope == NULL_TREE)
17574     scope_die = comp_unit_die;
17575   else if (TYPE_P (containing_scope))
17576     {
17577       /* For types, we can just look up the appropriate DIE.  But
17578          first we check to see if we're in the middle of emitting it
17579          so we know where the new DIE should go.  */
17580       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17581         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17582           break;
17583
17584       if (i < 0)
17585         {
17586           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17587                       || TREE_ASM_WRITTEN (containing_scope));
17588
17589           /* If none of the current dies are suitable, we get file scope.  */
17590           scope_die = comp_unit_die;
17591         }
17592       else
17593         scope_die = lookup_type_die (containing_scope);
17594     }
17595   else
17596     scope_die = context_die;
17597
17598   return scope_die;
17599 }
17600
17601 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17602
17603 static inline int
17604 local_scope_p (dw_die_ref context_die)
17605 {
17606   for (; context_die; context_die = context_die->die_parent)
17607     if (context_die->die_tag == DW_TAG_inlined_subroutine
17608         || context_die->die_tag == DW_TAG_subprogram)
17609       return 1;
17610
17611   return 0;
17612 }
17613
17614 /* Returns nonzero if CONTEXT_DIE is a class.  */
17615
17616 static inline int
17617 class_scope_p (dw_die_ref context_die)
17618 {
17619   return (context_die
17620           && (context_die->die_tag == DW_TAG_structure_type
17621               || context_die->die_tag == DW_TAG_class_type
17622               || context_die->die_tag == DW_TAG_interface_type
17623               || context_die->die_tag == DW_TAG_union_type));
17624 }
17625
17626 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17627    whether or not to treat a DIE in this context as a declaration.  */
17628
17629 static inline int
17630 class_or_namespace_scope_p (dw_die_ref context_die)
17631 {
17632   return (class_scope_p (context_die)
17633           || (context_die && context_die->die_tag == DW_TAG_namespace));
17634 }
17635
17636 /* Many forms of DIEs require a "type description" attribute.  This
17637    routine locates the proper "type descriptor" die for the type given
17638    by 'type', and adds a DW_AT_type attribute below the given die.  */
17639
17640 static void
17641 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17642                     int decl_volatile, dw_die_ref context_die)
17643 {
17644   enum tree_code code  = TREE_CODE (type);
17645   dw_die_ref type_die  = NULL;
17646
17647   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17648      or fixed-point type, use the inner type.  This is because we have no
17649      support for unnamed types in base_type_die.  This can happen if this is
17650      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17651   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17652       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17653     type = TREE_TYPE (type), code = TREE_CODE (type);
17654
17655   if (code == ERROR_MARK
17656       /* Handle a special case.  For functions whose return type is void, we
17657          generate *no* type attribute.  (Note that no object may have type
17658          `void', so this only applies to function return types).  */
17659       || code == VOID_TYPE)
17660     return;
17661
17662   type_die = modified_type_die (type,
17663                                 decl_const || TYPE_READONLY (type),
17664                                 decl_volatile || TYPE_VOLATILE (type),
17665                                 context_die);
17666
17667   if (type_die != NULL)
17668     add_AT_die_ref (object_die, DW_AT_type, type_die);
17669 }
17670
17671 /* Given an object die, add the calling convention attribute for the
17672    function call type.  */
17673 static void
17674 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17675 {
17676   enum dwarf_calling_convention value = DW_CC_normal;
17677
17678   value = ((enum dwarf_calling_convention)
17679            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17680
17681   /* DWARF doesn't provide a way to identify a program's source-level
17682      entry point.  DW_AT_calling_convention attributes are only meant
17683      to describe functions' calling conventions.  However, lacking a
17684      better way to signal the Fortran main program, we use this for the
17685      time being, following existing custom.  */
17686   if (is_fortran ()
17687       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17688     value = DW_CC_program;
17689
17690   /* Only add the attribute if the backend requests it, and
17691      is not DW_CC_normal.  */
17692   if (value && (value != DW_CC_normal))
17693     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17694 }
17695
17696 /* Given a tree pointer to a struct, class, union, or enum type node, return
17697    a pointer to the (string) tag name for the given type, or zero if the type
17698    was declared without a tag.  */
17699
17700 static const char *
17701 type_tag (const_tree type)
17702 {
17703   const char *name = 0;
17704
17705   if (TYPE_NAME (type) != 0)
17706     {
17707       tree t = 0;
17708
17709       /* Find the IDENTIFIER_NODE for the type name.  */
17710       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
17711         t = TYPE_NAME (type);
17712
17713       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17714          a TYPE_DECL node, regardless of whether or not a `typedef' was
17715          involved.  */
17716       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17717                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17718         {
17719           /* We want to be extra verbose.  Don't call dwarf_name if
17720              DECL_NAME isn't set.  The default hook for decl_printable_name
17721              doesn't like that, and in this context it's correct to return
17722              0, instead of "<anonymous>" or the like.  */
17723           if (DECL_NAME (TYPE_NAME (type)))
17724             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17725         }
17726
17727       /* Now get the name as a string, or invent one.  */
17728       if (!name && t != 0)
17729         name = IDENTIFIER_POINTER (t);
17730     }
17731
17732   return (name == 0 || *name == '\0') ? 0 : name;
17733 }
17734
17735 /* Return the type associated with a data member, make a special check
17736    for bit field types.  */
17737
17738 static inline tree
17739 member_declared_type (const_tree member)
17740 {
17741   return (DECL_BIT_FIELD_TYPE (member)
17742           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17743 }
17744
17745 /* Get the decl's label, as described by its RTL. This may be different
17746    from the DECL_NAME name used in the source file.  */
17747
17748 #if 0
17749 static const char *
17750 decl_start_label (tree decl)
17751 {
17752   rtx x;
17753   const char *fnname;
17754
17755   x = DECL_RTL (decl);
17756   gcc_assert (MEM_P (x));
17757
17758   x = XEXP (x, 0);
17759   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17760
17761   fnname = XSTR (x, 0);
17762   return fnname;
17763 }
17764 #endif
17765 \f
17766 /* These routines generate the internal representation of the DIE's for
17767    the compilation unit.  Debugging information is collected by walking
17768    the declaration trees passed in from dwarf2out_decl().  */
17769
17770 static void
17771 gen_array_type_die (tree type, dw_die_ref context_die)
17772 {
17773   dw_die_ref scope_die = scope_die_for (type, context_die);
17774   dw_die_ref array_die;
17775
17776   /* GNU compilers represent multidimensional array types as sequences of one
17777      dimensional array types whose element types are themselves array types.
17778      We sometimes squish that down to a single array_type DIE with multiple
17779      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17780      say that we are allowed to do this kind of compression in C, because
17781      there is no difference between an array of arrays and a multidimensional
17782      array.  We don't do this for Ada to remain as close as possible to the
17783      actual representation, which is especially important against the language
17784      flexibilty wrt arrays of variable size.  */
17785
17786   bool collapse_nested_arrays = !is_ada ();
17787   tree element_type;
17788
17789   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17790      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17791   if (TYPE_STRING_FLAG (type)
17792       && TREE_CODE (type) == ARRAY_TYPE
17793       && is_fortran ()
17794       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17795     {
17796       HOST_WIDE_INT size;
17797
17798       array_die = new_die (DW_TAG_string_type, scope_die, type);
17799       add_name_attribute (array_die, type_tag (type));
17800       equate_type_number_to_die (type, array_die);
17801       size = int_size_in_bytes (type);
17802       if (size >= 0)
17803         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17804       else if (TYPE_DOMAIN (type) != NULL_TREE
17805                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17806                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17807         {
17808           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17809           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17810
17811           size = int_size_in_bytes (TREE_TYPE (szdecl));
17812           if (loc && size > 0)
17813             {
17814               add_AT_location_description (array_die, DW_AT_string_length, loc);
17815               if (size != DWARF2_ADDR_SIZE)
17816                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17817             }
17818         }
17819       return;
17820     }
17821
17822   /* ??? The SGI dwarf reader fails for array of array of enum types
17823      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17824      array type comes before the outer array type.  We thus call gen_type_die
17825      before we new_die and must prevent nested array types collapsing for this
17826      target.  */
17827
17828 #ifdef MIPS_DEBUGGING_INFO
17829   gen_type_die (TREE_TYPE (type), context_die);
17830   collapse_nested_arrays = false;
17831 #endif
17832
17833   array_die = new_die (DW_TAG_array_type, scope_die, type);
17834   add_name_attribute (array_die, type_tag (type));
17835   equate_type_number_to_die (type, array_die);
17836
17837   if (TREE_CODE (type) == VECTOR_TYPE)
17838     {
17839       /* The frontend feeds us a representation for the vector as a struct
17840          containing an array.  Pull out the array type.  */
17841       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17842       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17843     }
17844
17845   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17846   if (is_fortran ()
17847       && TREE_CODE (type) == ARRAY_TYPE
17848       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17849       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17850     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17851
17852 #if 0
17853   /* We default the array ordering.  SDB will probably do
17854      the right things even if DW_AT_ordering is not present.  It's not even
17855      an issue until we start to get into multidimensional arrays anyway.  If
17856      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17857      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17858      and when we find out that we need to put these in, we will only do so
17859      for multidimensional arrays.  */
17860   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17861 #endif
17862
17863 #ifdef MIPS_DEBUGGING_INFO
17864   /* The SGI compilers handle arrays of unknown bound by setting
17865      AT_declaration and not emitting any subrange DIEs.  */
17866   if (! TYPE_DOMAIN (type))
17867     add_AT_flag (array_die, DW_AT_declaration, 1);
17868   else
17869 #endif
17870     add_subscript_info (array_die, type, collapse_nested_arrays);
17871
17872   /* Add representation of the type of the elements of this array type and
17873      emit the corresponding DIE if we haven't done it already.  */
17874   element_type = TREE_TYPE (type);
17875   if (collapse_nested_arrays)
17876     while (TREE_CODE (element_type) == ARRAY_TYPE)
17877       {
17878         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17879           break;
17880         element_type = TREE_TYPE (element_type);
17881       }
17882
17883 #ifndef MIPS_DEBUGGING_INFO
17884   gen_type_die (element_type, context_die);
17885 #endif
17886
17887   add_type_attribute (array_die, element_type, 0, 0, context_die);
17888
17889   if (get_AT (array_die, DW_AT_name))
17890     add_pubtype (type, array_die);
17891 }
17892
17893 static dw_loc_descr_ref
17894 descr_info_loc (tree val, tree base_decl)
17895 {
17896   HOST_WIDE_INT size;
17897   dw_loc_descr_ref loc, loc2;
17898   enum dwarf_location_atom op;
17899
17900   if (val == base_decl)
17901     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17902
17903   switch (TREE_CODE (val))
17904     {
17905     CASE_CONVERT:
17906       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17907     case VAR_DECL:
17908       return loc_descriptor_from_tree (val, 0);
17909     case INTEGER_CST:
17910       if (host_integerp (val, 0))
17911         return int_loc_descriptor (tree_low_cst (val, 0));
17912       break;
17913     case INDIRECT_REF:
17914       size = int_size_in_bytes (TREE_TYPE (val));
17915       if (size < 0)
17916         break;
17917       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17918       if (!loc)
17919         break;
17920       if (size == DWARF2_ADDR_SIZE)
17921         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17922       else
17923         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17924       return loc;
17925     case POINTER_PLUS_EXPR:
17926     case PLUS_EXPR:
17927       if (host_integerp (TREE_OPERAND (val, 1), 1)
17928           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17929              < 16384)
17930         {
17931           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17932           if (!loc)
17933             break;
17934           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17935         }
17936       else
17937         {
17938           op = DW_OP_plus;
17939         do_binop:
17940           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17941           if (!loc)
17942             break;
17943           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17944           if (!loc2)
17945             break;
17946           add_loc_descr (&loc, loc2);
17947           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17948         }
17949       return loc;
17950     case MINUS_EXPR:
17951       op = DW_OP_minus;
17952       goto do_binop;
17953     case MULT_EXPR:
17954       op = DW_OP_mul;
17955       goto do_binop;
17956     case EQ_EXPR:
17957       op = DW_OP_eq;
17958       goto do_binop;
17959     case NE_EXPR:
17960       op = DW_OP_ne;
17961       goto do_binop;
17962     default:
17963       break;
17964     }
17965   return NULL;
17966 }
17967
17968 static void
17969 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17970                       tree val, tree base_decl)
17971 {
17972   dw_loc_descr_ref loc;
17973
17974   if (host_integerp (val, 0))
17975     {
17976       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17977       return;
17978     }
17979
17980   loc = descr_info_loc (val, base_decl);
17981   if (!loc)
17982     return;
17983
17984   add_AT_loc (die, attr, loc);
17985 }
17986
17987 /* This routine generates DIE for array with hidden descriptor, details
17988    are filled into *info by a langhook.  */
17989
17990 static void
17991 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17992                           dw_die_ref context_die)
17993 {
17994   dw_die_ref scope_die = scope_die_for (type, context_die);
17995   dw_die_ref array_die;
17996   int dim;
17997
17998   array_die = new_die (DW_TAG_array_type, scope_die, type);
17999   add_name_attribute (array_die, type_tag (type));
18000   equate_type_number_to_die (type, array_die);
18001
18002   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18003   if (is_fortran ()
18004       && info->ndimensions >= 2)
18005     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18006
18007   if (info->data_location)
18008     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18009                           info->base_decl);
18010   if (info->associated)
18011     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18012                           info->base_decl);
18013   if (info->allocated)
18014     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18015                           info->base_decl);
18016
18017   for (dim = 0; dim < info->ndimensions; dim++)
18018     {
18019       dw_die_ref subrange_die
18020         = new_die (DW_TAG_subrange_type, array_die, NULL);
18021
18022       if (info->dimen[dim].lower_bound)
18023         {
18024           /* If it is the default value, omit it.  */
18025           int dflt;
18026
18027           if (host_integerp (info->dimen[dim].lower_bound, 0)
18028               && (dflt = lower_bound_default ()) != -1
18029               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18030             ;
18031           else
18032             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18033                                   info->dimen[dim].lower_bound,
18034                                   info->base_decl);
18035         }
18036       if (info->dimen[dim].upper_bound)
18037         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18038                               info->dimen[dim].upper_bound,
18039                               info->base_decl);
18040       if (info->dimen[dim].stride)
18041         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18042                               info->dimen[dim].stride,
18043                               info->base_decl);
18044     }
18045
18046   gen_type_die (info->element_type, context_die);
18047   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18048
18049   if (get_AT (array_die, DW_AT_name))
18050     add_pubtype (type, array_die);
18051 }
18052
18053 #if 0
18054 static void
18055 gen_entry_point_die (tree decl, dw_die_ref context_die)
18056 {
18057   tree origin = decl_ultimate_origin (decl);
18058   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18059
18060   if (origin != NULL)
18061     add_abstract_origin_attribute (decl_die, origin);
18062   else
18063     {
18064       add_name_and_src_coords_attributes (decl_die, decl);
18065       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18066                           0, 0, context_die);
18067     }
18068
18069   if (DECL_ABSTRACT (decl))
18070     equate_decl_number_to_die (decl, decl_die);
18071   else
18072     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18073 }
18074 #endif
18075
18076 /* Walk through the list of incomplete types again, trying once more to
18077    emit full debugging info for them.  */
18078
18079 static void
18080 retry_incomplete_types (void)
18081 {
18082   int i;
18083
18084   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18085     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18086                                   DINFO_USAGE_DIR_USE))
18087       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18088 }
18089
18090 /* Determine what tag to use for a record type.  */
18091
18092 static enum dwarf_tag
18093 record_type_tag (tree type)
18094 {
18095   if (! lang_hooks.types.classify_record)
18096     return DW_TAG_structure_type;
18097
18098   switch (lang_hooks.types.classify_record (type))
18099     {
18100     case RECORD_IS_STRUCT:
18101       return DW_TAG_structure_type;
18102
18103     case RECORD_IS_CLASS:
18104       return DW_TAG_class_type;
18105
18106     case RECORD_IS_INTERFACE:
18107       if (dwarf_version >= 3 || !dwarf_strict)
18108         return DW_TAG_interface_type;
18109       return DW_TAG_structure_type;
18110
18111     default:
18112       gcc_unreachable ();
18113     }
18114 }
18115
18116 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18117    include all of the information about the enumeration values also. Each
18118    enumerated type name/value is listed as a child of the enumerated type
18119    DIE.  */
18120
18121 static dw_die_ref
18122 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18123 {
18124   dw_die_ref type_die = lookup_type_die (type);
18125
18126   if (type_die == NULL)
18127     {
18128       type_die = new_die (DW_TAG_enumeration_type,
18129                           scope_die_for (type, context_die), type);
18130       equate_type_number_to_die (type, type_die);
18131       add_name_attribute (type_die, type_tag (type));
18132       if ((dwarf_version >= 4 || !dwarf_strict)
18133           && ENUM_IS_SCOPED (type))
18134         add_AT_flag (type_die, DW_AT_enum_class, 1);
18135     }
18136   else if (! TYPE_SIZE (type))
18137     return type_die;
18138   else
18139     remove_AT (type_die, DW_AT_declaration);
18140
18141   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18142      given enum type is incomplete, do not generate the DW_AT_byte_size
18143      attribute or the DW_AT_element_list attribute.  */
18144   if (TYPE_SIZE (type))
18145     {
18146       tree link;
18147
18148       TREE_ASM_WRITTEN (type) = 1;
18149       add_byte_size_attribute (type_die, type);
18150       if (TYPE_STUB_DECL (type) != NULL_TREE)
18151         {
18152           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18153           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18154         }
18155
18156       /* If the first reference to this type was as the return type of an
18157          inline function, then it may not have a parent.  Fix this now.  */
18158       if (type_die->die_parent == NULL)
18159         add_child_die (scope_die_for (type, context_die), type_die);
18160
18161       for (link = TYPE_VALUES (type);
18162            link != NULL; link = TREE_CHAIN (link))
18163         {
18164           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18165           tree value = TREE_VALUE (link);
18166
18167           add_name_attribute (enum_die,
18168                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18169
18170           if (TREE_CODE (value) == CONST_DECL)
18171             value = DECL_INITIAL (value);
18172
18173           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18174             /* DWARF2 does not provide a way of indicating whether or
18175                not enumeration constants are signed or unsigned.  GDB
18176                always assumes the values are signed, so we output all
18177                values as if they were signed.  That means that
18178                enumeration constants with very large unsigned values
18179                will appear to have negative values in the debugger.  */
18180             add_AT_int (enum_die, DW_AT_const_value,
18181                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18182         }
18183     }
18184   else
18185     add_AT_flag (type_die, DW_AT_declaration, 1);
18186
18187   if (get_AT (type_die, DW_AT_name))
18188     add_pubtype (type, type_die);
18189
18190   return type_die;
18191 }
18192
18193 /* Generate a DIE to represent either a real live formal parameter decl or to
18194    represent just the type of some formal parameter position in some function
18195    type.
18196
18197    Note that this routine is a bit unusual because its argument may be a
18198    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18199    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18200    node.  If it's the former then this function is being called to output a
18201    DIE to represent a formal parameter object (or some inlining thereof).  If
18202    it's the latter, then this function is only being called to output a
18203    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18204    argument type of some subprogram type.
18205    If EMIT_NAME_P is true, name and source coordinate attributes
18206    are emitted.  */
18207
18208 static dw_die_ref
18209 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18210                           dw_die_ref context_die)
18211 {
18212   tree node_or_origin = node ? node : origin;
18213   tree ultimate_origin;
18214   dw_die_ref parm_die
18215     = new_die (DW_TAG_formal_parameter, context_die, node);
18216
18217   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18218     {
18219     case tcc_declaration:
18220       ultimate_origin = decl_ultimate_origin (node_or_origin);
18221       if (node || ultimate_origin)
18222         origin = ultimate_origin;
18223       if (origin != NULL)
18224         add_abstract_origin_attribute (parm_die, origin);
18225       else if (emit_name_p)
18226         add_name_and_src_coords_attributes (parm_die, node);
18227       if (origin == NULL
18228           || (! DECL_ABSTRACT (node_or_origin)
18229               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18230                                            decl_function_context
18231                                                             (node_or_origin))))
18232         {
18233           tree type = TREE_TYPE (node_or_origin);
18234           if (decl_by_reference_p (node_or_origin))
18235             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18236                                 context_die);
18237           else
18238             add_type_attribute (parm_die, type,
18239                                 TREE_READONLY (node_or_origin),
18240                                 TREE_THIS_VOLATILE (node_or_origin),
18241                                 context_die);
18242         }
18243       if (origin == NULL && DECL_ARTIFICIAL (node))
18244         add_AT_flag (parm_die, DW_AT_artificial, 1);
18245
18246       if (node && node != origin)
18247         equate_decl_number_to_die (node, parm_die);
18248       if (! DECL_ABSTRACT (node_or_origin))
18249         add_location_or_const_value_attribute (parm_die, node_or_origin,
18250                                                DW_AT_location);
18251
18252       break;
18253
18254     case tcc_type:
18255       /* We were called with some kind of a ..._TYPE node.  */
18256       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18257       break;
18258
18259     default:
18260       gcc_unreachable ();
18261     }
18262
18263   return parm_die;
18264 }
18265
18266 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18267    children DW_TAG_formal_parameter DIEs representing the arguments of the
18268    parameter pack.
18269
18270    PARM_PACK must be a function parameter pack.
18271    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18272    must point to the subsequent arguments of the function PACK_ARG belongs to.
18273    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18274    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18275    following the last one for which a DIE was generated.  */
18276
18277 static dw_die_ref
18278 gen_formal_parameter_pack_die  (tree parm_pack,
18279                                 tree pack_arg,
18280                                 dw_die_ref subr_die,
18281                                 tree *next_arg)
18282 {
18283   tree arg;
18284   dw_die_ref parm_pack_die;
18285
18286   gcc_assert (parm_pack
18287               && lang_hooks.function_parameter_pack_p (parm_pack)
18288               && subr_die);
18289
18290   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18291   add_src_coords_attributes (parm_pack_die, parm_pack);
18292
18293   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18294     {
18295       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18296                                                                  parm_pack))
18297         break;
18298       gen_formal_parameter_die (arg, NULL,
18299                                 false /* Don't emit name attribute.  */,
18300                                 parm_pack_die);
18301     }
18302   if (next_arg)
18303     *next_arg = arg;
18304   return parm_pack_die;
18305 }
18306
18307 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18308    at the end of an (ANSI prototyped) formal parameters list.  */
18309
18310 static void
18311 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18312 {
18313   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18314 }
18315
18316 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18317    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18318    parameters as specified in some function type specification (except for
18319    those which appear as part of a function *definition*).  */
18320
18321 static void
18322 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18323 {
18324   tree link;
18325   tree formal_type = NULL;
18326   tree first_parm_type;
18327   tree arg;
18328
18329   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18330     {
18331       arg = DECL_ARGUMENTS (function_or_method_type);
18332       function_or_method_type = TREE_TYPE (function_or_method_type);
18333     }
18334   else
18335     arg = NULL_TREE;
18336
18337   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18338
18339   /* Make our first pass over the list of formal parameter types and output a
18340      DW_TAG_formal_parameter DIE for each one.  */
18341   for (link = first_parm_type; link; )
18342     {
18343       dw_die_ref parm_die;
18344
18345       formal_type = TREE_VALUE (link);
18346       if (formal_type == void_type_node)
18347         break;
18348
18349       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18350       parm_die = gen_formal_parameter_die (formal_type, NULL,
18351                                            true /* Emit name attribute.  */,
18352                                            context_die);
18353       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
18354            && link == first_parm_type)
18355           || (arg && DECL_ARTIFICIAL (arg)))
18356         add_AT_flag (parm_die, DW_AT_artificial, 1);
18357
18358       link = TREE_CHAIN (link);
18359       if (arg)
18360         arg = DECL_CHAIN (arg);
18361     }
18362
18363   /* If this function type has an ellipsis, add a
18364      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18365   if (formal_type != void_type_node)
18366     gen_unspecified_parameters_die (function_or_method_type, context_die);
18367
18368   /* Make our second (and final) pass over the list of formal parameter types
18369      and output DIEs to represent those types (as necessary).  */
18370   for (link = TYPE_ARG_TYPES (function_or_method_type);
18371        link && TREE_VALUE (link);
18372        link = TREE_CHAIN (link))
18373     gen_type_die (TREE_VALUE (link), context_die);
18374 }
18375
18376 /* We want to generate the DIE for TYPE so that we can generate the
18377    die for MEMBER, which has been defined; we will need to refer back
18378    to the member declaration nested within TYPE.  If we're trying to
18379    generate minimal debug info for TYPE, processing TYPE won't do the
18380    trick; we need to attach the member declaration by hand.  */
18381
18382 static void
18383 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18384 {
18385   gen_type_die (type, context_die);
18386
18387   /* If we're trying to avoid duplicate debug info, we may not have
18388      emitted the member decl for this function.  Emit it now.  */
18389   if (TYPE_STUB_DECL (type)
18390       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18391       && ! lookup_decl_die (member))
18392     {
18393       dw_die_ref type_die;
18394       gcc_assert (!decl_ultimate_origin (member));
18395
18396       push_decl_scope (type);
18397       type_die = lookup_type_die (type);
18398       if (TREE_CODE (member) == FUNCTION_DECL)
18399         gen_subprogram_die (member, type_die);
18400       else if (TREE_CODE (member) == FIELD_DECL)
18401         {
18402           /* Ignore the nameless fields that are used to skip bits but handle
18403              C++ anonymous unions and structs.  */
18404           if (DECL_NAME (member) != NULL_TREE
18405               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18406               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18407             {
18408               gen_type_die (member_declared_type (member), type_die);
18409               gen_field_die (member, type_die);
18410             }
18411         }
18412       else
18413         gen_variable_die (member, NULL_TREE, type_die);
18414
18415       pop_decl_scope ();
18416     }
18417 }
18418
18419 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18420    may later generate inlined and/or out-of-line instances of.  */
18421
18422 static void
18423 dwarf2out_abstract_function (tree decl)
18424 {
18425   dw_die_ref old_die;
18426   tree save_fn;
18427   tree context;
18428   int was_abstract;
18429   htab_t old_decl_loc_table;
18430
18431   /* Make sure we have the actual abstract inline, not a clone.  */
18432   decl = DECL_ORIGIN (decl);
18433
18434   old_die = lookup_decl_die (decl);
18435   if (old_die && get_AT (old_die, DW_AT_inline))
18436     /* We've already generated the abstract instance.  */
18437     return;
18438
18439   /* We can be called while recursively when seeing block defining inlined subroutine
18440      DIE.  Be sure to not clobber the outer location table nor use it or we would
18441      get locations in abstract instantces.  */
18442   old_decl_loc_table = decl_loc_table;
18443   decl_loc_table = NULL;
18444
18445   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18446      we don't get confused by DECL_ABSTRACT.  */
18447   if (debug_info_level > DINFO_LEVEL_TERSE)
18448     {
18449       context = decl_class_context (decl);
18450       if (context)
18451         gen_type_die_for_member
18452           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18453     }
18454
18455   /* Pretend we've just finished compiling this function.  */
18456   save_fn = current_function_decl;
18457   current_function_decl = decl;
18458   push_cfun (DECL_STRUCT_FUNCTION (decl));
18459
18460   was_abstract = DECL_ABSTRACT (decl);
18461   set_decl_abstract_flags (decl, 1);
18462   dwarf2out_decl (decl);
18463   if (! was_abstract)
18464     set_decl_abstract_flags (decl, 0);
18465
18466   current_function_decl = save_fn;
18467   decl_loc_table = old_decl_loc_table;
18468   pop_cfun ();
18469 }
18470
18471 /* Helper function of premark_used_types() which gets called through
18472    htab_traverse.
18473
18474    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18475    marked as unused by prune_unused_types.  */
18476
18477 static int
18478 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18479 {
18480   tree type;
18481   dw_die_ref die;
18482
18483   type = (tree) *slot;
18484   die = lookup_type_die (type);
18485   if (die != NULL)
18486     die->die_perennial_p = 1;
18487   return 1;
18488 }
18489
18490 /* Helper function of premark_types_used_by_global_vars which gets called
18491    through htab_traverse.
18492
18493    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18494    marked as unused by prune_unused_types. The DIE of the type is marked
18495    only if the global variable using the type will actually be emitted.  */
18496
18497 static int
18498 premark_types_used_by_global_vars_helper (void **slot,
18499                                           void *data ATTRIBUTE_UNUSED)
18500 {
18501   struct types_used_by_vars_entry *entry;
18502   dw_die_ref die;
18503
18504   entry = (struct types_used_by_vars_entry *) *slot;
18505   gcc_assert (entry->type != NULL
18506               && entry->var_decl != NULL);
18507   die = lookup_type_die (entry->type);
18508   if (die)
18509     {
18510       /* Ask cgraph if the global variable really is to be emitted.
18511          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18512       struct varpool_node *node = varpool_get_node (entry->var_decl);
18513       if (node && node->needed)
18514         {
18515           die->die_perennial_p = 1;
18516           /* Keep the parent DIEs as well.  */
18517           while ((die = die->die_parent) && die->die_perennial_p == 0)
18518             die->die_perennial_p = 1;
18519         }
18520     }
18521   return 1;
18522 }
18523
18524 /* Mark all members of used_types_hash as perennial.  */
18525
18526 static void
18527 premark_used_types (void)
18528 {
18529   if (cfun && cfun->used_types_hash)
18530     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18531 }
18532
18533 /* Mark all members of types_used_by_vars_entry as perennial.  */
18534
18535 static void
18536 premark_types_used_by_global_vars (void)
18537 {
18538   if (types_used_by_vars_hash)
18539     htab_traverse (types_used_by_vars_hash,
18540                    premark_types_used_by_global_vars_helper, NULL);
18541 }
18542
18543 /* Generate a DIE to represent a declared function (either file-scope or
18544    block-local).  */
18545
18546 static void
18547 gen_subprogram_die (tree decl, dw_die_ref context_die)
18548 {
18549   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18550   tree origin = decl_ultimate_origin (decl);
18551   dw_die_ref subr_die;
18552   tree fn_arg_types;
18553   tree outer_scope;
18554   dw_die_ref old_die = lookup_decl_die (decl);
18555   int declaration = (current_function_decl != decl
18556                      || class_or_namespace_scope_p (context_die));
18557
18558   premark_used_types ();
18559
18560   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18561      started to generate the abstract instance of an inline, decided to output
18562      its containing class, and proceeded to emit the declaration of the inline
18563      from the member list for the class.  If so, DECLARATION takes priority;
18564      we'll get back to the abstract instance when done with the class.  */
18565
18566   /* The class-scope declaration DIE must be the primary DIE.  */
18567   if (origin && declaration && class_or_namespace_scope_p (context_die))
18568     {
18569       origin = NULL;
18570       gcc_assert (!old_die);
18571     }
18572
18573   /* Now that the C++ front end lazily declares artificial member fns, we
18574      might need to retrofit the declaration into its class.  */
18575   if (!declaration && !origin && !old_die
18576       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18577       && !class_or_namespace_scope_p (context_die)
18578       && debug_info_level > DINFO_LEVEL_TERSE)
18579     old_die = force_decl_die (decl);
18580
18581   if (origin != NULL)
18582     {
18583       gcc_assert (!declaration || local_scope_p (context_die));
18584
18585       /* Fixup die_parent for the abstract instance of a nested
18586          inline function.  */
18587       if (old_die && old_die->die_parent == NULL)
18588         add_child_die (context_die, old_die);
18589
18590       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18591       add_abstract_origin_attribute (subr_die, origin);
18592     }
18593   else if (old_die)
18594     {
18595       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18596       struct dwarf_file_data * file_index = lookup_filename (s.file);
18597
18598       if (!get_AT_flag (old_die, DW_AT_declaration)
18599           /* We can have a normal definition following an inline one in the
18600              case of redefinition of GNU C extern inlines.
18601              It seems reasonable to use AT_specification in this case.  */
18602           && !get_AT (old_die, DW_AT_inline))
18603         {
18604           /* Detect and ignore this case, where we are trying to output
18605              something we have already output.  */
18606           return;
18607         }
18608
18609       /* If the definition comes from the same place as the declaration,
18610          maybe use the old DIE.  We always want the DIE for this function
18611          that has the *_pc attributes to be under comp_unit_die so the
18612          debugger can find it.  We also need to do this for abstract
18613          instances of inlines, since the spec requires the out-of-line copy
18614          to have the same parent.  For local class methods, this doesn't
18615          apply; we just use the old DIE.  */
18616       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18617           && (DECL_ARTIFICIAL (decl)
18618               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18619                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18620                       == (unsigned) s.line))))
18621         {
18622           subr_die = old_die;
18623
18624           /* Clear out the declaration attribute and the formal parameters.
18625              Do not remove all children, because it is possible that this
18626              declaration die was forced using force_decl_die(). In such
18627              cases die that forced declaration die (e.g. TAG_imported_module)
18628              is one of the children that we do not want to remove.  */
18629           remove_AT (subr_die, DW_AT_declaration);
18630           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18631         }
18632       else
18633         {
18634           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18635           add_AT_specification (subr_die, old_die);
18636           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18637             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18638           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18639             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18640         }
18641     }
18642   else
18643     {
18644       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18645
18646       if (TREE_PUBLIC (decl))
18647         add_AT_flag (subr_die, DW_AT_external, 1);
18648
18649       add_name_and_src_coords_attributes (subr_die, decl);
18650       if (debug_info_level > DINFO_LEVEL_TERSE)
18651         {
18652           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18653           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18654                               0, 0, context_die);
18655         }
18656
18657       add_pure_or_virtual_attribute (subr_die, decl);
18658       if (DECL_ARTIFICIAL (decl))
18659         add_AT_flag (subr_die, DW_AT_artificial, 1);
18660
18661       add_accessibility_attribute (subr_die, decl);
18662     }
18663
18664   if (declaration)
18665     {
18666       if (!old_die || !get_AT (old_die, DW_AT_inline))
18667         {
18668           add_AT_flag (subr_die, DW_AT_declaration, 1);
18669
18670           /* If this is an explicit function declaration then generate
18671              a DW_AT_explicit attribute.  */
18672           if (lang_hooks.decls.function_decl_explicit_p (decl)
18673               && (dwarf_version >= 3 || !dwarf_strict))
18674             add_AT_flag (subr_die, DW_AT_explicit, 1);
18675
18676           /* The first time we see a member function, it is in the context of
18677              the class to which it belongs.  We make sure of this by emitting
18678              the class first.  The next time is the definition, which is
18679              handled above.  The two may come from the same source text.
18680
18681              Note that force_decl_die() forces function declaration die. It is
18682              later reused to represent definition.  */
18683           equate_decl_number_to_die (decl, subr_die);
18684         }
18685     }
18686   else if (DECL_ABSTRACT (decl))
18687     {
18688       if (DECL_DECLARED_INLINE_P (decl))
18689         {
18690           if (cgraph_function_possibly_inlined_p (decl))
18691             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18692           else
18693             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18694         }
18695       else
18696         {
18697           if (cgraph_function_possibly_inlined_p (decl))
18698             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18699           else
18700             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18701         }
18702
18703       if (DECL_DECLARED_INLINE_P (decl)
18704           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18705         add_AT_flag (subr_die, DW_AT_artificial, 1);
18706
18707       equate_decl_number_to_die (decl, subr_die);
18708     }
18709   else if (!DECL_EXTERNAL (decl))
18710     {
18711       HOST_WIDE_INT cfa_fb_offset;
18712
18713       if (!old_die || !get_AT (old_die, DW_AT_inline))
18714         equate_decl_number_to_die (decl, subr_die);
18715
18716       if (!flag_reorder_blocks_and_partition)
18717         {
18718           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18719                                        current_function_funcdef_no);
18720           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18721           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18722                                        current_function_funcdef_no);
18723           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18724
18725 #if VMS_DEBUGGING_INFO
18726       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18727          Section 2.3 Prologue and Epilogue Attributes:
18728          When a breakpoint is set on entry to a function, it is generally
18729          desirable for execution to be suspended, not on the very first
18730          instruction of the function, but rather at a point after the
18731          function's frame has been set up, after any language defined local
18732          declaration processing has been completed, and before execution of
18733          the first statement of the function begins. Debuggers generally
18734          cannot properly determine where this point is.  Similarly for a
18735          breakpoint set on exit from a function. The prologue and epilogue
18736          attributes allow a compiler to communicate the location(s) to use.  */
18737
18738       {
18739         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18740
18741         if (fde->dw_fde_vms_end_prologue)
18742           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18743             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18744
18745         if (fde->dw_fde_vms_begin_epilogue)
18746           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18747             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18748       }
18749 #endif
18750
18751           add_pubname (decl, subr_die);
18752           add_arange (decl, subr_die);
18753         }
18754       else
18755         {  /* Do nothing for now; maybe need to duplicate die, one for
18756               hot section and one for cold section, then use the hot/cold
18757               section begin/end labels to generate the aranges...  */
18758           /*
18759             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18760             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18761             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18762             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18763
18764             add_pubname (decl, subr_die);
18765             add_arange (decl, subr_die);
18766             add_arange (decl, subr_die);
18767            */
18768         }
18769
18770 #ifdef MIPS_DEBUGGING_INFO
18771       /* Add a reference to the FDE for this routine.  */
18772       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18773 #endif
18774
18775       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18776
18777       /* We define the "frame base" as the function's CFA.  This is more
18778          convenient for several reasons: (1) It's stable across the prologue
18779          and epilogue, which makes it better than just a frame pointer,
18780          (2) With dwarf3, there exists a one-byte encoding that allows us
18781          to reference the .debug_frame data by proxy, but failing that,
18782          (3) We can at least reuse the code inspection and interpretation
18783          code that determines the CFA position at various points in the
18784          function.  */
18785       if (dwarf_version >= 3)
18786         {
18787           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18788           add_AT_loc (subr_die, DW_AT_frame_base, op);
18789         }
18790       else
18791         {
18792           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18793           if (list->dw_loc_next)
18794             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18795           else
18796             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18797         }
18798
18799       /* Compute a displacement from the "steady-state frame pointer" to
18800          the CFA.  The former is what all stack slots and argument slots
18801          will reference in the rtl; the later is what we've told the
18802          debugger about.  We'll need to adjust all frame_base references
18803          by this displacement.  */
18804       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18805
18806       if (cfun->static_chain_decl)
18807         add_AT_location_description (subr_die, DW_AT_static_link,
18808                  loc_list_from_tree (cfun->static_chain_decl, 2));
18809     }
18810
18811   /* Generate child dies for template paramaters.  */
18812   if (debug_info_level > DINFO_LEVEL_TERSE)
18813     gen_generic_params_dies (decl);
18814
18815   /* Now output descriptions of the arguments for this function. This gets
18816      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18817      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18818      `...' at the end of the formal parameter list.  In order to find out if
18819      there was a trailing ellipsis or not, we must instead look at the type
18820      associated with the FUNCTION_DECL.  This will be a node of type
18821      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18822      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18823      an ellipsis at the end.  */
18824
18825   /* In the case where we are describing a mere function declaration, all we
18826      need to do here (and all we *can* do here) is to describe the *types* of
18827      its formal parameters.  */
18828   if (debug_info_level <= DINFO_LEVEL_TERSE)
18829     ;
18830   else if (declaration)
18831     gen_formal_types_die (decl, subr_die);
18832   else
18833     {
18834       /* Generate DIEs to represent all known formal parameters.  */
18835       tree parm = DECL_ARGUMENTS (decl);
18836       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18837       tree generic_decl_parm = generic_decl
18838                                 ? DECL_ARGUMENTS (generic_decl)
18839                                 : NULL;
18840
18841       /* Now we want to walk the list of parameters of the function and
18842          emit their relevant DIEs.
18843
18844          We consider the case of DECL being an instance of a generic function
18845          as well as it being a normal function.
18846
18847          If DECL is an instance of a generic function we walk the
18848          parameters of the generic function declaration _and_ the parameters of
18849          DECL itself. This is useful because we want to emit specific DIEs for
18850          function parameter packs and those are declared as part of the
18851          generic function declaration. In that particular case,
18852          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18853          That DIE has children DIEs representing the set of arguments
18854          of the pack. Note that the set of pack arguments can be empty.
18855          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18856          children DIE.
18857
18858          Otherwise, we just consider the parameters of DECL.  */
18859       while (generic_decl_parm || parm)
18860         {
18861           if (generic_decl_parm
18862               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18863             gen_formal_parameter_pack_die (generic_decl_parm,
18864                                            parm, subr_die,
18865                                            &parm);
18866           else if (parm)
18867             {
18868               gen_decl_die (parm, NULL, subr_die);
18869               parm = DECL_CHAIN (parm);
18870             }
18871
18872           if (generic_decl_parm)
18873             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18874         }
18875
18876       /* Decide whether we need an unspecified_parameters DIE at the end.
18877          There are 2 more cases to do this for: 1) the ansi ... declaration -
18878          this is detectable when the end of the arg list is not a
18879          void_type_node 2) an unprototyped function declaration (not a
18880          definition).  This just means that we have no info about the
18881          parameters at all.  */
18882       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18883       if (fn_arg_types != NULL)
18884         {
18885           /* This is the prototyped case, check for....  */
18886           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18887             gen_unspecified_parameters_die (decl, subr_die);
18888         }
18889       else if (DECL_INITIAL (decl) == NULL_TREE)
18890         gen_unspecified_parameters_die (decl, subr_die);
18891     }
18892
18893   /* Output Dwarf info for all of the stuff within the body of the function
18894      (if it has one - it may be just a declaration).  */
18895   outer_scope = DECL_INITIAL (decl);
18896
18897   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18898      a function.  This BLOCK actually represents the outermost binding contour
18899      for the function, i.e. the contour in which the function's formal
18900      parameters and labels get declared. Curiously, it appears that the front
18901      end doesn't actually put the PARM_DECL nodes for the current function onto
18902      the BLOCK_VARS list for this outer scope, but are strung off of the
18903      DECL_ARGUMENTS list for the function instead.
18904
18905      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18906      the LABEL_DECL nodes for the function however, and we output DWARF info
18907      for those in decls_for_scope.  Just within the `outer_scope' there will be
18908      a BLOCK node representing the function's outermost pair of curly braces,
18909      and any blocks used for the base and member initializers of a C++
18910      constructor function.  */
18911   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18912     {
18913       /* Emit a DW_TAG_variable DIE for a named return value.  */
18914       if (DECL_NAME (DECL_RESULT (decl)))
18915         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18916
18917       current_function_has_inlines = 0;
18918       decls_for_scope (outer_scope, subr_die, 0);
18919
18920 #if 0 && defined (MIPS_DEBUGGING_INFO)
18921       if (current_function_has_inlines)
18922         {
18923           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18924           if (! comp_unit_has_inlines)
18925             {
18926               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18927               comp_unit_has_inlines = 1;
18928             }
18929         }
18930 #endif
18931     }
18932   /* Add the calling convention attribute if requested.  */
18933   add_calling_convention_attribute (subr_die, decl);
18934
18935 }
18936
18937 /* Returns a hash value for X (which really is a die_struct).  */
18938
18939 static hashval_t
18940 common_block_die_table_hash (const void *x)
18941 {
18942   const_dw_die_ref d = (const_dw_die_ref) x;
18943   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18944 }
18945
18946 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18947    as decl_id and die_parent of die_struct Y.  */
18948
18949 static int
18950 common_block_die_table_eq (const void *x, const void *y)
18951 {
18952   const_dw_die_ref d = (const_dw_die_ref) x;
18953   const_dw_die_ref e = (const_dw_die_ref) y;
18954   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18955 }
18956
18957 /* Generate a DIE to represent a declared data object.
18958    Either DECL or ORIGIN must be non-null.  */
18959
18960 static void
18961 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18962 {
18963   HOST_WIDE_INT off;
18964   tree com_decl;
18965   tree decl_or_origin = decl ? decl : origin;
18966   tree ultimate_origin;
18967   dw_die_ref var_die;
18968   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18969   dw_die_ref origin_die;
18970   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18971                       || class_or_namespace_scope_p (context_die));
18972   bool specialization_p = false;
18973
18974   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18975   if (decl || ultimate_origin)
18976     origin = ultimate_origin;
18977   com_decl = fortran_common (decl_or_origin, &off);
18978
18979   /* Symbol in common gets emitted as a child of the common block, in the form
18980      of a data member.  */
18981   if (com_decl)
18982     {
18983       dw_die_ref com_die;
18984       dw_loc_list_ref loc;
18985       die_node com_die_arg;
18986
18987       var_die = lookup_decl_die (decl_or_origin);
18988       if (var_die)
18989         {
18990           if (get_AT (var_die, DW_AT_location) == NULL)
18991             {
18992               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18993               if (loc)
18994                 {
18995                   if (off)
18996                     {
18997                       /* Optimize the common case.  */
18998                       if (single_element_loc_list_p (loc)
18999                           && loc->expr->dw_loc_opc == DW_OP_addr
19000                           && loc->expr->dw_loc_next == NULL
19001                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19002                              == SYMBOL_REF)
19003                         loc->expr->dw_loc_oprnd1.v.val_addr
19004                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19005                         else
19006                           loc_list_plus_const (loc, off);
19007                     }
19008                   add_AT_location_description (var_die, DW_AT_location, loc);
19009                   remove_AT (var_die, DW_AT_declaration);
19010                 }
19011             }
19012           return;
19013         }
19014
19015       if (common_block_die_table == NULL)
19016         common_block_die_table
19017           = htab_create_ggc (10, common_block_die_table_hash,
19018                              common_block_die_table_eq, NULL);
19019
19020       com_die_arg.decl_id = DECL_UID (com_decl);
19021       com_die_arg.die_parent = context_die;
19022       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19023       loc = loc_list_from_tree (com_decl, 2);
19024       if (com_die == NULL)
19025         {
19026           const char *cnam
19027             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19028           void **slot;
19029
19030           com_die = new_die (DW_TAG_common_block, context_die, decl);
19031           add_name_and_src_coords_attributes (com_die, com_decl);
19032           if (loc)
19033             {
19034               add_AT_location_description (com_die, DW_AT_location, loc);
19035               /* Avoid sharing the same loc descriptor between
19036                  DW_TAG_common_block and DW_TAG_variable.  */
19037               loc = loc_list_from_tree (com_decl, 2);
19038             }
19039           else if (DECL_EXTERNAL (decl))
19040             add_AT_flag (com_die, DW_AT_declaration, 1);
19041           add_pubname_string (cnam, com_die); /* ??? needed? */
19042           com_die->decl_id = DECL_UID (com_decl);
19043           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19044           *slot = (void *) com_die;
19045         }
19046       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19047         {
19048           add_AT_location_description (com_die, DW_AT_location, loc);
19049           loc = loc_list_from_tree (com_decl, 2);
19050           remove_AT (com_die, DW_AT_declaration);
19051         }
19052       var_die = new_die (DW_TAG_variable, com_die, decl);
19053       add_name_and_src_coords_attributes (var_die, decl);
19054       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19055                           TREE_THIS_VOLATILE (decl), context_die);
19056       add_AT_flag (var_die, DW_AT_external, 1);
19057       if (loc)
19058         {
19059           if (off)
19060             {
19061               /* Optimize the common case.  */
19062               if (single_element_loc_list_p (loc)
19063                   && loc->expr->dw_loc_opc == DW_OP_addr
19064                   && loc->expr->dw_loc_next == NULL
19065                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19066                 loc->expr->dw_loc_oprnd1.v.val_addr
19067                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19068               else
19069                 loc_list_plus_const (loc, off);
19070             }
19071           add_AT_location_description (var_die, DW_AT_location, loc);
19072         }
19073       else if (DECL_EXTERNAL (decl))
19074         add_AT_flag (var_die, DW_AT_declaration, 1);
19075       equate_decl_number_to_die (decl, var_die);
19076       return;
19077     }
19078
19079   /* If the compiler emitted a definition for the DECL declaration
19080      and if we already emitted a DIE for it, don't emit a second
19081      DIE for it again. Allow re-declarations of DECLs that are
19082      inside functions, though.  */
19083   if (old_die && declaration && !local_scope_p (context_die))
19084     return;
19085
19086   /* For static data members, the declaration in the class is supposed
19087      to have DW_TAG_member tag; the specification should still be
19088      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19089   if (declaration && class_scope_p (context_die))
19090     var_die = new_die (DW_TAG_member, context_die, decl);
19091   else
19092     var_die = new_die (DW_TAG_variable, context_die, decl);
19093
19094   origin_die = NULL;
19095   if (origin != NULL)
19096     origin_die = add_abstract_origin_attribute (var_die, origin);
19097
19098   /* Loop unrolling can create multiple blocks that refer to the same
19099      static variable, so we must test for the DW_AT_declaration flag.
19100
19101      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19102      copy decls and set the DECL_ABSTRACT flag on them instead of
19103      sharing them.
19104
19105      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19106
19107      ??? The declare_in_namespace support causes us to get two DIEs for one
19108      variable, both of which are declarations.  We want to avoid considering
19109      one to be a specification, so we must test that this DIE is not a
19110      declaration.  */
19111   else if (old_die && TREE_STATIC (decl) && ! declaration
19112            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19113     {
19114       /* This is a definition of a C++ class level static.  */
19115       add_AT_specification (var_die, old_die);
19116       specialization_p = true;
19117       if (DECL_NAME (decl))
19118         {
19119           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19120           struct dwarf_file_data * file_index = lookup_filename (s.file);
19121
19122           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19123             add_AT_file (var_die, DW_AT_decl_file, file_index);
19124
19125           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19126             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19127
19128           if (old_die->die_tag == DW_TAG_member)
19129             add_linkage_name (var_die, decl);
19130         }
19131     }
19132   else
19133     add_name_and_src_coords_attributes (var_die, decl);
19134
19135   if ((origin == NULL && !specialization_p)
19136       || (origin != NULL
19137           && !DECL_ABSTRACT (decl_or_origin)
19138           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19139                                        decl_function_context
19140                                                         (decl_or_origin))))
19141     {
19142       tree type = TREE_TYPE (decl_or_origin);
19143
19144       if (decl_by_reference_p (decl_or_origin))
19145         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19146       else
19147         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19148                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19149     }
19150
19151   if (origin == NULL && !specialization_p)
19152     {
19153       if (TREE_PUBLIC (decl))
19154         add_AT_flag (var_die, DW_AT_external, 1);
19155
19156       if (DECL_ARTIFICIAL (decl))
19157         add_AT_flag (var_die, DW_AT_artificial, 1);
19158
19159       add_accessibility_attribute (var_die, decl);
19160     }
19161
19162   if (declaration)
19163     add_AT_flag (var_die, DW_AT_declaration, 1);
19164
19165   if (decl && (DECL_ABSTRACT (decl) || declaration))
19166     equate_decl_number_to_die (decl, var_die);
19167
19168   if (! declaration
19169       && (! DECL_ABSTRACT (decl_or_origin)
19170           /* Local static vars are shared between all clones/inlines,
19171              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19172              already set.  */
19173           || (TREE_CODE (decl_or_origin) == VAR_DECL
19174               && TREE_STATIC (decl_or_origin)
19175               && DECL_RTL_SET_P (decl_or_origin)))
19176       /* When abstract origin already has DW_AT_location attribute, no need
19177          to add it again.  */
19178       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19179     {
19180       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19181           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19182         defer_location (decl_or_origin, var_die);
19183       else
19184         add_location_or_const_value_attribute (var_die,
19185                                                decl_or_origin,
19186                                                DW_AT_location);
19187       add_pubname (decl_or_origin, var_die);
19188     }
19189   else
19190     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19191 }
19192
19193 /* Generate a DIE to represent a named constant.  */
19194
19195 static void
19196 gen_const_die (tree decl, dw_die_ref context_die)
19197 {
19198   dw_die_ref const_die;
19199   tree type = TREE_TYPE (decl);
19200
19201   const_die = new_die (DW_TAG_constant, context_die, decl);
19202   add_name_and_src_coords_attributes (const_die, decl);
19203   add_type_attribute (const_die, type, 1, 0, context_die);
19204   if (TREE_PUBLIC (decl))
19205     add_AT_flag (const_die, DW_AT_external, 1);
19206   if (DECL_ARTIFICIAL (decl))
19207     add_AT_flag (const_die, DW_AT_artificial, 1);
19208   tree_add_const_value_attribute_for_decl (const_die, decl);
19209 }
19210
19211 /* Generate a DIE to represent a label identifier.  */
19212
19213 static void
19214 gen_label_die (tree decl, dw_die_ref context_die)
19215 {
19216   tree origin = decl_ultimate_origin (decl);
19217   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19218   rtx insn;
19219   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19220
19221   if (origin != NULL)
19222     add_abstract_origin_attribute (lbl_die, origin);
19223   else
19224     add_name_and_src_coords_attributes (lbl_die, decl);
19225
19226   if (DECL_ABSTRACT (decl))
19227     equate_decl_number_to_die (decl, lbl_die);
19228   else
19229     {
19230       insn = DECL_RTL_IF_SET (decl);
19231
19232       /* Deleted labels are programmer specified labels which have been
19233          eliminated because of various optimizations.  We still emit them
19234          here so that it is possible to put breakpoints on them.  */
19235       if (insn
19236           && (LABEL_P (insn)
19237               || ((NOTE_P (insn)
19238                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19239         {
19240           /* When optimization is enabled (via -O) some parts of the compiler
19241              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19242              represent source-level labels which were explicitly declared by
19243              the user.  This really shouldn't be happening though, so catch
19244              it if it ever does happen.  */
19245           gcc_assert (!INSN_DELETED_P (insn));
19246
19247           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19248           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19249         }
19250     }
19251 }
19252
19253 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19254    attributes to the DIE for a block STMT, to describe where the inlined
19255    function was called from.  This is similar to add_src_coords_attributes.  */
19256
19257 static inline void
19258 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19259 {
19260   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19261
19262   if (dwarf_version >= 3 || !dwarf_strict)
19263     {
19264       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19265       add_AT_unsigned (die, DW_AT_call_line, s.line);
19266     }
19267 }
19268
19269
19270 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19271    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19272
19273 static inline void
19274 add_high_low_attributes (tree stmt, dw_die_ref die)
19275 {
19276   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19277
19278   if (BLOCK_FRAGMENT_CHAIN (stmt)
19279       && (dwarf_version >= 3 || !dwarf_strict))
19280     {
19281       tree chain;
19282
19283       if (inlined_function_outer_scope_p (stmt))
19284         {
19285           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19286                                        BLOCK_NUMBER (stmt));
19287           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19288         }
19289
19290       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19291
19292       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19293       do
19294         {
19295           add_ranges (chain);
19296           chain = BLOCK_FRAGMENT_CHAIN (chain);
19297         }
19298       while (chain);
19299       add_ranges (NULL);
19300     }
19301   else
19302     {
19303       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19304                                    BLOCK_NUMBER (stmt));
19305       add_AT_lbl_id (die, DW_AT_low_pc, label);
19306       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19307                                    BLOCK_NUMBER (stmt));
19308       add_AT_lbl_id (die, DW_AT_high_pc, label);
19309     }
19310 }
19311
19312 /* Generate a DIE for a lexical block.  */
19313
19314 static void
19315 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19316 {
19317   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19318
19319   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19320     add_high_low_attributes (stmt, stmt_die);
19321
19322   decls_for_scope (stmt, stmt_die, depth);
19323 }
19324
19325 /* Generate a DIE for an inlined subprogram.  */
19326
19327 static void
19328 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19329 {
19330   tree decl;
19331
19332   /* The instance of function that is effectively being inlined shall not
19333      be abstract.  */
19334   gcc_assert (! BLOCK_ABSTRACT (stmt));
19335
19336   decl = block_ultimate_origin (stmt);
19337
19338   /* Emit info for the abstract instance first, if we haven't yet.  We
19339      must emit this even if the block is abstract, otherwise when we
19340      emit the block below (or elsewhere), we may end up trying to emit
19341      a die whose origin die hasn't been emitted, and crashing.  */
19342   dwarf2out_abstract_function (decl);
19343
19344   if (! BLOCK_ABSTRACT (stmt))
19345     {
19346       dw_die_ref subr_die
19347         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19348
19349       add_abstract_origin_attribute (subr_die, decl);
19350       if (TREE_ASM_WRITTEN (stmt))
19351         add_high_low_attributes (stmt, subr_die);
19352       add_call_src_coords_attributes (stmt, subr_die);
19353
19354       decls_for_scope (stmt, subr_die, depth);
19355       current_function_has_inlines = 1;
19356     }
19357 }
19358
19359 /* Generate a DIE for a field in a record, or structure.  */
19360
19361 static void
19362 gen_field_die (tree decl, dw_die_ref context_die)
19363 {
19364   dw_die_ref decl_die;
19365
19366   if (TREE_TYPE (decl) == error_mark_node)
19367     return;
19368
19369   decl_die = new_die (DW_TAG_member, context_die, decl);
19370   add_name_and_src_coords_attributes (decl_die, decl);
19371   add_type_attribute (decl_die, member_declared_type (decl),
19372                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19373                       context_die);
19374
19375   if (DECL_BIT_FIELD_TYPE (decl))
19376     {
19377       add_byte_size_attribute (decl_die, decl);
19378       add_bit_size_attribute (decl_die, decl);
19379       add_bit_offset_attribute (decl_die, decl);
19380     }
19381
19382   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19383     add_data_member_location_attribute (decl_die, decl);
19384
19385   if (DECL_ARTIFICIAL (decl))
19386     add_AT_flag (decl_die, DW_AT_artificial, 1);
19387
19388   add_accessibility_attribute (decl_die, decl);
19389
19390   /* Equate decl number to die, so that we can look up this decl later on.  */
19391   equate_decl_number_to_die (decl, decl_die);
19392 }
19393
19394 #if 0
19395 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19396    Use modified_type_die instead.
19397    We keep this code here just in case these types of DIEs may be needed to
19398    represent certain things in other languages (e.g. Pascal) someday.  */
19399
19400 static void
19401 gen_pointer_type_die (tree type, dw_die_ref context_die)
19402 {
19403   dw_die_ref ptr_die
19404     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19405
19406   equate_type_number_to_die (type, ptr_die);
19407   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19408   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19409 }
19410
19411 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19412    Use modified_type_die instead.
19413    We keep this code here just in case these types of DIEs may be needed to
19414    represent certain things in other languages (e.g. Pascal) someday.  */
19415
19416 static void
19417 gen_reference_type_die (tree type, dw_die_ref context_die)
19418 {
19419   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19420
19421   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19422     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19423   else
19424     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19425
19426   equate_type_number_to_die (type, ref_die);
19427   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19428   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19429 }
19430 #endif
19431
19432 /* Generate a DIE for a pointer to a member type.  */
19433
19434 static void
19435 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19436 {
19437   dw_die_ref ptr_die
19438     = new_die (DW_TAG_ptr_to_member_type,
19439                scope_die_for (type, context_die), type);
19440
19441   equate_type_number_to_die (type, ptr_die);
19442   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19443                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19444   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19445 }
19446
19447 /* Generate the DIE for the compilation unit.  */
19448
19449 static dw_die_ref
19450 gen_compile_unit_die (const char *filename)
19451 {
19452   dw_die_ref die;
19453   char producer[250];
19454   const char *language_string = lang_hooks.name;
19455   int language;
19456
19457   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19458
19459   if (filename)
19460     {
19461       add_name_attribute (die, filename);
19462       /* Don't add cwd for <built-in>.  */
19463       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19464         add_comp_dir_attribute (die);
19465     }
19466
19467   sprintf (producer, "%s %s", language_string, version_string);
19468
19469 #ifdef MIPS_DEBUGGING_INFO
19470   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19471      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19472      not appear in the producer string, the debugger reaches the conclusion
19473      that the object file is stripped and has no debugging information.
19474      To get the MIPS/SGI debugger to believe that there is debugging
19475      information in the object file, we add a -g to the producer string.  */
19476   if (debug_info_level > DINFO_LEVEL_TERSE)
19477     strcat (producer, " -g");
19478 #endif
19479
19480   add_AT_string (die, DW_AT_producer, producer);
19481
19482   language = DW_LANG_C89;
19483   if (strcmp (language_string, "GNU C++") == 0)
19484     language = DW_LANG_C_plus_plus;
19485   else if (strcmp (language_string, "GNU F77") == 0)
19486     language = DW_LANG_Fortran77;
19487   else if (strcmp (language_string, "GNU Pascal") == 0)
19488     language = DW_LANG_Pascal83;
19489   else if (dwarf_version >= 3 || !dwarf_strict)
19490     {
19491       if (strcmp (language_string, "GNU Ada") == 0)
19492         language = DW_LANG_Ada95;
19493       else if (strcmp (language_string, "GNU Fortran") == 0)
19494         language = DW_LANG_Fortran95;
19495       else if (strcmp (language_string, "GNU Java") == 0)
19496         language = DW_LANG_Java;
19497       else if (strcmp (language_string, "GNU Objective-C") == 0)
19498         language = DW_LANG_ObjC;
19499       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19500         language = DW_LANG_ObjC_plus_plus;
19501     }
19502
19503   add_AT_unsigned (die, DW_AT_language, language);
19504
19505   switch (language)
19506     {
19507     case DW_LANG_Fortran77:
19508     case DW_LANG_Fortran90:
19509     case DW_LANG_Fortran95:
19510       /* Fortran has case insensitive identifiers and the front-end
19511          lowercases everything.  */
19512       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19513       break;
19514     default:
19515       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19516       break;
19517     }
19518   return die;
19519 }
19520
19521 /* Generate the DIE for a base class.  */
19522
19523 static void
19524 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19525 {
19526   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19527
19528   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19529   add_data_member_location_attribute (die, binfo);
19530
19531   if (BINFO_VIRTUAL_P (binfo))
19532     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19533
19534   if (access == access_public_node)
19535     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19536   else if (access == access_protected_node)
19537     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19538 }
19539
19540 /* Generate a DIE for a class member.  */
19541
19542 static void
19543 gen_member_die (tree type, dw_die_ref context_die)
19544 {
19545   tree member;
19546   tree binfo = TYPE_BINFO (type);
19547   dw_die_ref child;
19548
19549   /* If this is not an incomplete type, output descriptions of each of its
19550      members. Note that as we output the DIEs necessary to represent the
19551      members of this record or union type, we will also be trying to output
19552      DIEs to represent the *types* of those members. However the `type'
19553      function (above) will specifically avoid generating type DIEs for member
19554      types *within* the list of member DIEs for this (containing) type except
19555      for those types (of members) which are explicitly marked as also being
19556      members of this (containing) type themselves.  The g++ front- end can
19557      force any given type to be treated as a member of some other (containing)
19558      type by setting the TYPE_CONTEXT of the given (member) type to point to
19559      the TREE node representing the appropriate (containing) type.  */
19560
19561   /* First output info about the base classes.  */
19562   if (binfo)
19563     {
19564       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19565       int i;
19566       tree base;
19567
19568       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19569         gen_inheritance_die (base,
19570                              (accesses ? VEC_index (tree, accesses, i)
19571                               : access_public_node), context_die);
19572     }
19573
19574   /* Now output info about the data members and type members.  */
19575   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19576     {
19577       /* If we thought we were generating minimal debug info for TYPE
19578          and then changed our minds, some of the member declarations
19579          may have already been defined.  Don't define them again, but
19580          do put them in the right order.  */
19581
19582       child = lookup_decl_die (member);
19583       if (child)
19584         splice_child_die (context_die, child);
19585       else
19586         gen_decl_die (member, NULL, context_die);
19587     }
19588
19589   /* Now output info about the function members (if any).  */
19590   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19591     {
19592       /* Don't include clones in the member list.  */
19593       if (DECL_ABSTRACT_ORIGIN (member))
19594         continue;
19595
19596       child = lookup_decl_die (member);
19597       if (child)
19598         splice_child_die (context_die, child);
19599       else
19600         gen_decl_die (member, NULL, context_die);
19601     }
19602 }
19603
19604 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19605    is set, we pretend that the type was never defined, so we only get the
19606    member DIEs needed by later specification DIEs.  */
19607
19608 static void
19609 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19610                                 enum debug_info_usage usage)
19611 {
19612   dw_die_ref type_die = lookup_type_die (type);
19613   dw_die_ref scope_die = 0;
19614   int nested = 0;
19615   int complete = (TYPE_SIZE (type)
19616                   && (! TYPE_STUB_DECL (type)
19617                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19618   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19619   complete = complete && should_emit_struct_debug (type, usage);
19620
19621   if (type_die && ! complete)
19622     return;
19623
19624   if (TYPE_CONTEXT (type) != NULL_TREE
19625       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19626           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19627     nested = 1;
19628
19629   scope_die = scope_die_for (type, context_die);
19630
19631   if (! type_die || (nested && scope_die == comp_unit_die))
19632     /* First occurrence of type or toplevel definition of nested class.  */
19633     {
19634       dw_die_ref old_die = type_die;
19635
19636       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19637                           ? record_type_tag (type) : DW_TAG_union_type,
19638                           scope_die, type);
19639       equate_type_number_to_die (type, type_die);
19640       if (old_die)
19641         add_AT_specification (type_die, old_die);
19642       else
19643         add_name_attribute (type_die, type_tag (type));
19644     }
19645   else
19646     remove_AT (type_die, DW_AT_declaration);
19647
19648   /* Generate child dies for template paramaters.  */
19649   if (debug_info_level > DINFO_LEVEL_TERSE
19650       && COMPLETE_TYPE_P (type))
19651     gen_generic_params_dies (type);
19652
19653   /* If this type has been completed, then give it a byte_size attribute and
19654      then give a list of members.  */
19655   if (complete && !ns_decl)
19656     {
19657       /* Prevent infinite recursion in cases where the type of some member of
19658          this type is expressed in terms of this type itself.  */
19659       TREE_ASM_WRITTEN (type) = 1;
19660       add_byte_size_attribute (type_die, type);
19661       if (TYPE_STUB_DECL (type) != NULL_TREE)
19662         {
19663           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19664           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19665         }
19666
19667       /* If the first reference to this type was as the return type of an
19668          inline function, then it may not have a parent.  Fix this now.  */
19669       if (type_die->die_parent == NULL)
19670         add_child_die (scope_die, type_die);
19671
19672       push_decl_scope (type);
19673       gen_member_die (type, type_die);
19674       pop_decl_scope ();
19675
19676       /* GNU extension: Record what type our vtable lives in.  */
19677       if (TYPE_VFIELD (type))
19678         {
19679           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19680
19681           gen_type_die (vtype, context_die);
19682           add_AT_die_ref (type_die, DW_AT_containing_type,
19683                           lookup_type_die (vtype));
19684         }
19685     }
19686   else
19687     {
19688       add_AT_flag (type_die, DW_AT_declaration, 1);
19689
19690       /* We don't need to do this for function-local types.  */
19691       if (TYPE_STUB_DECL (type)
19692           && ! decl_function_context (TYPE_STUB_DECL (type)))
19693         VEC_safe_push (tree, gc, incomplete_types, type);
19694     }
19695
19696   if (get_AT (type_die, DW_AT_name))
19697     add_pubtype (type, type_die);
19698 }
19699
19700 /* Generate a DIE for a subroutine _type_.  */
19701
19702 static void
19703 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19704 {
19705   tree return_type = TREE_TYPE (type);
19706   dw_die_ref subr_die
19707     = new_die (DW_TAG_subroutine_type,
19708                scope_die_for (type, context_die), type);
19709
19710   equate_type_number_to_die (type, subr_die);
19711   add_prototyped_attribute (subr_die, type);
19712   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19713   gen_formal_types_die (type, subr_die);
19714
19715   if (get_AT (subr_die, DW_AT_name))
19716     add_pubtype (type, subr_die);
19717 }
19718
19719 /* Generate a DIE for a type definition.  */
19720
19721 static void
19722 gen_typedef_die (tree decl, dw_die_ref context_die)
19723 {
19724   dw_die_ref type_die;
19725   tree origin;
19726
19727   if (TREE_ASM_WRITTEN (decl))
19728     return;
19729
19730   TREE_ASM_WRITTEN (decl) = 1;
19731   type_die = new_die (DW_TAG_typedef, context_die, decl);
19732   origin = decl_ultimate_origin (decl);
19733   if (origin != NULL)
19734     add_abstract_origin_attribute (type_die, origin);
19735   else
19736     {
19737       tree type;
19738
19739       add_name_and_src_coords_attributes (type_die, decl);
19740       if (DECL_ORIGINAL_TYPE (decl))
19741         {
19742           type = DECL_ORIGINAL_TYPE (decl);
19743
19744           gcc_assert (type != TREE_TYPE (decl));
19745           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19746         }
19747       else
19748         {
19749           type = TREE_TYPE (decl);
19750
19751           if (is_naming_typedef_decl (TYPE_NAME (type)))
19752             /* 
19753                Here, we are in the case of decl being a typedef naming
19754                an anonymous type, e.g:
19755                      typedef struct {...} foo;
19756                In that case TREE_TYPE (decl) is not a typedef variant
19757                type and TYPE_NAME of the anonymous type is set to the
19758                TYPE_DECL of the typedef. This construct is emitted by
19759                the C++ FE.
19760
19761                TYPE is the anonymous struct named by the typedef
19762                DECL. As we need the DW_AT_type attribute of the
19763                DW_TAG_typedef to point to the DIE of TYPE, let's
19764                generate that DIE right away. add_type_attribute
19765                called below will then pick (via lookup_type_die) that
19766                anonymous struct DIE.  */
19767             gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19768         }
19769
19770       add_type_attribute (type_die, type, TREE_READONLY (decl),
19771                           TREE_THIS_VOLATILE (decl), context_die);
19772
19773       if (is_naming_typedef_decl (decl))
19774         /* We want that all subsequent calls to lookup_type_die with
19775            TYPE in argument yield the DW_TAG_typedef we have just
19776            created.  */
19777         equate_type_number_to_die (type, type_die);
19778
19779       add_accessibility_attribute (type_die, decl);
19780     }
19781
19782   if (DECL_ABSTRACT (decl))
19783     equate_decl_number_to_die (decl, type_die);
19784
19785   if (get_AT (type_die, DW_AT_name))
19786     add_pubtype (decl, type_die);
19787 }
19788
19789 /* Generate a DIE for a struct, class, enum or union type.  */
19790
19791 static void
19792 gen_tagged_type_die (tree type,
19793                      dw_die_ref context_die,
19794                      enum debug_info_usage usage)
19795 {
19796   int need_pop;
19797
19798   if (type == NULL_TREE
19799       || !is_tagged_type (type))
19800     return;
19801
19802   /* If this is a nested type whose containing class hasn't been written
19803      out yet, writing it out will cover this one, too.  This does not apply
19804      to instantiations of member class templates; they need to be added to
19805      the containing class as they are generated.  FIXME: This hurts the
19806      idea of combining type decls from multiple TUs, since we can't predict
19807      what set of template instantiations we'll get.  */
19808   if (TYPE_CONTEXT (type)
19809       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19810       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19811     {
19812       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19813
19814       if (TREE_ASM_WRITTEN (type))
19815         return;
19816
19817       /* If that failed, attach ourselves to the stub.  */
19818       push_decl_scope (TYPE_CONTEXT (type));
19819       context_die = lookup_type_die (TYPE_CONTEXT (type));
19820       need_pop = 1;
19821     }
19822   else if (TYPE_CONTEXT (type) != NULL_TREE
19823            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19824     {
19825       /* If this type is local to a function that hasn't been written
19826          out yet, use a NULL context for now; it will be fixed up in
19827          decls_for_scope.  */
19828       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19829       need_pop = 0;
19830     }
19831   else
19832     {
19833       context_die = declare_in_namespace (type, context_die);
19834       need_pop = 0;
19835     }
19836
19837   if (TREE_CODE (type) == ENUMERAL_TYPE)
19838     {
19839       /* This might have been written out by the call to
19840          declare_in_namespace.  */
19841       if (!TREE_ASM_WRITTEN (type))
19842         gen_enumeration_type_die (type, context_die);
19843     }
19844   else
19845     gen_struct_or_union_type_die (type, context_die, usage);
19846
19847   if (need_pop)
19848     pop_decl_scope ();
19849
19850   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19851      it up if it is ever completed.  gen_*_type_die will set it for us
19852      when appropriate.  */
19853 }
19854
19855 /* Generate a type description DIE.  */
19856
19857 static void
19858 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19859                                 enum debug_info_usage usage)
19860 {
19861   struct array_descr_info info;
19862
19863   if (type == NULL_TREE || type == error_mark_node)
19864     return;
19865
19866   /* If TYPE is a typedef type variant, let's generate debug info
19867      for the parent typedef which TYPE is a type of.  */
19868   if (typedef_variant_p (type))
19869     {
19870       if (TREE_ASM_WRITTEN (type))
19871         return;
19872
19873       /* Prevent broken recursion; we can't hand off to the same type.  */
19874       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19875
19876       /* Use the DIE of the containing namespace as the parent DIE of
19877          the type description DIE we want to generate.  */
19878       if (DECL_CONTEXT (TYPE_NAME (type))
19879           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19880         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19881
19882       TREE_ASM_WRITTEN (type) = 1;
19883
19884       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19885       return;
19886     }
19887
19888   /* If type is an anonymous tagged type named by a typedef, let's
19889      generate debug info for the typedef.  */
19890   if (is_naming_typedef_decl (TYPE_NAME (type)))
19891     {
19892       /* Use the DIE of the containing namespace as the parent DIE of
19893          the type description DIE we want to generate.  */
19894       if (DECL_CONTEXT (TYPE_NAME (type))
19895           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19896         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19897       
19898       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19899       return;
19900     }
19901
19902   /* If this is an array type with hidden descriptor, handle it first.  */
19903   if (!TREE_ASM_WRITTEN (type)
19904       && lang_hooks.types.get_array_descr_info
19905       && lang_hooks.types.get_array_descr_info (type, &info)
19906       && (dwarf_version >= 3 || !dwarf_strict))
19907     {
19908       gen_descr_array_type_die (type, &info, context_die);
19909       TREE_ASM_WRITTEN (type) = 1;
19910       return;
19911     }
19912
19913   /* We are going to output a DIE to represent the unqualified version
19914      of this type (i.e. without any const or volatile qualifiers) so
19915      get the main variant (i.e. the unqualified version) of this type
19916      now.  (Vectors are special because the debugging info is in the
19917      cloned type itself).  */
19918   if (TREE_CODE (type) != VECTOR_TYPE)
19919     type = type_main_variant (type);
19920
19921   if (TREE_ASM_WRITTEN (type))
19922     return;
19923
19924   switch (TREE_CODE (type))
19925     {
19926     case ERROR_MARK:
19927       break;
19928
19929     case POINTER_TYPE:
19930     case REFERENCE_TYPE:
19931       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19932          ensures that the gen_type_die recursion will terminate even if the
19933          type is recursive.  Recursive types are possible in Ada.  */
19934       /* ??? We could perhaps do this for all types before the switch
19935          statement.  */
19936       TREE_ASM_WRITTEN (type) = 1;
19937
19938       /* For these types, all that is required is that we output a DIE (or a
19939          set of DIEs) to represent the "basis" type.  */
19940       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19941                                 DINFO_USAGE_IND_USE);
19942       break;
19943
19944     case OFFSET_TYPE:
19945       /* This code is used for C++ pointer-to-data-member types.
19946          Output a description of the relevant class type.  */
19947       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19948                                         DINFO_USAGE_IND_USE);
19949
19950       /* Output a description of the type of the object pointed to.  */
19951       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19952                                         DINFO_USAGE_IND_USE);
19953
19954       /* Now output a DIE to represent this pointer-to-data-member type
19955          itself.  */
19956       gen_ptr_to_mbr_type_die (type, context_die);
19957       break;
19958
19959     case FUNCTION_TYPE:
19960       /* Force out return type (in case it wasn't forced out already).  */
19961       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19962                                         DINFO_USAGE_DIR_USE);
19963       gen_subroutine_type_die (type, context_die);
19964       break;
19965
19966     case METHOD_TYPE:
19967       /* Force out return type (in case it wasn't forced out already).  */
19968       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19969                                         DINFO_USAGE_DIR_USE);
19970       gen_subroutine_type_die (type, context_die);
19971       break;
19972
19973     case ARRAY_TYPE:
19974       gen_array_type_die (type, context_die);
19975       break;
19976
19977     case VECTOR_TYPE:
19978       gen_array_type_die (type, context_die);
19979       break;
19980
19981     case ENUMERAL_TYPE:
19982     case RECORD_TYPE:
19983     case UNION_TYPE:
19984     case QUAL_UNION_TYPE:
19985       gen_tagged_type_die (type, context_die, usage);
19986       return;
19987
19988     case VOID_TYPE:
19989     case INTEGER_TYPE:
19990     case REAL_TYPE:
19991     case FIXED_POINT_TYPE:
19992     case COMPLEX_TYPE:
19993     case BOOLEAN_TYPE:
19994       /* No DIEs needed for fundamental types.  */
19995       break;
19996
19997     case LANG_TYPE:
19998       /* Just use DW_TAG_unspecified_type.  */
19999       {
20000         dw_die_ref type_die = lookup_type_die (type);
20001         if (type_die == NULL)
20002           {
20003             tree name = TYPE_NAME (type);
20004             if (TREE_CODE (name) == TYPE_DECL)
20005               name = DECL_NAME (name);
20006             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20007             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20008             equate_type_number_to_die (type, type_die);
20009           }
20010       }
20011       break;
20012
20013     default:
20014       gcc_unreachable ();
20015     }
20016
20017   TREE_ASM_WRITTEN (type) = 1;
20018 }
20019
20020 static void
20021 gen_type_die (tree type, dw_die_ref context_die)
20022 {
20023   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20024 }
20025
20026 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20027    things which are local to the given block.  */
20028
20029 static void
20030 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20031 {
20032   int must_output_die = 0;
20033   bool inlined_func;
20034
20035   /* Ignore blocks that are NULL.  */
20036   if (stmt == NULL_TREE)
20037     return;
20038
20039   inlined_func = inlined_function_outer_scope_p (stmt);
20040
20041   /* If the block is one fragment of a non-contiguous block, do not
20042      process the variables, since they will have been done by the
20043      origin block.  Do process subblocks.  */
20044   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20045     {
20046       tree sub;
20047
20048       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20049         gen_block_die (sub, context_die, depth + 1);
20050
20051       return;
20052     }
20053
20054   /* Determine if we need to output any Dwarf DIEs at all to represent this
20055      block.  */
20056   if (inlined_func)
20057     /* The outer scopes for inlinings *must* always be represented.  We
20058        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20059     must_output_die = 1;
20060   else
20061     {
20062       /* Determine if this block directly contains any "significant"
20063          local declarations which we will need to output DIEs for.  */
20064       if (debug_info_level > DINFO_LEVEL_TERSE)
20065         /* We are not in terse mode so *any* local declaration counts
20066            as being a "significant" one.  */
20067         must_output_die = ((BLOCK_VARS (stmt) != NULL
20068                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20069                            && (TREE_USED (stmt)
20070                                || TREE_ASM_WRITTEN (stmt)
20071                                || BLOCK_ABSTRACT (stmt)));
20072       else if ((TREE_USED (stmt)
20073                 || TREE_ASM_WRITTEN (stmt)
20074                 || BLOCK_ABSTRACT (stmt))
20075                && !dwarf2out_ignore_block (stmt))
20076         must_output_die = 1;
20077     }
20078
20079   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20080      DIE for any block which contains no significant local declarations at
20081      all.  Rather, in such cases we just call `decls_for_scope' so that any
20082      needed Dwarf info for any sub-blocks will get properly generated. Note
20083      that in terse mode, our definition of what constitutes a "significant"
20084      local declaration gets restricted to include only inlined function
20085      instances and local (nested) function definitions.  */
20086   if (must_output_die)
20087     {
20088       if (inlined_func)
20089         {
20090           /* If STMT block is abstract, that means we have been called
20091              indirectly from dwarf2out_abstract_function.
20092              That function rightfully marks the descendent blocks (of
20093              the abstract function it is dealing with) as being abstract,
20094              precisely to prevent us from emitting any
20095              DW_TAG_inlined_subroutine DIE as a descendent
20096              of an abstract function instance. So in that case, we should
20097              not call gen_inlined_subroutine_die.
20098
20099              Later though, when cgraph asks dwarf2out to emit info
20100              for the concrete instance of the function decl into which
20101              the concrete instance of STMT got inlined, the later will lead
20102              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20103           if (! BLOCK_ABSTRACT (stmt))
20104             gen_inlined_subroutine_die (stmt, context_die, depth);
20105         }
20106       else
20107         gen_lexical_block_die (stmt, context_die, depth);
20108     }
20109   else
20110     decls_for_scope (stmt, context_die, depth);
20111 }
20112
20113 /* Process variable DECL (or variable with origin ORIGIN) within
20114    block STMT and add it to CONTEXT_DIE.  */
20115 static void
20116 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20117 {
20118   dw_die_ref die;
20119   tree decl_or_origin = decl ? decl : origin;
20120
20121   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20122     die = lookup_decl_die (decl_or_origin);
20123   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20124            && TYPE_DECL_IS_STUB (decl_or_origin))
20125     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20126   else
20127     die = NULL;
20128
20129   if (die != NULL && die->die_parent == NULL)
20130     add_child_die (context_die, die);
20131   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20132     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20133                                          stmt, context_die);
20134   else
20135     gen_decl_die (decl, origin, context_die);
20136 }
20137
20138 /* Generate all of the decls declared within a given scope and (recursively)
20139    all of its sub-blocks.  */
20140
20141 static void
20142 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20143 {
20144   tree decl;
20145   unsigned int i;
20146   tree subblocks;
20147
20148   /* Ignore NULL blocks.  */
20149   if (stmt == NULL_TREE)
20150     return;
20151
20152   /* Output the DIEs to represent all of the data objects and typedefs
20153      declared directly within this block but not within any nested
20154      sub-blocks.  Also, nested function and tag DIEs have been
20155      generated with a parent of NULL; fix that up now.  */
20156   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20157     process_scope_var (stmt, decl, NULL_TREE, context_die);
20158   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20159     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20160                        context_die);
20161
20162   /* If we're at -g1, we're not interested in subblocks.  */
20163   if (debug_info_level <= DINFO_LEVEL_TERSE)
20164     return;
20165
20166   /* Output the DIEs to represent all sub-blocks (and the items declared
20167      therein) of this block.  */
20168   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20169        subblocks != NULL;
20170        subblocks = BLOCK_CHAIN (subblocks))
20171     gen_block_die (subblocks, context_die, depth + 1);
20172 }
20173
20174 /* Is this a typedef we can avoid emitting?  */
20175
20176 static inline int
20177 is_redundant_typedef (const_tree decl)
20178 {
20179   if (TYPE_DECL_IS_STUB (decl))
20180     return 1;
20181
20182   if (DECL_ARTIFICIAL (decl)
20183       && DECL_CONTEXT (decl)
20184       && is_tagged_type (DECL_CONTEXT (decl))
20185       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20186       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20187     /* Also ignore the artificial member typedef for the class name.  */
20188     return 1;
20189
20190   return 0;
20191 }
20192
20193 /* Return TRUE if TYPE is a typedef that names a type for linkage
20194    purposes. This kind of typedefs is produced by the C++ FE for
20195    constructs like:
20196
20197    typedef struct {...} foo;
20198
20199    In that case, there is no typedef variant type produced for foo.
20200    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20201    struct type.  */
20202
20203 static bool
20204 is_naming_typedef_decl (const_tree decl)
20205 {
20206   if (decl == NULL_TREE
20207       || TREE_CODE (decl) != TYPE_DECL
20208       || !is_tagged_type (TREE_TYPE (decl))
20209       || DECL_IS_BUILTIN (decl)
20210       || is_redundant_typedef (decl)
20211       /* It looks like Ada produces TYPE_DECLs that are very similar
20212          to C++ naming typedefs but that have different
20213          semantics. Let's be specific to c++ for now.  */
20214       || !is_cxx ())
20215     return FALSE;
20216
20217   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20218           && TYPE_NAME (TREE_TYPE (decl)) == decl
20219           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20220               != TYPE_NAME (TREE_TYPE (decl))));
20221 }
20222
20223 /* Returns the DIE for a context.  */
20224
20225 static inline dw_die_ref
20226 get_context_die (tree context)
20227 {
20228   if (context)
20229     {
20230       /* Find die that represents this context.  */
20231       if (TYPE_P (context))
20232         return force_type_die (TYPE_MAIN_VARIANT (context));
20233       else
20234         return force_decl_die (context);
20235     }
20236   return comp_unit_die;
20237 }
20238
20239 /* Returns the DIE for decl.  A DIE will always be returned.  */
20240
20241 static dw_die_ref
20242 force_decl_die (tree decl)
20243 {
20244   dw_die_ref decl_die;
20245   unsigned saved_external_flag;
20246   tree save_fn = NULL_TREE;
20247   decl_die = lookup_decl_die (decl);
20248   if (!decl_die)
20249     {
20250       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20251
20252       decl_die = lookup_decl_die (decl);
20253       if (decl_die)
20254         return decl_die;
20255
20256       switch (TREE_CODE (decl))
20257         {
20258         case FUNCTION_DECL:
20259           /* Clear current_function_decl, so that gen_subprogram_die thinks
20260              that this is a declaration. At this point, we just want to force
20261              declaration die.  */
20262           save_fn = current_function_decl;
20263           current_function_decl = NULL_TREE;
20264           gen_subprogram_die (decl, context_die);
20265           current_function_decl = save_fn;
20266           break;
20267
20268         case VAR_DECL:
20269           /* Set external flag to force declaration die. Restore it after
20270            gen_decl_die() call.  */
20271           saved_external_flag = DECL_EXTERNAL (decl);
20272           DECL_EXTERNAL (decl) = 1;
20273           gen_decl_die (decl, NULL, context_die);
20274           DECL_EXTERNAL (decl) = saved_external_flag;
20275           break;
20276
20277         case NAMESPACE_DECL:
20278           if (dwarf_version >= 3 || !dwarf_strict)
20279             dwarf2out_decl (decl);
20280           else
20281             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20282             decl_die = comp_unit_die;
20283           break;
20284
20285         default:
20286           gcc_unreachable ();
20287         }
20288
20289       /* We should be able to find the DIE now.  */
20290       if (!decl_die)
20291         decl_die = lookup_decl_die (decl);
20292       gcc_assert (decl_die);
20293     }
20294
20295   return decl_die;
20296 }
20297
20298 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20299    always returned.  */
20300
20301 static dw_die_ref
20302 force_type_die (tree type)
20303 {
20304   dw_die_ref type_die;
20305
20306   type_die = lookup_type_die (type);
20307   if (!type_die)
20308     {
20309       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20310
20311       type_die = modified_type_die (type, TYPE_READONLY (type),
20312                                     TYPE_VOLATILE (type), context_die);
20313       gcc_assert (type_die);
20314     }
20315   return type_die;
20316 }
20317
20318 /* Force out any required namespaces to be able to output DECL,
20319    and return the new context_die for it, if it's changed.  */
20320
20321 static dw_die_ref
20322 setup_namespace_context (tree thing, dw_die_ref context_die)
20323 {
20324   tree context = (DECL_P (thing)
20325                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20326   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20327     /* Force out the namespace.  */
20328     context_die = force_decl_die (context);
20329
20330   return context_die;
20331 }
20332
20333 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20334    type) within its namespace, if appropriate.
20335
20336    For compatibility with older debuggers, namespace DIEs only contain
20337    declarations; all definitions are emitted at CU scope.  */
20338
20339 static dw_die_ref
20340 declare_in_namespace (tree thing, dw_die_ref context_die)
20341 {
20342   dw_die_ref ns_context;
20343
20344   if (debug_info_level <= DINFO_LEVEL_TERSE)
20345     return context_die;
20346
20347   /* If this decl is from an inlined function, then don't try to emit it in its
20348      namespace, as we will get confused.  It would have already been emitted
20349      when the abstract instance of the inline function was emitted anyways.  */
20350   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20351     return context_die;
20352
20353   ns_context = setup_namespace_context (thing, context_die);
20354
20355   if (ns_context != context_die)
20356     {
20357       if (is_fortran ())
20358         return ns_context;
20359       if (DECL_P (thing))
20360         gen_decl_die (thing, NULL, ns_context);
20361       else
20362         gen_type_die (thing, ns_context);
20363     }
20364   return context_die;
20365 }
20366
20367 /* Generate a DIE for a namespace or namespace alias.  */
20368
20369 static void
20370 gen_namespace_die (tree decl, dw_die_ref context_die)
20371 {
20372   dw_die_ref namespace_die;
20373
20374   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20375      they are an alias of.  */
20376   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20377     {
20378       /* Output a real namespace or module.  */
20379       context_die = setup_namespace_context (decl, comp_unit_die);
20380       namespace_die = new_die (is_fortran ()
20381                                ? DW_TAG_module : DW_TAG_namespace,
20382                                context_die, decl);
20383       /* For Fortran modules defined in different CU don't add src coords.  */
20384       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20385         {
20386           const char *name = dwarf2_name (decl, 0);
20387           if (name)
20388             add_name_attribute (namespace_die, name);
20389         }
20390       else
20391         add_name_and_src_coords_attributes (namespace_die, decl);
20392       if (DECL_EXTERNAL (decl))
20393         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20394       equate_decl_number_to_die (decl, namespace_die);
20395     }
20396   else
20397     {
20398       /* Output a namespace alias.  */
20399
20400       /* Force out the namespace we are an alias of, if necessary.  */
20401       dw_die_ref origin_die
20402         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20403
20404       if (DECL_CONTEXT (decl) == NULL_TREE
20405           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20406         context_die = setup_namespace_context (decl, comp_unit_die);
20407       /* Now create the namespace alias DIE.  */
20408       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20409       add_name_and_src_coords_attributes (namespace_die, decl);
20410       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20411       equate_decl_number_to_die (decl, namespace_die);
20412     }
20413 }
20414
20415 /* Generate Dwarf debug information for a decl described by DECL.  */
20416
20417 static void
20418 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20419 {
20420   tree decl_or_origin = decl ? decl : origin;
20421   tree class_origin = NULL, ultimate_origin;
20422
20423   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20424     return;
20425
20426   switch (TREE_CODE (decl_or_origin))
20427     {
20428     case ERROR_MARK:
20429       break;
20430
20431     case CONST_DECL:
20432       if (!is_fortran ())
20433         {
20434           /* The individual enumerators of an enum type get output when we output
20435              the Dwarf representation of the relevant enum type itself.  */
20436           break;
20437         }
20438
20439       /* Emit its type.  */
20440       gen_type_die (TREE_TYPE (decl), context_die);
20441
20442       /* And its containing namespace.  */
20443       context_die = declare_in_namespace (decl, context_die);
20444
20445       gen_const_die (decl, context_die);
20446       break;
20447
20448     case FUNCTION_DECL:
20449       /* Don't output any DIEs to represent mere function declarations,
20450          unless they are class members or explicit block externs.  */
20451       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20452           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20453           && (current_function_decl == NULL_TREE
20454               || DECL_ARTIFICIAL (decl_or_origin)))
20455         break;
20456
20457 #if 0
20458       /* FIXME */
20459       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20460          on local redeclarations of global functions.  That seems broken.  */
20461       if (current_function_decl != decl)
20462         /* This is only a declaration.  */;
20463 #endif
20464
20465       /* If we're emitting a clone, emit info for the abstract instance.  */
20466       if (origin || DECL_ORIGIN (decl) != decl)
20467         dwarf2out_abstract_function (origin
20468                                      ? DECL_ORIGIN (origin)
20469                                      : DECL_ABSTRACT_ORIGIN (decl));
20470
20471       /* If we're emitting an out-of-line copy of an inline function,
20472          emit info for the abstract instance and set up to refer to it.  */
20473       else if (cgraph_function_possibly_inlined_p (decl)
20474                && ! DECL_ABSTRACT (decl)
20475                && ! class_or_namespace_scope_p (context_die)
20476                /* dwarf2out_abstract_function won't emit a die if this is just
20477                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20478                   that case, because that works only if we have a die.  */
20479                && DECL_INITIAL (decl) != NULL_TREE)
20480         {
20481           dwarf2out_abstract_function (decl);
20482           set_decl_origin_self (decl);
20483         }
20484
20485       /* Otherwise we're emitting the primary DIE for this decl.  */
20486       else if (debug_info_level > DINFO_LEVEL_TERSE)
20487         {
20488           /* Before we describe the FUNCTION_DECL itself, make sure that we
20489              have described its return type.  */
20490           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20491
20492           /* And its virtual context.  */
20493           if (DECL_VINDEX (decl) != NULL_TREE)
20494             gen_type_die (DECL_CONTEXT (decl), context_die);
20495
20496           /* And its containing type.  */
20497           if (!origin)
20498             origin = decl_class_context (decl);
20499           if (origin != NULL_TREE)
20500             gen_type_die_for_member (origin, decl, context_die);
20501
20502           /* And its containing namespace.  */
20503           context_die = declare_in_namespace (decl, context_die);
20504         }
20505
20506       /* Now output a DIE to represent the function itself.  */
20507       if (decl)
20508         gen_subprogram_die (decl, context_die);
20509       break;
20510
20511     case TYPE_DECL:
20512       /* If we are in terse mode, don't generate any DIEs to represent any
20513          actual typedefs.  */
20514       if (debug_info_level <= DINFO_LEVEL_TERSE)
20515         break;
20516
20517       /* In the special case of a TYPE_DECL node representing the declaration
20518          of some type tag, if the given TYPE_DECL is marked as having been
20519          instantiated from some other (original) TYPE_DECL node (e.g. one which
20520          was generated within the original definition of an inline function) we
20521          used to generate a special (abbreviated) DW_TAG_structure_type,
20522          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20523          should be actually referencing those DIEs, as variable DIEs with that
20524          type would be emitted already in the abstract origin, so it was always
20525          removed during unused type prunning.  Don't add anything in this
20526          case.  */
20527       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20528         break;
20529
20530       if (is_redundant_typedef (decl))
20531         gen_type_die (TREE_TYPE (decl), context_die);
20532       else
20533         /* Output a DIE to represent the typedef itself.  */
20534         gen_typedef_die (decl, context_die);
20535       break;
20536
20537     case LABEL_DECL:
20538       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20539         gen_label_die (decl, context_die);
20540       break;
20541
20542     case VAR_DECL:
20543     case RESULT_DECL:
20544       /* If we are in terse mode, don't generate any DIEs to represent any
20545          variable declarations or definitions.  */
20546       if (debug_info_level <= DINFO_LEVEL_TERSE)
20547         break;
20548
20549       /* Output any DIEs that are needed to specify the type of this data
20550          object.  */
20551       if (decl_by_reference_p (decl_or_origin))
20552         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20553       else
20554         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20555
20556       /* And its containing type.  */
20557       class_origin = decl_class_context (decl_or_origin);
20558       if (class_origin != NULL_TREE)
20559         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20560
20561       /* And its containing namespace.  */
20562       context_die = declare_in_namespace (decl_or_origin, context_die);
20563
20564       /* Now output the DIE to represent the data object itself.  This gets
20565          complicated because of the possibility that the VAR_DECL really
20566          represents an inlined instance of a formal parameter for an inline
20567          function.  */
20568       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20569       if (ultimate_origin != NULL_TREE
20570           && TREE_CODE (ultimate_origin) == PARM_DECL)
20571         gen_formal_parameter_die (decl, origin,
20572                                   true /* Emit name attribute.  */,
20573                                   context_die);
20574       else
20575         gen_variable_die (decl, origin, context_die);
20576       break;
20577
20578     case FIELD_DECL:
20579       /* Ignore the nameless fields that are used to skip bits but handle C++
20580          anonymous unions and structs.  */
20581       if (DECL_NAME (decl) != NULL_TREE
20582           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20583           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20584         {
20585           gen_type_die (member_declared_type (decl), context_die);
20586           gen_field_die (decl, context_die);
20587         }
20588       break;
20589
20590     case PARM_DECL:
20591       if (DECL_BY_REFERENCE (decl_or_origin))
20592         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20593       else
20594         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20595       gen_formal_parameter_die (decl, origin,
20596                                 true /* Emit name attribute.  */,
20597                                 context_die);
20598       break;
20599
20600     case NAMESPACE_DECL:
20601     case IMPORTED_DECL:
20602       if (dwarf_version >= 3 || !dwarf_strict)
20603         gen_namespace_die (decl, context_die);
20604       break;
20605
20606     default:
20607       /* Probably some frontend-internal decl.  Assume we don't care.  */
20608       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20609       break;
20610     }
20611 }
20612 \f
20613 /* Output debug information for global decl DECL.  Called from toplev.c after
20614    compilation proper has finished.  */
20615
20616 static void
20617 dwarf2out_global_decl (tree decl)
20618 {
20619   /* Output DWARF2 information for file-scope tentative data object
20620      declarations, file-scope (extern) function declarations (which
20621      had no corresponding body) and file-scope tagged type declarations
20622      and definitions which have not yet been forced out.  */
20623   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20624     dwarf2out_decl (decl);
20625 }
20626
20627 /* Output debug information for type decl DECL.  Called from toplev.c
20628    and from language front ends (to record built-in types).  */
20629 static void
20630 dwarf2out_type_decl (tree decl, int local)
20631 {
20632   if (!local)
20633     dwarf2out_decl (decl);
20634 }
20635
20636 /* Output debug information for imported module or decl DECL.
20637    NAME is non-NULL name in the lexical block if the decl has been renamed.
20638    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20639    that DECL belongs to.
20640    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20641 static void
20642 dwarf2out_imported_module_or_decl_1 (tree decl,
20643                                      tree name,
20644                                      tree lexical_block,
20645                                      dw_die_ref lexical_block_die)
20646 {
20647   expanded_location xloc;
20648   dw_die_ref imported_die = NULL;
20649   dw_die_ref at_import_die;
20650
20651   if (TREE_CODE (decl) == IMPORTED_DECL)
20652     {
20653       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20654       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20655       gcc_assert (decl);
20656     }
20657   else
20658     xloc = expand_location (input_location);
20659
20660   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20661     {
20662       at_import_die = force_type_die (TREE_TYPE (decl));
20663       /* For namespace N { typedef void T; } using N::T; base_type_die
20664          returns NULL, but DW_TAG_imported_declaration requires
20665          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20666       if (!at_import_die)
20667         {
20668           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20669           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20670           at_import_die = lookup_type_die (TREE_TYPE (decl));
20671           gcc_assert (at_import_die);
20672         }
20673     }
20674   else
20675     {
20676       at_import_die = lookup_decl_die (decl);
20677       if (!at_import_die)
20678         {
20679           /* If we're trying to avoid duplicate debug info, we may not have
20680              emitted the member decl for this field.  Emit it now.  */
20681           if (TREE_CODE (decl) == FIELD_DECL)
20682             {
20683               tree type = DECL_CONTEXT (decl);
20684
20685               if (TYPE_CONTEXT (type)
20686                   && TYPE_P (TYPE_CONTEXT (type))
20687                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20688                                                 DINFO_USAGE_DIR_USE))
20689                 return;
20690               gen_type_die_for_member (type, decl,
20691                                        get_context_die (TYPE_CONTEXT (type)));
20692             }
20693           at_import_die = force_decl_die (decl);
20694         }
20695     }
20696
20697   if (TREE_CODE (decl) == NAMESPACE_DECL)
20698     {
20699       if (dwarf_version >= 3 || !dwarf_strict)
20700         imported_die = new_die (DW_TAG_imported_module,
20701                                 lexical_block_die,
20702                                 lexical_block);
20703       else
20704         return;
20705     }
20706   else
20707     imported_die = new_die (DW_TAG_imported_declaration,
20708                             lexical_block_die,
20709                             lexical_block);
20710
20711   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20712   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20713   if (name)
20714     add_AT_string (imported_die, DW_AT_name,
20715                    IDENTIFIER_POINTER (name));
20716   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20717 }
20718
20719 /* Output debug information for imported module or decl DECL.
20720    NAME is non-NULL name in context if the decl has been renamed.
20721    CHILD is true if decl is one of the renamed decls as part of
20722    importing whole module.  */
20723
20724 static void
20725 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20726                                    bool child)
20727 {
20728   /* dw_die_ref at_import_die;  */
20729   dw_die_ref scope_die;
20730
20731   if (debug_info_level <= DINFO_LEVEL_TERSE)
20732     return;
20733
20734   gcc_assert (decl);
20735
20736   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20737      We need decl DIE for reference and scope die. First, get DIE for the decl
20738      itself.  */
20739
20740   /* Get the scope die for decl context. Use comp_unit_die for global module
20741      or decl. If die is not found for non globals, force new die.  */
20742   if (context
20743       && TYPE_P (context)
20744       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20745     return;
20746
20747   if (!(dwarf_version >= 3 || !dwarf_strict))
20748     return;
20749
20750   scope_die = get_context_die (context);
20751
20752   if (child)
20753     {
20754       gcc_assert (scope_die->die_child);
20755       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20756       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20757       scope_die = scope_die->die_child;
20758     }
20759
20760   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20761   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20762
20763 }
20764
20765 /* Write the debugging output for DECL.  */
20766
20767 void
20768 dwarf2out_decl (tree decl)
20769 {
20770   dw_die_ref context_die = comp_unit_die;
20771
20772   switch (TREE_CODE (decl))
20773     {
20774     case ERROR_MARK:
20775       return;
20776
20777     case FUNCTION_DECL:
20778       /* What we would really like to do here is to filter out all mere
20779          file-scope declarations of file-scope functions which are never
20780          referenced later within this translation unit (and keep all of ones
20781          that *are* referenced later on) but we aren't clairvoyant, so we have
20782          no idea which functions will be referenced in the future (i.e. later
20783          on within the current translation unit). So here we just ignore all
20784          file-scope function declarations which are not also definitions.  If
20785          and when the debugger needs to know something about these functions,
20786          it will have to hunt around and find the DWARF information associated
20787          with the definition of the function.
20788
20789          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20790          nodes represent definitions and which ones represent mere
20791          declarations.  We have to check DECL_INITIAL instead. That's because
20792          the C front-end supports some weird semantics for "extern inline"
20793          function definitions.  These can get inlined within the current
20794          translation unit (and thus, we need to generate Dwarf info for their
20795          abstract instances so that the Dwarf info for the concrete inlined
20796          instances can have something to refer to) but the compiler never
20797          generates any out-of-lines instances of such things (despite the fact
20798          that they *are* definitions).
20799
20800          The important point is that the C front-end marks these "extern
20801          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20802          them anyway. Note that the C++ front-end also plays some similar games
20803          for inline function definitions appearing within include files which
20804          also contain `#pragma interface' pragmas.  */
20805       if (DECL_INITIAL (decl) == NULL_TREE)
20806         return;
20807
20808       /* If we're a nested function, initially use a parent of NULL; if we're
20809          a plain function, this will be fixed up in decls_for_scope.  If
20810          we're a method, it will be ignored, since we already have a DIE.  */
20811       if (decl_function_context (decl)
20812           /* But if we're in terse mode, we don't care about scope.  */
20813           && debug_info_level > DINFO_LEVEL_TERSE)
20814         context_die = NULL;
20815       break;
20816
20817     case VAR_DECL:
20818       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20819          declaration and if the declaration was never even referenced from
20820          within this entire compilation unit.  We suppress these DIEs in
20821          order to save space in the .debug section (by eliminating entries
20822          which are probably useless).  Note that we must not suppress
20823          block-local extern declarations (whether used or not) because that
20824          would screw-up the debugger's name lookup mechanism and cause it to
20825          miss things which really ought to be in scope at a given point.  */
20826       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20827         return;
20828
20829       /* For local statics lookup proper context die.  */
20830       if (TREE_STATIC (decl) && decl_function_context (decl))
20831         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20832
20833       /* If we are in terse mode, don't generate any DIEs to represent any
20834          variable declarations or definitions.  */
20835       if (debug_info_level <= DINFO_LEVEL_TERSE)
20836         return;
20837       break;
20838
20839     case CONST_DECL:
20840       if (debug_info_level <= DINFO_LEVEL_TERSE)
20841         return;
20842       if (!is_fortran ())
20843         return;
20844       if (TREE_STATIC (decl) && decl_function_context (decl))
20845         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20846       break;
20847
20848     case NAMESPACE_DECL:
20849     case IMPORTED_DECL:
20850       if (debug_info_level <= DINFO_LEVEL_TERSE)
20851         return;
20852       if (lookup_decl_die (decl) != NULL)
20853         return;
20854       break;
20855
20856     case TYPE_DECL:
20857       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20858       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20859         return;
20860
20861       /* Don't bother trying to generate any DIEs to represent any of the
20862          normal built-in types for the language we are compiling.  */
20863       if (DECL_IS_BUILTIN (decl))
20864         {
20865           /* OK, we need to generate one for `bool' so GDB knows what type
20866              comparisons have.  */
20867           if (is_cxx ()
20868               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20869               && ! DECL_IGNORED_P (decl))
20870             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20871
20872           return;
20873         }
20874
20875       /* If we are in terse mode, don't generate any DIEs for types.  */
20876       if (debug_info_level <= DINFO_LEVEL_TERSE)
20877         return;
20878
20879       /* If we're a function-scope tag, initially use a parent of NULL;
20880          this will be fixed up in decls_for_scope.  */
20881       if (decl_function_context (decl))
20882         context_die = NULL;
20883
20884       break;
20885
20886     default:
20887       return;
20888     }
20889
20890   gen_decl_die (decl, NULL, context_die);
20891 }
20892
20893 /* Write the debugging output for DECL.  */
20894
20895 static void
20896 dwarf2out_function_decl (tree decl)
20897 {
20898   dwarf2out_decl (decl);
20899
20900   htab_empty (decl_loc_table);
20901 }
20902
20903 /* Output a marker (i.e. a label) for the beginning of the generated code for
20904    a lexical block.  */
20905
20906 static void
20907 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20908                        unsigned int blocknum)
20909 {
20910   switch_to_section (current_function_section ());
20911   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20912 }
20913
20914 /* Output a marker (i.e. a label) for the end of the generated code for a
20915    lexical block.  */
20916
20917 static void
20918 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20919 {
20920   switch_to_section (current_function_section ());
20921   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20922 }
20923
20924 /* Returns nonzero if it is appropriate not to emit any debugging
20925    information for BLOCK, because it doesn't contain any instructions.
20926
20927    Don't allow this for blocks with nested functions or local classes
20928    as we would end up with orphans, and in the presence of scheduling
20929    we may end up calling them anyway.  */
20930
20931 static bool
20932 dwarf2out_ignore_block (const_tree block)
20933 {
20934   tree decl;
20935   unsigned int i;
20936
20937   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20938     if (TREE_CODE (decl) == FUNCTION_DECL
20939         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20940       return 0;
20941   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20942     {
20943       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20944       if (TREE_CODE (decl) == FUNCTION_DECL
20945           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20946       return 0;
20947     }
20948
20949   return 1;
20950 }
20951
20952 /* Hash table routines for file_hash.  */
20953
20954 static int
20955 file_table_eq (const void *p1_p, const void *p2_p)
20956 {
20957   const struct dwarf_file_data *const p1 =
20958     (const struct dwarf_file_data *) p1_p;
20959   const char *const p2 = (const char *) p2_p;
20960   return strcmp (p1->filename, p2) == 0;
20961 }
20962
20963 static hashval_t
20964 file_table_hash (const void *p_p)
20965 {
20966   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20967   return htab_hash_string (p->filename);
20968 }
20969
20970 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20971    dwarf2out.c) and return its "index".  The index of each (known) filename is
20972    just a unique number which is associated with only that one filename.  We
20973    need such numbers for the sake of generating labels (in the .debug_sfnames
20974    section) and references to those files numbers (in the .debug_srcinfo
20975    and.debug_macinfo sections).  If the filename given as an argument is not
20976    found in our current list, add it to the list and assign it the next
20977    available unique index number.  In order to speed up searches, we remember
20978    the index of the filename was looked up last.  This handles the majority of
20979    all searches.  */
20980
20981 static struct dwarf_file_data *
20982 lookup_filename (const char *file_name)
20983 {
20984   void ** slot;
20985   struct dwarf_file_data * created;
20986
20987   /* Check to see if the file name that was searched on the previous
20988      call matches this file name.  If so, return the index.  */
20989   if (file_table_last_lookup
20990       && (file_name == file_table_last_lookup->filename
20991           || strcmp (file_table_last_lookup->filename, file_name) == 0))
20992     return file_table_last_lookup;
20993
20994   /* Didn't match the previous lookup, search the table.  */
20995   slot = htab_find_slot_with_hash (file_table, file_name,
20996                                    htab_hash_string (file_name), INSERT);
20997   if (*slot)
20998     return (struct dwarf_file_data *) *slot;
20999
21000   created = ggc_alloc_dwarf_file_data ();
21001   created->filename = file_name;
21002   created->emitted_number = 0;
21003   *slot = created;
21004   return created;
21005 }
21006
21007 /* If the assembler will construct the file table, then translate the compiler
21008    internal file table number into the assembler file table number, and emit
21009    a .file directive if we haven't already emitted one yet.  The file table
21010    numbers are different because we prune debug info for unused variables and
21011    types, which may include filenames.  */
21012
21013 static int
21014 maybe_emit_file (struct dwarf_file_data * fd)
21015 {
21016   if (! fd->emitted_number)
21017     {
21018       if (last_emitted_file)
21019         fd->emitted_number = last_emitted_file->emitted_number + 1;
21020       else
21021         fd->emitted_number = 1;
21022       last_emitted_file = fd;
21023
21024       if (DWARF2_ASM_LINE_DEBUG_INFO)
21025         {
21026           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21027           output_quoted_string (asm_out_file,
21028                                 remap_debug_filename (fd->filename));
21029           fputc ('\n', asm_out_file);
21030         }
21031     }
21032
21033   return fd->emitted_number;
21034 }
21035
21036 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21037    That generation should happen after function debug info has been
21038    generated. The value of the attribute is the constant value of ARG.  */
21039
21040 static void
21041 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21042 {
21043   die_arg_entry entry;
21044
21045   if (!die || !arg)
21046     return;
21047
21048   if (!tmpl_value_parm_die_table)
21049     tmpl_value_parm_die_table
21050       = VEC_alloc (die_arg_entry, gc, 32);
21051
21052   entry.die = die;
21053   entry.arg = arg;
21054   VEC_safe_push (die_arg_entry, gc,
21055                  tmpl_value_parm_die_table,
21056                  &entry);
21057 }
21058
21059 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21060    by append_entry_to_tmpl_value_parm_die_table. This function must
21061    be called after function DIEs have been generated.  */
21062
21063 static void
21064 gen_remaining_tmpl_value_param_die_attribute (void)
21065 {
21066   if (tmpl_value_parm_die_table)
21067     {
21068       unsigned i;
21069       die_arg_entry *e;
21070
21071       for (i = 0;
21072            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
21073            i++)
21074         tree_add_const_value_attribute (e->die, e->arg);
21075     }
21076 }
21077
21078
21079 /* Replace DW_AT_name for the decl with name.  */
21080
21081 static void
21082 dwarf2out_set_name (tree decl, tree name)
21083 {
21084   dw_die_ref die;
21085   dw_attr_ref attr;
21086   const char *dname;
21087
21088   die = TYPE_SYMTAB_DIE (decl);
21089   if (!die)
21090     return;
21091
21092   dname = dwarf2_name (name, 0);
21093   if (!dname)
21094     return;
21095
21096   attr = get_AT (die, DW_AT_name);
21097   if (attr)
21098     {
21099       struct indirect_string_node *node;
21100
21101       node = find_AT_string (dname);
21102       /* replace the string.  */
21103       attr->dw_attr_val.v.val_str = node;
21104     }
21105
21106   else
21107     add_name_attribute (die, dname);
21108 }
21109
21110 /* Called by the final INSN scan whenever we see a direct function call.
21111    Make an entry into the direct call table, recording the point of call
21112    and a reference to the target function's debug entry.  */
21113
21114 static void
21115 dwarf2out_direct_call (tree targ)
21116 {
21117   dcall_entry e;
21118   tree origin = decl_ultimate_origin (targ);
21119
21120   /* If this is a clone, use the abstract origin as the target.  */
21121   if (origin)
21122     targ = origin;
21123
21124   e.poc_label_num = poc_label_num++;
21125   e.poc_decl = current_function_decl;
21126   e.targ_die = force_decl_die (targ);
21127   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21128
21129   /* Drop a label at the return point to mark the point of call.  */
21130   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21131 }
21132
21133 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21134
21135 static hashval_t
21136 vcall_insn_table_hash (const void *x)
21137 {
21138   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21139 }
21140
21141 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21142    insnd_uid of *Y.  */
21143
21144 static int
21145 vcall_insn_table_eq (const void *x, const void *y)
21146 {
21147   return (((const struct vcall_insn *) x)->insn_uid
21148           == ((const struct vcall_insn *) y)->insn_uid);
21149 }
21150
21151 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21152
21153 static void
21154 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21155 {
21156   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21157   struct vcall_insn **slot;
21158
21159   gcc_assert (item);
21160   item->insn_uid = insn_uid;
21161   item->vtable_slot = vtable_slot;
21162   slot = (struct vcall_insn **)
21163       htab_find_slot_with_hash (vcall_insn_table, &item,
21164                                 (hashval_t) insn_uid, INSERT);
21165   *slot = item;
21166 }
21167
21168 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21169
21170 static unsigned int
21171 lookup_vcall_insn (unsigned int insn_uid)
21172 {
21173   struct vcall_insn item;
21174   struct vcall_insn *p;
21175
21176   item.insn_uid = insn_uid;
21177   item.vtable_slot = 0;
21178   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21179                                                  (void *) &item,
21180                                                  (hashval_t) insn_uid);
21181   if (p == NULL)
21182     return (unsigned int) -1;
21183   return p->vtable_slot;
21184 }
21185
21186
21187 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21188    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21189    is the vtable slot index that we will need to put in the virtual call
21190    table later.  */
21191
21192 static void
21193 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21194 {
21195   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21196     {
21197       tree token = OBJ_TYPE_REF_TOKEN (addr);
21198       if (TREE_CODE (token) == INTEGER_CST)
21199         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21200     }
21201 }
21202
21203 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21204    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21205    with NEW_INSN.  */
21206
21207 static void
21208 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21209 {
21210   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21211
21212   if (vtable_slot != (unsigned int) -1)
21213     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21214 }
21215
21216 /* Called by the final INSN scan whenever we see a virtual function call.
21217    Make an entry into the virtual call table, recording the point of call
21218    and the slot index of the vtable entry used to call the virtual member
21219    function.  The slot index was associated with the INSN_UID during the
21220    lowering to RTL.  */
21221
21222 static void
21223 dwarf2out_virtual_call (int insn_uid)
21224 {
21225   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21226   vcall_entry e;
21227
21228   if (vtable_slot == (unsigned int) -1)
21229     return;
21230
21231   e.poc_label_num = poc_label_num++;
21232   e.vtable_slot = vtable_slot;
21233   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21234
21235   /* Drop a label at the return point to mark the point of call.  */
21236   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21237 }
21238
21239 /* Called by the final INSN scan whenever we see a var location.  We
21240    use it to drop labels in the right places, and throw the location in
21241    our lookup table.  */
21242
21243 static void
21244 dwarf2out_var_location (rtx loc_note)
21245 {
21246   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21247   struct var_loc_node *newloc;
21248   rtx next_real;
21249   static const char *last_label;
21250   static const char *last_postcall_label;
21251   static bool last_in_cold_section_p;
21252   tree decl;
21253
21254   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21255     return;
21256
21257   next_real = next_real_insn (loc_note);
21258   /* If there are no instructions which would be affected by this note,
21259      don't do anything.  */
21260   if (next_real == NULL_RTX)
21261     return;
21262
21263   /* If there were any real insns between note we processed last time
21264      and this note (or if it is the first note), clear
21265      last_{,postcall_}label so that they are not reused this time.  */
21266   if (last_var_location_insn == NULL_RTX
21267       || last_var_location_insn != next_real
21268       || last_in_cold_section_p != in_cold_section_p)
21269     {
21270       last_label = NULL;
21271       last_postcall_label = NULL;
21272     }
21273
21274   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21275   newloc = add_var_loc_to_decl (decl, loc_note,
21276                                 NOTE_DURING_CALL_P (loc_note)
21277                                 ? last_postcall_label : last_label);
21278   if (newloc == NULL)
21279     return;
21280
21281   /* If there were no real insns between note we processed last time
21282      and this note, use the label we emitted last time.  Otherwise
21283      create a new label and emit it.  */
21284   if (last_label == NULL)
21285     {
21286       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21287       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21288       loclabel_num++;
21289       last_label = ggc_strdup (loclabel);
21290     }
21291
21292   if (!NOTE_DURING_CALL_P (loc_note))
21293     newloc->label = last_label;
21294   else
21295     {
21296       if (!last_postcall_label)
21297         {
21298           sprintf (loclabel, "%s-1", last_label);
21299           last_postcall_label = ggc_strdup (loclabel);
21300         }
21301       newloc->label = last_postcall_label;
21302     }
21303
21304   last_var_location_insn = next_real;
21305   last_in_cold_section_p = in_cold_section_p;
21306 }
21307
21308 /* We need to reset the locations at the beginning of each
21309    function. We can't do this in the end_function hook, because the
21310    declarations that use the locations won't have been output when
21311    that hook is called.  Also compute have_multiple_function_sections here.  */
21312
21313 static void
21314 dwarf2out_begin_function (tree fun)
21315 {
21316   if (function_section (fun) != text_section)
21317     have_multiple_function_sections = true;
21318
21319   dwarf2out_note_section_used ();
21320 }
21321
21322 /* Output a label to mark the beginning of a source code line entry
21323    and record information relating to this source line, in
21324    'line_info_table' for later output of the .debug_line section.  */
21325
21326 static void
21327 dwarf2out_source_line (unsigned int line, const char *filename,
21328                        int discriminator, bool is_stmt)
21329 {
21330   static bool last_is_stmt = true;
21331
21332   if (debug_info_level >= DINFO_LEVEL_NORMAL
21333       && line != 0)
21334     {
21335       int file_num = maybe_emit_file (lookup_filename (filename));
21336
21337       switch_to_section (current_function_section ());
21338
21339       /* If requested, emit something human-readable.  */
21340       if (flag_debug_asm)
21341         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21342                  filename, line);
21343
21344       if (DWARF2_ASM_LINE_DEBUG_INFO)
21345         {
21346           /* Emit the .loc directive understood by GNU as.  */
21347           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21348           if (is_stmt != last_is_stmt)
21349             {
21350               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21351               last_is_stmt = is_stmt;
21352             }
21353           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21354             fprintf (asm_out_file, " discriminator %d", discriminator);
21355           fputc ('\n', asm_out_file);
21356
21357           /* Indicate that line number info exists.  */
21358           line_info_table_in_use++;
21359         }
21360       else if (function_section (current_function_decl) != text_section)
21361         {
21362           dw_separate_line_info_ref line_info;
21363           targetm.asm_out.internal_label (asm_out_file,
21364                                           SEPARATE_LINE_CODE_LABEL,
21365                                           separate_line_info_table_in_use);
21366
21367           /* Expand the line info table if necessary.  */
21368           if (separate_line_info_table_in_use
21369               == separate_line_info_table_allocated)
21370             {
21371               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21372               separate_line_info_table
21373                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21374                                  separate_line_info_table,
21375                                  separate_line_info_table_allocated);
21376               memset (separate_line_info_table
21377                        + separate_line_info_table_in_use,
21378                       0,
21379                       (LINE_INFO_TABLE_INCREMENT
21380                        * sizeof (dw_separate_line_info_entry)));
21381             }
21382
21383           /* Add the new entry at the end of the line_info_table.  */
21384           line_info
21385             = &separate_line_info_table[separate_line_info_table_in_use++];
21386           line_info->dw_file_num = file_num;
21387           line_info->dw_line_num = line;
21388           line_info->function = current_function_funcdef_no;
21389         }
21390       else
21391         {
21392           dw_line_info_ref line_info;
21393
21394           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21395                                      line_info_table_in_use);
21396
21397           /* Expand the line info table if necessary.  */
21398           if (line_info_table_in_use == line_info_table_allocated)
21399             {
21400               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21401               line_info_table
21402                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21403                                  line_info_table_allocated);
21404               memset (line_info_table + line_info_table_in_use, 0,
21405                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21406             }
21407
21408           /* Add the new entry at the end of the line_info_table.  */
21409           line_info = &line_info_table[line_info_table_in_use++];
21410           line_info->dw_file_num = file_num;
21411           line_info->dw_line_num = line;
21412         }
21413     }
21414 }
21415
21416 /* Record the beginning of a new source file.  */
21417
21418 static void
21419 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21420 {
21421   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21422     {
21423       /* Record the beginning of the file for break_out_includes.  */
21424       dw_die_ref bincl_die;
21425
21426       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21427       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21428     }
21429
21430   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21431     {
21432       int file_num = maybe_emit_file (lookup_filename (filename));
21433
21434       switch_to_section (debug_macinfo_section);
21435       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21436       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21437                                    lineno);
21438
21439       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21440     }
21441 }
21442
21443 /* Record the end of a source file.  */
21444
21445 static void
21446 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21447 {
21448   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21449     /* Record the end of the file for break_out_includes.  */
21450     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21451
21452   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21453     {
21454       switch_to_section (debug_macinfo_section);
21455       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21456     }
21457 }
21458
21459 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21460    the tail part of the directive line, i.e. the part which is past the
21461    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21462
21463 static void
21464 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21465                   const char *buffer ATTRIBUTE_UNUSED)
21466 {
21467   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21468     {
21469       switch_to_section (debug_macinfo_section);
21470       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21471       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21472       dw2_asm_output_nstring (buffer, -1, "The macro");
21473     }
21474 }
21475
21476 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21477    the tail part of the directive line, i.e. the part which is past the
21478    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21479
21480 static void
21481 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21482                  const char *buffer ATTRIBUTE_UNUSED)
21483 {
21484   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21485     {
21486       switch_to_section (debug_macinfo_section);
21487       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21488       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21489       dw2_asm_output_nstring (buffer, -1, "The macro");
21490     }
21491 }
21492
21493 /* Set up for Dwarf output at the start of compilation.  */
21494
21495 static void
21496 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21497 {
21498   /* Allocate the file_table.  */
21499   file_table = htab_create_ggc (50, file_table_hash,
21500                                 file_table_eq, NULL);
21501
21502   /* Allocate the decl_die_table.  */
21503   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21504                                     decl_die_table_eq, NULL);
21505
21506   /* Allocate the decl_loc_table.  */
21507   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21508                                     decl_loc_table_eq, NULL);
21509
21510   /* Allocate the initial hunk of the decl_scope_table.  */
21511   decl_scope_table = VEC_alloc (tree, gc, 256);
21512
21513   /* Allocate the initial hunk of the abbrev_die_table.  */
21514   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21515     (ABBREV_DIE_TABLE_INCREMENT);
21516   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21517   /* Zero-th entry is allocated, but unused.  */
21518   abbrev_die_table_in_use = 1;
21519
21520   /* Allocate the initial hunk of the line_info_table.  */
21521   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21522     (LINE_INFO_TABLE_INCREMENT);
21523   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21524
21525   /* Zero-th entry is allocated, but unused.  */
21526   line_info_table_in_use = 1;
21527
21528   /* Allocate the pubtypes and pubnames vectors.  */
21529   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21530   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21531
21532   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21533   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21534                                       vcall_insn_table_eq, NULL);
21535
21536   /* Generate the initial DIE for the .debug section.  Note that the (string)
21537      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21538      will (typically) be a relative pathname and that this pathname should be
21539      taken as being relative to the directory from which the compiler was
21540      invoked when the given (base) source file was compiled.  We will fill
21541      in this value in dwarf2out_finish.  */
21542   comp_unit_die = gen_compile_unit_die (NULL);
21543
21544   incomplete_types = VEC_alloc (tree, gc, 64);
21545
21546   used_rtx_array = VEC_alloc (rtx, gc, 32);
21547
21548   debug_info_section = get_section (DEBUG_INFO_SECTION,
21549                                     SECTION_DEBUG, NULL);
21550   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21551                                       SECTION_DEBUG, NULL);
21552   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21553                                        SECTION_DEBUG, NULL);
21554   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21555                                        SECTION_DEBUG, NULL);
21556   debug_line_section = get_section (DEBUG_LINE_SECTION,
21557                                     SECTION_DEBUG, NULL);
21558   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21559                                    SECTION_DEBUG, NULL);
21560   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21561                                         SECTION_DEBUG, NULL);
21562   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21563                                         SECTION_DEBUG, NULL);
21564   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21565                                      SECTION_DEBUG, NULL);
21566   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21567                                      SECTION_DEBUG, NULL);
21568   debug_str_section = get_section (DEBUG_STR_SECTION,
21569                                    DEBUG_STR_SECTION_FLAGS, NULL);
21570   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21571                                       SECTION_DEBUG, NULL);
21572   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21573                                      SECTION_DEBUG, NULL);
21574
21575   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21576   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21577                                DEBUG_ABBREV_SECTION_LABEL, 0);
21578   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21579   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21580                                COLD_TEXT_SECTION_LABEL, 0);
21581   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21582
21583   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21584                                DEBUG_INFO_SECTION_LABEL, 0);
21585   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21586                                DEBUG_LINE_SECTION_LABEL, 0);
21587   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21588                                DEBUG_RANGES_SECTION_LABEL, 0);
21589   switch_to_section (debug_abbrev_section);
21590   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21591   switch_to_section (debug_info_section);
21592   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21593   switch_to_section (debug_line_section);
21594   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21595
21596   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21597     {
21598       switch_to_section (debug_macinfo_section);
21599       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21600                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21601       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21602     }
21603
21604   switch_to_section (text_section);
21605   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21606   if (flag_reorder_blocks_and_partition)
21607     {
21608       cold_text_section = unlikely_text_section ();
21609       switch_to_section (cold_text_section);
21610       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21611     }
21612
21613 }
21614
21615 /* Called before cgraph_optimize starts outputtting functions, variables
21616    and toplevel asms into assembly.  */
21617
21618 static void
21619 dwarf2out_assembly_start (void)
21620 {
21621   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21622     {
21623 #ifndef TARGET_UNWIND_INFO
21624       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21625 #endif
21626         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21627     }
21628 }
21629
21630 /* A helper function for dwarf2out_finish called through
21631    htab_traverse.  Emit one queued .debug_str string.  */
21632
21633 static int
21634 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21635 {
21636   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21637
21638   if (node->label && node->refcount)
21639     {
21640       switch_to_section (debug_str_section);
21641       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21642       assemble_string (node->str, strlen (node->str) + 1);
21643     }
21644
21645   return 1;
21646 }
21647
21648 #if ENABLE_ASSERT_CHECKING
21649 /* Verify that all marks are clear.  */
21650
21651 static void
21652 verify_marks_clear (dw_die_ref die)
21653 {
21654   dw_die_ref c;
21655
21656   gcc_assert (! die->die_mark);
21657   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21658 }
21659 #endif /* ENABLE_ASSERT_CHECKING */
21660
21661 /* Clear the marks for a die and its children.
21662    Be cool if the mark isn't set.  */
21663
21664 static void
21665 prune_unmark_dies (dw_die_ref die)
21666 {
21667   dw_die_ref c;
21668
21669   if (die->die_mark)
21670     die->die_mark = 0;
21671   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21672 }
21673
21674 /* Given DIE that we're marking as used, find any other dies
21675    it references as attributes and mark them as used.  */
21676
21677 static void
21678 prune_unused_types_walk_attribs (dw_die_ref die)
21679 {
21680   dw_attr_ref a;
21681   unsigned ix;
21682
21683   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21684     {
21685       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21686         {
21687           /* A reference to another DIE.
21688              Make sure that it will get emitted.
21689              If it was broken out into a comdat group, don't follow it.  */
21690           if (dwarf_version < 4
21691               || a->dw_attr == DW_AT_specification
21692               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21693             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21694         }
21695       /* Set the string's refcount to 0 so that prune_unused_types_mark
21696          accounts properly for it.  */
21697       if (AT_class (a) == dw_val_class_str)
21698         a->dw_attr_val.v.val_str->refcount = 0;
21699     }
21700 }
21701
21702
21703 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21704    to DIE's children.  */
21705
21706 static void
21707 prune_unused_types_mark (dw_die_ref die, int dokids)
21708 {
21709   dw_die_ref c;
21710
21711   if (die->die_mark == 0)
21712     {
21713       /* We haven't done this node yet.  Mark it as used.  */
21714       die->die_mark = 1;
21715
21716       /* We also have to mark its parents as used.
21717          (But we don't want to mark our parents' kids due to this.)  */
21718       if (die->die_parent)
21719         prune_unused_types_mark (die->die_parent, 0);
21720
21721       /* Mark any referenced nodes.  */
21722       prune_unused_types_walk_attribs (die);
21723
21724       /* If this node is a specification,
21725          also mark the definition, if it exists.  */
21726       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21727         prune_unused_types_mark (die->die_definition, 1);
21728     }
21729
21730   if (dokids && die->die_mark != 2)
21731     {
21732       /* We need to walk the children, but haven't done so yet.
21733          Remember that we've walked the kids.  */
21734       die->die_mark = 2;
21735
21736       /* If this is an array type, we need to make sure our
21737          kids get marked, even if they're types.  If we're
21738          breaking out types into comdat sections, do this
21739          for all type definitions.  */
21740       if (die->die_tag == DW_TAG_array_type
21741           || (dwarf_version >= 4
21742               && is_type_die (die) && ! is_declaration_die (die)))
21743         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21744       else
21745         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21746     }
21747 }
21748
21749 /* For local classes, look if any static member functions were emitted
21750    and if so, mark them.  */
21751
21752 static void
21753 prune_unused_types_walk_local_classes (dw_die_ref die)
21754 {
21755   dw_die_ref c;
21756
21757   if (die->die_mark == 2)
21758     return;
21759
21760   switch (die->die_tag)
21761     {
21762     case DW_TAG_structure_type:
21763     case DW_TAG_union_type:
21764     case DW_TAG_class_type:
21765       break;
21766
21767     case DW_TAG_subprogram:
21768       if (!get_AT_flag (die, DW_AT_declaration)
21769           || die->die_definition != NULL)
21770         prune_unused_types_mark (die, 1);
21771       return;
21772
21773     default:
21774       return;
21775     }
21776
21777   /* Mark children.  */
21778   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21779 }
21780
21781 /* Walk the tree DIE and mark types that we actually use.  */
21782
21783 static void
21784 prune_unused_types_walk (dw_die_ref die)
21785 {
21786   dw_die_ref c;
21787
21788   /* Don't do anything if this node is already marked and
21789      children have been marked as well.  */
21790   if (die->die_mark == 2)
21791     return;
21792
21793   switch (die->die_tag)
21794     {
21795     case DW_TAG_structure_type:
21796     case DW_TAG_union_type:
21797     case DW_TAG_class_type:
21798       if (die->die_perennial_p)
21799         break;
21800
21801       for (c = die->die_parent; c; c = c->die_parent)
21802         if (c->die_tag == DW_TAG_subprogram)
21803           break;
21804
21805       /* Finding used static member functions inside of classes
21806          is needed just for local classes, because for other classes
21807          static member function DIEs with DW_AT_specification
21808          are emitted outside of the DW_TAG_*_type.  If we ever change
21809          it, we'd need to call this even for non-local classes.  */
21810       if (c)
21811         prune_unused_types_walk_local_classes (die);
21812
21813       /* It's a type node --- don't mark it.  */
21814       return;
21815
21816     case DW_TAG_const_type:
21817     case DW_TAG_packed_type:
21818     case DW_TAG_pointer_type:
21819     case DW_TAG_reference_type:
21820     case DW_TAG_rvalue_reference_type:
21821     case DW_TAG_volatile_type:
21822     case DW_TAG_typedef:
21823     case DW_TAG_array_type:
21824     case DW_TAG_interface_type:
21825     case DW_TAG_friend:
21826     case DW_TAG_variant_part:
21827     case DW_TAG_enumeration_type:
21828     case DW_TAG_subroutine_type:
21829     case DW_TAG_string_type:
21830     case DW_TAG_set_type:
21831     case DW_TAG_subrange_type:
21832     case DW_TAG_ptr_to_member_type:
21833     case DW_TAG_file_type:
21834       if (die->die_perennial_p)
21835         break;
21836
21837       /* It's a type node --- don't mark it.  */
21838       return;
21839
21840     default:
21841       /* Mark everything else.  */
21842       break;
21843   }
21844
21845   if (die->die_mark == 0)
21846     {
21847       die->die_mark = 1;
21848
21849       /* Now, mark any dies referenced from here.  */
21850       prune_unused_types_walk_attribs (die);
21851     }
21852
21853   die->die_mark = 2;
21854
21855   /* Mark children.  */
21856   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21857 }
21858
21859 /* Increment the string counts on strings referred to from DIE's
21860    attributes.  */
21861
21862 static void
21863 prune_unused_types_update_strings (dw_die_ref die)
21864 {
21865   dw_attr_ref a;
21866   unsigned ix;
21867
21868   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21869     if (AT_class (a) == dw_val_class_str)
21870       {
21871         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21872         s->refcount++;
21873         /* Avoid unnecessarily putting strings that are used less than
21874            twice in the hash table.  */
21875         if (s->refcount
21876             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21877           {
21878             void ** slot;
21879             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21880                                              htab_hash_string (s->str),
21881                                              INSERT);
21882             gcc_assert (*slot == NULL);
21883             *slot = s;
21884           }
21885       }
21886 }
21887
21888 /* Remove from the tree DIE any dies that aren't marked.  */
21889
21890 static void
21891 prune_unused_types_prune (dw_die_ref die)
21892 {
21893   dw_die_ref c;
21894
21895   gcc_assert (die->die_mark);
21896   prune_unused_types_update_strings (die);
21897
21898   if (! die->die_child)
21899     return;
21900
21901   c = die->die_child;
21902   do {
21903     dw_die_ref prev = c;
21904     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21905       if (c == die->die_child)
21906         {
21907           /* No marked children between 'prev' and the end of the list.  */
21908           if (prev == c)
21909             /* No marked children at all.  */
21910             die->die_child = NULL;
21911           else
21912             {
21913               prev->die_sib = c->die_sib;
21914               die->die_child = prev;
21915             }
21916           return;
21917         }
21918
21919     if (c != prev->die_sib)
21920       prev->die_sib = c;
21921     prune_unused_types_prune (c);
21922   } while (c != die->die_child);
21923 }
21924
21925 /* A helper function for dwarf2out_finish called through
21926    htab_traverse.  Clear .debug_str strings that we haven't already
21927    decided to emit.  */
21928
21929 static int
21930 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21931 {
21932   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21933
21934   if (!node->label || !node->refcount)
21935     htab_clear_slot (debug_str_hash, h);
21936
21937   return 1;
21938 }
21939
21940 /* Remove dies representing declarations that we never use.  */
21941
21942 static void
21943 prune_unused_types (void)
21944 {
21945   unsigned int i;
21946   limbo_die_node *node;
21947   comdat_type_node *ctnode;
21948   pubname_ref pub;
21949   dcall_entry *dcall;
21950
21951 #if ENABLE_ASSERT_CHECKING
21952   /* All the marks should already be clear.  */
21953   verify_marks_clear (comp_unit_die);
21954   for (node = limbo_die_list; node; node = node->next)
21955     verify_marks_clear (node->die);
21956   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21957     verify_marks_clear (ctnode->root_die);
21958 #endif /* ENABLE_ASSERT_CHECKING */
21959
21960   /* Mark types that are used in global variables.  */
21961   premark_types_used_by_global_vars ();
21962
21963   /* Set the mark on nodes that are actually used.  */
21964   prune_unused_types_walk (comp_unit_die);
21965   for (node = limbo_die_list; node; node = node->next)
21966     prune_unused_types_walk (node->die);
21967   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21968     {
21969       prune_unused_types_walk (ctnode->root_die);
21970       prune_unused_types_mark (ctnode->type_die, 1);
21971     }
21972
21973   /* Also set the mark on nodes referenced from the
21974      pubname_table or arange_table.  */
21975   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
21976     prune_unused_types_mark (pub->die, 1);
21977   for (i = 0; i < arange_table_in_use; i++)
21978     prune_unused_types_mark (arange_table[i], 1);
21979
21980   /* Mark nodes referenced from the direct call table.  */
21981   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
21982     prune_unused_types_mark (dcall->targ_die, 1);
21983
21984   /* Get rid of nodes that aren't marked; and update the string counts.  */
21985   if (debug_str_hash && debug_str_hash_forced)
21986     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
21987   else if (debug_str_hash)
21988     htab_empty (debug_str_hash);
21989   prune_unused_types_prune (comp_unit_die);
21990   for (node = limbo_die_list; node; node = node->next)
21991     prune_unused_types_prune (node->die);
21992   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21993     prune_unused_types_prune (ctnode->root_die);
21994
21995   /* Leave the marks clear.  */
21996   prune_unmark_dies (comp_unit_die);
21997   for (node = limbo_die_list; node; node = node->next)
21998     prune_unmark_dies (node->die);
21999   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22000     prune_unmark_dies (ctnode->root_die);
22001 }
22002
22003 /* Set the parameter to true if there are any relative pathnames in
22004    the file table.  */
22005 static int
22006 file_table_relative_p (void ** slot, void *param)
22007 {
22008   bool *p = (bool *) param;
22009   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22010   if (!IS_ABSOLUTE_PATH (d->filename))
22011     {
22012       *p = true;
22013       return 0;
22014     }
22015   return 1;
22016 }
22017
22018 /* Routines to manipulate hash table of comdat type units.  */
22019
22020 static hashval_t
22021 htab_ct_hash (const void *of)
22022 {
22023   hashval_t h;
22024   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22025
22026   memcpy (&h, type_node->signature, sizeof (h));
22027   return h;
22028 }
22029
22030 static int
22031 htab_ct_eq (const void *of1, const void *of2)
22032 {
22033   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22034   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22035
22036   return (! memcmp (type_node_1->signature, type_node_2->signature,
22037                     DWARF_TYPE_SIGNATURE_SIZE));
22038 }
22039
22040 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22041    to the location it would have been added, should we know its
22042    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22043    probably improve compactness of debug info, removing equivalent
22044    abbrevs, and hide any differences caused by deferring the
22045    computation of the assembler name, triggered by e.g. PCH.  */
22046
22047 static inline void
22048 move_linkage_attr (dw_die_ref die)
22049 {
22050   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22051   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22052
22053   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22054               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22055
22056   while (--ix > 0)
22057     {
22058       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22059
22060       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22061         break;
22062     }
22063
22064   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22065     {
22066       VEC_pop (dw_attr_node, die->die_attr);
22067       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22068     }
22069 }
22070
22071 /* Helper function for resolve_addr, attempt to resolve
22072    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22073    SYMBOL_REFs refer to variables emitted in the current CU.  */
22074
22075 static int
22076 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22077 {
22078   rtx rtl = *addr;
22079
22080   if (GET_CODE (rtl) == CONST_STRING)
22081     {
22082       size_t len = strlen (XSTR (rtl, 0)) + 1;
22083       tree t = build_string (len, XSTR (rtl, 0));
22084       tree tlen = build_int_cst (NULL_TREE, len - 1);
22085       TREE_TYPE (t)
22086         = build_array_type (char_type_node, build_index_type (tlen));
22087       rtl = lookup_constant_def (t);
22088       if (!rtl || !MEM_P (rtl))
22089         return 1;
22090       rtl = XEXP (rtl, 0);
22091       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22092       *addr = rtl;
22093       return 0;
22094     }
22095
22096   if (GET_CODE (rtl) == SYMBOL_REF
22097       && SYMBOL_REF_DECL (rtl)
22098       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22099     return 1;
22100
22101   if (GET_CODE (rtl) == CONST
22102       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22103     return 1;
22104
22105   return 0;
22106 }
22107
22108 /* Helper function for resolve_addr, handle one location
22109    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22110    the location list couldn't be resolved.  */
22111
22112 static bool
22113 resolve_addr_in_expr (dw_loc_descr_ref loc)
22114 {
22115   for (; loc; loc = loc->dw_loc_next)
22116     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22117          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22118         || (loc->dw_loc_opc == DW_OP_implicit_value
22119             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22120             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22121       return false;
22122   return true;
22123 }
22124
22125 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22126    an address in .rodata section if the string literal is emitted there,
22127    or remove the containing location list or replace DW_AT_const_value
22128    with DW_AT_location and empty location expression, if it isn't found
22129    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22130    to something that has been emitted in the current CU.  */
22131
22132 static void
22133 resolve_addr (dw_die_ref die)
22134 {
22135   dw_die_ref c;
22136   dw_attr_ref a;
22137   dw_loc_list_ref *curr;
22138   unsigned ix;
22139
22140   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
22141     switch (AT_class (a))
22142       {
22143       case dw_val_class_loc_list:
22144         curr = AT_loc_list_ptr (a);
22145         while (*curr)
22146           {
22147             if (!resolve_addr_in_expr ((*curr)->expr))
22148               {
22149                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22150                 if (next && (*curr)->ll_symbol)
22151                   {
22152                     gcc_assert (!next->ll_symbol);
22153                     next->ll_symbol = (*curr)->ll_symbol;
22154                   }
22155                 *curr = next;
22156               }
22157             else
22158               curr = &(*curr)->dw_loc_next;
22159           }
22160         if (!AT_loc_list (a))
22161           {
22162             remove_AT (die, a->dw_attr);
22163             ix--;
22164           }
22165         break;
22166       case dw_val_class_loc:
22167         if (!resolve_addr_in_expr (AT_loc (a)))
22168           {
22169             remove_AT (die, a->dw_attr);
22170             ix--;
22171           }
22172         break;
22173       case dw_val_class_addr:
22174         if (a->dw_attr == DW_AT_const_value
22175             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22176           {
22177             remove_AT (die, a->dw_attr);
22178             ix--;
22179           }
22180         break;
22181       default:
22182         break;
22183       }
22184
22185   FOR_EACH_CHILD (die, c, resolve_addr (c));
22186 }
22187
22188 /* Output stuff that dwarf requires at the end of every file,
22189    and generate the DWARF-2 debugging info.  */
22190
22191 static void
22192 dwarf2out_finish (const char *filename)
22193 {
22194   limbo_die_node *node, *next_node;
22195   comdat_type_node *ctnode;
22196   htab_t comdat_type_table;
22197   dw_die_ref die = 0;
22198   unsigned int i;
22199
22200   gen_remaining_tmpl_value_param_die_attribute ();
22201
22202   /* Add the name for the main input file now.  We delayed this from
22203      dwarf2out_init to avoid complications with PCH.  */
22204   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22205   if (!IS_ABSOLUTE_PATH (filename))
22206     add_comp_dir_attribute (comp_unit_die);
22207   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22208     {
22209       bool p = false;
22210       htab_traverse (file_table, file_table_relative_p, &p);
22211       if (p)
22212         add_comp_dir_attribute (comp_unit_die);
22213     }
22214
22215   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22216     {
22217       add_location_or_const_value_attribute (
22218         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22219         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22220         DW_AT_location);
22221     }
22222
22223   /* Traverse the limbo die list, and add parent/child links.  The only
22224      dies without parents that should be here are concrete instances of
22225      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22226      For concrete instances, we can get the parent die from the abstract
22227      instance.  */
22228   for (node = limbo_die_list; node; node = next_node)
22229     {
22230       next_node = node->next;
22231       die = node->die;
22232
22233       if (die->die_parent == NULL)
22234         {
22235           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22236
22237           if (origin)
22238             add_child_die (origin->die_parent, die);
22239           else if (die == comp_unit_die)
22240             ;
22241           else if (seen_error ())
22242             /* It's OK to be confused by errors in the input.  */
22243             add_child_die (comp_unit_die, die);
22244           else
22245             {
22246               /* In certain situations, the lexical block containing a
22247                  nested function can be optimized away, which results
22248                  in the nested function die being orphaned.  Likewise
22249                  with the return type of that nested function.  Force
22250                  this to be a child of the containing function.
22251
22252                  It may happen that even the containing function got fully
22253                  inlined and optimized out.  In that case we are lost and
22254                  assign the empty child.  This should not be big issue as
22255                  the function is likely unreachable too.  */
22256               tree context = NULL_TREE;
22257
22258               gcc_assert (node->created_for);
22259
22260               if (DECL_P (node->created_for))
22261                 context = DECL_CONTEXT (node->created_for);
22262               else if (TYPE_P (node->created_for))
22263                 context = TYPE_CONTEXT (node->created_for);
22264
22265               gcc_assert (context
22266                           && (TREE_CODE (context) == FUNCTION_DECL
22267                               || TREE_CODE (context) == NAMESPACE_DECL));
22268
22269               origin = lookup_decl_die (context);
22270               if (origin)
22271                 add_child_die (origin, die);
22272               else
22273                 add_child_die (comp_unit_die, die);
22274             }
22275         }
22276     }
22277
22278   limbo_die_list = NULL;
22279
22280   resolve_addr (comp_unit_die);
22281
22282   for (node = deferred_asm_name; node; node = node->next)
22283     {
22284       tree decl = node->created_for;
22285       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22286         {
22287           add_linkage_attr (node->die, decl);
22288           move_linkage_attr (node->die);
22289         }
22290     }
22291
22292   deferred_asm_name = NULL;
22293
22294   /* Walk through the list of incomplete types again, trying once more to
22295      emit full debugging info for them.  */
22296   retry_incomplete_types ();
22297
22298   if (flag_eliminate_unused_debug_types)
22299     prune_unused_types ();
22300
22301   /* Generate separate CUs for each of the include files we've seen.
22302      They will go into limbo_die_list.  */
22303   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22304     break_out_includes (comp_unit_die);
22305
22306   /* Generate separate COMDAT sections for type DIEs. */
22307   if (dwarf_version >= 4)
22308     {
22309       break_out_comdat_types (comp_unit_die);
22310
22311       /* Each new type_unit DIE was added to the limbo die list when created.
22312          Since these have all been added to comdat_type_list, clear the
22313          limbo die list.  */
22314       limbo_die_list = NULL;
22315
22316       /* For each new comdat type unit, copy declarations for incomplete
22317          types to make the new unit self-contained (i.e., no direct
22318          references to the main compile unit).  */
22319       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22320         copy_decls_for_unworthy_types (ctnode->root_die);
22321       copy_decls_for_unworthy_types (comp_unit_die);
22322
22323       /* In the process of copying declarations from one unit to another,
22324          we may have left some declarations behind that are no longer
22325          referenced.  Prune them.  */
22326       prune_unused_types ();
22327     }
22328
22329   /* Traverse the DIE's and add add sibling attributes to those DIE's
22330      that have children.  */
22331   add_sibling_attributes (comp_unit_die);
22332   for (node = limbo_die_list; node; node = node->next)
22333     add_sibling_attributes (node->die);
22334   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22335     add_sibling_attributes (ctnode->root_die);
22336
22337   /* Output a terminator label for the .text section.  */
22338   switch_to_section (text_section);
22339   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22340   if (flag_reorder_blocks_and_partition)
22341     {
22342       switch_to_section (unlikely_text_section ());
22343       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22344     }
22345
22346   /* We can only use the low/high_pc attributes if all of the code was
22347      in .text.  */
22348   if (!have_multiple_function_sections
22349       || !(dwarf_version >= 3 || !dwarf_strict))
22350     {
22351       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22352       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22353     }
22354
22355   else
22356     {
22357       unsigned fde_idx = 0;
22358       bool range_list_added = false;
22359
22360       /* We need to give .debug_loc and .debug_ranges an appropriate
22361          "base address".  Use zero so that these addresses become
22362          absolute.  Historically, we've emitted the unexpected
22363          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22364          Emit both to give time for other tools to adapt.  */
22365       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22366       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22367
22368       if (text_section_used)
22369         add_ranges_by_labels (comp_unit_die, text_section_label,
22370                               text_end_label, &range_list_added);
22371       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22372         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22373                               cold_end_label, &range_list_added);
22374
22375       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22376         {
22377           dw_fde_ref fde = &fde_table[fde_idx];
22378
22379           if (fde->dw_fde_switched_sections)
22380             {
22381               if (!fde->in_std_section)
22382                 add_ranges_by_labels (comp_unit_die,
22383                                       fde->dw_fde_hot_section_label,
22384                                       fde->dw_fde_hot_section_end_label,
22385                                       &range_list_added);
22386               if (!fde->cold_in_std_section)
22387                 add_ranges_by_labels (comp_unit_die,
22388                                       fde->dw_fde_unlikely_section_label,
22389                                       fde->dw_fde_unlikely_section_end_label,
22390                                       &range_list_added);
22391             }
22392           else if (!fde->in_std_section)
22393             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22394                                   fde->dw_fde_end, &range_list_added);
22395         }
22396
22397       if (range_list_added)
22398         add_ranges (NULL);
22399     }
22400
22401   /* Output location list section if necessary.  */
22402   if (have_location_lists)
22403     {
22404       /* Output the location lists info.  */
22405       switch_to_section (debug_loc_section);
22406       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22407                                    DEBUG_LOC_SECTION_LABEL, 0);
22408       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22409       output_location_lists (die);
22410     }
22411
22412   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22413     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22414                     debug_line_section_label);
22415
22416   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22417     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22418
22419   /* Output all of the compilation units.  We put the main one last so that
22420      the offsets are available to output_pubnames.  */
22421   for (node = limbo_die_list; node; node = node->next)
22422     output_comp_unit (node->die, 0);
22423
22424   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22425   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22426     {
22427       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22428
22429       /* Don't output duplicate types.  */
22430       if (*slot != HTAB_EMPTY_ENTRY)
22431         continue;
22432
22433       /* Add a pointer to the line table for the main compilation unit
22434          so that the debugger can make sense of DW_AT_decl_file
22435          attributes.  */
22436       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22437         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22438                         debug_line_section_label);
22439
22440       output_comdat_type_unit (ctnode);
22441       *slot = ctnode;
22442     }
22443   htab_delete (comdat_type_table);
22444
22445   /* Output the main compilation unit if non-empty or if .debug_macinfo
22446      has been emitted.  */
22447   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22448
22449   /* Output the abbreviation table.  */
22450   switch_to_section (debug_abbrev_section);
22451   output_abbrev_section ();
22452
22453   /* Output public names table if necessary.  */
22454   if (!VEC_empty (pubname_entry, pubname_table))
22455     {
22456       switch_to_section (debug_pubnames_section);
22457       output_pubnames (pubname_table);
22458     }
22459
22460   /* Output public types table if necessary.  */
22461   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22462      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22463      simply won't look for the section.  */
22464   if (!VEC_empty (pubname_entry, pubtype_table))
22465     {
22466       switch_to_section (debug_pubtypes_section);
22467       output_pubnames (pubtype_table);
22468     }
22469
22470   /* Output direct and virtual call tables if necessary.  */
22471   if (!VEC_empty (dcall_entry, dcall_table))
22472     {
22473       switch_to_section (debug_dcall_section);
22474       output_dcall_table ();
22475     }
22476   if (!VEC_empty (vcall_entry, vcall_table))
22477     {
22478       switch_to_section (debug_vcall_section);
22479       output_vcall_table ();
22480     }
22481
22482   /* Output the address range information.  We only put functions in the arange
22483      table, so don't write it out if we don't have any.  */
22484   if (fde_table_in_use)
22485     {
22486       switch_to_section (debug_aranges_section);
22487       output_aranges ();
22488     }
22489
22490   /* Output ranges section if necessary.  */
22491   if (ranges_table_in_use)
22492     {
22493       switch_to_section (debug_ranges_section);
22494       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22495       output_ranges ();
22496     }
22497
22498   /* Output the source line correspondence table.  We must do this
22499      even if there is no line information.  Otherwise, on an empty
22500      translation unit, we will generate a present, but empty,
22501      .debug_info section.  IRIX 6.5 `nm' will then complain when
22502      examining the file.  This is done late so that any filenames
22503      used by the debug_info section are marked as 'used'.  */
22504   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22505     {
22506       switch_to_section (debug_line_section);
22507       output_line_info ();
22508     }
22509
22510   /* Have to end the macro section.  */
22511   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22512     {
22513       switch_to_section (debug_macinfo_section);
22514       dw2_asm_output_data (1, 0, "End compilation unit");
22515     }
22516
22517   /* If we emitted any DW_FORM_strp form attribute, output the string
22518      table too.  */
22519   if (debug_str_hash)
22520     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22521 }
22522 #else
22523
22524 /* This should never be used, but its address is needed for comparisons.  */
22525 const struct gcc_debug_hooks dwarf2_debug_hooks =
22526 {
22527   0,            /* init */
22528   0,            /* finish */
22529   0,            /* assembly_start */
22530   0,            /* define */
22531   0,            /* undef */
22532   0,            /* start_source_file */
22533   0,            /* end_source_file */
22534   0,            /* begin_block */
22535   0,            /* end_block */
22536   0,            /* ignore_block */
22537   0,            /* source_line */
22538   0,            /* begin_prologue */
22539   0,            /* end_prologue */
22540   0,            /* begin_epilogue */
22541   0,            /* end_epilogue */
22542   0,            /* begin_function */
22543   0,            /* end_function */
22544   0,            /* function_decl */
22545   0,            /* global_decl */
22546   0,            /* type_decl */
22547   0,            /* imported_module_or_decl */
22548   0,            /* deferred_inline_function */
22549   0,            /* outlining_inline_function */
22550   0,            /* label */
22551   0,            /* handle_pch */
22552   0,            /* var_location */
22553   0,            /* switch_text_section */
22554   0,            /* direct_call */
22555   0,            /* virtual_call_token */
22556   0,            /* copy_call_info */
22557   0,            /* virtual_call */
22558   0,            /* set_name */
22559   0             /* start_end_main_source_file */
22560 };
22561
22562 #endif /* DWARF2_DEBUGGING_INFO */
22563
22564 #include "gt-dwarf2out.h"