OSDN Git Service

5aa9df4417cd35d5491df97cbaec7ba0d24ad268
[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 "real.h"
68 #include "rtl.h"
69 #include "hard-reg-set.h"
70 #include "regs.h"
71 #include "insn-config.h"
72 #include "reload.h"
73 #include "function.h"
74 #include "output.h"
75 #include "expr.h"
76 #include "libfuncs.h"
77 #include "except.h"
78 #include "dwarf2.h"
79 #include "dwarf2out.h"
80 #include "dwarf2asm.h"
81 #include "toplev.h"
82 #include "varray.h"
83 #include "ggc.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "debug.h"
88 #include "target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.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_hot_section_label;
303   const char *dw_fde_hot_section_end_label;
304   const char *dw_fde_unlikely_section_label;
305   const char *dw_fde_unlikely_section_end_label;
306   dw_cfi_ref dw_fde_cfi;
307   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
308   unsigned funcdef_number;
309   HOST_WIDE_INT stack_realignment;
310   /* Dynamic realign argument pointer register.  */
311   unsigned int drap_reg;
312   /* Virtual dynamic realign argument pointer register.  */
313   unsigned int vdrap_reg;
314   unsigned all_throwers_are_sibcalls : 1;
315   unsigned nothrow : 1;
316   unsigned uses_eh_lsda : 1;
317   /* Whether we did stack realign in this call frame.  */
318   unsigned stack_realign : 1;
319   /* Whether dynamic realign argument pointer register has been saved.  */
320   unsigned drap_reg_saved: 1;
321   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
322   unsigned in_std_section : 1;
323   /* True iff dw_fde_unlikely_section_label is in text_section or
324      cold_text_section.  */
325   unsigned cold_in_std_section : 1;
326   /* True iff switched sections.  */
327   unsigned dw_fde_switched_sections : 1;
328   /* True iff switching from cold to hot section.  */
329   unsigned dw_fde_switched_cold_to_hot : 1;
330 }
331 dw_fde_node;
332
333 /* Maximum size (in bytes) of an artificially generated label.  */
334 #define MAX_ARTIFICIAL_LABEL_BYTES      30
335
336 /* The size of addresses as they appear in the Dwarf 2 data.
337    Some architectures use word addresses to refer to code locations,
338    but Dwarf 2 info always uses byte addresses.  On such machines,
339    Dwarf 2 addresses need to be larger than the architecture's
340    pointers.  */
341 #ifndef DWARF2_ADDR_SIZE
342 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
343 #endif
344
345 /* The size in bytes of a DWARF field indicating an offset or length
346    relative to a debug info section, specified to be 4 bytes in the
347    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
348    as PTR_SIZE.  */
349
350 #ifndef DWARF_OFFSET_SIZE
351 #define DWARF_OFFSET_SIZE 4
352 #endif
353
354 /* The size in bytes of a DWARF 4 type signature.  */
355
356 #ifndef DWARF_TYPE_SIGNATURE_SIZE
357 #define DWARF_TYPE_SIGNATURE_SIZE 8
358 #endif
359
360 /* According to the (draft) DWARF 3 specification, the initial length
361    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
362    bytes are 0xffffffff, followed by the length stored in the next 8
363    bytes.
364
365    However, the SGI/MIPS ABI uses an initial length which is equal to
366    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
367
368 #ifndef DWARF_INITIAL_LENGTH_SIZE
369 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
370 #endif
371
372 /* Round SIZE up to the nearest BOUNDARY.  */
373 #define DWARF_ROUND(SIZE,BOUNDARY) \
374   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
375
376 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
377 #ifndef DWARF_CIE_DATA_ALIGNMENT
378 #ifdef STACK_GROWS_DOWNWARD
379 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
380 #else
381 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
382 #endif
383 #endif
384
385 /* CIE identifier.  */
386 #if HOST_BITS_PER_WIDE_INT >= 64
387 #define DWARF_CIE_ID \
388   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
389 #else
390 #define DWARF_CIE_ID DW_CIE_ID
391 #endif
392
393 /* A pointer to the base of a table that contains frame description
394    information for each routine.  */
395 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
396
397 /* Number of elements currently allocated for fde_table.  */
398 static GTY(()) unsigned fde_table_allocated;
399
400 /* Number of elements in fde_table currently in use.  */
401 static GTY(()) unsigned fde_table_in_use;
402
403 /* Size (in elements) of increments by which we may expand the
404    fde_table.  */
405 #define FDE_TABLE_INCREMENT 256
406
407 /* Get the current fde_table entry we should use.  */
408
409 static inline dw_fde_ref
410 current_fde (void)
411 {
412   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
413 }
414
415 /* A list of call frame insns for the CIE.  */
416 static GTY(()) dw_cfi_ref cie_cfi_head;
417
418 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
419 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
420    attribute that accelerates the lookup of the FDE associated
421    with the subprogram.  This variable holds the table index of the FDE
422    associated with the current function (body) definition.  */
423 static unsigned current_funcdef_fde;
424 #endif
425
426 struct GTY(()) indirect_string_node {
427   const char *str;
428   unsigned int refcount;
429   enum dwarf_form form;
430   char *label;
431 };
432
433 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
434
435 /* True if the compilation unit has location entries that reference
436    debug strings.  */
437 static GTY(()) bool debug_str_hash_forced = false;
438
439 static GTY(()) int dw2_string_counter;
440 static GTY(()) unsigned long dwarf2out_cfi_label_num;
441
442 /* True if the compilation unit places functions in more than one section.  */
443 static GTY(()) bool have_multiple_function_sections = false;
444
445 /* Whether the default text and cold text sections have been used at all.  */
446
447 static GTY(()) bool text_section_used = false;
448 static GTY(()) bool cold_text_section_used = false;
449
450 /* The default cold text section.  */
451 static GTY(()) section *cold_text_section;
452
453 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
454
455 /* Forward declarations for functions defined in this file.  */
456
457 static char *stripattributes (const char *);
458 static const char *dwarf_cfi_name (unsigned);
459 static dw_cfi_ref new_cfi (void);
460 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
461 static void add_fde_cfi (const char *, dw_cfi_ref);
462 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
463 static void lookup_cfa (dw_cfa_location *);
464 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
465 #ifdef DWARF2_UNWIND_INFO
466 static void initial_return_save (rtx);
467 #endif
468 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
469                                           HOST_WIDE_INT);
470 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
471 static void output_cfi_directive (dw_cfi_ref);
472 static void output_call_frame_info (int);
473 static void dwarf2out_note_section_used (void);
474 static void flush_queued_reg_saves (void);
475 static bool clobbers_queued_reg_save (const_rtx);
476 static void dwarf2out_frame_debug_expr (rtx, const char *);
477
478 /* Support for complex CFA locations.  */
479 static void output_cfa_loc (dw_cfi_ref);
480 static void output_cfa_loc_raw (dw_cfi_ref);
481 static void get_cfa_from_loc_descr (dw_cfa_location *,
482                                     struct dw_loc_descr_struct *);
483 static struct dw_loc_descr_struct *build_cfa_loc
484   (dw_cfa_location *, HOST_WIDE_INT);
485 static struct dw_loc_descr_struct *build_cfa_aligned_loc
486   (HOST_WIDE_INT, HOST_WIDE_INT);
487 static void def_cfa_1 (const char *, dw_cfa_location *);
488
489 /* How to start an assembler comment.  */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
493
494 /* Data and reference forms for relocatable data.  */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
497
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION     ".debug_frame"
500 #endif
501
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL        "LFB"
504 #endif
505
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL          "LFE"
508 #endif
509
510 #ifndef FRAME_BEGIN_LABEL
511 #define FRAME_BEGIN_LABEL       "Lframe"
512 #endif
513 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
514 #define CIE_END_LABEL           "LECIE"
515 #define FDE_LABEL               "LSFDE"
516 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
517 #define FDE_END_LABEL           "LEFDE"
518 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
519 #define LINE_NUMBER_END_LABEL   "LELT"
520 #define LN_PROLOG_AS_LABEL      "LASLTP"
521 #define LN_PROLOG_END_LABEL     "LELTP"
522 #define DIE_LABEL_PREFIX        "DW"
523
524 /* The DWARF 2 CFA column which tracks the return address.  Normally this
525    is the column for PC, or the first column after all of the hard
526    registers.  */
527 #ifndef DWARF_FRAME_RETURN_COLUMN
528 #ifdef PC_REGNUM
529 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
530 #else
531 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
532 #endif
533 #endif
534
535 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
536    default, we just provide columns for all registers.  */
537 #ifndef DWARF_FRAME_REGNUM
538 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
539 #endif
540 \f
541 /* Hook used by __throw.  */
542
543 rtx
544 expand_builtin_dwarf_sp_column (void)
545 {
546   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
547   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
548 }
549
550 /* Return a pointer to a copy of the section string name S with all
551    attributes stripped off, and an asterisk prepended (for assemble_name).  */
552
553 static inline char *
554 stripattributes (const char *s)
555 {
556   char *stripped = XNEWVEC (char, strlen (s) + 2);
557   char *p = stripped;
558
559   *p++ = '*';
560
561   while (*s && *s != ',')
562     *p++ = *s++;
563
564   *p = '\0';
565   return stripped;
566 }
567
568 /* MEM is a memory reference for the register size table, each element of
569    which has mode MODE.  Initialize column C as a return address column.  */
570
571 static void
572 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
573 {
574   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
575   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
576   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
577 }
578
579 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
580
581 static inline HOST_WIDE_INT
582 div_data_align (HOST_WIDE_INT off)
583 {
584   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
585   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
586   return r;
587 }
588
589 /* Return true if we need a signed version of a given opcode
590    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
591
592 static inline bool
593 need_data_align_sf_opcode (HOST_WIDE_INT off)
594 {
595   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
596 }
597
598 /* Generate code to initialize the register size table.  */
599
600 void
601 expand_builtin_init_dwarf_reg_sizes (tree address)
602 {
603   unsigned int i;
604   enum machine_mode mode = TYPE_MODE (char_type_node);
605   rtx addr = expand_normal (address);
606   rtx mem = gen_rtx_MEM (BLKmode, addr);
607   bool wrote_return_column = false;
608
609   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
610     {
611       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
612
613       if (rnum < DWARF_FRAME_REGISTERS)
614         {
615           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
616           enum machine_mode save_mode = reg_raw_mode[i];
617           HOST_WIDE_INT size;
618
619           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
620             save_mode = choose_hard_reg_mode (i, 1, true);
621           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
622             {
623               if (save_mode == VOIDmode)
624                 continue;
625               wrote_return_column = true;
626             }
627           size = GET_MODE_SIZE (save_mode);
628           if (offset < 0)
629             continue;
630
631           emit_move_insn (adjust_address (mem, mode, offset),
632                           gen_int_mode (size, mode));
633         }
634     }
635
636   if (!wrote_return_column)
637     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
638
639 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
640   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
641 #endif
642
643   targetm.init_dwarf_reg_sizes_extra (address);
644 }
645
646 /* Convert a DWARF call frame info. operation to its string name */
647
648 static const char *
649 dwarf_cfi_name (unsigned int cfi_opc)
650 {
651   switch (cfi_opc)
652     {
653     case DW_CFA_advance_loc:
654       return "DW_CFA_advance_loc";
655     case DW_CFA_offset:
656       return "DW_CFA_offset";
657     case DW_CFA_restore:
658       return "DW_CFA_restore";
659     case DW_CFA_nop:
660       return "DW_CFA_nop";
661     case DW_CFA_set_loc:
662       return "DW_CFA_set_loc";
663     case DW_CFA_advance_loc1:
664       return "DW_CFA_advance_loc1";
665     case DW_CFA_advance_loc2:
666       return "DW_CFA_advance_loc2";
667     case DW_CFA_advance_loc4:
668       return "DW_CFA_advance_loc4";
669     case DW_CFA_offset_extended:
670       return "DW_CFA_offset_extended";
671     case DW_CFA_restore_extended:
672       return "DW_CFA_restore_extended";
673     case DW_CFA_undefined:
674       return "DW_CFA_undefined";
675     case DW_CFA_same_value:
676       return "DW_CFA_same_value";
677     case DW_CFA_register:
678       return "DW_CFA_register";
679     case DW_CFA_remember_state:
680       return "DW_CFA_remember_state";
681     case DW_CFA_restore_state:
682       return "DW_CFA_restore_state";
683     case DW_CFA_def_cfa:
684       return "DW_CFA_def_cfa";
685     case DW_CFA_def_cfa_register:
686       return "DW_CFA_def_cfa_register";
687     case DW_CFA_def_cfa_offset:
688       return "DW_CFA_def_cfa_offset";
689
690     /* DWARF 3 */
691     case DW_CFA_def_cfa_expression:
692       return "DW_CFA_def_cfa_expression";
693     case DW_CFA_expression:
694       return "DW_CFA_expression";
695     case DW_CFA_offset_extended_sf:
696       return "DW_CFA_offset_extended_sf";
697     case DW_CFA_def_cfa_sf:
698       return "DW_CFA_def_cfa_sf";
699     case DW_CFA_def_cfa_offset_sf:
700       return "DW_CFA_def_cfa_offset_sf";
701
702     /* SGI/MIPS specific */
703     case DW_CFA_MIPS_advance_loc8:
704       return "DW_CFA_MIPS_advance_loc8";
705
706     /* GNU extensions */
707     case DW_CFA_GNU_window_save:
708       return "DW_CFA_GNU_window_save";
709     case DW_CFA_GNU_args_size:
710       return "DW_CFA_GNU_args_size";
711     case DW_CFA_GNU_negative_offset_extended:
712       return "DW_CFA_GNU_negative_offset_extended";
713
714     default:
715       return "DW_CFA_<unknown>";
716     }
717 }
718
719 /* Return a pointer to a newly allocated Call Frame Instruction.  */
720
721 static inline dw_cfi_ref
722 new_cfi (void)
723 {
724   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
725
726   cfi->dw_cfi_next = NULL;
727   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
728   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
729
730   return cfi;
731 }
732
733 /* Add a Call Frame Instruction to list of instructions.  */
734
735 static inline void
736 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
737 {
738   dw_cfi_ref *p;
739   dw_fde_ref fde = current_fde ();
740
741   /* When DRAP is used, CFA is defined with an expression.  Redefine
742      CFA may lead to a different CFA value.   */
743   /* ??? Of course, this heuristic fails when we're annotating epilogues,
744      because of course we'll always want to redefine the CFA back to the
745      stack pointer on the way out.  Where should we move this check?  */
746   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
747     switch (cfi->dw_cfi_opc)
748       {
749         case DW_CFA_def_cfa_register:
750         case DW_CFA_def_cfa_offset:
751         case DW_CFA_def_cfa_offset_sf:
752         case DW_CFA_def_cfa:
753         case DW_CFA_def_cfa_sf:
754           gcc_unreachable ();
755
756         default:
757           break;
758       }
759
760   /* Find the end of the chain.  */
761   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
762     ;
763
764   *p = cfi;
765 }
766
767 /* Generate a new label for the CFI info to refer to.  FORCE is true
768    if a label needs to be output even when using .cfi_* directives.  */
769
770 char *
771 dwarf2out_cfi_label (bool force)
772 {
773   static char label[20];
774
775   if (!force && dwarf2out_do_cfi_asm ())
776     {
777       /* In this case, we will be emitting the asm directive instead of
778          the label, so just return a placeholder to keep the rest of the
779          interfaces happy.  */
780       strcpy (label, "<do not output>");
781     }
782   else
783     {
784       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
785       ASM_OUTPUT_LABEL (asm_out_file, label);
786     }
787
788   return label;
789 }
790
791 /* True if remember_state should be emitted before following CFI directive.  */
792 static bool emit_cfa_remember;
793
794 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
795    or to the CIE if LABEL is NULL.  */
796
797 static void
798 add_fde_cfi (const char *label, dw_cfi_ref cfi)
799 {
800   dw_cfi_ref *list_head;
801
802   if (emit_cfa_remember)
803     {
804       dw_cfi_ref cfi_remember;
805
806       /* Emit the state save.  */
807       emit_cfa_remember = false;
808       cfi_remember = new_cfi ();
809       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
810       add_fde_cfi (label, cfi_remember);
811     }
812
813   list_head = &cie_cfi_head;
814
815   if (dwarf2out_do_cfi_asm ())
816     {
817       if (label)
818         {
819           dw_fde_ref fde = current_fde ();
820
821           gcc_assert (fde != NULL);
822
823           /* We still have to add the cfi to the list so that lookup_cfa
824              works later on.  When -g2 and above we even need to force
825              emitting of CFI labels and add to list a DW_CFA_set_loc for
826              convert_cfa_to_fb_loc_list purposes.  If we're generating
827              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
828              convert_cfa_to_fb_loc_list.  */
829           if (dwarf_version == 2
830               && debug_info_level > DINFO_LEVEL_TERSE
831               && (write_symbols == DWARF2_DEBUG
832                   || write_symbols == VMS_AND_DWARF2_DEBUG))
833             {
834               switch (cfi->dw_cfi_opc)
835                 {
836                 case DW_CFA_def_cfa_offset:
837                 case DW_CFA_def_cfa_offset_sf:
838                 case DW_CFA_def_cfa_register:
839                 case DW_CFA_def_cfa:
840                 case DW_CFA_def_cfa_sf:
841                 case DW_CFA_def_cfa_expression:
842                 case DW_CFA_restore_state:
843                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
844                     label = dwarf2out_cfi_label (true);
845
846                   if (fde->dw_fde_current_label == NULL
847                       || strcmp (label, fde->dw_fde_current_label) != 0)
848                     {
849                       dw_cfi_ref xcfi;
850
851                       label = xstrdup (label);
852
853                       /* Set the location counter to the new label.  */
854                       xcfi = new_cfi ();
855                       /* It doesn't metter whether DW_CFA_set_loc
856                          or DW_CFA_advance_loc4 is added here, those aren't
857                          emitted into assembly, only looked up by
858                          convert_cfa_to_fb_loc_list.  */
859                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
860                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
861                       add_cfi (&fde->dw_fde_cfi, xcfi);
862                       fde->dw_fde_current_label = label;
863                     }
864                   break;
865                 default:
866                   break;
867                 }
868             }
869
870           output_cfi_directive (cfi);
871
872           list_head = &fde->dw_fde_cfi;
873         }
874       /* ??? If this is a CFI for the CIE, we don't emit.  This
875          assumes that the standard CIE contents that the assembler
876          uses matches the standard CIE contents that the compiler
877          uses.  This is probably a bad assumption.  I'm not quite
878          sure how to address this for now.  */
879     }
880   else if (label)
881     {
882       dw_fde_ref fde = current_fde ();
883
884       gcc_assert (fde != NULL);
885
886       if (*label == 0)
887         label = dwarf2out_cfi_label (false);
888
889       if (fde->dw_fde_current_label == NULL
890           || strcmp (label, fde->dw_fde_current_label) != 0)
891         {
892           dw_cfi_ref xcfi;
893
894           label = xstrdup (label);
895
896           /* Set the location counter to the new label.  */
897           xcfi = new_cfi ();
898           /* If we have a current label, advance from there, otherwise
899              set the location directly using set_loc.  */
900           xcfi->dw_cfi_opc = fde->dw_fde_current_label
901                              ? DW_CFA_advance_loc4
902                              : DW_CFA_set_loc;
903           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
904           add_cfi (&fde->dw_fde_cfi, xcfi);
905
906           fde->dw_fde_current_label = label;
907         }
908
909       list_head = &fde->dw_fde_cfi;
910     }
911
912   add_cfi (list_head, cfi);
913 }
914
915 /* Subroutine of lookup_cfa.  */
916
917 static void
918 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
919 {
920   switch (cfi->dw_cfi_opc)
921     {
922     case DW_CFA_def_cfa_offset:
923     case DW_CFA_def_cfa_offset_sf:
924       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
925       break;
926     case DW_CFA_def_cfa_register:
927       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
928       break;
929     case DW_CFA_def_cfa:
930     case DW_CFA_def_cfa_sf:
931       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
932       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
933       break;
934     case DW_CFA_def_cfa_expression:
935       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
936       break;
937
938     case DW_CFA_remember_state:
939       gcc_assert (!remember->in_use);
940       *remember = *loc;
941       remember->in_use = 1;
942       break;
943     case DW_CFA_restore_state:
944       gcc_assert (remember->in_use);
945       *loc = *remember;
946       remember->in_use = 0;
947       break;
948
949     default:
950       break;
951     }
952 }
953
954 /* Find the previous value for the CFA.  */
955
956 static void
957 lookup_cfa (dw_cfa_location *loc)
958 {
959   dw_cfi_ref cfi;
960   dw_fde_ref fde;
961   dw_cfa_location remember;
962
963   memset (loc, 0, sizeof (*loc));
964   loc->reg = INVALID_REGNUM;
965   remember = *loc;
966
967   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
968     lookup_cfa_1 (cfi, loc, &remember);
969
970   fde = current_fde ();
971   if (fde)
972     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
973       lookup_cfa_1 (cfi, loc, &remember);
974 }
975
976 /* The current rule for calculating the DWARF2 canonical frame address.  */
977 static dw_cfa_location cfa;
978
979 /* The register used for saving registers to the stack, and its offset
980    from the CFA.  */
981 static dw_cfa_location cfa_store;
982
983 /* The current save location around an epilogue.  */
984 static dw_cfa_location cfa_remember;
985
986 /* The running total of the size of arguments pushed onto the stack.  */
987 static HOST_WIDE_INT args_size;
988
989 /* The last args_size we actually output.  */
990 static HOST_WIDE_INT old_args_size;
991
992 /* Entry point to update the canonical frame address (CFA).
993    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
994    calculated from REG+OFFSET.  */
995
996 void
997 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
998 {
999   dw_cfa_location loc;
1000   loc.indirect = 0;
1001   loc.base_offset = 0;
1002   loc.reg = reg;
1003   loc.offset = offset;
1004   def_cfa_1 (label, &loc);
1005 }
1006
1007 /* Determine if two dw_cfa_location structures define the same data.  */
1008
1009 static bool
1010 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1011 {
1012   return (loc1->reg == loc2->reg
1013           && loc1->offset == loc2->offset
1014           && loc1->indirect == loc2->indirect
1015           && (loc1->indirect == 0
1016               || loc1->base_offset == loc2->base_offset));
1017 }
1018
1019 /* This routine does the actual work.  The CFA is now calculated from
1020    the dw_cfa_location structure.  */
1021
1022 static void
1023 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1024 {
1025   dw_cfi_ref cfi;
1026   dw_cfa_location old_cfa, loc;
1027
1028   cfa = *loc_p;
1029   loc = *loc_p;
1030
1031   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1032     cfa_store.offset = loc.offset;
1033
1034   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1035   lookup_cfa (&old_cfa);
1036
1037   /* If nothing changed, no need to issue any call frame instructions.  */
1038   if (cfa_equal_p (&loc, &old_cfa))
1039     return;
1040
1041   cfi = new_cfi ();
1042
1043   if (loc.reg == old_cfa.reg && !loc.indirect)
1044     {
1045       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1046          the CFA register did not change but the offset did.  The data
1047          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1048          in the assembler via the .cfi_def_cfa_offset directive.  */
1049       if (loc.offset < 0)
1050         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1051       else
1052         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1053       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1054     }
1055
1056 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1057   else if (loc.offset == old_cfa.offset
1058            && old_cfa.reg != INVALID_REGNUM
1059            && !loc.indirect)
1060     {
1061       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1062          indicating the CFA register has changed to <register> but the
1063          offset has not changed.  */
1064       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1065       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1066     }
1067 #endif
1068
1069   else if (loc.indirect == 0)
1070     {
1071       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1072          indicating the CFA register has changed to <register> with
1073          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1074          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1075          directive.  */
1076       if (loc.offset < 0)
1077         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1078       else
1079         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1080       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1081       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1082     }
1083   else
1084     {
1085       /* Construct a DW_CFA_def_cfa_expression instruction to
1086          calculate the CFA using a full location expression since no
1087          register-offset pair is available.  */
1088       struct dw_loc_descr_struct *loc_list;
1089
1090       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1091       loc_list = build_cfa_loc (&loc, 0);
1092       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1093     }
1094
1095   add_fde_cfi (label, cfi);
1096 }
1097
1098 /* Add the CFI for saving a register.  REG is the CFA column number.
1099    LABEL is passed to add_fde_cfi.
1100    If SREG is -1, the register is saved at OFFSET from the CFA;
1101    otherwise it is saved in SREG.  */
1102
1103 static void
1104 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1105 {
1106   dw_cfi_ref cfi = new_cfi ();
1107   dw_fde_ref fde = current_fde ();
1108
1109   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1110
1111   /* When stack is aligned, store REG using DW_CFA_expression with
1112      FP.  */
1113   if (fde
1114       && fde->stack_realign
1115       && sreg == INVALID_REGNUM)
1116     {
1117       cfi->dw_cfi_opc = DW_CFA_expression;
1118       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1119       cfi->dw_cfi_oprnd1.dw_cfi_loc
1120         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1121     }
1122   else if (sreg == INVALID_REGNUM)
1123     {
1124       if (need_data_align_sf_opcode (offset))
1125         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1126       else if (reg & ~0x3f)
1127         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1128       else
1129         cfi->dw_cfi_opc = DW_CFA_offset;
1130       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1131     }
1132   else if (sreg == reg)
1133     cfi->dw_cfi_opc = DW_CFA_same_value;
1134   else
1135     {
1136       cfi->dw_cfi_opc = DW_CFA_register;
1137       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1138     }
1139
1140   add_fde_cfi (label, cfi);
1141 }
1142
1143 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1144    This CFI tells the unwinder that it needs to restore the window registers
1145    from the previous frame's window save area.
1146
1147    ??? Perhaps we should note in the CIE where windows are saved (instead of
1148    assuming 0(cfa)) and what registers are in the window.  */
1149
1150 void
1151 dwarf2out_window_save (const char *label)
1152 {
1153   dw_cfi_ref cfi = new_cfi ();
1154
1155   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1156   add_fde_cfi (label, cfi);
1157 }
1158
1159 /* Entry point for saving a register to the stack.  REG is the GCC register
1160    number.  LABEL and OFFSET are passed to reg_save.  */
1161
1162 void
1163 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1164 {
1165   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1166 }
1167
1168 /* Entry point for saving the return address in the stack.
1169    LABEL and OFFSET are passed to reg_save.  */
1170
1171 void
1172 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1173 {
1174   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1175 }
1176
1177 /* Entry point for saving the return address in a register.
1178    LABEL and SREG are passed to reg_save.  */
1179
1180 void
1181 dwarf2out_return_reg (const char *label, unsigned int sreg)
1182 {
1183   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1184 }
1185
1186 #ifdef DWARF2_UNWIND_INFO
1187 /* Record the initial position of the return address.  RTL is
1188    INCOMING_RETURN_ADDR_RTX.  */
1189
1190 static void
1191 initial_return_save (rtx rtl)
1192 {
1193   unsigned int reg = INVALID_REGNUM;
1194   HOST_WIDE_INT offset = 0;
1195
1196   switch (GET_CODE (rtl))
1197     {
1198     case REG:
1199       /* RA is in a register.  */
1200       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1201       break;
1202
1203     case MEM:
1204       /* RA is on the stack.  */
1205       rtl = XEXP (rtl, 0);
1206       switch (GET_CODE (rtl))
1207         {
1208         case REG:
1209           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1210           offset = 0;
1211           break;
1212
1213         case PLUS:
1214           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1215           offset = INTVAL (XEXP (rtl, 1));
1216           break;
1217
1218         case MINUS:
1219           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1220           offset = -INTVAL (XEXP (rtl, 1));
1221           break;
1222
1223         default:
1224           gcc_unreachable ();
1225         }
1226
1227       break;
1228
1229     case PLUS:
1230       /* The return address is at some offset from any value we can
1231          actually load.  For instance, on the SPARC it is in %i7+8. Just
1232          ignore the offset for now; it doesn't matter for unwinding frames.  */
1233       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1234       initial_return_save (XEXP (rtl, 0));
1235       return;
1236
1237     default:
1238       gcc_unreachable ();
1239     }
1240
1241   if (reg != DWARF_FRAME_RETURN_COLUMN)
1242     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1243 }
1244 #endif
1245
1246 /* Given a SET, calculate the amount of stack adjustment it
1247    contains.  */
1248
1249 static HOST_WIDE_INT
1250 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1251                      HOST_WIDE_INT cur_offset)
1252 {
1253   const_rtx src = SET_SRC (pattern);
1254   const_rtx dest = SET_DEST (pattern);
1255   HOST_WIDE_INT offset = 0;
1256   enum rtx_code code;
1257
1258   if (dest == stack_pointer_rtx)
1259     {
1260       code = GET_CODE (src);
1261
1262       /* Assume (set (reg sp) (reg whatever)) sets args_size
1263          level to 0.  */
1264       if (code == REG && src != stack_pointer_rtx)
1265         {
1266           offset = -cur_args_size;
1267 #ifndef STACK_GROWS_DOWNWARD
1268           offset = -offset;
1269 #endif
1270           return offset - cur_offset;
1271         }
1272
1273       if (! (code == PLUS || code == MINUS)
1274           || XEXP (src, 0) != stack_pointer_rtx
1275           || !CONST_INT_P (XEXP (src, 1)))
1276         return 0;
1277
1278       /* (set (reg sp) (plus (reg sp) (const_int))) */
1279       offset = INTVAL (XEXP (src, 1));
1280       if (code == PLUS)
1281         offset = -offset;
1282       return offset;
1283     }
1284
1285   if (MEM_P (src) && !MEM_P (dest))
1286     dest = src;
1287   if (MEM_P (dest))
1288     {
1289       /* (set (mem (pre_dec (reg sp))) (foo)) */
1290       src = XEXP (dest, 0);
1291       code = GET_CODE (src);
1292
1293       switch (code)
1294         {
1295         case PRE_MODIFY:
1296         case POST_MODIFY:
1297           if (XEXP (src, 0) == stack_pointer_rtx)
1298             {
1299               rtx val = XEXP (XEXP (src, 1), 1);
1300               /* We handle only adjustments by constant amount.  */
1301               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1302                           && CONST_INT_P (val));
1303               offset = -INTVAL (val);
1304               break;
1305             }
1306           return 0;
1307
1308         case PRE_DEC:
1309         case POST_DEC:
1310           if (XEXP (src, 0) == stack_pointer_rtx)
1311             {
1312               offset = GET_MODE_SIZE (GET_MODE (dest));
1313               break;
1314             }
1315           return 0;
1316
1317         case PRE_INC:
1318         case POST_INC:
1319           if (XEXP (src, 0) == stack_pointer_rtx)
1320             {
1321               offset = -GET_MODE_SIZE (GET_MODE (dest));
1322               break;
1323             }
1324           return 0;
1325
1326         default:
1327           return 0;
1328         }
1329     }
1330   else
1331     return 0;
1332
1333   return offset;
1334 }
1335
1336 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1337    indexed by INSN_UID.  */
1338
1339 static HOST_WIDE_INT *barrier_args_size;
1340
1341 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1342
1343 static HOST_WIDE_INT
1344 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1345                              VEC (rtx, heap) **next)
1346 {
1347   HOST_WIDE_INT offset = 0;
1348   int i;
1349
1350   if (! RTX_FRAME_RELATED_P (insn))
1351     {
1352       if (prologue_epilogue_contains (insn))
1353         /* Nothing */;
1354       else if (GET_CODE (PATTERN (insn)) == SET)
1355         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1356       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1357                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1358         {
1359           /* There may be stack adjustments inside compound insns.  Search
1360              for them.  */
1361           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1362             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1363               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1364                                              cur_args_size, offset);
1365         }
1366     }
1367   else
1368     {
1369       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1370
1371       if (expr)
1372         {
1373           expr = XEXP (expr, 0);
1374           if (GET_CODE (expr) == PARALLEL
1375               || GET_CODE (expr) == SEQUENCE)
1376             for (i = 1; i < XVECLEN (expr, 0); i++)
1377               {
1378                 rtx elem = XVECEXP (expr, 0, i);
1379
1380                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1381                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1382               }
1383         }
1384     }
1385
1386 #ifndef STACK_GROWS_DOWNWARD
1387   offset = -offset;
1388 #endif
1389
1390   cur_args_size += offset;
1391   if (cur_args_size < 0)
1392     cur_args_size = 0;
1393
1394   if (JUMP_P (insn))
1395     {
1396       rtx dest = JUMP_LABEL (insn);
1397
1398       if (dest)
1399         {
1400           if (barrier_args_size [INSN_UID (dest)] < 0)
1401             {
1402               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1403               VEC_safe_push (rtx, heap, *next, dest);
1404             }
1405         }
1406     }
1407
1408   return cur_args_size;
1409 }
1410
1411 /* Walk the whole function and compute args_size on BARRIERs.  */
1412
1413 static void
1414 compute_barrier_args_size (void)
1415 {
1416   int max_uid = get_max_uid (), i;
1417   rtx insn;
1418   VEC (rtx, heap) *worklist, *next, *tmp;
1419
1420   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1421   for (i = 0; i < max_uid; i++)
1422     barrier_args_size[i] = -1;
1423
1424   worklist = VEC_alloc (rtx, heap, 20);
1425   next = VEC_alloc (rtx, heap, 20);
1426   insn = get_insns ();
1427   barrier_args_size[INSN_UID (insn)] = 0;
1428   VEC_quick_push (rtx, worklist, insn);
1429   for (;;)
1430     {
1431       while (!VEC_empty (rtx, worklist))
1432         {
1433           rtx prev, body, first_insn;
1434           HOST_WIDE_INT cur_args_size;
1435
1436           first_insn = insn = VEC_pop (rtx, worklist);
1437           cur_args_size = barrier_args_size[INSN_UID (insn)];
1438           prev = prev_nonnote_insn (insn);
1439           if (prev && BARRIER_P (prev))
1440             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1441
1442           for (; insn; insn = NEXT_INSN (insn))
1443             {
1444               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1445                 continue;
1446               if (BARRIER_P (insn))
1447                 break;
1448
1449               if (LABEL_P (insn))
1450                 {
1451                   if (insn == first_insn)
1452                     continue;
1453                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1454                     {
1455                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1456                       continue;
1457                     }
1458                   else
1459                     {
1460                       /* The insns starting with this label have been
1461                          already scanned or are in the worklist.  */
1462                       break;
1463                     }
1464                 }
1465
1466               body = PATTERN (insn);
1467               if (GET_CODE (body) == SEQUENCE)
1468                 {
1469                   HOST_WIDE_INT dest_args_size = cur_args_size;
1470                   for (i = 1; i < XVECLEN (body, 0); i++)
1471                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1472                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1473                       dest_args_size
1474                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1475                                                        dest_args_size, &next);
1476                     else
1477                       cur_args_size
1478                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1479                                                        cur_args_size, &next);
1480
1481                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1482                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1483                                                  dest_args_size, &next);
1484                   else
1485                     cur_args_size
1486                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1487                                                      cur_args_size, &next);
1488                 }
1489               else
1490                 cur_args_size
1491                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1492             }
1493         }
1494
1495       if (VEC_empty (rtx, next))
1496         break;
1497
1498       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1499       tmp = next;
1500       next = worklist;
1501       worklist = tmp;
1502       VEC_truncate (rtx, next, 0);
1503     }
1504
1505   VEC_free (rtx, heap, worklist);
1506   VEC_free (rtx, heap, next);
1507 }
1508
1509 /* Add a CFI to update the running total of the size of arguments
1510    pushed onto the stack.  */
1511
1512 static void
1513 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1514 {
1515   dw_cfi_ref cfi;
1516
1517   if (size == old_args_size)
1518     return;
1519
1520   old_args_size = size;
1521
1522   cfi = new_cfi ();
1523   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1524   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1525   add_fde_cfi (label, cfi);
1526 }
1527
1528 /* Record a stack adjustment of OFFSET bytes.  */
1529
1530 static void
1531 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1532 {
1533   if (cfa.reg == STACK_POINTER_REGNUM)
1534     cfa.offset += offset;
1535
1536   if (cfa_store.reg == STACK_POINTER_REGNUM)
1537     cfa_store.offset += offset;
1538
1539   if (ACCUMULATE_OUTGOING_ARGS)
1540     return;
1541
1542 #ifndef STACK_GROWS_DOWNWARD
1543   offset = -offset;
1544 #endif
1545
1546   args_size += offset;
1547   if (args_size < 0)
1548     args_size = 0;
1549
1550   def_cfa_1 (label, &cfa);
1551   if (flag_asynchronous_unwind_tables)
1552     dwarf2out_args_size (label, args_size);
1553 }
1554
1555 /* Check INSN to see if it looks like a push or a stack adjustment, and
1556    make a note of it if it does.  EH uses this information to find out
1557    how much extra space it needs to pop off the stack.  */
1558
1559 static void
1560 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1561 {
1562   HOST_WIDE_INT offset;
1563   const char *label;
1564   int i;
1565
1566   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1567      with this function.  Proper support would require all frame-related
1568      insns to be marked, and to be able to handle saving state around
1569      epilogues textually in the middle of the function.  */
1570   if (prologue_epilogue_contains (insn))
1571     return;
1572
1573   /* If INSN is an instruction from target of an annulled branch, the
1574      effects are for the target only and so current argument size
1575      shouldn't change at all.  */
1576   if (final_sequence
1577       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1578       && INSN_FROM_TARGET_P (insn))
1579     return;
1580
1581   /* If only calls can throw, and we have a frame pointer,
1582      save up adjustments until we see the CALL_INSN.  */
1583   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1584     {
1585       if (CALL_P (insn) && !after_p)
1586         {
1587           /* Extract the size of the args from the CALL rtx itself.  */
1588           insn = PATTERN (insn);
1589           if (GET_CODE (insn) == PARALLEL)
1590             insn = XVECEXP (insn, 0, 0);
1591           if (GET_CODE (insn) == SET)
1592             insn = SET_SRC (insn);
1593           gcc_assert (GET_CODE (insn) == CALL);
1594           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1595         }
1596       return;
1597     }
1598
1599   if (CALL_P (insn) && !after_p)
1600     {
1601       if (!flag_asynchronous_unwind_tables)
1602         dwarf2out_args_size ("", args_size);
1603       return;
1604     }
1605   else if (BARRIER_P (insn))
1606     {
1607       /* Don't call compute_barrier_args_size () if the only
1608          BARRIER is at the end of function.  */
1609       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1610         compute_barrier_args_size ();
1611       if (barrier_args_size == NULL)
1612         offset = 0;
1613       else
1614         {
1615           offset = barrier_args_size[INSN_UID (insn)];
1616           if (offset < 0)
1617             offset = 0;
1618         }
1619
1620       offset -= args_size;
1621 #ifndef STACK_GROWS_DOWNWARD
1622       offset = -offset;
1623 #endif
1624     }
1625   else if (GET_CODE (PATTERN (insn)) == SET)
1626     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1627   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1628            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1629     {
1630       /* There may be stack adjustments inside compound insns.  Search
1631          for them.  */
1632       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1633         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1634           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1635                                          args_size, offset);
1636     }
1637   else
1638     return;
1639
1640   if (offset == 0)
1641     return;
1642
1643   label = dwarf2out_cfi_label (false);
1644   dwarf2out_stack_adjust (offset, label);
1645 }
1646
1647 #endif
1648
1649 /* We delay emitting a register save until either (a) we reach the end
1650    of the prologue or (b) the register is clobbered.  This clusters
1651    register saves so that there are fewer pc advances.  */
1652
1653 struct GTY(()) queued_reg_save {
1654   struct queued_reg_save *next;
1655   rtx reg;
1656   HOST_WIDE_INT cfa_offset;
1657   rtx saved_reg;
1658 };
1659
1660 static GTY(()) struct queued_reg_save *queued_reg_saves;
1661
1662 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1663 struct GTY(()) reg_saved_in_data {
1664   rtx orig_reg;
1665   rtx saved_in_reg;
1666 };
1667
1668 /* A list of registers saved in other registers.
1669    The list intentionally has a small maximum capacity of 4; if your
1670    port needs more than that, you might consider implementing a
1671    more efficient data structure.  */
1672 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1673 static GTY(()) size_t num_regs_saved_in_regs;
1674
1675 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1676 static const char *last_reg_save_label;
1677
1678 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1679    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1680
1681 static void
1682 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1683 {
1684   struct queued_reg_save *q;
1685
1686   /* Duplicates waste space, but it's also necessary to remove them
1687      for correctness, since the queue gets output in reverse
1688      order.  */
1689   for (q = queued_reg_saves; q != NULL; q = q->next)
1690     if (REGNO (q->reg) == REGNO (reg))
1691       break;
1692
1693   if (q == NULL)
1694     {
1695       q = GGC_NEW (struct queued_reg_save);
1696       q->next = queued_reg_saves;
1697       queued_reg_saves = q;
1698     }
1699
1700   q->reg = reg;
1701   q->cfa_offset = offset;
1702   q->saved_reg = sreg;
1703
1704   last_reg_save_label = label;
1705 }
1706
1707 /* Output all the entries in QUEUED_REG_SAVES.  */
1708
1709 static void
1710 flush_queued_reg_saves (void)
1711 {
1712   struct queued_reg_save *q;
1713
1714   for (q = queued_reg_saves; q; q = q->next)
1715     {
1716       size_t i;
1717       unsigned int reg, sreg;
1718
1719       for (i = 0; i < num_regs_saved_in_regs; i++)
1720         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1721           break;
1722       if (q->saved_reg && i == num_regs_saved_in_regs)
1723         {
1724           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1725           num_regs_saved_in_regs++;
1726         }
1727       if (i != num_regs_saved_in_regs)
1728         {
1729           regs_saved_in_regs[i].orig_reg = q->reg;
1730           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1731         }
1732
1733       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1734       if (q->saved_reg)
1735         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1736       else
1737         sreg = INVALID_REGNUM;
1738       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1739     }
1740
1741   queued_reg_saves = NULL;
1742   last_reg_save_label = NULL;
1743 }
1744
1745 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1746    location for?  Or, does it clobber a register which we've previously
1747    said that some other register is saved in, and for which we now
1748    have a new location for?  */
1749
1750 static bool
1751 clobbers_queued_reg_save (const_rtx insn)
1752 {
1753   struct queued_reg_save *q;
1754
1755   for (q = queued_reg_saves; q; q = q->next)
1756     {
1757       size_t i;
1758       if (modified_in_p (q->reg, insn))
1759         return true;
1760       for (i = 0; i < num_regs_saved_in_regs; i++)
1761         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1762             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1763           return true;
1764     }
1765
1766   return false;
1767 }
1768
1769 /* Entry point for saving the first register into the second.  */
1770
1771 void
1772 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1773 {
1774   size_t i;
1775   unsigned int regno, sregno;
1776
1777   for (i = 0; i < num_regs_saved_in_regs; i++)
1778     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1779       break;
1780   if (i == num_regs_saved_in_regs)
1781     {
1782       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1783       num_regs_saved_in_regs++;
1784     }
1785   regs_saved_in_regs[i].orig_reg = reg;
1786   regs_saved_in_regs[i].saved_in_reg = sreg;
1787
1788   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1789   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1790   reg_save (label, regno, sregno, 0);
1791 }
1792
1793 /* What register, if any, is currently saved in REG?  */
1794
1795 static rtx
1796 reg_saved_in (rtx reg)
1797 {
1798   unsigned int regn = REGNO (reg);
1799   size_t i;
1800   struct queued_reg_save *q;
1801
1802   for (q = queued_reg_saves; q; q = q->next)
1803     if (q->saved_reg && regn == REGNO (q->saved_reg))
1804       return q->reg;
1805
1806   for (i = 0; i < num_regs_saved_in_regs; i++)
1807     if (regs_saved_in_regs[i].saved_in_reg
1808         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1809       return regs_saved_in_regs[i].orig_reg;
1810
1811   return NULL_RTX;
1812 }
1813
1814
1815 /* A temporary register holding an integral value used in adjusting SP
1816    or setting up the store_reg.  The "offset" field holds the integer
1817    value, not an offset.  */
1818 static dw_cfa_location cfa_temp;
1819
1820 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1821
1822 static void
1823 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1824 {
1825   memset (&cfa, 0, sizeof (cfa));
1826
1827   switch (GET_CODE (pat))
1828     {
1829     case PLUS:
1830       cfa.reg = REGNO (XEXP (pat, 0));
1831       cfa.offset = INTVAL (XEXP (pat, 1));
1832       break;
1833
1834     case REG:
1835       cfa.reg = REGNO (pat);
1836       break;
1837
1838     default:
1839       /* Recurse and define an expression.  */
1840       gcc_unreachable ();
1841     }
1842
1843   def_cfa_1 (label, &cfa);
1844 }
1845
1846 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1847
1848 static void
1849 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1850 {
1851   rtx src, dest;
1852
1853   gcc_assert (GET_CODE (pat) == SET);
1854   dest = XEXP (pat, 0);
1855   src = XEXP (pat, 1);
1856
1857   switch (GET_CODE (src))
1858     {
1859     case PLUS:
1860       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1861       cfa.offset -= INTVAL (XEXP (src, 1));
1862       break;
1863
1864     case REG:
1865         break;
1866
1867     default:
1868         gcc_unreachable ();
1869     }
1870
1871   cfa.reg = REGNO (dest);
1872   gcc_assert (cfa.indirect == 0);
1873
1874   def_cfa_1 (label, &cfa);
1875 }
1876
1877 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1878
1879 static void
1880 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1881 {
1882   HOST_WIDE_INT offset;
1883   rtx src, addr, span;
1884
1885   src = XEXP (set, 1);
1886   addr = XEXP (set, 0);
1887   gcc_assert (MEM_P (addr));
1888   addr = XEXP (addr, 0);
1889
1890   /* As documented, only consider extremely simple addresses.  */
1891   switch (GET_CODE (addr))
1892     {
1893     case REG:
1894       gcc_assert (REGNO (addr) == cfa.reg);
1895       offset = -cfa.offset;
1896       break;
1897     case PLUS:
1898       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1899       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1900       break;
1901     default:
1902       gcc_unreachable ();
1903     }
1904
1905   span = targetm.dwarf_register_span (src);
1906
1907   /* ??? We'd like to use queue_reg_save, but we need to come up with
1908      a different flushing heuristic for epilogues.  */
1909   if (!span)
1910     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1911   else
1912     {
1913       /* We have a PARALLEL describing where the contents of SRC live.
1914          Queue register saves for each piece of the PARALLEL.  */
1915       int par_index;
1916       int limit;
1917       HOST_WIDE_INT span_offset = offset;
1918
1919       gcc_assert (GET_CODE (span) == PARALLEL);
1920
1921       limit = XVECLEN (span, 0);
1922       for (par_index = 0; par_index < limit; par_index++)
1923         {
1924           rtx elem = XVECEXP (span, 0, par_index);
1925
1926           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1927                     INVALID_REGNUM, span_offset);
1928           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1929         }
1930     }
1931 }
1932
1933 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1934
1935 static void
1936 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1937 {
1938   rtx src, dest;
1939   unsigned sregno, dregno;
1940
1941   src = XEXP (set, 1);
1942   dest = XEXP (set, 0);
1943
1944   if (src == pc_rtx)
1945     sregno = DWARF_FRAME_RETURN_COLUMN;
1946   else
1947     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1948
1949   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1950
1951   /* ??? We'd like to use queue_reg_save, but we need to come up with
1952      a different flushing heuristic for epilogues.  */
1953   reg_save (label, sregno, dregno, 0);
1954 }
1955
1956 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1957
1958 static void
1959 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1960 {
1961   dw_cfi_ref cfi = new_cfi ();
1962   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1963
1964   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1965   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1966
1967   add_fde_cfi (label, cfi);
1968 }
1969
1970 /* Record call frame debugging information for an expression EXPR,
1971    which either sets SP or FP (adjusting how we calculate the frame
1972    address) or saves a register to the stack or another register.
1973    LABEL indicates the address of EXPR.
1974
1975    This function encodes a state machine mapping rtxes to actions on
1976    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1977    users need not read the source code.
1978
1979   The High-Level Picture
1980
1981   Changes in the register we use to calculate the CFA: Currently we
1982   assume that if you copy the CFA register into another register, we
1983   should take the other one as the new CFA register; this seems to
1984   work pretty well.  If it's wrong for some target, it's simple
1985   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1986
1987   Changes in the register we use for saving registers to the stack:
1988   This is usually SP, but not always.  Again, we deduce that if you
1989   copy SP into another register (and SP is not the CFA register),
1990   then the new register is the one we will be using for register
1991   saves.  This also seems to work.
1992
1993   Register saves: There's not much guesswork about this one; if
1994   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1995   register save, and the register used to calculate the destination
1996   had better be the one we think we're using for this purpose.
1997   It's also assumed that a copy from a call-saved register to another
1998   register is saving that register if RTX_FRAME_RELATED_P is set on
1999   that instruction.  If the copy is from a call-saved register to
2000   the *same* register, that means that the register is now the same
2001   value as in the caller.
2002
2003   Except: If the register being saved is the CFA register, and the
2004   offset is nonzero, we are saving the CFA, so we assume we have to
2005   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2006   the intent is to save the value of SP from the previous frame.
2007
2008   In addition, if a register has previously been saved to a different
2009   register,
2010
2011   Invariants / Summaries of Rules
2012
2013   cfa          current rule for calculating the CFA.  It usually
2014                consists of a register and an offset.
2015   cfa_store    register used by prologue code to save things to the stack
2016                cfa_store.offset is the offset from the value of
2017                cfa_store.reg to the actual CFA
2018   cfa_temp     register holding an integral value.  cfa_temp.offset
2019                stores the value, which will be used to adjust the
2020                stack pointer.  cfa_temp is also used like cfa_store,
2021                to track stores to the stack via fp or a temp reg.
2022
2023   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2024                with cfa.reg as the first operand changes the cfa.reg and its
2025                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2026                cfa_temp.offset.
2027
2028   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2029                expression yielding a constant.  This sets cfa_temp.reg
2030                and cfa_temp.offset.
2031
2032   Rule 5:      Create a new register cfa_store used to save items to the
2033                stack.
2034
2035   Rules 10-14: Save a register to the stack.  Define offset as the
2036                difference of the original location and cfa_store's
2037                location (or cfa_temp's location if cfa_temp is used).
2038
2039   Rules 16-20: If AND operation happens on sp in prologue, we assume
2040                stack is realigned.  We will use a group of DW_OP_XXX
2041                expressions to represent the location of the stored
2042                register instead of CFA+offset.
2043
2044   The Rules
2045
2046   "{a,b}" indicates a choice of a xor b.
2047   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2048
2049   Rule 1:
2050   (set <reg1> <reg2>:cfa.reg)
2051   effects: cfa.reg = <reg1>
2052            cfa.offset unchanged
2053            cfa_temp.reg = <reg1>
2054            cfa_temp.offset = cfa.offset
2055
2056   Rule 2:
2057   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2058                               {<const_int>,<reg>:cfa_temp.reg}))
2059   effects: cfa.reg = sp if fp used
2060            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2061            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2062              if cfa_store.reg==sp
2063
2064   Rule 3:
2065   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2066   effects: cfa.reg = fp
2067            cfa_offset += +/- <const_int>
2068
2069   Rule 4:
2070   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2071   constraints: <reg1> != fp
2072                <reg1> != sp
2073   effects: cfa.reg = <reg1>
2074            cfa_temp.reg = <reg1>
2075            cfa_temp.offset = cfa.offset
2076
2077   Rule 5:
2078   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2079   constraints: <reg1> != fp
2080                <reg1> != sp
2081   effects: cfa_store.reg = <reg1>
2082            cfa_store.offset = cfa.offset - cfa_temp.offset
2083
2084   Rule 6:
2085   (set <reg> <const_int>)
2086   effects: cfa_temp.reg = <reg>
2087            cfa_temp.offset = <const_int>
2088
2089   Rule 7:
2090   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2091   effects: cfa_temp.reg = <reg1>
2092            cfa_temp.offset |= <const_int>
2093
2094   Rule 8:
2095   (set <reg> (high <exp>))
2096   effects: none
2097
2098   Rule 9:
2099   (set <reg> (lo_sum <exp> <const_int>))
2100   effects: cfa_temp.reg = <reg>
2101            cfa_temp.offset = <const_int>
2102
2103   Rule 10:
2104   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2105   effects: cfa_store.offset -= <const_int>
2106            cfa.offset = cfa_store.offset if cfa.reg == sp
2107            cfa.reg = sp
2108            cfa.base_offset = -cfa_store.offset
2109
2110   Rule 11:
2111   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2112   effects: cfa_store.offset += -/+ mode_size(mem)
2113            cfa.offset = cfa_store.offset if cfa.reg == sp
2114            cfa.reg = sp
2115            cfa.base_offset = -cfa_store.offset
2116
2117   Rule 12:
2118   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2119
2120        <reg2>)
2121   effects: cfa.reg = <reg1>
2122            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2123
2124   Rule 13:
2125   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2126   effects: cfa.reg = <reg1>
2127            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2128
2129   Rule 14:
2130   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2131   effects: cfa.reg = <reg1>
2132            cfa.base_offset = -cfa_temp.offset
2133            cfa_temp.offset -= mode_size(mem)
2134
2135   Rule 15:
2136   (set <reg> {unspec, unspec_volatile})
2137   effects: target-dependent
2138
2139   Rule 16:
2140   (set sp (and: sp <const_int>))
2141   constraints: cfa_store.reg == sp
2142   effects: current_fde.stack_realign = 1
2143            cfa_store.offset = 0
2144            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2145
2146   Rule 17:
2147   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2148   effects: cfa_store.offset += -/+ mode_size(mem)
2149
2150   Rule 18:
2151   (set (mem ({pre_inc, pre_dec} sp)) fp)
2152   constraints: fde->stack_realign == 1
2153   effects: cfa_store.offset = 0
2154            cfa.reg != HARD_FRAME_POINTER_REGNUM
2155
2156   Rule 19:
2157   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2158   constraints: fde->stack_realign == 1
2159                && cfa.offset == 0
2160                && cfa.indirect == 0
2161                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2162   effects: Use DW_CFA_def_cfa_expression to define cfa
2163            cfa.reg == fde->drap_reg
2164
2165   Rule 20:
2166   (set reg fde->drap_reg)
2167   constraints: fde->vdrap_reg == INVALID_REGNUM
2168   effects: fde->vdrap_reg = reg.
2169   (set mem fde->drap_reg)
2170   constraints: fde->drap_reg_saved == 1
2171   effects: none.  */
2172
2173 static void
2174 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2175 {
2176   rtx src, dest, span;
2177   HOST_WIDE_INT offset;
2178   dw_fde_ref fde;
2179
2180   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2181      the PARALLEL independently. The first element is always processed if
2182      it is a SET. This is for backward compatibility.   Other elements
2183      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2184      flag is set in them.  */
2185   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2186     {
2187       int par_index;
2188       int limit = XVECLEN (expr, 0);
2189       rtx elem;
2190
2191       /* PARALLELs have strict read-modify-write semantics, so we
2192          ought to evaluate every rvalue before changing any lvalue.
2193          It's cumbersome to do that in general, but there's an
2194          easy approximation that is enough for all current users:
2195          handle register saves before register assignments.  */
2196       if (GET_CODE (expr) == PARALLEL)
2197         for (par_index = 0; par_index < limit; par_index++)
2198           {
2199             elem = XVECEXP (expr, 0, par_index);
2200             if (GET_CODE (elem) == SET
2201                 && MEM_P (SET_DEST (elem))
2202                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2203               dwarf2out_frame_debug_expr (elem, label);
2204           }
2205
2206       for (par_index = 0; par_index < limit; par_index++)
2207         {
2208           elem = XVECEXP (expr, 0, par_index);
2209           if (GET_CODE (elem) == SET
2210               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2211               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2212             dwarf2out_frame_debug_expr (elem, label);
2213           else if (GET_CODE (elem) == SET
2214                    && par_index != 0
2215                    && !RTX_FRAME_RELATED_P (elem))
2216             {
2217               /* Stack adjustment combining might combine some post-prologue
2218                  stack adjustment into a prologue stack adjustment.  */
2219               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2220
2221               if (offset != 0)
2222                 dwarf2out_stack_adjust (offset, label);
2223             }
2224         }
2225       return;
2226     }
2227
2228   gcc_assert (GET_CODE (expr) == SET);
2229
2230   src = SET_SRC (expr);
2231   dest = SET_DEST (expr);
2232
2233   if (REG_P (src))
2234     {
2235       rtx rsi = reg_saved_in (src);
2236       if (rsi)
2237         src = rsi;
2238     }
2239
2240   fde = current_fde ();
2241
2242   if (REG_P (src)
2243       && fde
2244       && fde->drap_reg == REGNO (src)
2245       && (fde->drap_reg_saved
2246           || REG_P (dest)))
2247     {
2248       /* Rule 20 */
2249       /* If we are saving dynamic realign argument pointer to a
2250          register, the destination is virtual dynamic realign
2251          argument pointer.  It may be used to access argument.  */
2252       if (REG_P (dest))
2253         {
2254           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2255           fde->vdrap_reg = REGNO (dest);
2256         }
2257       return;
2258     }
2259
2260   switch (GET_CODE (dest))
2261     {
2262     case REG:
2263       switch (GET_CODE (src))
2264         {
2265           /* Setting FP from SP.  */
2266         case REG:
2267           if (cfa.reg == (unsigned) REGNO (src))
2268             {
2269               /* Rule 1 */
2270               /* Update the CFA rule wrt SP or FP.  Make sure src is
2271                  relative to the current CFA register.
2272
2273                  We used to require that dest be either SP or FP, but the
2274                  ARM copies SP to a temporary register, and from there to
2275                  FP.  So we just rely on the backends to only set
2276                  RTX_FRAME_RELATED_P on appropriate insns.  */
2277               cfa.reg = REGNO (dest);
2278               cfa_temp.reg = cfa.reg;
2279               cfa_temp.offset = cfa.offset;
2280             }
2281           else
2282             {
2283               /* Saving a register in a register.  */
2284               gcc_assert (!fixed_regs [REGNO (dest)]
2285                           /* For the SPARC and its register window.  */
2286                           || (DWARF_FRAME_REGNUM (REGNO (src))
2287                               == DWARF_FRAME_RETURN_COLUMN));
2288
2289               /* After stack is aligned, we can only save SP in FP
2290                  if drap register is used.  In this case, we have
2291                  to restore stack pointer with the CFA value and we
2292                  don't generate this DWARF information.  */
2293               if (fde
2294                   && fde->stack_realign
2295                   && REGNO (src) == STACK_POINTER_REGNUM)
2296                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2297                             && fde->drap_reg != INVALID_REGNUM
2298                             && cfa.reg != REGNO (src));
2299               else
2300                 queue_reg_save (label, src, dest, 0);
2301             }
2302           break;
2303
2304         case PLUS:
2305         case MINUS:
2306         case LO_SUM:
2307           if (dest == stack_pointer_rtx)
2308             {
2309               /* Rule 2 */
2310               /* Adjusting SP.  */
2311               switch (GET_CODE (XEXP (src, 1)))
2312                 {
2313                 case CONST_INT:
2314                   offset = INTVAL (XEXP (src, 1));
2315                   break;
2316                 case REG:
2317                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2318                               == cfa_temp.reg);
2319                   offset = cfa_temp.offset;
2320                   break;
2321                 default:
2322                   gcc_unreachable ();
2323                 }
2324
2325               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2326                 {
2327                   /* Restoring SP from FP in the epilogue.  */
2328                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2329                   cfa.reg = STACK_POINTER_REGNUM;
2330                 }
2331               else if (GET_CODE (src) == LO_SUM)
2332                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2333                 ;
2334               else
2335                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2336
2337               if (GET_CODE (src) != MINUS)
2338                 offset = -offset;
2339               if (cfa.reg == STACK_POINTER_REGNUM)
2340                 cfa.offset += offset;
2341               if (cfa_store.reg == STACK_POINTER_REGNUM)
2342                 cfa_store.offset += offset;
2343             }
2344           else if (dest == hard_frame_pointer_rtx)
2345             {
2346               /* Rule 3 */
2347               /* Either setting the FP from an offset of the SP,
2348                  or adjusting the FP */
2349               gcc_assert (frame_pointer_needed);
2350
2351               gcc_assert (REG_P (XEXP (src, 0))
2352                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2353                           && CONST_INT_P (XEXP (src, 1)));
2354               offset = INTVAL (XEXP (src, 1));
2355               if (GET_CODE (src) != MINUS)
2356                 offset = -offset;
2357               cfa.offset += offset;
2358               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2359             }
2360           else
2361             {
2362               gcc_assert (GET_CODE (src) != MINUS);
2363
2364               /* Rule 4 */
2365               if (REG_P (XEXP (src, 0))
2366                   && REGNO (XEXP (src, 0)) == cfa.reg
2367                   && CONST_INT_P (XEXP (src, 1)))
2368                 {
2369                   /* Setting a temporary CFA register that will be copied
2370                      into the FP later on.  */
2371                   offset = - INTVAL (XEXP (src, 1));
2372                   cfa.offset += offset;
2373                   cfa.reg = REGNO (dest);
2374                   /* Or used to save regs to the stack.  */
2375                   cfa_temp.reg = cfa.reg;
2376                   cfa_temp.offset = cfa.offset;
2377                 }
2378
2379               /* Rule 5 */
2380               else if (REG_P (XEXP (src, 0))
2381                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2382                        && XEXP (src, 1) == stack_pointer_rtx)
2383                 {
2384                   /* Setting a scratch register that we will use instead
2385                      of SP for saving registers to the stack.  */
2386                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2387                   cfa_store.reg = REGNO (dest);
2388                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2389                 }
2390
2391               /* Rule 9 */
2392               else if (GET_CODE (src) == LO_SUM
2393                        && CONST_INT_P (XEXP (src, 1)))
2394                 {
2395                   cfa_temp.reg = REGNO (dest);
2396                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2397                 }
2398               else
2399                 gcc_unreachable ();
2400             }
2401           break;
2402
2403           /* Rule 6 */
2404         case CONST_INT:
2405           cfa_temp.reg = REGNO (dest);
2406           cfa_temp.offset = INTVAL (src);
2407           break;
2408
2409           /* Rule 7 */
2410         case IOR:
2411           gcc_assert (REG_P (XEXP (src, 0))
2412                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2413                       && CONST_INT_P (XEXP (src, 1)));
2414
2415           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2416             cfa_temp.reg = REGNO (dest);
2417           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2418           break;
2419
2420           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2421              which will fill in all of the bits.  */
2422           /* Rule 8 */
2423         case HIGH:
2424           break;
2425
2426           /* Rule 15 */
2427         case UNSPEC:
2428         case UNSPEC_VOLATILE:
2429           gcc_assert (targetm.dwarf_handle_frame_unspec);
2430           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2431           return;
2432
2433           /* Rule 16 */
2434         case AND:
2435           /* If this AND operation happens on stack pointer in prologue,
2436              we assume the stack is realigned and we extract the
2437              alignment.  */
2438           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2439             {
2440               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2441               fde->stack_realign = 1;
2442               fde->stack_realignment = INTVAL (XEXP (src, 1));
2443               cfa_store.offset = 0;
2444
2445               if (cfa.reg != STACK_POINTER_REGNUM
2446                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2447                 fde->drap_reg = cfa.reg;
2448             }
2449           return;
2450
2451         default:
2452           gcc_unreachable ();
2453         }
2454
2455       def_cfa_1 (label, &cfa);
2456       break;
2457
2458     case MEM:
2459
2460       /* Saving a register to the stack.  Make sure dest is relative to the
2461          CFA register.  */
2462       switch (GET_CODE (XEXP (dest, 0)))
2463         {
2464           /* Rule 10 */
2465           /* With a push.  */
2466         case PRE_MODIFY:
2467           /* We can't handle variable size modifications.  */
2468           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2469                       == CONST_INT);
2470           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2471
2472           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2473                       && cfa_store.reg == STACK_POINTER_REGNUM);
2474
2475           cfa_store.offset += offset;
2476           if (cfa.reg == STACK_POINTER_REGNUM)
2477             cfa.offset = cfa_store.offset;
2478
2479           offset = -cfa_store.offset;
2480           break;
2481
2482           /* Rule 11 */
2483         case PRE_INC:
2484         case PRE_DEC:
2485           offset = GET_MODE_SIZE (GET_MODE (dest));
2486           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2487             offset = -offset;
2488
2489           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2490                        == STACK_POINTER_REGNUM)
2491                       && cfa_store.reg == STACK_POINTER_REGNUM);
2492
2493           cfa_store.offset += offset;
2494
2495           /* Rule 18: If stack is aligned, we will use FP as a
2496              reference to represent the address of the stored
2497              regiser.  */
2498           if (fde
2499               && fde->stack_realign
2500               && src == hard_frame_pointer_rtx)
2501             {
2502               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2503               cfa_store.offset = 0;
2504             }
2505
2506           if (cfa.reg == STACK_POINTER_REGNUM)
2507             cfa.offset = cfa_store.offset;
2508
2509           offset = -cfa_store.offset;
2510           break;
2511
2512           /* Rule 12 */
2513           /* With an offset.  */
2514         case PLUS:
2515         case MINUS:
2516         case LO_SUM:
2517           {
2518             int regno;
2519
2520             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2521                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2522             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2523             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2524               offset = -offset;
2525
2526             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2527
2528             if (cfa_store.reg == (unsigned) regno)
2529               offset -= cfa_store.offset;
2530             else
2531               {
2532                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2533                 offset -= cfa_temp.offset;
2534               }
2535           }
2536           break;
2537
2538           /* Rule 13 */
2539           /* Without an offset.  */
2540         case REG:
2541           {
2542             int regno = REGNO (XEXP (dest, 0));
2543
2544             if (cfa_store.reg == (unsigned) regno)
2545               offset = -cfa_store.offset;
2546             else
2547               {
2548                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2549                 offset = -cfa_temp.offset;
2550               }
2551           }
2552           break;
2553
2554           /* Rule 14 */
2555         case POST_INC:
2556           gcc_assert (cfa_temp.reg
2557                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2558           offset = -cfa_temp.offset;
2559           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2560           break;
2561
2562         default:
2563           gcc_unreachable ();
2564         }
2565
2566         /* Rule 17 */
2567         /* If the source operand of this MEM operation is not a
2568            register, basically the source is return address.  Here
2569            we only care how much stack grew and we don't save it.  */
2570       if (!REG_P (src))
2571         break;
2572
2573       if (REGNO (src) != STACK_POINTER_REGNUM
2574           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2575           && (unsigned) REGNO (src) == cfa.reg)
2576         {
2577           /* We're storing the current CFA reg into the stack.  */
2578
2579           if (cfa.offset == 0)
2580             {
2581               /* Rule 19 */
2582               /* If stack is aligned, putting CFA reg into stack means
2583                  we can no longer use reg + offset to represent CFA.
2584                  Here we use DW_CFA_def_cfa_expression instead.  The
2585                  result of this expression equals to the original CFA
2586                  value.  */
2587               if (fde
2588                   && fde->stack_realign
2589                   && cfa.indirect == 0
2590                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2591                 {
2592                   dw_cfa_location cfa_exp;
2593
2594                   gcc_assert (fde->drap_reg == cfa.reg);
2595
2596                   cfa_exp.indirect = 1;
2597                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2598                   cfa_exp.base_offset = offset;
2599                   cfa_exp.offset = 0;
2600
2601                   fde->drap_reg_saved = 1;
2602
2603                   def_cfa_1 (label, &cfa_exp);
2604                   break;
2605                 }
2606
2607               /* If the source register is exactly the CFA, assume
2608                  we're saving SP like any other register; this happens
2609                  on the ARM.  */
2610               def_cfa_1 (label, &cfa);
2611               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2612               break;
2613             }
2614           else
2615             {
2616               /* Otherwise, we'll need to look in the stack to
2617                  calculate the CFA.  */
2618               rtx x = XEXP (dest, 0);
2619
2620               if (!REG_P (x))
2621                 x = XEXP (x, 0);
2622               gcc_assert (REG_P (x));
2623
2624               cfa.reg = REGNO (x);
2625               cfa.base_offset = offset;
2626               cfa.indirect = 1;
2627               def_cfa_1 (label, &cfa);
2628               break;
2629             }
2630         }
2631
2632       def_cfa_1 (label, &cfa);
2633       {
2634         span = targetm.dwarf_register_span (src);
2635
2636         if (!span)
2637           queue_reg_save (label, src, NULL_RTX, offset);
2638         else
2639           {
2640             /* We have a PARALLEL describing where the contents of SRC
2641                live.  Queue register saves for each piece of the
2642                PARALLEL.  */
2643             int par_index;
2644             int limit;
2645             HOST_WIDE_INT span_offset = offset;
2646
2647             gcc_assert (GET_CODE (span) == PARALLEL);
2648
2649             limit = XVECLEN (span, 0);
2650             for (par_index = 0; par_index < limit; par_index++)
2651               {
2652                 rtx elem = XVECEXP (span, 0, par_index);
2653
2654                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2655                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2656               }
2657           }
2658       }
2659       break;
2660
2661     default:
2662       gcc_unreachable ();
2663     }
2664 }
2665
2666 /* Record call frame debugging information for INSN, which either
2667    sets SP or FP (adjusting how we calculate the frame address) or saves a
2668    register to the stack.  If INSN is NULL_RTX, initialize our state.
2669
2670    If AFTER_P is false, we're being called before the insn is emitted,
2671    otherwise after.  Call instructions get invoked twice.  */
2672
2673 void
2674 dwarf2out_frame_debug (rtx insn, bool after_p)
2675 {
2676   const char *label;
2677   rtx note, n;
2678   bool handled_one = false;
2679
2680   if (insn == NULL_RTX)
2681     {
2682       size_t i;
2683
2684       /* Flush any queued register saves.  */
2685       flush_queued_reg_saves ();
2686
2687       /* Set up state for generating call frame debug info.  */
2688       lookup_cfa (&cfa);
2689       gcc_assert (cfa.reg
2690                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2691
2692       cfa.reg = STACK_POINTER_REGNUM;
2693       cfa_store = cfa;
2694       cfa_temp.reg = -1;
2695       cfa_temp.offset = 0;
2696
2697       for (i = 0; i < num_regs_saved_in_regs; i++)
2698         {
2699           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2700           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2701         }
2702       num_regs_saved_in_regs = 0;
2703
2704       if (barrier_args_size)
2705         {
2706           XDELETEVEC (barrier_args_size);
2707           barrier_args_size = NULL;
2708         }
2709       return;
2710     }
2711
2712   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2713     flush_queued_reg_saves ();
2714
2715   if (!RTX_FRAME_RELATED_P (insn))
2716     {
2717       /* ??? This should be done unconditionally since stack adjustments
2718          matter if the stack pointer is not the CFA register anymore but
2719          is still used to save registers.  */
2720       if (!ACCUMULATE_OUTGOING_ARGS)
2721         dwarf2out_notice_stack_adjust (insn, after_p);
2722       return;
2723     }
2724
2725   label = dwarf2out_cfi_label (false);
2726
2727   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2728     switch (REG_NOTE_KIND (note))
2729       {
2730       case REG_FRAME_RELATED_EXPR:
2731         insn = XEXP (note, 0);
2732         goto found;
2733
2734       case REG_CFA_DEF_CFA:
2735         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2736         handled_one = true;
2737         break;
2738
2739       case REG_CFA_ADJUST_CFA:
2740         n = XEXP (note, 0);
2741         if (n == NULL)
2742           {
2743             n = PATTERN (insn);
2744             if (GET_CODE (n) == PARALLEL)
2745               n = XVECEXP (n, 0, 0);
2746           }
2747         dwarf2out_frame_debug_adjust_cfa (n, label);
2748         handled_one = true;
2749         break;
2750
2751       case REG_CFA_OFFSET:
2752         n = XEXP (note, 0);
2753         if (n == NULL)
2754           n = single_set (insn);
2755         dwarf2out_frame_debug_cfa_offset (n, label);
2756         handled_one = true;
2757         break;
2758
2759       case REG_CFA_REGISTER:
2760         n = XEXP (note, 0);
2761         if (n == NULL)
2762           {
2763             n = PATTERN (insn);
2764             if (GET_CODE (n) == PARALLEL)
2765               n = XVECEXP (n, 0, 0);
2766           }
2767         dwarf2out_frame_debug_cfa_register (n, label);
2768         handled_one = true;
2769         break;
2770
2771       case REG_CFA_RESTORE:
2772         n = XEXP (note, 0);
2773         if (n == NULL)
2774           {
2775             n = PATTERN (insn);
2776             if (GET_CODE (n) == PARALLEL)
2777               n = XVECEXP (n, 0, 0);
2778             n = XEXP (n, 0);
2779           }
2780         dwarf2out_frame_debug_cfa_restore (n, label);
2781         handled_one = true;
2782         break;
2783
2784       default:
2785         break;
2786       }
2787   if (handled_one)
2788     return;
2789
2790   insn = PATTERN (insn);
2791  found:
2792   dwarf2out_frame_debug_expr (insn, label);
2793 }
2794
2795 /* Determine if we need to save and restore CFI information around this
2796    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2797    we do need to save/restore, then emit the save now, and insert a
2798    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2799
2800 void
2801 dwarf2out_begin_epilogue (rtx insn)
2802 {
2803   bool saw_frp = false;
2804   rtx i;
2805
2806   /* Scan forward to the return insn, noticing if there are possible
2807      frame related insns.  */
2808   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2809     {
2810       if (!INSN_P (i))
2811         continue;
2812
2813       /* Look for both regular and sibcalls to end the block.  */
2814       if (returnjump_p (i))
2815         break;
2816       if (CALL_P (i) && SIBLING_CALL_P (i))
2817         break;
2818
2819       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2820         {
2821           int idx;
2822           rtx seq = PATTERN (i);
2823
2824           if (returnjump_p (XVECEXP (seq, 0, 0)))
2825             break;
2826           if (CALL_P (XVECEXP (seq, 0, 0))
2827               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2828             break;
2829
2830           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2831             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2832               saw_frp = true;
2833         }
2834
2835       if (RTX_FRAME_RELATED_P (i))
2836         saw_frp = true;
2837     }
2838
2839   /* If the port doesn't emit epilogue unwind info, we don't need a
2840      save/restore pair.  */
2841   if (!saw_frp)
2842     return;
2843
2844   /* Otherwise, search forward to see if the return insn was the last
2845      basic block of the function.  If so, we don't need save/restore.  */
2846   gcc_assert (i != NULL);
2847   i = next_real_insn (i);
2848   if (i == NULL)
2849     return;
2850
2851   /* Insert the restore before that next real insn in the stream, and before
2852      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2853      properly nested.  This should be after any label or alignment.  This
2854      will be pushed into the CFI stream by the function below.  */
2855   while (1)
2856     {
2857       rtx p = PREV_INSN (i);
2858       if (!NOTE_P (p))
2859         break;
2860       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2861         break;
2862       i = p;
2863     }
2864   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2865
2866   emit_cfa_remember = true;
2867
2868   /* And emulate the state save.  */
2869   gcc_assert (!cfa_remember.in_use);
2870   cfa_remember = cfa;
2871   cfa_remember.in_use = 1;
2872 }
2873
2874 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2875
2876 void
2877 dwarf2out_frame_debug_restore_state (void)
2878 {
2879   dw_cfi_ref cfi = new_cfi ();
2880   const char *label = dwarf2out_cfi_label (false);
2881
2882   cfi->dw_cfi_opc = DW_CFA_restore_state;
2883   add_fde_cfi (label, cfi);
2884
2885   gcc_assert (cfa_remember.in_use);
2886   cfa = cfa_remember;
2887   cfa_remember.in_use = 0;
2888 }
2889
2890 #endif
2891
2892 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2893 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2894  (enum dwarf_call_frame_info cfi);
2895
2896 static enum dw_cfi_oprnd_type
2897 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2898 {
2899   switch (cfi)
2900     {
2901     case DW_CFA_nop:
2902     case DW_CFA_GNU_window_save:
2903     case DW_CFA_remember_state:
2904     case DW_CFA_restore_state:
2905       return dw_cfi_oprnd_unused;
2906
2907     case DW_CFA_set_loc:
2908     case DW_CFA_advance_loc1:
2909     case DW_CFA_advance_loc2:
2910     case DW_CFA_advance_loc4:
2911     case DW_CFA_MIPS_advance_loc8:
2912       return dw_cfi_oprnd_addr;
2913
2914     case DW_CFA_offset:
2915     case DW_CFA_offset_extended:
2916     case DW_CFA_def_cfa:
2917     case DW_CFA_offset_extended_sf:
2918     case DW_CFA_def_cfa_sf:
2919     case DW_CFA_restore:
2920     case DW_CFA_restore_extended:
2921     case DW_CFA_undefined:
2922     case DW_CFA_same_value:
2923     case DW_CFA_def_cfa_register:
2924     case DW_CFA_register:
2925       return dw_cfi_oprnd_reg_num;
2926
2927     case DW_CFA_def_cfa_offset:
2928     case DW_CFA_GNU_args_size:
2929     case DW_CFA_def_cfa_offset_sf:
2930       return dw_cfi_oprnd_offset;
2931
2932     case DW_CFA_def_cfa_expression:
2933     case DW_CFA_expression:
2934       return dw_cfi_oprnd_loc;
2935
2936     default:
2937       gcc_unreachable ();
2938     }
2939 }
2940
2941 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2942 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2943  (enum dwarf_call_frame_info cfi);
2944
2945 static enum dw_cfi_oprnd_type
2946 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2947 {
2948   switch (cfi)
2949     {
2950     case DW_CFA_def_cfa:
2951     case DW_CFA_def_cfa_sf:
2952     case DW_CFA_offset:
2953     case DW_CFA_offset_extended_sf:
2954     case DW_CFA_offset_extended:
2955       return dw_cfi_oprnd_offset;
2956
2957     case DW_CFA_register:
2958       return dw_cfi_oprnd_reg_num;
2959
2960     default:
2961       return dw_cfi_oprnd_unused;
2962     }
2963 }
2964
2965 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2966
2967 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2968    switch to the data section instead, and write out a synthetic start label
2969    for collect2 the first time around.  */
2970
2971 static void
2972 switch_to_eh_frame_section (bool back)
2973 {
2974   tree label;
2975
2976 #ifdef EH_FRAME_SECTION_NAME
2977   if (eh_frame_section == 0)
2978     {
2979       int flags;
2980
2981       if (EH_TABLES_CAN_BE_READ_ONLY)
2982         {
2983           int fde_encoding;
2984           int per_encoding;
2985           int lsda_encoding;
2986
2987           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2988                                                        /*global=*/0);
2989           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2990                                                        /*global=*/1);
2991           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2992                                                         /*global=*/0);
2993           flags = ((! flag_pic
2994                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2995                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2996                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2997                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2998                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2999                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3000                    ? 0 : SECTION_WRITE);
3001         }
3002       else
3003         flags = SECTION_WRITE;
3004       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3005     }
3006 #endif
3007
3008   if (eh_frame_section)
3009     switch_to_section (eh_frame_section);
3010   else
3011     {
3012       /* We have no special eh_frame section.  Put the information in
3013          the data section and emit special labels to guide collect2.  */
3014       switch_to_section (data_section);
3015
3016       if (!back)
3017         {
3018           label = get_file_function_name ("F");
3019           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3020           targetm.asm_out.globalize_label (asm_out_file,
3021                                            IDENTIFIER_POINTER (label));
3022           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3023         }
3024     }
3025 }
3026
3027 /* Switch [BACK] to the eh or debug frame table section, depending on
3028    FOR_EH.  */
3029
3030 static void
3031 switch_to_frame_table_section (int for_eh, bool back)
3032 {
3033   if (for_eh)
3034     switch_to_eh_frame_section (back);
3035   else
3036     {
3037       if (!debug_frame_section)
3038         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3039                                            SECTION_DEBUG, NULL);
3040       switch_to_section (debug_frame_section);
3041     }
3042 }
3043
3044 /* Output a Call Frame Information opcode and its operand(s).  */
3045
3046 static void
3047 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3048 {
3049   unsigned long r;
3050   HOST_WIDE_INT off;
3051
3052   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3053     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3054                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3055                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3056                          ((unsigned HOST_WIDE_INT)
3057                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3058   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3059     {
3060       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3061       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3062                            "DW_CFA_offset, column 0x%lx", r);
3063       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3064       dw2_asm_output_data_uleb128 (off, NULL);
3065     }
3066   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3067     {
3068       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3069       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3070                            "DW_CFA_restore, column 0x%lx", r);
3071     }
3072   else
3073     {
3074       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3075                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3076
3077       switch (cfi->dw_cfi_opc)
3078         {
3079         case DW_CFA_set_loc:
3080           if (for_eh)
3081             dw2_asm_output_encoded_addr_rtx (
3082                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3083                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3084                 false, NULL);
3085           else
3086             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3087                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3088           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3089           break;
3090
3091         case DW_CFA_advance_loc1:
3092           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3093                                 fde->dw_fde_current_label, NULL);
3094           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3095           break;
3096
3097         case DW_CFA_advance_loc2:
3098           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3099                                 fde->dw_fde_current_label, NULL);
3100           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3101           break;
3102
3103         case DW_CFA_advance_loc4:
3104           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3105                                 fde->dw_fde_current_label, NULL);
3106           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3107           break;
3108
3109         case DW_CFA_MIPS_advance_loc8:
3110           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3111                                 fde->dw_fde_current_label, NULL);
3112           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3113           break;
3114
3115         case DW_CFA_offset_extended:
3116           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3117           dw2_asm_output_data_uleb128 (r, NULL);
3118           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3119           dw2_asm_output_data_uleb128 (off, NULL);
3120           break;
3121
3122         case DW_CFA_def_cfa:
3123           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3124           dw2_asm_output_data_uleb128 (r, NULL);
3125           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3126           break;
3127
3128         case DW_CFA_offset_extended_sf:
3129           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130           dw2_asm_output_data_uleb128 (r, NULL);
3131           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3132           dw2_asm_output_data_sleb128 (off, NULL);
3133           break;
3134
3135         case DW_CFA_def_cfa_sf:
3136           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3137           dw2_asm_output_data_uleb128 (r, NULL);
3138           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3139           dw2_asm_output_data_sleb128 (off, NULL);
3140           break;
3141
3142         case DW_CFA_restore_extended:
3143         case DW_CFA_undefined:
3144         case DW_CFA_same_value:
3145         case DW_CFA_def_cfa_register:
3146           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3147           dw2_asm_output_data_uleb128 (r, NULL);
3148           break;
3149
3150         case DW_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           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3154           dw2_asm_output_data_uleb128 (r, NULL);
3155           break;
3156
3157         case DW_CFA_def_cfa_offset:
3158         case DW_CFA_GNU_args_size:
3159           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3160           break;
3161
3162         case DW_CFA_def_cfa_offset_sf:
3163           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3164           dw2_asm_output_data_sleb128 (off, NULL);
3165           break;
3166
3167         case DW_CFA_GNU_window_save:
3168           break;
3169
3170         case DW_CFA_def_cfa_expression:
3171         case DW_CFA_expression:
3172           output_cfa_loc (cfi);
3173           break;
3174
3175         case DW_CFA_GNU_negative_offset_extended:
3176           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3177           gcc_unreachable ();
3178
3179         default:
3180           break;
3181         }
3182     }
3183 }
3184
3185 /* Similar, but do it via assembler directives instead.  */
3186
3187 static void
3188 output_cfi_directive (dw_cfi_ref cfi)
3189 {
3190   unsigned long r, r2;
3191
3192   switch (cfi->dw_cfi_opc)
3193     {
3194     case DW_CFA_advance_loc:
3195     case DW_CFA_advance_loc1:
3196     case DW_CFA_advance_loc2:
3197     case DW_CFA_advance_loc4:
3198     case DW_CFA_MIPS_advance_loc8:
3199     case DW_CFA_set_loc:
3200       /* Should only be created by add_fde_cfi in a code path not
3201          followed when emitting via directives.  The assembler is
3202          going to take care of this for us.  */
3203       gcc_unreachable ();
3204
3205     case DW_CFA_offset:
3206     case DW_CFA_offset_extended:
3207     case DW_CFA_offset_extended_sf:
3208       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3209       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3210                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3211       break;
3212
3213     case DW_CFA_restore:
3214     case DW_CFA_restore_extended:
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3216       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3217       break;
3218
3219     case DW_CFA_undefined:
3220       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3221       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3222       break;
3223
3224     case DW_CFA_same_value:
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3226       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3227       break;
3228
3229     case DW_CFA_def_cfa:
3230     case DW_CFA_def_cfa_sf:
3231       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3232       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3233                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3234       break;
3235
3236     case DW_CFA_def_cfa_register:
3237       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3238       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3239       break;
3240
3241     case DW_CFA_register:
3242       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3243       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3244       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3245       break;
3246
3247     case DW_CFA_def_cfa_offset:
3248     case DW_CFA_def_cfa_offset_sf:
3249       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3250                HOST_WIDE_INT_PRINT_DEC"\n",
3251                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3252       break;
3253
3254     case DW_CFA_remember_state:
3255       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3256       break;
3257     case DW_CFA_restore_state:
3258       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3259       break;
3260
3261     case DW_CFA_GNU_args_size:
3262       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3263       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3264       if (flag_debug_asm)
3265         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3266                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3267       fputc ('\n', asm_out_file);
3268       break;
3269
3270     case DW_CFA_GNU_window_save:
3271       fprintf (asm_out_file, "\t.cfi_window_save\n");
3272       break;
3273
3274     case DW_CFA_def_cfa_expression:
3275     case DW_CFA_expression:
3276       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3277       output_cfa_loc_raw (cfi);
3278       fputc ('\n', asm_out_file);
3279       break;
3280
3281     default:
3282       gcc_unreachable ();
3283     }
3284 }
3285
3286 DEF_VEC_P (dw_cfi_ref);
3287 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3288
3289 /* Output CFIs to bring current FDE to the same state as after executing
3290    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3291    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3292    other arguments to pass to output_cfi.  */
3293
3294 static void
3295 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3296 {
3297   struct dw_cfi_struct cfi_buf;
3298   dw_cfi_ref cfi2;
3299   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3300   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3301   unsigned int len, idx;
3302
3303   for (;; cfi = cfi->dw_cfi_next)
3304     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3305       {
3306       case DW_CFA_advance_loc:
3307       case DW_CFA_advance_loc1:
3308       case DW_CFA_advance_loc2:
3309       case DW_CFA_advance_loc4:
3310       case DW_CFA_MIPS_advance_loc8:
3311       case DW_CFA_set_loc:
3312         /* All advances should be ignored.  */
3313         break;
3314       case DW_CFA_remember_state:
3315         {
3316           dw_cfi_ref args_size = cfi_args_size;
3317
3318           /* Skip everything between .cfi_remember_state and
3319              .cfi_restore_state.  */
3320           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3321             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3322               break;
3323             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3324               args_size = cfi2;
3325             else
3326               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3327
3328           if (cfi2 == NULL)
3329             goto flush_all;
3330           else
3331             {
3332               cfi = cfi2;
3333               cfi_args_size = args_size;
3334             }
3335           break;
3336         }
3337       case DW_CFA_GNU_args_size:
3338         cfi_args_size = cfi;
3339         break;
3340       case DW_CFA_GNU_window_save:
3341         goto flush_all;
3342       case DW_CFA_offset:
3343       case DW_CFA_offset_extended:
3344       case DW_CFA_offset_extended_sf:
3345       case DW_CFA_restore:
3346       case DW_CFA_restore_extended:
3347       case DW_CFA_undefined:
3348       case DW_CFA_same_value:
3349       case DW_CFA_register:
3350       case DW_CFA_val_offset:
3351       case DW_CFA_val_offset_sf:
3352       case DW_CFA_expression:
3353       case DW_CFA_val_expression:
3354       case DW_CFA_GNU_negative_offset_extended:
3355         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3356           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3357                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3358         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3359         break;
3360       case DW_CFA_def_cfa:
3361       case DW_CFA_def_cfa_sf:
3362       case DW_CFA_def_cfa_expression:
3363         cfi_cfa = cfi;
3364         cfi_cfa_offset = cfi;
3365         break;
3366       case DW_CFA_def_cfa_register:
3367         cfi_cfa = cfi;
3368         break;
3369       case DW_CFA_def_cfa_offset:
3370       case DW_CFA_def_cfa_offset_sf:
3371         cfi_cfa_offset = cfi;
3372         break;
3373       case DW_CFA_nop:
3374         gcc_assert (cfi == NULL);
3375       flush_all:
3376         len = VEC_length (dw_cfi_ref, regs);
3377         for (idx = 0; idx < len; idx++)
3378           {
3379             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3380             if (cfi2 != NULL
3381                 && cfi2->dw_cfi_opc != DW_CFA_restore
3382                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3383               {
3384                 if (do_cfi_asm)
3385                   output_cfi_directive (cfi2);
3386                 else
3387                   output_cfi (cfi2, fde, for_eh);
3388               }
3389           }
3390         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3391           {
3392             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3393             cfi_buf = *cfi_cfa;
3394             switch (cfi_cfa_offset->dw_cfi_opc)
3395               {
3396               case DW_CFA_def_cfa_offset:
3397                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3398                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3399                 break;
3400               case DW_CFA_def_cfa_offset_sf:
3401                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3402                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3403                 break;
3404               case DW_CFA_def_cfa:
3405               case DW_CFA_def_cfa_sf:
3406                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3407                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3408                 break;
3409               default:
3410                 gcc_unreachable ();
3411               }
3412             cfi_cfa = &cfi_buf;
3413           }
3414         else if (cfi_cfa_offset)
3415           cfi_cfa = cfi_cfa_offset;
3416         if (cfi_cfa)
3417           {
3418             if (do_cfi_asm)
3419               output_cfi_directive (cfi_cfa);
3420             else
3421               output_cfi (cfi_cfa, fde, for_eh);
3422           }
3423         cfi_cfa = NULL;
3424         cfi_cfa_offset = NULL;
3425         if (cfi_args_size
3426             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3427           {
3428             if (do_cfi_asm)
3429               output_cfi_directive (cfi_args_size);
3430             else
3431               output_cfi (cfi_args_size, fde, for_eh);
3432           }
3433         cfi_args_size = NULL;
3434         if (cfi == NULL)
3435           {
3436             VEC_free (dw_cfi_ref, heap, regs);
3437             return;
3438           }
3439         else if (do_cfi_asm)
3440           output_cfi_directive (cfi);
3441         else
3442           output_cfi (cfi, fde, for_eh);
3443         break;
3444       default:
3445         gcc_unreachable ();
3446     }
3447 }
3448
3449 /* Output one FDE.  */
3450
3451 static void
3452 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3453             char *section_start_label, int fde_encoding, char *augmentation,
3454             bool any_lsda_needed, int lsda_encoding)
3455 {
3456   const char *begin, *end;
3457   static unsigned int j;
3458   char l1[20], l2[20];
3459   dw_cfi_ref cfi;
3460
3461   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3462                                 /* empty */ 0);
3463   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3464                                   for_eh + j);
3465   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3466   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3467   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3468     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3469                          " indicating 64-bit DWARF extension");
3470   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3471                         "FDE Length");
3472   ASM_OUTPUT_LABEL (asm_out_file, l1);
3473
3474   if (for_eh)
3475     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3476   else
3477     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3478                            debug_frame_section, "FDE CIE offset");
3479
3480   if (!fde->dw_fde_switched_sections)
3481     {
3482       begin = fde->dw_fde_begin;
3483       end = fde->dw_fde_end;
3484     }
3485   else
3486     {
3487       /* For the first section, prefer dw_fde_begin over
3488          dw_fde_{hot,cold}_section_label, as the latter
3489          might be separated from the real start of the
3490          function by alignment padding.  */
3491       if (!second)
3492         begin = fde->dw_fde_begin;
3493       else if (fde->dw_fde_switched_cold_to_hot)
3494         begin = fde->dw_fde_hot_section_label;
3495       else
3496         begin = fde->dw_fde_unlikely_section_label;
3497       if (second ^ fde->dw_fde_switched_cold_to_hot)
3498         end = fde->dw_fde_unlikely_section_end_label;
3499       else
3500         end = fde->dw_fde_hot_section_end_label;
3501     }
3502
3503   if (for_eh)
3504     {
3505       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3506       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3507       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3508                                        "FDE initial location");
3509       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3510                             end, begin, "FDE address range");
3511     }
3512   else
3513     {
3514       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3515       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3516     }
3517
3518   if (augmentation[0])
3519     {
3520       if (any_lsda_needed)
3521         {
3522           int size = size_of_encoded_value (lsda_encoding);
3523
3524           if (lsda_encoding == DW_EH_PE_aligned)
3525             {
3526               int offset = (  4         /* Length */
3527                             + 4         /* CIE offset */
3528                             + 2 * size_of_encoded_value (fde_encoding)
3529                             + 1         /* Augmentation size */ );
3530               int pad = -offset & (PTR_SIZE - 1);
3531
3532               size += pad;
3533               gcc_assert (size_of_uleb128 (size) == 1);
3534             }
3535
3536           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3537
3538           if (fde->uses_eh_lsda)
3539             {
3540               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3541                                            fde->funcdef_number);
3542               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3543                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3544                                                false,
3545                                                "Language Specific Data Area");
3546             }
3547           else
3548             {
3549               if (lsda_encoding == DW_EH_PE_aligned)
3550                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3551               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3552                                    "Language Specific Data Area (none)");
3553             }
3554         }
3555       else
3556         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3557     }
3558
3559   /* Loop through the Call Frame Instructions associated with
3560      this FDE.  */
3561   fde->dw_fde_current_label = begin;
3562   if (!fde->dw_fde_switched_sections)
3563     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3564       output_cfi (cfi, fde, for_eh);
3565   else if (!second)
3566     {
3567       if (fde->dw_fde_switch_cfi)
3568         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3569           {
3570             output_cfi (cfi, fde, for_eh);
3571             if (cfi == fde->dw_fde_switch_cfi)
3572               break;
3573           }
3574     }
3575   else
3576     {
3577       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3578
3579       if (fde->dw_fde_switch_cfi)
3580         {
3581           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3582           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3583           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3584           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3585         }
3586       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3587         output_cfi (cfi, fde, for_eh);
3588     }
3589
3590   /* If we are to emit a ref/link from function bodies to their frame tables,
3591      do it now.  This is typically performed to make sure that tables
3592      associated with functions are dragged with them and not discarded in
3593      garbage collecting links. We need to do this on a per function basis to
3594      cope with -ffunction-sections.  */
3595
3596 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3597   /* Switch to the function section, emit the ref to the tables, and
3598      switch *back* into the table section.  */
3599   switch_to_section (function_section (fde->decl));
3600   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3601   switch_to_frame_table_section (for_eh, true);
3602 #endif
3603
3604   /* Pad the FDE out to an address sized boundary.  */
3605   ASM_OUTPUT_ALIGN (asm_out_file,
3606                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3607   ASM_OUTPUT_LABEL (asm_out_file, l2);
3608
3609   j += 2;
3610 }
3611
3612 /* Output the call frame information used to record information
3613    that relates to calculating the frame pointer, and records the
3614    location of saved registers.  */
3615
3616 static void
3617 output_call_frame_info (int for_eh)
3618 {
3619   unsigned int i;
3620   dw_fde_ref fde;
3621   dw_cfi_ref cfi;
3622   char l1[20], l2[20], section_start_label[20];
3623   bool any_lsda_needed = false;
3624   char augmentation[6];
3625   int augmentation_size;
3626   int fde_encoding = DW_EH_PE_absptr;
3627   int per_encoding = DW_EH_PE_absptr;
3628   int lsda_encoding = DW_EH_PE_absptr;
3629   int return_reg;
3630   rtx personality = NULL;
3631   int dw_cie_version;
3632
3633   /* Don't emit a CIE if there won't be any FDEs.  */
3634   if (fde_table_in_use == 0)
3635     return;
3636
3637   /* Nothing to do if the assembler's doing it all.  */
3638   if (dwarf2out_do_cfi_asm ())
3639     return;
3640
3641   /* If we make FDEs linkonce, we may have to emit an empty label for
3642      an FDE that wouldn't otherwise be emitted.  We want to avoid
3643      having an FDE kept around when the function it refers to is
3644      discarded.  Example where this matters: a primary function
3645      template in C++ requires EH information, but an explicit
3646      specialization doesn't.  */
3647   if (TARGET_USES_WEAK_UNWIND_INFO
3648       && ! flag_asynchronous_unwind_tables
3649       && flag_exceptions
3650       && for_eh)
3651     for (i = 0; i < fde_table_in_use; i++)
3652       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3653           && !fde_table[i].uses_eh_lsda
3654           && ! DECL_WEAK (fde_table[i].decl))
3655         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3656                                       for_eh, /* empty */ 1);
3657
3658   /* If we don't have any functions we'll want to unwind out of, don't
3659      emit any EH unwind information.  Note that if exceptions aren't
3660      enabled, we won't have collected nothrow information, and if we
3661      asked for asynchronous tables, we always want this info.  */
3662   if (for_eh)
3663     {
3664       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3665
3666       for (i = 0; i < fde_table_in_use; i++)
3667         if (fde_table[i].uses_eh_lsda)
3668           any_eh_needed = any_lsda_needed = true;
3669         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3670           any_eh_needed = true;
3671         else if (! fde_table[i].nothrow
3672                  && ! fde_table[i].all_throwers_are_sibcalls)
3673           any_eh_needed = true;
3674
3675       if (! any_eh_needed)
3676         return;
3677     }
3678
3679   /* We're going to be generating comments, so turn on app.  */
3680   if (flag_debug_asm)
3681     app_enable ();
3682
3683   /* Switch to the proper frame section, first time.  */
3684   switch_to_frame_table_section (for_eh, false);
3685
3686   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3687   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3688
3689   /* Output the CIE.  */
3690   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3691   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3692   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3693     dw2_asm_output_data (4, 0xffffffff,
3694       "Initial length escape value indicating 64-bit DWARF extension");
3695   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3696                         "Length of Common Information Entry");
3697   ASM_OUTPUT_LABEL (asm_out_file, l1);
3698
3699   /* Now that the CIE pointer is PC-relative for EH,
3700      use 0 to identify the CIE.  */
3701   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3702                        (for_eh ? 0 : DWARF_CIE_ID),
3703                        "CIE Identifier Tag");
3704
3705   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3706      use CIE version 1, unless that would produce incorrect results
3707      due to overflowing the return register column.  */
3708   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3709   dw_cie_version = 1;
3710   if (return_reg >= 256 || dwarf_version > 2)
3711     dw_cie_version = 3;
3712   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3713
3714   augmentation[0] = 0;
3715   augmentation_size = 0;
3716
3717   personality = current_unit_personality;
3718   if (for_eh)
3719     {
3720       char *p;
3721
3722       /* Augmentation:
3723          z      Indicates that a uleb128 is present to size the
3724                 augmentation section.
3725          L      Indicates the encoding (and thus presence) of
3726                 an LSDA pointer in the FDE augmentation.
3727          R      Indicates a non-default pointer encoding for
3728                 FDE code pointers.
3729          P      Indicates the presence of an encoding + language
3730                 personality routine in the CIE augmentation.  */
3731
3732       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3733       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3734       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3735
3736       p = augmentation + 1;
3737       if (personality)
3738         {
3739           *p++ = 'P';
3740           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3741           assemble_external_libcall (personality);
3742         }
3743       if (any_lsda_needed)
3744         {
3745           *p++ = 'L';
3746           augmentation_size += 1;
3747         }
3748       if (fde_encoding != DW_EH_PE_absptr)
3749         {
3750           *p++ = 'R';
3751           augmentation_size += 1;
3752         }
3753       if (p > augmentation + 1)
3754         {
3755           augmentation[0] = 'z';
3756           *p = '\0';
3757         }
3758
3759       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3760       if (personality && per_encoding == DW_EH_PE_aligned)
3761         {
3762           int offset = (  4             /* Length */
3763                         + 4             /* CIE Id */
3764                         + 1             /* CIE version */
3765                         + strlen (augmentation) + 1     /* Augmentation */
3766                         + size_of_uleb128 (1)           /* Code alignment */
3767                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3768                         + 1             /* RA column */
3769                         + 1             /* Augmentation size */
3770                         + 1             /* Personality encoding */ );
3771           int pad = -offset & (PTR_SIZE - 1);
3772
3773           augmentation_size += pad;
3774
3775           /* Augmentations should be small, so there's scarce need to
3776              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3777           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3778         }
3779     }
3780
3781   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3782   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3783   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3784                                "CIE Data Alignment Factor");
3785
3786   if (dw_cie_version == 1)
3787     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3788   else
3789     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3790
3791   if (augmentation[0])
3792     {
3793       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3794       if (personality)
3795         {
3796           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3797                                eh_data_format_name (per_encoding));
3798           dw2_asm_output_encoded_addr_rtx (per_encoding,
3799                                            personality,
3800                                            true, NULL);
3801         }
3802
3803       if (any_lsda_needed)
3804         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3805                              eh_data_format_name (lsda_encoding));
3806
3807       if (fde_encoding != DW_EH_PE_absptr)
3808         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3809                              eh_data_format_name (fde_encoding));
3810     }
3811
3812   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3813     output_cfi (cfi, NULL, for_eh);
3814
3815   /* Pad the CIE out to an address sized boundary.  */
3816   ASM_OUTPUT_ALIGN (asm_out_file,
3817                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3818   ASM_OUTPUT_LABEL (asm_out_file, l2);
3819
3820   /* Loop through all of the FDE's.  */
3821   for (i = 0; i < fde_table_in_use; i++)
3822     {
3823       unsigned int k;
3824       fde = &fde_table[i];
3825
3826       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3827       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3828           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3829           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3830           && !fde->uses_eh_lsda)
3831         continue;
3832
3833       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3834         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3835                     augmentation, any_lsda_needed, lsda_encoding);
3836     }
3837
3838   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3839     dw2_asm_output_data (4, 0, "End of Table");
3840 #ifdef MIPS_DEBUGGING_INFO
3841   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3842      get a value of 0.  Putting .align 0 after the label fixes it.  */
3843   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3844 #endif
3845
3846   /* Turn off app to make assembly quicker.  */
3847   if (flag_debug_asm)
3848     app_disable ();
3849 }
3850
3851 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3852
3853 static void
3854 dwarf2out_do_cfi_startproc (bool second)
3855 {
3856   int enc;
3857   rtx ref;
3858   rtx personality = get_personality_function (current_function_decl);
3859
3860   fprintf (asm_out_file, "\t.cfi_startproc\n");
3861
3862   if (personality)
3863     {
3864       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3865       ref = personality;
3866
3867       /* ??? The GAS support isn't entirely consistent.  We have to
3868          handle indirect support ourselves, but PC-relative is done
3869          in the assembler.  Further, the assembler can't handle any
3870          of the weirder relocation types.  */
3871       if (enc & DW_EH_PE_indirect)
3872         ref = dw2_force_const_mem (ref, true);
3873
3874       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3875       output_addr_const (asm_out_file, ref);
3876       fputc ('\n', asm_out_file);
3877     }
3878
3879   if (crtl->uses_eh_lsda)
3880     {
3881       char lab[20];
3882
3883       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3884       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3885                                    current_function_funcdef_no);
3886       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3887       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3888
3889       if (enc & DW_EH_PE_indirect)
3890         ref = dw2_force_const_mem (ref, true);
3891
3892       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3893       output_addr_const (asm_out_file, ref);
3894       fputc ('\n', asm_out_file);
3895     }
3896 }
3897
3898 /* Output a marker (i.e. a label) for the beginning of a function, before
3899    the prologue.  */
3900
3901 void
3902 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3903                           const char *file ATTRIBUTE_UNUSED)
3904 {
3905   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3906   char * dup_label;
3907   dw_fde_ref fde;
3908   section *fnsec;
3909
3910   current_function_func_begin_label = NULL;
3911
3912 #ifdef TARGET_UNWIND_INFO
3913   /* ??? current_function_func_begin_label is also used by except.c
3914      for call-site information.  We must emit this label if it might
3915      be used.  */
3916   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3917       && ! dwarf2out_do_frame ())
3918     return;
3919 #else
3920   if (! dwarf2out_do_frame ())
3921     return;
3922 #endif
3923
3924   fnsec = function_section (current_function_decl);
3925   switch_to_section (fnsec);
3926   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3927                                current_function_funcdef_no);
3928   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3929                           current_function_funcdef_no);
3930   dup_label = xstrdup (label);
3931   current_function_func_begin_label = dup_label;
3932
3933 #ifdef TARGET_UNWIND_INFO
3934   /* We can elide the fde allocation if we're not emitting debug info.  */
3935   if (! dwarf2out_do_frame ())
3936     return;
3937 #endif
3938
3939   /* Expand the fde table if necessary.  */
3940   if (fde_table_in_use == fde_table_allocated)
3941     {
3942       fde_table_allocated += FDE_TABLE_INCREMENT;
3943       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3944       memset (fde_table + fde_table_in_use, 0,
3945               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3946     }
3947
3948   /* Record the FDE associated with this function.  */
3949   current_funcdef_fde = fde_table_in_use;
3950
3951   /* Add the new FDE at the end of the fde_table.  */
3952   fde = &fde_table[fde_table_in_use++];
3953   fde->decl = current_function_decl;
3954   fde->dw_fde_begin = dup_label;
3955   fde->dw_fde_current_label = dup_label;
3956   fde->dw_fde_hot_section_label = NULL;
3957   fde->dw_fde_hot_section_end_label = NULL;
3958   fde->dw_fde_unlikely_section_label = NULL;
3959   fde->dw_fde_unlikely_section_end_label = NULL;
3960   fde->dw_fde_switched_sections = 0;
3961   fde->dw_fde_switched_cold_to_hot = 0;
3962   fde->dw_fde_end = NULL;
3963   fde->dw_fde_cfi = NULL;
3964   fde->dw_fde_switch_cfi = NULL;
3965   fde->funcdef_number = current_function_funcdef_no;
3966   fde->nothrow = crtl->nothrow;
3967   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3968   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3969   fde->drap_reg = INVALID_REGNUM;
3970   fde->vdrap_reg = INVALID_REGNUM;
3971   if (flag_reorder_blocks_and_partition)
3972     {
3973       section *unlikelysec;
3974       if (first_function_block_is_cold)
3975         fde->in_std_section = 1;
3976       else
3977         fde->in_std_section
3978           = (fnsec == text_section
3979              || (cold_text_section && fnsec == cold_text_section));
3980       unlikelysec = unlikely_text_section ();
3981       fde->cold_in_std_section
3982         = (unlikelysec == text_section
3983            || (cold_text_section && unlikelysec == cold_text_section));
3984     }
3985   else
3986     {
3987       fde->in_std_section
3988         = (fnsec == text_section
3989            || (cold_text_section && fnsec == cold_text_section));
3990       fde->cold_in_std_section = 0;
3991     }
3992
3993   args_size = old_args_size = 0;
3994
3995   /* We only want to output line number information for the genuine dwarf2
3996      prologue case, not the eh frame case.  */
3997 #ifdef DWARF2_DEBUGGING_INFO
3998   if (file)
3999     dwarf2out_source_line (line, file, 0, true);
4000 #endif
4001
4002   if (dwarf2out_do_cfi_asm ())
4003     dwarf2out_do_cfi_startproc (false);
4004   else
4005     {
4006       rtx personality = get_personality_function (current_function_decl);
4007       if (!current_unit_personality)
4008         current_unit_personality = personality;
4009
4010       /* We cannot keep a current personality per function as without CFI
4011          asm at the point where we emit the CFI data there is no current
4012          function anymore.  */
4013       if (personality
4014           && current_unit_personality != personality)
4015         sorry ("Multiple EH personalities are supported only with assemblers "
4016                "supporting .cfi.personality directive.");
4017     }
4018 }
4019
4020 /* Output a marker (i.e. a label) for the absolute end of the generated code
4021    for a function definition.  This gets called *after* the epilogue code has
4022    been generated.  */
4023
4024 void
4025 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4026                         const char *file ATTRIBUTE_UNUSED)
4027 {
4028   dw_fde_ref fde;
4029   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4030
4031 #ifdef DWARF2_DEBUGGING_INFO
4032   last_var_location_insn = NULL_RTX;
4033 #endif
4034
4035   if (dwarf2out_do_cfi_asm ())
4036     fprintf (asm_out_file, "\t.cfi_endproc\n");
4037
4038   /* Output a label to mark the endpoint of the code generated for this
4039      function.  */
4040   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4041                                current_function_funcdef_no);
4042   ASM_OUTPUT_LABEL (asm_out_file, label);
4043   fde = current_fde ();
4044   gcc_assert (fde != NULL);
4045   fde->dw_fde_end = xstrdup (label);
4046 }
4047
4048 void
4049 dwarf2out_frame_init (void)
4050 {
4051   /* Allocate the initial hunk of the fde_table.  */
4052   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4053   fde_table_allocated = FDE_TABLE_INCREMENT;
4054   fde_table_in_use = 0;
4055
4056   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4057      sake of lookup_cfa.  */
4058
4059   /* On entry, the Canonical Frame Address is at SP.  */
4060   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4061
4062 #ifdef DWARF2_UNWIND_INFO
4063   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4064     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4065 #endif
4066 }
4067
4068 void
4069 dwarf2out_frame_finish (void)
4070 {
4071   /* Output call frame information.  */
4072   if (DWARF2_FRAME_INFO)
4073     output_call_frame_info (0);
4074
4075 #ifndef TARGET_UNWIND_INFO
4076   /* Output another copy for the unwinder.  */
4077   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4078     output_call_frame_info (1);
4079 #endif
4080 }
4081
4082 /* Note that the current function section is being used for code.  */
4083
4084 static void
4085 dwarf2out_note_section_used (void)
4086 {
4087   section *sec = current_function_section ();
4088   if (sec == text_section)
4089     text_section_used = true;
4090   else if (sec == cold_text_section)
4091     cold_text_section_used = true;
4092 }
4093
4094 void
4095 dwarf2out_switch_text_section (void)
4096 {
4097   dw_fde_ref fde = current_fde ();
4098
4099   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4100
4101   fde->dw_fde_switched_sections = 1;
4102   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4103
4104   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4105   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4106   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4107   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4108   have_multiple_function_sections = true;
4109
4110   /* Reset the current label on switching text sections, so that we
4111      don't attempt to advance_loc4 between labels in different sections.  */
4112   fde->dw_fde_current_label = NULL;
4113
4114   /* There is no need to mark used sections when not debugging.  */
4115   if (cold_text_section != NULL)
4116     dwarf2out_note_section_used ();
4117
4118   if (dwarf2out_do_cfi_asm ())
4119     fprintf (asm_out_file, "\t.cfi_endproc\n");
4120
4121   /* Now do the real section switch.  */
4122   switch_to_section (current_function_section ());
4123
4124   if (dwarf2out_do_cfi_asm ())
4125     {
4126       dwarf2out_do_cfi_startproc (true);
4127       /* As this is a different FDE, insert all current CFI instructions
4128          again.  */
4129       output_cfis (fde->dw_fde_cfi, true, fde, true);
4130     }
4131   else
4132     {
4133       dw_cfi_ref cfi = fde->dw_fde_cfi;
4134
4135       cfi = fde->dw_fde_cfi;
4136       if (cfi)
4137         while (cfi->dw_cfi_next != NULL)
4138           cfi = cfi->dw_cfi_next;
4139       fde->dw_fde_switch_cfi = cfi;
4140     }
4141 }
4142 #endif
4143 \f
4144 /* And now, the subset of the debugging information support code necessary
4145    for emitting location expressions.  */
4146
4147 /* Data about a single source file.  */
4148 struct GTY(()) dwarf_file_data {
4149   const char * filename;
4150   int emitted_number;
4151 };
4152
4153 typedef struct dw_val_struct *dw_val_ref;
4154 typedef struct die_struct *dw_die_ref;
4155 typedef const struct die_struct *const_dw_die_ref;
4156 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4157 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4158
4159 typedef struct GTY(()) deferred_locations_struct
4160 {
4161   tree variable;
4162   dw_die_ref die;
4163 } deferred_locations;
4164
4165 DEF_VEC_O(deferred_locations);
4166 DEF_VEC_ALLOC_O(deferred_locations,gc);
4167
4168 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4169
4170 DEF_VEC_P(dw_die_ref);
4171 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4172
4173 /* Each DIE may have a series of attribute/value pairs.  Values
4174    can take on several forms.  The forms that are used in this
4175    implementation are listed below.  */
4176
4177 enum dw_val_class
4178 {
4179   dw_val_class_addr,
4180   dw_val_class_offset,
4181   dw_val_class_loc,
4182   dw_val_class_loc_list,
4183   dw_val_class_range_list,
4184   dw_val_class_const,
4185   dw_val_class_unsigned_const,
4186   dw_val_class_const_double,
4187   dw_val_class_vec,
4188   dw_val_class_flag,
4189   dw_val_class_die_ref,
4190   dw_val_class_fde_ref,
4191   dw_val_class_lbl_id,
4192   dw_val_class_lineptr,
4193   dw_val_class_str,
4194   dw_val_class_macptr,
4195   dw_val_class_file,
4196   dw_val_class_data8
4197 };
4198
4199 /* Describe a floating point constant value, or a vector constant value.  */
4200
4201 typedef struct GTY(()) dw_vec_struct {
4202   unsigned char * GTY((length ("%h.length"))) array;
4203   unsigned length;
4204   unsigned elt_size;
4205 }
4206 dw_vec_const;
4207
4208 /* The dw_val_node describes an attribute's value, as it is
4209    represented internally.  */
4210
4211 typedef struct GTY(()) dw_val_struct {
4212   enum dw_val_class val_class;
4213   union dw_val_struct_union
4214     {
4215       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4216       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4217       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4218       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4219       HOST_WIDE_INT GTY ((default)) val_int;
4220       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4221       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4222       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4223       struct dw_val_die_union
4224         {
4225           dw_die_ref die;
4226           int external;
4227         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4228       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4229       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4230       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4231       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4232       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4233       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4234     }
4235   GTY ((desc ("%1.val_class"))) v;
4236 }
4237 dw_val_node;
4238
4239 /* Locations in memory are described using a sequence of stack machine
4240    operations.  */
4241
4242 typedef struct GTY(()) dw_loc_descr_struct {
4243   dw_loc_descr_ref dw_loc_next;
4244   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4245   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4246      from DW_OP_addr with a dtp-relative symbol relocation.  */
4247   unsigned int dtprel : 1;
4248   int dw_loc_addr;
4249   dw_val_node dw_loc_oprnd1;
4250   dw_val_node dw_loc_oprnd2;
4251 }
4252 dw_loc_descr_node;
4253
4254 /* Location lists are ranges + location descriptions for that range,
4255    so you can track variables that are in different places over
4256    their entire life.  */
4257 typedef struct GTY(()) dw_loc_list_struct {
4258   dw_loc_list_ref dw_loc_next;
4259   const char *begin; /* Label for begin address of range */
4260   const char *end;  /* Label for end address of range */
4261   char *ll_symbol; /* Label for beginning of location list.
4262                       Only on head of list */
4263   const char *section; /* Section this loclist is relative to */
4264   dw_loc_descr_ref expr;
4265 } dw_loc_list_node;
4266
4267 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4268
4269 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4270
4271 /* Convert a DWARF stack opcode into its string name.  */
4272
4273 static const char *
4274 dwarf_stack_op_name (unsigned int op)
4275 {
4276   switch (op)
4277     {
4278     case DW_OP_addr:
4279       return "DW_OP_addr";
4280     case DW_OP_deref:
4281       return "DW_OP_deref";
4282     case DW_OP_const1u:
4283       return "DW_OP_const1u";
4284     case DW_OP_const1s:
4285       return "DW_OP_const1s";
4286     case DW_OP_const2u:
4287       return "DW_OP_const2u";
4288     case DW_OP_const2s:
4289       return "DW_OP_const2s";
4290     case DW_OP_const4u:
4291       return "DW_OP_const4u";
4292     case DW_OP_const4s:
4293       return "DW_OP_const4s";
4294     case DW_OP_const8u:
4295       return "DW_OP_const8u";
4296     case DW_OP_const8s:
4297       return "DW_OP_const8s";
4298     case DW_OP_constu:
4299       return "DW_OP_constu";
4300     case DW_OP_consts:
4301       return "DW_OP_consts";
4302     case DW_OP_dup:
4303       return "DW_OP_dup";
4304     case DW_OP_drop:
4305       return "DW_OP_drop";
4306     case DW_OP_over:
4307       return "DW_OP_over";
4308     case DW_OP_pick:
4309       return "DW_OP_pick";
4310     case DW_OP_swap:
4311       return "DW_OP_swap";
4312     case DW_OP_rot:
4313       return "DW_OP_rot";
4314     case DW_OP_xderef:
4315       return "DW_OP_xderef";
4316     case DW_OP_abs:
4317       return "DW_OP_abs";
4318     case DW_OP_and:
4319       return "DW_OP_and";
4320     case DW_OP_div:
4321       return "DW_OP_div";
4322     case DW_OP_minus:
4323       return "DW_OP_minus";
4324     case DW_OP_mod:
4325       return "DW_OP_mod";
4326     case DW_OP_mul:
4327       return "DW_OP_mul";
4328     case DW_OP_neg:
4329       return "DW_OP_neg";
4330     case DW_OP_not:
4331       return "DW_OP_not";
4332     case DW_OP_or:
4333       return "DW_OP_or";
4334     case DW_OP_plus:
4335       return "DW_OP_plus";
4336     case DW_OP_plus_uconst:
4337       return "DW_OP_plus_uconst";
4338     case DW_OP_shl:
4339       return "DW_OP_shl";
4340     case DW_OP_shr:
4341       return "DW_OP_shr";
4342     case DW_OP_shra:
4343       return "DW_OP_shra";
4344     case DW_OP_xor:
4345       return "DW_OP_xor";
4346     case DW_OP_bra:
4347       return "DW_OP_bra";
4348     case DW_OP_eq:
4349       return "DW_OP_eq";
4350     case DW_OP_ge:
4351       return "DW_OP_ge";
4352     case DW_OP_gt:
4353       return "DW_OP_gt";
4354     case DW_OP_le:
4355       return "DW_OP_le";
4356     case DW_OP_lt:
4357       return "DW_OP_lt";
4358     case DW_OP_ne:
4359       return "DW_OP_ne";
4360     case DW_OP_skip:
4361       return "DW_OP_skip";
4362     case DW_OP_lit0:
4363       return "DW_OP_lit0";
4364     case DW_OP_lit1:
4365       return "DW_OP_lit1";
4366     case DW_OP_lit2:
4367       return "DW_OP_lit2";
4368     case DW_OP_lit3:
4369       return "DW_OP_lit3";
4370     case DW_OP_lit4:
4371       return "DW_OP_lit4";
4372     case DW_OP_lit5:
4373       return "DW_OP_lit5";
4374     case DW_OP_lit6:
4375       return "DW_OP_lit6";
4376     case DW_OP_lit7:
4377       return "DW_OP_lit7";
4378     case DW_OP_lit8:
4379       return "DW_OP_lit8";
4380     case DW_OP_lit9:
4381       return "DW_OP_lit9";
4382     case DW_OP_lit10:
4383       return "DW_OP_lit10";
4384     case DW_OP_lit11:
4385       return "DW_OP_lit11";
4386     case DW_OP_lit12:
4387       return "DW_OP_lit12";
4388     case DW_OP_lit13:
4389       return "DW_OP_lit13";
4390     case DW_OP_lit14:
4391       return "DW_OP_lit14";
4392     case DW_OP_lit15:
4393       return "DW_OP_lit15";
4394     case DW_OP_lit16:
4395       return "DW_OP_lit16";
4396     case DW_OP_lit17:
4397       return "DW_OP_lit17";
4398     case DW_OP_lit18:
4399       return "DW_OP_lit18";
4400     case DW_OP_lit19:
4401       return "DW_OP_lit19";
4402     case DW_OP_lit20:
4403       return "DW_OP_lit20";
4404     case DW_OP_lit21:
4405       return "DW_OP_lit21";
4406     case DW_OP_lit22:
4407       return "DW_OP_lit22";
4408     case DW_OP_lit23:
4409       return "DW_OP_lit23";
4410     case DW_OP_lit24:
4411       return "DW_OP_lit24";
4412     case DW_OP_lit25:
4413       return "DW_OP_lit25";
4414     case DW_OP_lit26:
4415       return "DW_OP_lit26";
4416     case DW_OP_lit27:
4417       return "DW_OP_lit27";
4418     case DW_OP_lit28:
4419       return "DW_OP_lit28";
4420     case DW_OP_lit29:
4421       return "DW_OP_lit29";
4422     case DW_OP_lit30:
4423       return "DW_OP_lit30";
4424     case DW_OP_lit31:
4425       return "DW_OP_lit31";
4426     case DW_OP_reg0:
4427       return "DW_OP_reg0";
4428     case DW_OP_reg1:
4429       return "DW_OP_reg1";
4430     case DW_OP_reg2:
4431       return "DW_OP_reg2";
4432     case DW_OP_reg3:
4433       return "DW_OP_reg3";
4434     case DW_OP_reg4:
4435       return "DW_OP_reg4";
4436     case DW_OP_reg5:
4437       return "DW_OP_reg5";
4438     case DW_OP_reg6:
4439       return "DW_OP_reg6";
4440     case DW_OP_reg7:
4441       return "DW_OP_reg7";
4442     case DW_OP_reg8:
4443       return "DW_OP_reg8";
4444     case DW_OP_reg9:
4445       return "DW_OP_reg9";
4446     case DW_OP_reg10:
4447       return "DW_OP_reg10";
4448     case DW_OP_reg11:
4449       return "DW_OP_reg11";
4450     case DW_OP_reg12:
4451       return "DW_OP_reg12";
4452     case DW_OP_reg13:
4453       return "DW_OP_reg13";
4454     case DW_OP_reg14:
4455       return "DW_OP_reg14";
4456     case DW_OP_reg15:
4457       return "DW_OP_reg15";
4458     case DW_OP_reg16:
4459       return "DW_OP_reg16";
4460     case DW_OP_reg17:
4461       return "DW_OP_reg17";
4462     case DW_OP_reg18:
4463       return "DW_OP_reg18";
4464     case DW_OP_reg19:
4465       return "DW_OP_reg19";
4466     case DW_OP_reg20:
4467       return "DW_OP_reg20";
4468     case DW_OP_reg21:
4469       return "DW_OP_reg21";
4470     case DW_OP_reg22:
4471       return "DW_OP_reg22";
4472     case DW_OP_reg23:
4473       return "DW_OP_reg23";
4474     case DW_OP_reg24:
4475       return "DW_OP_reg24";
4476     case DW_OP_reg25:
4477       return "DW_OP_reg25";
4478     case DW_OP_reg26:
4479       return "DW_OP_reg26";
4480     case DW_OP_reg27:
4481       return "DW_OP_reg27";
4482     case DW_OP_reg28:
4483       return "DW_OP_reg28";
4484     case DW_OP_reg29:
4485       return "DW_OP_reg29";
4486     case DW_OP_reg30:
4487       return "DW_OP_reg30";
4488     case DW_OP_reg31:
4489       return "DW_OP_reg31";
4490     case DW_OP_breg0:
4491       return "DW_OP_breg0";
4492     case DW_OP_breg1:
4493       return "DW_OP_breg1";
4494     case DW_OP_breg2:
4495       return "DW_OP_breg2";
4496     case DW_OP_breg3:
4497       return "DW_OP_breg3";
4498     case DW_OP_breg4:
4499       return "DW_OP_breg4";
4500     case DW_OP_breg5:
4501       return "DW_OP_breg5";
4502     case DW_OP_breg6:
4503       return "DW_OP_breg6";
4504     case DW_OP_breg7:
4505       return "DW_OP_breg7";
4506     case DW_OP_breg8:
4507       return "DW_OP_breg8";
4508     case DW_OP_breg9:
4509       return "DW_OP_breg9";
4510     case DW_OP_breg10:
4511       return "DW_OP_breg10";
4512     case DW_OP_breg11:
4513       return "DW_OP_breg11";
4514     case DW_OP_breg12:
4515       return "DW_OP_breg12";
4516     case DW_OP_breg13:
4517       return "DW_OP_breg13";
4518     case DW_OP_breg14:
4519       return "DW_OP_breg14";
4520     case DW_OP_breg15:
4521       return "DW_OP_breg15";
4522     case DW_OP_breg16:
4523       return "DW_OP_breg16";
4524     case DW_OP_breg17:
4525       return "DW_OP_breg17";
4526     case DW_OP_breg18:
4527       return "DW_OP_breg18";
4528     case DW_OP_breg19:
4529       return "DW_OP_breg19";
4530     case DW_OP_breg20:
4531       return "DW_OP_breg20";
4532     case DW_OP_breg21:
4533       return "DW_OP_breg21";
4534     case DW_OP_breg22:
4535       return "DW_OP_breg22";
4536     case DW_OP_breg23:
4537       return "DW_OP_breg23";
4538     case DW_OP_breg24:
4539       return "DW_OP_breg24";
4540     case DW_OP_breg25:
4541       return "DW_OP_breg25";
4542     case DW_OP_breg26:
4543       return "DW_OP_breg26";
4544     case DW_OP_breg27:
4545       return "DW_OP_breg27";
4546     case DW_OP_breg28:
4547       return "DW_OP_breg28";
4548     case DW_OP_breg29:
4549       return "DW_OP_breg29";
4550     case DW_OP_breg30:
4551       return "DW_OP_breg30";
4552     case DW_OP_breg31:
4553       return "DW_OP_breg31";
4554     case DW_OP_regx:
4555       return "DW_OP_regx";
4556     case DW_OP_fbreg:
4557       return "DW_OP_fbreg";
4558     case DW_OP_bregx:
4559       return "DW_OP_bregx";
4560     case DW_OP_piece:
4561       return "DW_OP_piece";
4562     case DW_OP_deref_size:
4563       return "DW_OP_deref_size";
4564     case DW_OP_xderef_size:
4565       return "DW_OP_xderef_size";
4566     case DW_OP_nop:
4567       return "DW_OP_nop";
4568
4569     case DW_OP_push_object_address:
4570       return "DW_OP_push_object_address";
4571     case DW_OP_call2:
4572       return "DW_OP_call2";
4573     case DW_OP_call4:
4574       return "DW_OP_call4";
4575     case DW_OP_call_ref:
4576       return "DW_OP_call_ref";
4577     case DW_OP_implicit_value:
4578       return "DW_OP_implicit_value";
4579     case DW_OP_stack_value:
4580       return "DW_OP_stack_value";
4581     case DW_OP_form_tls_address:
4582       return "DW_OP_form_tls_address";
4583     case DW_OP_call_frame_cfa:
4584       return "DW_OP_call_frame_cfa";
4585     case DW_OP_bit_piece:
4586       return "DW_OP_bit_piece";
4587
4588     case DW_OP_GNU_push_tls_address:
4589       return "DW_OP_GNU_push_tls_address";
4590     case DW_OP_GNU_uninit:
4591       return "DW_OP_GNU_uninit";
4592     case DW_OP_GNU_encoded_addr:
4593       return "DW_OP_GNU_encoded_addr";
4594
4595     default:
4596       return "OP_<unknown>";
4597     }
4598 }
4599
4600 /* Return a pointer to a newly allocated location description.  Location
4601    descriptions are simple expression terms that can be strung
4602    together to form more complicated location (address) descriptions.  */
4603
4604 static inline dw_loc_descr_ref
4605 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4606                unsigned HOST_WIDE_INT oprnd2)
4607 {
4608   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4609
4610   descr->dw_loc_opc = op;
4611   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4612   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4613   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4614   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4615
4616   return descr;
4617 }
4618
4619 /* Return a pointer to a newly allocated location description for
4620    REG and OFFSET.  */
4621
4622 static inline dw_loc_descr_ref
4623 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4624 {
4625   if (reg <= 31)
4626     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4627                           offset, 0);
4628   else
4629     return new_loc_descr (DW_OP_bregx, reg, offset);
4630 }
4631
4632 /* Add a location description term to a location description expression.  */
4633
4634 static inline void
4635 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4636 {
4637   dw_loc_descr_ref *d;
4638
4639   /* Find the end of the chain.  */
4640   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4641     ;
4642
4643   *d = descr;
4644 }
4645
4646 /* Add a constant OFFSET to a location expression.  */
4647
4648 static void
4649 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4650 {
4651   dw_loc_descr_ref loc;
4652   HOST_WIDE_INT *p;
4653
4654   gcc_assert (*list_head != NULL);
4655
4656   if (!offset)
4657     return;
4658
4659   /* Find the end of the chain.  */
4660   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4661     ;
4662
4663   p = NULL;
4664   if (loc->dw_loc_opc == DW_OP_fbreg
4665       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4666     p = &loc->dw_loc_oprnd1.v.val_int;
4667   else if (loc->dw_loc_opc == DW_OP_bregx)
4668     p = &loc->dw_loc_oprnd2.v.val_int;
4669
4670   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4671      offset.  Don't optimize if an signed integer overflow would happen.  */
4672   if (p != NULL
4673       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4674           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4675     *p += offset;
4676
4677   else if (offset > 0)
4678     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4679
4680   else
4681     {
4682       loc->dw_loc_next = int_loc_descriptor (offset);
4683       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4684     }
4685 }
4686
4687 #ifdef DWARF2_DEBUGGING_INFO
4688 /* Add a constant OFFSET to a location list.  */
4689
4690 static void
4691 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4692 {
4693   dw_loc_list_ref d;
4694   for (d = list_head; d != NULL; d = d->dw_loc_next)
4695     loc_descr_plus_const (&d->expr, offset);
4696 }
4697 #endif
4698
4699 /* Return the size of a location descriptor.  */
4700
4701 static unsigned long
4702 size_of_loc_descr (dw_loc_descr_ref loc)
4703 {
4704   unsigned long size = 1;
4705
4706   switch (loc->dw_loc_opc)
4707     {
4708     case DW_OP_addr:
4709       size += DWARF2_ADDR_SIZE;
4710       break;
4711     case DW_OP_const1u:
4712     case DW_OP_const1s:
4713       size += 1;
4714       break;
4715     case DW_OP_const2u:
4716     case DW_OP_const2s:
4717       size += 2;
4718       break;
4719     case DW_OP_const4u:
4720     case DW_OP_const4s:
4721       size += 4;
4722       break;
4723     case DW_OP_const8u:
4724     case DW_OP_const8s:
4725       size += 8;
4726       break;
4727     case DW_OP_constu:
4728       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4729       break;
4730     case DW_OP_consts:
4731       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4732       break;
4733     case DW_OP_pick:
4734       size += 1;
4735       break;
4736     case DW_OP_plus_uconst:
4737       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4738       break;
4739     case DW_OP_skip:
4740     case DW_OP_bra:
4741       size += 2;
4742       break;
4743     case DW_OP_breg0:
4744     case DW_OP_breg1:
4745     case DW_OP_breg2:
4746     case DW_OP_breg3:
4747     case DW_OP_breg4:
4748     case DW_OP_breg5:
4749     case DW_OP_breg6:
4750     case DW_OP_breg7:
4751     case DW_OP_breg8:
4752     case DW_OP_breg9:
4753     case DW_OP_breg10:
4754     case DW_OP_breg11:
4755     case DW_OP_breg12:
4756     case DW_OP_breg13:
4757     case DW_OP_breg14:
4758     case DW_OP_breg15:
4759     case DW_OP_breg16:
4760     case DW_OP_breg17:
4761     case DW_OP_breg18:
4762     case DW_OP_breg19:
4763     case DW_OP_breg20:
4764     case DW_OP_breg21:
4765     case DW_OP_breg22:
4766     case DW_OP_breg23:
4767     case DW_OP_breg24:
4768     case DW_OP_breg25:
4769     case DW_OP_breg26:
4770     case DW_OP_breg27:
4771     case DW_OP_breg28:
4772     case DW_OP_breg29:
4773     case DW_OP_breg30:
4774     case DW_OP_breg31:
4775       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4776       break;
4777     case DW_OP_regx:
4778       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4779       break;
4780     case DW_OP_fbreg:
4781       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4782       break;
4783     case DW_OP_bregx:
4784       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4785       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4786       break;
4787     case DW_OP_piece:
4788       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4789       break;
4790     case DW_OP_deref_size:
4791     case DW_OP_xderef_size:
4792       size += 1;
4793       break;
4794     case DW_OP_call2:
4795       size += 2;
4796       break;
4797     case DW_OP_call4:
4798       size += 4;
4799       break;
4800     case DW_OP_call_ref:
4801       size += DWARF2_ADDR_SIZE;
4802       break;
4803     case DW_OP_implicit_value:
4804       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4805               + loc->dw_loc_oprnd1.v.val_unsigned;
4806       break;
4807     default:
4808       break;
4809     }
4810
4811   return size;
4812 }
4813
4814 /* Return the size of a series of location descriptors.  */
4815
4816 static unsigned long
4817 size_of_locs (dw_loc_descr_ref loc)
4818 {
4819   dw_loc_descr_ref l;
4820   unsigned long size;
4821
4822   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4823      field, to avoid writing to a PCH file.  */
4824   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4825     {
4826       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4827         break;
4828       size += size_of_loc_descr (l);
4829     }
4830   if (! l)
4831     return size;
4832
4833   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4834     {
4835       l->dw_loc_addr = size;
4836       size += size_of_loc_descr (l);
4837     }
4838
4839   return size;
4840 }
4841
4842 #ifdef DWARF2_DEBUGGING_INFO
4843 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4844 #endif
4845
4846 /* Output location description stack opcode's operands (if any).  */
4847
4848 static void
4849 output_loc_operands (dw_loc_descr_ref loc)
4850 {
4851   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4852   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4853
4854   switch (loc->dw_loc_opc)
4855     {
4856 #ifdef DWARF2_DEBUGGING_INFO
4857     case DW_OP_const2u:
4858     case DW_OP_const2s:
4859       dw2_asm_output_data (2, val1->v.val_int, NULL);
4860       break;
4861     case DW_OP_const4u:
4862     case DW_OP_const4s:
4863       dw2_asm_output_data (4, val1->v.val_int, NULL);
4864       break;
4865     case DW_OP_const8u:
4866     case DW_OP_const8s:
4867       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4868       dw2_asm_output_data (8, val1->v.val_int, NULL);
4869       break;
4870     case DW_OP_skip:
4871     case DW_OP_bra:
4872       {
4873         int offset;
4874
4875         gcc_assert (val1->val_class == dw_val_class_loc);
4876         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4877
4878         dw2_asm_output_data (2, offset, NULL);
4879       }
4880       break;
4881     case DW_OP_implicit_value:
4882       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4883       switch (val2->val_class)
4884         {
4885         case dw_val_class_const:
4886           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4887           break;
4888         case dw_val_class_vec:
4889           {
4890             unsigned int elt_size = val2->v.val_vec.elt_size;
4891             unsigned int len = val2->v.val_vec.length;
4892             unsigned int i;
4893             unsigned char *p;
4894
4895             if (elt_size > sizeof (HOST_WIDE_INT))
4896               {
4897                 elt_size /= 2;
4898                 len *= 2;
4899               }
4900             for (i = 0, p = val2->v.val_vec.array;
4901                  i < len;
4902                  i++, p += elt_size)
4903               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4904                                    "fp or vector constant word %u", i);
4905           }
4906           break;
4907         case dw_val_class_const_double:
4908           {
4909             unsigned HOST_WIDE_INT first, second;
4910
4911             if (WORDS_BIG_ENDIAN)
4912               {
4913                 first = val2->v.val_double.high;
4914                 second = val2->v.val_double.low;
4915               }
4916             else
4917               {
4918                 first = val2->v.val_double.low;
4919                 second = val2->v.val_double.high;
4920               }
4921             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4922                                  first, NULL);
4923             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4924                                  second, NULL);
4925           }
4926           break;
4927         case dw_val_class_addr:
4928           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4929           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4930           break;
4931         default:
4932           gcc_unreachable ();
4933         }
4934       break;
4935 #else
4936     case DW_OP_const2u:
4937     case DW_OP_const2s:
4938     case DW_OP_const4u:
4939     case DW_OP_const4s:
4940     case DW_OP_const8u:
4941     case DW_OP_const8s:
4942     case DW_OP_skip:
4943     case DW_OP_bra:
4944     case DW_OP_implicit_value:
4945       /* We currently don't make any attempt to make sure these are
4946          aligned properly like we do for the main unwind info, so
4947          don't support emitting things larger than a byte if we're
4948          only doing unwinding.  */
4949       gcc_unreachable ();
4950 #endif
4951     case DW_OP_const1u:
4952     case DW_OP_const1s:
4953       dw2_asm_output_data (1, val1->v.val_int, NULL);
4954       break;
4955     case DW_OP_constu:
4956       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4957       break;
4958     case DW_OP_consts:
4959       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4960       break;
4961     case DW_OP_pick:
4962       dw2_asm_output_data (1, val1->v.val_int, NULL);
4963       break;
4964     case DW_OP_plus_uconst:
4965       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4966       break;
4967     case DW_OP_breg0:
4968     case DW_OP_breg1:
4969     case DW_OP_breg2:
4970     case DW_OP_breg3:
4971     case DW_OP_breg4:
4972     case DW_OP_breg5:
4973     case DW_OP_breg6:
4974     case DW_OP_breg7:
4975     case DW_OP_breg8:
4976     case DW_OP_breg9:
4977     case DW_OP_breg10:
4978     case DW_OP_breg11:
4979     case DW_OP_breg12:
4980     case DW_OP_breg13:
4981     case DW_OP_breg14:
4982     case DW_OP_breg15:
4983     case DW_OP_breg16:
4984     case DW_OP_breg17:
4985     case DW_OP_breg18:
4986     case DW_OP_breg19:
4987     case DW_OP_breg20:
4988     case DW_OP_breg21:
4989     case DW_OP_breg22:
4990     case DW_OP_breg23:
4991     case DW_OP_breg24:
4992     case DW_OP_breg25:
4993     case DW_OP_breg26:
4994     case DW_OP_breg27:
4995     case DW_OP_breg28:
4996     case DW_OP_breg29:
4997     case DW_OP_breg30:
4998     case DW_OP_breg31:
4999       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5000       break;
5001     case DW_OP_regx:
5002       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5003       break;
5004     case DW_OP_fbreg:
5005       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5006       break;
5007     case DW_OP_bregx:
5008       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5009       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5010       break;
5011     case DW_OP_piece:
5012       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5013       break;
5014     case DW_OP_deref_size:
5015     case DW_OP_xderef_size:
5016       dw2_asm_output_data (1, val1->v.val_int, NULL);
5017       break;
5018
5019     case DW_OP_addr:
5020       if (loc->dtprel)
5021         {
5022           if (targetm.asm_out.output_dwarf_dtprel)
5023             {
5024               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5025                                                    DWARF2_ADDR_SIZE,
5026                                                    val1->v.val_addr);
5027               fputc ('\n', asm_out_file);
5028             }
5029           else
5030             gcc_unreachable ();
5031         }
5032       else
5033         {
5034 #ifdef DWARF2_DEBUGGING_INFO
5035           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5036 #else
5037           gcc_unreachable ();
5038 #endif
5039         }
5040       break;
5041
5042     default:
5043       /* Other codes have no operands.  */
5044       break;
5045     }
5046 }
5047
5048 /* Output a sequence of location operations.  */
5049
5050 static void
5051 output_loc_sequence (dw_loc_descr_ref loc)
5052 {
5053   for (; loc != NULL; loc = loc->dw_loc_next)
5054     {
5055       /* Output the opcode.  */
5056       dw2_asm_output_data (1, loc->dw_loc_opc,
5057                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5058
5059       /* Output the operand(s) (if any).  */
5060       output_loc_operands (loc);
5061     }
5062 }
5063
5064 /* Output location description stack opcode's operands (if any).
5065    The output is single bytes on a line, suitable for .cfi_escape.  */
5066
5067 static void
5068 output_loc_operands_raw (dw_loc_descr_ref loc)
5069 {
5070   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5071   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5072
5073   switch (loc->dw_loc_opc)
5074     {
5075     case DW_OP_addr:
5076     case DW_OP_implicit_value:
5077       /* We cannot output addresses in .cfi_escape, only bytes.  */
5078       gcc_unreachable ();
5079
5080     case DW_OP_const1u:
5081     case DW_OP_const1s:
5082     case DW_OP_pick:
5083     case DW_OP_deref_size:
5084     case DW_OP_xderef_size:
5085       fputc (',', asm_out_file);
5086       dw2_asm_output_data_raw (1, val1->v.val_int);
5087       break;
5088
5089     case DW_OP_const2u:
5090     case DW_OP_const2s:
5091       fputc (',', asm_out_file);
5092       dw2_asm_output_data_raw (2, val1->v.val_int);
5093       break;
5094
5095     case DW_OP_const4u:
5096     case DW_OP_const4s:
5097       fputc (',', asm_out_file);
5098       dw2_asm_output_data_raw (4, val1->v.val_int);
5099       break;
5100
5101     case DW_OP_const8u:
5102     case DW_OP_const8s:
5103       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5104       fputc (',', asm_out_file);
5105       dw2_asm_output_data_raw (8, val1->v.val_int);
5106       break;
5107
5108     case DW_OP_skip:
5109     case DW_OP_bra:
5110       {
5111         int offset;
5112
5113         gcc_assert (val1->val_class == dw_val_class_loc);
5114         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5115
5116         fputc (',', asm_out_file);
5117         dw2_asm_output_data_raw (2, offset);
5118       }
5119       break;
5120
5121     case DW_OP_constu:
5122     case DW_OP_plus_uconst:
5123     case DW_OP_regx:
5124     case DW_OP_piece:
5125       fputc (',', asm_out_file);
5126       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5127       break;
5128
5129     case DW_OP_consts:
5130     case DW_OP_breg0:
5131     case DW_OP_breg1:
5132     case DW_OP_breg2:
5133     case DW_OP_breg3:
5134     case DW_OP_breg4:
5135     case DW_OP_breg5:
5136     case DW_OP_breg6:
5137     case DW_OP_breg7:
5138     case DW_OP_breg8:
5139     case DW_OP_breg9:
5140     case DW_OP_breg10:
5141     case DW_OP_breg11:
5142     case DW_OP_breg12:
5143     case DW_OP_breg13:
5144     case DW_OP_breg14:
5145     case DW_OP_breg15:
5146     case DW_OP_breg16:
5147     case DW_OP_breg17:
5148     case DW_OP_breg18:
5149     case DW_OP_breg19:
5150     case DW_OP_breg20:
5151     case DW_OP_breg21:
5152     case DW_OP_breg22:
5153     case DW_OP_breg23:
5154     case DW_OP_breg24:
5155     case DW_OP_breg25:
5156     case DW_OP_breg26:
5157     case DW_OP_breg27:
5158     case DW_OP_breg28:
5159     case DW_OP_breg29:
5160     case DW_OP_breg30:
5161     case DW_OP_breg31:
5162     case DW_OP_fbreg:
5163       fputc (',', asm_out_file);
5164       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5165       break;
5166
5167     case DW_OP_bregx:
5168       fputc (',', asm_out_file);
5169       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5170       fputc (',', asm_out_file);
5171       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5172       break;
5173
5174     default:
5175       /* Other codes have no operands.  */
5176       break;
5177     }
5178 }
5179
5180 static void
5181 output_loc_sequence_raw (dw_loc_descr_ref loc)
5182 {
5183   while (1)
5184     {
5185       /* Output the opcode.  */
5186       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5187       output_loc_operands_raw (loc);
5188
5189       if (!loc->dw_loc_next)
5190         break;
5191       loc = loc->dw_loc_next;
5192
5193       fputc (',', asm_out_file);
5194     }
5195 }
5196
5197 /* This routine will generate the correct assembly data for a location
5198    description based on a cfi entry with a complex address.  */
5199
5200 static void
5201 output_cfa_loc (dw_cfi_ref cfi)
5202 {
5203   dw_loc_descr_ref loc;
5204   unsigned long size;
5205
5206   if (cfi->dw_cfi_opc == DW_CFA_expression)
5207     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5208
5209   /* Output the size of the block.  */
5210   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5211   size = size_of_locs (loc);
5212   dw2_asm_output_data_uleb128 (size, NULL);
5213
5214   /* Now output the operations themselves.  */
5215   output_loc_sequence (loc);
5216 }
5217
5218 /* Similar, but used for .cfi_escape.  */
5219
5220 static void
5221 output_cfa_loc_raw (dw_cfi_ref cfi)
5222 {
5223   dw_loc_descr_ref loc;
5224   unsigned long size;
5225
5226   if (cfi->dw_cfi_opc == DW_CFA_expression)
5227     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5228
5229   /* Output the size of the block.  */
5230   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5231   size = size_of_locs (loc);
5232   dw2_asm_output_data_uleb128_raw (size);
5233   fputc (',', asm_out_file);
5234
5235   /* Now output the operations themselves.  */
5236   output_loc_sequence_raw (loc);
5237 }
5238
5239 /* This function builds a dwarf location descriptor sequence from a
5240    dw_cfa_location, adding the given OFFSET to the result of the
5241    expression.  */
5242
5243 static struct dw_loc_descr_struct *
5244 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5245 {
5246   struct dw_loc_descr_struct *head, *tmp;
5247
5248   offset += cfa->offset;
5249
5250   if (cfa->indirect)
5251     {
5252       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5253       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5254       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5255       add_loc_descr (&head, tmp);
5256       if (offset != 0)
5257         {
5258           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5259           add_loc_descr (&head, tmp);
5260         }
5261     }
5262   else
5263     head = new_reg_loc_descr (cfa->reg, offset);
5264
5265   return head;
5266 }
5267
5268 /* This function builds a dwarf location descriptor sequence for
5269    the address at OFFSET from the CFA when stack is aligned to
5270    ALIGNMENT byte.  */
5271
5272 static struct dw_loc_descr_struct *
5273 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5274 {
5275   struct dw_loc_descr_struct *head;
5276   unsigned int dwarf_fp
5277     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5278
5279  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5280   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5281     {
5282       head = new_reg_loc_descr (dwarf_fp, 0);
5283       add_loc_descr (&head, int_loc_descriptor (alignment));
5284       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5285       loc_descr_plus_const (&head, offset);
5286     }
5287   else
5288     head = new_reg_loc_descr (dwarf_fp, offset);
5289   return head;
5290 }
5291
5292 /* This function fills in aa dw_cfa_location structure from a dwarf location
5293    descriptor sequence.  */
5294
5295 static void
5296 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5297 {
5298   struct dw_loc_descr_struct *ptr;
5299   cfa->offset = 0;
5300   cfa->base_offset = 0;
5301   cfa->indirect = 0;
5302   cfa->reg = -1;
5303
5304   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5305     {
5306       enum dwarf_location_atom op = ptr->dw_loc_opc;
5307
5308       switch (op)
5309         {
5310         case DW_OP_reg0:
5311         case DW_OP_reg1:
5312         case DW_OP_reg2:
5313         case DW_OP_reg3:
5314         case DW_OP_reg4:
5315         case DW_OP_reg5:
5316         case DW_OP_reg6:
5317         case DW_OP_reg7:
5318         case DW_OP_reg8:
5319         case DW_OP_reg9:
5320         case DW_OP_reg10:
5321         case DW_OP_reg11:
5322         case DW_OP_reg12:
5323         case DW_OP_reg13:
5324         case DW_OP_reg14:
5325         case DW_OP_reg15:
5326         case DW_OP_reg16:
5327         case DW_OP_reg17:
5328         case DW_OP_reg18:
5329         case DW_OP_reg19:
5330         case DW_OP_reg20:
5331         case DW_OP_reg21:
5332         case DW_OP_reg22:
5333         case DW_OP_reg23:
5334         case DW_OP_reg24:
5335         case DW_OP_reg25:
5336         case DW_OP_reg26:
5337         case DW_OP_reg27:
5338         case DW_OP_reg28:
5339         case DW_OP_reg29:
5340         case DW_OP_reg30:
5341         case DW_OP_reg31:
5342           cfa->reg = op - DW_OP_reg0;
5343           break;
5344         case DW_OP_regx:
5345           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5346           break;
5347         case DW_OP_breg0:
5348         case DW_OP_breg1:
5349         case DW_OP_breg2:
5350         case DW_OP_breg3:
5351         case DW_OP_breg4:
5352         case DW_OP_breg5:
5353         case DW_OP_breg6:
5354         case DW_OP_breg7:
5355         case DW_OP_breg8:
5356         case DW_OP_breg9:
5357         case DW_OP_breg10:
5358         case DW_OP_breg11:
5359         case DW_OP_breg12:
5360         case DW_OP_breg13:
5361         case DW_OP_breg14:
5362         case DW_OP_breg15:
5363         case DW_OP_breg16:
5364         case DW_OP_breg17:
5365         case DW_OP_breg18:
5366         case DW_OP_breg19:
5367         case DW_OP_breg20:
5368         case DW_OP_breg21:
5369         case DW_OP_breg22:
5370         case DW_OP_breg23:
5371         case DW_OP_breg24:
5372         case DW_OP_breg25:
5373         case DW_OP_breg26:
5374         case DW_OP_breg27:
5375         case DW_OP_breg28:
5376         case DW_OP_breg29:
5377         case DW_OP_breg30:
5378         case DW_OP_breg31:
5379           cfa->reg = op - DW_OP_breg0;
5380           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5381           break;
5382         case DW_OP_bregx:
5383           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5384           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5385           break;
5386         case DW_OP_deref:
5387           cfa->indirect = 1;
5388           break;
5389         case DW_OP_plus_uconst:
5390           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5391           break;
5392         default:
5393           internal_error ("DW_LOC_OP %s not implemented",
5394                           dwarf_stack_op_name (ptr->dw_loc_opc));
5395         }
5396     }
5397 }
5398 #endif /* .debug_frame support */
5399 \f
5400 /* And now, the support for symbolic debugging information.  */
5401 #ifdef DWARF2_DEBUGGING_INFO
5402
5403 /* .debug_str support.  */
5404 static int output_indirect_string (void **, void *);
5405
5406 static void dwarf2out_init (const char *);
5407 static void dwarf2out_finish (const char *);
5408 static void dwarf2out_assembly_start (void);
5409 static void dwarf2out_define (unsigned int, const char *);
5410 static void dwarf2out_undef (unsigned int, const char *);
5411 static void dwarf2out_start_source_file (unsigned, const char *);
5412 static void dwarf2out_end_source_file (unsigned);
5413 static void dwarf2out_begin_block (unsigned, unsigned);
5414 static void dwarf2out_end_block (unsigned, unsigned);
5415 static bool dwarf2out_ignore_block (const_tree);
5416 static void dwarf2out_global_decl (tree);
5417 static void dwarf2out_type_decl (tree, int);
5418 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5419 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5420                                                  dw_die_ref);
5421 static void dwarf2out_abstract_function (tree);
5422 static void dwarf2out_var_location (rtx);
5423 static void dwarf2out_direct_call (tree);
5424 static void dwarf2out_virtual_call_token (tree, int);
5425 static void dwarf2out_copy_call_info (rtx, rtx);
5426 static void dwarf2out_virtual_call (int);
5427 static void dwarf2out_begin_function (tree);
5428 static void dwarf2out_set_name (tree, tree);
5429
5430 /* The debug hooks structure.  */
5431
5432 const struct gcc_debug_hooks dwarf2_debug_hooks =
5433 {
5434   dwarf2out_init,
5435   dwarf2out_finish,
5436   dwarf2out_assembly_start,
5437   dwarf2out_define,
5438   dwarf2out_undef,
5439   dwarf2out_start_source_file,
5440   dwarf2out_end_source_file,
5441   dwarf2out_begin_block,
5442   dwarf2out_end_block,
5443   dwarf2out_ignore_block,
5444   dwarf2out_source_line,
5445   dwarf2out_begin_prologue,
5446   debug_nothing_int_charstar,   /* end_prologue */
5447   dwarf2out_end_epilogue,
5448   dwarf2out_begin_function,
5449   debug_nothing_int,            /* end_function */
5450   dwarf2out_decl,               /* function_decl */
5451   dwarf2out_global_decl,
5452   dwarf2out_type_decl,          /* type_decl */
5453   dwarf2out_imported_module_or_decl,
5454   debug_nothing_tree,           /* deferred_inline_function */
5455   /* The DWARF 2 backend tries to reduce debugging bloat by not
5456      emitting the abstract description of inline functions until
5457      something tries to reference them.  */
5458   dwarf2out_abstract_function,  /* outlining_inline_function */
5459   debug_nothing_rtx,            /* label */
5460   debug_nothing_int,            /* handle_pch */
5461   dwarf2out_var_location,
5462   dwarf2out_switch_text_section,
5463   dwarf2out_direct_call,
5464   dwarf2out_virtual_call_token,
5465   dwarf2out_copy_call_info,
5466   dwarf2out_virtual_call,
5467   dwarf2out_set_name,
5468   1                             /* start_end_main_source_file */
5469 };
5470 #endif
5471 \f
5472 /* NOTE: In the comments in this file, many references are made to
5473    "Debugging Information Entries".  This term is abbreviated as `DIE'
5474    throughout the remainder of this file.  */
5475
5476 /* An internal representation of the DWARF output is built, and then
5477    walked to generate the DWARF debugging info.  The walk of the internal
5478    representation is done after the entire program has been compiled.
5479    The types below are used to describe the internal representation.  */
5480
5481 /* Various DIE's use offsets relative to the beginning of the
5482    .debug_info section to refer to each other.  */
5483
5484 typedef long int dw_offset;
5485
5486 /* Define typedefs here to avoid circular dependencies.  */
5487
5488 typedef struct dw_attr_struct *dw_attr_ref;
5489 typedef struct dw_line_info_struct *dw_line_info_ref;
5490 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5491 typedef struct pubname_struct *pubname_ref;
5492 typedef struct dw_ranges_struct *dw_ranges_ref;
5493 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5494 typedef struct comdat_type_struct *comdat_type_node_ref;
5495
5496 /* Each entry in the line_info_table maintains the file and
5497    line number associated with the label generated for that
5498    entry.  The label gives the PC value associated with
5499    the line number entry.  */
5500
5501 typedef struct GTY(()) dw_line_info_struct {
5502   unsigned long dw_file_num;
5503   unsigned long dw_line_num;
5504 }
5505 dw_line_info_entry;
5506
5507 /* Line information for functions in separate sections; each one gets its
5508    own sequence.  */
5509 typedef struct GTY(()) dw_separate_line_info_struct {
5510   unsigned long dw_file_num;
5511   unsigned long dw_line_num;
5512   unsigned long function;
5513 }
5514 dw_separate_line_info_entry;
5515
5516 /* Each DIE attribute has a field specifying the attribute kind,
5517    a link to the next attribute in the chain, and an attribute value.
5518    Attributes are typically linked below the DIE they modify.  */
5519
5520 typedef struct GTY(()) dw_attr_struct {
5521   enum dwarf_attribute dw_attr;
5522   dw_val_node dw_attr_val;
5523 }
5524 dw_attr_node;
5525
5526 DEF_VEC_O(dw_attr_node);
5527 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5528
5529 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5530    The children of each node form a circular list linked by
5531    die_sib.  die_child points to the node *before* the "first" child node.  */
5532
5533 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5534   enum dwarf_tag die_tag;
5535   union die_symbol_or_type_node
5536     {
5537       char * GTY ((tag ("0"))) die_symbol;
5538       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5539     }
5540   GTY ((desc ("dwarf_version >= 4"))) die_id;
5541   VEC(dw_attr_node,gc) * die_attr;
5542   dw_die_ref die_parent;
5543   dw_die_ref die_child;
5544   dw_die_ref die_sib;
5545   dw_die_ref die_definition; /* ref from a specification to its definition */
5546   dw_offset die_offset;
5547   unsigned long die_abbrev;
5548   int die_mark;
5549   /* Die is used and must not be pruned as unused.  */
5550   int die_perennial_p;
5551   unsigned int decl_id;
5552 }
5553 die_node;
5554
5555 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5556 #define FOR_EACH_CHILD(die, c, expr) do {       \
5557   c = die->die_child;                           \
5558   if (c) do {                                   \
5559     c = c->die_sib;                             \
5560     expr;                                       \
5561   } while (c != die->die_child);                \
5562 } while (0)
5563
5564 /* The pubname structure */
5565
5566 typedef struct GTY(()) pubname_struct {
5567   dw_die_ref die;
5568   const char *name;
5569 }
5570 pubname_entry;
5571
5572 DEF_VEC_O(pubname_entry);
5573 DEF_VEC_ALLOC_O(pubname_entry, gc);
5574
5575 struct GTY(()) dw_ranges_struct {
5576   /* If this is positive, it's a block number, otherwise it's a
5577      bitwise-negated index into dw_ranges_by_label.  */
5578   int num;
5579 };
5580
5581 struct GTY(()) dw_ranges_by_label_struct {
5582   const char *begin;
5583   const char *end;
5584 };
5585
5586 /* The comdat type node structure.  */
5587 typedef struct GTY(()) comdat_type_struct
5588 {
5589   dw_die_ref root_die;
5590   dw_die_ref type_die;
5591   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5592   struct comdat_type_struct *next;
5593 }
5594 comdat_type_node;
5595
5596 /* The limbo die list structure.  */
5597 typedef struct GTY(()) limbo_die_struct {
5598   dw_die_ref die;
5599   tree created_for;
5600   struct limbo_die_struct *next;
5601 }
5602 limbo_die_node;
5603
5604 typedef struct GTY(()) skeleton_chain_struct
5605 {
5606   dw_die_ref old_die;
5607   dw_die_ref new_die;
5608   struct skeleton_chain_struct *parent;
5609 }
5610 skeleton_chain_node;
5611
5612 /* How to start an assembler comment.  */
5613 #ifndef ASM_COMMENT_START
5614 #define ASM_COMMENT_START ";#"
5615 #endif
5616
5617 /* Define a macro which returns nonzero for a TYPE_DECL which was
5618    implicitly generated for a tagged type.
5619
5620    Note that unlike the gcc front end (which generates a NULL named
5621    TYPE_DECL node for each complete tagged type, each array type, and
5622    each function type node created) the g++ front end generates a
5623    _named_ TYPE_DECL node for each tagged type node created.
5624    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5625    generate a DW_TAG_typedef DIE for them.  */
5626
5627 #define TYPE_DECL_IS_STUB(decl)                         \
5628   (DECL_NAME (decl) == NULL_TREE                        \
5629    || (DECL_ARTIFICIAL (decl)                           \
5630        && is_tagged_type (TREE_TYPE (decl))             \
5631        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5632            /* This is necessary for stub decls that     \
5633               appear in nested inline functions.  */    \
5634            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5635                && (decl_ultimate_origin (decl)          \
5636                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5637
5638 /* Information concerning the compilation unit's programming
5639    language, and compiler version.  */
5640
5641 /* Fixed size portion of the DWARF compilation unit header.  */
5642 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5643   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5644
5645 /* Fixed size portion of the DWARF comdat type unit header.  */
5646 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5647   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5648    + DWARF_OFFSET_SIZE)
5649
5650 /* Fixed size portion of public names info.  */
5651 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5652
5653 /* Fixed size portion of the address range info.  */
5654 #define DWARF_ARANGES_HEADER_SIZE                                       \
5655   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5656                 DWARF2_ADDR_SIZE * 2)                                   \
5657    - DWARF_INITIAL_LENGTH_SIZE)
5658
5659 /* Size of padding portion in the address range info.  It must be
5660    aligned to twice the pointer size.  */
5661 #define DWARF_ARANGES_PAD_SIZE \
5662   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5663                 DWARF2_ADDR_SIZE * 2)                              \
5664    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5665
5666 /* Use assembler line directives if available.  */
5667 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5668 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5669 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5670 #else
5671 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5672 #endif
5673 #endif
5674
5675 /* Minimum line offset in a special line info. opcode.
5676    This value was chosen to give a reasonable range of values.  */
5677 #define DWARF_LINE_BASE  -10
5678
5679 /* First special line opcode - leave room for the standard opcodes.  */
5680 #define DWARF_LINE_OPCODE_BASE  10
5681
5682 /* Range of line offsets in a special line info. opcode.  */
5683 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5684
5685 /* Flag that indicates the initial value of the is_stmt_start flag.
5686    In the present implementation, we do not mark any lines as
5687    the beginning of a source statement, because that information
5688    is not made available by the GCC front-end.  */
5689 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5690
5691 #ifdef DWARF2_DEBUGGING_INFO
5692 /* This location is used by calc_die_sizes() to keep track
5693    the offset of each DIE within the .debug_info section.  */
5694 static unsigned long next_die_offset;
5695 #endif
5696
5697 /* Record the root of the DIE's built for the current compilation unit.  */
5698 static GTY(()) dw_die_ref comp_unit_die;
5699
5700 /* A list of type DIEs that have been separated into comdat sections.  */
5701 static GTY(()) comdat_type_node *comdat_type_list;
5702
5703 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5704 static GTY(()) limbo_die_node *limbo_die_list;
5705
5706 /* A list of DIEs for which we may have to generate
5707    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5708    set.  */
5709 static GTY(()) limbo_die_node *deferred_asm_name;
5710
5711 /* Filenames referenced by this compilation unit.  */
5712 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5713
5714 /* A hash table of references to DIE's that describe declarations.
5715    The key is a DECL_UID() which is a unique number identifying each decl.  */
5716 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5717
5718 /* A hash table of references to DIE's that describe COMMON blocks.
5719    The key is DECL_UID() ^ die_parent.  */
5720 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5721
5722 typedef struct GTY(()) die_arg_entry_struct {
5723     dw_die_ref die;
5724     tree arg;
5725 } die_arg_entry;
5726
5727 DEF_VEC_O(die_arg_entry);
5728 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5729
5730 /* Node of the variable location list.  */
5731 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5732   rtx GTY (()) var_loc_note;
5733   const char * GTY (()) label;
5734   const char * GTY (()) section_label;
5735   struct var_loc_node * GTY (()) next;
5736 };
5737
5738 /* Variable location list.  */
5739 struct GTY (()) var_loc_list_def {
5740   struct var_loc_node * GTY (()) first;
5741
5742   /* Do not mark the last element of the chained list because
5743      it is marked through the chain.  */
5744   struct var_loc_node * GTY ((skip ("%h"))) last;
5745
5746   /* DECL_UID of the variable decl.  */
5747   unsigned int decl_id;
5748 };
5749 typedef struct var_loc_list_def var_loc_list;
5750
5751
5752 /* Table of decl location linked lists.  */
5753 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5754
5755 /* A pointer to the base of a list of references to DIE's that
5756    are uniquely identified by their tag, presence/absence of
5757    children DIE's, and list of attribute/value pairs.  */
5758 static GTY((length ("abbrev_die_table_allocated")))
5759   dw_die_ref *abbrev_die_table;
5760
5761 /* Number of elements currently allocated for abbrev_die_table.  */
5762 static GTY(()) unsigned abbrev_die_table_allocated;
5763
5764 /* Number of elements in type_die_table currently in use.  */
5765 static GTY(()) unsigned abbrev_die_table_in_use;
5766
5767 /* Size (in elements) of increments by which we may expand the
5768    abbrev_die_table.  */
5769 #define ABBREV_DIE_TABLE_INCREMENT 256
5770
5771 /* A pointer to the base of a table that contains line information
5772    for each source code line in .text in the compilation unit.  */
5773 static GTY((length ("line_info_table_allocated")))
5774      dw_line_info_ref line_info_table;
5775
5776 /* Number of elements currently allocated for line_info_table.  */
5777 static GTY(()) unsigned line_info_table_allocated;
5778
5779 /* Number of elements in line_info_table currently in use.  */
5780 static GTY(()) unsigned line_info_table_in_use;
5781
5782 /* A pointer to the base of a table that contains line information
5783    for each source code line outside of .text in the compilation unit.  */
5784 static GTY ((length ("separate_line_info_table_allocated")))
5785      dw_separate_line_info_ref separate_line_info_table;
5786
5787 /* Number of elements currently allocated for separate_line_info_table.  */
5788 static GTY(()) unsigned separate_line_info_table_allocated;
5789
5790 /* Number of elements in separate_line_info_table currently in use.  */
5791 static GTY(()) unsigned separate_line_info_table_in_use;
5792
5793 /* Size (in elements) of increments by which we may expand the
5794    line_info_table.  */
5795 #define LINE_INFO_TABLE_INCREMENT 1024
5796
5797 /* A pointer to the base of a table that contains a list of publicly
5798    accessible names.  */
5799 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5800
5801 /* A pointer to the base of a table that contains a list of publicly
5802    accessible types.  */
5803 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5804
5805 /* Array of dies for which we should generate .debug_arange info.  */
5806 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5807
5808 /* Number of elements currently allocated for arange_table.  */
5809 static GTY(()) unsigned arange_table_allocated;
5810
5811 /* Number of elements in arange_table currently in use.  */
5812 static GTY(()) unsigned arange_table_in_use;
5813
5814 /* Size (in elements) of increments by which we may expand the
5815    arange_table.  */
5816 #define ARANGE_TABLE_INCREMENT 64
5817
5818 /* Array of dies for which we should generate .debug_ranges info.  */
5819 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5820
5821 /* Number of elements currently allocated for ranges_table.  */
5822 static GTY(()) unsigned ranges_table_allocated;
5823
5824 /* Number of elements in ranges_table currently in use.  */
5825 static GTY(()) unsigned ranges_table_in_use;
5826
5827 /* Array of pairs of labels referenced in ranges_table.  */
5828 static GTY ((length ("ranges_by_label_allocated")))
5829      dw_ranges_by_label_ref ranges_by_label;
5830
5831 /* Number of elements currently allocated for ranges_by_label.  */
5832 static GTY(()) unsigned ranges_by_label_allocated;
5833
5834 /* Number of elements in ranges_by_label currently in use.  */
5835 static GTY(()) unsigned ranges_by_label_in_use;
5836
5837 /* Size (in elements) of increments by which we may expand the
5838    ranges_table.  */
5839 #define RANGES_TABLE_INCREMENT 64
5840
5841 /* Whether we have location lists that need outputting */
5842 static GTY(()) bool have_location_lists;
5843
5844 /* Unique label counter.  */
5845 static GTY(()) unsigned int loclabel_num;
5846
5847 /* Unique label counter for point-of-call tables.  */
5848 static GTY(()) unsigned int poc_label_num;
5849
5850 /* The direct call table structure.  */
5851
5852 typedef struct GTY(()) dcall_struct {
5853   unsigned int poc_label_num;
5854   tree poc_decl;
5855   dw_die_ref targ_die;
5856 }
5857 dcall_entry;
5858
5859 DEF_VEC_O(dcall_entry);
5860 DEF_VEC_ALLOC_O(dcall_entry, gc);
5861
5862 /* The virtual call table structure.  */
5863
5864 typedef struct GTY(()) vcall_struct {
5865   unsigned int poc_label_num;
5866   unsigned int vtable_slot;
5867 }
5868 vcall_entry;
5869
5870 DEF_VEC_O(vcall_entry);
5871 DEF_VEC_ALLOC_O(vcall_entry, gc);
5872
5873 /* Pointers to the direct and virtual call tables.  */
5874 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
5875 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
5876
5877 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
5878
5879 struct GTY (()) vcall_insn {
5880   int insn_uid;
5881   unsigned int vtable_slot;
5882 };
5883
5884 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
5885
5886 #ifdef DWARF2_DEBUGGING_INFO
5887 /* Record whether the function being analyzed contains inlined functions.  */
5888 static int current_function_has_inlines;
5889 #endif
5890 #if 0 && defined (MIPS_DEBUGGING_INFO)
5891 static int comp_unit_has_inlines;
5892 #endif
5893
5894 /* The last file entry emitted by maybe_emit_file().  */
5895 static GTY(()) struct dwarf_file_data * last_emitted_file;
5896
5897 /* Number of internal labels generated by gen_internal_sym().  */
5898 static GTY(()) int label_num;
5899
5900 /* Cached result of previous call to lookup_filename.  */
5901 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5902
5903 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5904
5905 #ifdef DWARF2_DEBUGGING_INFO
5906
5907 /* Offset from the "steady-state frame pointer" to the frame base,
5908    within the current function.  */
5909 static HOST_WIDE_INT frame_pointer_fb_offset;
5910
5911 /* Forward declarations for functions defined in this file.  */
5912
5913 static int is_pseudo_reg (const_rtx);
5914 static tree type_main_variant (tree);
5915 static int is_tagged_type (const_tree);
5916 static const char *dwarf_tag_name (unsigned);
5917 static const char *dwarf_attr_name (unsigned);
5918 static const char *dwarf_form_name (unsigned);
5919 static tree decl_ultimate_origin (const_tree);
5920 static tree decl_class_context (tree);
5921 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5922 static inline enum dw_val_class AT_class (dw_attr_ref);
5923 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5924 static inline unsigned AT_flag (dw_attr_ref);
5925 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5926 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5927 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5928 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5929 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5930                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5931 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5932                                unsigned int, unsigned char *);
5933 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
5934 static hashval_t debug_str_do_hash (const void *);
5935 static int debug_str_eq (const void *, const void *);
5936 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5937 static inline const char *AT_string (dw_attr_ref);
5938 static enum dwarf_form AT_string_form (dw_attr_ref);
5939 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5940 static void add_AT_specification (dw_die_ref, dw_die_ref);
5941 static inline dw_die_ref AT_ref (dw_attr_ref);
5942 static inline int AT_ref_external (dw_attr_ref);
5943 static inline void set_AT_ref_external (dw_attr_ref, int);
5944 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5945 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5946 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5947 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5948                              dw_loc_list_ref);
5949 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5950 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5951 static inline rtx AT_addr (dw_attr_ref);
5952 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5953 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5954 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5955 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5956                            unsigned HOST_WIDE_INT);
5957 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5958                                unsigned long);
5959 static inline const char *AT_lbl (dw_attr_ref);
5960 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5961 static const char *get_AT_low_pc (dw_die_ref);
5962 static const char *get_AT_hi_pc (dw_die_ref);
5963 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5964 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5965 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5966 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5967 static bool is_c_family (void);
5968 static bool is_cxx (void);
5969 static bool is_java (void);
5970 static bool is_fortran (void);
5971 static bool is_ada (void);
5972 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5973 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5974 static void add_child_die (dw_die_ref, dw_die_ref);
5975 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5976 static dw_die_ref lookup_type_die (tree);
5977 static void equate_type_number_to_die (tree, dw_die_ref);
5978 static hashval_t decl_die_table_hash (const void *);
5979 static int decl_die_table_eq (const void *, const void *);
5980 static dw_die_ref lookup_decl_die (tree);
5981 static hashval_t common_block_die_table_hash (const void *);
5982 static int common_block_die_table_eq (const void *, const void *);
5983 static hashval_t decl_loc_table_hash (const void *);
5984 static int decl_loc_table_eq (const void *, const void *);
5985 static var_loc_list *lookup_decl_loc (const_tree);
5986 static void equate_decl_number_to_die (tree, dw_die_ref);
5987 static struct var_loc_node *add_var_loc_to_decl (tree, rtx);
5988 static void print_spaces (FILE *);
5989 static void print_die (dw_die_ref, FILE *);
5990 static void print_dwarf_line_table (FILE *);
5991 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5992 static dw_die_ref pop_compile_unit (dw_die_ref);
5993 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5994 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5995 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5996 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
5997 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
5998 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
5999 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6000                                    struct md5_ctx *, int *);
6001 struct checksum_attributes;
6002 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6003 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6004 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6005 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6006 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6007 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6008 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6009 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6010 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6011 static void compute_section_prefix (dw_die_ref);
6012 static int is_type_die (dw_die_ref);
6013 static int is_comdat_die (dw_die_ref);
6014 static int is_symbol_die (dw_die_ref);
6015 static void assign_symbol_names (dw_die_ref);
6016 static void break_out_includes (dw_die_ref);
6017 static int is_declaration_die (dw_die_ref);
6018 static int should_move_die_to_comdat (dw_die_ref);
6019 static dw_die_ref clone_as_declaration (dw_die_ref);
6020 static dw_die_ref clone_die (dw_die_ref);
6021 static dw_die_ref clone_tree (dw_die_ref);
6022 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6023 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6024 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6025 static dw_die_ref generate_skeleton (dw_die_ref);
6026 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6027                                                          dw_die_ref);
6028 static void break_out_comdat_types (dw_die_ref);
6029 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6030 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6031 static void copy_decls_for_unworthy_types (dw_die_ref);
6032
6033 static hashval_t htab_cu_hash (const void *);
6034 static int htab_cu_eq (const void *, const void *);
6035 static void htab_cu_del (void *);
6036 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6037 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6038 static void add_sibling_attributes (dw_die_ref);
6039 static void build_abbrev_table (dw_die_ref);
6040 static void output_location_lists (dw_die_ref);
6041 static int constant_size (unsigned HOST_WIDE_INT);
6042 static unsigned long size_of_die (dw_die_ref);
6043 static void calc_die_sizes (dw_die_ref);
6044 static void mark_dies (dw_die_ref);
6045 static void unmark_dies (dw_die_ref);
6046 static void unmark_all_dies (dw_die_ref);
6047 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6048 static unsigned long size_of_aranges (void);
6049 static enum dwarf_form value_format (dw_attr_ref);
6050 static void output_value_format (dw_attr_ref);
6051 static void output_abbrev_section (void);
6052 static void output_die_symbol (dw_die_ref);
6053 static void output_die (dw_die_ref);
6054 static void output_compilation_unit_header (void);
6055 static void output_comp_unit (dw_die_ref, int);
6056 static void output_comdat_type_unit (comdat_type_node *);
6057 static const char *dwarf2_name (tree, int);
6058 static void add_pubname (tree, dw_die_ref);
6059 static void add_pubname_string (const char *, dw_die_ref);
6060 static void add_pubtype (tree, dw_die_ref);
6061 static void output_pubnames (VEC (pubname_entry,gc) *);
6062 static void add_arange (tree, dw_die_ref);
6063 static void output_aranges (void);
6064 static unsigned int add_ranges_num (int);
6065 static unsigned int add_ranges (const_tree);
6066 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6067                                   bool *);
6068 static void output_ranges (void);
6069 static void output_line_info (void);
6070 static void output_file_names (void);
6071 static dw_die_ref base_type_die (tree);
6072 static int is_base_type (tree);
6073 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6074 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6075 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6076 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6077 static int type_is_enum (const_tree);
6078 static unsigned int dbx_reg_number (const_rtx);
6079 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6080 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6081 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6082                                                 enum var_init_status);
6083 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6084                                                      enum var_init_status);
6085 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6086                                          enum var_init_status);
6087 static int is_based_loc (const_rtx);
6088 static int resolve_one_addr (rtx *, void *);
6089 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6090                                             enum var_init_status);
6091 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6092                                                enum var_init_status);
6093 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6094                                         enum var_init_status);
6095 static dw_loc_list_ref loc_list_from_tree (tree, int);
6096 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6097 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6098 static tree field_type (const_tree);
6099 static unsigned int simple_type_align_in_bits (const_tree);
6100 static unsigned int simple_decl_align_in_bits (const_tree);
6101 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6102 static HOST_WIDE_INT field_byte_offset (const_tree);
6103 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6104                                          dw_loc_list_ref);
6105 static void add_data_member_location_attribute (dw_die_ref, tree);
6106 static bool add_const_value_attribute (dw_die_ref, rtx);
6107 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6108 static void insert_float (const_rtx, unsigned char *);
6109 static rtx rtl_for_decl_location (tree);
6110 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6111                                                    enum dwarf_attribute);
6112 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6113 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6114 static void add_name_attribute (dw_die_ref, const char *);
6115 static void add_comp_dir_attribute (dw_die_ref);
6116 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6117 static void add_subscript_info (dw_die_ref, tree, bool);
6118 static void add_byte_size_attribute (dw_die_ref, tree);
6119 static void add_bit_offset_attribute (dw_die_ref, tree);
6120 static void add_bit_size_attribute (dw_die_ref, tree);
6121 static void add_prototyped_attribute (dw_die_ref, tree);
6122 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6123 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6124 static void add_src_coords_attributes (dw_die_ref, tree);
6125 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6126 static void push_decl_scope (tree);
6127 static void pop_decl_scope (void);
6128 static dw_die_ref scope_die_for (tree, dw_die_ref);
6129 static inline int local_scope_p (dw_die_ref);
6130 static inline int class_scope_p (dw_die_ref);
6131 static inline int class_or_namespace_scope_p (dw_die_ref);
6132 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6133 static void add_calling_convention_attribute (dw_die_ref, tree);
6134 static const char *type_tag (const_tree);
6135 static tree member_declared_type (const_tree);
6136 #if 0
6137 static const char *decl_start_label (tree);
6138 #endif
6139 static void gen_array_type_die (tree, dw_die_ref);
6140 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6141 #if 0
6142 static void gen_entry_point_die (tree, dw_die_ref);
6143 #endif
6144 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6145 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6146 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6147 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6148 static void gen_formal_types_die (tree, dw_die_ref);
6149 static void gen_subprogram_die (tree, dw_die_ref);
6150 static void gen_variable_die (tree, tree, dw_die_ref);
6151 static void gen_const_die (tree, dw_die_ref);
6152 static void gen_label_die (tree, dw_die_ref);
6153 static void gen_lexical_block_die (tree, dw_die_ref, int);
6154 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6155 static void gen_field_die (tree, dw_die_ref);
6156 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6157 static dw_die_ref gen_compile_unit_die (const char *);
6158 static void gen_inheritance_die (tree, tree, dw_die_ref);
6159 static void gen_member_die (tree, dw_die_ref);
6160 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6161                                                 enum debug_info_usage);
6162 static void gen_subroutine_type_die (tree, dw_die_ref);
6163 static void gen_typedef_die (tree, dw_die_ref);
6164 static void gen_type_die (tree, dw_die_ref);
6165 static void gen_block_die (tree, dw_die_ref, int);
6166 static void decls_for_scope (tree, dw_die_ref, int);
6167 static int is_redundant_typedef (const_tree);
6168 static inline dw_die_ref get_context_die (tree);
6169 static void gen_namespace_die (tree, dw_die_ref);
6170 static void gen_decl_die (tree, tree, dw_die_ref);
6171 static dw_die_ref force_decl_die (tree);
6172 static dw_die_ref force_type_die (tree);
6173 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6174 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6175 static struct dwarf_file_data * lookup_filename (const char *);
6176 static void retry_incomplete_types (void);
6177 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6178 static void gen_generic_params_dies (tree);
6179 static void splice_child_die (dw_die_ref, dw_die_ref);
6180 static int file_info_cmp (const void *, const void *);
6181 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6182                                      const char *, const char *);
6183 static void output_loc_list (dw_loc_list_ref);
6184 static char *gen_internal_sym (const char *);
6185
6186 static void prune_unmark_dies (dw_die_ref);
6187 static void prune_unused_types_mark (dw_die_ref, int);
6188 static void prune_unused_types_walk (dw_die_ref);
6189 static void prune_unused_types_walk_attribs (dw_die_ref);
6190 static void prune_unused_types_prune (dw_die_ref);
6191 static void prune_unused_types (void);
6192 static int maybe_emit_file (struct dwarf_file_data *fd);
6193 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6194 static void gen_remaining_tmpl_value_param_die_attribute (void);
6195
6196 /* Section names used to hold DWARF debugging information.  */
6197 #ifndef DEBUG_INFO_SECTION
6198 #define DEBUG_INFO_SECTION      ".debug_info"
6199 #endif
6200 #ifndef DEBUG_ABBREV_SECTION
6201 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6202 #endif
6203 #ifndef DEBUG_ARANGES_SECTION
6204 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6205 #endif
6206 #ifndef DEBUG_MACINFO_SECTION
6207 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6208 #endif
6209 #ifndef DEBUG_LINE_SECTION
6210 #define DEBUG_LINE_SECTION      ".debug_line"
6211 #endif
6212 #ifndef DEBUG_LOC_SECTION
6213 #define DEBUG_LOC_SECTION       ".debug_loc"
6214 #endif
6215 #ifndef DEBUG_PUBNAMES_SECTION
6216 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6217 #endif
6218 #ifndef DEBUG_PUBTYPES_SECTION
6219 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6220 #endif
6221 #ifndef DEBUG_DCALL_SECTION
6222 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6223 #endif
6224 #ifndef DEBUG_VCALL_SECTION
6225 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6226 #endif
6227 #ifndef DEBUG_STR_SECTION
6228 #define DEBUG_STR_SECTION       ".debug_str"
6229 #endif
6230 #ifndef DEBUG_RANGES_SECTION
6231 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6232 #endif
6233
6234 /* Standard ELF section names for compiled code and data.  */
6235 #ifndef TEXT_SECTION_NAME
6236 #define TEXT_SECTION_NAME       ".text"
6237 #endif
6238
6239 /* Section flags for .debug_str section.  */
6240 #define DEBUG_STR_SECTION_FLAGS \
6241   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6242    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6243    : SECTION_DEBUG)
6244
6245 /* Labels we insert at beginning sections we can reference instead of
6246    the section names themselves.  */
6247
6248 #ifndef TEXT_SECTION_LABEL
6249 #define TEXT_SECTION_LABEL              "Ltext"
6250 #endif
6251 #ifndef COLD_TEXT_SECTION_LABEL
6252 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6253 #endif
6254 #ifndef DEBUG_LINE_SECTION_LABEL
6255 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6256 #endif
6257 #ifndef DEBUG_INFO_SECTION_LABEL
6258 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6259 #endif
6260 #ifndef DEBUG_ABBREV_SECTION_LABEL
6261 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6262 #endif
6263 #ifndef DEBUG_LOC_SECTION_LABEL
6264 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6265 #endif
6266 #ifndef DEBUG_RANGES_SECTION_LABEL
6267 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6268 #endif
6269 #ifndef DEBUG_MACINFO_SECTION_LABEL
6270 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6271 #endif
6272
6273 /* Definitions of defaults for formats and names of various special
6274    (artificial) labels which may be generated within this file (when the -g
6275    options is used and DWARF2_DEBUGGING_INFO is in effect.
6276    If necessary, these may be overridden from within the tm.h file, but
6277    typically, overriding these defaults is unnecessary.  */
6278
6279 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6280 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6281 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6282 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6283 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6284 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6285 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6286 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6287 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6288 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6289
6290 #ifndef TEXT_END_LABEL
6291 #define TEXT_END_LABEL          "Letext"
6292 #endif
6293 #ifndef COLD_END_LABEL
6294 #define COLD_END_LABEL          "Letext_cold"
6295 #endif
6296 #ifndef BLOCK_BEGIN_LABEL
6297 #define BLOCK_BEGIN_LABEL       "LBB"
6298 #endif
6299 #ifndef BLOCK_END_LABEL
6300 #define BLOCK_END_LABEL         "LBE"
6301 #endif
6302 #ifndef LINE_CODE_LABEL
6303 #define LINE_CODE_LABEL         "LM"
6304 #endif
6305 #ifndef SEPARATE_LINE_CODE_LABEL
6306 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6307 #endif
6308
6309 \f
6310 /* We allow a language front-end to designate a function that is to be
6311    called to "demangle" any name before it is put into a DIE.  */
6312
6313 static const char *(*demangle_name_func) (const char *);
6314
6315 void
6316 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6317 {
6318   demangle_name_func = func;
6319 }
6320
6321 /* Test if rtl node points to a pseudo register.  */
6322
6323 static inline int
6324 is_pseudo_reg (const_rtx rtl)
6325 {
6326   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6327           || (GET_CODE (rtl) == SUBREG
6328               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6329 }
6330
6331 /* Return a reference to a type, with its const and volatile qualifiers
6332    removed.  */
6333
6334 static inline tree
6335 type_main_variant (tree type)
6336 {
6337   type = TYPE_MAIN_VARIANT (type);
6338
6339   /* ??? There really should be only one main variant among any group of
6340      variants of a given type (and all of the MAIN_VARIANT values for all
6341      members of the group should point to that one type) but sometimes the C
6342      front-end messes this up for array types, so we work around that bug
6343      here.  */
6344   if (TREE_CODE (type) == ARRAY_TYPE)
6345     while (type != TYPE_MAIN_VARIANT (type))
6346       type = TYPE_MAIN_VARIANT (type);
6347
6348   return type;
6349 }
6350
6351 /* Return nonzero if the given type node represents a tagged type.  */
6352
6353 static inline int
6354 is_tagged_type (const_tree type)
6355 {
6356   enum tree_code code = TREE_CODE (type);
6357
6358   return (code == RECORD_TYPE || code == UNION_TYPE
6359           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6360 }
6361
6362 /* Convert a DIE tag into its string name.  */
6363
6364 static const char *
6365 dwarf_tag_name (unsigned int tag)
6366 {
6367   switch (tag)
6368     {
6369     case DW_TAG_padding:
6370       return "DW_TAG_padding";
6371     case DW_TAG_array_type:
6372       return "DW_TAG_array_type";
6373     case DW_TAG_class_type:
6374       return "DW_TAG_class_type";
6375     case DW_TAG_entry_point:
6376       return "DW_TAG_entry_point";
6377     case DW_TAG_enumeration_type:
6378       return "DW_TAG_enumeration_type";
6379     case DW_TAG_formal_parameter:
6380       return "DW_TAG_formal_parameter";
6381     case DW_TAG_imported_declaration:
6382       return "DW_TAG_imported_declaration";
6383     case DW_TAG_label:
6384       return "DW_TAG_label";
6385     case DW_TAG_lexical_block:
6386       return "DW_TAG_lexical_block";
6387     case DW_TAG_member:
6388       return "DW_TAG_member";
6389     case DW_TAG_pointer_type:
6390       return "DW_TAG_pointer_type";
6391     case DW_TAG_reference_type:
6392       return "DW_TAG_reference_type";
6393     case DW_TAG_compile_unit:
6394       return "DW_TAG_compile_unit";
6395     case DW_TAG_string_type:
6396       return "DW_TAG_string_type";
6397     case DW_TAG_structure_type:
6398       return "DW_TAG_structure_type";
6399     case DW_TAG_subroutine_type:
6400       return "DW_TAG_subroutine_type";
6401     case DW_TAG_typedef:
6402       return "DW_TAG_typedef";
6403     case DW_TAG_union_type:
6404       return "DW_TAG_union_type";
6405     case DW_TAG_unspecified_parameters:
6406       return "DW_TAG_unspecified_parameters";
6407     case DW_TAG_variant:
6408       return "DW_TAG_variant";
6409     case DW_TAG_common_block:
6410       return "DW_TAG_common_block";
6411     case DW_TAG_common_inclusion:
6412       return "DW_TAG_common_inclusion";
6413     case DW_TAG_inheritance:
6414       return "DW_TAG_inheritance";
6415     case DW_TAG_inlined_subroutine:
6416       return "DW_TAG_inlined_subroutine";
6417     case DW_TAG_module:
6418       return "DW_TAG_module";
6419     case DW_TAG_ptr_to_member_type:
6420       return "DW_TAG_ptr_to_member_type";
6421     case DW_TAG_set_type:
6422       return "DW_TAG_set_type";
6423     case DW_TAG_subrange_type:
6424       return "DW_TAG_subrange_type";
6425     case DW_TAG_with_stmt:
6426       return "DW_TAG_with_stmt";
6427     case DW_TAG_access_declaration:
6428       return "DW_TAG_access_declaration";
6429     case DW_TAG_base_type:
6430       return "DW_TAG_base_type";
6431     case DW_TAG_catch_block:
6432       return "DW_TAG_catch_block";
6433     case DW_TAG_const_type:
6434       return "DW_TAG_const_type";
6435     case DW_TAG_constant:
6436       return "DW_TAG_constant";
6437     case DW_TAG_enumerator:
6438       return "DW_TAG_enumerator";
6439     case DW_TAG_file_type:
6440       return "DW_TAG_file_type";
6441     case DW_TAG_friend:
6442       return "DW_TAG_friend";
6443     case DW_TAG_namelist:
6444       return "DW_TAG_namelist";
6445     case DW_TAG_namelist_item:
6446       return "DW_TAG_namelist_item";
6447     case DW_TAG_packed_type:
6448       return "DW_TAG_packed_type";
6449     case DW_TAG_subprogram:
6450       return "DW_TAG_subprogram";
6451     case DW_TAG_template_type_param:
6452       return "DW_TAG_template_type_param";
6453     case DW_TAG_template_value_param:
6454       return "DW_TAG_template_value_param";
6455     case DW_TAG_thrown_type:
6456       return "DW_TAG_thrown_type";
6457     case DW_TAG_try_block:
6458       return "DW_TAG_try_block";
6459     case DW_TAG_variant_part:
6460       return "DW_TAG_variant_part";
6461     case DW_TAG_variable:
6462       return "DW_TAG_variable";
6463     case DW_TAG_volatile_type:
6464       return "DW_TAG_volatile_type";
6465     case DW_TAG_dwarf_procedure:
6466       return "DW_TAG_dwarf_procedure";
6467     case DW_TAG_restrict_type:
6468       return "DW_TAG_restrict_type";
6469     case DW_TAG_interface_type:
6470       return "DW_TAG_interface_type";
6471     case DW_TAG_namespace:
6472       return "DW_TAG_namespace";
6473     case DW_TAG_imported_module:
6474       return "DW_TAG_imported_module";
6475     case DW_TAG_unspecified_type:
6476       return "DW_TAG_unspecified_type";
6477     case DW_TAG_partial_unit:
6478       return "DW_TAG_partial_unit";
6479     case DW_TAG_imported_unit:
6480       return "DW_TAG_imported_unit";
6481     case DW_TAG_condition:
6482       return "DW_TAG_condition";
6483     case DW_TAG_shared_type:
6484       return "DW_TAG_shared_type";
6485     case DW_TAG_type_unit:
6486       return "DW_TAG_type_unit";
6487     case DW_TAG_rvalue_reference_type:
6488       return "DW_TAG_rvalue_reference_type";
6489     case DW_TAG_template_alias:
6490       return "DW_TAG_template_alias";
6491     case DW_TAG_GNU_template_parameter_pack:
6492       return "DW_TAG_GNU_template_parameter_pack";
6493     case DW_TAG_GNU_formal_parameter_pack:
6494       return "DW_TAG_GNU_formal_parameter_pack";
6495     case DW_TAG_MIPS_loop:
6496       return "DW_TAG_MIPS_loop";
6497     case DW_TAG_format_label:
6498       return "DW_TAG_format_label";
6499     case DW_TAG_function_template:
6500       return "DW_TAG_function_template";
6501     case DW_TAG_class_template:
6502       return "DW_TAG_class_template";
6503     case DW_TAG_GNU_BINCL:
6504       return "DW_TAG_GNU_BINCL";
6505     case DW_TAG_GNU_EINCL:
6506       return "DW_TAG_GNU_EINCL";
6507     case DW_TAG_GNU_template_template_param:
6508       return "DW_TAG_GNU_template_template_param";
6509     default:
6510       return "DW_TAG_<unknown>";
6511     }
6512 }
6513
6514 /* Convert a DWARF attribute code into its string name.  */
6515
6516 static const char *
6517 dwarf_attr_name (unsigned int attr)
6518 {
6519   switch (attr)
6520     {
6521     case DW_AT_sibling:
6522       return "DW_AT_sibling";
6523     case DW_AT_location:
6524       return "DW_AT_location";
6525     case DW_AT_name:
6526       return "DW_AT_name";
6527     case DW_AT_ordering:
6528       return "DW_AT_ordering";
6529     case DW_AT_subscr_data:
6530       return "DW_AT_subscr_data";
6531     case DW_AT_byte_size:
6532       return "DW_AT_byte_size";
6533     case DW_AT_bit_offset:
6534       return "DW_AT_bit_offset";
6535     case DW_AT_bit_size:
6536       return "DW_AT_bit_size";
6537     case DW_AT_element_list:
6538       return "DW_AT_element_list";
6539     case DW_AT_stmt_list:
6540       return "DW_AT_stmt_list";
6541     case DW_AT_low_pc:
6542       return "DW_AT_low_pc";
6543     case DW_AT_high_pc:
6544       return "DW_AT_high_pc";
6545     case DW_AT_language:
6546       return "DW_AT_language";
6547     case DW_AT_member:
6548       return "DW_AT_member";
6549     case DW_AT_discr:
6550       return "DW_AT_discr";
6551     case DW_AT_discr_value:
6552       return "DW_AT_discr_value";
6553     case DW_AT_visibility:
6554       return "DW_AT_visibility";
6555     case DW_AT_import:
6556       return "DW_AT_import";
6557     case DW_AT_string_length:
6558       return "DW_AT_string_length";
6559     case DW_AT_common_reference:
6560       return "DW_AT_common_reference";
6561     case DW_AT_comp_dir:
6562       return "DW_AT_comp_dir";
6563     case DW_AT_const_value:
6564       return "DW_AT_const_value";
6565     case DW_AT_containing_type:
6566       return "DW_AT_containing_type";
6567     case DW_AT_default_value:
6568       return "DW_AT_default_value";
6569     case DW_AT_inline:
6570       return "DW_AT_inline";
6571     case DW_AT_is_optional:
6572       return "DW_AT_is_optional";
6573     case DW_AT_lower_bound:
6574       return "DW_AT_lower_bound";
6575     case DW_AT_producer:
6576       return "DW_AT_producer";
6577     case DW_AT_prototyped:
6578       return "DW_AT_prototyped";
6579     case DW_AT_return_addr:
6580       return "DW_AT_return_addr";
6581     case DW_AT_start_scope:
6582       return "DW_AT_start_scope";
6583     case DW_AT_bit_stride:
6584       return "DW_AT_bit_stride";
6585     case DW_AT_upper_bound:
6586       return "DW_AT_upper_bound";
6587     case DW_AT_abstract_origin:
6588       return "DW_AT_abstract_origin";
6589     case DW_AT_accessibility:
6590       return "DW_AT_accessibility";
6591     case DW_AT_address_class:
6592       return "DW_AT_address_class";
6593     case DW_AT_artificial:
6594       return "DW_AT_artificial";
6595     case DW_AT_base_types:
6596       return "DW_AT_base_types";
6597     case DW_AT_calling_convention:
6598       return "DW_AT_calling_convention";
6599     case DW_AT_count:
6600       return "DW_AT_count";
6601     case DW_AT_data_member_location:
6602       return "DW_AT_data_member_location";
6603     case DW_AT_decl_column:
6604       return "DW_AT_decl_column";
6605     case DW_AT_decl_file:
6606       return "DW_AT_decl_file";
6607     case DW_AT_decl_line:
6608       return "DW_AT_decl_line";
6609     case DW_AT_declaration:
6610       return "DW_AT_declaration";
6611     case DW_AT_discr_list:
6612       return "DW_AT_discr_list";
6613     case DW_AT_encoding:
6614       return "DW_AT_encoding";
6615     case DW_AT_external:
6616       return "DW_AT_external";
6617     case DW_AT_explicit:
6618       return "DW_AT_explicit";
6619     case DW_AT_frame_base:
6620       return "DW_AT_frame_base";
6621     case DW_AT_friend:
6622       return "DW_AT_friend";
6623     case DW_AT_identifier_case:
6624       return "DW_AT_identifier_case";
6625     case DW_AT_macro_info:
6626       return "DW_AT_macro_info";
6627     case DW_AT_namelist_items:
6628       return "DW_AT_namelist_items";
6629     case DW_AT_priority:
6630       return "DW_AT_priority";
6631     case DW_AT_segment:
6632       return "DW_AT_segment";
6633     case DW_AT_specification:
6634       return "DW_AT_specification";
6635     case DW_AT_static_link:
6636       return "DW_AT_static_link";
6637     case DW_AT_type:
6638       return "DW_AT_type";
6639     case DW_AT_use_location:
6640       return "DW_AT_use_location";
6641     case DW_AT_variable_parameter:
6642       return "DW_AT_variable_parameter";
6643     case DW_AT_virtuality:
6644       return "DW_AT_virtuality";
6645     case DW_AT_vtable_elem_location:
6646       return "DW_AT_vtable_elem_location";
6647
6648     case DW_AT_allocated:
6649       return "DW_AT_allocated";
6650     case DW_AT_associated:
6651       return "DW_AT_associated";
6652     case DW_AT_data_location:
6653       return "DW_AT_data_location";
6654     case DW_AT_byte_stride:
6655       return "DW_AT_byte_stride";
6656     case DW_AT_entry_pc:
6657       return "DW_AT_entry_pc";
6658     case DW_AT_use_UTF8:
6659       return "DW_AT_use_UTF8";
6660     case DW_AT_extension:
6661       return "DW_AT_extension";
6662     case DW_AT_ranges:
6663       return "DW_AT_ranges";
6664     case DW_AT_trampoline:
6665       return "DW_AT_trampoline";
6666     case DW_AT_call_column:
6667       return "DW_AT_call_column";
6668     case DW_AT_call_file:
6669       return "DW_AT_call_file";
6670     case DW_AT_call_line:
6671       return "DW_AT_call_line";
6672
6673     case DW_AT_signature:
6674       return "DW_AT_signature";
6675     case DW_AT_main_subprogram:
6676       return "DW_AT_main_subprogram";
6677     case DW_AT_data_bit_offset:
6678       return "DW_AT_data_bit_offset";
6679     case DW_AT_const_expr:
6680       return "DW_AT_const_expr";
6681     case DW_AT_enum_class:
6682       return "DW_AT_enum_class";
6683     case DW_AT_linkage_name:
6684       return "DW_AT_linkage_name";
6685
6686     case DW_AT_MIPS_fde:
6687       return "DW_AT_MIPS_fde";
6688     case DW_AT_MIPS_loop_begin:
6689       return "DW_AT_MIPS_loop_begin";
6690     case DW_AT_MIPS_tail_loop_begin:
6691       return "DW_AT_MIPS_tail_loop_begin";
6692     case DW_AT_MIPS_epilog_begin:
6693       return "DW_AT_MIPS_epilog_begin";
6694     case DW_AT_MIPS_loop_unroll_factor:
6695       return "DW_AT_MIPS_loop_unroll_factor";
6696     case DW_AT_MIPS_software_pipeline_depth:
6697       return "DW_AT_MIPS_software_pipeline_depth";
6698     case DW_AT_MIPS_linkage_name:
6699       return "DW_AT_MIPS_linkage_name";
6700     case DW_AT_MIPS_stride:
6701       return "DW_AT_MIPS_stride";
6702     case DW_AT_MIPS_abstract_name:
6703       return "DW_AT_MIPS_abstract_name";
6704     case DW_AT_MIPS_clone_origin:
6705       return "DW_AT_MIPS_clone_origin";
6706     case DW_AT_MIPS_has_inlines:
6707       return "DW_AT_MIPS_has_inlines";
6708
6709     case DW_AT_sf_names:
6710       return "DW_AT_sf_names";
6711     case DW_AT_src_info:
6712       return "DW_AT_src_info";
6713     case DW_AT_mac_info:
6714       return "DW_AT_mac_info";
6715     case DW_AT_src_coords:
6716       return "DW_AT_src_coords";
6717     case DW_AT_body_begin:
6718       return "DW_AT_body_begin";
6719     case DW_AT_body_end:
6720       return "DW_AT_body_end";
6721     case DW_AT_GNU_vector:
6722       return "DW_AT_GNU_vector";
6723     case DW_AT_GNU_guarded_by:
6724       return "DW_AT_GNU_guarded_by";
6725     case DW_AT_GNU_pt_guarded_by:
6726       return "DW_AT_GNU_pt_guarded_by";
6727     case DW_AT_GNU_guarded:
6728       return "DW_AT_GNU_guarded";
6729     case DW_AT_GNU_pt_guarded:
6730       return "DW_AT_GNU_pt_guarded";
6731     case DW_AT_GNU_locks_excluded:
6732       return "DW_AT_GNU_locks_excluded";
6733     case DW_AT_GNU_exclusive_locks_required:
6734       return "DW_AT_GNU_exclusive_locks_required";
6735     case DW_AT_GNU_shared_locks_required:
6736       return "DW_AT_GNU_shared_locks_required";
6737     case DW_AT_GNU_odr_signature:
6738       return "DW_AT_GNU_odr_signature";
6739     case DW_AT_GNU_template_name:
6740       return "DW_AT_GNU_template_name";
6741
6742     case DW_AT_VMS_rtnbeg_pd_address:
6743       return "DW_AT_VMS_rtnbeg_pd_address";
6744
6745     default:
6746       return "DW_AT_<unknown>";
6747     }
6748 }
6749
6750 /* Convert a DWARF value form code into its string name.  */
6751
6752 static const char *
6753 dwarf_form_name (unsigned int form)
6754 {
6755   switch (form)
6756     {
6757     case DW_FORM_addr:
6758       return "DW_FORM_addr";
6759     case DW_FORM_block2:
6760       return "DW_FORM_block2";
6761     case DW_FORM_block4:
6762       return "DW_FORM_block4";
6763     case DW_FORM_data2:
6764       return "DW_FORM_data2";
6765     case DW_FORM_data4:
6766       return "DW_FORM_data4";
6767     case DW_FORM_data8:
6768       return "DW_FORM_data8";
6769     case DW_FORM_string:
6770       return "DW_FORM_string";
6771     case DW_FORM_block:
6772       return "DW_FORM_block";
6773     case DW_FORM_block1:
6774       return "DW_FORM_block1";
6775     case DW_FORM_data1:
6776       return "DW_FORM_data1";
6777     case DW_FORM_flag:
6778       return "DW_FORM_flag";
6779     case DW_FORM_sdata:
6780       return "DW_FORM_sdata";
6781     case DW_FORM_strp:
6782       return "DW_FORM_strp";
6783     case DW_FORM_udata:
6784       return "DW_FORM_udata";
6785     case DW_FORM_ref_addr:
6786       return "DW_FORM_ref_addr";
6787     case DW_FORM_ref1:
6788       return "DW_FORM_ref1";
6789     case DW_FORM_ref2:
6790       return "DW_FORM_ref2";
6791     case DW_FORM_ref4:
6792       return "DW_FORM_ref4";
6793     case DW_FORM_ref8:
6794       return "DW_FORM_ref8";
6795     case DW_FORM_ref_udata:
6796       return "DW_FORM_ref_udata";
6797     case DW_FORM_indirect:
6798       return "DW_FORM_indirect";
6799     case DW_FORM_sec_offset:
6800       return "DW_FORM_sec_offset";
6801     case DW_FORM_exprloc:
6802       return "DW_FORM_exprloc";
6803     case DW_FORM_flag_present:
6804       return "DW_FORM_flag_present";
6805     case DW_FORM_ref_sig8:
6806       return "DW_FORM_ref_sig8";
6807     default:
6808       return "DW_FORM_<unknown>";
6809     }
6810 }
6811 \f
6812 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6813    instance of an inlined instance of a decl which is local to an inline
6814    function, so we have to trace all of the way back through the origin chain
6815    to find out what sort of node actually served as the original seed for the
6816    given block.  */
6817
6818 static tree
6819 decl_ultimate_origin (const_tree decl)
6820 {
6821   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6822     return NULL_TREE;
6823
6824   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6825      nodes in the function to point to themselves; ignore that if
6826      we're trying to output the abstract instance of this function.  */
6827   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6828     return NULL_TREE;
6829
6830   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6831      most distant ancestor, this should never happen.  */
6832   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6833
6834   return DECL_ABSTRACT_ORIGIN (decl);
6835 }
6836
6837 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6838    of a virtual function may refer to a base class, so we check the 'this'
6839    parameter.  */
6840
6841 static tree
6842 decl_class_context (tree decl)
6843 {
6844   tree context = NULL_TREE;
6845
6846   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6847     context = DECL_CONTEXT (decl);
6848   else
6849     context = TYPE_MAIN_VARIANT
6850       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6851
6852   if (context && !TYPE_P (context))
6853     context = NULL_TREE;
6854
6855   return context;
6856 }
6857 \f
6858 /* Add an attribute/value pair to a DIE.  */
6859
6860 static inline void
6861 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6862 {
6863   /* Maybe this should be an assert?  */
6864   if (die == NULL)
6865     return;
6866
6867   if (die->die_attr == NULL)
6868     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6869   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6870 }
6871
6872 static inline enum dw_val_class
6873 AT_class (dw_attr_ref a)
6874 {
6875   return a->dw_attr_val.val_class;
6876 }
6877
6878 /* Add a flag value attribute to a DIE.  */
6879
6880 static inline void
6881 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6882 {
6883   dw_attr_node attr;
6884
6885   attr.dw_attr = attr_kind;
6886   attr.dw_attr_val.val_class = dw_val_class_flag;
6887   attr.dw_attr_val.v.val_flag = flag;
6888   add_dwarf_attr (die, &attr);
6889 }
6890
6891 static inline unsigned
6892 AT_flag (dw_attr_ref a)
6893 {
6894   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6895   return a->dw_attr_val.v.val_flag;
6896 }
6897
6898 /* Add a signed integer attribute value to a DIE.  */
6899
6900 static inline void
6901 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6902 {
6903   dw_attr_node attr;
6904
6905   attr.dw_attr = attr_kind;
6906   attr.dw_attr_val.val_class = dw_val_class_const;
6907   attr.dw_attr_val.v.val_int = int_val;
6908   add_dwarf_attr (die, &attr);
6909 }
6910
6911 static inline HOST_WIDE_INT
6912 AT_int (dw_attr_ref a)
6913 {
6914   gcc_assert (a && AT_class (a) == dw_val_class_const);
6915   return a->dw_attr_val.v.val_int;
6916 }
6917
6918 /* Add an unsigned integer attribute value to a DIE.  */
6919
6920 static inline void
6921 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6922                  unsigned HOST_WIDE_INT unsigned_val)
6923 {
6924   dw_attr_node attr;
6925
6926   attr.dw_attr = attr_kind;
6927   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6928   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6929   add_dwarf_attr (die, &attr);
6930 }
6931
6932 static inline unsigned HOST_WIDE_INT
6933 AT_unsigned (dw_attr_ref a)
6934 {
6935   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6936   return a->dw_attr_val.v.val_unsigned;
6937 }
6938
6939 /* Add an unsigned double integer attribute value to a DIE.  */
6940
6941 static inline void
6942 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6943                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6944 {
6945   dw_attr_node attr;
6946
6947   attr.dw_attr = attr_kind;
6948   attr.dw_attr_val.val_class = dw_val_class_const_double;
6949   attr.dw_attr_val.v.val_double.high = high;
6950   attr.dw_attr_val.v.val_double.low = low;
6951   add_dwarf_attr (die, &attr);
6952 }
6953
6954 /* Add a floating point attribute value to a DIE and return it.  */
6955
6956 static inline void
6957 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6958             unsigned int length, unsigned int elt_size, unsigned char *array)
6959 {
6960   dw_attr_node attr;
6961
6962   attr.dw_attr = attr_kind;
6963   attr.dw_attr_val.val_class = dw_val_class_vec;
6964   attr.dw_attr_val.v.val_vec.length = length;
6965   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6966   attr.dw_attr_val.v.val_vec.array = array;
6967   add_dwarf_attr (die, &attr);
6968 }
6969
6970 /* Add an 8-byte data attribute value to a DIE.  */
6971
6972 static inline void
6973 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
6974               unsigned char data8[8])
6975 {
6976   dw_attr_node attr;
6977
6978   attr.dw_attr = attr_kind;
6979   attr.dw_attr_val.val_class = dw_val_class_data8;
6980   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
6981   add_dwarf_attr (die, &attr);
6982 }
6983
6984 /* Hash and equality functions for debug_str_hash.  */
6985
6986 static hashval_t
6987 debug_str_do_hash (const void *x)
6988 {
6989   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6990 }
6991
6992 static int
6993 debug_str_eq (const void *x1, const void *x2)
6994 {
6995   return strcmp ((((const struct indirect_string_node *)x1)->str),
6996                  (const char *)x2) == 0;
6997 }
6998
6999 /* Add STR to the indirect string hash table.  */
7000
7001 static struct indirect_string_node *
7002 find_AT_string (const char *str)
7003 {
7004   struct indirect_string_node *node;
7005   void **slot;
7006
7007   if (! debug_str_hash)
7008     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7009                                       debug_str_eq, NULL);
7010
7011   slot = htab_find_slot_with_hash (debug_str_hash, str,
7012                                    htab_hash_string (str), INSERT);
7013   if (*slot == NULL)
7014     {
7015       node = (struct indirect_string_node *)
7016                ggc_alloc_cleared (sizeof (struct indirect_string_node));
7017       node->str = ggc_strdup (str);
7018       *slot = node;
7019     }
7020   else
7021     node = (struct indirect_string_node *) *slot;
7022
7023   node->refcount++;
7024   return node;
7025 }
7026
7027 /* Add a string attribute value to a DIE.  */
7028
7029 static inline void
7030 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7031 {
7032   dw_attr_node attr;
7033   struct indirect_string_node *node;
7034
7035   node = find_AT_string (str);
7036
7037   attr.dw_attr = attr_kind;
7038   attr.dw_attr_val.val_class = dw_val_class_str;
7039   attr.dw_attr_val.v.val_str = node;
7040   add_dwarf_attr (die, &attr);
7041 }
7042
7043 /* Create a label for an indirect string node, ensuring it is going to
7044    be output, unless its reference count goes down to zero.  */
7045
7046 static inline void
7047 gen_label_for_indirect_string (struct indirect_string_node *node)
7048 {
7049   char label[32];
7050
7051   if (node->label)
7052     return;
7053
7054   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7055   ++dw2_string_counter;
7056   node->label = xstrdup (label);
7057 }
7058
7059 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7060    debug string STR.  */
7061
7062 static inline rtx
7063 get_debug_string_label (const char *str)
7064 {
7065   struct indirect_string_node *node = find_AT_string (str);
7066
7067   debug_str_hash_forced = true;
7068
7069   gen_label_for_indirect_string (node);
7070
7071   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7072 }
7073
7074 static inline const char *
7075 AT_string (dw_attr_ref a)
7076 {
7077   gcc_assert (a && AT_class (a) == dw_val_class_str);
7078   return a->dw_attr_val.v.val_str->str;
7079 }
7080
7081 /* Find out whether a string should be output inline in DIE
7082    or out-of-line in .debug_str section.  */
7083
7084 static enum dwarf_form
7085 AT_string_form (dw_attr_ref a)
7086 {
7087   struct indirect_string_node *node;
7088   unsigned int len;
7089
7090   gcc_assert (a && AT_class (a) == dw_val_class_str);
7091
7092   node = a->dw_attr_val.v.val_str;
7093   if (node->form)
7094     return node->form;
7095
7096   len = strlen (node->str) + 1;
7097
7098   /* If the string is shorter or equal to the size of the reference, it is
7099      always better to put it inline.  */
7100   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7101     return node->form = DW_FORM_string;
7102
7103   /* If we cannot expect the linker to merge strings in .debug_str
7104      section, only put it into .debug_str if it is worth even in this
7105      single module.  */
7106   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7107       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7108       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7109     return node->form = DW_FORM_string;
7110
7111   gen_label_for_indirect_string (node);
7112
7113   return node->form = DW_FORM_strp;
7114 }
7115
7116 /* Add a DIE reference attribute value to a DIE.  */
7117
7118 static inline void
7119 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7120 {
7121   dw_attr_node attr;
7122
7123   attr.dw_attr = attr_kind;
7124   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7125   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7126   attr.dw_attr_val.v.val_die_ref.external = 0;
7127   add_dwarf_attr (die, &attr);
7128 }
7129
7130 /* Add an AT_specification attribute to a DIE, and also make the back
7131    pointer from the specification to the definition.  */
7132
7133 static inline void
7134 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7135 {
7136   add_AT_die_ref (die, DW_AT_specification, targ_die);
7137   gcc_assert (!targ_die->die_definition);
7138   targ_die->die_definition = die;
7139 }
7140
7141 static inline dw_die_ref
7142 AT_ref (dw_attr_ref a)
7143 {
7144   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7145   return a->dw_attr_val.v.val_die_ref.die;
7146 }
7147
7148 static inline int
7149 AT_ref_external (dw_attr_ref a)
7150 {
7151   if (a && AT_class (a) == dw_val_class_die_ref)
7152     return a->dw_attr_val.v.val_die_ref.external;
7153
7154   return 0;
7155 }
7156
7157 static inline void
7158 set_AT_ref_external (dw_attr_ref a, int i)
7159 {
7160   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7161   a->dw_attr_val.v.val_die_ref.external = i;
7162 }
7163
7164 /* Add an FDE reference attribute value to a DIE.  */
7165
7166 static inline void
7167 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7168 {
7169   dw_attr_node attr;
7170
7171   attr.dw_attr = attr_kind;
7172   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7173   attr.dw_attr_val.v.val_fde_index = targ_fde;
7174   add_dwarf_attr (die, &attr);
7175 }
7176
7177 /* Add a location description attribute value to a DIE.  */
7178
7179 static inline void
7180 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7181 {
7182   dw_attr_node attr;
7183
7184   attr.dw_attr = attr_kind;
7185   attr.dw_attr_val.val_class = dw_val_class_loc;
7186   attr.dw_attr_val.v.val_loc = loc;
7187   add_dwarf_attr (die, &attr);
7188 }
7189
7190 static inline dw_loc_descr_ref
7191 AT_loc (dw_attr_ref a)
7192 {
7193   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7194   return a->dw_attr_val.v.val_loc;
7195 }
7196
7197 static inline void
7198 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7199 {
7200   dw_attr_node attr;
7201
7202   attr.dw_attr = attr_kind;
7203   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7204   attr.dw_attr_val.v.val_loc_list = loc_list;
7205   add_dwarf_attr (die, &attr);
7206   have_location_lists = true;
7207 }
7208
7209 static inline dw_loc_list_ref
7210 AT_loc_list (dw_attr_ref a)
7211 {
7212   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7213   return a->dw_attr_val.v.val_loc_list;
7214 }
7215
7216 static inline dw_loc_list_ref *
7217 AT_loc_list_ptr (dw_attr_ref a)
7218 {
7219   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7220   return &a->dw_attr_val.v.val_loc_list;
7221 }
7222
7223 /* Add an address constant attribute value to a DIE.  */
7224
7225 static inline void
7226 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7227 {
7228   dw_attr_node attr;
7229
7230   attr.dw_attr = attr_kind;
7231   attr.dw_attr_val.val_class = dw_val_class_addr;
7232   attr.dw_attr_val.v.val_addr = addr;
7233   add_dwarf_attr (die, &attr);
7234 }
7235
7236 /* Get the RTX from to an address DIE attribute.  */
7237
7238 static inline rtx
7239 AT_addr (dw_attr_ref a)
7240 {
7241   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7242   return a->dw_attr_val.v.val_addr;
7243 }
7244
7245 /* Add a file attribute value to a DIE.  */
7246
7247 static inline void
7248 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7249              struct dwarf_file_data *fd)
7250 {
7251   dw_attr_node attr;
7252
7253   attr.dw_attr = attr_kind;
7254   attr.dw_attr_val.val_class = dw_val_class_file;
7255   attr.dw_attr_val.v.val_file = fd;
7256   add_dwarf_attr (die, &attr);
7257 }
7258
7259 /* Get the dwarf_file_data from a file DIE attribute.  */
7260
7261 static inline struct dwarf_file_data *
7262 AT_file (dw_attr_ref a)
7263 {
7264   gcc_assert (a && AT_class (a) == dw_val_class_file);
7265   return a->dw_attr_val.v.val_file;
7266 }
7267
7268 /* Add a label identifier attribute value to a DIE.  */
7269
7270 static inline void
7271 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7272 {
7273   dw_attr_node attr;
7274
7275   attr.dw_attr = attr_kind;
7276   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7277   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7278   add_dwarf_attr (die, &attr);
7279 }
7280
7281 /* Add a section offset attribute value to a DIE, an offset into the
7282    debug_line section.  */
7283
7284 static inline void
7285 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7286                 const char *label)
7287 {
7288   dw_attr_node attr;
7289
7290   attr.dw_attr = attr_kind;
7291   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7292   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7293   add_dwarf_attr (die, &attr);
7294 }
7295
7296 /* Add a section offset attribute value to a DIE, an offset into the
7297    debug_macinfo section.  */
7298
7299 static inline void
7300 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7301                const char *label)
7302 {
7303   dw_attr_node attr;
7304
7305   attr.dw_attr = attr_kind;
7306   attr.dw_attr_val.val_class = dw_val_class_macptr;
7307   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7308   add_dwarf_attr (die, &attr);
7309 }
7310
7311 /* Add an offset attribute value to a DIE.  */
7312
7313 static inline void
7314 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7315                unsigned HOST_WIDE_INT offset)
7316 {
7317   dw_attr_node attr;
7318
7319   attr.dw_attr = attr_kind;
7320   attr.dw_attr_val.val_class = dw_val_class_offset;
7321   attr.dw_attr_val.v.val_offset = offset;
7322   add_dwarf_attr (die, &attr);
7323 }
7324
7325 /* Add an range_list attribute value to a DIE.  */
7326
7327 static void
7328 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7329                    long unsigned int offset)
7330 {
7331   dw_attr_node attr;
7332
7333   attr.dw_attr = attr_kind;
7334   attr.dw_attr_val.val_class = dw_val_class_range_list;
7335   attr.dw_attr_val.v.val_offset = offset;
7336   add_dwarf_attr (die, &attr);
7337 }
7338
7339 static inline const char *
7340 AT_lbl (dw_attr_ref a)
7341 {
7342   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7343                     || AT_class (a) == dw_val_class_lineptr
7344                     || AT_class (a) == dw_val_class_macptr));
7345   return a->dw_attr_val.v.val_lbl_id;
7346 }
7347
7348 /* Get the attribute of type attr_kind.  */
7349
7350 static dw_attr_ref
7351 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7352 {
7353   dw_attr_ref a;
7354   unsigned ix;
7355   dw_die_ref spec = NULL;
7356
7357   if (! die)
7358     return NULL;
7359
7360   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7361     if (a->dw_attr == attr_kind)
7362       return a;
7363     else if (a->dw_attr == DW_AT_specification
7364              || a->dw_attr == DW_AT_abstract_origin)
7365       spec = AT_ref (a);
7366
7367   if (spec)
7368     return get_AT (spec, attr_kind);
7369
7370   return NULL;
7371 }
7372
7373 /* Return the "low pc" attribute value, typically associated with a subprogram
7374    DIE.  Return null if the "low pc" attribute is either not present, or if it
7375    cannot be represented as an assembler label identifier.  */
7376
7377 static inline const char *
7378 get_AT_low_pc (dw_die_ref die)
7379 {
7380   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7381
7382   return a ? AT_lbl (a) : NULL;
7383 }
7384
7385 /* Return the "high pc" attribute value, typically associated with a subprogram
7386    DIE.  Return null if the "high pc" attribute is either not present, or if it
7387    cannot be represented as an assembler label identifier.  */
7388
7389 static inline const char *
7390 get_AT_hi_pc (dw_die_ref die)
7391 {
7392   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7393
7394   return a ? AT_lbl (a) : NULL;
7395 }
7396
7397 /* Return the value of the string attribute designated by ATTR_KIND, or
7398    NULL if it is not present.  */
7399
7400 static inline const char *
7401 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7402 {
7403   dw_attr_ref a = get_AT (die, attr_kind);
7404
7405   return a ? AT_string (a) : NULL;
7406 }
7407
7408 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7409    if it is not present.  */
7410
7411 static inline int
7412 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7413 {
7414   dw_attr_ref a = get_AT (die, attr_kind);
7415
7416   return a ? AT_flag (a) : 0;
7417 }
7418
7419 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7420    if it is not present.  */
7421
7422 static inline unsigned
7423 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7424 {
7425   dw_attr_ref a = get_AT (die, attr_kind);
7426
7427   return a ? AT_unsigned (a) : 0;
7428 }
7429
7430 static inline dw_die_ref
7431 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7432 {
7433   dw_attr_ref a = get_AT (die, attr_kind);
7434
7435   return a ? AT_ref (a) : NULL;
7436 }
7437
7438 static inline struct dwarf_file_data *
7439 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7440 {
7441   dw_attr_ref a = get_AT (die, attr_kind);
7442
7443   return a ? AT_file (a) : NULL;
7444 }
7445
7446 /* Return TRUE if the language is C or C++.  */
7447
7448 static inline bool
7449 is_c_family (void)
7450 {
7451   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7452
7453   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7454           || lang == DW_LANG_C99
7455           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7456 }
7457
7458 /* Return TRUE if the language is C++.  */
7459
7460 static inline bool
7461 is_cxx (void)
7462 {
7463   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7464
7465   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7466 }
7467
7468 /* Return TRUE if the language is Fortran.  */
7469
7470 static inline bool
7471 is_fortran (void)
7472 {
7473   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7474
7475   return (lang == DW_LANG_Fortran77
7476           || lang == DW_LANG_Fortran90
7477           || lang == DW_LANG_Fortran95);
7478 }
7479
7480 /* Return TRUE if the language is Java.  */
7481
7482 static inline bool
7483 is_java (void)
7484 {
7485   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7486
7487   return lang == DW_LANG_Java;
7488 }
7489
7490 /* Return TRUE if the language is Ada.  */
7491
7492 static inline bool
7493 is_ada (void)
7494 {
7495   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7496
7497   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7498 }
7499
7500 /* Remove the specified attribute if present.  */
7501
7502 static void
7503 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7504 {
7505   dw_attr_ref a;
7506   unsigned ix;
7507
7508   if (! die)
7509     return;
7510
7511   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7512     if (a->dw_attr == attr_kind)
7513       {
7514         if (AT_class (a) == dw_val_class_str)
7515           if (a->dw_attr_val.v.val_str->refcount)
7516             a->dw_attr_val.v.val_str->refcount--;
7517
7518         /* VEC_ordered_remove should help reduce the number of abbrevs
7519            that are needed.  */
7520         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7521         return;
7522       }
7523 }
7524
7525 /* Remove CHILD from its parent.  PREV must have the property that
7526    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7527
7528 static void
7529 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7530 {
7531   gcc_assert (child->die_parent == prev->die_parent);
7532   gcc_assert (prev->die_sib == child);
7533   if (prev == child)
7534     {
7535       gcc_assert (child->die_parent->die_child == child);
7536       prev = NULL;
7537     }
7538   else
7539     prev->die_sib = child->die_sib;
7540   if (child->die_parent->die_child == child)
7541     child->die_parent->die_child = prev;
7542 }
7543
7544 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7545    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7546
7547 static void
7548 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7549 {
7550   dw_die_ref parent = old_child->die_parent;
7551
7552   gcc_assert (parent == prev->die_parent);
7553   gcc_assert (prev->die_sib == old_child);
7554
7555   new_child->die_parent = parent;
7556   if (prev == old_child)
7557     {
7558       gcc_assert (parent->die_child == old_child);
7559       new_child->die_sib = new_child;
7560     }
7561   else
7562     {
7563       prev->die_sib = new_child;
7564       new_child->die_sib = old_child->die_sib;
7565     }
7566   if (old_child->die_parent->die_child == old_child)
7567     old_child->die_parent->die_child = new_child;
7568 }
7569
7570 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7571
7572 static void
7573 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7574 {
7575   dw_die_ref c;
7576   new_parent->die_child = old_parent->die_child;
7577   old_parent->die_child = NULL;
7578   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7579 }
7580
7581 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7582    matches TAG.  */
7583
7584 static void
7585 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7586 {
7587   dw_die_ref c;
7588
7589   c = die->die_child;
7590   if (c) do {
7591     dw_die_ref prev = c;
7592     c = c->die_sib;
7593     while (c->die_tag == tag)
7594       {
7595         remove_child_with_prev (c, prev);
7596         /* Might have removed every child.  */
7597         if (c == c->die_sib)
7598           return;
7599         c = c->die_sib;
7600       }
7601   } while (c != die->die_child);
7602 }
7603
7604 /* Add a CHILD_DIE as the last child of DIE.  */
7605
7606 static void
7607 add_child_die (dw_die_ref die, dw_die_ref child_die)
7608 {
7609   /* FIXME this should probably be an assert.  */
7610   if (! die || ! child_die)
7611     return;
7612   gcc_assert (die != child_die);
7613
7614   child_die->die_parent = die;
7615   if (die->die_child)
7616     {
7617       child_die->die_sib = die->die_child->die_sib;
7618       die->die_child->die_sib = child_die;
7619     }
7620   else
7621     child_die->die_sib = child_die;
7622   die->die_child = child_die;
7623 }
7624
7625 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7626    is the specification, to the end of PARENT's list of children.
7627    This is done by removing and re-adding it.  */
7628
7629 static void
7630 splice_child_die (dw_die_ref parent, dw_die_ref child)
7631 {
7632   dw_die_ref p;
7633
7634   /* We want the declaration DIE from inside the class, not the
7635      specification DIE at toplevel.  */
7636   if (child->die_parent != parent)
7637     {
7638       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7639
7640       if (tmp)
7641         child = tmp;
7642     }
7643
7644   gcc_assert (child->die_parent == parent
7645               || (child->die_parent
7646                   == get_AT_ref (parent, DW_AT_specification)));
7647
7648   for (p = child->die_parent->die_child; ; p = p->die_sib)
7649     if (p->die_sib == child)
7650       {
7651         remove_child_with_prev (child, p);
7652         break;
7653       }
7654
7655   add_child_die (parent, child);
7656 }
7657
7658 /* Return a pointer to a newly created DIE node.  */
7659
7660 static inline dw_die_ref
7661 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7662 {
7663   dw_die_ref die = GGC_CNEW (die_node);
7664
7665   die->die_tag = tag_value;
7666
7667   if (parent_die != NULL)
7668     add_child_die (parent_die, die);
7669   else
7670     {
7671       limbo_die_node *limbo_node;
7672
7673       limbo_node = GGC_CNEW (limbo_die_node);
7674       limbo_node->die = die;
7675       limbo_node->created_for = t;
7676       limbo_node->next = limbo_die_list;
7677       limbo_die_list = limbo_node;
7678     }
7679
7680   return die;
7681 }
7682
7683 /* Return the DIE associated with the given type specifier.  */
7684
7685 static inline dw_die_ref
7686 lookup_type_die (tree type)
7687 {
7688   return TYPE_SYMTAB_DIE (type);
7689 }
7690
7691 /* Equate a DIE to a given type specifier.  */
7692
7693 static inline void
7694 equate_type_number_to_die (tree type, dw_die_ref type_die)
7695 {
7696   TYPE_SYMTAB_DIE (type) = type_die;
7697 }
7698
7699 /* Returns a hash value for X (which really is a die_struct).  */
7700
7701 static hashval_t
7702 decl_die_table_hash (const void *x)
7703 {
7704   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7705 }
7706
7707 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7708
7709 static int
7710 decl_die_table_eq (const void *x, const void *y)
7711 {
7712   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7713 }
7714
7715 /* Return the DIE associated with a given declaration.  */
7716
7717 static inline dw_die_ref
7718 lookup_decl_die (tree decl)
7719 {
7720   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7721 }
7722
7723 /* Returns a hash value for X (which really is a var_loc_list).  */
7724
7725 static hashval_t
7726 decl_loc_table_hash (const void *x)
7727 {
7728   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7729 }
7730
7731 /* Return nonzero if decl_id of var_loc_list X is the same as
7732    UID of decl *Y.  */
7733
7734 static int
7735 decl_loc_table_eq (const void *x, const void *y)
7736 {
7737   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7738 }
7739
7740 /* Return the var_loc list associated with a given declaration.  */
7741
7742 static inline var_loc_list *
7743 lookup_decl_loc (const_tree decl)
7744 {
7745   if (!decl_loc_table)
7746     return NULL;
7747   return (var_loc_list *)
7748     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7749 }
7750
7751 /* Equate a DIE to a particular declaration.  */
7752
7753 static void
7754 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7755 {
7756   unsigned int decl_id = DECL_UID (decl);
7757   void **slot;
7758
7759   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7760   *slot = decl_die;
7761   decl_die->decl_id = decl_id;
7762 }
7763
7764 /* Add a variable location node to the linked list for DECL.  */
7765
7766 static struct var_loc_node *
7767 add_var_loc_to_decl (tree decl, rtx loc_note)
7768 {
7769   unsigned int decl_id = DECL_UID (decl);
7770   var_loc_list *temp;
7771   void **slot;
7772   struct var_loc_node *loc = NULL;
7773
7774   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7775   if (*slot == NULL)
7776     {
7777       temp = GGC_CNEW (var_loc_list);
7778       temp->decl_id = decl_id;
7779       *slot = temp;
7780     }
7781   else
7782     temp = (var_loc_list *) *slot;
7783
7784   if (temp->last)
7785     {
7786       /* If the current location is the same as the end of the list,
7787          and either both or neither of the locations is uninitialized,
7788          we have nothing to do.  */
7789       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7790                          NOTE_VAR_LOCATION_LOC (loc_note)))
7791           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7792                != NOTE_VAR_LOCATION_STATUS (loc_note))
7793               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7794                    == VAR_INIT_STATUS_UNINITIALIZED)
7795                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
7796                       == VAR_INIT_STATUS_UNINITIALIZED))))
7797         {
7798           /* Add LOC to the end of list and update LAST.  */
7799           loc = GGC_CNEW (struct var_loc_node);
7800           temp->last->next = loc;
7801           temp->last = loc;
7802         }
7803     }
7804   else
7805     {
7806       loc = GGC_CNEW (struct var_loc_node);
7807       temp->first = loc;
7808       temp->last = loc;
7809     }
7810   return loc;
7811 }
7812 \f
7813 /* Keep track of the number of spaces used to indent the
7814    output of the debugging routines that print the structure of
7815    the DIE internal representation.  */
7816 static int print_indent;
7817
7818 /* Indent the line the number of spaces given by print_indent.  */
7819
7820 static inline void
7821 print_spaces (FILE *outfile)
7822 {
7823   fprintf (outfile, "%*s", print_indent, "");
7824 }
7825
7826 /* Print a type signature in hex.  */
7827
7828 static inline void
7829 print_signature (FILE *outfile, char *sig)
7830 {
7831   int i;
7832
7833   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7834     fprintf (outfile, "%02x", sig[i] & 0xff);
7835 }
7836
7837 /* Print the information associated with a given DIE, and its children.
7838    This routine is a debugging aid only.  */
7839
7840 static void
7841 print_die (dw_die_ref die, FILE *outfile)
7842 {
7843   dw_attr_ref a;
7844   dw_die_ref c;
7845   unsigned ix;
7846
7847   print_spaces (outfile);
7848   fprintf (outfile, "DIE %4ld: %s\n",
7849            die->die_offset, dwarf_tag_name (die->die_tag));
7850   print_spaces (outfile);
7851   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7852   fprintf (outfile, " offset: %ld\n", die->die_offset);
7853   if (dwarf_version >= 4 && die->die_id.die_type_node)
7854     {
7855       print_spaces (outfile);
7856       fprintf (outfile, "  signature: ");
7857       print_signature (outfile, die->die_id.die_type_node->signature);
7858       fprintf (outfile, "\n");
7859     }
7860
7861   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7862     {
7863       print_spaces (outfile);
7864       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7865
7866       switch (AT_class (a))
7867         {
7868         case dw_val_class_addr:
7869           fprintf (outfile, "address");
7870           break;
7871         case dw_val_class_offset:
7872           fprintf (outfile, "offset");
7873           break;
7874         case dw_val_class_loc:
7875           fprintf (outfile, "location descriptor");
7876           break;
7877         case dw_val_class_loc_list:
7878           fprintf (outfile, "location list -> label:%s",
7879                    AT_loc_list (a)->ll_symbol);
7880           break;
7881         case dw_val_class_range_list:
7882           fprintf (outfile, "range list");
7883           break;
7884         case dw_val_class_const:
7885           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7886           break;
7887         case dw_val_class_unsigned_const:
7888           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7889           break;
7890         case dw_val_class_const_double:
7891           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7892                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7893                    a->dw_attr_val.v.val_double.high,
7894                    a->dw_attr_val.v.val_double.low);
7895           break;
7896         case dw_val_class_vec:
7897           fprintf (outfile, "floating-point or vector constant");
7898           break;
7899         case dw_val_class_flag:
7900           fprintf (outfile, "%u", AT_flag (a));
7901           break;
7902         case dw_val_class_die_ref:
7903           if (AT_ref (a) != NULL)
7904             {
7905               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
7906                 {
7907                   fprintf (outfile, "die -> signature: ");
7908                   print_signature (outfile,
7909                                    AT_ref (a)->die_id.die_type_node->signature);
7910                 }
7911               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
7912                 fprintf (outfile, "die -> label: %s",
7913                          AT_ref (a)->die_id.die_symbol);
7914               else
7915                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7916             }
7917           else
7918             fprintf (outfile, "die -> <null>");
7919           break;
7920         case dw_val_class_lbl_id:
7921         case dw_val_class_lineptr:
7922         case dw_val_class_macptr:
7923           fprintf (outfile, "label: %s", AT_lbl (a));
7924           break;
7925         case dw_val_class_str:
7926           if (AT_string (a) != NULL)
7927             fprintf (outfile, "\"%s\"", AT_string (a));
7928           else
7929             fprintf (outfile, "<null>");
7930           break;
7931         case dw_val_class_file:
7932           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7933                    AT_file (a)->emitted_number);
7934           break;
7935         case dw_val_class_data8:
7936           {
7937             int i;
7938
7939             for (i = 0; i < 8; i++)
7940               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
7941             break;
7942           }
7943         default:
7944           break;
7945         }
7946
7947       fprintf (outfile, "\n");
7948     }
7949
7950   if (die->die_child != NULL)
7951     {
7952       print_indent += 4;
7953       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7954       print_indent -= 4;
7955     }
7956   if (print_indent == 0)
7957     fprintf (outfile, "\n");
7958 }
7959
7960 /* Print the contents of the source code line number correspondence table.
7961    This routine is a debugging aid only.  */
7962
7963 static void
7964 print_dwarf_line_table (FILE *outfile)
7965 {
7966   unsigned i;
7967   dw_line_info_ref line_info;
7968
7969   fprintf (outfile, "\n\nDWARF source line information\n");
7970   for (i = 1; i < line_info_table_in_use; i++)
7971     {
7972       line_info = &line_info_table[i];
7973       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7974                line_info->dw_file_num,
7975                line_info->dw_line_num);
7976     }
7977
7978   fprintf (outfile, "\n\n");
7979 }
7980
7981 /* Print the information collected for a given DIE.  */
7982
7983 void
7984 debug_dwarf_die (dw_die_ref die)
7985 {
7986   print_die (die, stderr);
7987 }
7988
7989 /* Print all DWARF information collected for the compilation unit.
7990    This routine is a debugging aid only.  */
7991
7992 void
7993 debug_dwarf (void)
7994 {
7995   print_indent = 0;
7996   print_die (comp_unit_die, stderr);
7997   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7998     print_dwarf_line_table (stderr);
7999 }
8000 \f
8001 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8002    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8003    DIE that marks the start of the DIEs for this include file.  */
8004
8005 static dw_die_ref
8006 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8007 {
8008   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8009   dw_die_ref new_unit = gen_compile_unit_die (filename);
8010
8011   new_unit->die_sib = old_unit;
8012   return new_unit;
8013 }
8014
8015 /* Close an include-file CU and reopen the enclosing one.  */
8016
8017 static dw_die_ref
8018 pop_compile_unit (dw_die_ref old_unit)
8019 {
8020   dw_die_ref new_unit = old_unit->die_sib;
8021
8022   old_unit->die_sib = NULL;
8023   return new_unit;
8024 }
8025
8026 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8027 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8028
8029 /* Calculate the checksum of a location expression.  */
8030
8031 static inline void
8032 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8033 {
8034   int tem;
8035
8036   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8037   CHECKSUM (tem);
8038   CHECKSUM (loc->dw_loc_oprnd1);
8039   CHECKSUM (loc->dw_loc_oprnd2);
8040 }
8041
8042 /* Calculate the checksum of an attribute.  */
8043
8044 static void
8045 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8046 {
8047   dw_loc_descr_ref loc;
8048   rtx r;
8049
8050   CHECKSUM (at->dw_attr);
8051
8052   /* We don't care that this was compiled with a different compiler
8053      snapshot; if the output is the same, that's what matters.  */
8054   if (at->dw_attr == DW_AT_producer)
8055     return;
8056
8057   switch (AT_class (at))
8058     {
8059     case dw_val_class_const:
8060       CHECKSUM (at->dw_attr_val.v.val_int);
8061       break;
8062     case dw_val_class_unsigned_const:
8063       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8064       break;
8065     case dw_val_class_const_double:
8066       CHECKSUM (at->dw_attr_val.v.val_double);
8067       break;
8068     case dw_val_class_vec:
8069       CHECKSUM (at->dw_attr_val.v.val_vec);
8070       break;
8071     case dw_val_class_flag:
8072       CHECKSUM (at->dw_attr_val.v.val_flag);
8073       break;
8074     case dw_val_class_str:
8075       CHECKSUM_STRING (AT_string (at));
8076       break;
8077
8078     case dw_val_class_addr:
8079       r = AT_addr (at);
8080       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8081       CHECKSUM_STRING (XSTR (r, 0));
8082       break;
8083
8084     case dw_val_class_offset:
8085       CHECKSUM (at->dw_attr_val.v.val_offset);
8086       break;
8087
8088     case dw_val_class_loc:
8089       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8090         loc_checksum (loc, ctx);
8091       break;
8092
8093     case dw_val_class_die_ref:
8094       die_checksum (AT_ref (at), ctx, mark);
8095       break;
8096
8097     case dw_val_class_fde_ref:
8098     case dw_val_class_lbl_id:
8099     case dw_val_class_lineptr:
8100     case dw_val_class_macptr:
8101       break;
8102
8103     case dw_val_class_file:
8104       CHECKSUM_STRING (AT_file (at)->filename);
8105       break;
8106
8107     case dw_val_class_data8:
8108       CHECKSUM (at->dw_attr_val.v.val_data8);
8109       break;
8110
8111     default:
8112       break;
8113     }
8114 }
8115
8116 /* Calculate the checksum of a DIE.  */
8117
8118 static void
8119 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8120 {
8121   dw_die_ref c;
8122   dw_attr_ref a;
8123   unsigned ix;
8124
8125   /* To avoid infinite recursion.  */
8126   if (die->die_mark)
8127     {
8128       CHECKSUM (die->die_mark);
8129       return;
8130     }
8131   die->die_mark = ++(*mark);
8132
8133   CHECKSUM (die->die_tag);
8134
8135   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8136     attr_checksum (a, ctx, mark);
8137
8138   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8139 }
8140
8141 #undef CHECKSUM
8142 #undef CHECKSUM_STRING
8143
8144 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8145 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8146 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8147 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8148 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8149 #define CHECKSUM_ATTR(FOO) \
8150   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8151
8152 /* Calculate the checksum of a number in signed LEB128 format.  */
8153
8154 static void
8155 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8156 {
8157   unsigned char byte;
8158   bool more;
8159
8160   while (1)
8161     {
8162       byte = (value & 0x7f);
8163       value >>= 7;
8164       more = !((value == 0 && (byte & 0x40) == 0)
8165                 || (value == -1 && (byte & 0x40) != 0));
8166       if (more)
8167         byte |= 0x80;
8168       CHECKSUM (byte);
8169       if (!more)
8170         break;
8171     }
8172 }
8173
8174 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8175
8176 static void
8177 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8178 {
8179   while (1)
8180     {
8181       unsigned char byte = (value & 0x7f);
8182       value >>= 7;
8183       if (value != 0)
8184         /* More bytes to follow.  */
8185         byte |= 0x80;
8186       CHECKSUM (byte);
8187       if (value == 0)
8188         break;
8189     }
8190 }
8191
8192 /* Checksum the context of the DIE.  This adds the names of any
8193    surrounding namespaces or structures to the checksum.  */
8194
8195 static void
8196 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8197 {
8198   const char *name;
8199   dw_die_ref spec;
8200   int tag = die->die_tag;
8201
8202   if (tag != DW_TAG_namespace
8203       && tag != DW_TAG_structure_type
8204       && tag != DW_TAG_class_type)
8205     return;
8206
8207   name = get_AT_string (die, DW_AT_name);
8208
8209   spec = get_AT_ref (die, DW_AT_specification);
8210   if (spec != NULL)
8211     die = spec;
8212
8213   if (die->die_parent != NULL)
8214     checksum_die_context (die->die_parent, ctx);
8215
8216   CHECKSUM_ULEB128 ('C');
8217   CHECKSUM_ULEB128 (tag);
8218   if (name != NULL)
8219     CHECKSUM_STRING (name);
8220 }
8221
8222 /* Calculate the checksum of a location expression.  */
8223
8224 static inline void
8225 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8226 {
8227   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8228      were emitted as a DW_FORM_sdata instead of a location expression.  */
8229   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8230     {
8231       CHECKSUM_ULEB128 (DW_FORM_sdata);
8232       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8233       return;
8234     }
8235
8236   /* Otherwise, just checksum the raw location expression.  */
8237   while (loc != NULL)
8238     {
8239       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8240       CHECKSUM (loc->dw_loc_oprnd1);
8241       CHECKSUM (loc->dw_loc_oprnd2);
8242       loc = loc->dw_loc_next;
8243     }
8244 }
8245
8246 /* Calculate the checksum of an attribute.  */
8247
8248 static void
8249 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8250                        struct md5_ctx *ctx, int *mark)
8251 {
8252   dw_loc_descr_ref loc;
8253   rtx r;
8254
8255   if (AT_class (at) == dw_val_class_die_ref)
8256     {
8257       dw_die_ref target_die = AT_ref (at);
8258
8259       /* For pointer and reference types, we checksum only the (qualified)
8260          name of the target type (if there is a name).  For friend entries,
8261          we checksum only the (qualified) name of the target type or function.
8262          This allows the checksum to remain the same whether the target type
8263          is complete or not.  */
8264       if ((at->dw_attr == DW_AT_type
8265            && (tag == DW_TAG_pointer_type
8266                || tag == DW_TAG_reference_type
8267                || tag == DW_TAG_ptr_to_member_type))
8268           || (at->dw_attr == DW_AT_friend
8269               && tag == DW_TAG_friend))
8270         {
8271           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8272
8273           if (name_attr != NULL)
8274             {
8275               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8276
8277               if (decl == NULL)
8278                 decl = target_die;
8279               CHECKSUM_ULEB128 ('N');
8280               CHECKSUM_ULEB128 (at->dw_attr);
8281               if (decl->die_parent != NULL)
8282                 checksum_die_context (decl->die_parent, ctx);
8283               CHECKSUM_ULEB128 ('E');
8284               CHECKSUM_STRING (AT_string (name_attr));
8285               return;
8286             }
8287         }
8288
8289       /* For all other references to another DIE, we check to see if the
8290          target DIE has already been visited.  If it has, we emit a
8291          backward reference; if not, we descend recursively.  */
8292       if (target_die->die_mark > 0)
8293         {
8294           CHECKSUM_ULEB128 ('R');
8295           CHECKSUM_ULEB128 (at->dw_attr);
8296           CHECKSUM_ULEB128 (target_die->die_mark);
8297         }
8298       else
8299         {
8300           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8301
8302           if (decl == NULL)
8303             decl = target_die;
8304           target_die->die_mark = ++(*mark);
8305           CHECKSUM_ULEB128 ('T');
8306           CHECKSUM_ULEB128 (at->dw_attr);
8307           if (decl->die_parent != NULL)
8308             checksum_die_context (decl->die_parent, ctx);
8309           die_checksum_ordered (target_die, ctx, mark);
8310         }
8311       return;
8312     }
8313
8314   CHECKSUM_ULEB128 ('A');
8315   CHECKSUM_ULEB128 (at->dw_attr);
8316
8317   switch (AT_class (at))
8318     {
8319     case dw_val_class_const:
8320       CHECKSUM_ULEB128 (DW_FORM_sdata);
8321       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8322       break;
8323
8324     case dw_val_class_unsigned_const:
8325       CHECKSUM_ULEB128 (DW_FORM_sdata);
8326       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8327       break;
8328
8329     case dw_val_class_const_double:
8330       CHECKSUM_ULEB128 (DW_FORM_block);
8331       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8332       CHECKSUM (at->dw_attr_val.v.val_double);
8333       break;
8334
8335     case dw_val_class_vec:
8336       CHECKSUM_ULEB128 (DW_FORM_block);
8337       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8338       CHECKSUM (at->dw_attr_val.v.val_vec);
8339       break;
8340
8341     case dw_val_class_flag:
8342       CHECKSUM_ULEB128 (DW_FORM_flag);
8343       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8344       break;
8345
8346     case dw_val_class_str:
8347       CHECKSUM_ULEB128 (DW_FORM_string);
8348       CHECKSUM_STRING (AT_string (at));
8349       break;
8350
8351     case dw_val_class_addr:
8352       r = AT_addr (at);
8353       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8354       CHECKSUM_ULEB128 (DW_FORM_string);
8355       CHECKSUM_STRING (XSTR (r, 0));
8356       break;
8357
8358     case dw_val_class_offset:
8359       CHECKSUM_ULEB128 (DW_FORM_sdata);
8360       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8361       break;
8362
8363     case dw_val_class_loc:
8364       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8365         loc_checksum_ordered (loc, ctx);
8366       break;
8367
8368     case dw_val_class_fde_ref:
8369     case dw_val_class_lbl_id:
8370     case dw_val_class_lineptr:
8371     case dw_val_class_macptr:
8372       break;
8373
8374     case dw_val_class_file:
8375       CHECKSUM_ULEB128 (DW_FORM_string);
8376       CHECKSUM_STRING (AT_file (at)->filename);
8377       break;
8378
8379     case dw_val_class_data8:
8380       CHECKSUM (at->dw_attr_val.v.val_data8);
8381       break;
8382
8383     default:
8384       break;
8385     }
8386 }
8387
8388 struct checksum_attributes
8389 {
8390   dw_attr_ref at_name;
8391   dw_attr_ref at_type;
8392   dw_attr_ref at_friend;
8393   dw_attr_ref at_accessibility;
8394   dw_attr_ref at_address_class;
8395   dw_attr_ref at_allocated;
8396   dw_attr_ref at_artificial;
8397   dw_attr_ref at_associated;
8398   dw_attr_ref at_binary_scale;
8399   dw_attr_ref at_bit_offset;
8400   dw_attr_ref at_bit_size;
8401   dw_attr_ref at_bit_stride;
8402   dw_attr_ref at_byte_size;
8403   dw_attr_ref at_byte_stride;
8404   dw_attr_ref at_const_value;
8405   dw_attr_ref at_containing_type;
8406   dw_attr_ref at_count;
8407   dw_attr_ref at_data_location;
8408   dw_attr_ref at_data_member_location;
8409   dw_attr_ref at_decimal_scale;
8410   dw_attr_ref at_decimal_sign;
8411   dw_attr_ref at_default_value;
8412   dw_attr_ref at_digit_count;
8413   dw_attr_ref at_discr;
8414   dw_attr_ref at_discr_list;
8415   dw_attr_ref at_discr_value;
8416   dw_attr_ref at_encoding;
8417   dw_attr_ref at_endianity;
8418   dw_attr_ref at_explicit;
8419   dw_attr_ref at_is_optional;
8420   dw_attr_ref at_location;
8421   dw_attr_ref at_lower_bound;
8422   dw_attr_ref at_mutable;
8423   dw_attr_ref at_ordering;
8424   dw_attr_ref at_picture_string;
8425   dw_attr_ref at_prototyped;
8426   dw_attr_ref at_small;
8427   dw_attr_ref at_segment;
8428   dw_attr_ref at_string_length;
8429   dw_attr_ref at_threads_scaled;
8430   dw_attr_ref at_upper_bound;
8431   dw_attr_ref at_use_location;
8432   dw_attr_ref at_use_UTF8;
8433   dw_attr_ref at_variable_parameter;
8434   dw_attr_ref at_virtuality;
8435   dw_attr_ref at_visibility;
8436   dw_attr_ref at_vtable_elem_location;
8437 };
8438
8439 /* Collect the attributes that we will want to use for the checksum.  */
8440
8441 static void
8442 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8443 {
8444   dw_attr_ref a;
8445   unsigned ix;
8446
8447   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8448     {
8449       switch (a->dw_attr)
8450         {
8451         case DW_AT_name:
8452           attrs->at_name = a;
8453           break;
8454         case DW_AT_type:
8455           attrs->at_type = a;
8456           break;
8457         case DW_AT_friend:
8458           attrs->at_friend = a;
8459           break;
8460         case DW_AT_accessibility:
8461           attrs->at_accessibility = a;
8462           break;
8463         case DW_AT_address_class:
8464           attrs->at_address_class = a;
8465           break;
8466         case DW_AT_allocated:
8467           attrs->at_allocated = a;
8468           break;
8469         case DW_AT_artificial:
8470           attrs->at_artificial = a;
8471           break;
8472         case DW_AT_associated:
8473           attrs->at_associated = a;
8474           break;
8475         case DW_AT_binary_scale:
8476           attrs->at_binary_scale = a;
8477           break;
8478         case DW_AT_bit_offset:
8479           attrs->at_bit_offset = a;
8480           break;
8481         case DW_AT_bit_size:
8482           attrs->at_bit_size = a;
8483           break;
8484         case DW_AT_bit_stride:
8485           attrs->at_bit_stride = a;
8486           break;
8487         case DW_AT_byte_size:
8488           attrs->at_byte_size = a;
8489           break;
8490         case DW_AT_byte_stride:
8491           attrs->at_byte_stride = a;
8492           break;
8493         case DW_AT_const_value:
8494           attrs->at_const_value = a;
8495           break;
8496         case DW_AT_containing_type:
8497           attrs->at_containing_type = a;
8498           break;
8499         case DW_AT_count:
8500           attrs->at_count = a;
8501           break;
8502         case DW_AT_data_location:
8503           attrs->at_data_location = a;
8504           break;
8505         case DW_AT_data_member_location:
8506           attrs->at_data_member_location = a;
8507           break;
8508         case DW_AT_decimal_scale:
8509           attrs->at_decimal_scale = a;
8510           break;
8511         case DW_AT_decimal_sign:
8512           attrs->at_decimal_sign = a;
8513           break;
8514         case DW_AT_default_value:
8515           attrs->at_default_value = a;
8516           break;
8517         case DW_AT_digit_count:
8518           attrs->at_digit_count = a;
8519           break;
8520         case DW_AT_discr:
8521           attrs->at_discr = a;
8522           break;
8523         case DW_AT_discr_list:
8524           attrs->at_discr_list = a;
8525           break;
8526         case DW_AT_discr_value:
8527           attrs->at_discr_value = a;
8528           break;
8529         case DW_AT_encoding:
8530           attrs->at_encoding = a;
8531           break;
8532         case DW_AT_endianity:
8533           attrs->at_endianity = a;
8534           break;
8535         case DW_AT_explicit:
8536           attrs->at_explicit = a;
8537           break;
8538         case DW_AT_is_optional:
8539           attrs->at_is_optional = a;
8540           break;
8541         case DW_AT_location:
8542           attrs->at_location = a;
8543           break;
8544         case DW_AT_lower_bound:
8545           attrs->at_lower_bound = a;
8546           break;
8547         case DW_AT_mutable:
8548           attrs->at_mutable = a;
8549           break;
8550         case DW_AT_ordering:
8551           attrs->at_ordering = a;
8552           break;
8553         case DW_AT_picture_string:
8554           attrs->at_picture_string = a;
8555           break;
8556         case DW_AT_prototyped:
8557           attrs->at_prototyped = a;
8558           break;
8559         case DW_AT_small:
8560           attrs->at_small = a;
8561           break;
8562         case DW_AT_segment:
8563           attrs->at_segment = a;
8564           break;
8565         case DW_AT_string_length:
8566           attrs->at_string_length = a;
8567           break;
8568         case DW_AT_threads_scaled:
8569           attrs->at_threads_scaled = a;
8570           break;
8571         case DW_AT_upper_bound:
8572           attrs->at_upper_bound = a;
8573           break;
8574         case DW_AT_use_location:
8575           attrs->at_use_location = a;
8576           break;
8577         case DW_AT_use_UTF8:
8578           attrs->at_use_UTF8 = a;
8579           break;
8580         case DW_AT_variable_parameter:
8581           attrs->at_variable_parameter = a;
8582           break;
8583         case DW_AT_virtuality:
8584           attrs->at_virtuality = a;
8585           break;
8586         case DW_AT_visibility:
8587           attrs->at_visibility = a;
8588           break;
8589         case DW_AT_vtable_elem_location:
8590           attrs->at_vtable_elem_location = a;
8591           break;
8592         default:
8593           break;
8594         }
8595     }
8596 }
8597
8598 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8599
8600 static void
8601 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8602 {
8603   dw_die_ref c;
8604   dw_die_ref decl;
8605   struct checksum_attributes attrs;
8606
8607   CHECKSUM_ULEB128 ('D');
8608   CHECKSUM_ULEB128 (die->die_tag);
8609
8610   memset (&attrs, 0, sizeof (attrs));
8611
8612   decl = get_AT_ref (die, DW_AT_specification);
8613   if (decl != NULL)
8614     collect_checksum_attributes (&attrs, decl);
8615   collect_checksum_attributes (&attrs, die);
8616
8617   CHECKSUM_ATTR (attrs.at_name);
8618   CHECKSUM_ATTR (attrs.at_accessibility);
8619   CHECKSUM_ATTR (attrs.at_address_class);
8620   CHECKSUM_ATTR (attrs.at_allocated);
8621   CHECKSUM_ATTR (attrs.at_artificial);
8622   CHECKSUM_ATTR (attrs.at_associated);
8623   CHECKSUM_ATTR (attrs.at_binary_scale);
8624   CHECKSUM_ATTR (attrs.at_bit_offset);
8625   CHECKSUM_ATTR (attrs.at_bit_size);
8626   CHECKSUM_ATTR (attrs.at_bit_stride);
8627   CHECKSUM_ATTR (attrs.at_byte_size);
8628   CHECKSUM_ATTR (attrs.at_byte_stride);
8629   CHECKSUM_ATTR (attrs.at_const_value);
8630   CHECKSUM_ATTR (attrs.at_containing_type);
8631   CHECKSUM_ATTR (attrs.at_count);
8632   CHECKSUM_ATTR (attrs.at_data_location);
8633   CHECKSUM_ATTR (attrs.at_data_member_location);
8634   CHECKSUM_ATTR (attrs.at_decimal_scale);
8635   CHECKSUM_ATTR (attrs.at_decimal_sign);
8636   CHECKSUM_ATTR (attrs.at_default_value);
8637   CHECKSUM_ATTR (attrs.at_digit_count);
8638   CHECKSUM_ATTR (attrs.at_discr);
8639   CHECKSUM_ATTR (attrs.at_discr_list);
8640   CHECKSUM_ATTR (attrs.at_discr_value);
8641   CHECKSUM_ATTR (attrs.at_encoding);
8642   CHECKSUM_ATTR (attrs.at_endianity);
8643   CHECKSUM_ATTR (attrs.at_explicit);
8644   CHECKSUM_ATTR (attrs.at_is_optional);
8645   CHECKSUM_ATTR (attrs.at_location);
8646   CHECKSUM_ATTR (attrs.at_lower_bound);
8647   CHECKSUM_ATTR (attrs.at_mutable);
8648   CHECKSUM_ATTR (attrs.at_ordering);
8649   CHECKSUM_ATTR (attrs.at_picture_string);
8650   CHECKSUM_ATTR (attrs.at_prototyped);
8651   CHECKSUM_ATTR (attrs.at_small);
8652   CHECKSUM_ATTR (attrs.at_segment);
8653   CHECKSUM_ATTR (attrs.at_string_length);
8654   CHECKSUM_ATTR (attrs.at_threads_scaled);
8655   CHECKSUM_ATTR (attrs.at_upper_bound);
8656   CHECKSUM_ATTR (attrs.at_use_location);
8657   CHECKSUM_ATTR (attrs.at_use_UTF8);
8658   CHECKSUM_ATTR (attrs.at_variable_parameter);
8659   CHECKSUM_ATTR (attrs.at_virtuality);
8660   CHECKSUM_ATTR (attrs.at_visibility);
8661   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
8662   CHECKSUM_ATTR (attrs.at_type);
8663   CHECKSUM_ATTR (attrs.at_friend);
8664
8665   /* Checksum the child DIEs, except for nested types and member functions.  */
8666   c = die->die_child;
8667   if (c) do {
8668     dw_attr_ref name_attr;
8669
8670     c = c->die_sib;
8671     name_attr = get_AT (c, DW_AT_name);
8672     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
8673         && name_attr != NULL)
8674       {
8675         CHECKSUM_ULEB128 ('S');
8676         CHECKSUM_ULEB128 (c->die_tag);
8677         CHECKSUM_STRING (AT_string (name_attr));
8678       }
8679     else
8680       {
8681         /* Mark this DIE so it gets processed when unmarking.  */
8682         if (c->die_mark == 0)
8683           c->die_mark = -1;
8684         die_checksum_ordered (c, ctx, mark);
8685       }
8686   } while (c != die->die_child);
8687
8688   CHECKSUM_ULEB128 (0);
8689 }
8690
8691 #undef CHECKSUM
8692 #undef CHECKSUM_STRING
8693 #undef CHECKSUM_ATTR
8694 #undef CHECKSUM_LEB128
8695 #undef CHECKSUM_ULEB128
8696
8697 /* Generate the type signature for DIE.  This is computed by generating an
8698    MD5 checksum over the DIE's tag, its relevant attributes, and its
8699    children.  Attributes that are references to other DIEs are processed
8700    by recursion, using the MARK field to prevent infinite recursion.
8701    If the DIE is nested inside a namespace or another type, we also
8702    need to include that context in the signature.  The lower 64 bits
8703    of the resulting MD5 checksum comprise the signature.  */
8704
8705 static void
8706 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
8707 {
8708   int mark;
8709   const char *name;
8710   unsigned char checksum[16];
8711   struct md5_ctx ctx;
8712   dw_die_ref decl;
8713
8714   name = get_AT_string (die, DW_AT_name);
8715   decl = get_AT_ref (die, DW_AT_specification);
8716
8717   /* First, compute a signature for just the type name (and its surrounding
8718      context, if any.  This is stored in the type unit DIE for link-time
8719      ODR (one-definition rule) checking.  */
8720
8721   if (is_cxx() && name != NULL)
8722     {
8723       md5_init_ctx (&ctx);
8724
8725       /* Checksum the names of surrounding namespaces and structures.  */
8726       if (decl != NULL && decl->die_parent != NULL)
8727         checksum_die_context (decl->die_parent, &ctx);
8728
8729       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
8730       md5_process_bytes (name, strlen (name) + 1, &ctx);
8731       md5_finish_ctx (&ctx, checksum);
8732
8733       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
8734     }
8735
8736   /* Next, compute the complete type signature.  */
8737
8738   md5_init_ctx (&ctx);
8739   mark = 1;
8740   die->die_mark = mark;
8741
8742   /* Checksum the names of surrounding namespaces and structures.  */
8743   if (decl != NULL && decl->die_parent != NULL)
8744     checksum_die_context (decl->die_parent, &ctx);
8745
8746   /* Checksum the DIE and its children.  */
8747   die_checksum_ordered (die, &ctx, &mark);
8748   unmark_all_dies (die);
8749   md5_finish_ctx (&ctx, checksum);
8750
8751   /* Store the signature in the type node and link the type DIE and the
8752      type node together.  */
8753   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
8754           DWARF_TYPE_SIGNATURE_SIZE);
8755   die->die_id.die_type_node = type_node;
8756   type_node->type_die = die;
8757
8758   /* If the DIE is a specification, link its declaration to the type node
8759      as well.  */
8760   if (decl != NULL)
8761     decl->die_id.die_type_node = type_node;
8762 }
8763
8764 /* Do the location expressions look same?  */
8765 static inline int
8766 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
8767 {
8768   return loc1->dw_loc_opc == loc2->dw_loc_opc
8769          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
8770          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
8771 }
8772
8773 /* Do the values look the same?  */
8774 static int
8775 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
8776 {
8777   dw_loc_descr_ref loc1, loc2;
8778   rtx r1, r2;
8779
8780   if (v1->val_class != v2->val_class)
8781     return 0;
8782
8783   switch (v1->val_class)
8784     {
8785     case dw_val_class_const:
8786       return v1->v.val_int == v2->v.val_int;
8787     case dw_val_class_unsigned_const:
8788       return v1->v.val_unsigned == v2->v.val_unsigned;
8789     case dw_val_class_const_double:
8790       return v1->v.val_double.high == v2->v.val_double.high
8791              && v1->v.val_double.low == v2->v.val_double.low;
8792     case dw_val_class_vec:
8793       if (v1->v.val_vec.length != v2->v.val_vec.length
8794           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
8795         return 0;
8796       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
8797                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
8798         return 0;
8799       return 1;
8800     case dw_val_class_flag:
8801       return v1->v.val_flag == v2->v.val_flag;
8802     case dw_val_class_str:
8803       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
8804
8805     case dw_val_class_addr:
8806       r1 = v1->v.val_addr;
8807       r2 = v2->v.val_addr;
8808       if (GET_CODE (r1) != GET_CODE (r2))
8809         return 0;
8810       return !rtx_equal_p (r1, r2);
8811
8812     case dw_val_class_offset:
8813       return v1->v.val_offset == v2->v.val_offset;
8814
8815     case dw_val_class_loc:
8816       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
8817            loc1 && loc2;
8818            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
8819         if (!same_loc_p (loc1, loc2, mark))
8820           return 0;
8821       return !loc1 && !loc2;
8822
8823     case dw_val_class_die_ref:
8824       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
8825
8826     case dw_val_class_fde_ref:
8827     case dw_val_class_lbl_id:
8828     case dw_val_class_lineptr:
8829     case dw_val_class_macptr:
8830       return 1;
8831
8832     case dw_val_class_file:
8833       return v1->v.val_file == v2->v.val_file;
8834
8835     case dw_val_class_data8:
8836       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
8837
8838     default:
8839       return 1;
8840     }
8841 }
8842
8843 /* Do the attributes look the same?  */
8844
8845 static int
8846 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
8847 {
8848   if (at1->dw_attr != at2->dw_attr)
8849     return 0;
8850
8851   /* We don't care that this was compiled with a different compiler
8852      snapshot; if the output is the same, that's what matters. */
8853   if (at1->dw_attr == DW_AT_producer)
8854     return 1;
8855
8856   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
8857 }
8858
8859 /* Do the dies look the same?  */
8860
8861 static int
8862 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
8863 {
8864   dw_die_ref c1, c2;
8865   dw_attr_ref a1;
8866   unsigned ix;
8867
8868   /* To avoid infinite recursion.  */
8869   if (die1->die_mark)
8870     return die1->die_mark == die2->die_mark;
8871   die1->die_mark = die2->die_mark = ++(*mark);
8872
8873   if (die1->die_tag != die2->die_tag)
8874     return 0;
8875
8876   if (VEC_length (dw_attr_node, die1->die_attr)
8877       != VEC_length (dw_attr_node, die2->die_attr))
8878     return 0;
8879
8880   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
8881     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
8882       return 0;
8883
8884   c1 = die1->die_child;
8885   c2 = die2->die_child;
8886   if (! c1)
8887     {
8888       if (c2)
8889         return 0;
8890     }
8891   else
8892     for (;;)
8893       {
8894         if (!same_die_p (c1, c2, mark))
8895           return 0;
8896         c1 = c1->die_sib;
8897         c2 = c2->die_sib;
8898         if (c1 == die1->die_child)
8899           {
8900             if (c2 == die2->die_child)
8901               break;
8902             else
8903               return 0;
8904           }
8905     }
8906
8907   return 1;
8908 }
8909
8910 /* Do the dies look the same?  Wrapper around same_die_p.  */
8911
8912 static int
8913 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8914 {
8915   int mark = 0;
8916   int ret = same_die_p (die1, die2, &mark);
8917
8918   unmark_all_dies (die1);
8919   unmark_all_dies (die2);
8920
8921   return ret;
8922 }
8923
8924 /* The prefix to attach to symbols on DIEs in the current comdat debug
8925    info section.  */
8926 static char *comdat_symbol_id;
8927
8928 /* The index of the current symbol within the current comdat CU.  */
8929 static unsigned int comdat_symbol_number;
8930
8931 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8932    children, and set comdat_symbol_id accordingly.  */
8933
8934 static void
8935 compute_section_prefix (dw_die_ref unit_die)
8936 {
8937   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8938   const char *base = die_name ? lbasename (die_name) : "anonymous";
8939   char *name = XALLOCAVEC (char, strlen (base) + 64);
8940   char *p;
8941   int i, mark;
8942   unsigned char checksum[16];
8943   struct md5_ctx ctx;
8944
8945   /* Compute the checksum of the DIE, then append part of it as hex digits to
8946      the name filename of the unit.  */
8947
8948   md5_init_ctx (&ctx);
8949   mark = 0;
8950   die_checksum (unit_die, &ctx, &mark);
8951   unmark_all_dies (unit_die);
8952   md5_finish_ctx (&ctx, checksum);
8953
8954   sprintf (name, "%s.", base);
8955   clean_symbol_name (name);
8956
8957   p = name + strlen (name);
8958   for (i = 0; i < 4; i++)
8959     {
8960       sprintf (p, "%.2x", checksum[i]);
8961       p += 2;
8962     }
8963
8964   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
8965   comdat_symbol_number = 0;
8966 }
8967
8968 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8969
8970 static int
8971 is_type_die (dw_die_ref die)
8972 {
8973   switch (die->die_tag)
8974     {
8975     case DW_TAG_array_type:
8976     case DW_TAG_class_type:
8977     case DW_TAG_interface_type:
8978     case DW_TAG_enumeration_type:
8979     case DW_TAG_pointer_type:
8980     case DW_TAG_reference_type:
8981     case DW_TAG_string_type:
8982     case DW_TAG_structure_type:
8983     case DW_TAG_subroutine_type:
8984     case DW_TAG_union_type:
8985     case DW_TAG_ptr_to_member_type:
8986     case DW_TAG_set_type:
8987     case DW_TAG_subrange_type:
8988     case DW_TAG_base_type:
8989     case DW_TAG_const_type:
8990     case DW_TAG_file_type:
8991     case DW_TAG_packed_type:
8992     case DW_TAG_volatile_type:
8993     case DW_TAG_typedef:
8994       return 1;
8995     default:
8996       return 0;
8997     }
8998 }
8999
9000 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9001    Basically, we want to choose the bits that are likely to be shared between
9002    compilations (types) and leave out the bits that are specific to individual
9003    compilations (functions).  */
9004
9005 static int
9006 is_comdat_die (dw_die_ref c)
9007 {
9008   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9009      we do for stabs.  The advantage is a greater likelihood of sharing between
9010      objects that don't include headers in the same order (and therefore would
9011      put the base types in a different comdat).  jason 8/28/00 */
9012
9013   if (c->die_tag == DW_TAG_base_type)
9014     return 0;
9015
9016   if (c->die_tag == DW_TAG_pointer_type
9017       || c->die_tag == DW_TAG_reference_type
9018       || c->die_tag == DW_TAG_const_type
9019       || c->die_tag == DW_TAG_volatile_type)
9020     {
9021       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9022
9023       return t ? is_comdat_die (t) : 0;
9024     }
9025
9026   return is_type_die (c);
9027 }
9028
9029 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9030    compilation unit.  */
9031
9032 static int
9033 is_symbol_die (dw_die_ref c)
9034 {
9035   return (is_type_die (c)
9036           || is_declaration_die (c)
9037           || c->die_tag == DW_TAG_namespace
9038           || c->die_tag == DW_TAG_module);
9039 }
9040
9041 static char *
9042 gen_internal_sym (const char *prefix)
9043 {
9044   char buf[256];
9045
9046   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9047   return xstrdup (buf);
9048 }
9049
9050 /* Assign symbols to all worthy DIEs under DIE.  */
9051
9052 static void
9053 assign_symbol_names (dw_die_ref die)
9054 {
9055   dw_die_ref c;
9056
9057   if (is_symbol_die (die))
9058     {
9059       if (comdat_symbol_id)
9060         {
9061           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9062
9063           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9064                    comdat_symbol_id, comdat_symbol_number++);
9065           die->die_id.die_symbol = xstrdup (p);
9066         }
9067       else
9068         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9069     }
9070
9071   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9072 }
9073
9074 struct cu_hash_table_entry
9075 {
9076   dw_die_ref cu;
9077   unsigned min_comdat_num, max_comdat_num;
9078   struct cu_hash_table_entry *next;
9079 };
9080
9081 /* Routines to manipulate hash table of CUs.  */
9082 static hashval_t
9083 htab_cu_hash (const void *of)
9084 {
9085   const struct cu_hash_table_entry *const entry =
9086     (const struct cu_hash_table_entry *) of;
9087
9088   return htab_hash_string (entry->cu->die_id.die_symbol);
9089 }
9090
9091 static int
9092 htab_cu_eq (const void *of1, const void *of2)
9093 {
9094   const struct cu_hash_table_entry *const entry1 =
9095     (const struct cu_hash_table_entry *) of1;
9096   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9097
9098   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9099 }
9100
9101 static void
9102 htab_cu_del (void *what)
9103 {
9104   struct cu_hash_table_entry *next,
9105     *entry = (struct cu_hash_table_entry *) what;
9106
9107   while (entry)
9108     {
9109       next = entry->next;
9110       free (entry);
9111       entry = next;
9112     }
9113 }
9114
9115 /* Check whether we have already seen this CU and set up SYM_NUM
9116    accordingly.  */
9117 static int
9118 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9119 {
9120   struct cu_hash_table_entry dummy;
9121   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9122
9123   dummy.max_comdat_num = 0;
9124
9125   slot = (struct cu_hash_table_entry **)
9126     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9127         INSERT);
9128   entry = *slot;
9129
9130   for (; entry; last = entry, entry = entry->next)
9131     {
9132       if (same_die_p_wrap (cu, entry->cu))
9133         break;
9134     }
9135
9136   if (entry)
9137     {
9138       *sym_num = entry->min_comdat_num;
9139       return 1;
9140     }
9141
9142   entry = XCNEW (struct cu_hash_table_entry);
9143   entry->cu = cu;
9144   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9145   entry->next = *slot;
9146   *slot = entry;
9147
9148   return 0;
9149 }
9150
9151 /* Record SYM_NUM to record of CU in HTABLE.  */
9152 static void
9153 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9154 {
9155   struct cu_hash_table_entry **slot, *entry;
9156
9157   slot = (struct cu_hash_table_entry **)
9158     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9159         NO_INSERT);
9160   entry = *slot;
9161
9162   entry->max_comdat_num = sym_num;
9163 }
9164
9165 /* Traverse the DIE (which is always comp_unit_die), and set up
9166    additional compilation units for each of the include files we see
9167    bracketed by BINCL/EINCL.  */
9168
9169 static void
9170 break_out_includes (dw_die_ref die)
9171 {
9172   dw_die_ref c;
9173   dw_die_ref unit = NULL;
9174   limbo_die_node *node, **pnode;
9175   htab_t cu_hash_table;
9176
9177   c = die->die_child;
9178   if (c) do {
9179     dw_die_ref prev = c;
9180     c = c->die_sib;
9181     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9182            || (unit && is_comdat_die (c)))
9183       {
9184         dw_die_ref next = c->die_sib;
9185
9186         /* This DIE is for a secondary CU; remove it from the main one.  */
9187         remove_child_with_prev (c, prev);
9188
9189         if (c->die_tag == DW_TAG_GNU_BINCL)
9190           unit = push_new_compile_unit (unit, c);
9191         else if (c->die_tag == DW_TAG_GNU_EINCL)
9192           unit = pop_compile_unit (unit);
9193         else
9194           add_child_die (unit, c);
9195         c = next;
9196         if (c == die->die_child)
9197           break;
9198       }
9199   } while (c != die->die_child);
9200
9201 #if 0
9202   /* We can only use this in debugging, since the frontend doesn't check
9203      to make sure that we leave every include file we enter.  */
9204   gcc_assert (!unit);
9205 #endif
9206
9207   assign_symbol_names (die);
9208   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9209   for (node = limbo_die_list, pnode = &limbo_die_list;
9210        node;
9211        node = node->next)
9212     {
9213       int is_dupl;
9214
9215       compute_section_prefix (node->die);
9216       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9217                         &comdat_symbol_number);
9218       assign_symbol_names (node->die);
9219       if (is_dupl)
9220         *pnode = node->next;
9221       else
9222         {
9223           pnode = &node->next;
9224           record_comdat_symbol_number (node->die, cu_hash_table,
9225                 comdat_symbol_number);
9226         }
9227     }
9228   htab_delete (cu_hash_table);
9229 }
9230
9231 /* Return non-zero if this DIE is a declaration.  */
9232
9233 static int
9234 is_declaration_die (dw_die_ref die)
9235 {
9236   dw_attr_ref a;
9237   unsigned ix;
9238
9239   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9240     if (a->dw_attr == DW_AT_declaration)
9241       return 1;
9242
9243   return 0;
9244 }
9245
9246 /* Return non-zero if this is a type DIE that should be moved to a
9247    COMDAT .debug_types section.  */
9248
9249 static int
9250 should_move_die_to_comdat (dw_die_ref die)
9251 {
9252   switch (die->die_tag)
9253     {
9254     case DW_TAG_class_type:
9255     case DW_TAG_structure_type:
9256     case DW_TAG_enumeration_type:
9257     case DW_TAG_union_type:
9258       /* Don't move declarations or inlined instances.  */
9259       if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin))
9260         return 0;
9261       return 1;
9262     case DW_TAG_array_type:
9263     case DW_TAG_interface_type:
9264     case DW_TAG_pointer_type:
9265     case DW_TAG_reference_type:
9266     case DW_TAG_string_type:
9267     case DW_TAG_subroutine_type:
9268     case DW_TAG_ptr_to_member_type:
9269     case DW_TAG_set_type:
9270     case DW_TAG_subrange_type:
9271     case DW_TAG_base_type:
9272     case DW_TAG_const_type:
9273     case DW_TAG_file_type:
9274     case DW_TAG_packed_type:
9275     case DW_TAG_volatile_type:
9276     case DW_TAG_typedef:
9277     default:
9278       return 0;
9279     }
9280 }
9281
9282 /* Make a clone of DIE.  */
9283
9284 static dw_die_ref
9285 clone_die (dw_die_ref die)
9286 {
9287   dw_die_ref clone;
9288   dw_attr_ref a;
9289   unsigned ix;
9290
9291   clone = GGC_CNEW (die_node);
9292   clone->die_tag = die->die_tag;
9293
9294   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9295     add_dwarf_attr (clone, a);
9296
9297   return clone;
9298 }
9299
9300 /* Make a clone of the tree rooted at DIE.  */
9301
9302 static dw_die_ref
9303 clone_tree (dw_die_ref die)
9304 {
9305   dw_die_ref c;
9306   dw_die_ref clone = clone_die (die);
9307
9308   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9309
9310   return clone;
9311 }
9312
9313 /* Make a clone of DIE as a declaration.  */
9314
9315 static dw_die_ref
9316 clone_as_declaration (dw_die_ref die)
9317 {
9318   dw_die_ref clone;
9319   dw_die_ref decl;
9320   dw_attr_ref a;
9321   unsigned ix;
9322
9323   /* If the DIE is already a declaration, just clone it.  */
9324   if (is_declaration_die (die))
9325     return clone_die (die);
9326
9327   /* If the DIE is a specification, just clone its declaration DIE.  */
9328   decl = get_AT_ref (die, DW_AT_specification);
9329   if (decl != NULL)
9330     return clone_die (decl);
9331
9332   clone = GGC_CNEW (die_node);
9333   clone->die_tag = die->die_tag;
9334
9335   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9336     {
9337       /* We don't want to copy over all attributes.
9338          For example we don't want DW_AT_byte_size because otherwise we will no
9339          longer have a declaration and GDB will treat it as a definition.  */
9340
9341       switch (a->dw_attr)
9342         {
9343         case DW_AT_artificial:
9344         case DW_AT_containing_type:
9345         case DW_AT_external:
9346         case DW_AT_name:
9347         case DW_AT_type:
9348         case DW_AT_virtuality:
9349         case DW_AT_MIPS_linkage_name:
9350           add_dwarf_attr (clone, a);
9351           break;
9352         case DW_AT_byte_size:
9353         default:
9354           break;
9355         }
9356     }
9357
9358   if (die->die_id.die_type_node)
9359     add_AT_die_ref (clone, DW_AT_signature, die);
9360
9361   add_AT_flag (clone, DW_AT_declaration, 1);
9362   return clone;
9363 }
9364
9365 /* Copy the declaration context to the new compile unit DIE.  This includes
9366    any surrounding namespace or type declarations.  If the DIE has an
9367    AT_specification attribute, it also includes attributes and children
9368    attached to the specification.  */
9369
9370 static void
9371 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9372 {
9373   dw_die_ref decl;
9374   dw_die_ref new_decl;
9375
9376   decl = get_AT_ref (die, DW_AT_specification);
9377   if (decl == NULL)
9378     decl = die;
9379   else
9380     {
9381       unsigned ix;
9382       dw_die_ref c;
9383       dw_attr_ref a;
9384
9385       /* Copy the type node pointer from the new DIE to the original
9386          declaration DIE so we can forward references later.  */
9387       decl->die_id.die_type_node = die->die_id.die_type_node;
9388
9389       remove_AT (die, DW_AT_specification);
9390
9391       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9392         {
9393           if (a->dw_attr != DW_AT_name
9394               && a->dw_attr != DW_AT_declaration
9395               && a->dw_attr != DW_AT_external)
9396             add_dwarf_attr (die, a);
9397         }
9398
9399       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9400     }
9401
9402   if (decl->die_parent != NULL
9403       && decl->die_parent->die_tag != DW_TAG_compile_unit
9404       && decl->die_parent->die_tag != DW_TAG_type_unit)
9405     {
9406       new_decl = copy_ancestor_tree (unit, decl, NULL);
9407       if (new_decl != NULL)
9408         {
9409           remove_AT (new_decl, DW_AT_signature);
9410           add_AT_specification (die, new_decl);
9411         }
9412     }
9413 }
9414
9415 /* Generate the skeleton ancestor tree for the given NODE, then clone
9416    the DIE and add the clone into the tree.  */
9417
9418 static void
9419 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9420 {
9421   if (node->new_die != NULL)
9422     return;
9423
9424   node->new_die = clone_as_declaration (node->old_die);
9425
9426   if (node->parent != NULL)
9427     {
9428       generate_skeleton_ancestor_tree (node->parent);
9429       add_child_die (node->parent->new_die, node->new_die);
9430     }
9431 }
9432
9433 /* Generate a skeleton tree of DIEs containing any declarations that are
9434    found in the original tree.  We traverse the tree looking for declaration
9435    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9436
9437 static void
9438 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9439 {
9440   skeleton_chain_node node;
9441   dw_die_ref c;
9442   dw_die_ref first;
9443   dw_die_ref prev = NULL;
9444   dw_die_ref next = NULL;
9445
9446   node.parent = parent;
9447
9448   first = c = parent->old_die->die_child;
9449   if (c)
9450     next = c->die_sib;
9451   if (c) do {
9452     if (prev == NULL || prev->die_sib == c)
9453       prev = c;
9454     c = next;
9455     next = (c == first ? NULL : c->die_sib);
9456     node.old_die = c;
9457     node.new_die = NULL;
9458     if (is_declaration_die (c))
9459       {
9460         /* Clone the existing DIE, move the original to the skeleton
9461            tree (which is in the main CU), and put the clone, with
9462            all the original's children, where the original came from.  */
9463         dw_die_ref clone = clone_die (c);
9464         move_all_children (c, clone);
9465
9466         replace_child (c, clone, prev);
9467         generate_skeleton_ancestor_tree (parent);
9468         add_child_die (parent->new_die, c);
9469         node.new_die = c;
9470         c = clone;
9471       }
9472     generate_skeleton_bottom_up (&node);
9473   } while (next != NULL);
9474 }
9475
9476 /* Wrapper function for generate_skeleton_bottom_up.  */
9477
9478 static dw_die_ref
9479 generate_skeleton (dw_die_ref die)
9480 {
9481   skeleton_chain_node node;
9482
9483   node.old_die = die;
9484   node.new_die = NULL;
9485   node.parent = NULL;
9486
9487   /* If this type definition is nested inside another type,
9488      always leave at least a declaration in its place.  */
9489   if (die->die_parent != NULL && is_type_die (die->die_parent))
9490     node.new_die = clone_as_declaration (die);
9491
9492   generate_skeleton_bottom_up (&node);
9493   return node.new_die;
9494 }
9495
9496 /* Remove the DIE from its parent, possibly replacing it with a cloned
9497    declaration.  The original DIE will be moved to a new compile unit
9498    so that existing references to it follow it to the new location.  If
9499    any of the original DIE's descendants is a declaration, we need to
9500    replace the original DIE with a skeleton tree and move the
9501    declarations back into the skeleton tree.  */
9502
9503 static dw_die_ref
9504 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9505 {
9506   dw_die_ref skeleton;
9507
9508   skeleton = generate_skeleton (child);
9509   if (skeleton == NULL)
9510     remove_child_with_prev (child, prev);
9511   else
9512     {
9513       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9514       replace_child (child, skeleton, prev);
9515     }
9516
9517   return skeleton;
9518 }
9519
9520 /* Traverse the DIE and set up additional .debug_types sections for each
9521    type worthy of being placed in a COMDAT section.  */
9522
9523 static void
9524 break_out_comdat_types (dw_die_ref die)
9525 {
9526   dw_die_ref c;
9527   dw_die_ref first;
9528   dw_die_ref prev = NULL;
9529   dw_die_ref next = NULL;
9530   dw_die_ref unit = NULL;
9531
9532   first = c = die->die_child;
9533   if (c)
9534     next = c->die_sib;
9535   if (c) do {
9536     if (prev == NULL || prev->die_sib == c)
9537       prev = c;
9538     c = next;
9539     next = (c == first ? NULL : c->die_sib);
9540     if (should_move_die_to_comdat (c))
9541       {
9542         dw_die_ref replacement;
9543         comdat_type_node_ref type_node;
9544
9545         /* Create a new type unit DIE as the root for the new tree, and
9546            add it to the list of comdat types.  */
9547         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9548         add_AT_unsigned (unit, DW_AT_language,
9549                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9550         type_node = GGC_CNEW (comdat_type_node);
9551         type_node->root_die = unit;
9552         type_node->next = comdat_type_list;
9553         comdat_type_list = type_node;
9554
9555         /* Generate the type signature.  */
9556         generate_type_signature (c, type_node);
9557
9558         /* Copy the declaration context, attributes, and children of the
9559            declaration into the new compile unit DIE.  */
9560         copy_declaration_context (unit, c);
9561
9562         /* Remove this DIE from the main CU.  */
9563         replacement = remove_child_or_replace_with_skeleton (c, prev);
9564
9565         /* Break out nested types into their own type units.  */
9566         break_out_comdat_types (c);
9567
9568         /* Add the DIE to the new compunit.  */
9569         add_child_die (unit, c);
9570
9571         if (replacement != NULL)
9572           c = replacement;
9573       }
9574     else if (c->die_tag == DW_TAG_namespace
9575              || c->die_tag == DW_TAG_class_type
9576              || c->die_tag == DW_TAG_structure_type
9577              || c->die_tag == DW_TAG_union_type)
9578       {
9579         /* Look for nested types that can be broken out.  */
9580         break_out_comdat_types (c);
9581       }
9582   } while (next != NULL);
9583 }
9584
9585 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9586
9587 struct decl_table_entry
9588 {
9589   dw_die_ref orig;
9590   dw_die_ref copy;
9591 };
9592
9593 /* Routines to manipulate hash table of copied declarations.  */
9594
9595 static hashval_t
9596 htab_decl_hash (const void *of)
9597 {
9598   const struct decl_table_entry *const entry =
9599     (const struct decl_table_entry *) of;
9600
9601   return htab_hash_pointer (entry->orig);
9602 }
9603
9604 static int
9605 htab_decl_eq (const void *of1, const void *of2)
9606 {
9607   const struct decl_table_entry *const entry1 =
9608     (const struct decl_table_entry *) of1;
9609   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9610
9611   return entry1->orig == entry2;
9612 }
9613
9614 static void
9615 htab_decl_del (void *what)
9616 {
9617   struct decl_table_entry *entry = (struct decl_table_entry *) what;
9618
9619   free (entry);
9620 }
9621
9622 /* Copy DIE and its ancestors, up to, but not including, the compile unit
9623    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
9624    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
9625    to check if the ancestor has already been copied into UNIT.  */
9626
9627 static dw_die_ref
9628 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9629 {
9630   dw_die_ref parent = die->die_parent;
9631   dw_die_ref new_parent = unit;
9632   dw_die_ref copy;
9633   void **slot = NULL;
9634   struct decl_table_entry *entry = NULL;
9635
9636   if (decl_table)
9637     {
9638       /* Check if the entry has already been copied to UNIT.  */
9639       slot = htab_find_slot_with_hash (decl_table, die,
9640                                        htab_hash_pointer (die), INSERT);
9641       if (*slot != HTAB_EMPTY_ENTRY)
9642         {
9643           entry = (struct decl_table_entry *) *slot;
9644           return entry->copy;
9645         }
9646
9647       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
9648       entry = XCNEW (struct decl_table_entry);
9649       entry->orig = die;
9650       entry->copy = NULL;
9651       *slot = entry;
9652     }
9653
9654   if (parent != NULL)
9655     {
9656       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
9657       if (spec != NULL)
9658         parent = spec;
9659       if (parent->die_tag != DW_TAG_compile_unit
9660           && parent->die_tag != DW_TAG_type_unit)
9661         new_parent = copy_ancestor_tree (unit, parent, decl_table);
9662     }
9663
9664   copy = clone_as_declaration (die);
9665   add_child_die (new_parent, copy);
9666
9667   if (decl_table != NULL)
9668     {
9669       /* Make sure the copy is marked as part of the type unit.  */
9670       copy->die_mark = 1;
9671       /* Record the pointer to the copy.  */
9672       entry->copy = copy;
9673     }
9674
9675   return copy;
9676 }
9677
9678 /* Walk the DIE and its children, looking for references to incomplete
9679    or trivial types that are unmarked (i.e., that are not in the current
9680    type_unit).  */
9681
9682 static void
9683 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9684 {
9685   dw_die_ref c;
9686   dw_attr_ref a;
9687   unsigned ix;
9688
9689   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9690     {
9691       if (AT_class (a) == dw_val_class_die_ref)
9692         {
9693           dw_die_ref targ = AT_ref (a);
9694           comdat_type_node_ref type_node = targ->die_id.die_type_node;
9695           void **slot;
9696           struct decl_table_entry *entry;
9697
9698           if (targ->die_mark != 0 || type_node != NULL)
9699             continue;
9700
9701           slot = htab_find_slot_with_hash (decl_table, targ,
9702                                            htab_hash_pointer (targ), INSERT);
9703
9704           if (*slot != HTAB_EMPTY_ENTRY)
9705             {
9706               /* TARG has already been copied, so we just need to
9707                  modify the reference to point to the copy.  */
9708               entry = (struct decl_table_entry *) *slot;
9709               a->dw_attr_val.v.val_die_ref.die = entry->copy;
9710             }
9711           else
9712             {
9713               dw_die_ref parent = unit;
9714               dw_die_ref copy = clone_tree (targ);
9715
9716               /* Make sure the cloned tree is marked as part of the
9717                  type unit.  */
9718               mark_dies (copy);
9719
9720               /* Record in DECL_TABLE that TARG has been copied.
9721                  Need to do this now, before the recursive call,
9722                  because DECL_TABLE may be expanded and SLOT
9723                  would no longer be a valid pointer.  */
9724               entry = XCNEW (struct decl_table_entry);
9725               entry->orig = targ;
9726               entry->copy = copy;
9727               *slot = entry;
9728
9729               /* If TARG has surrounding context, copy its ancestor tree
9730                  into the new type unit.  */
9731               if (targ->die_parent != NULL
9732                   && targ->die_parent->die_tag != DW_TAG_compile_unit
9733                   && targ->die_parent->die_tag != DW_TAG_type_unit)
9734                 parent = copy_ancestor_tree (unit, targ->die_parent,
9735                                              decl_table);
9736
9737               add_child_die (parent, copy);
9738               a->dw_attr_val.v.val_die_ref.die = copy;
9739
9740               /* Make sure the newly-copied DIE is walked.  If it was
9741                  installed in a previously-added context, it won't
9742                  get visited otherwise.  */
9743               if (parent != unit)
9744                 copy_decls_walk (unit, parent, decl_table);
9745             }
9746         }
9747     }
9748
9749   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
9750 }
9751
9752 /* Copy declarations for "unworthy" types into the new comdat section.
9753    Incomplete types, modified types, and certain other types aren't broken
9754    out into comdat sections of their own, so they don't have a signature,
9755    and we need to copy the declaration into the same section so that we
9756    don't have an external reference.  */
9757
9758 static void
9759 copy_decls_for_unworthy_types (dw_die_ref unit)
9760 {
9761   htab_t decl_table;
9762
9763   mark_dies (unit);
9764   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
9765   copy_decls_walk (unit, unit, decl_table);
9766   htab_delete (decl_table);
9767   unmark_dies (unit);
9768 }
9769
9770 /* Traverse the DIE and add a sibling attribute if it may have the
9771    effect of speeding up access to siblings.  To save some space,
9772    avoid generating sibling attributes for DIE's without children.  */
9773
9774 static void
9775 add_sibling_attributes (dw_die_ref die)
9776 {
9777   dw_die_ref c;
9778
9779   if (! die->die_child)
9780     return;
9781
9782   if (die->die_parent && die != die->die_parent->die_child)
9783     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
9784
9785   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
9786 }
9787
9788 /* Output all location lists for the DIE and its children.  */
9789
9790 static void
9791 output_location_lists (dw_die_ref die)
9792 {
9793   dw_die_ref c;
9794   dw_attr_ref a;
9795   unsigned ix;
9796
9797   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9798     if (AT_class (a) == dw_val_class_loc_list)
9799       output_loc_list (AT_loc_list (a));
9800
9801   FOR_EACH_CHILD (die, c, output_location_lists (c));
9802 }
9803
9804 /* The format of each DIE (and its attribute value pairs) is encoded in an
9805    abbreviation table.  This routine builds the abbreviation table and assigns
9806    a unique abbreviation id for each abbreviation entry.  The children of each
9807    die are visited recursively.  */
9808
9809 static void
9810 build_abbrev_table (dw_die_ref die)
9811 {
9812   unsigned long abbrev_id;
9813   unsigned int n_alloc;
9814   dw_die_ref c;
9815   dw_attr_ref a;
9816   unsigned ix;
9817
9818   /* Scan the DIE references, and mark as external any that refer to
9819      DIEs from other CUs (i.e. those which are not marked).  */
9820   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9821     if (AT_class (a) == dw_val_class_die_ref
9822         && AT_ref (a)->die_mark == 0)
9823       {
9824         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
9825         set_AT_ref_external (a, 1);
9826       }
9827
9828   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
9829     {
9830       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
9831       dw_attr_ref die_a, abbrev_a;
9832       unsigned ix;
9833       bool ok = true;
9834
9835       if (abbrev->die_tag != die->die_tag)
9836         continue;
9837       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9838         continue;
9839
9840       if (VEC_length (dw_attr_node, abbrev->die_attr)
9841           != VEC_length (dw_attr_node, die->die_attr))
9842         continue;
9843
9844       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
9845         {
9846           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
9847           if ((abbrev_a->dw_attr != die_a->dw_attr)
9848               || (value_format (abbrev_a) != value_format (die_a)))
9849             {
9850               ok = false;
9851               break;
9852             }
9853         }
9854       if (ok)
9855         break;
9856     }
9857
9858   if (abbrev_id >= abbrev_die_table_in_use)
9859     {
9860       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
9861         {
9862           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
9863           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
9864                                             n_alloc);
9865
9866           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
9867                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
9868           abbrev_die_table_allocated = n_alloc;
9869         }
9870
9871       ++abbrev_die_table_in_use;
9872       abbrev_die_table[abbrev_id] = die;
9873     }
9874
9875   die->die_abbrev = abbrev_id;
9876   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
9877 }
9878 \f
9879 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9880
9881 static int
9882 constant_size (unsigned HOST_WIDE_INT value)
9883 {
9884   int log;
9885
9886   if (value == 0)
9887     log = 0;
9888   else
9889     log = floor_log2 (value);
9890
9891   log = log / 8;
9892   log = 1 << (floor_log2 (log) + 1);
9893
9894   return log;
9895 }
9896
9897 /* Return the size of a DIE as it is represented in the
9898    .debug_info section.  */
9899
9900 static unsigned long
9901 size_of_die (dw_die_ref die)
9902 {
9903   unsigned long size = 0;
9904   dw_attr_ref a;
9905   unsigned ix;
9906
9907   size += size_of_uleb128 (die->die_abbrev);
9908   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9909     {
9910       switch (AT_class (a))
9911         {
9912         case dw_val_class_addr:
9913           size += DWARF2_ADDR_SIZE;
9914           break;
9915         case dw_val_class_offset:
9916           size += DWARF_OFFSET_SIZE;
9917           break;
9918         case dw_val_class_loc:
9919           {
9920             unsigned long lsize = size_of_locs (AT_loc (a));
9921
9922             /* Block length.  */
9923             size += constant_size (lsize);
9924             size += lsize;
9925           }
9926           break;
9927         case dw_val_class_loc_list:
9928           size += DWARF_OFFSET_SIZE;
9929           break;
9930         case dw_val_class_range_list:
9931           size += DWARF_OFFSET_SIZE;
9932           break;
9933         case dw_val_class_const:
9934           size += size_of_sleb128 (AT_int (a));
9935           break;
9936         case dw_val_class_unsigned_const:
9937           size += constant_size (AT_unsigned (a));
9938           break;
9939         case dw_val_class_const_double:
9940           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9941           if (HOST_BITS_PER_WIDE_INT >= 64)
9942             size++; /* block */
9943           break;
9944         case dw_val_class_vec:
9945           size += constant_size (a->dw_attr_val.v.val_vec.length
9946                                  * a->dw_attr_val.v.val_vec.elt_size)
9947                   + a->dw_attr_val.v.val_vec.length
9948                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
9949           break;
9950         case dw_val_class_flag:
9951           size += 1;
9952           break;
9953         case dw_val_class_die_ref:
9954           if (AT_ref_external (a))
9955             {
9956               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
9957                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9958                  is sized by target address length, whereas in DWARF3
9959                  it's always sized as an offset.  */
9960               if (dwarf_version >= 4)
9961                 size += DWARF_TYPE_SIGNATURE_SIZE;
9962               else if (dwarf_version == 2)
9963                 size += DWARF2_ADDR_SIZE;
9964               else
9965                 size += DWARF_OFFSET_SIZE;
9966             }
9967           else
9968             size += DWARF_OFFSET_SIZE;
9969           break;
9970         case dw_val_class_fde_ref:
9971           size += DWARF_OFFSET_SIZE;
9972           break;
9973         case dw_val_class_lbl_id:
9974           size += DWARF2_ADDR_SIZE;
9975           break;
9976         case dw_val_class_lineptr:
9977         case dw_val_class_macptr:
9978           size += DWARF_OFFSET_SIZE;
9979           break;
9980         case dw_val_class_str:
9981           if (AT_string_form (a) == DW_FORM_strp)
9982             size += DWARF_OFFSET_SIZE;
9983           else
9984             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9985           break;
9986         case dw_val_class_file:
9987           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9988           break;
9989         case dw_val_class_data8:
9990           size += 8;
9991           break;
9992         default:
9993           gcc_unreachable ();
9994         }
9995     }
9996
9997   return size;
9998 }
9999
10000 /* Size the debugging information associated with a given DIE.  Visits the
10001    DIE's children recursively.  Updates the global variable next_die_offset, on
10002    each time through.  Uses the current value of next_die_offset to update the
10003    die_offset field in each DIE.  */
10004
10005 static void
10006 calc_die_sizes (dw_die_ref die)
10007 {
10008   dw_die_ref c;
10009
10010   die->die_offset = next_die_offset;
10011   next_die_offset += size_of_die (die);
10012
10013   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10014
10015   if (die->die_child != NULL)
10016     /* Count the null byte used to terminate sibling lists.  */
10017     next_die_offset += 1;
10018 }
10019
10020 /* Set the marks for a die and its children.  We do this so
10021    that we know whether or not a reference needs to use FORM_ref_addr; only
10022    DIEs in the same CU will be marked.  We used to clear out the offset
10023    and use that as the flag, but ran into ordering problems.  */
10024
10025 static void
10026 mark_dies (dw_die_ref die)
10027 {
10028   dw_die_ref c;
10029
10030   gcc_assert (!die->die_mark);
10031
10032   die->die_mark = 1;
10033   FOR_EACH_CHILD (die, c, mark_dies (c));
10034 }
10035
10036 /* Clear the marks for a die and its children.  */
10037
10038 static void
10039 unmark_dies (dw_die_ref die)
10040 {
10041   dw_die_ref c;
10042
10043   if (dwarf_version < 4)
10044     gcc_assert (die->die_mark);
10045
10046   die->die_mark = 0;
10047   FOR_EACH_CHILD (die, c, unmark_dies (c));
10048 }
10049
10050 /* Clear the marks for a die, its children and referred dies.  */
10051
10052 static void
10053 unmark_all_dies (dw_die_ref die)
10054 {
10055   dw_die_ref c;
10056   dw_attr_ref a;
10057   unsigned ix;
10058
10059   if (!die->die_mark)
10060     return;
10061   die->die_mark = 0;
10062
10063   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10064
10065   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10066     if (AT_class (a) == dw_val_class_die_ref)
10067       unmark_all_dies (AT_ref (a));
10068 }
10069
10070 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10071    generated for the compilation unit.  */
10072
10073 static unsigned long
10074 size_of_pubnames (VEC (pubname_entry, gc) * names)
10075 {
10076   unsigned long size;
10077   unsigned i;
10078   pubname_ref p;
10079
10080   size = DWARF_PUBNAMES_HEADER_SIZE;
10081   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10082     if (names != pubtype_table
10083         || p->die->die_offset != 0
10084         || !flag_eliminate_unused_debug_types)
10085       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10086
10087   size += DWARF_OFFSET_SIZE;
10088   return size;
10089 }
10090
10091 /* Return the size of the information in the .debug_aranges section.  */
10092
10093 static unsigned long
10094 size_of_aranges (void)
10095 {
10096   unsigned long size;
10097
10098   size = DWARF_ARANGES_HEADER_SIZE;
10099
10100   /* Count the address/length pair for this compilation unit.  */
10101   if (text_section_used)
10102     size += 2 * DWARF2_ADDR_SIZE;
10103   if (cold_text_section_used)
10104     size += 2 * DWARF2_ADDR_SIZE;
10105   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10106
10107   /* Count the two zero words used to terminated the address range table.  */
10108   size += 2 * DWARF2_ADDR_SIZE;
10109   return size;
10110 }
10111 \f
10112 /* Select the encoding of an attribute value.  */
10113
10114 static enum dwarf_form
10115 value_format (dw_attr_ref a)
10116 {
10117   switch (a->dw_attr_val.val_class)
10118     {
10119     case dw_val_class_addr:
10120       /* Only very few attributes allow DW_FORM_addr.  */
10121       switch (a->dw_attr)
10122         {
10123         case DW_AT_low_pc:
10124         case DW_AT_high_pc:
10125         case DW_AT_entry_pc:
10126         case DW_AT_trampoline:
10127           return DW_FORM_addr;
10128         default:
10129           break;
10130         }
10131       switch (DWARF2_ADDR_SIZE)
10132         {
10133         case 1:
10134           return DW_FORM_data1;
10135         case 2:
10136           return DW_FORM_data2;
10137         case 4:
10138           return DW_FORM_data4;
10139         case 8:
10140           return DW_FORM_data8;
10141         default:
10142           gcc_unreachable ();
10143         }
10144     case dw_val_class_range_list:
10145     case dw_val_class_offset:
10146     case dw_val_class_loc_list:
10147       switch (DWARF_OFFSET_SIZE)
10148         {
10149         case 4:
10150           return DW_FORM_data4;
10151         case 8:
10152           return DW_FORM_data8;
10153         default:
10154           gcc_unreachable ();
10155         }
10156     case dw_val_class_loc:
10157       switch (constant_size (size_of_locs (AT_loc (a))))
10158         {
10159         case 1:
10160           return DW_FORM_block1;
10161         case 2:
10162           return DW_FORM_block2;
10163         default:
10164           gcc_unreachable ();
10165         }
10166     case dw_val_class_const:
10167       return DW_FORM_sdata;
10168     case dw_val_class_unsigned_const:
10169       switch (constant_size (AT_unsigned (a)))
10170         {
10171         case 1:
10172           return DW_FORM_data1;
10173         case 2:
10174           return DW_FORM_data2;
10175         case 4:
10176           return DW_FORM_data4;
10177         case 8:
10178           return DW_FORM_data8;
10179         default:
10180           gcc_unreachable ();
10181         }
10182     case dw_val_class_const_double:
10183       switch (HOST_BITS_PER_WIDE_INT)
10184         {
10185         case 8:
10186           return DW_FORM_data2;
10187         case 16:
10188           return DW_FORM_data4;
10189         case 32:
10190           return DW_FORM_data8;
10191         case 64:
10192         default:
10193           return DW_FORM_block1;
10194         }
10195     case dw_val_class_vec:
10196       switch (constant_size (a->dw_attr_val.v.val_vec.length
10197                              * a->dw_attr_val.v.val_vec.elt_size))
10198         {
10199         case 1:
10200           return DW_FORM_block1;
10201         case 2:
10202           return DW_FORM_block2;
10203         case 4:
10204           return DW_FORM_block4;
10205         default:
10206           gcc_unreachable ();
10207         }
10208     case dw_val_class_flag:
10209       return DW_FORM_flag;
10210     case dw_val_class_die_ref:
10211       if (AT_ref_external (a))
10212         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10213       else
10214         return DW_FORM_ref;
10215     case dw_val_class_fde_ref:
10216       return DW_FORM_data;
10217     case dw_val_class_lbl_id:
10218       return DW_FORM_addr;
10219     case dw_val_class_lineptr:
10220     case dw_val_class_macptr:
10221       return DW_FORM_data;
10222     case dw_val_class_str:
10223       return AT_string_form (a);
10224     case dw_val_class_file:
10225       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10226         {
10227         case 1:
10228           return DW_FORM_data1;
10229         case 2:
10230           return DW_FORM_data2;
10231         case 4:
10232           return DW_FORM_data4;
10233         default:
10234           gcc_unreachable ();
10235         }
10236
10237     case dw_val_class_data8:
10238       return DW_FORM_data8;
10239
10240     default:
10241       gcc_unreachable ();
10242     }
10243 }
10244
10245 /* Output the encoding of an attribute value.  */
10246
10247 static void
10248 output_value_format (dw_attr_ref a)
10249 {
10250   enum dwarf_form form = value_format (a);
10251
10252   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10253 }
10254
10255 /* Output the .debug_abbrev section which defines the DIE abbreviation
10256    table.  */
10257
10258 static void
10259 output_abbrev_section (void)
10260 {
10261   unsigned long abbrev_id;
10262
10263   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10264     {
10265       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10266       unsigned ix;
10267       dw_attr_ref a_attr;
10268
10269       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10270       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10271                                    dwarf_tag_name (abbrev->die_tag));
10272
10273       if (abbrev->die_child != NULL)
10274         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10275       else
10276         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10277
10278       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10279            ix++)
10280         {
10281           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10282                                        dwarf_attr_name (a_attr->dw_attr));
10283           output_value_format (a_attr);
10284         }
10285
10286       dw2_asm_output_data (1, 0, NULL);
10287       dw2_asm_output_data (1, 0, NULL);
10288     }
10289
10290   /* Terminate the table.  */
10291   dw2_asm_output_data (1, 0, NULL);
10292 }
10293
10294 /* Output a symbol we can use to refer to this DIE from another CU.  */
10295
10296 static inline void
10297 output_die_symbol (dw_die_ref die)
10298 {
10299   char *sym = die->die_id.die_symbol;
10300
10301   if (sym == 0)
10302     return;
10303
10304   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10305     /* We make these global, not weak; if the target doesn't support
10306        .linkonce, it doesn't support combining the sections, so debugging
10307        will break.  */
10308     targetm.asm_out.globalize_label (asm_out_file, sym);
10309
10310   ASM_OUTPUT_LABEL (asm_out_file, sym);
10311 }
10312
10313 /* Return a new location list, given the begin and end range, and the
10314    expression.  */
10315
10316 static inline dw_loc_list_ref
10317 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10318               const char *section)
10319 {
10320   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
10321
10322   retlist->begin = begin;
10323   retlist->end = end;
10324   retlist->expr = expr;
10325   retlist->section = section;
10326
10327   return retlist;
10328 }
10329
10330 /* Generate a new internal symbol for this location list node, if it
10331    hasn't got one yet.  */
10332
10333 static inline void
10334 gen_llsym (dw_loc_list_ref list)
10335 {
10336   gcc_assert (!list->ll_symbol);
10337   list->ll_symbol = gen_internal_sym ("LLST");
10338 }
10339
10340 /* Output the location list given to us.  */
10341
10342 static void
10343 output_loc_list (dw_loc_list_ref list_head)
10344 {
10345   dw_loc_list_ref curr = list_head;
10346
10347   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10348
10349   /* Walk the location list, and output each range + expression.  */
10350   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10351     {
10352       unsigned long size;
10353       /* Don't output an entry that starts and ends at the same address.  */
10354       if (strcmp (curr->begin, curr->end) == 0)
10355         continue;
10356       if (!have_multiple_function_sections)
10357         {
10358           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10359                                 "Location list begin address (%s)",
10360                                 list_head->ll_symbol);
10361           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10362                                 "Location list end address (%s)",
10363                                 list_head->ll_symbol);
10364         }
10365       else
10366         {
10367           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10368                                "Location list begin address (%s)",
10369                                list_head->ll_symbol);
10370           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10371                                "Location list end address (%s)",
10372                                list_head->ll_symbol);
10373         }
10374       size = size_of_locs (curr->expr);
10375
10376       /* Output the block length for this list of location operations.  */
10377       gcc_assert (size <= 0xffff);
10378       dw2_asm_output_data (2, size, "%s", "Location expression size");
10379
10380       output_loc_sequence (curr->expr);
10381     }
10382
10383   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10384                        "Location list terminator begin (%s)",
10385                        list_head->ll_symbol);
10386   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10387                        "Location list terminator end (%s)",
10388                        list_head->ll_symbol);
10389 }
10390
10391 /* Output a type signature.  */
10392
10393 static inline void
10394 output_signature (const char *sig, const char *name)
10395 {
10396   int i;
10397
10398   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10399     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10400 }
10401
10402 /* Output the DIE and its attributes.  Called recursively to generate
10403    the definitions of each child DIE.  */
10404
10405 static void
10406 output_die (dw_die_ref die)
10407 {
10408   dw_attr_ref a;
10409   dw_die_ref c;
10410   unsigned long size;
10411   unsigned ix;
10412
10413   /* If someone in another CU might refer to us, set up a symbol for
10414      them to point to.  */
10415   if (dwarf_version < 4 && die->die_id.die_symbol)
10416     output_die_symbol (die);
10417
10418   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
10419                                (unsigned long)die->die_offset,
10420                                dwarf_tag_name (die->die_tag));
10421
10422   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10423     {
10424       const char *name = dwarf_attr_name (a->dw_attr);
10425
10426       switch (AT_class (a))
10427         {
10428         case dw_val_class_addr:
10429           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10430           break;
10431
10432         case dw_val_class_offset:
10433           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10434                                "%s", name);
10435           break;
10436
10437         case dw_val_class_range_list:
10438           {
10439             char *p = strchr (ranges_section_label, '\0');
10440
10441             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10442                      a->dw_attr_val.v.val_offset);
10443             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10444                                    debug_ranges_section, "%s", name);
10445             *p = '\0';
10446           }
10447           break;
10448
10449         case dw_val_class_loc:
10450           size = size_of_locs (AT_loc (a));
10451
10452           /* Output the block length for this list of location operations.  */
10453           dw2_asm_output_data (constant_size (size), size, "%s", name);
10454
10455           output_loc_sequence (AT_loc (a));
10456           break;
10457
10458         case dw_val_class_const:
10459           /* ??? It would be slightly more efficient to use a scheme like is
10460              used for unsigned constants below, but gdb 4.x does not sign
10461              extend.  Gdb 5.x does sign extend.  */
10462           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10463           break;
10464
10465         case dw_val_class_unsigned_const:
10466           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10467                                AT_unsigned (a), "%s", name);
10468           break;
10469
10470         case dw_val_class_const_double:
10471           {
10472             unsigned HOST_WIDE_INT first, second;
10473
10474             if (HOST_BITS_PER_WIDE_INT >= 64)
10475               dw2_asm_output_data (1,
10476                                    2 * HOST_BITS_PER_WIDE_INT
10477                                    / HOST_BITS_PER_CHAR,
10478                                    NULL);
10479
10480             if (WORDS_BIG_ENDIAN)
10481               {
10482                 first = a->dw_attr_val.v.val_double.high;
10483                 second = a->dw_attr_val.v.val_double.low;
10484               }
10485             else
10486               {
10487                 first = a->dw_attr_val.v.val_double.low;
10488                 second = a->dw_attr_val.v.val_double.high;
10489               }
10490
10491             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10492                                  first, name);
10493             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10494                                  second, NULL);
10495           }
10496           break;
10497
10498         case dw_val_class_vec:
10499           {
10500             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10501             unsigned int len = a->dw_attr_val.v.val_vec.length;
10502             unsigned int i;
10503             unsigned char *p;
10504
10505             dw2_asm_output_data (constant_size (len * elt_size),
10506                                  len * elt_size, "%s", name);
10507             if (elt_size > sizeof (HOST_WIDE_INT))
10508               {
10509                 elt_size /= 2;
10510                 len *= 2;
10511               }
10512             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10513                  i < len;
10514                  i++, p += elt_size)
10515               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10516                                    "fp or vector constant word %u", i);
10517             break;
10518           }
10519
10520         case dw_val_class_flag:
10521           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10522           break;
10523
10524         case dw_val_class_loc_list:
10525           {
10526             char *sym = AT_loc_list (a)->ll_symbol;
10527
10528             gcc_assert (sym);
10529             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10530                                    "%s", name);
10531           }
10532           break;
10533
10534         case dw_val_class_die_ref:
10535           if (AT_ref_external (a))
10536             {
10537               if (dwarf_version >= 4)
10538                 {
10539                   comdat_type_node_ref type_node =
10540                     AT_ref (a)->die_id.die_type_node;
10541
10542                   gcc_assert (type_node);
10543                   output_signature (type_node->signature, name);
10544                 }
10545               else
10546                 {
10547                   char *sym = AT_ref (a)->die_id.die_symbol;
10548                   int size;
10549
10550                   gcc_assert (sym);
10551                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
10552                      length, whereas in DWARF3 it's always sized as an
10553                      offset.  */
10554                   if (dwarf_version == 2)
10555                     size = DWARF2_ADDR_SIZE;
10556                   else
10557                     size = DWARF_OFFSET_SIZE;
10558                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10559                                          name);
10560                 }
10561             }
10562           else
10563             {
10564               gcc_assert (AT_ref (a)->die_offset);
10565               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10566                                    "%s", name);
10567             }
10568           break;
10569
10570         case dw_val_class_fde_ref:
10571           {
10572             char l1[20];
10573
10574             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10575                                          a->dw_attr_val.v.val_fde_index * 2);
10576             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10577                                    "%s", name);
10578           }
10579           break;
10580
10581         case dw_val_class_lbl_id:
10582           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10583           break;
10584
10585         case dw_val_class_lineptr:
10586           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10587                                  debug_line_section, "%s", name);
10588           break;
10589
10590         case dw_val_class_macptr:
10591           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10592                                  debug_macinfo_section, "%s", name);
10593           break;
10594
10595         case dw_val_class_str:
10596           if (AT_string_form (a) == DW_FORM_strp)
10597             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10598                                    a->dw_attr_val.v.val_str->label,
10599                                    debug_str_section,
10600                                    "%s: \"%s\"", name, AT_string (a));
10601           else
10602             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10603           break;
10604
10605         case dw_val_class_file:
10606           {
10607             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10608
10609             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10610                                  a->dw_attr_val.v.val_file->filename);
10611             break;
10612           }
10613
10614         case dw_val_class_data8:
10615           {
10616             int i;
10617
10618             for (i = 0; i < 8; i++)
10619               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10620                                    i == 0 ? "%s" : NULL, name);
10621             break;
10622           }
10623
10624         default:
10625           gcc_unreachable ();
10626         }
10627     }
10628
10629   FOR_EACH_CHILD (die, c, output_die (c));
10630
10631   /* Add null byte to terminate sibling list.  */
10632   if (die->die_child != NULL)
10633     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
10634                          (unsigned long) die->die_offset);
10635 }
10636
10637 /* Output the compilation unit that appears at the beginning of the
10638    .debug_info section, and precedes the DIE descriptions.  */
10639
10640 static void
10641 output_compilation_unit_header (void)
10642 {
10643   int ver = dwarf_version;
10644
10645   /* Don't mark the output as DWARF-4 until we make full use of the
10646      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
10647      selects only a few extensions from the DWARF-4 spec.  */
10648   if (ver > 3)
10649     ver = 3;
10650   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10651     dw2_asm_output_data (4, 0xffffffff,
10652       "Initial length escape value indicating 64-bit DWARF extension");
10653   dw2_asm_output_data (DWARF_OFFSET_SIZE,
10654                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10655                        "Length of Compilation Unit Info");
10656   dw2_asm_output_data (2, ver, "DWARF version number");
10657   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10658                          debug_abbrev_section,
10659                          "Offset Into Abbrev. Section");
10660   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10661 }
10662
10663 /* Output the compilation unit DIE and its children.  */
10664
10665 static void
10666 output_comp_unit (dw_die_ref die, int output_if_empty)
10667 {
10668   const char *secname;
10669   char *oldsym, *tmp;
10670
10671   /* Unless we are outputting main CU, we may throw away empty ones.  */
10672   if (!output_if_empty && die->die_child == NULL)
10673     return;
10674
10675   /* Even if there are no children of this DIE, we must output the information
10676      about the compilation unit.  Otherwise, on an empty translation unit, we
10677      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
10678      will then complain when examining the file.  First mark all the DIEs in
10679      this CU so we know which get local refs.  */
10680   mark_dies (die);
10681
10682   build_abbrev_table (die);
10683
10684   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10685   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10686   calc_die_sizes (die);
10687
10688   oldsym = die->die_id.die_symbol;
10689   if (oldsym)
10690     {
10691       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10692
10693       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10694       secname = tmp;
10695       die->die_id.die_symbol = NULL;
10696       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10697     }
10698   else
10699     switch_to_section (debug_info_section);
10700
10701   /* Output debugging information.  */
10702   output_compilation_unit_header ();
10703   output_die (die);
10704
10705   /* Leave the marks on the main CU, so we can check them in
10706      output_pubnames.  */
10707   if (oldsym)
10708     {
10709       unmark_dies (die);
10710       die->die_id.die_symbol = oldsym;
10711     }
10712 }
10713
10714 /* Output a comdat type unit DIE and its children.  */
10715
10716 static void
10717 output_comdat_type_unit (comdat_type_node *node)
10718 {
10719   const char *secname;
10720   char *tmp;
10721   int i;
10722 #if defined (OBJECT_FORMAT_ELF)
10723   tree comdat_key;
10724 #endif
10725
10726   /* First mark all the DIEs in this CU so we know which get local refs.  */
10727   mark_dies (node->root_die);
10728
10729   build_abbrev_table (node->root_die);
10730
10731   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10732   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10733   calc_die_sizes (node->root_die);
10734
10735 #if defined (OBJECT_FORMAT_ELF)
10736   secname = ".debug_types";
10737   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10738   sprintf (tmp, "wt.");
10739   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10740     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10741   comdat_key = get_identifier (tmp);
10742   targetm.asm_out.named_section (secname,
10743                                  SECTION_DEBUG | SECTION_LINKONCE,
10744                                  comdat_key);
10745 #else
10746   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10747   sprintf (tmp, ".gnu.linkonce.wt.");
10748   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10749     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10750   secname = tmp;
10751   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10752 #endif
10753
10754   /* Output debugging information.  */
10755   output_compilation_unit_header ();
10756   output_signature (node->signature, "Type Signature");
10757   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10758                        "Offset to Type DIE");
10759   output_die (node->root_die);
10760
10761   unmark_dies (node->root_die);
10762 }
10763
10764 /* Return the DWARF2/3 pubname associated with a decl.  */
10765
10766 static const char *
10767 dwarf2_name (tree decl, int scope)
10768 {
10769   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10770 }
10771
10772 /* Add a new entry to .debug_pubnames if appropriate.  */
10773
10774 static void
10775 add_pubname_string (const char *str, dw_die_ref die)
10776 {
10777   pubname_entry e;
10778
10779   e.die = die;
10780   e.name = xstrdup (str);
10781   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
10782 }
10783
10784 static void
10785 add_pubname (tree decl, dw_die_ref die)
10786 {
10787   if (TREE_PUBLIC (decl))
10788     {
10789       const char *name = dwarf2_name (decl, 1);
10790       if (name)
10791         add_pubname_string (name, die);
10792     }
10793 }
10794
10795 /* Add a new entry to .debug_pubtypes if appropriate.  */
10796
10797 static void
10798 add_pubtype (tree decl, dw_die_ref die)
10799 {
10800   pubname_entry e;
10801
10802   e.name = NULL;
10803   if ((TREE_PUBLIC (decl)
10804        || die->die_parent == comp_unit_die)
10805       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10806     {
10807       e.die = die;
10808       if (TYPE_P (decl))
10809         {
10810           if (TYPE_NAME (decl))
10811             {
10812               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
10813                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
10814               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
10815                        && DECL_NAME (TYPE_NAME (decl)))
10816                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
10817               else
10818                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
10819             }
10820         }
10821       else
10822         {
10823           e.name = dwarf2_name (decl, 1);
10824           if (e.name)
10825             e.name = xstrdup (e.name);
10826         }
10827
10828       /* If we don't have a name for the type, there's no point in adding
10829          it to the table.  */
10830       if (e.name && e.name[0] != '\0')
10831         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
10832     }
10833 }
10834
10835 /* Output the public names table used to speed up access to externally
10836    visible names; or the public types table used to find type definitions.  */
10837
10838 static void
10839 output_pubnames (VEC (pubname_entry, gc) * names)
10840 {
10841   unsigned i;
10842   unsigned long pubnames_length = size_of_pubnames (names);
10843   pubname_ref pub;
10844
10845   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10846     dw2_asm_output_data (4, 0xffffffff,
10847       "Initial length escape value indicating 64-bit DWARF extension");
10848   if (names == pubname_table)
10849     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10850                          "Length of Public Names Info");
10851   else
10852     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10853                          "Length of Public Type Names Info");
10854   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
10855   dw2_asm_output_data (2, 2, "DWARF Version");
10856   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10857                          debug_info_section,
10858                          "Offset of Compilation Unit Info");
10859   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10860                        "Compilation Unit Length");
10861
10862   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
10863     {
10864       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
10865       if (names == pubname_table)
10866         gcc_assert (pub->die->die_mark);
10867
10868       if (names != pubtype_table
10869           || pub->die->die_offset != 0
10870           || !flag_eliminate_unused_debug_types)
10871         {
10872           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
10873                                "DIE offset");
10874
10875           dw2_asm_output_nstring (pub->name, -1, "external name");
10876         }
10877     }
10878
10879   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10880 }
10881
10882 /* Add a new entry to .debug_aranges if appropriate.  */
10883
10884 static void
10885 add_arange (tree decl, dw_die_ref die)
10886 {
10887   if (! DECL_SECTION_NAME (decl))
10888     return;
10889
10890   if (arange_table_in_use == arange_table_allocated)
10891     {
10892       arange_table_allocated += ARANGE_TABLE_INCREMENT;
10893       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
10894                                     arange_table_allocated);
10895       memset (arange_table + arange_table_in_use, 0,
10896               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
10897     }
10898
10899   arange_table[arange_table_in_use++] = die;
10900 }
10901
10902 /* Output the information that goes into the .debug_aranges table.
10903    Namely, define the beginning and ending address range of the
10904    text section generated for this compilation unit.  */
10905
10906 static void
10907 output_aranges (void)
10908 {
10909   unsigned i;
10910   unsigned long aranges_length = size_of_aranges ();
10911
10912   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10913     dw2_asm_output_data (4, 0xffffffff,
10914       "Initial length escape value indicating 64-bit DWARF extension");
10915   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10916                        "Length of Address Ranges Info");
10917   /* Version number for aranges is still 2, even in DWARF3.  */
10918   dw2_asm_output_data (2, 2, "DWARF Version");
10919   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10920                          debug_info_section,
10921                          "Offset of Compilation Unit Info");
10922   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10923   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10924
10925   /* We need to align to twice the pointer size here.  */
10926   if (DWARF_ARANGES_PAD_SIZE)
10927     {
10928       /* Pad using a 2 byte words so that padding is correct for any
10929          pointer size.  */
10930       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10931                            2 * DWARF2_ADDR_SIZE);
10932       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10933         dw2_asm_output_data (2, 0, NULL);
10934     }
10935
10936   /* It is necessary not to output these entries if the sections were
10937      not used; if the sections were not used, the length will be 0 and
10938      the address may end up as 0 if the section is discarded by ld
10939      --gc-sections, leaving an invalid (0, 0) entry that can be
10940      confused with the terminator.  */
10941   if (text_section_used)
10942     {
10943       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10944       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10945                             text_section_label, "Length");
10946     }
10947   if (cold_text_section_used)
10948     {
10949       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10950                            "Address");
10951       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10952                             cold_text_section_label, "Length");
10953     }
10954
10955   for (i = 0; i < arange_table_in_use; i++)
10956     {
10957       dw_die_ref die = arange_table[i];
10958
10959       /* We shouldn't see aranges for DIEs outside of the main CU.  */
10960       gcc_assert (die->die_mark);
10961
10962       if (die->die_tag == DW_TAG_subprogram)
10963         {
10964           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
10965                                "Address");
10966           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
10967                                 get_AT_low_pc (die), "Length");
10968         }
10969       else
10970         {
10971           /* A static variable; extract the symbol from DW_AT_location.
10972              Note that this code isn't currently hit, as we only emit
10973              aranges for functions (jason 9/23/99).  */
10974           dw_attr_ref a = get_AT (die, DW_AT_location);
10975           dw_loc_descr_ref loc;
10976
10977           gcc_assert (a && AT_class (a) == dw_val_class_loc);
10978
10979           loc = AT_loc (a);
10980           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
10981
10982           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
10983                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
10984           dw2_asm_output_data (DWARF2_ADDR_SIZE,
10985                                get_AT_unsigned (die, DW_AT_byte_size),
10986                                "Length");
10987         }
10988     }
10989
10990   /* Output the terminator words.  */
10991   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10992   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10993 }
10994
10995 /* Add a new entry to .debug_ranges.  Return the offset at which it
10996    was placed.  */
10997
10998 static unsigned int
10999 add_ranges_num (int num)
11000 {
11001   unsigned int in_use = ranges_table_in_use;
11002
11003   if (in_use == ranges_table_allocated)
11004     {
11005       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11006       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11007                                     ranges_table_allocated);
11008       memset (ranges_table + ranges_table_in_use, 0,
11009               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11010     }
11011
11012   ranges_table[in_use].num = num;
11013   ranges_table_in_use = in_use + 1;
11014
11015   return in_use * 2 * DWARF2_ADDR_SIZE;
11016 }
11017
11018 /* Add a new entry to .debug_ranges corresponding to a block, or a
11019    range terminator if BLOCK is NULL.  */
11020
11021 static unsigned int
11022 add_ranges (const_tree block)
11023 {
11024   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11025 }
11026
11027 /* Add a new entry to .debug_ranges corresponding to a pair of
11028    labels.  */
11029
11030 static void
11031 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11032                       bool *added)
11033 {
11034   unsigned int in_use = ranges_by_label_in_use;
11035   unsigned int offset;
11036
11037   if (in_use == ranges_by_label_allocated)
11038     {
11039       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11040       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11041                                        ranges_by_label,
11042                                        ranges_by_label_allocated);
11043       memset (ranges_by_label + ranges_by_label_in_use, 0,
11044               RANGES_TABLE_INCREMENT
11045               * sizeof (struct dw_ranges_by_label_struct));
11046     }
11047
11048   ranges_by_label[in_use].begin = begin;
11049   ranges_by_label[in_use].end = end;
11050   ranges_by_label_in_use = in_use + 1;
11051
11052   offset = add_ranges_num (-(int)in_use - 1);
11053   if (!*added)
11054     {
11055       add_AT_range_list (die, DW_AT_ranges, offset);
11056       *added = true;
11057     }
11058 }
11059
11060 static void
11061 output_ranges (void)
11062 {
11063   unsigned i;
11064   static const char *const start_fmt = "Offset 0x%x";
11065   const char *fmt = start_fmt;
11066
11067   for (i = 0; i < ranges_table_in_use; i++)
11068     {
11069       int block_num = ranges_table[i].num;
11070
11071       if (block_num > 0)
11072         {
11073           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11074           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11075
11076           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11077           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11078
11079           /* If all code is in the text section, then the compilation
11080              unit base address defaults to DW_AT_low_pc, which is the
11081              base of the text section.  */
11082           if (!have_multiple_function_sections)
11083             {
11084               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11085                                     text_section_label,
11086                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11087               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11088                                     text_section_label, NULL);
11089             }
11090
11091           /* Otherwise, the compilation unit base address is zero,
11092              which allows us to use absolute addresses, and not worry
11093              about whether the target supports cross-section
11094              arithmetic.  */
11095           else
11096             {
11097               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11098                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11099               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11100             }
11101
11102           fmt = NULL;
11103         }
11104
11105       /* Negative block_num stands for an index into ranges_by_label.  */
11106       else if (block_num < 0)
11107         {
11108           int lab_idx = - block_num - 1;
11109
11110           if (!have_multiple_function_sections)
11111             {
11112               gcc_unreachable ();
11113 #if 0
11114               /* If we ever use add_ranges_by_labels () for a single
11115                  function section, all we have to do is to take out
11116                  the #if 0 above.  */
11117               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11118                                     ranges_by_label[lab_idx].begin,
11119                                     text_section_label,
11120                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11121               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11122                                     ranges_by_label[lab_idx].end,
11123                                     text_section_label, NULL);
11124 #endif
11125             }
11126           else
11127             {
11128               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11129                                    ranges_by_label[lab_idx].begin,
11130                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11131               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11132                                    ranges_by_label[lab_idx].end,
11133                                    NULL);
11134             }
11135         }
11136       else
11137         {
11138           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11139           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11140           fmt = start_fmt;
11141         }
11142     }
11143 }
11144
11145 /* Data structure containing information about input files.  */
11146 struct file_info
11147 {
11148   const char *path;     /* Complete file name.  */
11149   const char *fname;    /* File name part.  */
11150   int length;           /* Length of entire string.  */
11151   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11152   int dir_idx;          /* Index in directory table.  */
11153 };
11154
11155 /* Data structure containing information about directories with source
11156    files.  */
11157 struct dir_info
11158 {
11159   const char *path;     /* Path including directory name.  */
11160   int length;           /* Path length.  */
11161   int prefix;           /* Index of directory entry which is a prefix.  */
11162   int count;            /* Number of files in this directory.  */
11163   int dir_idx;          /* Index of directory used as base.  */
11164 };
11165
11166 /* Callback function for file_info comparison.  We sort by looking at
11167    the directories in the path.  */
11168
11169 static int
11170 file_info_cmp (const void *p1, const void *p2)
11171 {
11172   const struct file_info *const s1 = (const struct file_info *) p1;
11173   const struct file_info *const s2 = (const struct file_info *) p2;
11174   const unsigned char *cp1;
11175   const unsigned char *cp2;
11176
11177   /* Take care of file names without directories.  We need to make sure that
11178      we return consistent values to qsort since some will get confused if
11179      we return the same value when identical operands are passed in opposite
11180      orders.  So if neither has a directory, return 0 and otherwise return
11181      1 or -1 depending on which one has the directory.  */
11182   if ((s1->path == s1->fname || s2->path == s2->fname))
11183     return (s2->path == s2->fname) - (s1->path == s1->fname);
11184
11185   cp1 = (const unsigned char *) s1->path;
11186   cp2 = (const unsigned char *) s2->path;
11187
11188   while (1)
11189     {
11190       ++cp1;
11191       ++cp2;
11192       /* Reached the end of the first path?  If so, handle like above.  */
11193       if ((cp1 == (const unsigned char *) s1->fname)
11194           || (cp2 == (const unsigned char *) s2->fname))
11195         return ((cp2 == (const unsigned char *) s2->fname)
11196                 - (cp1 == (const unsigned char *) s1->fname));
11197
11198       /* Character of current path component the same?  */
11199       else if (*cp1 != *cp2)
11200         return *cp1 - *cp2;
11201     }
11202 }
11203
11204 struct file_name_acquire_data
11205 {
11206   struct file_info *files;
11207   int used_files;
11208   int max_files;
11209 };
11210
11211 /* Traversal function for the hash table.  */
11212
11213 static int
11214 file_name_acquire (void ** slot, void *data)
11215 {
11216   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11217   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11218   struct file_info *fi;
11219   const char *f;
11220
11221   gcc_assert (fnad->max_files >= d->emitted_number);
11222
11223   if (! d->emitted_number)
11224     return 1;
11225
11226   gcc_assert (fnad->max_files != fnad->used_files);
11227
11228   fi = fnad->files + fnad->used_files++;
11229
11230   /* Skip all leading "./".  */
11231   f = d->filename;
11232   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11233     f += 2;
11234
11235   /* Create a new array entry.  */
11236   fi->path = f;
11237   fi->length = strlen (f);
11238   fi->file_idx = d;
11239
11240   /* Search for the file name part.  */
11241   f = strrchr (f, DIR_SEPARATOR);
11242 #if defined (DIR_SEPARATOR_2)
11243   {
11244     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11245
11246     if (g != NULL)
11247       {
11248         if (f == NULL || f < g)
11249           f = g;
11250       }
11251   }
11252 #endif
11253
11254   fi->fname = f == NULL ? fi->path : f + 1;
11255   return 1;
11256 }
11257
11258 /* Output the directory table and the file name table.  We try to minimize
11259    the total amount of memory needed.  A heuristic is used to avoid large
11260    slowdowns with many input files.  */
11261
11262 static void
11263 output_file_names (void)
11264 {
11265   struct file_name_acquire_data fnad;
11266   int numfiles;
11267   struct file_info *files;
11268   struct dir_info *dirs;
11269   int *saved;
11270   int *savehere;
11271   int *backmap;
11272   int ndirs;
11273   int idx_offset;
11274   int i;
11275
11276   if (!last_emitted_file)
11277     {
11278       dw2_asm_output_data (1, 0, "End directory table");
11279       dw2_asm_output_data (1, 0, "End file name table");
11280       return;
11281     }
11282
11283   numfiles = last_emitted_file->emitted_number;
11284
11285   /* Allocate the various arrays we need.  */
11286   files = XALLOCAVEC (struct file_info, numfiles);
11287   dirs = XALLOCAVEC (struct dir_info, numfiles);
11288
11289   fnad.files = files;
11290   fnad.used_files = 0;
11291   fnad.max_files = numfiles;
11292   htab_traverse (file_table, file_name_acquire, &fnad);
11293   gcc_assert (fnad.used_files == fnad.max_files);
11294
11295   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11296
11297   /* Find all the different directories used.  */
11298   dirs[0].path = files[0].path;
11299   dirs[0].length = files[0].fname - files[0].path;
11300   dirs[0].prefix = -1;
11301   dirs[0].count = 1;
11302   dirs[0].dir_idx = 0;
11303   files[0].dir_idx = 0;
11304   ndirs = 1;
11305
11306   for (i = 1; i < numfiles; i++)
11307     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11308         && memcmp (dirs[ndirs - 1].path, files[i].path,
11309                    dirs[ndirs - 1].length) == 0)
11310       {
11311         /* Same directory as last entry.  */
11312         files[i].dir_idx = ndirs - 1;
11313         ++dirs[ndirs - 1].count;
11314       }
11315     else
11316       {
11317         int j;
11318
11319         /* This is a new directory.  */
11320         dirs[ndirs].path = files[i].path;
11321         dirs[ndirs].length = files[i].fname - files[i].path;
11322         dirs[ndirs].count = 1;
11323         dirs[ndirs].dir_idx = ndirs;
11324         files[i].dir_idx = ndirs;
11325
11326         /* Search for a prefix.  */
11327         dirs[ndirs].prefix = -1;
11328         for (j = 0; j < ndirs; j++)
11329           if (dirs[j].length < dirs[ndirs].length
11330               && dirs[j].length > 1
11331               && (dirs[ndirs].prefix == -1
11332                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11333               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11334             dirs[ndirs].prefix = j;
11335
11336         ++ndirs;
11337       }
11338
11339   /* Now to the actual work.  We have to find a subset of the directories which
11340      allow expressing the file name using references to the directory table
11341      with the least amount of characters.  We do not do an exhaustive search
11342      where we would have to check out every combination of every single
11343      possible prefix.  Instead we use a heuristic which provides nearly optimal
11344      results in most cases and never is much off.  */
11345   saved = XALLOCAVEC (int, ndirs);
11346   savehere = XALLOCAVEC (int, ndirs);
11347
11348   memset (saved, '\0', ndirs * sizeof (saved[0]));
11349   for (i = 0; i < ndirs; i++)
11350     {
11351       int j;
11352       int total;
11353
11354       /* We can always save some space for the current directory.  But this
11355          does not mean it will be enough to justify adding the directory.  */
11356       savehere[i] = dirs[i].length;
11357       total = (savehere[i] - saved[i]) * dirs[i].count;
11358
11359       for (j = i + 1; j < ndirs; j++)
11360         {
11361           savehere[j] = 0;
11362           if (saved[j] < dirs[i].length)
11363             {
11364               /* Determine whether the dirs[i] path is a prefix of the
11365                  dirs[j] path.  */
11366               int k;
11367
11368               k = dirs[j].prefix;
11369               while (k != -1 && k != (int) i)
11370                 k = dirs[k].prefix;
11371
11372               if (k == (int) i)
11373                 {
11374                   /* Yes it is.  We can possibly save some memory by
11375                      writing the filenames in dirs[j] relative to
11376                      dirs[i].  */
11377                   savehere[j] = dirs[i].length;
11378                   total += (savehere[j] - saved[j]) * dirs[j].count;
11379                 }
11380             }
11381         }
11382
11383       /* Check whether we can save enough to justify adding the dirs[i]
11384          directory.  */
11385       if (total > dirs[i].length + 1)
11386         {
11387           /* It's worthwhile adding.  */
11388           for (j = i; j < ndirs; j++)
11389             if (savehere[j] > 0)
11390               {
11391                 /* Remember how much we saved for this directory so far.  */
11392                 saved[j] = savehere[j];
11393
11394                 /* Remember the prefix directory.  */
11395                 dirs[j].dir_idx = i;
11396               }
11397         }
11398     }
11399
11400   /* Emit the directory name table.  */
11401   idx_offset = dirs[0].length > 0 ? 1 : 0;
11402   for (i = 1 - idx_offset; i < ndirs; i++)
11403     dw2_asm_output_nstring (dirs[i].path,
11404                             dirs[i].length
11405                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11406                             "Directory Entry: 0x%x", i + idx_offset);
11407
11408   dw2_asm_output_data (1, 0, "End directory table");
11409
11410   /* We have to emit them in the order of emitted_number since that's
11411      used in the debug info generation.  To do this efficiently we
11412      generate a back-mapping of the indices first.  */
11413   backmap = XALLOCAVEC (int, numfiles);
11414   for (i = 0; i < numfiles; i++)
11415     backmap[files[i].file_idx->emitted_number - 1] = i;
11416
11417   /* Now write all the file names.  */
11418   for (i = 0; i < numfiles; i++)
11419     {
11420       int file_idx = backmap[i];
11421       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11422
11423 #ifdef VMS_DEBUGGING_INFO
11424 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11425
11426       /* Setting these fields can lead to debugger miscomparisons,
11427          but VMS Debug requires them to be set correctly.  */
11428
11429       int ver;
11430       long long cdt;
11431       long siz;
11432       int maxfilelen = strlen (files[file_idx].path)
11433                                + dirs[dir_idx].length
11434                                + MAX_VMS_VERSION_LEN + 1;
11435       char *filebuf = XALLOCAVEC (char, maxfilelen);
11436
11437       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11438       snprintf (filebuf, maxfilelen, "%s;%d",
11439                 files[file_idx].path + dirs[dir_idx].length, ver);
11440
11441       dw2_asm_output_nstring
11442         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
11443
11444       /* Include directory index.  */
11445       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11446
11447       /* Modification time.  */
11448       dw2_asm_output_data_uleb128
11449         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11450           ? cdt : 0,
11451          NULL);
11452
11453       /* File length in bytes.  */
11454       dw2_asm_output_data_uleb128
11455         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11456           ? siz : 0,
11457          NULL);
11458 #else
11459       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11460                               "File Entry: 0x%x", (unsigned) i + 1);
11461
11462       /* Include directory index.  */
11463       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11464
11465       /* Modification time.  */
11466       dw2_asm_output_data_uleb128 (0, NULL);
11467
11468       /* File length in bytes.  */
11469       dw2_asm_output_data_uleb128 (0, NULL);
11470 #endif
11471     }
11472
11473   dw2_asm_output_data (1, 0, "End file name table");
11474 }
11475
11476
11477 /* Output the source line number correspondence information.  This
11478    information goes into the .debug_line section.  */
11479
11480 static void
11481 output_line_info (void)
11482 {
11483   char l1[20], l2[20], p1[20], p2[20];
11484   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11485   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11486   unsigned opc;
11487   unsigned n_op_args;
11488   unsigned long lt_index;
11489   unsigned long current_line;
11490   long line_offset;
11491   long line_delta;
11492   unsigned long current_file;
11493   unsigned long function;
11494   int ver = dwarf_version;
11495
11496   /* Don't mark the output as DWARF-4 until we make full use of the
11497      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
11498      selects only a few extensions from the DWARF-4 spec.  */
11499   if (ver > 3)
11500     ver = 3;
11501
11502   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11503   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11504   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11505   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11506
11507   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11508     dw2_asm_output_data (4, 0xffffffff,
11509       "Initial length escape value indicating 64-bit DWARF extension");
11510   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11511                         "Length of Source Line Info");
11512   ASM_OUTPUT_LABEL (asm_out_file, l1);
11513
11514   dw2_asm_output_data (2, ver, "DWARF Version");
11515   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11516   ASM_OUTPUT_LABEL (asm_out_file, p1);
11517
11518   /* Define the architecture-dependent minimum instruction length (in
11519    bytes).  In this implementation of DWARF, this field is used for
11520    information purposes only.  Since GCC generates assembly language,
11521    we have no a priori knowledge of how many instruction bytes are
11522    generated for each source line, and therefore can use only the
11523    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11524    commands.  Accordingly, we fix this as `1', which is "correct
11525    enough" for all architectures, and don't let the target override.  */
11526   dw2_asm_output_data (1, 1,
11527                        "Minimum Instruction Length");
11528
11529   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11530                        "Default is_stmt_start flag");
11531   dw2_asm_output_data (1, DWARF_LINE_BASE,
11532                        "Line Base Value (Special Opcodes)");
11533   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11534                        "Line Range Value (Special Opcodes)");
11535   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11536                        "Special Opcode Base");
11537
11538   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11539     {
11540       switch (opc)
11541         {
11542         case DW_LNS_advance_pc:
11543         case DW_LNS_advance_line:
11544         case DW_LNS_set_file:
11545         case DW_LNS_set_column:
11546         case DW_LNS_fixed_advance_pc:
11547           n_op_args = 1;
11548           break;
11549         default:
11550           n_op_args = 0;
11551           break;
11552         }
11553
11554       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
11555                            opc, n_op_args);
11556     }
11557
11558   /* Write out the information about the files we use.  */
11559   output_file_names ();
11560   ASM_OUTPUT_LABEL (asm_out_file, p2);
11561
11562   /* We used to set the address register to the first location in the text
11563      section here, but that didn't accomplish anything since we already
11564      have a line note for the opening brace of the first function.  */
11565
11566   /* Generate the line number to PC correspondence table, encoded as
11567      a series of state machine operations.  */
11568   current_file = 1;
11569   current_line = 1;
11570
11571   if (cfun && in_cold_section_p)
11572     strcpy (prev_line_label, crtl->subsections.cold_section_label);
11573   else
11574     strcpy (prev_line_label, text_section_label);
11575   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
11576     {
11577       dw_line_info_ref line_info = &line_info_table[lt_index];
11578
11579 #if 0
11580       /* Disable this optimization for now; GDB wants to see two line notes
11581          at the beginning of a function so it can find the end of the
11582          prologue.  */
11583
11584       /* Don't emit anything for redundant notes.  Just updating the
11585          address doesn't accomplish anything, because we already assume
11586          that anything after the last address is this line.  */
11587       if (line_info->dw_line_num == current_line
11588           && line_info->dw_file_num == current_file)
11589         continue;
11590 #endif
11591
11592       /* Emit debug info for the address of the current line.
11593
11594          Unfortunately, we have little choice here currently, and must always
11595          use the most general form.  GCC does not know the address delta
11596          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
11597          attributes which will give an upper bound on the address range.  We
11598          could perhaps use length attributes to determine when it is safe to
11599          use DW_LNS_fixed_advance_pc.  */
11600
11601       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
11602       if (0)
11603         {
11604           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
11605           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11606                                "DW_LNS_fixed_advance_pc");
11607           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11608         }
11609       else
11610         {
11611           /* This can handle any delta.  This takes
11612              4+DWARF2_ADDR_SIZE bytes.  */
11613           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11614           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11615           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11616           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11617         }
11618
11619       strcpy (prev_line_label, line_label);
11620
11621       /* Emit debug info for the source file of the current line, if
11622          different from the previous line.  */
11623       if (line_info->dw_file_num != current_file)
11624         {
11625           current_file = line_info->dw_file_num;
11626           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11627           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11628         }
11629
11630       /* Emit debug info for the current line number, choosing the encoding
11631          that uses the least amount of space.  */
11632       if (line_info->dw_line_num != current_line)
11633         {
11634           line_offset = line_info->dw_line_num - current_line;
11635           line_delta = line_offset - DWARF_LINE_BASE;
11636           current_line = line_info->dw_line_num;
11637           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11638             /* This can handle deltas from -10 to 234, using the current
11639                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
11640                takes 1 byte.  */
11641             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11642                                  "line %lu", current_line);
11643           else
11644             {
11645               /* This can handle any delta.  This takes at least 4 bytes,
11646                  depending on the value being encoded.  */
11647               dw2_asm_output_data (1, DW_LNS_advance_line,
11648                                    "advance to line %lu", current_line);
11649               dw2_asm_output_data_sleb128 (line_offset, NULL);
11650               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11651             }
11652         }
11653       else
11654         /* We still need to start a new row, so output a copy insn.  */
11655         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11656     }
11657
11658   /* Emit debug info for the address of the end of the function.  */
11659   if (0)
11660     {
11661       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11662                            "DW_LNS_fixed_advance_pc");
11663       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
11664     }
11665   else
11666     {
11667       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11668       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11669       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11670       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
11671     }
11672
11673   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11674   dw2_asm_output_data_uleb128 (1, NULL);
11675   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11676
11677   function = 0;
11678   current_file = 1;
11679   current_line = 1;
11680   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
11681     {
11682       dw_separate_line_info_ref line_info
11683         = &separate_line_info_table[lt_index];
11684
11685 #if 0
11686       /* Don't emit anything for redundant notes.  */
11687       if (line_info->dw_line_num == current_line
11688           && line_info->dw_file_num == current_file
11689           && line_info->function == function)
11690         goto cont;
11691 #endif
11692
11693       /* Emit debug info for the address of the current line.  If this is
11694          a new function, or the first line of a function, then we need
11695          to handle it differently.  */
11696       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
11697                                    lt_index);
11698       if (function != line_info->function)
11699         {
11700           function = line_info->function;
11701
11702           /* Set the address register to the first line in the function.  */
11703           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11704           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11705           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11706           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11707         }
11708       else
11709         {
11710           /* ??? See the DW_LNS_advance_pc comment above.  */
11711           if (0)
11712             {
11713               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11714                                    "DW_LNS_fixed_advance_pc");
11715               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11716             }
11717           else
11718             {
11719               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11720               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11721               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11722               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11723             }
11724         }
11725
11726       strcpy (prev_line_label, line_label);
11727
11728       /* Emit debug info for the source file of the current line, if
11729          different from the previous line.  */
11730       if (line_info->dw_file_num != current_file)
11731         {
11732           current_file = line_info->dw_file_num;
11733           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11734           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11735         }
11736
11737       /* Emit debug info for the current line number, choosing the encoding
11738          that uses the least amount of space.  */
11739       if (line_info->dw_line_num != current_line)
11740         {
11741           line_offset = line_info->dw_line_num - current_line;
11742           line_delta = line_offset - DWARF_LINE_BASE;
11743           current_line = line_info->dw_line_num;
11744           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11745             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11746                                  "line %lu", current_line);
11747           else
11748             {
11749               dw2_asm_output_data (1, DW_LNS_advance_line,
11750                                    "advance to line %lu", current_line);
11751               dw2_asm_output_data_sleb128 (line_offset, NULL);
11752               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11753             }
11754         }
11755       else
11756         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11757
11758 #if 0
11759     cont:
11760 #endif
11761
11762       lt_index++;
11763
11764       /* If we're done with a function, end its sequence.  */
11765       if (lt_index == separate_line_info_table_in_use
11766           || separate_line_info_table[lt_index].function != function)
11767         {
11768           current_file = 1;
11769           current_line = 1;
11770
11771           /* Emit debug info for the address of the end of the function.  */
11772           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
11773           if (0)
11774             {
11775               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11776                                    "DW_LNS_fixed_advance_pc");
11777               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11778             }
11779           else
11780             {
11781               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11782               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11783               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11784               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11785             }
11786
11787           /* Output the marker for the end of this sequence.  */
11788           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11789           dw2_asm_output_data_uleb128 (1, NULL);
11790           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11791         }
11792     }
11793
11794   /* Output the marker for the end of the line number info.  */
11795   ASM_OUTPUT_LABEL (asm_out_file, l2);
11796 }
11797
11798 /* Return the size of the .debug_dcall table for the compilation unit.  */
11799
11800 static unsigned long
11801 size_of_dcall_table (void)
11802 {
11803   unsigned long size;
11804   unsigned int i;
11805   dcall_entry *p;
11806   tree last_poc_decl = NULL;
11807
11808   /* Header:  version + debug info section pointer + pointer size.  */
11809   size = 2 + DWARF_OFFSET_SIZE + 1;
11810
11811   /* Each entry:  code label + DIE offset.  */
11812   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11813     {
11814       gcc_assert (p->targ_die != NULL);
11815       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11816       if (p->poc_decl != last_poc_decl)
11817         {
11818           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11819           gcc_assert (poc_die);
11820           last_poc_decl = p->poc_decl;
11821           if (poc_die)
11822             size += (DWARF_OFFSET_SIZE
11823                      + size_of_uleb128 (poc_die->die_offset));
11824         }
11825       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
11826     }
11827
11828   return size;
11829 }
11830
11831 /* Output the direct call table used to disambiguate PC values when
11832    identical function have been merged.  */
11833
11834 static void
11835 output_dcall_table (void)
11836 {
11837   unsigned i;
11838   unsigned long dcall_length = size_of_dcall_table ();
11839   dcall_entry *p;
11840   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11841   tree last_poc_decl = NULL;
11842
11843   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11844     dw2_asm_output_data (4, 0xffffffff,
11845       "Initial length escape value indicating 64-bit DWARF extension");
11846   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
11847                        "Length of Direct Call Table");
11848   dw2_asm_output_data (2, 4, "Version number");
11849   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11850                          debug_info_section,
11851                          "Offset of Compilation Unit Info");
11852   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11853
11854   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11855     {
11856       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11857       if (p->poc_decl != last_poc_decl)
11858         {
11859           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11860           last_poc_decl = p->poc_decl;
11861           if (poc_die)
11862             {
11863               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
11864               dw2_asm_output_data_uleb128 (poc_die->die_offset,
11865                                            "Caller DIE offset");
11866             }
11867         }
11868       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11869       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11870       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
11871                                    "Callee DIE offset");
11872     }
11873 }
11874 \f
11875 /* Return the size of the .debug_vcall table for the compilation unit.  */
11876
11877 static unsigned long
11878 size_of_vcall_table (void)
11879 {
11880   unsigned long size;
11881   unsigned int i;
11882   vcall_entry *p;
11883
11884   /* Header:  version + pointer size.  */
11885   size = 2 + 1;
11886
11887   /* Each entry:  code label + vtable slot index.  */
11888   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11889     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
11890
11891   return size;
11892 }
11893
11894 /* Output the virtual call table used to disambiguate PC values when
11895    identical function have been merged.  */
11896
11897 static void
11898 output_vcall_table (void)
11899 {
11900   unsigned i;
11901   unsigned long vcall_length = size_of_vcall_table ();
11902   vcall_entry *p;
11903   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11904
11905   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11906     dw2_asm_output_data (4, 0xffffffff,
11907       "Initial length escape value indicating 64-bit DWARF extension");
11908   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
11909                        "Length of Virtual Call Table");
11910   dw2_asm_output_data (2, 4, "Version number");
11911   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11912
11913   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11914     {
11915       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11916       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11917       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
11918     }
11919 }
11920 \f
11921 /* Given a pointer to a tree node for some base type, return a pointer to
11922    a DIE that describes the given type.
11923
11924    This routine must only be called for GCC type nodes that correspond to
11925    Dwarf base (fundamental) types.  */
11926
11927 static dw_die_ref
11928 base_type_die (tree type)
11929 {
11930   dw_die_ref base_type_result;
11931   enum dwarf_type encoding;
11932
11933   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11934     return 0;
11935
11936   /* If this is a subtype that should not be emitted as a subrange type,
11937      use the base type.  See subrange_type_for_debug_p.  */
11938   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11939     type = TREE_TYPE (type);
11940
11941   switch (TREE_CODE (type))
11942     {
11943     case INTEGER_TYPE:
11944       if (TYPE_STRING_FLAG (type))
11945         {
11946           if (TYPE_UNSIGNED (type))
11947             encoding = DW_ATE_unsigned_char;
11948           else
11949             encoding = DW_ATE_signed_char;
11950         }
11951       else if (TYPE_UNSIGNED (type))
11952         encoding = DW_ATE_unsigned;
11953       else
11954         encoding = DW_ATE_signed;
11955       break;
11956
11957     case REAL_TYPE:
11958       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
11959         {
11960           if (dwarf_version >= 3 || !dwarf_strict)
11961             encoding = DW_ATE_decimal_float;
11962           else
11963             encoding = DW_ATE_lo_user;
11964         }
11965       else
11966         encoding = DW_ATE_float;
11967       break;
11968
11969     case FIXED_POINT_TYPE:
11970       if (!(dwarf_version >= 3 || !dwarf_strict))
11971         encoding = DW_ATE_lo_user;
11972       else if (TYPE_UNSIGNED (type))
11973         encoding = DW_ATE_unsigned_fixed;
11974       else
11975         encoding = DW_ATE_signed_fixed;
11976       break;
11977
11978       /* Dwarf2 doesn't know anything about complex ints, so use
11979          a user defined type for it.  */
11980     case COMPLEX_TYPE:
11981       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
11982         encoding = DW_ATE_complex_float;
11983       else
11984         encoding = DW_ATE_lo_user;
11985       break;
11986
11987     case BOOLEAN_TYPE:
11988       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
11989       encoding = DW_ATE_boolean;
11990       break;
11991
11992     default:
11993       /* No other TREE_CODEs are Dwarf fundamental types.  */
11994       gcc_unreachable ();
11995     }
11996
11997   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
11998
11999   /* This probably indicates a bug.  */
12000   if (! TYPE_NAME (type))
12001     add_name_attribute (base_type_result, "__unknown__");
12002
12003   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12004                    int_size_in_bytes (type));
12005   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12006
12007   return base_type_result;
12008 }
12009
12010 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12011    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12012
12013 static inline int
12014 is_base_type (tree type)
12015 {
12016   switch (TREE_CODE (type))
12017     {
12018     case ERROR_MARK:
12019     case VOID_TYPE:
12020     case INTEGER_TYPE:
12021     case REAL_TYPE:
12022     case FIXED_POINT_TYPE:
12023     case COMPLEX_TYPE:
12024     case BOOLEAN_TYPE:
12025       return 1;
12026
12027     case ARRAY_TYPE:
12028     case RECORD_TYPE:
12029     case UNION_TYPE:
12030     case QUAL_UNION_TYPE:
12031     case ENUMERAL_TYPE:
12032     case FUNCTION_TYPE:
12033     case METHOD_TYPE:
12034     case POINTER_TYPE:
12035     case REFERENCE_TYPE:
12036     case OFFSET_TYPE:
12037     case LANG_TYPE:
12038     case VECTOR_TYPE:
12039       return 0;
12040
12041     default:
12042       gcc_unreachable ();
12043     }
12044
12045   return 0;
12046 }
12047
12048 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12049    node, return the size in bits for the type if it is a constant, or else
12050    return the alignment for the type if the type's size is not constant, or
12051    else return BITS_PER_WORD if the type actually turns out to be an
12052    ERROR_MARK node.  */
12053
12054 static inline unsigned HOST_WIDE_INT
12055 simple_type_size_in_bits (const_tree type)
12056 {
12057   if (TREE_CODE (type) == ERROR_MARK)
12058     return BITS_PER_WORD;
12059   else if (TYPE_SIZE (type) == NULL_TREE)
12060     return 0;
12061   else if (host_integerp (TYPE_SIZE (type), 1))
12062     return tree_low_cst (TYPE_SIZE (type), 1);
12063   else
12064     return TYPE_ALIGN (type);
12065 }
12066
12067 /*  Given a pointer to a tree node for a subrange type, return a pointer
12068     to a DIE that describes the given type.  */
12069
12070 static dw_die_ref
12071 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12072 {
12073   dw_die_ref subrange_die;
12074   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12075
12076   if (context_die == NULL)
12077     context_die = comp_unit_die;
12078
12079   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12080
12081   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12082     {
12083       /* The size of the subrange type and its base type do not match,
12084          so we need to generate a size attribute for the subrange type.  */
12085       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12086     }
12087
12088   if (low)
12089     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12090   if (high)
12091     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12092
12093   return subrange_die;
12094 }
12095
12096 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12097    entry that chains various modifiers in front of the given type.  */
12098
12099 static dw_die_ref
12100 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12101                    dw_die_ref context_die)
12102 {
12103   enum tree_code code = TREE_CODE (type);
12104   dw_die_ref mod_type_die;
12105   dw_die_ref sub_die = NULL;
12106   tree item_type = NULL;
12107   tree qualified_type;
12108   tree name, low, high;
12109
12110   if (code == ERROR_MARK)
12111     return NULL;
12112
12113   /* See if we already have the appropriately qualified variant of
12114      this type.  */
12115   qualified_type
12116     = get_qualified_type (type,
12117                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12118                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12119
12120   /* If we do, then we can just use its DIE, if it exists.  */
12121   if (qualified_type)
12122     {
12123       mod_type_die = lookup_type_die (qualified_type);
12124       if (mod_type_die)
12125         return mod_type_die;
12126     }
12127
12128   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12129
12130   /* Handle C typedef types.  */
12131   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
12132     {
12133       tree dtype = TREE_TYPE (name);
12134
12135       if (qualified_type == dtype)
12136         {
12137           /* For a named type, use the typedef.  */
12138           gen_type_die (qualified_type, context_die);
12139           return lookup_type_die (qualified_type);
12140         }
12141       else if (is_const_type < TYPE_READONLY (dtype)
12142                || is_volatile_type < TYPE_VOLATILE (dtype)
12143                || (is_const_type <= TYPE_READONLY (dtype)
12144                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12145                    && DECL_ORIGINAL_TYPE (name) != type))
12146         /* cv-unqualified version of named type.  Just use the unnamed
12147            type to which it refers.  */
12148         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12149                                   is_const_type, is_volatile_type,
12150                                   context_die);
12151       /* Else cv-qualified version of named type; fall through.  */
12152     }
12153
12154   if (is_const_type)
12155     {
12156       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12157       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12158     }
12159   else if (is_volatile_type)
12160     {
12161       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12162       sub_die = modified_type_die (type, 0, 0, context_die);
12163     }
12164   else if (code == POINTER_TYPE)
12165     {
12166       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12167       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12168                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12169       item_type = TREE_TYPE (type);
12170       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12171         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12172                          TYPE_ADDR_SPACE (item_type));
12173     }
12174   else if (code == REFERENCE_TYPE)
12175     {
12176       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12177       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12178                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12179       item_type = TREE_TYPE (type);
12180       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12181         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12182                          TYPE_ADDR_SPACE (item_type));
12183     }
12184   else if (code == INTEGER_TYPE
12185            && TREE_TYPE (type) != NULL_TREE
12186            && subrange_type_for_debug_p (type, &low, &high))
12187     {
12188       mod_type_die = subrange_type_die (type, low, high, context_die);
12189       item_type = TREE_TYPE (type);
12190     }
12191   else if (is_base_type (type))
12192     mod_type_die = base_type_die (type);
12193   else
12194     {
12195       gen_type_die (type, context_die);
12196
12197       /* We have to get the type_main_variant here (and pass that to the
12198          `lookup_type_die' routine) because the ..._TYPE node we have
12199          might simply be a *copy* of some original type node (where the
12200          copy was created to help us keep track of typedef names) and
12201          that copy might have a different TYPE_UID from the original
12202          ..._TYPE node.  */
12203       if (TREE_CODE (type) != VECTOR_TYPE)
12204         return lookup_type_die (type_main_variant (type));
12205       else
12206         /* Vectors have the debugging information in the type,
12207            not the main variant.  */
12208         return lookup_type_die (type);
12209     }
12210
12211   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12212      don't output a DW_TAG_typedef, since there isn't one in the
12213      user's program; just attach a DW_AT_name to the type.
12214      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12215      if the base type already has the same name.  */
12216   if (name
12217       && ((TREE_CODE (name) != TYPE_DECL
12218            && (qualified_type == TYPE_MAIN_VARIANT (type)
12219                || (!is_const_type && !is_volatile_type)))
12220           || (TREE_CODE (name) == TYPE_DECL
12221               && TREE_TYPE (name) == qualified_type
12222               && DECL_NAME (name))))
12223     {
12224       if (TREE_CODE (name) == TYPE_DECL)
12225         /* Could just call add_name_and_src_coords_attributes here,
12226            but since this is a builtin type it doesn't have any
12227            useful source coordinates anyway.  */
12228         name = DECL_NAME (name);
12229       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12230     }
12231
12232   if (qualified_type)
12233     equate_type_number_to_die (qualified_type, mod_type_die);
12234
12235   if (item_type)
12236     /* We must do this after the equate_type_number_to_die call, in case
12237        this is a recursive type.  This ensures that the modified_type_die
12238        recursion will terminate even if the type is recursive.  Recursive
12239        types are possible in Ada.  */
12240     sub_die = modified_type_die (item_type,
12241                                  TYPE_READONLY (item_type),
12242                                  TYPE_VOLATILE (item_type),
12243                                  context_die);
12244
12245   if (sub_die != NULL)
12246     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12247
12248   return mod_type_die;
12249 }
12250
12251 /* Generate DIEs for the generic parameters of T.
12252    T must be either a generic type or a generic function.
12253    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12254
12255 static void
12256 gen_generic_params_dies (tree t)
12257 {
12258   tree parms, args;
12259   int parms_num, i;
12260   dw_die_ref die = NULL;
12261
12262   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12263     return;
12264
12265   if (TYPE_P (t))
12266     die = lookup_type_die (t);
12267   else if (DECL_P (t))
12268     die = lookup_decl_die (t);
12269
12270   gcc_assert (die);
12271
12272   parms = lang_hooks.get_innermost_generic_parms (t);
12273   if (!parms)
12274     /* T has no generic parameter. It means T is neither a generic type
12275        or function. End of story.  */
12276     return;
12277
12278   parms_num = TREE_VEC_LENGTH (parms);
12279   args = lang_hooks.get_innermost_generic_args (t);
12280   for (i = 0; i < parms_num; i++)
12281     {
12282       tree parm, arg, arg_pack_elems;
12283
12284       parm = TREE_VEC_ELT (parms, i);
12285       arg = TREE_VEC_ELT (args, i);
12286       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12287       gcc_assert (parm && TREE_VALUE (parm) && arg);
12288
12289       if (parm && TREE_VALUE (parm) && arg)
12290         {
12291           /* If PARM represents a template parameter pack,
12292              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12293              by DW_TAG_template_*_parameter DIEs for the argument
12294              pack elements of ARG. Note that ARG would then be
12295              an argument pack.  */
12296           if (arg_pack_elems)
12297             template_parameter_pack_die (TREE_VALUE (parm),
12298                                          arg_pack_elems,
12299                                          die);
12300           else
12301             generic_parameter_die (TREE_VALUE (parm), arg,
12302                                    true /* Emit DW_AT_name */, die);
12303         }
12304     }
12305 }
12306
12307 /* Create and return a DIE for PARM which should be
12308    the representation of a generic type parameter.
12309    For instance, in the C++ front end, PARM would be a template parameter.
12310    ARG is the argument to PARM.
12311    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12312    name of the PARM.
12313    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12314    as a child node.  */
12315
12316 static dw_die_ref
12317 generic_parameter_die (tree parm, tree arg,
12318                        bool emit_name_p,
12319                        dw_die_ref parent_die)
12320 {
12321   dw_die_ref tmpl_die = NULL;
12322   const char *name = NULL;
12323
12324   if (!parm || !DECL_NAME (parm) || !arg)
12325     return NULL;
12326
12327   /* We support non-type generic parameters and arguments,
12328      type generic parameters and arguments, as well as
12329      generic generic parameters (a.k.a. template template parameters in C++)
12330      and arguments.  */
12331   if (TREE_CODE (parm) == PARM_DECL)
12332     /* PARM is a nontype generic parameter  */
12333     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12334   else if (TREE_CODE (parm) == TYPE_DECL)
12335     /* PARM is a type generic parameter.  */
12336     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12337   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12338     /* PARM is a generic generic parameter.
12339        Its DIE is a GNU extension. It shall have a
12340        DW_AT_name attribute to represent the name of the template template
12341        parameter, and a DW_AT_GNU_template_name attribute to represent the
12342        name of the template template argument.  */
12343     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12344                         parent_die, parm);
12345   else
12346     gcc_unreachable ();
12347
12348   if (tmpl_die)
12349     {
12350       tree tmpl_type;
12351
12352       /* If PARM is a generic parameter pack, it means we are
12353          emitting debug info for a template argument pack element.
12354          In other terms, ARG is a template argument pack element.
12355          In that case, we don't emit any DW_AT_name attribute for
12356          the die.  */
12357       if (emit_name_p)
12358         {
12359           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12360           gcc_assert (name);
12361           add_AT_string (tmpl_die, DW_AT_name, name);
12362         }
12363
12364       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12365         {
12366           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12367              TMPL_DIE should have a child DW_AT_type attribute that is set
12368              to the type of the argument to PARM, which is ARG.
12369              If PARM is a type generic parameter, TMPL_DIE should have a
12370              child DW_AT_type that is set to ARG.  */
12371           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12372           add_type_attribute (tmpl_die, tmpl_type, 0,
12373                               TREE_THIS_VOLATILE (tmpl_type),
12374                               parent_die);
12375         }
12376       else
12377         {
12378           /* So TMPL_DIE is a DIE representing a
12379              a generic generic template parameter, a.k.a template template
12380              parameter in C++ and arg is a template.  */
12381
12382           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12383              to the name of the argument.  */
12384           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12385           if (name)
12386             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12387         }
12388
12389       if (TREE_CODE (parm) == PARM_DECL)
12390         /* So PARM is a non-type generic parameter.
12391            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12392            attribute of TMPL_DIE which value represents the value
12393            of ARG.
12394            We must be careful here:
12395            The value of ARG might reference some function decls.
12396            We might currently be emitting debug info for a generic
12397            type and types are emitted before function decls, we don't
12398            know if the function decls referenced by ARG will actually be
12399            emitted after cgraph computations.
12400            So must defer the generation of the DW_AT_const_value to
12401            after cgraph is ready.  */
12402         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12403     }
12404
12405   return tmpl_die;
12406 }
12407
12408 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12409    PARM_PACK must be a template parameter pack. The returned DIE
12410    will be child DIE of PARENT_DIE.  */
12411
12412 static dw_die_ref
12413 template_parameter_pack_die (tree parm_pack,
12414                              tree parm_pack_args,
12415                              dw_die_ref parent_die)
12416 {
12417   dw_die_ref die;
12418   int j;
12419
12420   gcc_assert (parent_die && parm_pack);
12421
12422   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12423   add_name_and_src_coords_attributes (die, parm_pack);
12424   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12425     generic_parameter_die (parm_pack,
12426                            TREE_VEC_ELT (parm_pack_args, j),
12427                            false /* Don't emit DW_AT_name */,
12428                            die);
12429   return die;
12430 }
12431
12432 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12433    an enumerated type.  */
12434
12435 static inline int
12436 type_is_enum (const_tree type)
12437 {
12438   return TREE_CODE (type) == ENUMERAL_TYPE;
12439 }
12440
12441 /* Return the DBX register number described by a given RTL node.  */
12442
12443 static unsigned int
12444 dbx_reg_number (const_rtx rtl)
12445 {
12446   unsigned regno = REGNO (rtl);
12447
12448   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12449
12450 #ifdef LEAF_REG_REMAP
12451   if (current_function_uses_only_leaf_regs)
12452     {
12453       int leaf_reg = LEAF_REG_REMAP (regno);
12454       if (leaf_reg != -1)
12455         regno = (unsigned) leaf_reg;
12456     }
12457 #endif
12458
12459   return DBX_REGISTER_NUMBER (regno);
12460 }
12461
12462 /* Optionally add a DW_OP_piece term to a location description expression.
12463    DW_OP_piece is only added if the location description expression already
12464    doesn't end with DW_OP_piece.  */
12465
12466 static void
12467 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12468 {
12469   dw_loc_descr_ref loc;
12470
12471   if (*list_head != NULL)
12472     {
12473       /* Find the end of the chain.  */
12474       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12475         ;
12476
12477       if (loc->dw_loc_opc != DW_OP_piece)
12478         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12479     }
12480 }
12481
12482 /* Return a location descriptor that designates a machine register or
12483    zero if there is none.  */
12484
12485 static dw_loc_descr_ref
12486 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12487 {
12488   rtx regs;
12489
12490   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12491     return 0;
12492
12493   regs = targetm.dwarf_register_span (rtl);
12494
12495   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
12496     return multiple_reg_loc_descriptor (rtl, regs, initialized);
12497   else
12498     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
12499 }
12500
12501 /* Return a location descriptor that designates a machine register for
12502    a given hard register number.  */
12503
12504 static dw_loc_descr_ref
12505 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12506 {
12507   dw_loc_descr_ref reg_loc_descr;
12508
12509   if (regno <= 31)
12510     reg_loc_descr
12511       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12512   else
12513     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12514
12515   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12516     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12517
12518   return reg_loc_descr;
12519 }
12520
12521 /* Given an RTL of a register, return a location descriptor that
12522    designates a value that spans more than one register.  */
12523
12524 static dw_loc_descr_ref
12525 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12526                              enum var_init_status initialized)
12527 {
12528   int nregs, size, i;
12529   unsigned reg;
12530   dw_loc_descr_ref loc_result = NULL;
12531
12532   reg = REGNO (rtl);
12533 #ifdef LEAF_REG_REMAP
12534   if (current_function_uses_only_leaf_regs)
12535     {
12536       int leaf_reg = LEAF_REG_REMAP (reg);
12537       if (leaf_reg != -1)
12538         reg = (unsigned) leaf_reg;
12539     }
12540 #endif
12541   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12542   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
12543
12544   /* Simple, contiguous registers.  */
12545   if (regs == NULL_RTX)
12546     {
12547       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12548
12549       loc_result = NULL;
12550       while (nregs--)
12551         {
12552           dw_loc_descr_ref t;
12553
12554           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12555                                       VAR_INIT_STATUS_INITIALIZED);
12556           add_loc_descr (&loc_result, t);
12557           add_loc_descr_op_piece (&loc_result, size);
12558           ++reg;
12559         }
12560       return loc_result;
12561     }
12562
12563   /* Now onto stupid register sets in non contiguous locations.  */
12564
12565   gcc_assert (GET_CODE (regs) == PARALLEL);
12566
12567   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12568   loc_result = NULL;
12569
12570   for (i = 0; i < XVECLEN (regs, 0); ++i)
12571     {
12572       dw_loc_descr_ref t;
12573
12574       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
12575                                   VAR_INIT_STATUS_INITIALIZED);
12576       add_loc_descr (&loc_result, t);
12577       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12578       add_loc_descr_op_piece (&loc_result, size);
12579     }
12580
12581   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12582     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12583   return loc_result;
12584 }
12585
12586 #endif /* DWARF2_DEBUGGING_INFO */
12587
12588 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
12589
12590 /* Return a location descriptor that designates a constant.  */
12591
12592 static dw_loc_descr_ref
12593 int_loc_descriptor (HOST_WIDE_INT i)
12594 {
12595   enum dwarf_location_atom op;
12596
12597   /* Pick the smallest representation of a constant, rather than just
12598      defaulting to the LEB encoding.  */
12599   if (i >= 0)
12600     {
12601       if (i <= 31)
12602         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
12603       else if (i <= 0xff)
12604         op = DW_OP_const1u;
12605       else if (i <= 0xffff)
12606         op = DW_OP_const2u;
12607       else if (HOST_BITS_PER_WIDE_INT == 32
12608                || i <= 0xffffffff)
12609         op = DW_OP_const4u;
12610       else
12611         op = DW_OP_constu;
12612     }
12613   else
12614     {
12615       if (i >= -0x80)
12616         op = DW_OP_const1s;
12617       else if (i >= -0x8000)
12618         op = DW_OP_const2s;
12619       else if (HOST_BITS_PER_WIDE_INT == 32
12620                || i >= -0x80000000)
12621         op = DW_OP_const4s;
12622       else
12623         op = DW_OP_consts;
12624     }
12625
12626   return new_loc_descr (op, i, 0);
12627 }
12628 #endif
12629
12630 #ifdef DWARF2_DEBUGGING_INFO
12631 /* Return loc description representing "address" of integer value.
12632    This can appear only as toplevel expression.  */
12633
12634 static dw_loc_descr_ref
12635 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12636 {
12637   int litsize;
12638   dw_loc_descr_ref loc_result = NULL;
12639
12640   if (!(dwarf_version >= 4 || !dwarf_strict))
12641     return NULL;
12642
12643   if (i >= 0)
12644     {
12645       if (i <= 31)
12646         litsize = 1;
12647       else if (i <= 0xff)
12648         litsize = 2;
12649       else if (i <= 0xffff)
12650         litsize = 3;
12651       else if (HOST_BITS_PER_WIDE_INT == 32
12652                || i <= 0xffffffff)
12653         litsize = 5;
12654       else
12655         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12656     }
12657   else
12658     {
12659       if (i >= -0x80)
12660         litsize = 2;
12661       else if (i >= -0x8000)
12662         litsize = 3;
12663       else if (HOST_BITS_PER_WIDE_INT == 32
12664                || i >= -0x80000000)
12665         litsize = 5;
12666       else
12667         litsize = 1 + size_of_sleb128 (i);
12668     }
12669   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12670      is more compact.  For DW_OP_stack_value we need:
12671      litsize + 1 (DW_OP_stack_value)
12672      and for DW_OP_implicit_value:
12673      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12674   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12675     {
12676       loc_result = int_loc_descriptor (i);
12677       add_loc_descr (&loc_result,
12678                      new_loc_descr (DW_OP_stack_value, 0, 0));
12679       return loc_result;
12680     }
12681
12682   loc_result = new_loc_descr (DW_OP_implicit_value,
12683                               size, 0);
12684   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12685   loc_result->dw_loc_oprnd2.v.val_int = i;
12686   return loc_result;
12687 }
12688
12689 /* Return a location descriptor that designates a base+offset location.  */
12690
12691 static dw_loc_descr_ref
12692 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12693                  enum var_init_status initialized)
12694 {
12695   unsigned int regno;
12696   dw_loc_descr_ref result;
12697   dw_fde_ref fde = current_fde ();
12698
12699   /* We only use "frame base" when we're sure we're talking about the
12700      post-prologue local stack frame.  We do this by *not* running
12701      register elimination until this point, and recognizing the special
12702      argument pointer and soft frame pointer rtx's.  */
12703   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12704     {
12705       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12706
12707       if (elim != reg)
12708         {
12709           if (GET_CODE (elim) == PLUS)
12710             {
12711               offset += INTVAL (XEXP (elim, 1));
12712               elim = XEXP (elim, 0);
12713             }
12714           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12715                        && (elim == hard_frame_pointer_rtx
12716                            || elim == stack_pointer_rtx))
12717                       || elim == (frame_pointer_needed
12718                                   ? hard_frame_pointer_rtx
12719                                   : stack_pointer_rtx));
12720
12721           /* If drap register is used to align stack, use frame
12722              pointer + offset to access stack variables.  If stack
12723              is aligned without drap, use stack pointer + offset to
12724              access stack variables.  */
12725           if (crtl->stack_realign_tried
12726               && reg == frame_pointer_rtx)
12727             {
12728               int base_reg
12729                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12730                                       ? HARD_FRAME_POINTER_REGNUM
12731                                       : STACK_POINTER_REGNUM);
12732               return new_reg_loc_descr (base_reg, offset);
12733             }
12734
12735           offset += frame_pointer_fb_offset;
12736           return new_loc_descr (DW_OP_fbreg, offset, 0);
12737         }
12738     }
12739   else if (fde
12740            && fde->drap_reg != INVALID_REGNUM
12741            && (fde->drap_reg == REGNO (reg)
12742                || fde->vdrap_reg == REGNO (reg)))
12743     {
12744       /* Use cfa+offset to represent the location of arguments passed
12745          on stack when drap is used to align stack.  */
12746       return new_loc_descr (DW_OP_fbreg, offset, 0);
12747     }
12748
12749   regno = dbx_reg_number (reg);
12750   if (regno <= 31)
12751     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12752                             offset, 0);
12753   else
12754     result = new_loc_descr (DW_OP_bregx, regno, offset);
12755
12756   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12757     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12758
12759   return result;
12760 }
12761
12762 /* Return true if this RTL expression describes a base+offset calculation.  */
12763
12764 static inline int
12765 is_based_loc (const_rtx rtl)
12766 {
12767   return (GET_CODE (rtl) == PLUS
12768           && ((REG_P (XEXP (rtl, 0))
12769                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12770                && CONST_INT_P (XEXP (rtl, 1)))));
12771 }
12772
12773 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12774    failed.  */
12775
12776 static dw_loc_descr_ref
12777 tls_mem_loc_descriptor (rtx mem)
12778 {
12779   tree base;
12780   dw_loc_descr_ref loc_result;
12781
12782   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
12783     return NULL;
12784
12785   base = get_base_address (MEM_EXPR (mem));
12786   if (base == NULL
12787       || TREE_CODE (base) != VAR_DECL
12788       || !DECL_THREAD_LOCAL_P (base))
12789     return NULL;
12790
12791   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
12792   if (loc_result == NULL)
12793     return NULL;
12794
12795   if (INTVAL (MEM_OFFSET (mem)))
12796     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
12797
12798   return loc_result;
12799 }
12800
12801 /* Output debug info about reason why we failed to expand expression as dwarf
12802    expression.  */
12803
12804 static void
12805 expansion_failed (tree expr, rtx rtl, char const *reason)
12806 {
12807   if (dump_file && (dump_flags & TDF_DETAILS))
12808     {
12809       fprintf (dump_file, "Failed to expand as dwarf: ");
12810       if (expr)
12811         print_generic_expr (dump_file, expr, dump_flags);
12812       if (rtl)
12813         {
12814           fprintf (dump_file, "\n");
12815           print_rtl (dump_file, rtl);
12816         }
12817       fprintf (dump_file, "\nReason: %s\n", reason);
12818     }
12819 }
12820
12821 /* Helper function for const_ok_for_output, called either directly
12822    or via for_each_rtx.  */
12823
12824 static int
12825 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
12826 {
12827   rtx rtl = *rtlp;
12828
12829   if (GET_CODE (rtl) == UNSPEC)
12830     {
12831       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12832          we can't express it in the debug info.  */
12833 #ifdef ENABLE_CHECKING
12834       inform (current_function_decl
12835               ? DECL_SOURCE_LOCATION (current_function_decl)
12836               : UNKNOWN_LOCATION,
12837               "non-delegitimized UNSPEC %d found in variable location",
12838               XINT (rtl, 1));
12839 #endif
12840       expansion_failed (NULL_TREE, rtl,
12841                         "UNSPEC hasn't been delegitimized.\n");
12842       return 1;
12843     }
12844
12845   if (GET_CODE (rtl) != SYMBOL_REF)
12846     return 0;
12847
12848   if (CONSTANT_POOL_ADDRESS_P (rtl))
12849     {
12850       bool marked;
12851       get_pool_constant_mark (rtl, &marked);
12852       /* If all references to this pool constant were optimized away,
12853          it was not output and thus we can't represent it.  */
12854       if (!marked)
12855         {
12856           expansion_failed (NULL_TREE, rtl,
12857                             "Constant was removed from constant pool.\n");
12858           return 1;
12859         }
12860     }
12861
12862   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12863     return 1;
12864
12865   /* Avoid references to external symbols in debug info, on several targets
12866      the linker might even refuse to link when linking a shared library,
12867      and in many other cases the relocations for .debug_info/.debug_loc are
12868      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12869      to be defined within the same shared library or executable are fine.  */
12870   if (SYMBOL_REF_EXTERNAL_P (rtl))
12871     {
12872       tree decl = SYMBOL_REF_DECL (rtl);
12873
12874       if (decl == NULL || !targetm.binds_local_p (decl))
12875         {
12876           expansion_failed (NULL_TREE, rtl,
12877                             "Symbol not defined in current TU.\n");
12878           return 1;
12879         }
12880     }
12881
12882   return 0;
12883 }
12884
12885 /* Return true if constant RTL can be emitted in DW_OP_addr or
12886    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12887    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12888
12889 static bool
12890 const_ok_for_output (rtx rtl)
12891 {
12892   if (GET_CODE (rtl) == SYMBOL_REF)
12893     return const_ok_for_output_1 (&rtl, NULL) == 0;
12894
12895   if (GET_CODE (rtl) == CONST)
12896     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
12897
12898   return true;
12899 }
12900
12901 /* The following routine converts the RTL for a variable or parameter
12902    (resident in memory) into an equivalent Dwarf representation of a
12903    mechanism for getting the address of that same variable onto the top of a
12904    hypothetical "address evaluation" stack.
12905
12906    When creating memory location descriptors, we are effectively transforming
12907    the RTL for a memory-resident object into its Dwarf postfix expression
12908    equivalent.  This routine recursively descends an RTL tree, turning
12909    it into Dwarf postfix code as it goes.
12910
12911    MODE is the mode of the memory reference, needed to handle some
12912    autoincrement addressing modes.
12913
12914    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
12915    location list for RTL.
12916
12917    Return 0 if we can't represent the location.  */
12918
12919 static dw_loc_descr_ref
12920 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12921                     enum var_init_status initialized)
12922 {
12923   dw_loc_descr_ref mem_loc_result = NULL;
12924   enum dwarf_location_atom op;
12925   dw_loc_descr_ref op0, op1;
12926
12927   /* Note that for a dynamically sized array, the location we will generate a
12928      description of here will be the lowest numbered location which is
12929      actually within the array.  That's *not* necessarily the same as the
12930      zeroth element of the array.  */
12931
12932   rtl = targetm.delegitimize_address (rtl);
12933
12934   switch (GET_CODE (rtl))
12935     {
12936     case POST_INC:
12937     case POST_DEC:
12938     case POST_MODIFY:
12939       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
12940
12941     case SUBREG:
12942       /* The case of a subreg may arise when we have a local (register)
12943          variable or a formal (register) parameter which doesn't quite fill
12944          up an entire register.  For now, just assume that it is
12945          legitimate to make the Dwarf info refer to the whole register which
12946          contains the given subreg.  */
12947       if (!subreg_lowpart_p (rtl))
12948         break;
12949       rtl = SUBREG_REG (rtl);
12950       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
12951         break;
12952       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
12953         break;
12954       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
12955       break;
12956
12957     case REG:
12958       /* Whenever a register number forms a part of the description of the
12959          method for calculating the (dynamic) address of a memory resident
12960          object, DWARF rules require the register number be referred to as
12961          a "base register".  This distinction is not based in any way upon
12962          what category of register the hardware believes the given register
12963          belongs to.  This is strictly DWARF terminology we're dealing with
12964          here. Note that in cases where the location of a memory-resident
12965          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12966          OP_CONST (0)) the actual DWARF location descriptor that we generate
12967          may just be OP_BASEREG (basereg).  This may look deceptively like
12968          the object in question was allocated to a register (rather than in
12969          memory) so DWARF consumers need to be aware of the subtle
12970          distinction between OP_REG and OP_BASEREG.  */
12971       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12972         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12973       else if (stack_realign_drap
12974                && crtl->drap_reg
12975                && crtl->args.internal_arg_pointer == rtl
12976                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12977         {
12978           /* If RTL is internal_arg_pointer, which has been optimized
12979              out, use DRAP instead.  */
12980           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12981                                             VAR_INIT_STATUS_INITIALIZED);
12982         }
12983       break;
12984
12985     case SIGN_EXTEND:
12986     case ZERO_EXTEND:
12987       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12988                                 VAR_INIT_STATUS_INITIALIZED);
12989       if (op0 == 0)
12990         break;
12991       else
12992         {
12993           int shift = DWARF2_ADDR_SIZE
12994                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12995           shift *= BITS_PER_UNIT;
12996           if (GET_CODE (rtl) == SIGN_EXTEND)
12997             op = DW_OP_shra;
12998           else
12999             op = DW_OP_shr;
13000           mem_loc_result = op0;
13001           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13002           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13003           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13004           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13005         }
13006       break;
13007
13008     case MEM:
13009       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13010                                            VAR_INIT_STATUS_INITIALIZED);
13011       if (mem_loc_result == NULL)
13012         mem_loc_result = tls_mem_loc_descriptor (rtl);
13013       if (mem_loc_result != 0)
13014         {
13015           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13016             {
13017               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13018               return 0;
13019             }
13020           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13021             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13022           else
13023             add_loc_descr (&mem_loc_result,
13024                            new_loc_descr (DW_OP_deref_size,
13025                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13026         }
13027       else
13028         {
13029           rtx new_rtl = avoid_constant_pool_reference (rtl);
13030           if (new_rtl != rtl)
13031             return mem_loc_descriptor (new_rtl, mode, initialized);
13032         }
13033       break;
13034
13035     case LO_SUM:
13036          rtl = XEXP (rtl, 1);
13037
13038       /* ... fall through ...  */
13039
13040     case LABEL_REF:
13041       /* Some ports can transform a symbol ref into a label ref, because
13042          the symbol ref is too far away and has to be dumped into a constant
13043          pool.  */
13044     case CONST:
13045     case SYMBOL_REF:
13046       if (GET_CODE (rtl) == SYMBOL_REF
13047           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13048         {
13049           dw_loc_descr_ref temp;
13050
13051           /* If this is not defined, we have no way to emit the data.  */
13052           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13053             break;
13054
13055           temp = new_loc_descr (DW_OP_addr, 0, 0);
13056           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13057           temp->dw_loc_oprnd1.v.val_addr = rtl;
13058           temp->dtprel = true;
13059
13060           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13061           add_loc_descr (&mem_loc_result, temp);
13062
13063           break;
13064         }
13065
13066       if (!const_ok_for_output (rtl))
13067         break;
13068
13069     symref:
13070       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13071       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13072       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13073       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13074       break;
13075
13076     case CONCAT:
13077     case CONCATN:
13078     case VAR_LOCATION:
13079       expansion_failed (NULL_TREE, rtl,
13080                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13081       return 0;
13082
13083     case PRE_MODIFY:
13084       /* Extract the PLUS expression nested inside and fall into
13085          PLUS code below.  */
13086       rtl = XEXP (rtl, 1);
13087       goto plus;
13088
13089     case PRE_INC:
13090     case PRE_DEC:
13091       /* Turn these into a PLUS expression and fall into the PLUS code
13092          below.  */
13093       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13094                           GEN_INT (GET_CODE (rtl) == PRE_INC
13095                                    ? GET_MODE_UNIT_SIZE (mode)
13096                                    : -GET_MODE_UNIT_SIZE (mode)));
13097
13098       /* ... fall through ...  */
13099
13100     case PLUS:
13101     plus:
13102       if (is_based_loc (rtl))
13103         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13104                                           INTVAL (XEXP (rtl, 1)),
13105                                           VAR_INIT_STATUS_INITIALIZED);
13106       else
13107         {
13108           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13109                                                VAR_INIT_STATUS_INITIALIZED);
13110           if (mem_loc_result == 0)
13111             break;
13112
13113           if (CONST_INT_P (XEXP (rtl, 1)))
13114             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13115           else
13116             {
13117               dw_loc_descr_ref mem_loc_result2
13118                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13119                                       VAR_INIT_STATUS_INITIALIZED);
13120               if (mem_loc_result2 == 0)
13121                 break;
13122               add_loc_descr (&mem_loc_result, mem_loc_result2);
13123               add_loc_descr (&mem_loc_result,
13124                              new_loc_descr (DW_OP_plus, 0, 0));
13125             }
13126         }
13127       break;
13128
13129     /* If a pseudo-reg is optimized away, it is possible for it to
13130        be replaced with a MEM containing a multiply or shift.  */
13131     case MINUS:
13132       op = DW_OP_minus;
13133       goto do_binop;
13134
13135     case MULT:
13136       op = DW_OP_mul;
13137       goto do_binop;
13138
13139     case DIV:
13140       op = DW_OP_div;
13141       goto do_binop;
13142
13143     case UMOD:
13144       op = DW_OP_mod;
13145       goto do_binop;
13146
13147     case ASHIFT:
13148       op = DW_OP_shl;
13149       goto do_binop;
13150
13151     case ASHIFTRT:
13152       op = DW_OP_shra;
13153       goto do_binop;
13154
13155     case LSHIFTRT:
13156       op = DW_OP_shr;
13157       goto do_binop;
13158
13159     case AND:
13160       op = DW_OP_and;
13161       goto do_binop;
13162
13163     case IOR:
13164       op = DW_OP_or;
13165       goto do_binop;
13166
13167     case XOR:
13168       op = DW_OP_xor;
13169       goto do_binop;
13170
13171     do_binop:
13172       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13173                                 VAR_INIT_STATUS_INITIALIZED);
13174       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13175                                 VAR_INIT_STATUS_INITIALIZED);
13176
13177       if (op0 == 0 || op1 == 0)
13178         break;
13179
13180       mem_loc_result = op0;
13181       add_loc_descr (&mem_loc_result, op1);
13182       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13183       break;
13184
13185     case MOD:
13186       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13187                                 VAR_INIT_STATUS_INITIALIZED);
13188       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13189                                 VAR_INIT_STATUS_INITIALIZED);
13190
13191       if (op0 == 0 || op1 == 0)
13192         break;
13193
13194       mem_loc_result = op0;
13195       add_loc_descr (&mem_loc_result, op1);
13196       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13197       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13198       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13199       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13200       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13201       break;
13202
13203     case NOT:
13204       op = DW_OP_not;
13205       goto do_unop;
13206
13207     case ABS:
13208       op = DW_OP_abs;
13209       goto do_unop;
13210
13211     case NEG:
13212       op = DW_OP_neg;
13213       goto do_unop;
13214
13215     do_unop:
13216       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13217                                 VAR_INIT_STATUS_INITIALIZED);
13218
13219       if (op0 == 0)
13220         break;
13221
13222       mem_loc_result = op0;
13223       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13224       break;
13225
13226     case CONST_INT:
13227       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13228       break;
13229
13230     case EQ:
13231       op = DW_OP_eq;
13232       goto do_scompare;
13233
13234     case GE:
13235       op = DW_OP_ge;
13236       goto do_scompare;
13237
13238     case GT:
13239       op = DW_OP_gt;
13240       goto do_scompare;
13241
13242     case LE:
13243       op = DW_OP_le;
13244       goto do_scompare;
13245
13246     case LT:
13247       op = DW_OP_lt;
13248       goto do_scompare;
13249
13250     case NE:
13251       op = DW_OP_ne;
13252       goto do_scompare;
13253
13254     do_scompare:
13255       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13256           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13257         break;
13258       else
13259         {
13260           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13261
13262           if (op_mode == VOIDmode)
13263             op_mode = GET_MODE (XEXP (rtl, 1));
13264           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13265             break;
13266
13267           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13268                                     VAR_INIT_STATUS_INITIALIZED);
13269           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13270                                     VAR_INIT_STATUS_INITIALIZED);
13271
13272           if (op0 == 0 || op1 == 0)
13273             break;
13274
13275           if (op_mode != VOIDmode
13276               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13277             {
13278               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13279               shift *= BITS_PER_UNIT;
13280               /* For eq/ne, if the operands are known to be zero-extended,
13281                  there is no need to do the fancy shifting up.  */
13282               if (op == DW_OP_eq || op == DW_OP_ne)
13283                 {
13284                   dw_loc_descr_ref last0, last1;
13285                   for (last0 = op0;
13286                        last0->dw_loc_next != NULL;
13287                        last0 = last0->dw_loc_next)
13288                     ;
13289                   for (last1 = op1;
13290                        last1->dw_loc_next != NULL;
13291                        last1 = last1->dw_loc_next)
13292                     ;
13293                   /* deref_size zero extends, and for constants we can check
13294                      whether they are zero extended or not.  */
13295                   if (((last0->dw_loc_opc == DW_OP_deref_size
13296                         && last0->dw_loc_oprnd1.v.val_int
13297                            <= GET_MODE_SIZE (op_mode))
13298                        || (CONST_INT_P (XEXP (rtl, 0))
13299                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13300                                == (INTVAL (XEXP (rtl, 0))
13301                                    & GET_MODE_MASK (op_mode))))
13302                       && ((last1->dw_loc_opc == DW_OP_deref_size
13303                            && last1->dw_loc_oprnd1.v.val_int
13304                               <= GET_MODE_SIZE (op_mode))
13305                           || (CONST_INT_P (XEXP (rtl, 1))
13306                               && (unsigned HOST_WIDE_INT)
13307                                  INTVAL (XEXP (rtl, 1))
13308                                  == (INTVAL (XEXP (rtl, 1))
13309                                      & GET_MODE_MASK (op_mode)))))
13310                     goto do_compare;
13311                 }
13312               add_loc_descr (&op0, int_loc_descriptor (shift));
13313               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13314               if (CONST_INT_P (XEXP (rtl, 1)))
13315                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13316               else
13317                 {
13318                   add_loc_descr (&op1, int_loc_descriptor (shift));
13319                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13320                 }
13321             }
13322         }
13323
13324     do_compare:
13325       mem_loc_result = op0;
13326       add_loc_descr (&mem_loc_result, op1);
13327       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13328       if (STORE_FLAG_VALUE != 1)
13329         {
13330           add_loc_descr (&mem_loc_result,
13331                          int_loc_descriptor (STORE_FLAG_VALUE));
13332           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13333         }
13334       break;
13335
13336     case GEU:
13337       op = DW_OP_ge;
13338       goto do_ucompare;
13339
13340     case GTU:
13341       op = DW_OP_gt;
13342       goto do_ucompare;
13343
13344     case LEU:
13345       op = DW_OP_le;
13346       goto do_ucompare;
13347
13348     case LTU:
13349       op = DW_OP_lt;
13350       goto do_ucompare;
13351
13352     do_ucompare:
13353       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13354           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13355         break;
13356       else
13357         {
13358           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13359
13360           if (op_mode == VOIDmode)
13361             op_mode = GET_MODE (XEXP (rtl, 1));
13362           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13363             break;
13364
13365           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13366                                     VAR_INIT_STATUS_INITIALIZED);
13367           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13368                                     VAR_INIT_STATUS_INITIALIZED);
13369
13370           if (op0 == 0 || op1 == 0)
13371             break;
13372
13373           if (op_mode != VOIDmode
13374               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13375             {
13376               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13377               dw_loc_descr_ref last0, last1;
13378               for (last0 = op0;
13379                    last0->dw_loc_next != NULL;
13380                    last0 = last0->dw_loc_next)
13381                 ;
13382               for (last1 = op1;
13383                    last1->dw_loc_next != NULL;
13384                    last1 = last1->dw_loc_next)
13385                 ;
13386               if (CONST_INT_P (XEXP (rtl, 0)))
13387                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13388               /* deref_size zero extends, so no need to mask it again.  */
13389               else if (last0->dw_loc_opc != DW_OP_deref_size
13390                        || last0->dw_loc_oprnd1.v.val_int
13391                           > GET_MODE_SIZE (op_mode))
13392                 {
13393                   add_loc_descr (&op0, int_loc_descriptor (mask));
13394                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13395                 }
13396               if (CONST_INT_P (XEXP (rtl, 1)))
13397                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13398               /* deref_size zero extends, so no need to mask it again.  */
13399               else if (last1->dw_loc_opc != DW_OP_deref_size
13400                        || last1->dw_loc_oprnd1.v.val_int
13401                           > GET_MODE_SIZE (op_mode))
13402                 {
13403                   add_loc_descr (&op1, int_loc_descriptor (mask));
13404                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13405                 }
13406             }
13407           else
13408             {
13409               HOST_WIDE_INT bias = 1;
13410               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13411               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13412               if (CONST_INT_P (XEXP (rtl, 1)))
13413                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13414                                           + INTVAL (XEXP (rtl, 1)));
13415               else
13416                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13417                                                     bias, 0));
13418             }
13419         }
13420       goto do_compare;
13421
13422     case SMIN:
13423     case SMAX:
13424     case UMIN:
13425     case UMAX:
13426       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13427           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13428           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13429         break;
13430
13431       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13432                                 VAR_INIT_STATUS_INITIALIZED);
13433       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13434                                 VAR_INIT_STATUS_INITIALIZED);
13435
13436       if (op0 == 0 || op1 == 0)
13437         break;
13438
13439       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13440       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13441       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13442       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13443         {
13444           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13445             {
13446               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13447               add_loc_descr (&op0, int_loc_descriptor (mask));
13448               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13449               add_loc_descr (&op1, int_loc_descriptor (mask));
13450               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13451             }
13452           else
13453             {
13454               HOST_WIDE_INT bias = 1;
13455               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13456               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13457               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13458             }
13459         }
13460       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13461         {
13462           int shift = DWARF2_ADDR_SIZE
13463                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13464           shift *= BITS_PER_UNIT;
13465           add_loc_descr (&op0, int_loc_descriptor (shift));
13466           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13467           add_loc_descr (&op1, int_loc_descriptor (shift));
13468           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13469         }
13470
13471       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13472         op = DW_OP_lt;
13473       else
13474         op = DW_OP_gt;
13475       mem_loc_result = op0;
13476       add_loc_descr (&mem_loc_result, op1);
13477       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13478       {
13479         dw_loc_descr_ref bra_node, drop_node;
13480
13481         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13482         add_loc_descr (&mem_loc_result, bra_node);
13483         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13484         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13485         add_loc_descr (&mem_loc_result, drop_node);
13486         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13487         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13488       }
13489       break;
13490
13491     case ZERO_EXTRACT:
13492     case SIGN_EXTRACT:
13493       if (CONST_INT_P (XEXP (rtl, 1))
13494           && CONST_INT_P (XEXP (rtl, 2))
13495           && ((unsigned) INTVAL (XEXP (rtl, 1))
13496               + (unsigned) INTVAL (XEXP (rtl, 2))
13497               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
13498           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13499           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13500         {
13501           int shift, size;
13502           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13503                                     VAR_INIT_STATUS_INITIALIZED);
13504           if (op0 == 0)
13505             break;
13506           if (GET_CODE (rtl) == SIGN_EXTRACT)
13507             op = DW_OP_shra;
13508           else
13509             op = DW_OP_shr;
13510           mem_loc_result = op0;
13511           size = INTVAL (XEXP (rtl, 1));
13512           shift = INTVAL (XEXP (rtl, 2));
13513           if (BITS_BIG_ENDIAN)
13514             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13515                     - shift - size;
13516           if (shift + size != (int) DWARF2_ADDR_SIZE)
13517             {
13518               add_loc_descr (&mem_loc_result,
13519                              int_loc_descriptor (DWARF2_ADDR_SIZE
13520                                                  - shift - size));
13521               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13522             }
13523           if (size != (int) DWARF2_ADDR_SIZE)
13524             {
13525               add_loc_descr (&mem_loc_result,
13526                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13527               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13528             }
13529         }
13530       break;
13531
13532     case COMPARE:
13533     case IF_THEN_ELSE:
13534     case ROTATE:
13535     case ROTATERT:
13536     case TRUNCATE:
13537       /* In theory, we could implement the above.  */
13538       /* DWARF cannot represent the unsigned compare operations
13539          natively.  */
13540     case SS_MULT:
13541     case US_MULT:
13542     case SS_DIV:
13543     case US_DIV:
13544     case SS_PLUS:
13545     case US_PLUS:
13546     case SS_MINUS:
13547     case US_MINUS:
13548     case SS_NEG:
13549     case US_NEG:
13550     case SS_ABS:
13551     case SS_ASHIFT:
13552     case US_ASHIFT:
13553     case SS_TRUNCATE:
13554     case US_TRUNCATE:
13555     case UDIV:
13556     case UNORDERED:
13557     case ORDERED:
13558     case UNEQ:
13559     case UNGE:
13560     case UNGT:
13561     case UNLE:
13562     case UNLT:
13563     case LTGT:
13564     case FLOAT_EXTEND:
13565     case FLOAT_TRUNCATE:
13566     case FLOAT:
13567     case UNSIGNED_FLOAT:
13568     case FIX:
13569     case UNSIGNED_FIX:
13570     case FRACT_CONVERT:
13571     case UNSIGNED_FRACT_CONVERT:
13572     case SAT_FRACT:
13573     case UNSIGNED_SAT_FRACT:
13574     case SQRT:
13575     case BSWAP:
13576     case FFS:
13577     case CLZ:
13578     case CTZ:
13579     case POPCOUNT:
13580     case PARITY:
13581     case ASM_OPERANDS:
13582     case UNSPEC:
13583     case HIGH:
13584       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13585          can't express it in the debug info.  This can happen e.g. with some
13586          TLS UNSPECs.  */
13587       break;
13588
13589     case CONST_STRING:
13590       resolve_one_addr (&rtl, NULL);
13591       goto symref;
13592
13593     default:
13594 #ifdef ENABLE_CHECKING
13595       print_rtl (stderr, rtl);
13596       gcc_unreachable ();
13597 #else
13598       break;
13599 #endif
13600     }
13601
13602   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13603     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13604
13605   return mem_loc_result;
13606 }
13607
13608 /* Return a descriptor that describes the concatenation of two locations.
13609    This is typically a complex variable.  */
13610
13611 static dw_loc_descr_ref
13612 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13613 {
13614   dw_loc_descr_ref cc_loc_result = NULL;
13615   dw_loc_descr_ref x0_ref
13616     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13617   dw_loc_descr_ref x1_ref
13618     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13619
13620   if (x0_ref == 0 || x1_ref == 0)
13621     return 0;
13622
13623   cc_loc_result = x0_ref;
13624   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13625
13626   add_loc_descr (&cc_loc_result, x1_ref);
13627   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13628
13629   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13630     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13631
13632   return cc_loc_result;
13633 }
13634
13635 /* Return a descriptor that describes the concatenation of N
13636    locations.  */
13637
13638 static dw_loc_descr_ref
13639 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13640 {
13641   unsigned int i;
13642   dw_loc_descr_ref cc_loc_result = NULL;
13643   unsigned int n = XVECLEN (concatn, 0);
13644
13645   for (i = 0; i < n; ++i)
13646     {
13647       dw_loc_descr_ref ref;
13648       rtx x = XVECEXP (concatn, 0, i);
13649
13650       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13651       if (ref == NULL)
13652         return NULL;
13653
13654       add_loc_descr (&cc_loc_result, ref);
13655       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13656     }
13657
13658   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13659     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13660
13661   return cc_loc_result;
13662 }
13663
13664 /* Output a proper Dwarf location descriptor for a variable or parameter
13665    which is either allocated in a register or in a memory location.  For a
13666    register, we just generate an OP_REG and the register number.  For a
13667    memory location we provide a Dwarf postfix expression describing how to
13668    generate the (dynamic) address of the object onto the address stack.
13669
13670    MODE is mode of the decl if this loc_descriptor is going to be used in
13671    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13672    allowed, VOIDmode otherwise.
13673
13674    If we don't know how to describe it, return 0.  */
13675
13676 static dw_loc_descr_ref
13677 loc_descriptor (rtx rtl, enum machine_mode mode,
13678                 enum var_init_status initialized)
13679 {
13680   dw_loc_descr_ref loc_result = NULL;
13681
13682   switch (GET_CODE (rtl))
13683     {
13684     case SUBREG:
13685       /* The case of a subreg may arise when we have a local (register)
13686          variable or a formal (register) parameter which doesn't quite fill
13687          up an entire register.  For now, just assume that it is
13688          legitimate to make the Dwarf info refer to the whole register which
13689          contains the given subreg.  */
13690       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
13691       break;
13692
13693     case REG:
13694       loc_result = reg_loc_descriptor (rtl, initialized);
13695       break;
13696
13697     case SIGN_EXTEND:
13698     case ZERO_EXTEND:
13699       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13700       break;
13701
13702     case MEM:
13703       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13704                                        initialized);
13705       if (loc_result == NULL)
13706         loc_result = tls_mem_loc_descriptor (rtl);
13707       if (loc_result == NULL)
13708         {
13709           rtx new_rtl = avoid_constant_pool_reference (rtl);
13710           if (new_rtl != rtl)
13711             loc_result = loc_descriptor (new_rtl, mode, initialized);
13712         }
13713       break;
13714
13715     case CONCAT:
13716       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13717                                           initialized);
13718       break;
13719
13720     case CONCATN:
13721       loc_result = concatn_loc_descriptor (rtl, initialized);
13722       break;
13723
13724     case VAR_LOCATION:
13725       /* Single part.  */
13726       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13727         {
13728           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
13729                                        initialized);
13730           break;
13731         }
13732
13733       rtl = XEXP (rtl, 1);
13734       /* FALLTHRU */
13735
13736     case PARALLEL:
13737       {
13738         rtvec par_elems = XVEC (rtl, 0);
13739         int num_elem = GET_NUM_ELEM (par_elems);
13740         enum machine_mode mode;
13741         int i;
13742
13743         /* Create the first one, so we have something to add to.  */
13744         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13745                                      VOIDmode, initialized);
13746         if (loc_result == NULL)
13747           return NULL;
13748         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13749         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13750         for (i = 1; i < num_elem; i++)
13751           {
13752             dw_loc_descr_ref temp;
13753
13754             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13755                                    VOIDmode, initialized);
13756             if (temp == NULL)
13757               return NULL;
13758             add_loc_descr (&loc_result, temp);
13759             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13760             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13761           }
13762       }
13763       break;
13764
13765     case CONST_INT:
13766       if (mode != VOIDmode && mode != BLKmode)
13767         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13768                                                     INTVAL (rtl));
13769       break;
13770
13771     case CONST_DOUBLE:
13772       if (mode == VOIDmode)
13773         mode = GET_MODE (rtl);
13774
13775       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13776         {
13777           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13778
13779           /* Note that a CONST_DOUBLE rtx could represent either an integer
13780              or a floating-point constant.  A CONST_DOUBLE is used whenever
13781              the constant requires more than one word in order to be
13782              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13783           loc_result = new_loc_descr (DW_OP_implicit_value,
13784                                       GET_MODE_SIZE (mode), 0);
13785           if (SCALAR_FLOAT_MODE_P (mode))
13786             {
13787               unsigned int length = GET_MODE_SIZE (mode);
13788               unsigned char *array = GGC_NEWVEC (unsigned char, length);
13789
13790               insert_float (rtl, array);
13791               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13792               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13793               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13794               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13795             }
13796           else
13797             {
13798               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13799               loc_result->dw_loc_oprnd2.v.val_double.high
13800                 = CONST_DOUBLE_HIGH (rtl);
13801               loc_result->dw_loc_oprnd2.v.val_double.low
13802                 = CONST_DOUBLE_LOW (rtl);
13803             }
13804         }
13805       break;
13806
13807     case CONST_VECTOR:
13808       if (mode == VOIDmode)
13809         mode = GET_MODE (rtl);
13810
13811       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13812         {
13813           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13814           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13815           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13816           unsigned int i;
13817           unsigned char *p;
13818
13819           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13820           switch (GET_MODE_CLASS (mode))
13821             {
13822             case MODE_VECTOR_INT:
13823               for (i = 0, p = array; i < length; i++, p += elt_size)
13824                 {
13825                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13826                   HOST_WIDE_INT lo, hi;
13827
13828                   switch (GET_CODE (elt))
13829                     {
13830                     case CONST_INT:
13831                       lo = INTVAL (elt);
13832                       hi = -(lo < 0);
13833                       break;
13834
13835                     case CONST_DOUBLE:
13836                       lo = CONST_DOUBLE_LOW (elt);
13837                       hi = CONST_DOUBLE_HIGH (elt);
13838                       break;
13839
13840                     default:
13841                       gcc_unreachable ();
13842                     }
13843
13844                   if (elt_size <= sizeof (HOST_WIDE_INT))
13845                     insert_int (lo, elt_size, p);
13846                   else
13847                     {
13848                       unsigned char *p0 = p;
13849                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13850
13851                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13852                       if (WORDS_BIG_ENDIAN)
13853                         {
13854                           p0 = p1;
13855                           p1 = p;
13856                         }
13857                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13858                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13859                     }
13860                 }
13861               break;
13862
13863             case MODE_VECTOR_FLOAT:
13864               for (i = 0, p = array; i < length; i++, p += elt_size)
13865                 {
13866                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13867                   insert_float (elt, p);
13868                 }
13869               break;
13870
13871             default:
13872               gcc_unreachable ();
13873             }
13874
13875           loc_result = new_loc_descr (DW_OP_implicit_value,
13876                                       length * elt_size, 0);
13877           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13878           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13879           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13880           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13881         }
13882       break;
13883
13884     case CONST:
13885       if (mode == VOIDmode
13886           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13887           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13888           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13889         {
13890           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13891           break;
13892         }
13893       /* FALLTHROUGH */
13894     case SYMBOL_REF:
13895       if (!const_ok_for_output (rtl))
13896         break;
13897     case LABEL_REF:
13898       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13899           && (dwarf_version >= 4 || !dwarf_strict))
13900         {
13901           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13902           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13903           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13904           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13905           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13906         }
13907       break;
13908
13909     default:
13910       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13911           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13912           && (dwarf_version >= 4 || !dwarf_strict))
13913         {
13914           /* Value expression.  */
13915           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13916           if (loc_result)
13917             add_loc_descr (&loc_result,
13918                            new_loc_descr (DW_OP_stack_value, 0, 0));
13919         }
13920       break;
13921     }
13922
13923   return loc_result;
13924 }
13925
13926 /* We need to figure out what section we should use as the base for the
13927    address ranges where a given location is valid.
13928    1. If this particular DECL has a section associated with it, use that.
13929    2. If this function has a section associated with it, use that.
13930    3. Otherwise, use the text section.
13931    XXX: If you split a variable across multiple sections, we won't notice.  */
13932
13933 static const char *
13934 secname_for_decl (const_tree decl)
13935 {
13936   const char *secname;
13937
13938   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13939     {
13940       tree sectree = DECL_SECTION_NAME (decl);
13941       secname = TREE_STRING_POINTER (sectree);
13942     }
13943   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13944     {
13945       tree sectree = DECL_SECTION_NAME (current_function_decl);
13946       secname = TREE_STRING_POINTER (sectree);
13947     }
13948   else if (cfun && in_cold_section_p)
13949     secname = crtl->subsections.cold_section_label;
13950   else
13951     secname = text_section_label;
13952
13953   return secname;
13954 }
13955
13956 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13957
13958 static bool
13959 decl_by_reference_p (tree decl)
13960 {
13961   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13962            || TREE_CODE (decl) == VAR_DECL)
13963           && DECL_BY_REFERENCE (decl));
13964 }
13965
13966 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13967    for VARLOC.  */
13968
13969 static dw_loc_descr_ref
13970 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13971                enum var_init_status initialized)
13972 {
13973   int have_address = 0;
13974   dw_loc_descr_ref descr;
13975   enum machine_mode mode;
13976
13977   if (want_address != 2)
13978     {
13979       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13980       /* Single part.  */
13981       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
13982         {
13983           varloc = XEXP (XEXP (varloc, 1), 0);
13984           mode = GET_MODE (varloc);
13985           if (MEM_P (varloc))
13986             {
13987               rtx addr = XEXP (varloc, 0);
13988               descr = mem_loc_descriptor (addr, mode, initialized);
13989               if (descr)
13990                 have_address = 1;
13991               else
13992                 {
13993                   rtx x = avoid_constant_pool_reference (varloc);
13994                   if (x != varloc)
13995                     descr = mem_loc_descriptor (x, mode, initialized);
13996                 }
13997             }
13998           else
13999             descr = mem_loc_descriptor (varloc, mode, initialized);
14000         }
14001       else
14002         return 0;
14003     }
14004   else
14005     {
14006       descr = loc_descriptor (varloc, DECL_MODE (loc), initialized);
14007       have_address = 1;
14008     }
14009
14010   if (!descr)
14011     return 0;
14012
14013   if (want_address == 2 && !have_address
14014       && (dwarf_version >= 4 || !dwarf_strict))
14015     {
14016       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14017         {
14018           expansion_failed (loc, NULL_RTX,
14019                             "DWARF address size mismatch");
14020           return 0;
14021         }
14022       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14023       have_address = 1;
14024     }
14025   /* Show if we can't fill the request for an address.  */
14026   if (want_address && !have_address)
14027     {
14028       expansion_failed (loc, NULL_RTX,
14029                         "Want address and only have value");
14030       return 0;
14031     }
14032
14033   /* If we've got an address and don't want one, dereference.  */
14034   if (!want_address && have_address)
14035     {
14036       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14037       enum dwarf_location_atom op;
14038
14039       if (size > DWARF2_ADDR_SIZE || size == -1)
14040         {
14041           expansion_failed (loc, NULL_RTX,
14042                             "DWARF address size mismatch");
14043           return 0;
14044         }
14045       else if (size == DWARF2_ADDR_SIZE)
14046         op = DW_OP_deref;
14047       else
14048         op = DW_OP_deref_size;
14049
14050       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14051     }
14052
14053   return descr;
14054 }
14055
14056 /* Return the dwarf representation of the location list LOC_LIST of
14057    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14058    function.  */
14059
14060 static dw_loc_list_ref
14061 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14062 {
14063   const char *endname, *secname;
14064   rtx varloc;
14065   enum var_init_status initialized;
14066   struct var_loc_node *node;
14067   dw_loc_descr_ref descr;
14068   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14069   dw_loc_list_ref list = NULL;
14070   dw_loc_list_ref *listp = &list;
14071
14072   /* Now that we know what section we are using for a base,
14073      actually construct the list of locations.
14074      The first location information is what is passed to the
14075      function that creates the location list, and the remaining
14076      locations just get added on to that list.
14077      Note that we only know the start address for a location
14078      (IE location changes), so to build the range, we use
14079      the range [current location start, next location start].
14080      This means we have to special case the last node, and generate
14081      a range of [last location start, end of function label].  */
14082
14083   secname = secname_for_decl (decl);
14084
14085   for (node = loc_list->first; node->next; node = node->next)
14086     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
14087       {
14088         /* The variable has a location between NODE->LABEL and
14089            NODE->NEXT->LABEL.  */
14090         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14091         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
14092         descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14093         if (descr)
14094           {
14095             *listp = new_loc_list (descr, node->label, node->next->label,
14096                                    secname);
14097             listp = &(*listp)->dw_loc_next;
14098           }
14099       }
14100
14101   /* If the variable has a location at the last label
14102      it keeps its location until the end of function.  */
14103   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
14104     {
14105       initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14106       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
14107       descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14108       if (descr)
14109         {
14110           if (!current_function_decl)
14111             endname = text_end_label;
14112           else
14113             {
14114               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14115                                            current_function_funcdef_no);
14116               endname = ggc_strdup (label_id);
14117             }
14118
14119           *listp = new_loc_list (descr, node->label, endname, secname);
14120           listp = &(*listp)->dw_loc_next;
14121         }
14122     }
14123
14124   /* Try to avoid the overhead of a location list emitting a location
14125      expression instead, but only if we didn't have more than one
14126      location entry in the first place.  If some entries were not
14127      representable, we don't want to pretend a single entry that was
14128      applies to the entire scope in which the variable is
14129      available.  */
14130   if (list && loc_list->first->next)
14131     gen_llsym (list);
14132
14133   return list;
14134 }
14135
14136 /* Return if the loc_list has only single element and thus can be represented
14137    as location description.   */
14138
14139 static bool
14140 single_element_loc_list_p (dw_loc_list_ref list)
14141 {
14142   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14143   return !list->ll_symbol;
14144 }
14145
14146 /* To each location in list LIST add loc descr REF.  */
14147
14148 static void
14149 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14150 {
14151   dw_loc_descr_ref copy;
14152   add_loc_descr (&list->expr, ref);
14153   list = list->dw_loc_next;
14154   while (list)
14155     {
14156       copy = GGC_CNEW (dw_loc_descr_node);
14157       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14158       add_loc_descr (&list->expr, copy);
14159       while (copy->dw_loc_next)
14160         {
14161           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
14162           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14163           copy->dw_loc_next = new_copy;
14164           copy = new_copy;
14165         }
14166       list = list->dw_loc_next;
14167     }
14168 }
14169
14170 /* Given two lists RET and LIST
14171    produce location list that is result of adding expression in LIST
14172    to expression in RET on each possition in program.
14173    Might be destructive on both RET and LIST.
14174
14175    TODO: We handle only simple cases of RET or LIST having at most one
14176    element. General case would inolve sorting the lists in program order
14177    and merging them that will need some additional work.
14178    Adding that will improve quality of debug info especially for SRA-ed
14179    structures.  */
14180
14181 static void
14182 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14183 {
14184   if (!list)
14185     return;
14186   if (!*ret)
14187     {
14188       *ret = list;
14189       return;
14190     }
14191   if (!list->dw_loc_next)
14192     {
14193       add_loc_descr_to_each (*ret, list->expr);
14194       return;
14195     }
14196   if (!(*ret)->dw_loc_next)
14197     {
14198       add_loc_descr_to_each (list, (*ret)->expr);
14199       *ret = list;
14200       return;
14201     }
14202   expansion_failed (NULL_TREE, NULL_RTX,
14203                     "Don't know how to merge two non-trivial"
14204                     " location lists.\n");
14205   *ret = NULL;
14206   return;
14207 }
14208
14209 /* LOC is constant expression.  Try a luck, look it up in constant
14210    pool and return its loc_descr of its address.  */
14211
14212 static dw_loc_descr_ref
14213 cst_pool_loc_descr (tree loc)
14214 {
14215   /* Get an RTL for this, if something has been emitted.  */
14216   rtx rtl = lookup_constant_def (loc);
14217   enum machine_mode mode;
14218
14219   if (!rtl || !MEM_P (rtl))
14220     {
14221       gcc_assert (!rtl);
14222       return 0;
14223     }
14224   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14225
14226   /* TODO: We might get more coverage if we was actually delaying expansion
14227      of all expressions till end of compilation when constant pools are fully
14228      populated.  */
14229   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14230     {
14231       expansion_failed (loc, NULL_RTX,
14232                         "CST value in contant pool but not marked.");
14233       return 0;
14234     }
14235   mode = GET_MODE (rtl);
14236   rtl = XEXP (rtl, 0);
14237   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14238 }
14239
14240 /* Return dw_loc_list representing address of addr_expr LOC
14241    by looking for innder INDIRECT_REF expression and turing it
14242    into simple arithmetics.  */
14243
14244 static dw_loc_list_ref
14245 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14246 {
14247   tree obj, offset;
14248   HOST_WIDE_INT bitsize, bitpos, bytepos;
14249   enum machine_mode mode;
14250   int volatilep;
14251   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14252   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14253
14254   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14255                              &bitsize, &bitpos, &offset, &mode,
14256                              &unsignedp, &volatilep, false);
14257   STRIP_NOPS (obj);
14258   if (bitpos % BITS_PER_UNIT)
14259     {
14260       expansion_failed (loc, NULL_RTX, "bitfield access");
14261       return 0;
14262     }
14263   if (!INDIRECT_REF_P (obj))
14264     {
14265       expansion_failed (obj,
14266                         NULL_RTX, "no indirect ref in inner refrence");
14267       return 0;
14268     }
14269   if (!offset && !bitpos)
14270     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14271   else if (toplev
14272            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14273            && (dwarf_version >= 4 || !dwarf_strict))
14274     {
14275       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14276       if (!list_ret)
14277         return 0;
14278       if (offset)
14279         {
14280           /* Variable offset.  */
14281           list_ret1 = loc_list_from_tree (offset, 0);
14282           if (list_ret1 == 0)
14283             return 0;
14284           add_loc_list (&list_ret, list_ret1);
14285           if (!list_ret)
14286             return 0;
14287           add_loc_descr_to_each (list_ret,
14288                                  new_loc_descr (DW_OP_plus, 0, 0));
14289         }
14290       bytepos = bitpos / BITS_PER_UNIT;
14291       if (bytepos > 0)
14292         add_loc_descr_to_each (list_ret,
14293                                new_loc_descr (DW_OP_plus_uconst,
14294                                               bytepos, 0));
14295       else if (bytepos < 0)
14296         loc_list_plus_const (list_ret, bytepos);
14297       add_loc_descr_to_each (list_ret,
14298                              new_loc_descr (DW_OP_stack_value, 0, 0));
14299     }
14300   return list_ret;
14301 }
14302
14303
14304 /* Generate Dwarf location list representing LOC.
14305    If WANT_ADDRESS is false, expression computing LOC will be computed
14306    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14307    if WANT_ADDRESS is 2, expression computing address useable in location
14308      will be returned (i.e. DW_OP_reg can be used
14309      to refer to register values).  */
14310
14311 static dw_loc_list_ref
14312 loc_list_from_tree (tree loc, int want_address)
14313 {
14314   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14315   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14316   int have_address = 0;
14317   enum dwarf_location_atom op;
14318
14319   /* ??? Most of the time we do not take proper care for sign/zero
14320      extending the values properly.  Hopefully this won't be a real
14321      problem...  */
14322
14323   switch (TREE_CODE (loc))
14324     {
14325     case ERROR_MARK:
14326       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14327       return 0;
14328
14329     case PLACEHOLDER_EXPR:
14330       /* This case involves extracting fields from an object to determine the
14331          position of other fields.  We don't try to encode this here.  The
14332          only user of this is Ada, which encodes the needed information using
14333          the names of types.  */
14334       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14335       return 0;
14336
14337     case CALL_EXPR:
14338       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14339       /* There are no opcodes for these operations.  */
14340       return 0;
14341
14342     case PREINCREMENT_EXPR:
14343     case PREDECREMENT_EXPR:
14344     case POSTINCREMENT_EXPR:
14345     case POSTDECREMENT_EXPR:
14346       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14347       /* There are no opcodes for these operations.  */
14348       return 0;
14349
14350     case ADDR_EXPR:
14351       /* If we already want an address, see if there is INDIRECT_REF inside
14352          e.g. for &this->field.  */
14353       if (want_address)
14354         {
14355           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14356                        (loc, want_address == 2);
14357           if (list_ret)
14358             have_address = 1;
14359           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14360                    && (ret = cst_pool_loc_descr (loc)))
14361             have_address = 1;
14362         }
14363         /* Otherwise, process the argument and look for the address.  */
14364       if (!list_ret && !ret)
14365         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14366       else
14367         {
14368           if (want_address)
14369             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14370           return NULL;
14371         }
14372       break;
14373
14374     case VAR_DECL:
14375       if (DECL_THREAD_LOCAL_P (loc))
14376         {
14377           rtx rtl;
14378           enum dwarf_location_atom first_op;
14379           enum dwarf_location_atom second_op;
14380           bool dtprel = false;
14381
14382           if (targetm.have_tls)
14383             {
14384               /* If this is not defined, we have no way to emit the
14385                  data.  */
14386               if (!targetm.asm_out.output_dwarf_dtprel)
14387                 return 0;
14388
14389                /* The way DW_OP_GNU_push_tls_address is specified, we
14390                   can only look up addresses of objects in the current
14391                   module.  */
14392               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14393                 return 0;
14394               first_op = DW_OP_addr;
14395               dtprel = true;
14396               second_op = DW_OP_GNU_push_tls_address;
14397             }
14398           else
14399             {
14400               if (!targetm.emutls.debug_form_tls_address
14401                   || !(dwarf_version >= 3 || !dwarf_strict))
14402                 return 0;
14403               loc = emutls_decl (loc);
14404               first_op = DW_OP_addr;
14405               second_op = DW_OP_form_tls_address;
14406             }
14407
14408           rtl = rtl_for_decl_location (loc);
14409           if (rtl == NULL_RTX)
14410             return 0;
14411
14412           if (!MEM_P (rtl))
14413             return 0;
14414           rtl = XEXP (rtl, 0);
14415           if (! CONSTANT_P (rtl))
14416             return 0;
14417
14418           ret = new_loc_descr (first_op, 0, 0);
14419           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14420           ret->dw_loc_oprnd1.v.val_addr = rtl;
14421           ret->dtprel = dtprel;
14422
14423           ret1 = new_loc_descr (second_op, 0, 0);
14424           add_loc_descr (&ret, ret1);
14425
14426           have_address = 1;
14427           break;
14428         }
14429       /* FALLTHRU */
14430
14431     case PARM_DECL:
14432       if (DECL_HAS_VALUE_EXPR_P (loc))
14433         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14434                                    want_address);
14435       /* FALLTHRU */
14436
14437     case RESULT_DECL:
14438     case FUNCTION_DECL:
14439       {
14440         rtx rtl;
14441         var_loc_list *loc_list = lookup_decl_loc (loc);
14442
14443         if (loc_list && loc_list->first)
14444           {
14445             list_ret = dw_loc_list (loc_list, loc, want_address);
14446             have_address = want_address != 0;
14447             break;
14448           }
14449         rtl = rtl_for_decl_location (loc);
14450         if (rtl == NULL_RTX)
14451           {
14452             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14453             return 0;
14454           }
14455         else if (CONST_INT_P (rtl))
14456           {
14457             HOST_WIDE_INT val = INTVAL (rtl);
14458             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14459               val &= GET_MODE_MASK (DECL_MODE (loc));
14460             ret = int_loc_descriptor (val);
14461           }
14462         else if (GET_CODE (rtl) == CONST_STRING)
14463           {
14464             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14465             return 0;
14466           }
14467         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14468           {
14469             ret = new_loc_descr (DW_OP_addr, 0, 0);
14470             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14471             ret->dw_loc_oprnd1.v.val_addr = rtl;
14472           }
14473         else
14474           {
14475             enum machine_mode mode;
14476
14477             /* Certain constructs can only be represented at top-level.  */
14478             if (want_address == 2)
14479               {
14480                 ret = loc_descriptor (rtl, VOIDmode,
14481                                       VAR_INIT_STATUS_INITIALIZED);
14482                 have_address = 1;
14483               }
14484             else
14485               {
14486                 mode = GET_MODE (rtl);
14487                 if (MEM_P (rtl))
14488                   {
14489                     rtl = XEXP (rtl, 0);
14490                     have_address = 1;
14491                   }
14492                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14493               }
14494             if (!ret)
14495               expansion_failed (loc, rtl,
14496                                 "failed to produce loc descriptor for rtl");
14497           }
14498       }
14499       break;
14500
14501     case INDIRECT_REF:
14502     case ALIGN_INDIRECT_REF:
14503     case MISALIGNED_INDIRECT_REF:
14504       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14505       have_address = 1;
14506       break;
14507
14508     case COMPOUND_EXPR:
14509       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14510
14511     CASE_CONVERT:
14512     case VIEW_CONVERT_EXPR:
14513     case SAVE_EXPR:
14514     case MODIFY_EXPR:
14515       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14516
14517     case COMPONENT_REF:
14518     case BIT_FIELD_REF:
14519     case ARRAY_REF:
14520     case ARRAY_RANGE_REF:
14521     case REALPART_EXPR:
14522     case IMAGPART_EXPR:
14523       {
14524         tree obj, offset;
14525         HOST_WIDE_INT bitsize, bitpos, bytepos;
14526         enum machine_mode mode;
14527         int volatilep;
14528         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14529
14530         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14531                                    &unsignedp, &volatilep, false);
14532
14533         gcc_assert (obj != loc);
14534
14535         list_ret = loc_list_from_tree (obj,
14536                                        want_address == 2
14537                                        && !bitpos && !offset ? 2 : 1);
14538         /* TODO: We can extract value of the small expression via shifting even
14539            for nonzero bitpos.  */
14540         if (list_ret == 0)
14541           return 0;
14542         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14543           {
14544             expansion_failed (loc, NULL_RTX,
14545                               "bitfield access");
14546             return 0;
14547           }
14548
14549         if (offset != NULL_TREE)
14550           {
14551             /* Variable offset.  */
14552             list_ret1 = loc_list_from_tree (offset, 0);
14553             if (list_ret1 == 0)
14554               return 0;
14555             add_loc_list (&list_ret, list_ret1);
14556             if (!list_ret)
14557               return 0;
14558             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14559           }
14560
14561         bytepos = bitpos / BITS_PER_UNIT;
14562         if (bytepos > 0)
14563           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14564         else if (bytepos < 0)
14565           loc_list_plus_const (list_ret, bytepos);
14566
14567         have_address = 1;
14568         break;
14569       }
14570
14571     case INTEGER_CST:
14572       if ((want_address || !host_integerp (loc, 0))
14573           && (ret = cst_pool_loc_descr (loc)))
14574         have_address = 1;
14575       else if (want_address == 2
14576                && host_integerp (loc, 0)
14577                && (ret = address_of_int_loc_descriptor
14578                            (int_size_in_bytes (TREE_TYPE (loc)),
14579                             tree_low_cst (loc, 0))))
14580         have_address = 1;
14581       else if (host_integerp (loc, 0))
14582         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14583       else
14584         {
14585           expansion_failed (loc, NULL_RTX,
14586                             "Integer operand is not host integer");
14587           return 0;
14588         }
14589       break;
14590
14591     case CONSTRUCTOR:
14592     case REAL_CST:
14593     case STRING_CST:
14594     case COMPLEX_CST:
14595       if ((ret = cst_pool_loc_descr (loc)))
14596         have_address = 1;
14597       else
14598       /* We can construct small constants here using int_loc_descriptor.  */
14599         expansion_failed (loc, NULL_RTX,
14600                           "constructor or constant not in constant pool");
14601       break;
14602
14603     case TRUTH_AND_EXPR:
14604     case TRUTH_ANDIF_EXPR:
14605     case BIT_AND_EXPR:
14606       op = DW_OP_and;
14607       goto do_binop;
14608
14609     case TRUTH_XOR_EXPR:
14610     case BIT_XOR_EXPR:
14611       op = DW_OP_xor;
14612       goto do_binop;
14613
14614     case TRUTH_OR_EXPR:
14615     case TRUTH_ORIF_EXPR:
14616     case BIT_IOR_EXPR:
14617       op = DW_OP_or;
14618       goto do_binop;
14619
14620     case FLOOR_DIV_EXPR:
14621     case CEIL_DIV_EXPR:
14622     case ROUND_DIV_EXPR:
14623     case TRUNC_DIV_EXPR:
14624       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14625         return 0;
14626       op = DW_OP_div;
14627       goto do_binop;
14628
14629     case MINUS_EXPR:
14630       op = DW_OP_minus;
14631       goto do_binop;
14632
14633     case FLOOR_MOD_EXPR:
14634     case CEIL_MOD_EXPR:
14635     case ROUND_MOD_EXPR:
14636     case TRUNC_MOD_EXPR:
14637       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14638         {
14639           op = DW_OP_mod;
14640           goto do_binop;
14641         }
14642       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14643       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14644       if (list_ret == 0 || list_ret1 == 0)
14645         return 0;
14646
14647       add_loc_list (&list_ret, list_ret1);
14648       if (list_ret == 0)
14649         return 0;
14650       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14651       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14652       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14653       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14654       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14655       break;
14656
14657     case MULT_EXPR:
14658       op = DW_OP_mul;
14659       goto do_binop;
14660
14661     case LSHIFT_EXPR:
14662       op = DW_OP_shl;
14663       goto do_binop;
14664
14665     case RSHIFT_EXPR:
14666       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14667       goto do_binop;
14668
14669     case POINTER_PLUS_EXPR:
14670     case PLUS_EXPR:
14671       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
14672           && host_integerp (TREE_OPERAND (loc, 1), 0))
14673         {
14674           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14675           if (list_ret == 0)
14676             return 0;
14677
14678           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14679           break;
14680         }
14681
14682       op = DW_OP_plus;
14683       goto do_binop;
14684
14685     case LE_EXPR:
14686       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14687         return 0;
14688
14689       op = DW_OP_le;
14690       goto do_binop;
14691
14692     case GE_EXPR:
14693       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14694         return 0;
14695
14696       op = DW_OP_ge;
14697       goto do_binop;
14698
14699     case LT_EXPR:
14700       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14701         return 0;
14702
14703       op = DW_OP_lt;
14704       goto do_binop;
14705
14706     case GT_EXPR:
14707       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14708         return 0;
14709
14710       op = DW_OP_gt;
14711       goto do_binop;
14712
14713     case EQ_EXPR:
14714       op = DW_OP_eq;
14715       goto do_binop;
14716
14717     case NE_EXPR:
14718       op = DW_OP_ne;
14719       goto do_binop;
14720
14721     do_binop:
14722       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14723       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14724       if (list_ret == 0 || list_ret1 == 0)
14725         return 0;
14726
14727       add_loc_list (&list_ret, list_ret1);
14728       if (list_ret == 0)
14729         return 0;
14730       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14731       break;
14732
14733     case TRUTH_NOT_EXPR:
14734     case BIT_NOT_EXPR:
14735       op = DW_OP_not;
14736       goto do_unop;
14737
14738     case ABS_EXPR:
14739       op = DW_OP_abs;
14740       goto do_unop;
14741
14742     case NEGATE_EXPR:
14743       op = DW_OP_neg;
14744       goto do_unop;
14745
14746     do_unop:
14747       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14748       if (list_ret == 0)
14749         return 0;
14750
14751       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14752       break;
14753
14754     case MIN_EXPR:
14755     case MAX_EXPR:
14756       {
14757         const enum tree_code code =
14758           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14759
14760         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14761                       build2 (code, integer_type_node,
14762                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14763                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14764       }
14765
14766       /* ... fall through ...  */
14767
14768     case COND_EXPR:
14769       {
14770         dw_loc_descr_ref lhs
14771           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14772         dw_loc_list_ref rhs
14773           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14774         dw_loc_descr_ref bra_node, jump_node, tmp;
14775
14776         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14777         if (list_ret == 0 || lhs == 0 || rhs == 0)
14778           return 0;
14779
14780         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14781         add_loc_descr_to_each (list_ret, bra_node);
14782
14783         add_loc_list (&list_ret, rhs);
14784         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14785         add_loc_descr_to_each (list_ret, jump_node);
14786
14787         add_loc_descr_to_each (list_ret, lhs);
14788         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14789         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14790
14791         /* ??? Need a node to point the skip at.  Use a nop.  */
14792         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14793         add_loc_descr_to_each (list_ret, tmp);
14794         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14795         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14796       }
14797       break;
14798
14799     case FIX_TRUNC_EXPR:
14800       return 0;
14801
14802     default:
14803       /* Leave front-end specific codes as simply unknown.  This comes
14804          up, for instance, with the C STMT_EXPR.  */
14805       if ((unsigned int) TREE_CODE (loc)
14806           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14807         {
14808           expansion_failed (loc, NULL_RTX,
14809                             "language specific tree node");
14810           return 0;
14811         }
14812
14813 #ifdef ENABLE_CHECKING
14814       /* Otherwise this is a generic code; we should just lists all of
14815          these explicitly.  We forgot one.  */
14816       gcc_unreachable ();
14817 #else
14818       /* In a release build, we want to degrade gracefully: better to
14819          generate incomplete debugging information than to crash.  */
14820       return NULL;
14821 #endif
14822     }
14823
14824   if (!ret && !list_ret)
14825     return 0;
14826
14827   if (want_address == 2 && !have_address
14828       && (dwarf_version >= 4 || !dwarf_strict))
14829     {
14830       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14831         {
14832           expansion_failed (loc, NULL_RTX,
14833                             "DWARF address size mismatch");
14834           return 0;
14835         }
14836       if (ret)
14837         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14838       else
14839         add_loc_descr_to_each (list_ret,
14840                                new_loc_descr (DW_OP_stack_value, 0, 0));
14841       have_address = 1;
14842     }
14843   /* Show if we can't fill the request for an address.  */
14844   if (want_address && !have_address)
14845     {
14846       expansion_failed (loc, NULL_RTX,
14847                         "Want address and only have value");
14848       return 0;
14849     }
14850
14851   gcc_assert (!ret || !list_ret);
14852
14853   /* If we've got an address and don't want one, dereference.  */
14854   if (!want_address && have_address)
14855     {
14856       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14857
14858       if (size > DWARF2_ADDR_SIZE || size == -1)
14859         {
14860           expansion_failed (loc, NULL_RTX,
14861                             "DWARF address size mismatch");
14862           return 0;
14863         }
14864       else if (size == DWARF2_ADDR_SIZE)
14865         op = DW_OP_deref;
14866       else
14867         op = DW_OP_deref_size;
14868
14869       if (ret)
14870         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14871       else
14872         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14873     }
14874   if (ret)
14875     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14876
14877   return list_ret;
14878 }
14879
14880 /* Same as above but return only single location expression.  */
14881 static dw_loc_descr_ref
14882 loc_descriptor_from_tree (tree loc, int want_address)
14883 {
14884   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14885   if (!ret)
14886     return NULL;
14887   if (ret->dw_loc_next)
14888     {
14889       expansion_failed (loc, NULL_RTX,
14890                         "Location list where only loc descriptor needed");
14891       return NULL;
14892     }
14893   return ret->expr;
14894 }
14895
14896 /* Given a value, round it up to the lowest multiple of `boundary'
14897    which is not less than the value itself.  */
14898
14899 static inline HOST_WIDE_INT
14900 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14901 {
14902   return (((value + boundary - 1) / boundary) * boundary);
14903 }
14904
14905 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14906    pointer to the declared type for the relevant field variable, or return
14907    `integer_type_node' if the given node turns out to be an
14908    ERROR_MARK node.  */
14909
14910 static inline tree
14911 field_type (const_tree decl)
14912 {
14913   tree type;
14914
14915   if (TREE_CODE (decl) == ERROR_MARK)
14916     return integer_type_node;
14917
14918   type = DECL_BIT_FIELD_TYPE (decl);
14919   if (type == NULL_TREE)
14920     type = TREE_TYPE (decl);
14921
14922   return type;
14923 }
14924
14925 /* Given a pointer to a tree node, return the alignment in bits for
14926    it, or else return BITS_PER_WORD if the node actually turns out to
14927    be an ERROR_MARK node.  */
14928
14929 static inline unsigned
14930 simple_type_align_in_bits (const_tree type)
14931 {
14932   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14933 }
14934
14935 static inline unsigned
14936 simple_decl_align_in_bits (const_tree decl)
14937 {
14938   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14939 }
14940
14941 /* Return the result of rounding T up to ALIGN.  */
14942
14943 static inline HOST_WIDE_INT
14944 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
14945 {
14946   /* We must be careful if T is negative because HOST_WIDE_INT can be
14947      either "above" or "below" unsigned int as per the C promotion
14948      rules, depending on the host, thus making the signedness of the
14949      direct multiplication and division unpredictable.  */
14950   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
14951
14952   u += align - 1;
14953   u /= align;
14954   u *= align;
14955
14956   return (HOST_WIDE_INT) u;
14957 }
14958
14959 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14960    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14961    or return 0 if we are unable to determine what that offset is, either
14962    because the argument turns out to be a pointer to an ERROR_MARK node, or
14963    because the offset is actually variable.  (We can't handle the latter case
14964    just yet).  */
14965
14966 static HOST_WIDE_INT
14967 field_byte_offset (const_tree decl)
14968 {
14969   HOST_WIDE_INT object_offset_in_bits;
14970   HOST_WIDE_INT bitpos_int;
14971
14972   if (TREE_CODE (decl) == ERROR_MARK)
14973     return 0;
14974
14975   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14976
14977   /* We cannot yet cope with fields whose positions are variable, so
14978      for now, when we see such things, we simply return 0.  Someday, we may
14979      be able to handle such cases, but it will be damn difficult.  */
14980   if (! host_integerp (bit_position (decl), 0))
14981     return 0;
14982
14983   bitpos_int = int_bit_position (decl);
14984
14985 #ifdef PCC_BITFIELD_TYPE_MATTERS
14986   if (PCC_BITFIELD_TYPE_MATTERS)
14987     {
14988       tree type;
14989       tree field_size_tree;
14990       HOST_WIDE_INT deepest_bitpos;
14991       unsigned HOST_WIDE_INT field_size_in_bits;
14992       unsigned int type_align_in_bits;
14993       unsigned int decl_align_in_bits;
14994       unsigned HOST_WIDE_INT type_size_in_bits;
14995
14996       type = field_type (decl);
14997       type_size_in_bits = simple_type_size_in_bits (type);
14998       type_align_in_bits = simple_type_align_in_bits (type);
14999
15000       field_size_tree = DECL_SIZE (decl);
15001
15002       /* The size could be unspecified if there was an error, or for
15003          a flexible array member.  */
15004       if (!field_size_tree)
15005         field_size_tree = bitsize_zero_node;
15006
15007       /* If the size of the field is not constant, use the type size.  */
15008       if (host_integerp (field_size_tree, 1))
15009         field_size_in_bits = tree_low_cst (field_size_tree, 1);
15010       else
15011         field_size_in_bits = type_size_in_bits;
15012
15013       decl_align_in_bits = simple_decl_align_in_bits (decl);
15014
15015       /* The GCC front-end doesn't make any attempt to keep track of the
15016          starting bit offset (relative to the start of the containing
15017          structure type) of the hypothetical "containing object" for a
15018          bit-field.  Thus, when computing the byte offset value for the
15019          start of the "containing object" of a bit-field, we must deduce
15020          this information on our own. This can be rather tricky to do in
15021          some cases.  For example, handling the following structure type
15022          definition when compiling for an i386/i486 target (which only
15023          aligns long long's to 32-bit boundaries) can be very tricky:
15024
15025          struct S { int field1; long long field2:31; };
15026
15027          Fortunately, there is a simple rule-of-thumb which can be used
15028          in such cases.  When compiling for an i386/i486, GCC will
15029          allocate 8 bytes for the structure shown above.  It decides to
15030          do this based upon one simple rule for bit-field allocation.
15031          GCC allocates each "containing object" for each bit-field at
15032          the first (i.e. lowest addressed) legitimate alignment boundary
15033          (based upon the required minimum alignment for the declared
15034          type of the field) which it can possibly use, subject to the
15035          condition that there is still enough available space remaining
15036          in the containing object (when allocated at the selected point)
15037          to fully accommodate all of the bits of the bit-field itself.
15038
15039          This simple rule makes it obvious why GCC allocates 8 bytes for
15040          each object of the structure type shown above.  When looking
15041          for a place to allocate the "containing object" for `field2',
15042          the compiler simply tries to allocate a 64-bit "containing
15043          object" at each successive 32-bit boundary (starting at zero)
15044          until it finds a place to allocate that 64- bit field such that
15045          at least 31 contiguous (and previously unallocated) bits remain
15046          within that selected 64 bit field.  (As it turns out, for the
15047          example above, the compiler finds it is OK to allocate the
15048          "containing object" 64-bit field at bit-offset zero within the
15049          structure type.)
15050
15051          Here we attempt to work backwards from the limited set of facts
15052          we're given, and we try to deduce from those facts, where GCC
15053          must have believed that the containing object started (within
15054          the structure type). The value we deduce is then used (by the
15055          callers of this routine) to generate DW_AT_location and
15056          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15057          the case of DW_AT_location, regular fields as well).  */
15058
15059       /* Figure out the bit-distance from the start of the structure to
15060          the "deepest" bit of the bit-field.  */
15061       deepest_bitpos = bitpos_int + field_size_in_bits;
15062
15063       /* This is the tricky part.  Use some fancy footwork to deduce
15064          where the lowest addressed bit of the containing object must
15065          be.  */
15066       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15067
15068       /* Round up to type_align by default.  This works best for
15069          bitfields.  */
15070       object_offset_in_bits
15071         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15072
15073       if (object_offset_in_bits > bitpos_int)
15074         {
15075           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15076
15077           /* Round up to decl_align instead.  */
15078           object_offset_in_bits
15079             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15080         }
15081     }
15082   else
15083 #endif
15084     object_offset_in_bits = bitpos_int;
15085
15086   return object_offset_in_bits / BITS_PER_UNIT;
15087 }
15088 \f
15089 /* The following routines define various Dwarf attributes and any data
15090    associated with them.  */
15091
15092 /* Add a location description attribute value to a DIE.
15093
15094    This emits location attributes suitable for whole variables and
15095    whole parameters.  Note that the location attributes for struct fields are
15096    generated by the routine `data_member_location_attribute' below.  */
15097
15098 static inline void
15099 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15100                              dw_loc_list_ref descr)
15101 {
15102   if (descr == 0)
15103     return;
15104   if (single_element_loc_list_p (descr))
15105     add_AT_loc (die, attr_kind, descr->expr);
15106   else
15107     add_AT_loc_list (die, attr_kind, descr);
15108 }
15109
15110 /* Attach the specialized form of location attribute used for data members of
15111    struct and union types.  In the special case of a FIELD_DECL node which
15112    represents a bit-field, the "offset" part of this special location
15113    descriptor must indicate the distance in bytes from the lowest-addressed
15114    byte of the containing struct or union type to the lowest-addressed byte of
15115    the "containing object" for the bit-field.  (See the `field_byte_offset'
15116    function above).
15117
15118    For any given bit-field, the "containing object" is a hypothetical object
15119    (of some integral or enum type) within which the given bit-field lives.  The
15120    type of this hypothetical "containing object" is always the same as the
15121    declared type of the individual bit-field itself (for GCC anyway... the
15122    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15123    bytes) of the hypothetical "containing object" which will be given in the
15124    DW_AT_byte_size attribute for this bit-field.  (See the
15125    `byte_size_attribute' function below.)  It is also used when calculating the
15126    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15127    function below.)  */
15128
15129 static void
15130 add_data_member_location_attribute (dw_die_ref die, tree decl)
15131 {
15132   HOST_WIDE_INT offset;
15133   dw_loc_descr_ref loc_descr = 0;
15134
15135   if (TREE_CODE (decl) == TREE_BINFO)
15136     {
15137       /* We're working on the TAG_inheritance for a base class.  */
15138       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15139         {
15140           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15141              aren't at a fixed offset from all (sub)objects of the same
15142              type.  We need to extract the appropriate offset from our
15143              vtable.  The following dwarf expression means
15144
15145                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15146
15147              This is specific to the V3 ABI, of course.  */
15148
15149           dw_loc_descr_ref tmp;
15150
15151           /* Make a copy of the object address.  */
15152           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15153           add_loc_descr (&loc_descr, tmp);
15154
15155           /* Extract the vtable address.  */
15156           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15157           add_loc_descr (&loc_descr, tmp);
15158
15159           /* Calculate the address of the offset.  */
15160           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15161           gcc_assert (offset < 0);
15162
15163           tmp = int_loc_descriptor (-offset);
15164           add_loc_descr (&loc_descr, tmp);
15165           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15166           add_loc_descr (&loc_descr, tmp);
15167
15168           /* Extract the offset.  */
15169           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15170           add_loc_descr (&loc_descr, tmp);
15171
15172           /* Add it to the object address.  */
15173           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15174           add_loc_descr (&loc_descr, tmp);
15175         }
15176       else
15177         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15178     }
15179   else
15180     offset = field_byte_offset (decl);
15181
15182   if (! loc_descr)
15183     {
15184       if (dwarf_version > 2)
15185         {
15186           /* Don't need to output a location expression, just the constant. */
15187           add_AT_int (die, DW_AT_data_member_location, offset);
15188           return;
15189         }
15190       else
15191         {
15192           enum dwarf_location_atom op;
15193
15194           /* The DWARF2 standard says that we should assume that the structure
15195              address is already on the stack, so we can specify a structure
15196              field address by using DW_OP_plus_uconst.  */
15197
15198 #ifdef MIPS_DEBUGGING_INFO
15199           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15200              operator correctly.  It works only if we leave the offset on the
15201              stack.  */
15202           op = DW_OP_constu;
15203 #else
15204           op = DW_OP_plus_uconst;
15205 #endif
15206
15207           loc_descr = new_loc_descr (op, offset, 0);
15208         }
15209     }
15210
15211   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15212 }
15213
15214 /* Writes integer values to dw_vec_const array.  */
15215
15216 static void
15217 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15218 {
15219   while (size != 0)
15220     {
15221       *dest++ = val & 0xff;
15222       val >>= 8;
15223       --size;
15224     }
15225 }
15226
15227 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15228
15229 static HOST_WIDE_INT
15230 extract_int (const unsigned char *src, unsigned int size)
15231 {
15232   HOST_WIDE_INT val = 0;
15233
15234   src += size;
15235   while (size != 0)
15236     {
15237       val <<= 8;
15238       val |= *--src & 0xff;
15239       --size;
15240     }
15241   return val;
15242 }
15243
15244 /* Writes floating point values to dw_vec_const array.  */
15245
15246 static void
15247 insert_float (const_rtx rtl, unsigned char *array)
15248 {
15249   REAL_VALUE_TYPE rv;
15250   long val[4];
15251   int i;
15252
15253   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15254   real_to_target (val, &rv, GET_MODE (rtl));
15255
15256   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15257   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15258     {
15259       insert_int (val[i], 4, array);
15260       array += 4;
15261     }
15262 }
15263
15264 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15265    does not have a "location" either in memory or in a register.  These
15266    things can arise in GNU C when a constant is passed as an actual parameter
15267    to an inlined function.  They can also arise in C++ where declared
15268    constants do not necessarily get memory "homes".  */
15269
15270 static bool
15271 add_const_value_attribute (dw_die_ref die, rtx rtl)
15272 {
15273   switch (GET_CODE (rtl))
15274     {
15275     case CONST_INT:
15276       {
15277         HOST_WIDE_INT val = INTVAL (rtl);
15278
15279         if (val < 0)
15280           add_AT_int (die, DW_AT_const_value, val);
15281         else
15282           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15283       }
15284       return true;
15285
15286     case CONST_DOUBLE:
15287       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15288          floating-point constant.  A CONST_DOUBLE is used whenever the
15289          constant requires more than one word in order to be adequately
15290          represented.  */
15291       {
15292         enum machine_mode mode = GET_MODE (rtl);
15293
15294         if (SCALAR_FLOAT_MODE_P (mode))
15295           {
15296             unsigned int length = GET_MODE_SIZE (mode);
15297             unsigned char *array = GGC_NEWVEC (unsigned char, length);
15298
15299             insert_float (rtl, array);
15300             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15301           }
15302         else
15303           add_AT_double (die, DW_AT_const_value,
15304                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15305       }
15306       return true;
15307
15308     case CONST_VECTOR:
15309       {
15310         enum machine_mode mode = GET_MODE (rtl);
15311         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15312         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15313         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
15314         unsigned int i;
15315         unsigned char *p;
15316
15317         switch (GET_MODE_CLASS (mode))
15318           {
15319           case MODE_VECTOR_INT:
15320             for (i = 0, p = array; i < length; i++, p += elt_size)
15321               {
15322                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15323                 HOST_WIDE_INT lo, hi;
15324
15325                 switch (GET_CODE (elt))
15326                   {
15327                   case CONST_INT:
15328                     lo = INTVAL (elt);
15329                     hi = -(lo < 0);
15330                     break;
15331
15332                   case CONST_DOUBLE:
15333                     lo = CONST_DOUBLE_LOW (elt);
15334                     hi = CONST_DOUBLE_HIGH (elt);
15335                     break;
15336
15337                   default:
15338                     gcc_unreachable ();
15339                   }
15340
15341                 if (elt_size <= sizeof (HOST_WIDE_INT))
15342                   insert_int (lo, elt_size, p);
15343                 else
15344                   {
15345                     unsigned char *p0 = p;
15346                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
15347
15348                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15349                     if (WORDS_BIG_ENDIAN)
15350                       {
15351                         p0 = p1;
15352                         p1 = p;
15353                       }
15354                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
15355                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
15356                   }
15357               }
15358             break;
15359
15360           case MODE_VECTOR_FLOAT:
15361             for (i = 0, p = array; i < length; i++, p += elt_size)
15362               {
15363                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15364                 insert_float (elt, p);
15365               }
15366             break;
15367
15368           default:
15369             gcc_unreachable ();
15370           }
15371
15372         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15373       }
15374       return true;
15375
15376     case CONST_STRING:
15377       if (dwarf_version >= 4 || !dwarf_strict)
15378         {
15379           dw_loc_descr_ref loc_result;
15380           resolve_one_addr (&rtl, NULL);
15381         rtl_addr:
15382           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15383           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15384           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15385           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15386           add_AT_loc (die, DW_AT_location, loc_result);
15387           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15388           return true;
15389         }
15390       return false;
15391
15392     case CONST:
15393       if (CONSTANT_P (XEXP (rtl, 0)))
15394         return add_const_value_attribute (die, XEXP (rtl, 0));
15395       /* FALLTHROUGH */
15396     case SYMBOL_REF:
15397       if (!const_ok_for_output (rtl))
15398         return false;
15399     case LABEL_REF:
15400       if (dwarf_version >= 4 || !dwarf_strict)
15401         goto rtl_addr;
15402       return false;
15403
15404     case PLUS:
15405       /* In cases where an inlined instance of an inline function is passed
15406          the address of an `auto' variable (which is local to the caller) we
15407          can get a situation where the DECL_RTL of the artificial local
15408          variable (for the inlining) which acts as a stand-in for the
15409          corresponding formal parameter (of the inline function) will look
15410          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15411          exactly a compile-time constant expression, but it isn't the address
15412          of the (artificial) local variable either.  Rather, it represents the
15413          *value* which the artificial local variable always has during its
15414          lifetime.  We currently have no way to represent such quasi-constant
15415          values in Dwarf, so for now we just punt and generate nothing.  */
15416       return false;
15417
15418     case HIGH:
15419     case CONST_FIXED:
15420       return false;
15421
15422     case MEM:
15423       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15424           && MEM_READONLY_P (rtl)
15425           && GET_MODE (rtl) == BLKmode)
15426         {
15427           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15428           return true;
15429         }
15430       return false;
15431
15432     default:
15433       /* No other kinds of rtx should be possible here.  */
15434       gcc_unreachable ();
15435     }
15436   return false;
15437 }
15438
15439 /* Determine whether the evaluation of EXPR references any variables
15440    or functions which aren't otherwise used (and therefore may not be
15441    output).  */
15442 static tree
15443 reference_to_unused (tree * tp, int * walk_subtrees,
15444                      void * data ATTRIBUTE_UNUSED)
15445 {
15446   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15447     *walk_subtrees = 0;
15448
15449   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15450       && ! TREE_ASM_WRITTEN (*tp))
15451     return *tp;
15452   /* ???  The C++ FE emits debug information for using decls, so
15453      putting gcc_unreachable here falls over.  See PR31899.  For now
15454      be conservative.  */
15455   else if (!cgraph_global_info_ready
15456            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15457     return *tp;
15458   else if (TREE_CODE (*tp) == VAR_DECL)
15459     {
15460       struct varpool_node *node = varpool_node (*tp);
15461       if (!node->needed)
15462         return *tp;
15463     }
15464   else if (TREE_CODE (*tp) == FUNCTION_DECL
15465            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15466     {
15467       /* The call graph machinery must have finished analyzing,
15468          optimizing and gimplifying the CU by now.
15469          So if *TP has no call graph node associated
15470          to it, it means *TP will not be emitted.  */
15471       if (!cgraph_get_node (*tp))
15472         return *tp;
15473     }
15474   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15475     return *tp;
15476
15477   return NULL_TREE;
15478 }
15479
15480 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15481    for use in a later add_const_value_attribute call.  */
15482
15483 static rtx
15484 rtl_for_decl_init (tree init, tree type)
15485 {
15486   rtx rtl = NULL_RTX;
15487
15488   /* If a variable is initialized with a string constant without embedded
15489      zeros, build CONST_STRING.  */
15490   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15491     {
15492       tree enttype = TREE_TYPE (type);
15493       tree domain = TYPE_DOMAIN (type);
15494       enum machine_mode mode = TYPE_MODE (enttype);
15495
15496       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15497           && domain
15498           && integer_zerop (TYPE_MIN_VALUE (domain))
15499           && compare_tree_int (TYPE_MAX_VALUE (domain),
15500                                TREE_STRING_LENGTH (init) - 1) == 0
15501           && ((size_t) TREE_STRING_LENGTH (init)
15502               == strlen (TREE_STRING_POINTER (init)) + 1))
15503         {
15504           rtl = gen_rtx_CONST_STRING (VOIDmode,
15505                                       ggc_strdup (TREE_STRING_POINTER (init)));
15506           rtl = gen_rtx_MEM (BLKmode, rtl);
15507           MEM_READONLY_P (rtl) = 1;
15508         }
15509     }
15510   /* Other aggregates, and complex values, could be represented using
15511      CONCAT: FIXME!  */
15512   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
15513     ;
15514   /* Vectors only work if their mode is supported by the target.
15515      FIXME: generic vectors ought to work too.  */
15516   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
15517     ;
15518   /* If the initializer is something that we know will expand into an
15519      immediate RTL constant, expand it now.  We must be careful not to
15520      reference variables which won't be output.  */
15521   else if (initializer_constant_valid_p (init, type)
15522            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15523     {
15524       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15525          possible.  */
15526       if (TREE_CODE (type) == VECTOR_TYPE)
15527         switch (TREE_CODE (init))
15528           {
15529           case VECTOR_CST:
15530             break;
15531           case CONSTRUCTOR:
15532             if (TREE_CONSTANT (init))
15533               {
15534                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
15535                 bool constant_p = true;
15536                 tree value;
15537                 unsigned HOST_WIDE_INT ix;
15538
15539                 /* Even when ctor is constant, it might contain non-*_CST
15540                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15541                    belong into VECTOR_CST nodes.  */
15542                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15543                   if (!CONSTANT_CLASS_P (value))
15544                     {
15545                       constant_p = false;
15546                       break;
15547                     }
15548
15549                 if (constant_p)
15550                   {
15551                     init = build_vector_from_ctor (type, elts);
15552                     break;
15553                   }
15554               }
15555             /* FALLTHRU */
15556
15557           default:
15558             return NULL;
15559           }
15560
15561       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15562
15563       /* If expand_expr returns a MEM, it wasn't immediate.  */
15564       gcc_assert (!rtl || !MEM_P (rtl));
15565     }
15566
15567   return rtl;
15568 }
15569
15570 /* Generate RTL for the variable DECL to represent its location.  */
15571
15572 static rtx
15573 rtl_for_decl_location (tree decl)
15574 {
15575   rtx rtl;
15576
15577   /* Here we have to decide where we are going to say the parameter "lives"
15578      (as far as the debugger is concerned).  We only have a couple of
15579      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15580
15581      DECL_RTL normally indicates where the parameter lives during most of the
15582      activation of the function.  If optimization is enabled however, this
15583      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15584      that the parameter doesn't really live anywhere (as far as the code
15585      generation parts of GCC are concerned) during most of the function's
15586      activation.  That will happen (for example) if the parameter is never
15587      referenced within the function.
15588
15589      We could just generate a location descriptor here for all non-NULL
15590      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15591      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15592      where DECL_RTL is NULL or is a pseudo-reg.
15593
15594      Note however that we can only get away with using DECL_INCOMING_RTL as
15595      a backup substitute for DECL_RTL in certain limited cases.  In cases
15596      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15597      we can be sure that the parameter was passed using the same type as it is
15598      declared to have within the function, and that its DECL_INCOMING_RTL
15599      points us to a place where a value of that type is passed.
15600
15601      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15602      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15603      because in these cases DECL_INCOMING_RTL points us to a value of some
15604      type which is *different* from the type of the parameter itself.  Thus,
15605      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15606      such cases, the debugger would end up (for example) trying to fetch a
15607      `float' from a place which actually contains the first part of a
15608      `double'.  That would lead to really incorrect and confusing
15609      output at debug-time.
15610
15611      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15612      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15613      are a couple of exceptions however.  On little-endian machines we can
15614      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15615      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15616      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15617      when (on a little-endian machine) a non-prototyped function has a
15618      parameter declared to be of type `short' or `char'.  In such cases,
15619      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15620      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15621      passed `int' value.  If the debugger then uses that address to fetch
15622      a `short' or a `char' (on a little-endian machine) the result will be
15623      the correct data, so we allow for such exceptional cases below.
15624
15625      Note that our goal here is to describe the place where the given formal
15626      parameter lives during most of the function's activation (i.e. between the
15627      end of the prologue and the start of the epilogue).  We'll do that as best
15628      as we can. Note however that if the given formal parameter is modified
15629      sometime during the execution of the function, then a stack backtrace (at
15630      debug-time) will show the function as having been called with the *new*
15631      value rather than the value which was originally passed in.  This happens
15632      rarely enough that it is not a major problem, but it *is* a problem, and
15633      I'd like to fix it.
15634
15635      A future version of dwarf2out.c may generate two additional attributes for
15636      any given DW_TAG_formal_parameter DIE which will describe the "passed
15637      type" and the "passed location" for the given formal parameter in addition
15638      to the attributes we now generate to indicate the "declared type" and the
15639      "active location" for each parameter.  This additional set of attributes
15640      could be used by debuggers for stack backtraces. Separately, note that
15641      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15642      This happens (for example) for inlined-instances of inline function formal
15643      parameters which are never referenced.  This really shouldn't be
15644      happening.  All PARM_DECL nodes should get valid non-NULL
15645      DECL_INCOMING_RTL values.  FIXME.  */
15646
15647   /* Use DECL_RTL as the "location" unless we find something better.  */
15648   rtl = DECL_RTL_IF_SET (decl);
15649
15650   /* When generating abstract instances, ignore everything except
15651      constants, symbols living in memory, and symbols living in
15652      fixed registers.  */
15653   if (! reload_completed)
15654     {
15655       if (rtl
15656           && (CONSTANT_P (rtl)
15657               || (MEM_P (rtl)
15658                   && CONSTANT_P (XEXP (rtl, 0)))
15659               || (REG_P (rtl)
15660                   && TREE_CODE (decl) == VAR_DECL
15661                   && TREE_STATIC (decl))))
15662         {
15663           rtl = targetm.delegitimize_address (rtl);
15664           return rtl;
15665         }
15666       rtl = NULL_RTX;
15667     }
15668   else if (TREE_CODE (decl) == PARM_DECL)
15669     {
15670       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
15671         {
15672           tree declared_type = TREE_TYPE (decl);
15673           tree passed_type = DECL_ARG_TYPE (decl);
15674           enum machine_mode dmode = TYPE_MODE (declared_type);
15675           enum machine_mode pmode = TYPE_MODE (passed_type);
15676
15677           /* This decl represents a formal parameter which was optimized out.
15678              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15679              all cases where (rtl == NULL_RTX) just below.  */
15680           if (dmode == pmode)
15681             rtl = DECL_INCOMING_RTL (decl);
15682           else if (SCALAR_INT_MODE_P (dmode)
15683                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15684                    && DECL_INCOMING_RTL (decl))
15685             {
15686               rtx inc = DECL_INCOMING_RTL (decl);
15687               if (REG_P (inc))
15688                 rtl = inc;
15689               else if (MEM_P (inc))
15690                 {
15691                   if (BYTES_BIG_ENDIAN)
15692                     rtl = adjust_address_nv (inc, dmode,
15693                                              GET_MODE_SIZE (pmode)
15694                                              - GET_MODE_SIZE (dmode));
15695                   else
15696                     rtl = inc;
15697                 }
15698             }
15699         }
15700
15701       /* If the parm was passed in registers, but lives on the stack, then
15702          make a big endian correction if the mode of the type of the
15703          parameter is not the same as the mode of the rtl.  */
15704       /* ??? This is the same series of checks that are made in dbxout.c before
15705          we reach the big endian correction code there.  It isn't clear if all
15706          of these checks are necessary here, but keeping them all is the safe
15707          thing to do.  */
15708       else if (MEM_P (rtl)
15709                && XEXP (rtl, 0) != const0_rtx
15710                && ! CONSTANT_P (XEXP (rtl, 0))
15711                /* Not passed in memory.  */
15712                && !MEM_P (DECL_INCOMING_RTL (decl))
15713                /* Not passed by invisible reference.  */
15714                && (!REG_P (XEXP (rtl, 0))
15715                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15716                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15717 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
15718                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15719 #endif
15720                      )
15721                /* Big endian correction check.  */
15722                && BYTES_BIG_ENDIAN
15723                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15724                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15725                    < UNITS_PER_WORD))
15726         {
15727           int offset = (UNITS_PER_WORD
15728                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15729
15730           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15731                              plus_constant (XEXP (rtl, 0), offset));
15732         }
15733     }
15734   else if (TREE_CODE (decl) == VAR_DECL
15735            && rtl
15736            && MEM_P (rtl)
15737            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15738            && BYTES_BIG_ENDIAN)
15739     {
15740       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15741       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15742
15743       /* If a variable is declared "register" yet is smaller than
15744          a register, then if we store the variable to memory, it
15745          looks like we're storing a register-sized value, when in
15746          fact we are not.  We need to adjust the offset of the
15747          storage location to reflect the actual value's bytes,
15748          else gdb will not be able to display it.  */
15749       if (rsize > dsize)
15750         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15751                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15752     }
15753
15754   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15755      and will have been substituted directly into all expressions that use it.
15756      C does not have such a concept, but C++ and other languages do.  */
15757   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15758     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15759
15760   if (rtl)
15761     rtl = targetm.delegitimize_address (rtl);
15762
15763   /* If we don't look past the constant pool, we risk emitting a
15764      reference to a constant pool entry that isn't referenced from
15765      code, and thus is not emitted.  */
15766   if (rtl)
15767     rtl = avoid_constant_pool_reference (rtl);
15768
15769   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15770      in the current CU, resolve_addr will remove the expression referencing
15771      it.  */
15772   if (rtl == NULL_RTX
15773       && TREE_CODE (decl) == VAR_DECL
15774       && !DECL_EXTERNAL (decl)
15775       && TREE_STATIC (decl)
15776       && DECL_NAME (decl)
15777       && !DECL_HARD_REGISTER (decl)
15778       && DECL_MODE (decl) != VOIDmode)
15779     {
15780       rtl = DECL_RTL (decl);
15781       /* Reset DECL_RTL back, as various parts of the compiler expects
15782          DECL_RTL set meaning it is actually going to be output.  */
15783       SET_DECL_RTL (decl, NULL);
15784       if (!MEM_P (rtl)
15785           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15786           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15787         rtl = NULL_RTX;
15788     }
15789
15790   return rtl;
15791 }
15792
15793 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15794    returned.  If so, the decl for the COMMON block is returned, and the
15795    value is the offset into the common block for the symbol.  */
15796
15797 static tree
15798 fortran_common (tree decl, HOST_WIDE_INT *value)
15799 {
15800   tree val_expr, cvar;
15801   enum machine_mode mode;
15802   HOST_WIDE_INT bitsize, bitpos;
15803   tree offset;
15804   int volatilep = 0, unsignedp = 0;
15805
15806   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15807      it does not have a value (the offset into the common area), or if it
15808      is thread local (as opposed to global) then it isn't common, and shouldn't
15809      be handled as such.  */
15810   if (TREE_CODE (decl) != VAR_DECL
15811       || !TREE_STATIC (decl)
15812       || !DECL_HAS_VALUE_EXPR_P (decl)
15813       || !is_fortran ())
15814     return NULL_TREE;
15815
15816   val_expr = DECL_VALUE_EXPR (decl);
15817   if (TREE_CODE (val_expr) != COMPONENT_REF)
15818     return NULL_TREE;
15819
15820   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15821                               &mode, &unsignedp, &volatilep, true);
15822
15823   if (cvar == NULL_TREE
15824       || TREE_CODE (cvar) != VAR_DECL
15825       || DECL_ARTIFICIAL (cvar)
15826       || !TREE_PUBLIC (cvar))
15827     return NULL_TREE;
15828
15829   *value = 0;
15830   if (offset != NULL)
15831     {
15832       if (!host_integerp (offset, 0))
15833         return NULL_TREE;
15834       *value = tree_low_cst (offset, 0);
15835     }
15836   if (bitpos != 0)
15837     *value += bitpos / BITS_PER_UNIT;
15838
15839   return cvar;
15840 }
15841
15842 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15843    data attribute for a variable or a parameter.  We generate the
15844    DW_AT_const_value attribute only in those cases where the given variable
15845    or parameter does not have a true "location" either in memory or in a
15846    register.  This can happen (for example) when a constant is passed as an
15847    actual argument in a call to an inline function.  (It's possible that
15848    these things can crop up in other ways also.)  Note that one type of
15849    constant value which can be passed into an inlined function is a constant
15850    pointer.  This can happen for example if an actual argument in an inlined
15851    function call evaluates to a compile-time constant address.  */
15852
15853 static bool
15854 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
15855                                        enum dwarf_attribute attr)
15856 {
15857   rtx rtl;
15858   dw_loc_list_ref list;
15859   var_loc_list *loc_list;
15860
15861   if (TREE_CODE (decl) == ERROR_MARK)
15862     return false;
15863
15864   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15865               || TREE_CODE (decl) == RESULT_DECL);
15866
15867   /* Try to get some constant RTL for this decl, and use that as the value of
15868      the location.  */
15869
15870   rtl = rtl_for_decl_location (decl);
15871   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15872       && add_const_value_attribute (die, rtl))
15873     return true;
15874
15875   /* See if we have single element location list that is equivalent to
15876      a constant value.  That way we are better to use add_const_value_attribute
15877      rather than expanding constant value equivalent.  */
15878   loc_list = lookup_decl_loc (decl);
15879   if (loc_list
15880       && loc_list->first
15881       && loc_list->first == loc_list->last
15882       && NOTE_VAR_LOCATION (loc_list->first->var_loc_note)
15883       && NOTE_VAR_LOCATION_LOC (loc_list->first->var_loc_note))
15884     {
15885       struct var_loc_node *node;
15886
15887       node = loc_list->first;
15888       rtl = NOTE_VAR_LOCATION_LOC (node->var_loc_note);
15889       if (GET_CODE (rtl) != PARALLEL)
15890         rtl = XEXP (rtl, 0);
15891       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15892           && add_const_value_attribute (die, rtl))
15893          return true;
15894     }
15895   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15896   if (list)
15897     {
15898       add_AT_location_description (die, attr, list);
15899       return true;
15900     }
15901   /* None of that worked, so it must not really have a location;
15902      try adding a constant value attribute from the DECL_INITIAL.  */
15903   return tree_add_const_value_attribute_for_decl (die, decl);
15904 }
15905
15906 /* Add VARIABLE and DIE into deferred locations list.  */
15907
15908 static void
15909 defer_location (tree variable, dw_die_ref die)
15910 {
15911   deferred_locations entry;
15912   entry.variable = variable;
15913   entry.die = die;
15914   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15915 }
15916
15917 /* Helper function for tree_add_const_value_attribute.  Natively encode
15918    initializer INIT into an array.  Return true if successful.  */
15919
15920 static bool
15921 native_encode_initializer (tree init, unsigned char *array, int size)
15922 {
15923   tree type;
15924
15925   if (init == NULL_TREE)
15926     return false;
15927
15928   STRIP_NOPS (init);
15929   switch (TREE_CODE (init))
15930     {
15931     case STRING_CST:
15932       type = TREE_TYPE (init);
15933       if (TREE_CODE (type) == ARRAY_TYPE)
15934         {
15935           tree enttype = TREE_TYPE (type);
15936           enum machine_mode mode = TYPE_MODE (enttype);
15937
15938           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15939             return false;
15940           if (int_size_in_bytes (type) != size)
15941             return false;
15942           if (size > TREE_STRING_LENGTH (init))
15943             {
15944               memcpy (array, TREE_STRING_POINTER (init),
15945                       TREE_STRING_LENGTH (init));
15946               memset (array + TREE_STRING_LENGTH (init),
15947                       '\0', size - TREE_STRING_LENGTH (init));
15948             }
15949           else
15950             memcpy (array, TREE_STRING_POINTER (init), size);
15951           return true;
15952         }
15953       return false;
15954     case CONSTRUCTOR:
15955       type = TREE_TYPE (init);
15956       if (int_size_in_bytes (type) != size)
15957         return false;
15958       if (TREE_CODE (type) == ARRAY_TYPE)
15959         {
15960           HOST_WIDE_INT min_index;
15961           unsigned HOST_WIDE_INT cnt;
15962           int curpos = 0, fieldsize;
15963           constructor_elt *ce;
15964
15965           if (TYPE_DOMAIN (type) == NULL_TREE
15966               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15967             return false;
15968
15969           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15970           if (fieldsize <= 0)
15971             return false;
15972
15973           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15974           memset (array, '\0', size);
15975           for (cnt = 0;
15976                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15977                cnt++)
15978             {
15979               tree val = ce->value;
15980               tree index = ce->index;
15981               int pos = curpos;
15982               if (index && TREE_CODE (index) == RANGE_EXPR)
15983                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15984                       * fieldsize;
15985               else if (index)
15986                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15987
15988               if (val)
15989                 {
15990                   STRIP_NOPS (val);
15991                   if (!native_encode_initializer (val, array + pos, fieldsize))
15992                     return false;
15993                 }
15994               curpos = pos + fieldsize;
15995               if (index && TREE_CODE (index) == RANGE_EXPR)
15996                 {
15997                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15998                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15999                   while (count > 0)
16000                     {
16001                       if (val)
16002                         memcpy (array + curpos, array + pos, fieldsize);
16003                       curpos += fieldsize;
16004                     }
16005                 }
16006               gcc_assert (curpos <= size);
16007             }
16008           return true;
16009         }
16010       else if (TREE_CODE (type) == RECORD_TYPE
16011                || TREE_CODE (type) == UNION_TYPE)
16012         {
16013           tree field = NULL_TREE;
16014           unsigned HOST_WIDE_INT cnt;
16015           constructor_elt *ce;
16016
16017           if (int_size_in_bytes (type) != size)
16018             return false;
16019
16020           if (TREE_CODE (type) == RECORD_TYPE)
16021             field = TYPE_FIELDS (type);
16022
16023           for (cnt = 0;
16024                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16025                cnt++, field = field ? TREE_CHAIN (field) : 0)
16026             {
16027               tree val = ce->value;
16028               int pos, fieldsize;
16029
16030               if (ce->index != 0)
16031                 field = ce->index;
16032
16033               if (val)
16034                 STRIP_NOPS (val);
16035
16036               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16037                 return false;
16038
16039               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16040                   && TYPE_DOMAIN (TREE_TYPE (field))
16041                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16042                 return false;
16043               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16044                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16045                 return false;
16046               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16047               pos = int_byte_position (field);
16048               gcc_assert (pos + fieldsize <= size);
16049               if (val
16050                   && !native_encode_initializer (val, array + pos, fieldsize))
16051                 return false;
16052             }
16053           return true;
16054         }
16055       return false;
16056     case VIEW_CONVERT_EXPR:
16057     case NON_LVALUE_EXPR:
16058       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16059     default:
16060       return native_encode_expr (init, array, size) == size;
16061     }
16062 }
16063
16064 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16065    attribute is the const value T.  */
16066
16067 static bool
16068 tree_add_const_value_attribute (dw_die_ref die, tree t)
16069 {
16070   tree init;
16071   tree type = TREE_TYPE (t);
16072   rtx rtl;
16073
16074   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16075     return false;
16076
16077   init = t;
16078   gcc_assert (!DECL_P (init));
16079
16080   rtl = rtl_for_decl_init (init, type);
16081   if (rtl)
16082     return add_const_value_attribute (die, rtl);
16083   /* If the host and target are sane, try harder.  */
16084   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16085            && initializer_constant_valid_p (init, type))
16086     {
16087       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16088       if (size > 0 && (int) size == size)
16089         {
16090           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
16091
16092           if (native_encode_initializer (init, array, size))
16093             {
16094               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16095               return true;
16096             }
16097         }
16098     }
16099   return false;
16100 }
16101
16102 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16103    attribute is the const value of T, where T is an integral constant
16104    variable with static storage duration
16105    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16106
16107 static bool
16108 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16109 {
16110
16111   if (!decl
16112       || (TREE_CODE (decl) != VAR_DECL
16113           && TREE_CODE (decl) != CONST_DECL))
16114     return false;
16115
16116     if (TREE_READONLY (decl)
16117         && ! TREE_THIS_VOLATILE (decl)
16118         && DECL_INITIAL (decl))
16119       /* OK */;
16120     else
16121       return false;
16122
16123   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16124   if (get_AT (var_die, DW_AT_const_value))
16125     return false;
16126
16127   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16128 }
16129
16130 /* Convert the CFI instructions for the current function into a
16131    location list.  This is used for DW_AT_frame_base when we targeting
16132    a dwarf2 consumer that does not support the dwarf3
16133    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16134    expressions.  */
16135
16136 static dw_loc_list_ref
16137 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16138 {
16139   dw_fde_ref fde;
16140   dw_loc_list_ref list, *list_tail;
16141   dw_cfi_ref cfi;
16142   dw_cfa_location last_cfa, next_cfa;
16143   const char *start_label, *last_label, *section;
16144   dw_cfa_location remember;
16145
16146   fde = current_fde ();
16147   gcc_assert (fde != NULL);
16148
16149   section = secname_for_decl (current_function_decl);
16150   list_tail = &list;
16151   list = NULL;
16152
16153   memset (&next_cfa, 0, sizeof (next_cfa));
16154   next_cfa.reg = INVALID_REGNUM;
16155   remember = next_cfa;
16156
16157   start_label = fde->dw_fde_begin;
16158
16159   /* ??? Bald assumption that the CIE opcode list does not contain
16160      advance opcodes.  */
16161   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16162     lookup_cfa_1 (cfi, &next_cfa, &remember);
16163
16164   last_cfa = next_cfa;
16165   last_label = start_label;
16166
16167   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16168     switch (cfi->dw_cfi_opc)
16169       {
16170       case DW_CFA_set_loc:
16171       case DW_CFA_advance_loc1:
16172       case DW_CFA_advance_loc2:
16173       case DW_CFA_advance_loc4:
16174         if (!cfa_equal_p (&last_cfa, &next_cfa))
16175           {
16176             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16177                                        start_label, last_label, section);
16178
16179             list_tail = &(*list_tail)->dw_loc_next;
16180             last_cfa = next_cfa;
16181             start_label = last_label;
16182           }
16183         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16184         break;
16185
16186       case DW_CFA_advance_loc:
16187         /* The encoding is complex enough that we should never emit this.  */
16188         gcc_unreachable ();
16189
16190       default:
16191         lookup_cfa_1 (cfi, &next_cfa, &remember);
16192         break;
16193       }
16194
16195   if (!cfa_equal_p (&last_cfa, &next_cfa))
16196     {
16197       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16198                                  start_label, last_label, section);
16199       list_tail = &(*list_tail)->dw_loc_next;
16200       start_label = last_label;
16201     }
16202
16203   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16204                              start_label, fde->dw_fde_end, section);
16205
16206   if (list && list->dw_loc_next)
16207     gen_llsym (list);
16208
16209   return list;
16210 }
16211
16212 /* Compute a displacement from the "steady-state frame pointer" to the
16213    frame base (often the same as the CFA), and store it in
16214    frame_pointer_fb_offset.  OFFSET is added to the displacement
16215    before the latter is negated.  */
16216
16217 static void
16218 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16219 {
16220   rtx reg, elim;
16221
16222 #ifdef FRAME_POINTER_CFA_OFFSET
16223   reg = frame_pointer_rtx;
16224   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16225 #else
16226   reg = arg_pointer_rtx;
16227   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16228 #endif
16229
16230   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16231   if (GET_CODE (elim) == PLUS)
16232     {
16233       offset += INTVAL (XEXP (elim, 1));
16234       elim = XEXP (elim, 0);
16235     }
16236
16237   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16238                && (elim == hard_frame_pointer_rtx
16239                    || elim == stack_pointer_rtx))
16240               || elim == (frame_pointer_needed
16241                           ? hard_frame_pointer_rtx
16242                           : stack_pointer_rtx));
16243
16244   frame_pointer_fb_offset = -offset;
16245 }
16246
16247 /* Generate a DW_AT_name attribute given some string value to be included as
16248    the value of the attribute.  */
16249
16250 static void
16251 add_name_attribute (dw_die_ref die, const char *name_string)
16252 {
16253   if (name_string != NULL && *name_string != 0)
16254     {
16255       if (demangle_name_func)
16256         name_string = (*demangle_name_func) (name_string);
16257
16258       add_AT_string (die, DW_AT_name, name_string);
16259     }
16260 }
16261
16262 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16263
16264 static void
16265 add_comp_dir_attribute (dw_die_ref die)
16266 {
16267   const char *wd = get_src_pwd ();
16268   char *wd1;
16269
16270   if (wd == NULL)
16271     return;
16272
16273   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16274     {
16275       int wdlen;
16276
16277       wdlen = strlen (wd);
16278       wd1 = GGC_NEWVEC (char, wdlen + 2);
16279       strcpy (wd1, wd);
16280       wd1 [wdlen] = DIR_SEPARATOR;
16281       wd1 [wdlen + 1] = 0;
16282       wd = wd1;
16283     }
16284
16285     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16286 }
16287
16288 /* Given a tree node describing an array bound (either lower or upper) output
16289    a representation for that bound.  */
16290
16291 static void
16292 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16293 {
16294   int want_address = 2;
16295
16296   switch (TREE_CODE (bound))
16297     {
16298     case ERROR_MARK:
16299       return;
16300
16301     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
16302     case INTEGER_CST:
16303       {
16304         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16305
16306         /* Use the default if possible.  */
16307         if (bound_attr == DW_AT_lower_bound
16308             && (((is_c_family () || is_java ()) && integer_zerop (bound))
16309                 || (is_fortran () && integer_onep (bound))))
16310           ;
16311
16312         /* Otherwise represent the bound as an unsigned value with the
16313            precision of its type.  The precision and signedness of the
16314            type will be necessary to re-interpret it unambiguously.  */
16315         else if (prec < HOST_BITS_PER_WIDE_INT)
16316           {
16317             unsigned HOST_WIDE_INT mask
16318               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16319             add_AT_unsigned (subrange_die, bound_attr,
16320                              TREE_INT_CST_LOW (bound) & mask);
16321           }
16322         else if (prec == HOST_BITS_PER_WIDE_INT
16323                  || TREE_INT_CST_HIGH (bound) == 0)
16324           add_AT_unsigned (subrange_die, bound_attr,
16325                            TREE_INT_CST_LOW (bound));
16326         else
16327           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16328                          TREE_INT_CST_LOW (bound));
16329       }
16330       break;
16331
16332     CASE_CONVERT:
16333     case VIEW_CONVERT_EXPR:
16334       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16335       break;
16336
16337     case SAVE_EXPR:
16338       break;
16339
16340     case VAR_DECL:
16341     case PARM_DECL:
16342     case RESULT_DECL:
16343       {
16344         dw_die_ref decl_die = lookup_decl_die (bound);
16345
16346         /* ??? Can this happen, or should the variable have been bound
16347            first?  Probably it can, since I imagine that we try to create
16348            the types of parameters in the order in which they exist in
16349            the list, and won't have created a forward reference to a
16350            later parameter.  */
16351         if (decl_die != NULL)
16352           {
16353             add_AT_die_ref (subrange_die, bound_attr, decl_die);
16354             break;
16355           }
16356         want_address = 0;
16357       }
16358       /* FALLTHRU */
16359
16360     default:
16361       {
16362         /* Otherwise try to create a stack operation procedure to
16363            evaluate the value of the array bound.  */
16364
16365         dw_die_ref ctx, decl_die;
16366         dw_loc_list_ref list;
16367
16368         list = loc_list_from_tree (bound, want_address);
16369         if (list == NULL)
16370           break;
16371
16372         if (single_element_loc_list_p (list))
16373           {
16374             add_AT_loc (subrange_die, bound_attr, list->expr);
16375             break;
16376           }
16377
16378         if (current_function_decl == 0)
16379           ctx = comp_unit_die;
16380         else
16381           ctx = lookup_decl_die (current_function_decl);
16382
16383         decl_die = new_die (DW_TAG_variable, ctx, bound);
16384         add_AT_flag (decl_die, DW_AT_artificial, 1);
16385         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16386         add_AT_location_description (decl_die, DW_AT_location, list);
16387         add_AT_die_ref (subrange_die, bound_attr, decl_die);
16388         break;
16389       }
16390     }
16391 }
16392
16393 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16394    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16395    Note that the block of subscript information for an array type also
16396    includes information about the element type of the given array type.  */
16397
16398 static void
16399 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16400 {
16401   unsigned dimension_number;
16402   tree lower, upper;
16403   dw_die_ref subrange_die;
16404
16405   for (dimension_number = 0;
16406        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16407        type = TREE_TYPE (type), dimension_number++)
16408     {
16409       tree domain = TYPE_DOMAIN (type);
16410
16411       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16412         break;
16413
16414       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16415          and (in GNU C only) variable bounds.  Handle all three forms
16416          here.  */
16417       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16418       if (domain)
16419         {
16420           /* We have an array type with specified bounds.  */
16421           lower = TYPE_MIN_VALUE (domain);
16422           upper = TYPE_MAX_VALUE (domain);
16423
16424           /* Define the index type.  */
16425           if (TREE_TYPE (domain))
16426             {
16427               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16428                  TREE_TYPE field.  We can't emit debug info for this
16429                  because it is an unnamed integral type.  */
16430               if (TREE_CODE (domain) == INTEGER_TYPE
16431                   && TYPE_NAME (domain) == NULL_TREE
16432                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16433                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16434                 ;
16435               else
16436                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16437                                     type_die);
16438             }
16439
16440           /* ??? If upper is NULL, the array has unspecified length,
16441              but it does have a lower bound.  This happens with Fortran
16442                dimension arr(N:*)
16443              Since the debugger is definitely going to need to know N
16444              to produce useful results, go ahead and output the lower
16445              bound solo, and hope the debugger can cope.  */
16446
16447           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16448           if (upper)
16449             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16450         }
16451
16452       /* Otherwise we have an array type with an unspecified length.  The
16453          DWARF-2 spec does not say how to handle this; let's just leave out the
16454          bounds.  */
16455     }
16456 }
16457
16458 static void
16459 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16460 {
16461   unsigned size;
16462
16463   switch (TREE_CODE (tree_node))
16464     {
16465     case ERROR_MARK:
16466       size = 0;
16467       break;
16468     case ENUMERAL_TYPE:
16469     case RECORD_TYPE:
16470     case UNION_TYPE:
16471     case QUAL_UNION_TYPE:
16472       size = int_size_in_bytes (tree_node);
16473       break;
16474     case FIELD_DECL:
16475       /* For a data member of a struct or union, the DW_AT_byte_size is
16476          generally given as the number of bytes normally allocated for an
16477          object of the *declared* type of the member itself.  This is true
16478          even for bit-fields.  */
16479       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16480       break;
16481     default:
16482       gcc_unreachable ();
16483     }
16484
16485   /* Note that `size' might be -1 when we get to this point.  If it is, that
16486      indicates that the byte size of the entity in question is variable.  We
16487      have no good way of expressing this fact in Dwarf at the present time,
16488      so just let the -1 pass on through.  */
16489   add_AT_unsigned (die, DW_AT_byte_size, size);
16490 }
16491
16492 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16493    which specifies the distance in bits from the highest order bit of the
16494    "containing object" for the bit-field to the highest order bit of the
16495    bit-field itself.
16496
16497    For any given bit-field, the "containing object" is a hypothetical object
16498    (of some integral or enum type) within which the given bit-field lives.  The
16499    type of this hypothetical "containing object" is always the same as the
16500    declared type of the individual bit-field itself.  The determination of the
16501    exact location of the "containing object" for a bit-field is rather
16502    complicated.  It's handled by the `field_byte_offset' function (above).
16503
16504    Note that it is the size (in bytes) of the hypothetical "containing object"
16505    which will be given in the DW_AT_byte_size attribute for this bit-field.
16506    (See `byte_size_attribute' above).  */
16507
16508 static inline void
16509 add_bit_offset_attribute (dw_die_ref die, tree decl)
16510 {
16511   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16512   tree type = DECL_BIT_FIELD_TYPE (decl);
16513   HOST_WIDE_INT bitpos_int;
16514   HOST_WIDE_INT highest_order_object_bit_offset;
16515   HOST_WIDE_INT highest_order_field_bit_offset;
16516   HOST_WIDE_INT unsigned bit_offset;
16517
16518   /* Must be a field and a bit field.  */
16519   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16520
16521   /* We can't yet handle bit-fields whose offsets are variable, so if we
16522      encounter such things, just return without generating any attribute
16523      whatsoever.  Likewise for variable or too large size.  */
16524   if (! host_integerp (bit_position (decl), 0)
16525       || ! host_integerp (DECL_SIZE (decl), 1))
16526     return;
16527
16528   bitpos_int = int_bit_position (decl);
16529
16530   /* Note that the bit offset is always the distance (in bits) from the
16531      highest-order bit of the "containing object" to the highest-order bit of
16532      the bit-field itself.  Since the "high-order end" of any object or field
16533      is different on big-endian and little-endian machines, the computation
16534      below must take account of these differences.  */
16535   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16536   highest_order_field_bit_offset = bitpos_int;
16537
16538   if (! BYTES_BIG_ENDIAN)
16539     {
16540       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16541       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16542     }
16543
16544   bit_offset
16545     = (! BYTES_BIG_ENDIAN
16546        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16547        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16548
16549   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
16550 }
16551
16552 /* For a FIELD_DECL node which represents a bit field, output an attribute
16553    which specifies the length in bits of the given field.  */
16554
16555 static inline void
16556 add_bit_size_attribute (dw_die_ref die, tree decl)
16557 {
16558   /* Must be a field and a bit field.  */
16559   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16560               && DECL_BIT_FIELD_TYPE (decl));
16561
16562   if (host_integerp (DECL_SIZE (decl), 1))
16563     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16564 }
16565
16566 /* If the compiled language is ANSI C, then add a 'prototyped'
16567    attribute, if arg types are given for the parameters of a function.  */
16568
16569 static inline void
16570 add_prototyped_attribute (dw_die_ref die, tree func_type)
16571 {
16572   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
16573       && TYPE_ARG_TYPES (func_type) != NULL)
16574     add_AT_flag (die, DW_AT_prototyped, 1);
16575 }
16576
16577 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16578    by looking in either the type declaration or object declaration
16579    equate table.  */
16580
16581 static inline dw_die_ref
16582 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16583 {
16584   dw_die_ref origin_die = NULL;
16585
16586   if (TREE_CODE (origin) != FUNCTION_DECL)
16587     {
16588       /* We may have gotten separated from the block for the inlined
16589          function, if we're in an exception handler or some such; make
16590          sure that the abstract function has been written out.
16591
16592          Doing this for nested functions is wrong, however; functions are
16593          distinct units, and our context might not even be inline.  */
16594       tree fn = origin;
16595
16596       if (TYPE_P (fn))
16597         fn = TYPE_STUB_DECL (fn);
16598
16599       fn = decl_function_context (fn);
16600       if (fn)
16601         dwarf2out_abstract_function (fn);
16602     }
16603
16604   if (DECL_P (origin))
16605     origin_die = lookup_decl_die (origin);
16606   else if (TYPE_P (origin))
16607     origin_die = lookup_type_die (origin);
16608
16609   /* XXX: Functions that are never lowered don't always have correct block
16610      trees (in the case of java, they simply have no block tree, in some other
16611      languages).  For these functions, there is nothing we can really do to
16612      output correct debug info for inlined functions in all cases.  Rather
16613      than die, we'll just produce deficient debug info now, in that we will
16614      have variables without a proper abstract origin.  In the future, when all
16615      functions are lowered, we should re-add a gcc_assert (origin_die)
16616      here.  */
16617
16618   if (origin_die)
16619     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16620   return origin_die;
16621 }
16622
16623 /* We do not currently support the pure_virtual attribute.  */
16624
16625 static inline void
16626 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16627 {
16628   if (DECL_VINDEX (func_decl))
16629     {
16630       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16631
16632       if (host_integerp (DECL_VINDEX (func_decl), 0))
16633         add_AT_loc (die, DW_AT_vtable_elem_location,
16634                     new_loc_descr (DW_OP_constu,
16635                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16636                                    0));
16637
16638       /* GNU extension: Record what type this method came from originally.  */
16639       if (debug_info_level > DINFO_LEVEL_TERSE
16640           && DECL_CONTEXT (func_decl))
16641         add_AT_die_ref (die, DW_AT_containing_type,
16642                         lookup_type_die (DECL_CONTEXT (func_decl)));
16643     }
16644 }
16645 \f
16646 /* Add source coordinate attributes for the given decl.  */
16647
16648 static void
16649 add_src_coords_attributes (dw_die_ref die, tree decl)
16650 {
16651   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16652
16653   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16654   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16655 }
16656
16657 /* Add a DW_AT_name attribute and source coordinate attribute for the
16658    given decl, but only if it actually has a name.  */
16659
16660 static void
16661 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16662 {
16663   tree decl_name;
16664
16665   decl_name = DECL_NAME (decl);
16666   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16667     {
16668       const char *name = dwarf2_name (decl, 0);
16669       if (name)
16670         add_name_attribute (die, name);
16671       if (! DECL_ARTIFICIAL (decl))
16672         add_src_coords_attributes (die, decl);
16673
16674       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16675           && TREE_PUBLIC (decl)
16676           && !DECL_ABSTRACT (decl)
16677           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16678           && !is_fortran ())
16679         {
16680           /* Defer until we have an assembler name set.  */
16681           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16682             {
16683               limbo_die_node *asm_name;
16684
16685               asm_name = GGC_CNEW (limbo_die_node);
16686               asm_name->die = die;
16687               asm_name->created_for = decl;
16688               asm_name->next = deferred_asm_name;
16689               deferred_asm_name = asm_name;
16690             }
16691           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16692             add_AT_string (die, DW_AT_MIPS_linkage_name,
16693                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16694         }
16695     }
16696
16697 #ifdef VMS_DEBUGGING_INFO
16698   /* Get the function's name, as described by its RTL.  This may be different
16699      from the DECL_NAME name used in the source file.  */
16700   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16701     {
16702       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16703                    XEXP (DECL_RTL (decl), 0));
16704       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16705     }
16706 #endif
16707 }
16708
16709 /* Push a new declaration scope.  */
16710
16711 static void
16712 push_decl_scope (tree scope)
16713 {
16714   VEC_safe_push (tree, gc, decl_scope_table, scope);
16715 }
16716
16717 /* Pop a declaration scope.  */
16718
16719 static inline void
16720 pop_decl_scope (void)
16721 {
16722   VEC_pop (tree, decl_scope_table);
16723 }
16724
16725 /* Return the DIE for the scope that immediately contains this type.
16726    Non-named types get global scope.  Named types nested in other
16727    types get their containing scope if it's open, or global scope
16728    otherwise.  All other types (i.e. function-local named types) get
16729    the current active scope.  */
16730
16731 static dw_die_ref
16732 scope_die_for (tree t, dw_die_ref context_die)
16733 {
16734   dw_die_ref scope_die = NULL;
16735   tree containing_scope;
16736   int i;
16737
16738   /* Non-types always go in the current scope.  */
16739   gcc_assert (TYPE_P (t));
16740
16741   containing_scope = TYPE_CONTEXT (t);
16742
16743   /* Use the containing namespace if it was passed in (for a declaration).  */
16744   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16745     {
16746       if (context_die == lookup_decl_die (containing_scope))
16747         /* OK */;
16748       else
16749         containing_scope = NULL_TREE;
16750     }
16751
16752   /* Ignore function type "scopes" from the C frontend.  They mean that
16753      a tagged type is local to a parmlist of a function declarator, but
16754      that isn't useful to DWARF.  */
16755   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16756     containing_scope = NULL_TREE;
16757
16758   if (containing_scope == NULL_TREE)
16759     scope_die = comp_unit_die;
16760   else if (TYPE_P (containing_scope))
16761     {
16762       /* For types, we can just look up the appropriate DIE.  But
16763          first we check to see if we're in the middle of emitting it
16764          so we know where the new DIE should go.  */
16765       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16766         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16767           break;
16768
16769       if (i < 0)
16770         {
16771           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16772                       || TREE_ASM_WRITTEN (containing_scope));
16773
16774           /* If none of the current dies are suitable, we get file scope.  */
16775           scope_die = comp_unit_die;
16776         }
16777       else
16778         scope_die = lookup_type_die (containing_scope);
16779     }
16780   else
16781     scope_die = context_die;
16782
16783   return scope_die;
16784 }
16785
16786 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16787
16788 static inline int
16789 local_scope_p (dw_die_ref context_die)
16790 {
16791   for (; context_die; context_die = context_die->die_parent)
16792     if (context_die->die_tag == DW_TAG_inlined_subroutine
16793         || context_die->die_tag == DW_TAG_subprogram)
16794       return 1;
16795
16796   return 0;
16797 }
16798
16799 /* Returns nonzero if CONTEXT_DIE is a class.  */
16800
16801 static inline int
16802 class_scope_p (dw_die_ref context_die)
16803 {
16804   return (context_die
16805           && (context_die->die_tag == DW_TAG_structure_type
16806               || context_die->die_tag == DW_TAG_class_type
16807               || context_die->die_tag == DW_TAG_interface_type
16808               || context_die->die_tag == DW_TAG_union_type));
16809 }
16810
16811 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16812    whether or not to treat a DIE in this context as a declaration.  */
16813
16814 static inline int
16815 class_or_namespace_scope_p (dw_die_ref context_die)
16816 {
16817   return (class_scope_p (context_die)
16818           || (context_die && context_die->die_tag == DW_TAG_namespace));
16819 }
16820
16821 /* Many forms of DIEs require a "type description" attribute.  This
16822    routine locates the proper "type descriptor" die for the type given
16823    by 'type', and adds a DW_AT_type attribute below the given die.  */
16824
16825 static void
16826 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16827                     int decl_volatile, dw_die_ref context_die)
16828 {
16829   enum tree_code code  = TREE_CODE (type);
16830   dw_die_ref type_die  = NULL;
16831
16832   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16833      or fixed-point type, use the inner type.  This is because we have no
16834      support for unnamed types in base_type_die.  This can happen if this is
16835      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16836   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16837       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16838     type = TREE_TYPE (type), code = TREE_CODE (type);
16839
16840   if (code == ERROR_MARK
16841       /* Handle a special case.  For functions whose return type is void, we
16842          generate *no* type attribute.  (Note that no object may have type
16843          `void', so this only applies to function return types).  */
16844       || code == VOID_TYPE)
16845     return;
16846
16847   type_die = modified_type_die (type,
16848                                 decl_const || TYPE_READONLY (type),
16849                                 decl_volatile || TYPE_VOLATILE (type),
16850                                 context_die);
16851
16852   if (type_die != NULL)
16853     add_AT_die_ref (object_die, DW_AT_type, type_die);
16854 }
16855
16856 /* Given an object die, add the calling convention attribute for the
16857    function call type.  */
16858 static void
16859 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16860 {
16861   enum dwarf_calling_convention value = DW_CC_normal;
16862
16863   value = ((enum dwarf_calling_convention)
16864            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16865
16866   /* DWARF doesn't provide a way to identify a program's source-level
16867      entry point.  DW_AT_calling_convention attributes are only meant
16868      to describe functions' calling conventions.  However, lacking a
16869      better way to signal the Fortran main program, we use this for the
16870      time being, following existing custom.  */
16871   if (is_fortran ()
16872       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16873     value = DW_CC_program;
16874
16875   /* Only add the attribute if the backend requests it, and
16876      is not DW_CC_normal.  */
16877   if (value && (value != DW_CC_normal))
16878     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16879 }
16880
16881 /* Given a tree pointer to a struct, class, union, or enum type node, return
16882    a pointer to the (string) tag name for the given type, or zero if the type
16883    was declared without a tag.  */
16884
16885 static const char *
16886 type_tag (const_tree type)
16887 {
16888   const char *name = 0;
16889
16890   if (TYPE_NAME (type) != 0)
16891     {
16892       tree t = 0;
16893
16894       /* Find the IDENTIFIER_NODE for the type name.  */
16895       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
16896         t = TYPE_NAME (type);
16897
16898       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16899          a TYPE_DECL node, regardless of whether or not a `typedef' was
16900          involved.  */
16901       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16902                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16903         {
16904           /* We want to be extra verbose.  Don't call dwarf_name if
16905              DECL_NAME isn't set.  The default hook for decl_printable_name
16906              doesn't like that, and in this context it's correct to return
16907              0, instead of "<anonymous>" or the like.  */
16908           if (DECL_NAME (TYPE_NAME (type)))
16909             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16910         }
16911
16912       /* Now get the name as a string, or invent one.  */
16913       if (!name && t != 0)
16914         name = IDENTIFIER_POINTER (t);
16915     }
16916
16917   return (name == 0 || *name == '\0') ? 0 : name;
16918 }
16919
16920 /* Return the type associated with a data member, make a special check
16921    for bit field types.  */
16922
16923 static inline tree
16924 member_declared_type (const_tree member)
16925 {
16926   return (DECL_BIT_FIELD_TYPE (member)
16927           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16928 }
16929
16930 /* Get the decl's label, as described by its RTL. This may be different
16931    from the DECL_NAME name used in the source file.  */
16932
16933 #if 0
16934 static const char *
16935 decl_start_label (tree decl)
16936 {
16937   rtx x;
16938   const char *fnname;
16939
16940   x = DECL_RTL (decl);
16941   gcc_assert (MEM_P (x));
16942
16943   x = XEXP (x, 0);
16944   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16945
16946   fnname = XSTR (x, 0);
16947   return fnname;
16948 }
16949 #endif
16950 \f
16951 /* These routines generate the internal representation of the DIE's for
16952    the compilation unit.  Debugging information is collected by walking
16953    the declaration trees passed in from dwarf2out_decl().  */
16954
16955 static void
16956 gen_array_type_die (tree type, dw_die_ref context_die)
16957 {
16958   dw_die_ref scope_die = scope_die_for (type, context_die);
16959   dw_die_ref array_die;
16960
16961   /* GNU compilers represent multidimensional array types as sequences of one
16962      dimensional array types whose element types are themselves array types.
16963      We sometimes squish that down to a single array_type DIE with multiple
16964      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16965      say that we are allowed to do this kind of compression in C, because
16966      there is no difference between an array of arrays and a multidimensional
16967      array.  We don't do this for Ada to remain as close as possible to the
16968      actual representation, which is especially important against the language
16969      flexibilty wrt arrays of variable size.  */
16970
16971   bool collapse_nested_arrays = !is_ada ();
16972   tree element_type;
16973
16974   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16975      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16976   if (TYPE_STRING_FLAG (type)
16977       && TREE_CODE (type) == ARRAY_TYPE
16978       && is_fortran ()
16979       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16980     {
16981       HOST_WIDE_INT size;
16982
16983       array_die = new_die (DW_TAG_string_type, scope_die, type);
16984       add_name_attribute (array_die, type_tag (type));
16985       equate_type_number_to_die (type, array_die);
16986       size = int_size_in_bytes (type);
16987       if (size >= 0)
16988         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16989       else if (TYPE_DOMAIN (type) != NULL_TREE
16990                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16991                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16992         {
16993           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16994           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16995
16996           size = int_size_in_bytes (TREE_TYPE (szdecl));
16997           if (loc && size > 0)
16998             {
16999               add_AT_location_description (array_die, DW_AT_string_length, loc);
17000               if (size != DWARF2_ADDR_SIZE)
17001                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17002             }
17003         }
17004       return;
17005     }
17006
17007   /* ??? The SGI dwarf reader fails for array of array of enum types
17008      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17009      array type comes before the outer array type.  We thus call gen_type_die
17010      before we new_die and must prevent nested array types collapsing for this
17011      target.  */
17012
17013 #ifdef MIPS_DEBUGGING_INFO
17014   gen_type_die (TREE_TYPE (type), context_die);
17015   collapse_nested_arrays = false;
17016 #endif
17017
17018   array_die = new_die (DW_TAG_array_type, scope_die, type);
17019   add_name_attribute (array_die, type_tag (type));
17020   equate_type_number_to_die (type, array_die);
17021
17022   if (TREE_CODE (type) == VECTOR_TYPE)
17023     {
17024       /* The frontend feeds us a representation for the vector as a struct
17025          containing an array.  Pull out the array type.  */
17026       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17027       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17028     }
17029
17030   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17031   if (is_fortran ()
17032       && TREE_CODE (type) == ARRAY_TYPE
17033       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17034       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17035     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17036
17037 #if 0
17038   /* We default the array ordering.  SDB will probably do
17039      the right things even if DW_AT_ordering is not present.  It's not even
17040      an issue until we start to get into multidimensional arrays anyway.  If
17041      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17042      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17043      and when we find out that we need to put these in, we will only do so
17044      for multidimensional arrays.  */
17045   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17046 #endif
17047
17048 #ifdef MIPS_DEBUGGING_INFO
17049   /* The SGI compilers handle arrays of unknown bound by setting
17050      AT_declaration and not emitting any subrange DIEs.  */
17051   if (! TYPE_DOMAIN (type))
17052     add_AT_flag (array_die, DW_AT_declaration, 1);
17053   else
17054 #endif
17055     add_subscript_info (array_die, type, collapse_nested_arrays);
17056
17057   /* Add representation of the type of the elements of this array type and
17058      emit the corresponding DIE if we haven't done it already.  */
17059   element_type = TREE_TYPE (type);
17060   if (collapse_nested_arrays)
17061     while (TREE_CODE (element_type) == ARRAY_TYPE)
17062       {
17063         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17064           break;
17065         element_type = TREE_TYPE (element_type);
17066       }
17067
17068 #ifndef MIPS_DEBUGGING_INFO
17069   gen_type_die (element_type, context_die);
17070 #endif
17071
17072   add_type_attribute (array_die, element_type, 0, 0, context_die);
17073
17074   if (get_AT (array_die, DW_AT_name))
17075     add_pubtype (type, array_die);
17076 }
17077
17078 static dw_loc_descr_ref
17079 descr_info_loc (tree val, tree base_decl)
17080 {
17081   HOST_WIDE_INT size;
17082   dw_loc_descr_ref loc, loc2;
17083   enum dwarf_location_atom op;
17084
17085   if (val == base_decl)
17086     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17087
17088   switch (TREE_CODE (val))
17089     {
17090     CASE_CONVERT:
17091       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17092     case VAR_DECL:
17093       return loc_descriptor_from_tree (val, 0);
17094     case INTEGER_CST:
17095       if (host_integerp (val, 0))
17096         return int_loc_descriptor (tree_low_cst (val, 0));
17097       break;
17098     case INDIRECT_REF:
17099       size = int_size_in_bytes (TREE_TYPE (val));
17100       if (size < 0)
17101         break;
17102       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17103       if (!loc)
17104         break;
17105       if (size == DWARF2_ADDR_SIZE)
17106         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17107       else
17108         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17109       return loc;
17110     case POINTER_PLUS_EXPR:
17111     case PLUS_EXPR:
17112       if (host_integerp (TREE_OPERAND (val, 1), 1)
17113           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17114              < 16384)
17115         {
17116           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17117           if (!loc)
17118             break;
17119           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17120         }
17121       else
17122         {
17123           op = DW_OP_plus;
17124         do_binop:
17125           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17126           if (!loc)
17127             break;
17128           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17129           if (!loc2)
17130             break;
17131           add_loc_descr (&loc, loc2);
17132           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17133         }
17134       return loc;
17135     case MINUS_EXPR:
17136       op = DW_OP_minus;
17137       goto do_binop;
17138     case MULT_EXPR:
17139       op = DW_OP_mul;
17140       goto do_binop;
17141     case EQ_EXPR:
17142       op = DW_OP_eq;
17143       goto do_binop;
17144     case NE_EXPR:
17145       op = DW_OP_ne;
17146       goto do_binop;
17147     default:
17148       break;
17149     }
17150   return NULL;
17151 }
17152
17153 static void
17154 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17155                       tree val, tree base_decl)
17156 {
17157   dw_loc_descr_ref loc;
17158
17159   if (host_integerp (val, 0))
17160     {
17161       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17162       return;
17163     }
17164
17165   loc = descr_info_loc (val, base_decl);
17166   if (!loc)
17167     return;
17168
17169   add_AT_loc (die, attr, loc);
17170 }
17171
17172 /* This routine generates DIE for array with hidden descriptor, details
17173    are filled into *info by a langhook.  */
17174
17175 static void
17176 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17177                           dw_die_ref context_die)
17178 {
17179   dw_die_ref scope_die = scope_die_for (type, context_die);
17180   dw_die_ref array_die;
17181   int dim;
17182
17183   array_die = new_die (DW_TAG_array_type, scope_die, type);
17184   add_name_attribute (array_die, type_tag (type));
17185   equate_type_number_to_die (type, array_die);
17186
17187   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17188   if (is_fortran ()
17189       && info->ndimensions >= 2)
17190     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17191
17192   if (info->data_location)
17193     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17194                           info->base_decl);
17195   if (info->associated)
17196     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17197                           info->base_decl);
17198   if (info->allocated)
17199     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17200                           info->base_decl);
17201
17202   for (dim = 0; dim < info->ndimensions; dim++)
17203     {
17204       dw_die_ref subrange_die
17205         = new_die (DW_TAG_subrange_type, array_die, NULL);
17206
17207       if (info->dimen[dim].lower_bound)
17208         {
17209           /* If it is the default value, omit it.  */
17210           if ((is_c_family () || is_java ())
17211               && integer_zerop (info->dimen[dim].lower_bound))
17212             ;
17213           else if (is_fortran ()
17214                    && integer_onep (info->dimen[dim].lower_bound))
17215             ;
17216           else
17217             add_descr_info_field (subrange_die, DW_AT_lower_bound,
17218                                   info->dimen[dim].lower_bound,
17219                                   info->base_decl);
17220         }
17221       if (info->dimen[dim].upper_bound)
17222         add_descr_info_field (subrange_die, DW_AT_upper_bound,
17223                               info->dimen[dim].upper_bound,
17224                               info->base_decl);
17225       if (info->dimen[dim].stride)
17226         add_descr_info_field (subrange_die, DW_AT_byte_stride,
17227                               info->dimen[dim].stride,
17228                               info->base_decl);
17229     }
17230
17231   gen_type_die (info->element_type, context_die);
17232   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17233
17234   if (get_AT (array_die, DW_AT_name))
17235     add_pubtype (type, array_die);
17236 }
17237
17238 #if 0
17239 static void
17240 gen_entry_point_die (tree decl, dw_die_ref context_die)
17241 {
17242   tree origin = decl_ultimate_origin (decl);
17243   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17244
17245   if (origin != NULL)
17246     add_abstract_origin_attribute (decl_die, origin);
17247   else
17248     {
17249       add_name_and_src_coords_attributes (decl_die, decl);
17250       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17251                           0, 0, context_die);
17252     }
17253
17254   if (DECL_ABSTRACT (decl))
17255     equate_decl_number_to_die (decl, decl_die);
17256   else
17257     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17258 }
17259 #endif
17260
17261 /* Walk through the list of incomplete types again, trying once more to
17262    emit full debugging info for them.  */
17263
17264 static void
17265 retry_incomplete_types (void)
17266 {
17267   int i;
17268
17269   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
17270     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
17271                                   DINFO_USAGE_DIR_USE))
17272       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
17273 }
17274
17275 /* Determine what tag to use for a record type.  */
17276
17277 static enum dwarf_tag
17278 record_type_tag (tree type)
17279 {
17280   if (! lang_hooks.types.classify_record)
17281     return DW_TAG_structure_type;
17282
17283   switch (lang_hooks.types.classify_record (type))
17284     {
17285     case RECORD_IS_STRUCT:
17286       return DW_TAG_structure_type;
17287
17288     case RECORD_IS_CLASS:
17289       return DW_TAG_class_type;
17290
17291     case RECORD_IS_INTERFACE:
17292       if (dwarf_version >= 3 || !dwarf_strict)
17293         return DW_TAG_interface_type;
17294       return DW_TAG_structure_type;
17295
17296     default:
17297       gcc_unreachable ();
17298     }
17299 }
17300
17301 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17302    include all of the information about the enumeration values also. Each
17303    enumerated type name/value is listed as a child of the enumerated type
17304    DIE.  */
17305
17306 static dw_die_ref
17307 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17308 {
17309   dw_die_ref type_die = lookup_type_die (type);
17310
17311   if (type_die == NULL)
17312     {
17313       type_die = new_die (DW_TAG_enumeration_type,
17314                           scope_die_for (type, context_die), type);
17315       equate_type_number_to_die (type, type_die);
17316       add_name_attribute (type_die, type_tag (type));
17317     }
17318   else if (! TYPE_SIZE (type))
17319     return type_die;
17320   else
17321     remove_AT (type_die, DW_AT_declaration);
17322
17323   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17324      given enum type is incomplete, do not generate the DW_AT_byte_size
17325      attribute or the DW_AT_element_list attribute.  */
17326   if (TYPE_SIZE (type))
17327     {
17328       tree link;
17329
17330       TREE_ASM_WRITTEN (type) = 1;
17331       add_byte_size_attribute (type_die, type);
17332       if (TYPE_STUB_DECL (type) != NULL_TREE)
17333         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17334
17335       /* If the first reference to this type was as the return type of an
17336          inline function, then it may not have a parent.  Fix this now.  */
17337       if (type_die->die_parent == NULL)
17338         add_child_die (scope_die_for (type, context_die), type_die);
17339
17340       for (link = TYPE_VALUES (type);
17341            link != NULL; link = TREE_CHAIN (link))
17342         {
17343           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17344           tree value = TREE_VALUE (link);
17345
17346           add_name_attribute (enum_die,
17347                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17348
17349           if (TREE_CODE (value) == CONST_DECL)
17350             value = DECL_INITIAL (value);
17351
17352           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17353             /* DWARF2 does not provide a way of indicating whether or
17354                not enumeration constants are signed or unsigned.  GDB
17355                always assumes the values are signed, so we output all
17356                values as if they were signed.  That means that
17357                enumeration constants with very large unsigned values
17358                will appear to have negative values in the debugger.  */
17359             add_AT_int (enum_die, DW_AT_const_value,
17360                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17361         }
17362     }
17363   else
17364     add_AT_flag (type_die, DW_AT_declaration, 1);
17365
17366   if (get_AT (type_die, DW_AT_name))
17367     add_pubtype (type, type_die);
17368
17369   return type_die;
17370 }
17371
17372 /* Generate a DIE to represent either a real live formal parameter decl or to
17373    represent just the type of some formal parameter position in some function
17374    type.
17375
17376    Note that this routine is a bit unusual because its argument may be a
17377    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17378    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17379    node.  If it's the former then this function is being called to output a
17380    DIE to represent a formal parameter object (or some inlining thereof).  If
17381    it's the latter, then this function is only being called to output a
17382    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17383    argument type of some subprogram type.
17384    If EMIT_NAME_P is true, name and source coordinate attributes
17385    are emitted.  */
17386
17387 static dw_die_ref
17388 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17389                           dw_die_ref context_die)
17390 {
17391   tree node_or_origin = node ? node : origin;
17392   dw_die_ref parm_die
17393     = new_die (DW_TAG_formal_parameter, context_die, node);
17394
17395   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17396     {
17397     case tcc_declaration:
17398       if (!origin)
17399         origin = decl_ultimate_origin (node);
17400       if (origin != NULL)
17401         add_abstract_origin_attribute (parm_die, origin);
17402       else
17403         {
17404           tree type = TREE_TYPE (node);
17405           if (emit_name_p)
17406             add_name_and_src_coords_attributes (parm_die, node);
17407           if (decl_by_reference_p (node))
17408             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17409                                 context_die);
17410           else
17411             add_type_attribute (parm_die, type,
17412                                 TREE_READONLY (node),
17413                                 TREE_THIS_VOLATILE (node),
17414                                 context_die);
17415           if (DECL_ARTIFICIAL (node))
17416             add_AT_flag (parm_die, DW_AT_artificial, 1);
17417         }
17418
17419       if (node && node != origin)
17420         equate_decl_number_to_die (node, parm_die);
17421       if (! DECL_ABSTRACT (node_or_origin))
17422         add_location_or_const_value_attribute (parm_die, node_or_origin,
17423                                                DW_AT_location);
17424
17425       break;
17426
17427     case tcc_type:
17428       /* We were called with some kind of a ..._TYPE node.  */
17429       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17430       break;
17431
17432     default:
17433       gcc_unreachable ();
17434     }
17435
17436   return parm_die;
17437 }
17438
17439 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17440    children DW_TAG_formal_parameter DIEs representing the arguments of the
17441    parameter pack.
17442
17443    PARM_PACK must be a function parameter pack.
17444    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17445    must point to the subsequent arguments of the function PACK_ARG belongs to.
17446    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17447    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17448    following the last one for which a DIE was generated.  */
17449
17450 static dw_die_ref
17451 gen_formal_parameter_pack_die  (tree parm_pack,
17452                                 tree pack_arg,
17453                                 dw_die_ref subr_die,
17454                                 tree *next_arg)
17455 {
17456   tree arg;
17457   dw_die_ref parm_pack_die;
17458
17459   gcc_assert (parm_pack
17460               && lang_hooks.function_parameter_pack_p (parm_pack)
17461               && subr_die);
17462
17463   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17464   add_src_coords_attributes (parm_pack_die, parm_pack);
17465
17466   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
17467     {
17468       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17469                                                                  parm_pack))
17470         break;
17471       gen_formal_parameter_die (arg, NULL,
17472                                 false /* Don't emit name attribute.  */,
17473                                 parm_pack_die);
17474     }
17475   if (next_arg)
17476     *next_arg = arg;
17477   return parm_pack_die;
17478 }
17479
17480 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17481    at the end of an (ANSI prototyped) formal parameters list.  */
17482
17483 static void
17484 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17485 {
17486   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17487 }
17488
17489 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17490    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17491    parameters as specified in some function type specification (except for
17492    those which appear as part of a function *definition*).  */
17493
17494 static void
17495 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17496 {
17497   tree link;
17498   tree formal_type = NULL;
17499   tree first_parm_type;
17500   tree arg;
17501
17502   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17503     {
17504       arg = DECL_ARGUMENTS (function_or_method_type);
17505       function_or_method_type = TREE_TYPE (function_or_method_type);
17506     }
17507   else
17508     arg = NULL_TREE;
17509
17510   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17511
17512   /* Make our first pass over the list of formal parameter types and output a
17513      DW_TAG_formal_parameter DIE for each one.  */
17514   for (link = first_parm_type; link; )
17515     {
17516       dw_die_ref parm_die;
17517
17518       formal_type = TREE_VALUE (link);
17519       if (formal_type == void_type_node)
17520         break;
17521
17522       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17523       parm_die = gen_formal_parameter_die (formal_type, NULL,
17524                                            true /* Emit name attribute.  */,
17525                                            context_die);
17526       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
17527            && link == first_parm_type)
17528           || (arg && DECL_ARTIFICIAL (arg)))
17529         add_AT_flag (parm_die, DW_AT_artificial, 1);
17530
17531       link = TREE_CHAIN (link);
17532       if (arg)
17533         arg = TREE_CHAIN (arg);
17534     }
17535
17536   /* If this function type has an ellipsis, add a
17537      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17538   if (formal_type != void_type_node)
17539     gen_unspecified_parameters_die (function_or_method_type, context_die);
17540
17541   /* Make our second (and final) pass over the list of formal parameter types
17542      and output DIEs to represent those types (as necessary).  */
17543   for (link = TYPE_ARG_TYPES (function_or_method_type);
17544        link && TREE_VALUE (link);
17545        link = TREE_CHAIN (link))
17546     gen_type_die (TREE_VALUE (link), context_die);
17547 }
17548
17549 /* We want to generate the DIE for TYPE so that we can generate the
17550    die for MEMBER, which has been defined; we will need to refer back
17551    to the member declaration nested within TYPE.  If we're trying to
17552    generate minimal debug info for TYPE, processing TYPE won't do the
17553    trick; we need to attach the member declaration by hand.  */
17554
17555 static void
17556 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17557 {
17558   gen_type_die (type, context_die);
17559
17560   /* If we're trying to avoid duplicate debug info, we may not have
17561      emitted the member decl for this function.  Emit it now.  */
17562   if (TYPE_STUB_DECL (type)
17563       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17564       && ! lookup_decl_die (member))
17565     {
17566       dw_die_ref type_die;
17567       gcc_assert (!decl_ultimate_origin (member));
17568
17569       push_decl_scope (type);
17570       type_die = lookup_type_die (type);
17571       if (TREE_CODE (member) == FUNCTION_DECL)
17572         gen_subprogram_die (member, type_die);
17573       else if (TREE_CODE (member) == FIELD_DECL)
17574         {
17575           /* Ignore the nameless fields that are used to skip bits but handle
17576              C++ anonymous unions and structs.  */
17577           if (DECL_NAME (member) != NULL_TREE
17578               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17579               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17580             {
17581               gen_type_die (member_declared_type (member), type_die);
17582               gen_field_die (member, type_die);
17583             }
17584         }
17585       else
17586         gen_variable_die (member, NULL_TREE, type_die);
17587
17588       pop_decl_scope ();
17589     }
17590 }
17591
17592 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17593    may later generate inlined and/or out-of-line instances of.  */
17594
17595 static void
17596 dwarf2out_abstract_function (tree decl)
17597 {
17598   dw_die_ref old_die;
17599   tree save_fn;
17600   tree context;
17601   int was_abstract;
17602   htab_t old_decl_loc_table;
17603
17604   /* Make sure we have the actual abstract inline, not a clone.  */
17605   decl = DECL_ORIGIN (decl);
17606
17607   old_die = lookup_decl_die (decl);
17608   if (old_die && get_AT (old_die, DW_AT_inline))
17609     /* We've already generated the abstract instance.  */
17610     return;
17611
17612   /* We can be called while recursively when seeing block defining inlined subroutine
17613      DIE.  Be sure to not clobber the outer location table nor use it or we would
17614      get locations in abstract instantces.  */
17615   old_decl_loc_table = decl_loc_table;
17616   decl_loc_table = NULL;
17617
17618   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17619      we don't get confused by DECL_ABSTRACT.  */
17620   if (debug_info_level > DINFO_LEVEL_TERSE)
17621     {
17622       context = decl_class_context (decl);
17623       if (context)
17624         gen_type_die_for_member
17625           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
17626     }
17627
17628   /* Pretend we've just finished compiling this function.  */
17629   save_fn = current_function_decl;
17630   current_function_decl = decl;
17631   push_cfun (DECL_STRUCT_FUNCTION (decl));
17632
17633   was_abstract = DECL_ABSTRACT (decl);
17634   set_decl_abstract_flags (decl, 1);
17635   dwarf2out_decl (decl);
17636   if (! was_abstract)
17637     set_decl_abstract_flags (decl, 0);
17638
17639   current_function_decl = save_fn;
17640   decl_loc_table = old_decl_loc_table;
17641   pop_cfun ();
17642 }
17643
17644 /* Helper function of premark_used_types() which gets called through
17645    htab_traverse.
17646
17647    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17648    marked as unused by prune_unused_types.  */
17649
17650 static int
17651 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17652 {
17653   tree type;
17654   dw_die_ref die;
17655
17656   type = (tree) *slot;
17657   die = lookup_type_die (type);
17658   if (die != NULL)
17659     die->die_perennial_p = 1;
17660   return 1;
17661 }
17662
17663 /* Helper function of premark_types_used_by_global_vars which gets called
17664    through htab_traverse.
17665
17666    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17667    marked as unused by prune_unused_types. The DIE of the type is marked
17668    only if the global variable using the type will actually be emitted.  */
17669
17670 static int
17671 premark_types_used_by_global_vars_helper (void **slot,
17672                                           void *data ATTRIBUTE_UNUSED)
17673 {
17674   struct types_used_by_vars_entry *entry;
17675   dw_die_ref die;
17676
17677   entry = (struct types_used_by_vars_entry *) *slot;
17678   gcc_assert (entry->type != NULL
17679               && entry->var_decl != NULL);
17680   die = lookup_type_die (entry->type);
17681   if (die)
17682     {
17683       /* Ask cgraph if the global variable really is to be emitted.
17684          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17685       struct varpool_node *node = varpool_node (entry->var_decl);
17686       if (node->needed)
17687         {
17688           die->die_perennial_p = 1;
17689           /* Keep the parent DIEs as well.  */
17690           while ((die = die->die_parent) && die->die_perennial_p == 0)
17691             die->die_perennial_p = 1;
17692         }
17693     }
17694   return 1;
17695 }
17696
17697 /* Mark all members of used_types_hash as perennial.  */
17698
17699 static void
17700 premark_used_types (void)
17701 {
17702   if (cfun && cfun->used_types_hash)
17703     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17704 }
17705
17706 /* Mark all members of types_used_by_vars_entry as perennial.  */
17707
17708 static void
17709 premark_types_used_by_global_vars (void)
17710 {
17711   if (types_used_by_vars_hash)
17712     htab_traverse (types_used_by_vars_hash,
17713                    premark_types_used_by_global_vars_helper, NULL);
17714 }
17715
17716 /* Generate a DIE to represent a declared function (either file-scope or
17717    block-local).  */
17718
17719 static void
17720 gen_subprogram_die (tree decl, dw_die_ref context_die)
17721 {
17722   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17723   tree origin = decl_ultimate_origin (decl);
17724   dw_die_ref subr_die;
17725   tree fn_arg_types;
17726   tree outer_scope;
17727   dw_die_ref old_die = lookup_decl_die (decl);
17728   int declaration = (current_function_decl != decl
17729                      || class_or_namespace_scope_p (context_die));
17730
17731   premark_used_types ();
17732
17733   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17734      started to generate the abstract instance of an inline, decided to output
17735      its containing class, and proceeded to emit the declaration of the inline
17736      from the member list for the class.  If so, DECLARATION takes priority;
17737      we'll get back to the abstract instance when done with the class.  */
17738
17739   /* The class-scope declaration DIE must be the primary DIE.  */
17740   if (origin && declaration && class_or_namespace_scope_p (context_die))
17741     {
17742       origin = NULL;
17743       gcc_assert (!old_die);
17744     }
17745
17746   /* Now that the C++ front end lazily declares artificial member fns, we
17747      might need to retrofit the declaration into its class.  */
17748   if (!declaration && !origin && !old_die
17749       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17750       && !class_or_namespace_scope_p (context_die)
17751       && debug_info_level > DINFO_LEVEL_TERSE)
17752     old_die = force_decl_die (decl);
17753
17754   if (origin != NULL)
17755     {
17756       gcc_assert (!declaration || local_scope_p (context_die));
17757
17758       /* Fixup die_parent for the abstract instance of a nested
17759          inline function.  */
17760       if (old_die && old_die->die_parent == NULL)
17761         add_child_die (context_die, old_die);
17762
17763       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17764       add_abstract_origin_attribute (subr_die, origin);
17765     }
17766   else if (old_die)
17767     {
17768       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17769       struct dwarf_file_data * file_index = lookup_filename (s.file);
17770
17771       if (!get_AT_flag (old_die, DW_AT_declaration)
17772           /* We can have a normal definition following an inline one in the
17773              case of redefinition of GNU C extern inlines.
17774              It seems reasonable to use AT_specification in this case.  */
17775           && !get_AT (old_die, DW_AT_inline))
17776         {
17777           /* Detect and ignore this case, where we are trying to output
17778              something we have already output.  */
17779           return;
17780         }
17781
17782       /* If the definition comes from the same place as the declaration,
17783          maybe use the old DIE.  We always want the DIE for this function
17784          that has the *_pc attributes to be under comp_unit_die so the
17785          debugger can find it.  We also need to do this for abstract
17786          instances of inlines, since the spec requires the out-of-line copy
17787          to have the same parent.  For local class methods, this doesn't
17788          apply; we just use the old DIE.  */
17789       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
17790           && (DECL_ARTIFICIAL (decl)
17791               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17792                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17793                       == (unsigned) s.line))))
17794         {
17795           subr_die = old_die;
17796
17797           /* Clear out the declaration attribute and the formal parameters.
17798              Do not remove all children, because it is possible that this
17799              declaration die was forced using force_decl_die(). In such
17800              cases die that forced declaration die (e.g. TAG_imported_module)
17801              is one of the children that we do not want to remove.  */
17802           remove_AT (subr_die, DW_AT_declaration);
17803           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17804         }
17805       else
17806         {
17807           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17808           add_AT_specification (subr_die, old_die);
17809           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17810             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17811           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17812             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17813         }
17814     }
17815   else
17816     {
17817       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17818
17819       if (TREE_PUBLIC (decl))
17820         add_AT_flag (subr_die, DW_AT_external, 1);
17821
17822       add_name_and_src_coords_attributes (subr_die, decl);
17823       if (debug_info_level > DINFO_LEVEL_TERSE)
17824         {
17825           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17826           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17827                               0, 0, context_die);
17828         }
17829
17830       add_pure_or_virtual_attribute (subr_die, decl);
17831       if (DECL_ARTIFICIAL (decl))
17832         add_AT_flag (subr_die, DW_AT_artificial, 1);
17833
17834       if (TREE_PROTECTED (decl))
17835         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
17836       else if (TREE_PRIVATE (decl))
17837         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
17838     }
17839
17840   if (declaration)
17841     {
17842       if (!old_die || !get_AT (old_die, DW_AT_inline))
17843         {
17844           add_AT_flag (subr_die, DW_AT_declaration, 1);
17845
17846           /* If this is an explicit function declaration then generate
17847              a DW_AT_explicit attribute.  */
17848           if (lang_hooks.decls.function_decl_explicit_p (decl)
17849               && (dwarf_version >= 3 || !dwarf_strict))
17850             add_AT_flag (subr_die, DW_AT_explicit, 1);
17851
17852           /* The first time we see a member function, it is in the context of
17853              the class to which it belongs.  We make sure of this by emitting
17854              the class first.  The next time is the definition, which is
17855              handled above.  The two may come from the same source text.
17856
17857              Note that force_decl_die() forces function declaration die. It is
17858              later reused to represent definition.  */
17859           equate_decl_number_to_die (decl, subr_die);
17860         }
17861     }
17862   else if (DECL_ABSTRACT (decl))
17863     {
17864       if (DECL_DECLARED_INLINE_P (decl))
17865         {
17866           if (cgraph_function_possibly_inlined_p (decl))
17867             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17868           else
17869             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17870         }
17871       else
17872         {
17873           if (cgraph_function_possibly_inlined_p (decl))
17874             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17875           else
17876             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17877         }
17878
17879       if (DECL_DECLARED_INLINE_P (decl)
17880           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17881         add_AT_flag (subr_die, DW_AT_artificial, 1);
17882
17883       equate_decl_number_to_die (decl, subr_die);
17884     }
17885   else if (!DECL_EXTERNAL (decl))
17886     {
17887       HOST_WIDE_INT cfa_fb_offset;
17888
17889       if (!old_die || !get_AT (old_die, DW_AT_inline))
17890         equate_decl_number_to_die (decl, subr_die);
17891
17892       if (!flag_reorder_blocks_and_partition)
17893         {
17894           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17895                                        current_function_funcdef_no);
17896           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17897           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17898                                        current_function_funcdef_no);
17899           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17900
17901           add_pubname (decl, subr_die);
17902           add_arange (decl, subr_die);
17903         }
17904       else
17905         {  /* Do nothing for now; maybe need to duplicate die, one for
17906               hot section and one for cold section, then use the hot/cold
17907               section begin/end labels to generate the aranges...  */
17908           /*
17909             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
17910             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
17911             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
17912             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
17913
17914             add_pubname (decl, subr_die);
17915             add_arange (decl, subr_die);
17916             add_arange (decl, subr_die);
17917            */
17918         }
17919
17920 #ifdef MIPS_DEBUGGING_INFO
17921       /* Add a reference to the FDE for this routine.  */
17922       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17923 #endif
17924
17925       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17926
17927       /* We define the "frame base" as the function's CFA.  This is more
17928          convenient for several reasons: (1) It's stable across the prologue
17929          and epilogue, which makes it better than just a frame pointer,
17930          (2) With dwarf3, there exists a one-byte encoding that allows us
17931          to reference the .debug_frame data by proxy, but failing that,
17932          (3) We can at least reuse the code inspection and interpretation
17933          code that determines the CFA position at various points in the
17934          function.  */
17935       if (dwarf_version >= 3)
17936         {
17937           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17938           add_AT_loc (subr_die, DW_AT_frame_base, op);
17939         }
17940       else
17941         {
17942           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17943           if (list->dw_loc_next)
17944             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17945           else
17946             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17947         }
17948
17949       /* Compute a displacement from the "steady-state frame pointer" to
17950          the CFA.  The former is what all stack slots and argument slots
17951          will reference in the rtl; the later is what we've told the
17952          debugger about.  We'll need to adjust all frame_base references
17953          by this displacement.  */
17954       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17955
17956       if (cfun->static_chain_decl)
17957         add_AT_location_description (subr_die, DW_AT_static_link,
17958                  loc_list_from_tree (cfun->static_chain_decl, 2));
17959     }
17960
17961   /* Generate child dies for template paramaters.  */
17962   if (debug_info_level > DINFO_LEVEL_TERSE)
17963     gen_generic_params_dies (decl);
17964
17965   /* Now output descriptions of the arguments for this function. This gets
17966      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17967      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17968      `...' at the end of the formal parameter list.  In order to find out if
17969      there was a trailing ellipsis or not, we must instead look at the type
17970      associated with the FUNCTION_DECL.  This will be a node of type
17971      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17972      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17973      an ellipsis at the end.  */
17974
17975   /* In the case where we are describing a mere function declaration, all we
17976      need to do here (and all we *can* do here) is to describe the *types* of
17977      its formal parameters.  */
17978   if (debug_info_level <= DINFO_LEVEL_TERSE)
17979     ;
17980   else if (declaration)
17981     gen_formal_types_die (decl, subr_die);
17982   else
17983     {
17984       /* Generate DIEs to represent all known formal parameters.  */
17985       tree parm = DECL_ARGUMENTS (decl);
17986       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17987       tree generic_decl_parm = generic_decl
17988                                 ? DECL_ARGUMENTS (generic_decl)
17989                                 : NULL;
17990
17991       /* Now we want to walk the list of parameters of the function and
17992          emit their relevant DIEs.
17993
17994          We consider the case of DECL being an instance of a generic function
17995          as well as it being a normal function.
17996
17997          If DECL is an instance of a generic function we walk the
17998          parameters of the generic function declaration _and_ the parameters of
17999          DECL itself. This is useful because we want to emit specific DIEs for
18000          function parameter packs and those are declared as part of the
18001          generic function declaration. In that particular case,
18002          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18003          That DIE has children DIEs representing the set of arguments
18004          of the pack. Note that the set of pack arguments can be empty.
18005          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18006          children DIE.
18007
18008          Otherwise, we just consider the parameters of DECL.  */
18009       while (generic_decl_parm || parm)
18010         {
18011           if (generic_decl_parm
18012               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18013             gen_formal_parameter_pack_die (generic_decl_parm,
18014                                            parm, subr_die,
18015                                            &parm);
18016           else if (parm)
18017             {
18018               gen_decl_die (parm, NULL, subr_die);
18019               parm = TREE_CHAIN (parm);
18020             }
18021
18022           if (generic_decl_parm)
18023             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
18024         }
18025
18026       /* Decide whether we need an unspecified_parameters DIE at the end.
18027          There are 2 more cases to do this for: 1) the ansi ... declaration -
18028          this is detectable when the end of the arg list is not a
18029          void_type_node 2) an unprototyped function declaration (not a
18030          definition).  This just means that we have no info about the
18031          parameters at all.  */
18032       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18033       if (fn_arg_types != NULL)
18034         {
18035           /* This is the prototyped case, check for....  */
18036           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18037             gen_unspecified_parameters_die (decl, subr_die);
18038         }
18039       else if (DECL_INITIAL (decl) == NULL_TREE)
18040         gen_unspecified_parameters_die (decl, subr_die);
18041     }
18042
18043   /* Output Dwarf info for all of the stuff within the body of the function
18044      (if it has one - it may be just a declaration).  */
18045   outer_scope = DECL_INITIAL (decl);
18046
18047   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18048      a function.  This BLOCK actually represents the outermost binding contour
18049      for the function, i.e. the contour in which the function's formal
18050      parameters and labels get declared. Curiously, it appears that the front
18051      end doesn't actually put the PARM_DECL nodes for the current function onto
18052      the BLOCK_VARS list for this outer scope, but are strung off of the
18053      DECL_ARGUMENTS list for the function instead.
18054
18055      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18056      the LABEL_DECL nodes for the function however, and we output DWARF info
18057      for those in decls_for_scope.  Just within the `outer_scope' there will be
18058      a BLOCK node representing the function's outermost pair of curly braces,
18059      and any blocks used for the base and member initializers of a C++
18060      constructor function.  */
18061   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18062     {
18063       /* Emit a DW_TAG_variable DIE for a named return value.  */
18064       if (DECL_NAME (DECL_RESULT (decl)))
18065         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18066
18067       current_function_has_inlines = 0;
18068       decls_for_scope (outer_scope, subr_die, 0);
18069
18070 #if 0 && defined (MIPS_DEBUGGING_INFO)
18071       if (current_function_has_inlines)
18072         {
18073           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18074           if (! comp_unit_has_inlines)
18075             {
18076               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18077               comp_unit_has_inlines = 1;
18078             }
18079         }
18080 #endif
18081     }
18082   /* Add the calling convention attribute if requested.  */
18083   add_calling_convention_attribute (subr_die, decl);
18084
18085 }
18086
18087 /* Returns a hash value for X (which really is a die_struct).  */
18088
18089 static hashval_t
18090 common_block_die_table_hash (const void *x)
18091 {
18092   const_dw_die_ref d = (const_dw_die_ref) x;
18093   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18094 }
18095
18096 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18097    as decl_id and die_parent of die_struct Y.  */
18098
18099 static int
18100 common_block_die_table_eq (const void *x, const void *y)
18101 {
18102   const_dw_die_ref d = (const_dw_die_ref) x;
18103   const_dw_die_ref e = (const_dw_die_ref) y;
18104   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18105 }
18106
18107 /* Generate a DIE to represent a declared data object.
18108    Either DECL or ORIGIN must be non-null.  */
18109
18110 static void
18111 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18112 {
18113   HOST_WIDE_INT off;
18114   tree com_decl;
18115   tree decl_or_origin = decl ? decl : origin;
18116   dw_die_ref var_die;
18117   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18118   dw_die_ref origin_die;
18119   int declaration = (DECL_EXTERNAL (decl_or_origin)
18120                      || class_or_namespace_scope_p (context_die));
18121
18122   if (!origin)
18123     origin = decl_ultimate_origin (decl);
18124
18125   com_decl = fortran_common (decl_or_origin, &off);
18126
18127   /* Symbol in common gets emitted as a child of the common block, in the form
18128      of a data member.  */
18129   if (com_decl)
18130     {
18131       dw_die_ref com_die;
18132       dw_loc_list_ref loc;
18133       die_node com_die_arg;
18134
18135       var_die = lookup_decl_die (decl_or_origin);
18136       if (var_die)
18137         {
18138           if (get_AT (var_die, DW_AT_location) == NULL)
18139             {
18140               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18141               if (loc)
18142                 {
18143                   if (off)
18144                     {
18145                       /* Optimize the common case.  */
18146                       if (single_element_loc_list_p (loc)
18147                           && loc->expr->dw_loc_opc == DW_OP_addr
18148                           && loc->expr->dw_loc_next == NULL
18149                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18150                              == SYMBOL_REF)
18151                         loc->expr->dw_loc_oprnd1.v.val_addr
18152                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18153                         else
18154                           loc_list_plus_const (loc, off);
18155                     }
18156                   add_AT_location_description (var_die, DW_AT_location, loc);
18157                   remove_AT (var_die, DW_AT_declaration);
18158                 }
18159             }
18160           return;
18161         }
18162
18163       if (common_block_die_table == NULL)
18164         common_block_die_table
18165           = htab_create_ggc (10, common_block_die_table_hash,
18166                              common_block_die_table_eq, NULL);
18167
18168       com_die_arg.decl_id = DECL_UID (com_decl);
18169       com_die_arg.die_parent = context_die;
18170       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18171       loc = loc_list_from_tree (com_decl, 2);
18172       if (com_die == NULL)
18173         {
18174           const char *cnam
18175             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18176           void **slot;
18177
18178           com_die = new_die (DW_TAG_common_block, context_die, decl);
18179           add_name_and_src_coords_attributes (com_die, com_decl);
18180           if (loc)
18181             {
18182               add_AT_location_description (com_die, DW_AT_location, loc);
18183               /* Avoid sharing the same loc descriptor between
18184                  DW_TAG_common_block and DW_TAG_variable.  */
18185               loc = loc_list_from_tree (com_decl, 2);
18186             }
18187           else if (DECL_EXTERNAL (decl))
18188             add_AT_flag (com_die, DW_AT_declaration, 1);
18189           add_pubname_string (cnam, com_die); /* ??? needed? */
18190           com_die->decl_id = DECL_UID (com_decl);
18191           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18192           *slot = (void *) com_die;
18193         }
18194       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18195         {
18196           add_AT_location_description (com_die, DW_AT_location, loc);
18197           loc = loc_list_from_tree (com_decl, 2);
18198           remove_AT (com_die, DW_AT_declaration);
18199         }
18200       var_die = new_die (DW_TAG_variable, com_die, decl);
18201       add_name_and_src_coords_attributes (var_die, decl);
18202       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18203                           TREE_THIS_VOLATILE (decl), context_die);
18204       add_AT_flag (var_die, DW_AT_external, 1);
18205       if (loc)
18206         {
18207           if (off)
18208             {
18209               /* Optimize the common case.  */
18210               if (single_element_loc_list_p (loc)
18211                   && loc->expr->dw_loc_opc == DW_OP_addr
18212                   && loc->expr->dw_loc_next == NULL
18213                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18214                 loc->expr->dw_loc_oprnd1.v.val_addr
18215                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18216               else
18217                 loc_list_plus_const (loc, off);
18218             }
18219           add_AT_location_description (var_die, DW_AT_location, loc);
18220         }
18221       else if (DECL_EXTERNAL (decl))
18222         add_AT_flag (var_die, DW_AT_declaration, 1);
18223       equate_decl_number_to_die (decl, var_die);
18224       return;
18225     }
18226
18227   /* If the compiler emitted a definition for the DECL declaration
18228      and if we already emitted a DIE for it, don't emit a second
18229      DIE for it again.  */
18230   if (old_die
18231       && declaration)
18232     return;
18233
18234   /* For static data members, the declaration in the class is supposed
18235      to have DW_TAG_member tag; the specification should still be
18236      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18237   if (declaration && class_scope_p (context_die))
18238     var_die = new_die (DW_TAG_member, context_die, decl);
18239   else
18240     var_die = new_die (DW_TAG_variable, context_die, decl);
18241
18242   origin_die = NULL;
18243   if (origin != NULL)
18244     origin_die = add_abstract_origin_attribute (var_die, origin);
18245
18246   /* Loop unrolling can create multiple blocks that refer to the same
18247      static variable, so we must test for the DW_AT_declaration flag.
18248
18249      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18250      copy decls and set the DECL_ABSTRACT flag on them instead of
18251      sharing them.
18252
18253      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18254
18255      ??? The declare_in_namespace support causes us to get two DIEs for one
18256      variable, both of which are declarations.  We want to avoid considering
18257      one to be a specification, so we must test that this DIE is not a
18258      declaration.  */
18259   else if (old_die && TREE_STATIC (decl) && ! declaration
18260            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18261     {
18262       /* This is a definition of a C++ class level static.  */
18263       add_AT_specification (var_die, old_die);
18264       if (DECL_NAME (decl))
18265         {
18266           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18267           struct dwarf_file_data * file_index = lookup_filename (s.file);
18268
18269           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18270             add_AT_file (var_die, DW_AT_decl_file, file_index);
18271
18272           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18273             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18274         }
18275     }
18276   else
18277     {
18278       tree type = TREE_TYPE (decl);
18279
18280       add_name_and_src_coords_attributes (var_die, decl);
18281       if (decl_by_reference_p (decl))
18282         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18283       else
18284         add_type_attribute (var_die, type, TREE_READONLY (decl),
18285                             TREE_THIS_VOLATILE (decl), context_die);
18286
18287       if (TREE_PUBLIC (decl))
18288         add_AT_flag (var_die, DW_AT_external, 1);
18289
18290       if (DECL_ARTIFICIAL (decl))
18291         add_AT_flag (var_die, DW_AT_artificial, 1);
18292
18293       if (TREE_PROTECTED (decl))
18294         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
18295       else if (TREE_PRIVATE (decl))
18296         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
18297     }
18298
18299   if (declaration)
18300     add_AT_flag (var_die, DW_AT_declaration, 1);
18301
18302   if (decl && (DECL_ABSTRACT (decl) || declaration))
18303     equate_decl_number_to_die (decl, var_die);
18304
18305   if (! declaration
18306       && (! DECL_ABSTRACT (decl_or_origin)
18307           /* Local static vars are shared between all clones/inlines,
18308              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18309              already set.  */
18310           || (TREE_CODE (decl_or_origin) == VAR_DECL
18311               && TREE_STATIC (decl_or_origin)
18312               && DECL_RTL_SET_P (decl_or_origin)))
18313       /* When abstract origin already has DW_AT_location attribute, no need
18314          to add it again.  */
18315       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18316     {
18317       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18318           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18319         defer_location (decl_or_origin, var_die);
18320       else
18321         add_location_or_const_value_attribute (var_die,
18322                                                decl_or_origin,
18323                                                DW_AT_location);
18324       add_pubname (decl_or_origin, var_die);
18325     }
18326   else
18327     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18328 }
18329
18330 /* Generate a DIE to represent a named constant.  */
18331
18332 static void
18333 gen_const_die (tree decl, dw_die_ref context_die)
18334 {
18335   dw_die_ref const_die;
18336   tree type = TREE_TYPE (decl);
18337
18338   const_die = new_die (DW_TAG_constant, context_die, decl);
18339   add_name_and_src_coords_attributes (const_die, decl);
18340   add_type_attribute (const_die, type, 1, 0, context_die);
18341   if (TREE_PUBLIC (decl))
18342     add_AT_flag (const_die, DW_AT_external, 1);
18343   if (DECL_ARTIFICIAL (decl))
18344     add_AT_flag (const_die, DW_AT_artificial, 1);
18345   tree_add_const_value_attribute_for_decl (const_die, decl);
18346 }
18347
18348 /* Generate a DIE to represent a label identifier.  */
18349
18350 static void
18351 gen_label_die (tree decl, dw_die_ref context_die)
18352 {
18353   tree origin = decl_ultimate_origin (decl);
18354   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18355   rtx insn;
18356   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18357
18358   if (origin != NULL)
18359     add_abstract_origin_attribute (lbl_die, origin);
18360   else
18361     add_name_and_src_coords_attributes (lbl_die, decl);
18362
18363   if (DECL_ABSTRACT (decl))
18364     equate_decl_number_to_die (decl, lbl_die);
18365   else
18366     {
18367       insn = DECL_RTL_IF_SET (decl);
18368
18369       /* Deleted labels are programmer specified labels which have been
18370          eliminated because of various optimizations.  We still emit them
18371          here so that it is possible to put breakpoints on them.  */
18372       if (insn
18373           && (LABEL_P (insn)
18374               || ((NOTE_P (insn)
18375                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18376         {
18377           /* When optimization is enabled (via -O) some parts of the compiler
18378              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18379              represent source-level labels which were explicitly declared by
18380              the user.  This really shouldn't be happening though, so catch
18381              it if it ever does happen.  */
18382           gcc_assert (!INSN_DELETED_P (insn));
18383
18384           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18385           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18386         }
18387     }
18388 }
18389
18390 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18391    attributes to the DIE for a block STMT, to describe where the inlined
18392    function was called from.  This is similar to add_src_coords_attributes.  */
18393
18394 static inline void
18395 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18396 {
18397   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18398
18399   if (dwarf_version >= 3 || !dwarf_strict)
18400     {
18401       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18402       add_AT_unsigned (die, DW_AT_call_line, s.line);
18403     }
18404 }
18405
18406
18407 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18408    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18409
18410 static inline void
18411 add_high_low_attributes (tree stmt, dw_die_ref die)
18412 {
18413   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18414
18415   if (BLOCK_FRAGMENT_CHAIN (stmt)
18416       && (dwarf_version >= 3 || !dwarf_strict))
18417     {
18418       tree chain;
18419
18420       if (inlined_function_outer_scope_p (stmt))
18421         {
18422           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18423                                        BLOCK_NUMBER (stmt));
18424           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18425         }
18426
18427       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18428
18429       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18430       do
18431         {
18432           add_ranges (chain);
18433           chain = BLOCK_FRAGMENT_CHAIN (chain);
18434         }
18435       while (chain);
18436       add_ranges (NULL);
18437     }
18438   else
18439     {
18440       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18441                                    BLOCK_NUMBER (stmt));
18442       add_AT_lbl_id (die, DW_AT_low_pc, label);
18443       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18444                                    BLOCK_NUMBER (stmt));
18445       add_AT_lbl_id (die, DW_AT_high_pc, label);
18446     }
18447 }
18448
18449 /* Generate a DIE for a lexical block.  */
18450
18451 static void
18452 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18453 {
18454   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18455
18456   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18457     add_high_low_attributes (stmt, stmt_die);
18458
18459   decls_for_scope (stmt, stmt_die, depth);
18460 }
18461
18462 /* Generate a DIE for an inlined subprogram.  */
18463
18464 static void
18465 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18466 {
18467   tree decl;
18468
18469   /* The instance of function that is effectively being inlined shall not
18470      be abstract.  */
18471   gcc_assert (! BLOCK_ABSTRACT (stmt));
18472
18473   decl = block_ultimate_origin (stmt);
18474
18475   /* Emit info for the abstract instance first, if we haven't yet.  We
18476      must emit this even if the block is abstract, otherwise when we
18477      emit the block below (or elsewhere), we may end up trying to emit
18478      a die whose origin die hasn't been emitted, and crashing.  */
18479   dwarf2out_abstract_function (decl);
18480
18481   if (! BLOCK_ABSTRACT (stmt))
18482     {
18483       dw_die_ref subr_die
18484         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18485
18486       add_abstract_origin_attribute (subr_die, decl);
18487       if (TREE_ASM_WRITTEN (stmt))
18488         add_high_low_attributes (stmt, subr_die);
18489       add_call_src_coords_attributes (stmt, subr_die);
18490
18491       decls_for_scope (stmt, subr_die, depth);
18492       current_function_has_inlines = 1;
18493     }
18494 }
18495
18496 /* Generate a DIE for a field in a record, or structure.  */
18497
18498 static void
18499 gen_field_die (tree decl, dw_die_ref context_die)
18500 {
18501   dw_die_ref decl_die;
18502
18503   if (TREE_TYPE (decl) == error_mark_node)
18504     return;
18505
18506   decl_die = new_die (DW_TAG_member, context_die, decl);
18507   add_name_and_src_coords_attributes (decl_die, decl);
18508   add_type_attribute (decl_die, member_declared_type (decl),
18509                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18510                       context_die);
18511
18512   if (DECL_BIT_FIELD_TYPE (decl))
18513     {
18514       add_byte_size_attribute (decl_die, decl);
18515       add_bit_size_attribute (decl_die, decl);
18516       add_bit_offset_attribute (decl_die, decl);
18517     }
18518
18519   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18520     add_data_member_location_attribute (decl_die, decl);
18521
18522   if (DECL_ARTIFICIAL (decl))
18523     add_AT_flag (decl_die, DW_AT_artificial, 1);
18524
18525   if (TREE_PROTECTED (decl))
18526     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
18527   else if (TREE_PRIVATE (decl))
18528     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
18529
18530   /* Equate decl number to die, so that we can look up this decl later on.  */
18531   equate_decl_number_to_die (decl, decl_die);
18532 }
18533
18534 #if 0
18535 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18536    Use modified_type_die instead.
18537    We keep this code here just in case these types of DIEs may be needed to
18538    represent certain things in other languages (e.g. Pascal) someday.  */
18539
18540 static void
18541 gen_pointer_type_die (tree type, dw_die_ref context_die)
18542 {
18543   dw_die_ref ptr_die
18544     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18545
18546   equate_type_number_to_die (type, ptr_die);
18547   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18548   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18549 }
18550
18551 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18552    Use modified_type_die instead.
18553    We keep this code here just in case these types of DIEs may be needed to
18554    represent certain things in other languages (e.g. Pascal) someday.  */
18555
18556 static void
18557 gen_reference_type_die (tree type, dw_die_ref context_die)
18558 {
18559   dw_die_ref ref_die
18560     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
18561
18562   equate_type_number_to_die (type, ref_die);
18563   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18564   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18565 }
18566 #endif
18567
18568 /* Generate a DIE for a pointer to a member type.  */
18569
18570 static void
18571 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18572 {
18573   dw_die_ref ptr_die
18574     = new_die (DW_TAG_ptr_to_member_type,
18575                scope_die_for (type, context_die), type);
18576
18577   equate_type_number_to_die (type, ptr_die);
18578   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18579                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18580   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18581 }
18582
18583 /* Generate the DIE for the compilation unit.  */
18584
18585 static dw_die_ref
18586 gen_compile_unit_die (const char *filename)
18587 {
18588   dw_die_ref die;
18589   char producer[250];
18590   const char *language_string = lang_hooks.name;
18591   int language;
18592
18593   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18594
18595   if (filename)
18596     {
18597       add_name_attribute (die, filename);
18598       /* Don't add cwd for <built-in>.  */
18599       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18600         add_comp_dir_attribute (die);
18601     }
18602
18603   sprintf (producer, "%s %s", language_string, version_string);
18604
18605 #ifdef MIPS_DEBUGGING_INFO
18606   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18607      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18608      not appear in the producer string, the debugger reaches the conclusion
18609      that the object file is stripped and has no debugging information.
18610      To get the MIPS/SGI debugger to believe that there is debugging
18611      information in the object file, we add a -g to the producer string.  */
18612   if (debug_info_level > DINFO_LEVEL_TERSE)
18613     strcat (producer, " -g");
18614 #endif
18615
18616   add_AT_string (die, DW_AT_producer, producer);
18617
18618   language = DW_LANG_C89;
18619   if (strcmp (language_string, "GNU C++") == 0)
18620     language = DW_LANG_C_plus_plus;
18621   else if (strcmp (language_string, "GNU F77") == 0)
18622     language = DW_LANG_Fortran77;
18623   else if (strcmp (language_string, "GNU Pascal") == 0)
18624     language = DW_LANG_Pascal83;
18625   else if (dwarf_version >= 3 || !dwarf_strict)
18626     {
18627       if (strcmp (language_string, "GNU Ada") == 0)
18628         language = DW_LANG_Ada95;
18629       else if (strcmp (language_string, "GNU Fortran") == 0)
18630         language = DW_LANG_Fortran95;
18631       else if (strcmp (language_string, "GNU Java") == 0)
18632         language = DW_LANG_Java;
18633       else if (strcmp (language_string, "GNU Objective-C") == 0)
18634         language = DW_LANG_ObjC;
18635       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18636         language = DW_LANG_ObjC_plus_plus;
18637     }
18638
18639   add_AT_unsigned (die, DW_AT_language, language);
18640   return die;
18641 }
18642
18643 /* Generate the DIE for a base class.  */
18644
18645 static void
18646 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18647 {
18648   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18649
18650   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18651   add_data_member_location_attribute (die, binfo);
18652
18653   if (BINFO_VIRTUAL_P (binfo))
18654     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18655
18656   if (access == access_public_node)
18657     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18658   else if (access == access_protected_node)
18659     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18660 }
18661
18662 /* Generate a DIE for a class member.  */
18663
18664 static void
18665 gen_member_die (tree type, dw_die_ref context_die)
18666 {
18667   tree member;
18668   tree binfo = TYPE_BINFO (type);
18669   dw_die_ref child;
18670
18671   /* If this is not an incomplete type, output descriptions of each of its
18672      members. Note that as we output the DIEs necessary to represent the
18673      members of this record or union type, we will also be trying to output
18674      DIEs to represent the *types* of those members. However the `type'
18675      function (above) will specifically avoid generating type DIEs for member
18676      types *within* the list of member DIEs for this (containing) type except
18677      for those types (of members) which are explicitly marked as also being
18678      members of this (containing) type themselves.  The g++ front- end can
18679      force any given type to be treated as a member of some other (containing)
18680      type by setting the TYPE_CONTEXT of the given (member) type to point to
18681      the TREE node representing the appropriate (containing) type.  */
18682
18683   /* First output info about the base classes.  */
18684   if (binfo)
18685     {
18686       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18687       int i;
18688       tree base;
18689
18690       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18691         gen_inheritance_die (base,
18692                              (accesses ? VEC_index (tree, accesses, i)
18693                               : access_public_node), context_die);
18694     }
18695
18696   /* Now output info about the data members and type members.  */
18697   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
18698     {
18699       /* If we thought we were generating minimal debug info for TYPE
18700          and then changed our minds, some of the member declarations
18701          may have already been defined.  Don't define them again, but
18702          do put them in the right order.  */
18703
18704       child = lookup_decl_die (member);
18705       if (child)
18706         splice_child_die (context_die, child);
18707       else
18708         gen_decl_die (member, NULL, context_die);
18709     }
18710
18711   /* Now output info about the function members (if any).  */
18712   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
18713     {
18714       /* Don't include clones in the member list.  */
18715       if (DECL_ABSTRACT_ORIGIN (member))
18716         continue;
18717
18718       child = lookup_decl_die (member);
18719       if (child)
18720         splice_child_die (context_die, child);
18721       else
18722         gen_decl_die (member, NULL, context_die);
18723     }
18724 }
18725
18726 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18727    is set, we pretend that the type was never defined, so we only get the
18728    member DIEs needed by later specification DIEs.  */
18729
18730 static void
18731 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18732                                 enum debug_info_usage usage)
18733 {
18734   dw_die_ref type_die = lookup_type_die (type);
18735   dw_die_ref scope_die = 0;
18736   int nested = 0;
18737   int complete = (TYPE_SIZE (type)
18738                   && (! TYPE_STUB_DECL (type)
18739                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18740   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18741   complete = complete && should_emit_struct_debug (type, usage);
18742
18743   if (type_die && ! complete)
18744     return;
18745
18746   if (TYPE_CONTEXT (type) != NULL_TREE
18747       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18748           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18749     nested = 1;
18750
18751   scope_die = scope_die_for (type, context_die);
18752
18753   if (! type_die || (nested && scope_die == comp_unit_die))
18754     /* First occurrence of type or toplevel definition of nested class.  */
18755     {
18756       dw_die_ref old_die = type_die;
18757
18758       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18759                           ? record_type_tag (type) : DW_TAG_union_type,
18760                           scope_die, type);
18761       equate_type_number_to_die (type, type_die);
18762       if (old_die)
18763         add_AT_specification (type_die, old_die);
18764       else
18765         add_name_attribute (type_die, type_tag (type));
18766     }
18767   else
18768     remove_AT (type_die, DW_AT_declaration);
18769
18770   /* Generate child dies for template paramaters.  */
18771   if (debug_info_level > DINFO_LEVEL_TERSE
18772       && COMPLETE_TYPE_P (type))
18773     gen_generic_params_dies (type);
18774
18775   /* If this type has been completed, then give it a byte_size attribute and
18776      then give a list of members.  */
18777   if (complete && !ns_decl)
18778     {
18779       /* Prevent infinite recursion in cases where the type of some member of
18780          this type is expressed in terms of this type itself.  */
18781       TREE_ASM_WRITTEN (type) = 1;
18782       add_byte_size_attribute (type_die, type);
18783       if (TYPE_STUB_DECL (type) != NULL_TREE)
18784         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18785
18786       /* If the first reference to this type was as the return type of an
18787          inline function, then it may not have a parent.  Fix this now.  */
18788       if (type_die->die_parent == NULL)
18789         add_child_die (scope_die, type_die);
18790
18791       push_decl_scope (type);
18792       gen_member_die (type, type_die);
18793       pop_decl_scope ();
18794
18795       /* GNU extension: Record what type our vtable lives in.  */
18796       if (TYPE_VFIELD (type))
18797         {
18798           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18799
18800           gen_type_die (vtype, context_die);
18801           add_AT_die_ref (type_die, DW_AT_containing_type,
18802                           lookup_type_die (vtype));
18803         }
18804     }
18805   else
18806     {
18807       add_AT_flag (type_die, DW_AT_declaration, 1);
18808
18809       /* We don't need to do this for function-local types.  */
18810       if (TYPE_STUB_DECL (type)
18811           && ! decl_function_context (TYPE_STUB_DECL (type)))
18812         VEC_safe_push (tree, gc, incomplete_types, type);
18813     }
18814
18815   if (get_AT (type_die, DW_AT_name))
18816     add_pubtype (type, type_die);
18817 }
18818
18819 /* Generate a DIE for a subroutine _type_.  */
18820
18821 static void
18822 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18823 {
18824   tree return_type = TREE_TYPE (type);
18825   dw_die_ref subr_die
18826     = new_die (DW_TAG_subroutine_type,
18827                scope_die_for (type, context_die), type);
18828
18829   equate_type_number_to_die (type, subr_die);
18830   add_prototyped_attribute (subr_die, type);
18831   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18832   gen_formal_types_die (type, subr_die);
18833
18834   if (get_AT (subr_die, DW_AT_name))
18835     add_pubtype (type, subr_die);
18836 }
18837
18838 /* Generate a DIE for a type definition.  */
18839
18840 static void
18841 gen_typedef_die (tree decl, dw_die_ref context_die)
18842 {
18843   dw_die_ref type_die;
18844   tree origin;
18845
18846   if (TREE_ASM_WRITTEN (decl))
18847     return;
18848
18849   TREE_ASM_WRITTEN (decl) = 1;
18850   type_die = new_die (DW_TAG_typedef, context_die, decl);
18851   origin = decl_ultimate_origin (decl);
18852   if (origin != NULL)
18853     add_abstract_origin_attribute (type_die, origin);
18854   else
18855     {
18856       tree type;
18857
18858       add_name_and_src_coords_attributes (type_die, decl);
18859       if (DECL_ORIGINAL_TYPE (decl))
18860         {
18861           type = DECL_ORIGINAL_TYPE (decl);
18862
18863           gcc_assert (type != TREE_TYPE (decl));
18864           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18865         }
18866       else
18867         type = TREE_TYPE (decl);
18868
18869       add_type_attribute (type_die, type, TREE_READONLY (decl),
18870                           TREE_THIS_VOLATILE (decl), context_die);
18871     }
18872
18873   if (DECL_ABSTRACT (decl))
18874     equate_decl_number_to_die (decl, type_die);
18875
18876   if (get_AT (type_die, DW_AT_name))
18877     add_pubtype (decl, type_die);
18878 }
18879
18880 /* Generate a type description DIE.  */
18881
18882 static void
18883 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18884                                 enum debug_info_usage usage)
18885 {
18886   int need_pop;
18887   struct array_descr_info info;
18888
18889   if (type == NULL_TREE || type == error_mark_node)
18890     return;
18891
18892   /* If TYPE is a typedef type variant, let's generate debug info
18893      for the parent typedef which TYPE is a type of.  */
18894   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18895       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18896     {
18897       if (TREE_ASM_WRITTEN (type))
18898         return;
18899
18900       /* Prevent broken recursion; we can't hand off to the same type.  */
18901       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18902
18903       /* Use the DIE of the containing namespace as the parent DIE of
18904          the type description DIE we want to generate.  */
18905       if (DECL_CONTEXT (TYPE_NAME (type))
18906           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18907         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18908
18909       TREE_ASM_WRITTEN (type) = 1;
18910       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18911       return;
18912     }
18913
18914   /* If this is an array type with hidden descriptor, handle it first.  */
18915   if (!TREE_ASM_WRITTEN (type)
18916       && lang_hooks.types.get_array_descr_info
18917       && lang_hooks.types.get_array_descr_info (type, &info)
18918       && (dwarf_version >= 3 || !dwarf_strict))
18919     {
18920       gen_descr_array_type_die (type, &info, context_die);
18921       TREE_ASM_WRITTEN (type) = 1;
18922       return;
18923     }
18924
18925   /* We are going to output a DIE to represent the unqualified version
18926      of this type (i.e. without any const or volatile qualifiers) so
18927      get the main variant (i.e. the unqualified version) of this type
18928      now.  (Vectors are special because the debugging info is in the
18929      cloned type itself).  */
18930   if (TREE_CODE (type) != VECTOR_TYPE)
18931     type = type_main_variant (type);
18932
18933   if (TREE_ASM_WRITTEN (type))
18934     return;
18935
18936   switch (TREE_CODE (type))
18937     {
18938     case ERROR_MARK:
18939       break;
18940
18941     case POINTER_TYPE:
18942     case REFERENCE_TYPE:
18943       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18944          ensures that the gen_type_die recursion will terminate even if the
18945          type is recursive.  Recursive types are possible in Ada.  */
18946       /* ??? We could perhaps do this for all types before the switch
18947          statement.  */
18948       TREE_ASM_WRITTEN (type) = 1;
18949
18950       /* For these types, all that is required is that we output a DIE (or a
18951          set of DIEs) to represent the "basis" type.  */
18952       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18953                                 DINFO_USAGE_IND_USE);
18954       break;
18955
18956     case OFFSET_TYPE:
18957       /* This code is used for C++ pointer-to-data-member types.
18958          Output a description of the relevant class type.  */
18959       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18960                                         DINFO_USAGE_IND_USE);
18961
18962       /* Output a description of the type of the object pointed to.  */
18963       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18964                                         DINFO_USAGE_IND_USE);
18965
18966       /* Now output a DIE to represent this pointer-to-data-member type
18967          itself.  */
18968       gen_ptr_to_mbr_type_die (type, context_die);
18969       break;
18970
18971     case FUNCTION_TYPE:
18972       /* Force out return type (in case it wasn't forced out already).  */
18973       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18974                                         DINFO_USAGE_DIR_USE);
18975       gen_subroutine_type_die (type, context_die);
18976       break;
18977
18978     case METHOD_TYPE:
18979       /* Force out return type (in case it wasn't forced out already).  */
18980       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18981                                         DINFO_USAGE_DIR_USE);
18982       gen_subroutine_type_die (type, context_die);
18983       break;
18984
18985     case ARRAY_TYPE:
18986       gen_array_type_die (type, context_die);
18987       break;
18988
18989     case VECTOR_TYPE:
18990       gen_array_type_die (type, context_die);
18991       break;
18992
18993     case ENUMERAL_TYPE:
18994     case RECORD_TYPE:
18995     case UNION_TYPE:
18996     case QUAL_UNION_TYPE:
18997       /* If this is a nested type whose containing class hasn't been written
18998          out yet, writing it out will cover this one, too.  This does not apply
18999          to instantiations of member class templates; they need to be added to
19000          the containing class as they are generated.  FIXME: This hurts the
19001          idea of combining type decls from multiple TUs, since we can't predict
19002          what set of template instantiations we'll get.  */
19003       if (TYPE_CONTEXT (type)
19004           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19005           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19006         {
19007           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19008
19009           if (TREE_ASM_WRITTEN (type))
19010             return;
19011
19012           /* If that failed, attach ourselves to the stub.  */
19013           push_decl_scope (TYPE_CONTEXT (type));
19014           context_die = lookup_type_die (TYPE_CONTEXT (type));
19015           need_pop = 1;
19016         }
19017       else if (TYPE_CONTEXT (type) != NULL_TREE
19018                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19019         {
19020           /* If this type is local to a function that hasn't been written
19021              out yet, use a NULL context for now; it will be fixed up in
19022              decls_for_scope.  */
19023           context_die = lookup_decl_die (TYPE_CONTEXT (type));
19024           need_pop = 0;
19025         }
19026       else
19027         {
19028           context_die = declare_in_namespace (type, context_die);
19029           need_pop = 0;
19030         }
19031
19032       if (TREE_CODE (type) == ENUMERAL_TYPE)
19033         {
19034           /* This might have been written out by the call to
19035              declare_in_namespace.  */
19036           if (!TREE_ASM_WRITTEN (type))
19037             gen_enumeration_type_die (type, context_die);
19038         }
19039       else
19040         gen_struct_or_union_type_die (type, context_die, usage);
19041
19042       if (need_pop)
19043         pop_decl_scope ();
19044
19045       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19046          it up if it is ever completed.  gen_*_type_die will set it for us
19047          when appropriate.  */
19048       return;
19049
19050     case VOID_TYPE:
19051     case INTEGER_TYPE:
19052     case REAL_TYPE:
19053     case FIXED_POINT_TYPE:
19054     case COMPLEX_TYPE:
19055     case BOOLEAN_TYPE:
19056       /* No DIEs needed for fundamental types.  */
19057       break;
19058
19059     case LANG_TYPE:
19060       /* No Dwarf representation currently defined.  */
19061       break;
19062
19063     default:
19064       gcc_unreachable ();
19065     }
19066
19067   TREE_ASM_WRITTEN (type) = 1;
19068 }
19069
19070 static void
19071 gen_type_die (tree type, dw_die_ref context_die)
19072 {
19073   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19074 }
19075
19076 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19077    things which are local to the given block.  */
19078
19079 static void
19080 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19081 {
19082   int must_output_die = 0;
19083   bool inlined_func;
19084
19085   /* Ignore blocks that are NULL.  */
19086   if (stmt == NULL_TREE)
19087     return;
19088
19089   inlined_func = inlined_function_outer_scope_p (stmt);
19090
19091   /* If the block is one fragment of a non-contiguous block, do not
19092      process the variables, since they will have been done by the
19093      origin block.  Do process subblocks.  */
19094   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19095     {
19096       tree sub;
19097
19098       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19099         gen_block_die (sub, context_die, depth + 1);
19100
19101       return;
19102     }
19103
19104   /* Determine if we need to output any Dwarf DIEs at all to represent this
19105      block.  */
19106   if (inlined_func)
19107     /* The outer scopes for inlinings *must* always be represented.  We
19108        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19109     must_output_die = 1;
19110   else
19111     {
19112       /* Determine if this block directly contains any "significant"
19113          local declarations which we will need to output DIEs for.  */
19114       if (debug_info_level > DINFO_LEVEL_TERSE)
19115         /* We are not in terse mode so *any* local declaration counts
19116            as being a "significant" one.  */
19117         must_output_die = ((BLOCK_VARS (stmt) != NULL
19118                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19119                            && (TREE_USED (stmt)
19120                                || TREE_ASM_WRITTEN (stmt)
19121                                || BLOCK_ABSTRACT (stmt)));
19122       else if ((TREE_USED (stmt)
19123                 || TREE_ASM_WRITTEN (stmt)
19124                 || BLOCK_ABSTRACT (stmt))
19125                && !dwarf2out_ignore_block (stmt))
19126         must_output_die = 1;
19127     }
19128
19129   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19130      DIE for any block which contains no significant local declarations at
19131      all.  Rather, in such cases we just call `decls_for_scope' so that any
19132      needed Dwarf info for any sub-blocks will get properly generated. Note
19133      that in terse mode, our definition of what constitutes a "significant"
19134      local declaration gets restricted to include only inlined function
19135      instances and local (nested) function definitions.  */
19136   if (must_output_die)
19137     {
19138       if (inlined_func)
19139         {
19140           /* If STMT block is abstract, that means we have been called
19141              indirectly from dwarf2out_abstract_function.
19142              That function rightfully marks the descendent blocks (of
19143              the abstract function it is dealing with) as being abstract,
19144              precisely to prevent us from emitting any
19145              DW_TAG_inlined_subroutine DIE as a descendent
19146              of an abstract function instance. So in that case, we should
19147              not call gen_inlined_subroutine_die.
19148
19149              Later though, when cgraph asks dwarf2out to emit info
19150              for the concrete instance of the function decl into which
19151              the concrete instance of STMT got inlined, the later will lead
19152              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19153           if (! BLOCK_ABSTRACT (stmt))
19154             gen_inlined_subroutine_die (stmt, context_die, depth);
19155         }
19156       else
19157         gen_lexical_block_die (stmt, context_die, depth);
19158     }
19159   else
19160     decls_for_scope (stmt, context_die, depth);
19161 }
19162
19163 /* Process variable DECL (or variable with origin ORIGIN) within
19164    block STMT and add it to CONTEXT_DIE.  */
19165 static void
19166 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19167 {
19168   dw_die_ref die;
19169   tree decl_or_origin = decl ? decl : origin;
19170   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
19171
19172   if (ultimate_origin)
19173     origin = ultimate_origin;
19174
19175   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19176     die = lookup_decl_die (decl_or_origin);
19177   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19178            && TYPE_DECL_IS_STUB (decl_or_origin))
19179     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19180   else
19181     die = NULL;
19182
19183   if (die != NULL && die->die_parent == NULL)
19184     add_child_die (context_die, die);
19185   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19186     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19187                                          stmt, context_die);
19188   else
19189     gen_decl_die (decl, origin, context_die);
19190 }
19191
19192 /* Generate all of the decls declared within a given scope and (recursively)
19193    all of its sub-blocks.  */
19194
19195 static void
19196 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19197 {
19198   tree decl;
19199   unsigned int i;
19200   tree subblocks;
19201
19202   /* Ignore NULL blocks.  */
19203   if (stmt == NULL_TREE)
19204     return;
19205
19206   /* Output the DIEs to represent all of the data objects and typedefs
19207      declared directly within this block but not within any nested
19208      sub-blocks.  Also, nested function and tag DIEs have been
19209      generated with a parent of NULL; fix that up now.  */
19210   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
19211     process_scope_var (stmt, decl, NULL_TREE, context_die);
19212   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19213     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19214                        context_die);
19215
19216   /* If we're at -g1, we're not interested in subblocks.  */
19217   if (debug_info_level <= DINFO_LEVEL_TERSE)
19218     return;
19219
19220   /* Output the DIEs to represent all sub-blocks (and the items declared
19221      therein) of this block.  */
19222   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19223        subblocks != NULL;
19224        subblocks = BLOCK_CHAIN (subblocks))
19225     gen_block_die (subblocks, context_die, depth + 1);
19226 }
19227
19228 /* Is this a typedef we can avoid emitting?  */
19229
19230 static inline int
19231 is_redundant_typedef (const_tree decl)
19232 {
19233   if (TYPE_DECL_IS_STUB (decl))
19234     return 1;
19235
19236   if (DECL_ARTIFICIAL (decl)
19237       && DECL_CONTEXT (decl)
19238       && is_tagged_type (DECL_CONTEXT (decl))
19239       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19240       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19241     /* Also ignore the artificial member typedef for the class name.  */
19242     return 1;
19243
19244   return 0;
19245 }
19246
19247 /* Returns the DIE for a context.  */
19248
19249 static inline dw_die_ref
19250 get_context_die (tree context)
19251 {
19252   if (context)
19253     {
19254       /* Find die that represents this context.  */
19255       if (TYPE_P (context))
19256         return force_type_die (TYPE_MAIN_VARIANT (context));
19257       else
19258         return force_decl_die (context);
19259     }
19260   return comp_unit_die;
19261 }
19262
19263 /* Returns the DIE for decl.  A DIE will always be returned.  */
19264
19265 static dw_die_ref
19266 force_decl_die (tree decl)
19267 {
19268   dw_die_ref decl_die;
19269   unsigned saved_external_flag;
19270   tree save_fn = NULL_TREE;
19271   decl_die = lookup_decl_die (decl);
19272   if (!decl_die)
19273     {
19274       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19275
19276       decl_die = lookup_decl_die (decl);
19277       if (decl_die)
19278         return decl_die;
19279
19280       switch (TREE_CODE (decl))
19281         {
19282         case FUNCTION_DECL:
19283           /* Clear current_function_decl, so that gen_subprogram_die thinks
19284              that this is a declaration. At this point, we just want to force
19285              declaration die.  */
19286           save_fn = current_function_decl;
19287           current_function_decl = NULL_TREE;
19288           gen_subprogram_die (decl, context_die);
19289           current_function_decl = save_fn;
19290           break;
19291
19292         case VAR_DECL:
19293           /* Set external flag to force declaration die. Restore it after
19294            gen_decl_die() call.  */
19295           saved_external_flag = DECL_EXTERNAL (decl);
19296           DECL_EXTERNAL (decl) = 1;
19297           gen_decl_die (decl, NULL, context_die);
19298           DECL_EXTERNAL (decl) = saved_external_flag;
19299           break;
19300
19301         case NAMESPACE_DECL:
19302           if (dwarf_version >= 3 || !dwarf_strict)
19303             dwarf2out_decl (decl);
19304           else
19305             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19306             decl_die = comp_unit_die;
19307           break;
19308
19309         default:
19310           gcc_unreachable ();
19311         }
19312
19313       /* We should be able to find the DIE now.  */
19314       if (!decl_die)
19315         decl_die = lookup_decl_die (decl);
19316       gcc_assert (decl_die);
19317     }
19318
19319   return decl_die;
19320 }
19321
19322 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19323    always returned.  */
19324
19325 static dw_die_ref
19326 force_type_die (tree type)
19327 {
19328   dw_die_ref type_die;
19329
19330   type_die = lookup_type_die (type);
19331   if (!type_die)
19332     {
19333       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19334
19335       type_die = modified_type_die (type, TYPE_READONLY (type),
19336                                     TYPE_VOLATILE (type), context_die);
19337       gcc_assert (type_die);
19338     }
19339   return type_die;
19340 }
19341
19342 /* Force out any required namespaces to be able to output DECL,
19343    and return the new context_die for it, if it's changed.  */
19344
19345 static dw_die_ref
19346 setup_namespace_context (tree thing, dw_die_ref context_die)
19347 {
19348   tree context = (DECL_P (thing)
19349                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19350   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19351     /* Force out the namespace.  */
19352     context_die = force_decl_die (context);
19353
19354   return context_die;
19355 }
19356
19357 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19358    type) within its namespace, if appropriate.
19359
19360    For compatibility with older debuggers, namespace DIEs only contain
19361    declarations; all definitions are emitted at CU scope.  */
19362
19363 static dw_die_ref
19364 declare_in_namespace (tree thing, dw_die_ref context_die)
19365 {
19366   dw_die_ref ns_context;
19367
19368   if (debug_info_level <= DINFO_LEVEL_TERSE)
19369     return context_die;
19370
19371   /* If this decl is from an inlined function, then don't try to emit it in its
19372      namespace, as we will get confused.  It would have already been emitted
19373      when the abstract instance of the inline function was emitted anyways.  */
19374   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19375     return context_die;
19376
19377   ns_context = setup_namespace_context (thing, context_die);
19378
19379   if (ns_context != context_die)
19380     {
19381       if (is_fortran ())
19382         return ns_context;
19383       if (DECL_P (thing))
19384         gen_decl_die (thing, NULL, ns_context);
19385       else
19386         gen_type_die (thing, ns_context);
19387     }
19388   return context_die;
19389 }
19390
19391 /* Generate a DIE for a namespace or namespace alias.  */
19392
19393 static void
19394 gen_namespace_die (tree decl, dw_die_ref context_die)
19395 {
19396   dw_die_ref namespace_die;
19397
19398   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19399      they are an alias of.  */
19400   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19401     {
19402       /* Output a real namespace or module.  */
19403       context_die = setup_namespace_context (decl, comp_unit_die);
19404       namespace_die = new_die (is_fortran ()
19405                                ? DW_TAG_module : DW_TAG_namespace,
19406                                context_die, decl);
19407       /* For Fortran modules defined in different CU don't add src coords.  */
19408       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19409         {
19410           const char *name = dwarf2_name (decl, 0);
19411           if (name)
19412             add_name_attribute (namespace_die, name);
19413         }
19414       else
19415         add_name_and_src_coords_attributes (namespace_die, decl);
19416       if (DECL_EXTERNAL (decl))
19417         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19418       equate_decl_number_to_die (decl, namespace_die);
19419     }
19420   else
19421     {
19422       /* Output a namespace alias.  */
19423
19424       /* Force out the namespace we are an alias of, if necessary.  */
19425       dw_die_ref origin_die
19426         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19427
19428       if (DECL_CONTEXT (decl) == NULL_TREE
19429           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19430         context_die = setup_namespace_context (decl, comp_unit_die);
19431       /* Now create the namespace alias DIE.  */
19432       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19433       add_name_and_src_coords_attributes (namespace_die, decl);
19434       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19435       equate_decl_number_to_die (decl, namespace_die);
19436     }
19437 }
19438
19439 /* Generate Dwarf debug information for a decl described by DECL.  */
19440
19441 static void
19442 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19443 {
19444   tree decl_or_origin = decl ? decl : origin;
19445   tree class_origin = NULL;
19446
19447   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19448     return;
19449
19450   switch (TREE_CODE (decl_or_origin))
19451     {
19452     case ERROR_MARK:
19453       break;
19454
19455     case CONST_DECL:
19456       if (!is_fortran ())
19457         {
19458           /* The individual enumerators of an enum type get output when we output
19459              the Dwarf representation of the relevant enum type itself.  */
19460           break;
19461         }
19462
19463       /* Emit its type.  */
19464       gen_type_die (TREE_TYPE (decl), context_die);
19465
19466       /* And its containing namespace.  */
19467       context_die = declare_in_namespace (decl, context_die);
19468
19469       gen_const_die (decl, context_die);
19470       break;
19471
19472     case FUNCTION_DECL:
19473       /* Don't output any DIEs to represent mere function declarations,
19474          unless they are class members or explicit block externs.  */
19475       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19476           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
19477           && (current_function_decl == NULL_TREE
19478               || DECL_ARTIFICIAL (decl_or_origin)))
19479         break;
19480
19481 #if 0
19482       /* FIXME */
19483       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19484          on local redeclarations of global functions.  That seems broken.  */
19485       if (current_function_decl != decl)
19486         /* This is only a declaration.  */;
19487 #endif
19488
19489       /* If we're emitting a clone, emit info for the abstract instance.  */
19490       if (origin || DECL_ORIGIN (decl) != decl)
19491         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
19492
19493       /* If we're emitting an out-of-line copy of an inline function,
19494          emit info for the abstract instance and set up to refer to it.  */
19495       else if (cgraph_function_possibly_inlined_p (decl)
19496                && ! DECL_ABSTRACT (decl)
19497                && ! class_or_namespace_scope_p (context_die)
19498                /* dwarf2out_abstract_function won't emit a die if this is just
19499                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19500                   that case, because that works only if we have a die.  */
19501                && DECL_INITIAL (decl) != NULL_TREE)
19502         {
19503           dwarf2out_abstract_function (decl);
19504           set_decl_origin_self (decl);
19505         }
19506
19507       /* Otherwise we're emitting the primary DIE for this decl.  */
19508       else if (debug_info_level > DINFO_LEVEL_TERSE)
19509         {
19510           /* Before we describe the FUNCTION_DECL itself, make sure that we
19511              have described its return type.  */
19512           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19513
19514           /* And its virtual context.  */
19515           if (DECL_VINDEX (decl) != NULL_TREE)
19516             gen_type_die (DECL_CONTEXT (decl), context_die);
19517
19518           /* And its containing type.  */
19519           if (!origin)
19520             origin = decl_class_context (decl);
19521           if (origin != NULL_TREE)
19522             gen_type_die_for_member (origin, decl, context_die);
19523
19524           /* And its containing namespace.  */
19525           context_die = declare_in_namespace (decl, context_die);
19526         }
19527
19528       /* Now output a DIE to represent the function itself.  */
19529       if (decl)
19530         gen_subprogram_die (decl, context_die);
19531       break;
19532
19533     case TYPE_DECL:
19534       /* If we are in terse mode, don't generate any DIEs to represent any
19535          actual typedefs.  */
19536       if (debug_info_level <= DINFO_LEVEL_TERSE)
19537         break;
19538
19539       /* In the special case of a TYPE_DECL node representing the declaration
19540          of some type tag, if the given TYPE_DECL is marked as having been
19541          instantiated from some other (original) TYPE_DECL node (e.g. one which
19542          was generated within the original definition of an inline function) we
19543          used to generate a special (abbreviated) DW_TAG_structure_type,
19544          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19545          should be actually referencing those DIEs, as variable DIEs with that
19546          type would be emitted already in the abstract origin, so it was always
19547          removed during unused type prunning.  Don't add anything in this
19548          case.  */
19549       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19550         break;
19551
19552       if (is_redundant_typedef (decl))
19553         gen_type_die (TREE_TYPE (decl), context_die);
19554       else
19555         /* Output a DIE to represent the typedef itself.  */
19556         gen_typedef_die (decl, context_die);
19557       break;
19558
19559     case LABEL_DECL:
19560       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19561         gen_label_die (decl, context_die);
19562       break;
19563
19564     case VAR_DECL:
19565     case RESULT_DECL:
19566       /* If we are in terse mode, don't generate any DIEs to represent any
19567          variable declarations or definitions.  */
19568       if (debug_info_level <= DINFO_LEVEL_TERSE)
19569         break;
19570
19571       /* Output any DIEs that are needed to specify the type of this data
19572          object.  */
19573       if (decl_by_reference_p (decl_or_origin))
19574         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19575       else
19576         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19577
19578       /* And its containing type.  */
19579       class_origin = decl_class_context (decl_or_origin);
19580       if (class_origin != NULL_TREE)
19581         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19582
19583       /* And its containing namespace.  */
19584       context_die = declare_in_namespace (decl_or_origin, context_die);
19585
19586       /* Now output the DIE to represent the data object itself.  This gets
19587          complicated because of the possibility that the VAR_DECL really
19588          represents an inlined instance of a formal parameter for an inline
19589          function.  */
19590       if (!origin)
19591         origin = decl_ultimate_origin (decl);
19592       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
19593         gen_formal_parameter_die (decl, origin,
19594                                   true /* Emit name attribute.  */,
19595                                   context_die);
19596       else
19597         gen_variable_die (decl, origin, context_die);
19598       break;
19599
19600     case FIELD_DECL:
19601       /* Ignore the nameless fields that are used to skip bits but handle C++
19602          anonymous unions and structs.  */
19603       if (DECL_NAME (decl) != NULL_TREE
19604           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19605           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19606         {
19607           gen_type_die (member_declared_type (decl), context_die);
19608           gen_field_die (decl, context_die);
19609         }
19610       break;
19611
19612     case PARM_DECL:
19613       if (DECL_BY_REFERENCE (decl_or_origin))
19614         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19615       else
19616         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19617       gen_formal_parameter_die (decl, origin,
19618                                 true /* Emit name attribute.  */,
19619                                 context_die);
19620       break;
19621
19622     case NAMESPACE_DECL:
19623     case IMPORTED_DECL:
19624       if (dwarf_version >= 3 || !dwarf_strict)
19625         gen_namespace_die (decl, context_die);
19626       break;
19627
19628     default:
19629       /* Probably some frontend-internal decl.  Assume we don't care.  */
19630       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19631       break;
19632     }
19633 }
19634 \f
19635 /* Output debug information for global decl DECL.  Called from toplev.c after
19636    compilation proper has finished.  */
19637
19638 static void
19639 dwarf2out_global_decl (tree decl)
19640 {
19641   /* Output DWARF2 information for file-scope tentative data object
19642      declarations, file-scope (extern) function declarations (which
19643      had no corresponding body) and file-scope tagged type declarations
19644      and definitions which have not yet been forced out.  */
19645   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19646     dwarf2out_decl (decl);
19647 }
19648
19649 /* Output debug information for type decl DECL.  Called from toplev.c
19650    and from language front ends (to record built-in types).  */
19651 static void
19652 dwarf2out_type_decl (tree decl, int local)
19653 {
19654   if (!local)
19655     dwarf2out_decl (decl);
19656 }
19657
19658 /* Output debug information for imported module or decl DECL.
19659    NAME is non-NULL name in the lexical block if the decl has been renamed.
19660    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19661    that DECL belongs to.
19662    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19663 static void
19664 dwarf2out_imported_module_or_decl_1 (tree decl,
19665                                      tree name,
19666                                      tree lexical_block,
19667                                      dw_die_ref lexical_block_die)
19668 {
19669   expanded_location xloc;
19670   dw_die_ref imported_die = NULL;
19671   dw_die_ref at_import_die;
19672
19673   if (TREE_CODE (decl) == IMPORTED_DECL)
19674     {
19675       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19676       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19677       gcc_assert (decl);
19678     }
19679   else
19680     xloc = expand_location (input_location);
19681
19682   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19683     {
19684       if (is_base_type (TREE_TYPE (decl)))
19685         at_import_die = base_type_die (TREE_TYPE (decl));
19686       else
19687         at_import_die = force_type_die (TREE_TYPE (decl));
19688       /* For namespace N { typedef void T; } using N::T; base_type_die
19689          returns NULL, but DW_TAG_imported_declaration requires
19690          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19691       if (!at_import_die)
19692         {
19693           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19694           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19695           at_import_die = lookup_type_die (TREE_TYPE (decl));
19696           gcc_assert (at_import_die);
19697         }
19698     }
19699   else
19700     {
19701       at_import_die = lookup_decl_die (decl);
19702       if (!at_import_die)
19703         {
19704           /* If we're trying to avoid duplicate debug info, we may not have
19705              emitted the member decl for this field.  Emit it now.  */
19706           if (TREE_CODE (decl) == FIELD_DECL)
19707             {
19708               tree type = DECL_CONTEXT (decl);
19709
19710               if (TYPE_CONTEXT (type)
19711                   && TYPE_P (TYPE_CONTEXT (type))
19712                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19713                                                 DINFO_USAGE_DIR_USE))
19714                 return;
19715               gen_type_die_for_member (type, decl,
19716                                        get_context_die (TYPE_CONTEXT (type)));
19717             }
19718           at_import_die = force_decl_die (decl);
19719         }
19720     }
19721
19722   if (TREE_CODE (decl) == NAMESPACE_DECL)
19723     {
19724       if (dwarf_version >= 3 || !dwarf_strict)
19725         imported_die = new_die (DW_TAG_imported_module,
19726                                 lexical_block_die,
19727                                 lexical_block);
19728       else
19729         return;
19730     }
19731   else
19732     imported_die = new_die (DW_TAG_imported_declaration,
19733                             lexical_block_die,
19734                             lexical_block);
19735
19736   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19737   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19738   if (name)
19739     add_AT_string (imported_die, DW_AT_name,
19740                    IDENTIFIER_POINTER (name));
19741   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19742 }
19743
19744 /* Output debug information for imported module or decl DECL.
19745    NAME is non-NULL name in context if the decl has been renamed.
19746    CHILD is true if decl is one of the renamed decls as part of
19747    importing whole module.  */
19748
19749 static void
19750 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19751                                    bool child)
19752 {
19753   /* dw_die_ref at_import_die;  */
19754   dw_die_ref scope_die;
19755
19756   if (debug_info_level <= DINFO_LEVEL_TERSE)
19757     return;
19758
19759   gcc_assert (decl);
19760
19761   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19762      We need decl DIE for reference and scope die. First, get DIE for the decl
19763      itself.  */
19764
19765   /* Get the scope die for decl context. Use comp_unit_die for global module
19766      or decl. If die is not found for non globals, force new die.  */
19767   if (context
19768       && TYPE_P (context)
19769       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19770     return;
19771
19772   if (!(dwarf_version >= 3 || !dwarf_strict))
19773     return;
19774
19775   scope_die = get_context_die (context);
19776
19777   if (child)
19778     {
19779       gcc_assert (scope_die->die_child);
19780       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19781       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19782       scope_die = scope_die->die_child;
19783     }
19784
19785   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19786   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19787
19788 }
19789
19790 /* Write the debugging output for DECL.  */
19791
19792 void
19793 dwarf2out_decl (tree decl)
19794 {
19795   dw_die_ref context_die = comp_unit_die;
19796
19797   switch (TREE_CODE (decl))
19798     {
19799     case ERROR_MARK:
19800       return;
19801
19802     case FUNCTION_DECL:
19803       /* What we would really like to do here is to filter out all mere
19804          file-scope declarations of file-scope functions which are never
19805          referenced later within this translation unit (and keep all of ones
19806          that *are* referenced later on) but we aren't clairvoyant, so we have
19807          no idea which functions will be referenced in the future (i.e. later
19808          on within the current translation unit). So here we just ignore all
19809          file-scope function declarations which are not also definitions.  If
19810          and when the debugger needs to know something about these functions,
19811          it will have to hunt around and find the DWARF information associated
19812          with the definition of the function.
19813
19814          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19815          nodes represent definitions and which ones represent mere
19816          declarations.  We have to check DECL_INITIAL instead. That's because
19817          the C front-end supports some weird semantics for "extern inline"
19818          function definitions.  These can get inlined within the current
19819          translation unit (and thus, we need to generate Dwarf info for their
19820          abstract instances so that the Dwarf info for the concrete inlined
19821          instances can have something to refer to) but the compiler never
19822          generates any out-of-lines instances of such things (despite the fact
19823          that they *are* definitions).
19824
19825          The important point is that the C front-end marks these "extern
19826          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19827          them anyway. Note that the C++ front-end also plays some similar games
19828          for inline function definitions appearing within include files which
19829          also contain `#pragma interface' pragmas.  */
19830       if (DECL_INITIAL (decl) == NULL_TREE)
19831         return;
19832
19833       /* If we're a nested function, initially use a parent of NULL; if we're
19834          a plain function, this will be fixed up in decls_for_scope.  If
19835          we're a method, it will be ignored, since we already have a DIE.  */
19836       if (decl_function_context (decl)
19837           /* But if we're in terse mode, we don't care about scope.  */
19838           && debug_info_level > DINFO_LEVEL_TERSE)
19839         context_die = NULL;
19840       break;
19841
19842     case VAR_DECL:
19843       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19844          declaration and if the declaration was never even referenced from
19845          within this entire compilation unit.  We suppress these DIEs in
19846          order to save space in the .debug section (by eliminating entries
19847          which are probably useless).  Note that we must not suppress
19848          block-local extern declarations (whether used or not) because that
19849          would screw-up the debugger's name lookup mechanism and cause it to
19850          miss things which really ought to be in scope at a given point.  */
19851       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19852         return;
19853
19854       /* For local statics lookup proper context die.  */
19855       if (TREE_STATIC (decl) && decl_function_context (decl))
19856         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19857
19858       /* If we are in terse mode, don't generate any DIEs to represent any
19859          variable declarations or definitions.  */
19860       if (debug_info_level <= DINFO_LEVEL_TERSE)
19861         return;
19862       break;
19863
19864     case CONST_DECL:
19865       if (debug_info_level <= DINFO_LEVEL_TERSE)
19866         return;
19867       if (!is_fortran ())
19868         return;
19869       if (TREE_STATIC (decl) && decl_function_context (decl))
19870         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19871       break;
19872
19873     case NAMESPACE_DECL:
19874     case IMPORTED_DECL:
19875       if (debug_info_level <= DINFO_LEVEL_TERSE)
19876         return;
19877       if (lookup_decl_die (decl) != NULL)
19878         return;
19879       break;
19880
19881     case TYPE_DECL:
19882       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19883       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19884         return;
19885
19886       /* Don't bother trying to generate any DIEs to represent any of the
19887          normal built-in types for the language we are compiling.  */
19888       if (DECL_IS_BUILTIN (decl))
19889         {
19890           /* OK, we need to generate one for `bool' so GDB knows what type
19891              comparisons have.  */
19892           if (is_cxx ()
19893               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
19894               && ! DECL_IGNORED_P (decl))
19895             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
19896
19897           return;
19898         }
19899
19900       /* If we are in terse mode, don't generate any DIEs for types.  */
19901       if (debug_info_level <= DINFO_LEVEL_TERSE)
19902         return;
19903
19904       /* If we're a function-scope tag, initially use a parent of NULL;
19905          this will be fixed up in decls_for_scope.  */
19906       if (decl_function_context (decl))
19907         context_die = NULL;
19908
19909       break;
19910
19911     default:
19912       return;
19913     }
19914
19915   gen_decl_die (decl, NULL, context_die);
19916 }
19917
19918 /* Output a marker (i.e. a label) for the beginning of the generated code for
19919    a lexical block.  */
19920
19921 static void
19922 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19923                        unsigned int blocknum)
19924 {
19925   switch_to_section (current_function_section ());
19926   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19927 }
19928
19929 /* Output a marker (i.e. a label) for the end of the generated code for a
19930    lexical block.  */
19931
19932 static void
19933 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19934 {
19935   switch_to_section (current_function_section ());
19936   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19937 }
19938
19939 /* Returns nonzero if it is appropriate not to emit any debugging
19940    information for BLOCK, because it doesn't contain any instructions.
19941
19942    Don't allow this for blocks with nested functions or local classes
19943    as we would end up with orphans, and in the presence of scheduling
19944    we may end up calling them anyway.  */
19945
19946 static bool
19947 dwarf2out_ignore_block (const_tree block)
19948 {
19949   tree decl;
19950   unsigned int i;
19951
19952   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
19953     if (TREE_CODE (decl) == FUNCTION_DECL
19954         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19955       return 0;
19956   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19957     {
19958       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19959       if (TREE_CODE (decl) == FUNCTION_DECL
19960           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19961       return 0;
19962     }
19963
19964   return 1;
19965 }
19966
19967 /* Hash table routines for file_hash.  */
19968
19969 static int
19970 file_table_eq (const void *p1_p, const void *p2_p)
19971 {
19972   const struct dwarf_file_data *const p1 =
19973     (const struct dwarf_file_data *) p1_p;
19974   const char *const p2 = (const char *) p2_p;
19975   return strcmp (p1->filename, p2) == 0;
19976 }
19977
19978 static hashval_t
19979 file_table_hash (const void *p_p)
19980 {
19981   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19982   return htab_hash_string (p->filename);
19983 }
19984
19985 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19986    dwarf2out.c) and return its "index".  The index of each (known) filename is
19987    just a unique number which is associated with only that one filename.  We
19988    need such numbers for the sake of generating labels (in the .debug_sfnames
19989    section) and references to those files numbers (in the .debug_srcinfo
19990    and.debug_macinfo sections).  If the filename given as an argument is not
19991    found in our current list, add it to the list and assign it the next
19992    available unique index number.  In order to speed up searches, we remember
19993    the index of the filename was looked up last.  This handles the majority of
19994    all searches.  */
19995
19996 static struct dwarf_file_data *
19997 lookup_filename (const char *file_name)
19998 {
19999   void ** slot;
20000   struct dwarf_file_data * created;
20001
20002   /* Check to see if the file name that was searched on the previous
20003      call matches this file name.  If so, return the index.  */
20004   if (file_table_last_lookup
20005       && (file_name == file_table_last_lookup->filename
20006           || strcmp (file_table_last_lookup->filename, file_name) == 0))
20007     return file_table_last_lookup;
20008
20009   /* Didn't match the previous lookup, search the table.  */
20010   slot = htab_find_slot_with_hash (file_table, file_name,
20011                                    htab_hash_string (file_name), INSERT);
20012   if (*slot)
20013     return (struct dwarf_file_data *) *slot;
20014
20015   created = GGC_NEW (struct dwarf_file_data);
20016   created->filename = file_name;
20017   created->emitted_number = 0;
20018   *slot = created;
20019   return created;
20020 }
20021
20022 /* If the assembler will construct the file table, then translate the compiler
20023    internal file table number into the assembler file table number, and emit
20024    a .file directive if we haven't already emitted one yet.  The file table
20025    numbers are different because we prune debug info for unused variables and
20026    types, which may include filenames.  */
20027
20028 static int
20029 maybe_emit_file (struct dwarf_file_data * fd)
20030 {
20031   if (! fd->emitted_number)
20032     {
20033       if (last_emitted_file)
20034         fd->emitted_number = last_emitted_file->emitted_number + 1;
20035       else
20036         fd->emitted_number = 1;
20037       last_emitted_file = fd;
20038
20039       if (DWARF2_ASM_LINE_DEBUG_INFO)
20040         {
20041           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20042           output_quoted_string (asm_out_file,
20043                                 remap_debug_filename (fd->filename));
20044           fputc ('\n', asm_out_file);
20045         }
20046     }
20047
20048   return fd->emitted_number;
20049 }
20050
20051 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20052    That generation should happen after function debug info has been
20053    generated. The value of the attribute is the constant value of ARG.  */
20054
20055 static void
20056 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20057 {
20058   die_arg_entry entry;
20059
20060   if (!die || !arg)
20061     return;
20062
20063   if (!tmpl_value_parm_die_table)
20064     tmpl_value_parm_die_table
20065       = VEC_alloc (die_arg_entry, gc, 32);
20066
20067   entry.die = die;
20068   entry.arg = arg;
20069   VEC_safe_push (die_arg_entry, gc,
20070                  tmpl_value_parm_die_table,
20071                  &entry);
20072 }
20073
20074 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20075    by append_entry_to_tmpl_value_parm_die_table. This function must
20076    be called after function DIEs have been generated.  */
20077
20078 static void
20079 gen_remaining_tmpl_value_param_die_attribute (void)
20080 {
20081   if (tmpl_value_parm_die_table)
20082     {
20083       unsigned i;
20084       die_arg_entry *e;
20085
20086       for (i = 0;
20087            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
20088            i++)
20089         tree_add_const_value_attribute (e->die, e->arg);
20090     }
20091 }
20092
20093
20094 /* Replace DW_AT_name for the decl with name.  */
20095
20096 static void
20097 dwarf2out_set_name (tree decl, tree name)
20098 {
20099   dw_die_ref die;
20100   dw_attr_ref attr;
20101   const char *dname;
20102
20103   die = TYPE_SYMTAB_DIE (decl);
20104   if (!die)
20105     return;
20106
20107   dname = dwarf2_name (name, 0);
20108   if (!dname)
20109     return;
20110
20111   attr = get_AT (die, DW_AT_name);
20112   if (attr)
20113     {
20114       struct indirect_string_node *node;
20115
20116       node = find_AT_string (dname);
20117       /* replace the string.  */
20118       attr->dw_attr_val.v.val_str = node;
20119     }
20120
20121   else
20122     add_name_attribute (die, dname);
20123 }
20124
20125 /* Called by the final INSN scan whenever we see a direct function call.
20126    Make an entry into the direct call table, recording the point of call
20127    and a reference to the target function's debug entry.  */
20128
20129 static void
20130 dwarf2out_direct_call (tree targ)
20131 {
20132   dcall_entry e;
20133   tree origin = decl_ultimate_origin (targ);
20134
20135   /* If this is a clone, use the abstract origin as the target.  */
20136   if (origin)
20137     targ = origin;
20138
20139   e.poc_label_num = poc_label_num++;
20140   e.poc_decl = current_function_decl;
20141   e.targ_die = force_decl_die (targ);
20142   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
20143
20144   /* Drop a label at the return point to mark the point of call.  */
20145   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
20146 }
20147
20148 /* Returns a hash value for X (which really is a struct vcall_insn).  */
20149
20150 static hashval_t
20151 vcall_insn_table_hash (const void *x)
20152 {
20153   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
20154 }
20155
20156 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
20157    insnd_uid of *Y.  */
20158
20159 static int
20160 vcall_insn_table_eq (const void *x, const void *y)
20161 {
20162   return (((const struct vcall_insn *) x)->insn_uid
20163           == ((const struct vcall_insn *) y)->insn_uid);
20164 }
20165
20166 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
20167
20168 static void
20169 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
20170 {
20171   struct vcall_insn *item = GGC_NEW (struct vcall_insn);
20172   struct vcall_insn **slot;
20173
20174   gcc_assert (item);
20175   item->insn_uid = insn_uid;
20176   item->vtable_slot = vtable_slot;
20177   slot = (struct vcall_insn **)
20178       htab_find_slot_with_hash (vcall_insn_table, &item,
20179                                 (hashval_t) insn_uid, INSERT);
20180   *slot = item;
20181 }
20182
20183 /* Return the VTABLE_SLOT associated with INSN_UID.  */
20184
20185 static unsigned int
20186 lookup_vcall_insn (unsigned int insn_uid)
20187 {
20188   struct vcall_insn item;
20189   struct vcall_insn *p;
20190
20191   item.insn_uid = insn_uid;
20192   item.vtable_slot = 0;
20193   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
20194                                                  (void *) &item,
20195                                                  (hashval_t) insn_uid);
20196   if (p == NULL)
20197     return (unsigned int) -1;
20198   return p->vtable_slot;
20199 }
20200
20201
20202 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
20203    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
20204    is the vtable slot index that we will need to put in the virtual call
20205    table later.  */
20206
20207 static void
20208 dwarf2out_virtual_call_token (tree addr, int insn_uid)
20209 {
20210   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
20211     {
20212       tree token = OBJ_TYPE_REF_TOKEN (addr);
20213       if (TREE_CODE (token) == INTEGER_CST)
20214         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
20215     }
20216 }
20217
20218 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
20219    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
20220    with NEW_INSN.  */
20221
20222 static void
20223 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
20224 {
20225   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
20226
20227   if (vtable_slot != (unsigned int) -1)
20228     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
20229 }
20230
20231 /* Called by the final INSN scan whenever we see a virtual function call.
20232    Make an entry into the virtual call table, recording the point of call
20233    and the slot index of the vtable entry used to call the virtual member
20234    function.  The slot index was associated with the INSN_UID during the
20235    lowering to RTL.  */
20236
20237 static void
20238 dwarf2out_virtual_call (int insn_uid)
20239 {
20240   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
20241   vcall_entry e;
20242
20243   if (vtable_slot == (unsigned int) -1)
20244     return;
20245
20246   e.poc_label_num = poc_label_num++;
20247   e.vtable_slot = vtable_slot;
20248   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
20249
20250   /* Drop a label at the return point to mark the point of call.  */
20251   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
20252 }
20253
20254 /* Called by the final INSN scan whenever we see a var location.  We
20255    use it to drop labels in the right places, and throw the location in
20256    our lookup table.  */
20257
20258 static void
20259 dwarf2out_var_location (rtx loc_note)
20260 {
20261   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20262   struct var_loc_node *newloc;
20263   rtx next_real;
20264   static const char *last_label;
20265   static const char *last_postcall_label;
20266   static bool last_in_cold_section_p;
20267   tree decl;
20268
20269   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20270     return;
20271
20272   next_real = next_real_insn (loc_note);
20273   /* If there are no instructions which would be affected by this note,
20274      don't do anything.  */
20275   if (next_real == NULL_RTX)
20276     return;
20277
20278   decl = NOTE_VAR_LOCATION_DECL (loc_note);
20279   newloc = add_var_loc_to_decl (decl, loc_note);
20280   if (newloc == NULL)
20281     return;
20282
20283   /* If there were no real insns between note we processed last time
20284      and this note, use the label we emitted last time.  */
20285   if (last_var_location_insn == NULL_RTX
20286       || last_var_location_insn != next_real
20287       || last_in_cold_section_p != in_cold_section_p)
20288     {
20289       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20290       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20291       loclabel_num++;
20292       last_label = ggc_strdup (loclabel);
20293       last_postcall_label = NULL;
20294     }
20295   newloc->var_loc_note = loc_note;
20296   newloc->next = NULL;
20297
20298   if (!NOTE_DURING_CALL_P (loc_note))
20299     newloc->label = last_label;
20300   else
20301     {
20302       if (!last_postcall_label)
20303         {
20304           sprintf (loclabel, "%s-1", last_label);
20305           last_postcall_label = ggc_strdup (loclabel);
20306         }
20307       newloc->label = last_postcall_label;
20308     }
20309
20310   if (cfun && in_cold_section_p)
20311     newloc->section_label = crtl->subsections.cold_section_label;
20312   else
20313     newloc->section_label = text_section_label;
20314
20315   last_var_location_insn = next_real;
20316   last_in_cold_section_p = in_cold_section_p;
20317 }
20318
20319 /* We need to reset the locations at the beginning of each
20320    function. We can't do this in the end_function hook, because the
20321    declarations that use the locations won't have been output when
20322    that hook is called.  Also compute have_multiple_function_sections here.  */
20323
20324 static void
20325 dwarf2out_begin_function (tree fun)
20326 {
20327   htab_empty (decl_loc_table);
20328
20329   if (function_section (fun) != text_section)
20330     have_multiple_function_sections = true;
20331
20332   dwarf2out_note_section_used ();
20333 }
20334
20335 /* Output a label to mark the beginning of a source code line entry
20336    and record information relating to this source line, in
20337    'line_info_table' for later output of the .debug_line section.  */
20338
20339 static void
20340 dwarf2out_source_line (unsigned int line, const char *filename,
20341                        int discriminator, bool is_stmt)
20342 {
20343   static bool last_is_stmt = true;
20344
20345   if (debug_info_level >= DINFO_LEVEL_NORMAL
20346       && line != 0)
20347     {
20348       int file_num = maybe_emit_file (lookup_filename (filename));
20349
20350       switch_to_section (current_function_section ());
20351
20352       /* If requested, emit something human-readable.  */
20353       if (flag_debug_asm)
20354         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
20355                  filename, line);
20356
20357       if (DWARF2_ASM_LINE_DEBUG_INFO)
20358         {
20359           /* Emit the .loc directive understood by GNU as.  */
20360           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20361           if (is_stmt != last_is_stmt)
20362             {
20363               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20364               last_is_stmt = is_stmt;
20365             }
20366           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20367             fprintf (asm_out_file, " discriminator %d", discriminator);
20368           fputc ('\n', asm_out_file);
20369
20370           /* Indicate that line number info exists.  */
20371           line_info_table_in_use++;
20372         }
20373       else if (function_section (current_function_decl) != text_section)
20374         {
20375           dw_separate_line_info_ref line_info;
20376           targetm.asm_out.internal_label (asm_out_file,
20377                                           SEPARATE_LINE_CODE_LABEL,
20378                                           separate_line_info_table_in_use);
20379
20380           /* Expand the line info table if necessary.  */
20381           if (separate_line_info_table_in_use
20382               == separate_line_info_table_allocated)
20383             {
20384               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20385               separate_line_info_table
20386                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
20387                                  separate_line_info_table,
20388                                  separate_line_info_table_allocated);
20389               memset (separate_line_info_table
20390                        + separate_line_info_table_in_use,
20391                       0,
20392                       (LINE_INFO_TABLE_INCREMENT
20393                        * sizeof (dw_separate_line_info_entry)));
20394             }
20395
20396           /* Add the new entry at the end of the line_info_table.  */
20397           line_info
20398             = &separate_line_info_table[separate_line_info_table_in_use++];
20399           line_info->dw_file_num = file_num;
20400           line_info->dw_line_num = line;
20401           line_info->function = current_function_funcdef_no;
20402         }
20403       else
20404         {
20405           dw_line_info_ref line_info;
20406
20407           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
20408                                      line_info_table_in_use);
20409
20410           /* Expand the line info table if necessary.  */
20411           if (line_info_table_in_use == line_info_table_allocated)
20412             {
20413               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20414               line_info_table
20415                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
20416                                  line_info_table_allocated);
20417               memset (line_info_table + line_info_table_in_use, 0,
20418                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
20419             }
20420
20421           /* Add the new entry at the end of the line_info_table.  */
20422           line_info = &line_info_table[line_info_table_in_use++];
20423           line_info->dw_file_num = file_num;
20424           line_info->dw_line_num = line;
20425         }
20426     }
20427 }
20428
20429 /* Record the beginning of a new source file.  */
20430
20431 static void
20432 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20433 {
20434   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20435     {
20436       /* Record the beginning of the file for break_out_includes.  */
20437       dw_die_ref bincl_die;
20438
20439       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
20440       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20441     }
20442
20443   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20444     {
20445       int file_num = maybe_emit_file (lookup_filename (filename));
20446
20447       switch_to_section (debug_macinfo_section);
20448       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20449       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
20450                                    lineno);
20451
20452       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
20453     }
20454 }
20455
20456 /* Record the end of a source file.  */
20457
20458 static void
20459 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20460 {
20461   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20462     /* Record the end of the file for break_out_includes.  */
20463     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
20464
20465   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20466     {
20467       switch_to_section (debug_macinfo_section);
20468       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20469     }
20470 }
20471
20472 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20473    the tail part of the directive line, i.e. the part which is past the
20474    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20475
20476 static void
20477 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20478                   const char *buffer ATTRIBUTE_UNUSED)
20479 {
20480   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20481     {
20482       switch_to_section (debug_macinfo_section);
20483       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20484       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20485       dw2_asm_output_nstring (buffer, -1, "The macro");
20486     }
20487 }
20488
20489 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20490    the tail part of the directive line, i.e. the part which is past the
20491    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20492
20493 static void
20494 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20495                  const char *buffer ATTRIBUTE_UNUSED)
20496 {
20497   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20498     {
20499       switch_to_section (debug_macinfo_section);
20500       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20501       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20502       dw2_asm_output_nstring (buffer, -1, "The macro");
20503     }
20504 }
20505
20506 /* Set up for Dwarf output at the start of compilation.  */
20507
20508 static void
20509 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20510 {
20511   /* Allocate the file_table.  */
20512   file_table = htab_create_ggc (50, file_table_hash,
20513                                 file_table_eq, NULL);
20514
20515   /* Allocate the decl_die_table.  */
20516   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20517                                     decl_die_table_eq, NULL);
20518
20519   /* Allocate the decl_loc_table.  */
20520   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20521                                     decl_loc_table_eq, NULL);
20522
20523   /* Allocate the initial hunk of the decl_scope_table.  */
20524   decl_scope_table = VEC_alloc (tree, gc, 256);
20525
20526   /* Allocate the initial hunk of the abbrev_die_table.  */
20527   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
20528   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20529   /* Zero-th entry is allocated, but unused.  */
20530   abbrev_die_table_in_use = 1;
20531
20532   /* Allocate the initial hunk of the line_info_table.  */
20533   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
20534   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
20535
20536   /* Zero-th entry is allocated, but unused.  */
20537   line_info_table_in_use = 1;
20538
20539   /* Allocate the pubtypes and pubnames vectors.  */
20540   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20541   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20542
20543   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
20544   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
20545                                       vcall_insn_table_eq, NULL);
20546
20547   /* Generate the initial DIE for the .debug section.  Note that the (string)
20548      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
20549      will (typically) be a relative pathname and that this pathname should be
20550      taken as being relative to the directory from which the compiler was
20551      invoked when the given (base) source file was compiled.  We will fill
20552      in this value in dwarf2out_finish.  */
20553   comp_unit_die = gen_compile_unit_die (NULL);
20554
20555   incomplete_types = VEC_alloc (tree, gc, 64);
20556
20557   used_rtx_array = VEC_alloc (rtx, gc, 32);
20558
20559   debug_info_section = get_section (DEBUG_INFO_SECTION,
20560                                     SECTION_DEBUG, NULL);
20561   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20562                                       SECTION_DEBUG, NULL);
20563   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20564                                        SECTION_DEBUG, NULL);
20565   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20566                                        SECTION_DEBUG, NULL);
20567   debug_line_section = get_section (DEBUG_LINE_SECTION,
20568                                     SECTION_DEBUG, NULL);
20569   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20570                                    SECTION_DEBUG, NULL);
20571   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20572                                         SECTION_DEBUG, NULL);
20573   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20574                                         SECTION_DEBUG, NULL);
20575   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
20576                                      SECTION_DEBUG, NULL);
20577   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
20578                                      SECTION_DEBUG, NULL);
20579   debug_str_section = get_section (DEBUG_STR_SECTION,
20580                                    DEBUG_STR_SECTION_FLAGS, NULL);
20581   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20582                                       SECTION_DEBUG, NULL);
20583   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20584                                      SECTION_DEBUG, NULL);
20585
20586   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20587   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20588                                DEBUG_ABBREV_SECTION_LABEL, 0);
20589   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20590   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20591                                COLD_TEXT_SECTION_LABEL, 0);
20592   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20593
20594   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20595                                DEBUG_INFO_SECTION_LABEL, 0);
20596   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20597                                DEBUG_LINE_SECTION_LABEL, 0);
20598   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20599                                DEBUG_RANGES_SECTION_LABEL, 0);
20600   switch_to_section (debug_abbrev_section);
20601   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
20602   switch_to_section (debug_info_section);
20603   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
20604   switch_to_section (debug_line_section);
20605   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
20606
20607   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20608     {
20609       switch_to_section (debug_macinfo_section);
20610       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20611                                    DEBUG_MACINFO_SECTION_LABEL, 0);
20612       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
20613     }
20614
20615   switch_to_section (text_section);
20616   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20617   if (flag_reorder_blocks_and_partition)
20618     {
20619       cold_text_section = unlikely_text_section ();
20620       switch_to_section (cold_text_section);
20621       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20622     }
20623
20624 }
20625
20626 /* Called before cgraph_optimize starts outputtting functions, variables
20627    and toplevel asms into assembly.  */
20628
20629 static void
20630 dwarf2out_assembly_start (void)
20631 {
20632   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
20633     {
20634 #ifndef TARGET_UNWIND_INFO
20635       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
20636 #endif
20637         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20638     }
20639 }
20640
20641 /* A helper function for dwarf2out_finish called through
20642    htab_traverse.  Emit one queued .debug_str string.  */
20643
20644 static int
20645 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20646 {
20647   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20648
20649   if (node->label && node->refcount)
20650     {
20651       switch_to_section (debug_str_section);
20652       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20653       assemble_string (node->str, strlen (node->str) + 1);
20654     }
20655
20656   return 1;
20657 }
20658
20659 #if ENABLE_ASSERT_CHECKING
20660 /* Verify that all marks are clear.  */
20661
20662 static void
20663 verify_marks_clear (dw_die_ref die)
20664 {
20665   dw_die_ref c;
20666
20667   gcc_assert (! die->die_mark);
20668   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20669 }
20670 #endif /* ENABLE_ASSERT_CHECKING */
20671
20672 /* Clear the marks for a die and its children.
20673    Be cool if the mark isn't set.  */
20674
20675 static void
20676 prune_unmark_dies (dw_die_ref die)
20677 {
20678   dw_die_ref c;
20679
20680   if (die->die_mark)
20681     die->die_mark = 0;
20682   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20683 }
20684
20685 /* Given DIE that we're marking as used, find any other dies
20686    it references as attributes and mark them as used.  */
20687
20688 static void
20689 prune_unused_types_walk_attribs (dw_die_ref die)
20690 {
20691   dw_attr_ref a;
20692   unsigned ix;
20693
20694   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20695     {
20696       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20697         {
20698           /* A reference to another DIE.
20699              Make sure that it will get emitted.
20700              If it was broken out into a comdat group, don't follow it.  */
20701           if (dwarf_version < 4
20702               || a->dw_attr == DW_AT_specification
20703               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20704             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20705         }
20706       /* Set the string's refcount to 0 so that prune_unused_types_mark
20707          accounts properly for it.  */
20708       if (AT_class (a) == dw_val_class_str)
20709         a->dw_attr_val.v.val_str->refcount = 0;
20710     }
20711 }
20712
20713
20714 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20715    to DIE's children.  */
20716
20717 static void
20718 prune_unused_types_mark (dw_die_ref die, int dokids)
20719 {
20720   dw_die_ref c;
20721
20722   if (die->die_mark == 0)
20723     {
20724       /* We haven't done this node yet.  Mark it as used.  */
20725       die->die_mark = 1;
20726
20727       /* We also have to mark its parents as used.
20728          (But we don't want to mark our parents' kids due to this.)  */
20729       if (die->die_parent)
20730         prune_unused_types_mark (die->die_parent, 0);
20731
20732       /* Mark any referenced nodes.  */
20733       prune_unused_types_walk_attribs (die);
20734
20735       /* If this node is a specification,
20736          also mark the definition, if it exists.  */
20737       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20738         prune_unused_types_mark (die->die_definition, 1);
20739     }
20740
20741   if (dokids && die->die_mark != 2)
20742     {
20743       /* We need to walk the children, but haven't done so yet.
20744          Remember that we've walked the kids.  */
20745       die->die_mark = 2;
20746
20747       /* If this is an array type, we need to make sure our
20748          kids get marked, even if they're types.  If we're
20749          breaking out types into comdat sections, do this
20750          for all type definitions.  */
20751       if (die->die_tag == DW_TAG_array_type
20752           || (dwarf_version >= 4
20753               && is_type_die (die) && ! is_declaration_die (die)))
20754         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20755       else
20756         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20757     }
20758 }
20759
20760 /* For local classes, look if any static member functions were emitted
20761    and if so, mark them.  */
20762
20763 static void
20764 prune_unused_types_walk_local_classes (dw_die_ref die)
20765 {
20766   dw_die_ref c;
20767
20768   if (die->die_mark == 2)
20769     return;
20770
20771   switch (die->die_tag)
20772     {
20773     case DW_TAG_structure_type:
20774     case DW_TAG_union_type:
20775     case DW_TAG_class_type:
20776       break;
20777
20778     case DW_TAG_subprogram:
20779       if (!get_AT_flag (die, DW_AT_declaration)
20780           || die->die_definition != NULL)
20781         prune_unused_types_mark (die, 1);
20782       return;
20783
20784     default:
20785       return;
20786     }
20787
20788   /* Mark children.  */
20789   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20790 }
20791
20792 /* Walk the tree DIE and mark types that we actually use.  */
20793
20794 static void
20795 prune_unused_types_walk (dw_die_ref die)
20796 {
20797   dw_die_ref c;
20798
20799   /* Don't do anything if this node is already marked and
20800      children have been marked as well.  */
20801   if (die->die_mark == 2)
20802     return;
20803
20804   switch (die->die_tag)
20805     {
20806     case DW_TAG_structure_type:
20807     case DW_TAG_union_type:
20808     case DW_TAG_class_type:
20809       if (die->die_perennial_p)
20810         break;
20811
20812       for (c = die->die_parent; c; c = c->die_parent)
20813         if (c->die_tag == DW_TAG_subprogram)
20814           break;
20815
20816       /* Finding used static member functions inside of classes
20817          is needed just for local classes, because for other classes
20818          static member function DIEs with DW_AT_specification
20819          are emitted outside of the DW_TAG_*_type.  If we ever change
20820          it, we'd need to call this even for non-local classes.  */
20821       if (c)
20822         prune_unused_types_walk_local_classes (die);
20823
20824       /* It's a type node --- don't mark it.  */
20825       return;
20826
20827     case DW_TAG_const_type:
20828     case DW_TAG_packed_type:
20829     case DW_TAG_pointer_type:
20830     case DW_TAG_reference_type:
20831     case DW_TAG_volatile_type:
20832     case DW_TAG_typedef:
20833     case DW_TAG_array_type:
20834     case DW_TAG_interface_type:
20835     case DW_TAG_friend:
20836     case DW_TAG_variant_part:
20837     case DW_TAG_enumeration_type:
20838     case DW_TAG_subroutine_type:
20839     case DW_TAG_string_type:
20840     case DW_TAG_set_type:
20841     case DW_TAG_subrange_type:
20842     case DW_TAG_ptr_to_member_type:
20843     case DW_TAG_file_type:
20844       if (die->die_perennial_p)
20845         break;
20846
20847       /* It's a type node --- don't mark it.  */
20848       return;
20849
20850     default:
20851       /* Mark everything else.  */
20852       break;
20853   }
20854
20855   if (die->die_mark == 0)
20856     {
20857       die->die_mark = 1;
20858
20859       /* Now, mark any dies referenced from here.  */
20860       prune_unused_types_walk_attribs (die);
20861     }
20862
20863   die->die_mark = 2;
20864
20865   /* Mark children.  */
20866   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20867 }
20868
20869 /* Increment the string counts on strings referred to from DIE's
20870    attributes.  */
20871
20872 static void
20873 prune_unused_types_update_strings (dw_die_ref die)
20874 {
20875   dw_attr_ref a;
20876   unsigned ix;
20877
20878   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20879     if (AT_class (a) == dw_val_class_str)
20880       {
20881         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20882         s->refcount++;
20883         /* Avoid unnecessarily putting strings that are used less than
20884            twice in the hash table.  */
20885         if (s->refcount
20886             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20887           {
20888             void ** slot;
20889             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20890                                              htab_hash_string (s->str),
20891                                              INSERT);
20892             gcc_assert (*slot == NULL);
20893             *slot = s;
20894           }
20895       }
20896 }
20897
20898 /* Remove from the tree DIE any dies that aren't marked.  */
20899
20900 static void
20901 prune_unused_types_prune (dw_die_ref die)
20902 {
20903   dw_die_ref c;
20904
20905   gcc_assert (die->die_mark);
20906   prune_unused_types_update_strings (die);
20907
20908   if (! die->die_child)
20909     return;
20910
20911   c = die->die_child;
20912   do {
20913     dw_die_ref prev = c;
20914     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20915       if (c == die->die_child)
20916         {
20917           /* No marked children between 'prev' and the end of the list.  */
20918           if (prev == c)
20919             /* No marked children at all.  */
20920             die->die_child = NULL;
20921           else
20922             {
20923               prev->die_sib = c->die_sib;
20924               die->die_child = prev;
20925             }
20926           return;
20927         }
20928
20929     if (c != prev->die_sib)
20930       prev->die_sib = c;
20931     prune_unused_types_prune (c);
20932   } while (c != die->die_child);
20933 }
20934
20935 /* A helper function for dwarf2out_finish called through
20936    htab_traverse.  Clear .debug_str strings that we haven't already
20937    decided to emit.  */
20938
20939 static int
20940 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20941 {
20942   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20943
20944   if (!node->label || !node->refcount)
20945     htab_clear_slot (debug_str_hash, h);
20946
20947   return 1;
20948 }
20949
20950 /* Remove dies representing declarations that we never use.  */
20951
20952 static void
20953 prune_unused_types (void)
20954 {
20955   unsigned int i;
20956   limbo_die_node *node;
20957   comdat_type_node *ctnode;
20958   pubname_ref pub;
20959   dcall_entry *dcall;
20960
20961 #if ENABLE_ASSERT_CHECKING
20962   /* All the marks should already be clear.  */
20963   verify_marks_clear (comp_unit_die);
20964   for (node = limbo_die_list; node; node = node->next)
20965     verify_marks_clear (node->die);
20966   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20967     verify_marks_clear (ctnode->root_die);
20968 #endif /* ENABLE_ASSERT_CHECKING */
20969
20970   /* Mark types that are used in global variables.  */
20971   premark_types_used_by_global_vars ();
20972
20973   /* Set the mark on nodes that are actually used.  */
20974   prune_unused_types_walk (comp_unit_die);
20975   for (node = limbo_die_list; node; node = node->next)
20976     prune_unused_types_walk (node->die);
20977   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20978     {
20979       prune_unused_types_walk (ctnode->root_die);
20980       prune_unused_types_mark (ctnode->type_die, 1);
20981     }
20982
20983   /* Also set the mark on nodes referenced from the
20984      pubname_table or arange_table.  */
20985   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
20986     prune_unused_types_mark (pub->die, 1);
20987   for (i = 0; i < arange_table_in_use; i++)
20988     prune_unused_types_mark (arange_table[i], 1);
20989
20990   /* Mark nodes referenced from the direct call table.  */
20991   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
20992     prune_unused_types_mark (dcall->targ_die, 1);
20993
20994   /* Get rid of nodes that aren't marked; and update the string counts.  */
20995   if (debug_str_hash && debug_str_hash_forced)
20996     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
20997   else if (debug_str_hash)
20998     htab_empty (debug_str_hash);
20999   prune_unused_types_prune (comp_unit_die);
21000   for (node = limbo_die_list; node; node = node->next)
21001     prune_unused_types_prune (node->die);
21002   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21003     prune_unused_types_prune (ctnode->root_die);
21004
21005   /* Leave the marks clear.  */
21006   prune_unmark_dies (comp_unit_die);
21007   for (node = limbo_die_list; node; node = node->next)
21008     prune_unmark_dies (node->die);
21009   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21010     prune_unmark_dies (ctnode->root_die);
21011 }
21012
21013 /* Set the parameter to true if there are any relative pathnames in
21014    the file table.  */
21015 static int
21016 file_table_relative_p (void ** slot, void *param)
21017 {
21018   bool *p = (bool *) param;
21019   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21020   if (!IS_ABSOLUTE_PATH (d->filename))
21021     {
21022       *p = true;
21023       return 0;
21024     }
21025   return 1;
21026 }
21027
21028 /* Routines to manipulate hash table of comdat type units.  */
21029
21030 static hashval_t
21031 htab_ct_hash (const void *of)
21032 {
21033   hashval_t h;
21034   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21035
21036   memcpy (&h, type_node->signature, sizeof (h));
21037   return h;
21038 }
21039
21040 static int
21041 htab_ct_eq (const void *of1, const void *of2)
21042 {
21043   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21044   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21045
21046   return (! memcmp (type_node_1->signature, type_node_2->signature,
21047                     DWARF_TYPE_SIGNATURE_SIZE));
21048 }
21049
21050 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
21051    to the location it would have been added, should we know its
21052    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21053    probably improve compactness of debug info, removing equivalent
21054    abbrevs, and hide any differences caused by deferring the
21055    computation of the assembler name, triggered by e.g. PCH.  */
21056
21057 static inline void
21058 move_linkage_attr (dw_die_ref die)
21059 {
21060   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21061   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21062
21063   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
21064
21065   while (--ix > 0)
21066     {
21067       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21068
21069       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21070         break;
21071     }
21072
21073   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21074     {
21075       VEC_pop (dw_attr_node, die->die_attr);
21076       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21077     }
21078 }
21079
21080 /* Helper function for resolve_addr, attempt to resolve
21081    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21082    SYMBOL_REFs refer to variables emitted in the current CU.  */
21083
21084 static int
21085 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21086 {
21087   rtx rtl = *addr;
21088
21089   if (GET_CODE (rtl) == CONST_STRING)
21090     {
21091       size_t len = strlen (XSTR (rtl, 0)) + 1;
21092       tree t = build_string (len, XSTR (rtl, 0));
21093       tree tlen = build_int_cst (NULL_TREE, len - 1);
21094       TREE_TYPE (t)
21095         = build_array_type (char_type_node, build_index_type (tlen));
21096       rtl = lookup_constant_def (t);
21097       if (!rtl || !MEM_P (rtl))
21098         return 1;
21099       rtl = XEXP (rtl, 0);
21100       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21101       *addr = rtl;
21102       return 0;
21103     }
21104
21105   if (GET_CODE (rtl) == SYMBOL_REF
21106       && SYMBOL_REF_DECL (rtl)
21107       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
21108       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21109     return 1;
21110
21111   if (GET_CODE (rtl) == CONST
21112       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21113     return 1;
21114
21115   return 0;
21116 }
21117
21118 /* Helper function for resolve_addr, handle one location
21119    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21120    the location list couldn't be resolved.  */
21121
21122 static bool
21123 resolve_addr_in_expr (dw_loc_descr_ref loc)
21124 {
21125   for (; loc; loc = loc->dw_loc_next)
21126     if ((loc->dw_loc_opc == DW_OP_addr
21127          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21128         || (loc->dw_loc_opc == DW_OP_implicit_value
21129             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21130             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
21131       return false;
21132   return true;
21133 }
21134
21135 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21136    an address in .rodata section if the string literal is emitted there,
21137    or remove the containing location list or replace DW_AT_const_value
21138    with DW_AT_location and empty location expression, if it isn't found
21139    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21140    to something that has been emitted in the current CU.  */
21141
21142 static void
21143 resolve_addr (dw_die_ref die)
21144 {
21145   dw_die_ref c;
21146   dw_attr_ref a;
21147   dw_loc_list_ref *curr;
21148   unsigned ix;
21149
21150   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21151     switch (AT_class (a))
21152       {
21153       case dw_val_class_loc_list:
21154         curr = AT_loc_list_ptr (a);
21155         while (*curr)
21156           {
21157             if (!resolve_addr_in_expr ((*curr)->expr))
21158               {
21159                 dw_loc_list_ref next = (*curr)->dw_loc_next;
21160                 if (next && (*curr)->ll_symbol)
21161                   {
21162                     gcc_assert (!next->ll_symbol);
21163                     next->ll_symbol = (*curr)->ll_symbol;
21164                   }
21165                 *curr = next;
21166               }
21167             else
21168               curr = &(*curr)->dw_loc_next;
21169           }
21170         if (!AT_loc_list (a))
21171           {
21172             remove_AT (die, a->dw_attr);
21173             ix--;
21174           }
21175         break;
21176       case dw_val_class_loc:
21177         if (!resolve_addr_in_expr (AT_loc (a)))
21178           {
21179             remove_AT (die, a->dw_attr);
21180             ix--;
21181           }
21182         break;
21183       case dw_val_class_addr:
21184         if (a->dw_attr == DW_AT_const_value
21185             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21186           {
21187             remove_AT (die, a->dw_attr);
21188             ix--;
21189           }
21190         break;
21191       default:
21192         break;
21193       }
21194
21195   FOR_EACH_CHILD (die, c, resolve_addr (c));
21196 }
21197
21198 /* Output stuff that dwarf requires at the end of every file,
21199    and generate the DWARF-2 debugging info.  */
21200
21201 static void
21202 dwarf2out_finish (const char *filename)
21203 {
21204   limbo_die_node *node, *next_node;
21205   comdat_type_node *ctnode;
21206   htab_t comdat_type_table;
21207   dw_die_ref die = 0;
21208   unsigned int i;
21209
21210   gen_remaining_tmpl_value_param_die_attribute ();
21211
21212   /* Add the name for the main input file now.  We delayed this from
21213      dwarf2out_init to avoid complications with PCH.  */
21214   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
21215   if (!IS_ABSOLUTE_PATH (filename))
21216     add_comp_dir_attribute (comp_unit_die);
21217   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
21218     {
21219       bool p = false;
21220       htab_traverse (file_table, file_table_relative_p, &p);
21221       if (p)
21222         add_comp_dir_attribute (comp_unit_die);
21223     }
21224
21225   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21226     {
21227       add_location_or_const_value_attribute (
21228         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21229         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21230         DW_AT_location);
21231     }
21232
21233   /* Traverse the limbo die list, and add parent/child links.  The only
21234      dies without parents that should be here are concrete instances of
21235      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21236      For concrete instances, we can get the parent die from the abstract
21237      instance.  */
21238   for (node = limbo_die_list; node; node = next_node)
21239     {
21240       next_node = node->next;
21241       die = node->die;
21242
21243       if (die->die_parent == NULL)
21244         {
21245           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21246
21247           if (origin)
21248             add_child_die (origin->die_parent, die);
21249           else if (die == comp_unit_die)
21250             ;
21251           else if (errorcount > 0 || sorrycount > 0)
21252             /* It's OK to be confused by errors in the input.  */
21253             add_child_die (comp_unit_die, die);
21254           else
21255             {
21256               /* In certain situations, the lexical block containing a
21257                  nested function can be optimized away, which results
21258                  in the nested function die being orphaned.  Likewise
21259                  with the return type of that nested function.  Force
21260                  this to be a child of the containing function.
21261
21262                  It may happen that even the containing function got fully
21263                  inlined and optimized out.  In that case we are lost and
21264                  assign the empty child.  This should not be big issue as
21265                  the function is likely unreachable too.  */
21266               tree context = NULL_TREE;
21267
21268               gcc_assert (node->created_for);
21269
21270               if (DECL_P (node->created_for))
21271                 context = DECL_CONTEXT (node->created_for);
21272               else if (TYPE_P (node->created_for))
21273                 context = TYPE_CONTEXT (node->created_for);
21274
21275               gcc_assert (context
21276                           && (TREE_CODE (context) == FUNCTION_DECL
21277                               || TREE_CODE (context) == NAMESPACE_DECL));
21278
21279               origin = lookup_decl_die (context);
21280               if (origin)
21281                 add_child_die (origin, die);
21282               else
21283                 add_child_die (comp_unit_die, die);
21284             }
21285         }
21286     }
21287
21288   limbo_die_list = NULL;
21289
21290   resolve_addr (comp_unit_die);
21291
21292   for (node = deferred_asm_name; node; node = node->next)
21293     {
21294       tree decl = node->created_for;
21295       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21296         {
21297           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
21298                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
21299           move_linkage_attr (node->die);
21300         }
21301     }
21302
21303   deferred_asm_name = NULL;
21304
21305   /* Walk through the list of incomplete types again, trying once more to
21306      emit full debugging info for them.  */
21307   retry_incomplete_types ();
21308
21309   if (flag_eliminate_unused_debug_types)
21310     prune_unused_types ();
21311
21312   /* Generate separate CUs for each of the include files we've seen.
21313      They will go into limbo_die_list.  */
21314   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21315     break_out_includes (comp_unit_die);
21316
21317   /* Generate separate COMDAT sections for type DIEs. */
21318   if (dwarf_version >= 4)
21319     {
21320       break_out_comdat_types (comp_unit_die);
21321
21322       /* Each new type_unit DIE was added to the limbo die list when created.
21323          Since these have all been added to comdat_type_list, clear the
21324          limbo die list.  */
21325       limbo_die_list = NULL;
21326
21327       /* For each new comdat type unit, copy declarations for incomplete
21328          types to make the new unit self-contained (i.e., no direct
21329          references to the main compile unit).  */
21330       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21331         copy_decls_for_unworthy_types (ctnode->root_die);
21332       copy_decls_for_unworthy_types (comp_unit_die);
21333
21334       /* In the process of copying declarations from one unit to another,
21335          we may have left some declarations behind that are no longer
21336          referenced.  Prune them.  */
21337       prune_unused_types ();
21338     }
21339
21340   /* Traverse the DIE's and add add sibling attributes to those DIE's
21341      that have children.  */
21342   add_sibling_attributes (comp_unit_die);
21343   for (node = limbo_die_list; node; node = node->next)
21344     add_sibling_attributes (node->die);
21345   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21346     add_sibling_attributes (ctnode->root_die);
21347
21348   /* Output a terminator label for the .text section.  */
21349   switch_to_section (text_section);
21350   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
21351   if (flag_reorder_blocks_and_partition)
21352     {
21353       switch_to_section (unlikely_text_section ());
21354       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
21355     }
21356
21357   /* We can only use the low/high_pc attributes if all of the code was
21358      in .text.  */
21359   if (!have_multiple_function_sections
21360       || !(dwarf_version >= 3 || !dwarf_strict))
21361     {
21362       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
21363       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
21364     }
21365
21366   else
21367     {
21368       unsigned fde_idx = 0;
21369       bool range_list_added = false;
21370
21371       /* We need to give .debug_loc and .debug_ranges an appropriate
21372          "base address".  Use zero so that these addresses become
21373          absolute.  Historically, we've emitted the unexpected
21374          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
21375          Emit both to give time for other tools to adapt.  */
21376       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
21377       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
21378
21379       if (text_section_used)
21380         add_ranges_by_labels (comp_unit_die, text_section_label,
21381                               text_end_label, &range_list_added);
21382       if (flag_reorder_blocks_and_partition && cold_text_section_used)
21383         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
21384                               cold_end_label, &range_list_added);
21385
21386       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
21387         {
21388           dw_fde_ref fde = &fde_table[fde_idx];
21389
21390           if (fde->dw_fde_switched_sections)
21391             {
21392               if (!fde->in_std_section)
21393                 add_ranges_by_labels (comp_unit_die,
21394                                       fde->dw_fde_hot_section_label,
21395                                       fde->dw_fde_hot_section_end_label,
21396                                       &range_list_added);
21397               if (!fde->cold_in_std_section)
21398                 add_ranges_by_labels (comp_unit_die,
21399                                       fde->dw_fde_unlikely_section_label,
21400                                       fde->dw_fde_unlikely_section_end_label,
21401                                       &range_list_added);
21402             }
21403           else if (!fde->in_std_section)
21404             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
21405                                   fde->dw_fde_end, &range_list_added);
21406         }
21407
21408       if (range_list_added)
21409         add_ranges (NULL);
21410     }
21411
21412   /* Output location list section if necessary.  */
21413   if (have_location_lists)
21414     {
21415       /* Output the location lists info.  */
21416       switch_to_section (debug_loc_section);
21417       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
21418                                    DEBUG_LOC_SECTION_LABEL, 0);
21419       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
21420       output_location_lists (die);
21421     }
21422
21423   if (debug_info_level >= DINFO_LEVEL_NORMAL)
21424     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
21425                     debug_line_section_label);
21426
21427   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21428     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
21429
21430   /* Output all of the compilation units.  We put the main one last so that
21431      the offsets are available to output_pubnames.  */
21432   for (node = limbo_die_list; node; node = node->next)
21433     output_comp_unit (node->die, 0);
21434
21435   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
21436   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21437     {
21438       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
21439
21440       /* Don't output duplicate types.  */
21441       if (*slot != HTAB_EMPTY_ENTRY)
21442         continue;
21443
21444       /* Add a pointer to the line table for the main compilation unit
21445          so that the debugger can make sense of DW_AT_decl_file
21446          attributes.  */
21447       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21448         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
21449                         debug_line_section_label);
21450
21451       output_comdat_type_unit (ctnode);
21452       *slot = ctnode;
21453     }
21454   htab_delete (comdat_type_table);
21455
21456   /* Output the main compilation unit if non-empty or if .debug_macinfo
21457      has been emitted.  */
21458   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
21459
21460   /* Output the abbreviation table.  */
21461   switch_to_section (debug_abbrev_section);
21462   output_abbrev_section ();
21463
21464   /* Output public names table if necessary.  */
21465   if (!VEC_empty (pubname_entry, pubname_table))
21466     {
21467       switch_to_section (debug_pubnames_section);
21468       output_pubnames (pubname_table);
21469     }
21470
21471   /* Output public types table if necessary.  */
21472   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
21473      It shouldn't hurt to emit it always, since pure DWARF2 consumers
21474      simply won't look for the section.  */
21475   if (!VEC_empty (pubname_entry, pubtype_table))
21476     {
21477       switch_to_section (debug_pubtypes_section);
21478       output_pubnames (pubtype_table);
21479     }
21480
21481   /* Output direct and virtual call tables if necessary.  */
21482   if (!VEC_empty (dcall_entry, dcall_table))
21483     {
21484       switch_to_section (debug_dcall_section);
21485       output_dcall_table ();
21486     }
21487   if (!VEC_empty (vcall_entry, vcall_table))
21488     {
21489       switch_to_section (debug_vcall_section);
21490       output_vcall_table ();
21491     }
21492
21493   /* Output the address range information.  We only put functions in the arange
21494      table, so don't write it out if we don't have any.  */
21495   if (fde_table_in_use)
21496     {
21497       switch_to_section (debug_aranges_section);
21498       output_aranges ();
21499     }
21500
21501   /* Output ranges section if necessary.  */
21502   if (ranges_table_in_use)
21503     {
21504       switch_to_section (debug_ranges_section);
21505       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
21506       output_ranges ();
21507     }
21508
21509   /* Output the source line correspondence table.  We must do this
21510      even if there is no line information.  Otherwise, on an empty
21511      translation unit, we will generate a present, but empty,
21512      .debug_info section.  IRIX 6.5 `nm' will then complain when
21513      examining the file.  This is done late so that any filenames
21514      used by the debug_info section are marked as 'used'.  */
21515   if (! DWARF2_ASM_LINE_DEBUG_INFO)
21516     {
21517       switch_to_section (debug_line_section);
21518       output_line_info ();
21519     }
21520
21521   /* Have to end the macro section.  */
21522   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21523     {
21524       switch_to_section (debug_macinfo_section);
21525       dw2_asm_output_data (1, 0, "End compilation unit");
21526     }
21527
21528   /* If we emitted any DW_FORM_strp form attribute, output the string
21529      table too.  */
21530   if (debug_str_hash)
21531     htab_traverse (debug_str_hash, output_indirect_string, NULL);
21532 }
21533 #else
21534
21535 /* This should never be used, but its address is needed for comparisons.  */
21536 const struct gcc_debug_hooks dwarf2_debug_hooks =
21537 {
21538   0,            /* init */
21539   0,            /* finish */
21540   0,            /* assembly_start */
21541   0,            /* define */
21542   0,            /* undef */
21543   0,            /* start_source_file */
21544   0,            /* end_source_file */
21545   0,            /* begin_block */
21546   0,            /* end_block */
21547   0,            /* ignore_block */
21548   0,            /* source_line */
21549   0,            /* begin_prologue */
21550   0,            /* end_prologue */
21551   0,            /* end_epilogue */
21552   0,            /* begin_function */
21553   0,            /* end_function */
21554   0,            /* function_decl */
21555   0,            /* global_decl */
21556   0,            /* type_decl */
21557   0,            /* imported_module_or_decl */
21558   0,            /* deferred_inline_function */
21559   0,            /* outlining_inline_function */
21560   0,            /* label */
21561   0,            /* handle_pch */
21562   0,            /* var_location */
21563   0,            /* switch_text_section */
21564   0,            /* direct_call */
21565   0,            /* virtual_call_token */
21566   0,            /* copy_call_info */
21567   0,            /* virtual_call */
21568   0,            /* set_name */
21569   0             /* start_end_main_source_file */
21570 };
21571
21572 #endif /* DWARF2_DEBUGGING_INFO */
21573
21574 #include "gt-dwarf2out.h"