OSDN Git Service

* config/i386/sse.md (xop_pmacsww, xop_pmacssww, xop_pmacsdd,
[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 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
183     {
184 #ifdef TARGET_UNWIND_INFO
185       return false;
186 #else
187       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
188         return false;
189 #endif
190     }
191
192   saved_do_cfi_asm = true;
193   return true;
194 }
195
196 /* The size of the target's pointer type.  */
197 #ifndef PTR_SIZE
198 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
199 #endif
200
201 /* Array of RTXes referenced by the debugging information, which therefore
202    must be kept around forever.  */
203 static GTY(()) VEC(rtx,gc) *used_rtx_array;
204
205 /* A pointer to the base of a list of incomplete types which might be
206    completed at some later time.  incomplete_types_list needs to be a
207    VEC(tree,gc) because we want to tell the garbage collector about
208    it.  */
209 static GTY(()) VEC(tree,gc) *incomplete_types;
210
211 /* A pointer to the base of a table of references to declaration
212    scopes.  This table is a display which tracks the nesting
213    of declaration scopes at the current scope and containing
214    scopes.  This table is used to find the proper place to
215    define type declaration DIE's.  */
216 static GTY(()) VEC(tree,gc) *decl_scope_table;
217
218 /* Pointers to various DWARF2 sections.  */
219 static GTY(()) section *debug_info_section;
220 static GTY(()) section *debug_abbrev_section;
221 static GTY(()) section *debug_aranges_section;
222 static GTY(()) section *debug_macinfo_section;
223 static GTY(()) section *debug_line_section;
224 static GTY(()) section *debug_loc_section;
225 static GTY(()) section *debug_pubnames_section;
226 static GTY(()) section *debug_pubtypes_section;
227 static GTY(()) section *debug_dcall_section;
228 static GTY(()) section *debug_vcall_section;
229 static GTY(()) section *debug_str_section;
230 static GTY(()) section *debug_ranges_section;
231 static GTY(()) section *debug_frame_section;
232
233 /* Personality decl of current unit.  Used only when assembler does not support
234    personality CFI.  */
235 static GTY(()) rtx current_unit_personality;
236
237 /* How to start an assembler comment.  */
238 #ifndef ASM_COMMENT_START
239 #define ASM_COMMENT_START ";#"
240 #endif
241
242 typedef struct dw_cfi_struct *dw_cfi_ref;
243 typedef struct dw_fde_struct *dw_fde_ref;
244 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
245
246 /* Call frames are described using a sequence of Call Frame
247    Information instructions.  The register number, offset
248    and address fields are provided as possible operands;
249    their use is selected by the opcode field.  */
250
251 enum dw_cfi_oprnd_type {
252   dw_cfi_oprnd_unused,
253   dw_cfi_oprnd_reg_num,
254   dw_cfi_oprnd_offset,
255   dw_cfi_oprnd_addr,
256   dw_cfi_oprnd_loc
257 };
258
259 typedef union GTY(()) dw_cfi_oprnd_struct {
260   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
261   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
262   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
263   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
264 }
265 dw_cfi_oprnd;
266
267 typedef struct GTY(()) dw_cfi_struct {
268   dw_cfi_ref dw_cfi_next;
269   enum dwarf_call_frame_info dw_cfi_opc;
270   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
271     dw_cfi_oprnd1;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd2;
274 }
275 dw_cfi_node;
276
277 /* This is how we define the location of the CFA. We use to handle it
278    as REG + OFFSET all the time,  but now it can be more complex.
279    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
280    Instead of passing around REG and OFFSET, we pass a copy
281    of this structure.  */
282 typedef struct GTY(()) cfa_loc {
283   HOST_WIDE_INT offset;
284   HOST_WIDE_INT base_offset;
285   unsigned int reg;
286   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
287   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
288 } dw_cfa_location;
289
290 /* All call frame descriptions (FDE's) in the GCC generated DWARF
291    refer to a single Common Information Entry (CIE), defined at
292    the beginning of the .debug_frame section.  This use of a single
293    CIE obviates the need to keep track of multiple CIE's
294    in the DWARF generation routines below.  */
295
296 typedef struct GTY(()) dw_fde_struct {
297   tree decl;
298   const char *dw_fde_begin;
299   const char *dw_fde_current_label;
300   const char *dw_fde_end;
301   const char *dw_fde_hot_section_label;
302   const char *dw_fde_hot_section_end_label;
303   const char *dw_fde_unlikely_section_label;
304   const char *dw_fde_unlikely_section_end_label;
305   dw_cfi_ref dw_fde_cfi;
306   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
307   unsigned funcdef_number;
308   HOST_WIDE_INT stack_realignment;
309   /* Dynamic realign argument pointer register.  */
310   unsigned int drap_reg;
311   /* Virtual dynamic realign argument pointer register.  */
312   unsigned int vdrap_reg;
313   unsigned all_throwers_are_sibcalls : 1;
314   unsigned nothrow : 1;
315   unsigned uses_eh_lsda : 1;
316   /* Whether we did stack realign in this call frame.  */
317   unsigned stack_realign : 1;
318   /* Whether dynamic realign argument pointer register has been saved.  */
319   unsigned drap_reg_saved: 1;
320   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
321   unsigned in_std_section : 1;
322   /* True iff dw_fde_unlikely_section_label is in text_section or
323      cold_text_section.  */
324   unsigned cold_in_std_section : 1;
325   /* True iff switched sections.  */
326   unsigned dw_fde_switched_sections : 1;
327   /* True iff switching from cold to hot section.  */
328   unsigned dw_fde_switched_cold_to_hot : 1;
329 }
330 dw_fde_node;
331
332 /* Maximum size (in bytes) of an artificially generated label.  */
333 #define MAX_ARTIFICIAL_LABEL_BYTES      30
334
335 /* The size of addresses as they appear in the Dwarf 2 data.
336    Some architectures use word addresses to refer to code locations,
337    but Dwarf 2 info always uses byte addresses.  On such machines,
338    Dwarf 2 addresses need to be larger than the architecture's
339    pointers.  */
340 #ifndef DWARF2_ADDR_SIZE
341 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
342 #endif
343
344 /* The size in bytes of a DWARF field indicating an offset or length
345    relative to a debug info section, specified to be 4 bytes in the
346    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
347    as PTR_SIZE.  */
348
349 #ifndef DWARF_OFFSET_SIZE
350 #define DWARF_OFFSET_SIZE 4
351 #endif
352
353 /* The size in bytes of a DWARF 4 type signature.  */
354
355 #ifndef DWARF_TYPE_SIGNATURE_SIZE
356 #define DWARF_TYPE_SIGNATURE_SIZE 8
357 #endif
358
359 /* According to the (draft) DWARF 3 specification, the initial length
360    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
361    bytes are 0xffffffff, followed by the length stored in the next 8
362    bytes.
363
364    However, the SGI/MIPS ABI uses an initial length which is equal to
365    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
366
367 #ifndef DWARF_INITIAL_LENGTH_SIZE
368 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
369 #endif
370
371 /* Round SIZE up to the nearest BOUNDARY.  */
372 #define DWARF_ROUND(SIZE,BOUNDARY) \
373   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
374
375 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
376 #ifndef DWARF_CIE_DATA_ALIGNMENT
377 #ifdef STACK_GROWS_DOWNWARD
378 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
379 #else
380 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
381 #endif
382 #endif
383
384 /* CIE identifier.  */
385 #if HOST_BITS_PER_WIDE_INT >= 64
386 #define DWARF_CIE_ID \
387   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
388 #else
389 #define DWARF_CIE_ID DW_CIE_ID
390 #endif
391
392 /* A pointer to the base of a table that contains frame description
393    information for each routine.  */
394 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
395
396 /* Number of elements currently allocated for fde_table.  */
397 static GTY(()) unsigned fde_table_allocated;
398
399 /* Number of elements in fde_table currently in use.  */
400 static GTY(()) unsigned fde_table_in_use;
401
402 /* Size (in elements) of increments by which we may expand the
403    fde_table.  */
404 #define FDE_TABLE_INCREMENT 256
405
406 /* Get the current fde_table entry we should use.  */
407
408 static inline dw_fde_ref
409 current_fde (void)
410 {
411   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
412 }
413
414 /* A list of call frame insns for the CIE.  */
415 static GTY(()) dw_cfi_ref cie_cfi_head;
416
417 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
418 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
419    attribute that accelerates the lookup of the FDE associated
420    with the subprogram.  This variable holds the table index of the FDE
421    associated with the current function (body) definition.  */
422 static unsigned current_funcdef_fde;
423 #endif
424
425 struct GTY(()) indirect_string_node {
426   const char *str;
427   unsigned int refcount;
428   enum dwarf_form form;
429   char *label;
430 };
431
432 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
433
434 /* True if the compilation unit has location entries that reference
435    debug strings.  */
436 static GTY(()) bool debug_str_hash_forced = false;
437
438 static GTY(()) int dw2_string_counter;
439 static GTY(()) unsigned long dwarf2out_cfi_label_num;
440
441 /* True if the compilation unit places functions in more than one section.  */
442 static GTY(()) bool have_multiple_function_sections = false;
443
444 /* Whether the default text and cold text sections have been used at all.  */
445
446 static GTY(()) bool text_section_used = false;
447 static GTY(()) bool cold_text_section_used = false;
448
449 /* The default cold text section.  */
450 static GTY(()) section *cold_text_section;
451
452 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
453
454 /* Forward declarations for functions defined in this file.  */
455
456 static char *stripattributes (const char *);
457 static const char *dwarf_cfi_name (unsigned);
458 static dw_cfi_ref new_cfi (void);
459 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
460 static void add_fde_cfi (const char *, dw_cfi_ref);
461 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
462 static void lookup_cfa (dw_cfa_location *);
463 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
464 #ifdef DWARF2_UNWIND_INFO
465 static void initial_return_save (rtx);
466 #endif
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468                                           HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static void dwarf2out_stack_adjust (rtx, bool);
474 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
475 static void flush_queued_reg_saves (void);
476 static bool clobbers_queued_reg_save (const_rtx);
477 static void dwarf2out_frame_debug_expr (rtx, const char *);
478
479 /* Support for complex CFA locations.  */
480 static void output_cfa_loc (dw_cfi_ref);
481 static void output_cfa_loc_raw (dw_cfi_ref);
482 static void get_cfa_from_loc_descr (dw_cfa_location *,
483                                     struct dw_loc_descr_struct *);
484 static struct dw_loc_descr_struct *build_cfa_loc
485   (dw_cfa_location *, HOST_WIDE_INT);
486 static struct dw_loc_descr_struct *build_cfa_aligned_loc
487   (HOST_WIDE_INT, HOST_WIDE_INT);
488 static void def_cfa_1 (const char *, dw_cfa_location *);
489
490 /* How to start an assembler comment.  */
491 #ifndef ASM_COMMENT_START
492 #define ASM_COMMENT_START ";#"
493 #endif
494
495 /* Data and reference forms for relocatable data.  */
496 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
497 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
498
499 #ifndef DEBUG_FRAME_SECTION
500 #define DEBUG_FRAME_SECTION     ".debug_frame"
501 #endif
502
503 #ifndef FUNC_BEGIN_LABEL
504 #define FUNC_BEGIN_LABEL        "LFB"
505 #endif
506
507 #ifndef FUNC_END_LABEL
508 #define FUNC_END_LABEL          "LFE"
509 #endif
510
511 #ifndef FRAME_BEGIN_LABEL
512 #define FRAME_BEGIN_LABEL       "Lframe"
513 #endif
514 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
515 #define CIE_END_LABEL           "LECIE"
516 #define FDE_LABEL               "LSFDE"
517 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
518 #define FDE_END_LABEL           "LEFDE"
519 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
520 #define LINE_NUMBER_END_LABEL   "LELT"
521 #define LN_PROLOG_AS_LABEL      "LASLTP"
522 #define LN_PROLOG_END_LABEL     "LELTP"
523 #define DIE_LABEL_PREFIX        "DW"
524
525 /* The DWARF 2 CFA column which tracks the return address.  Normally this
526    is the column for PC, or the first column after all of the hard
527    registers.  */
528 #ifndef DWARF_FRAME_RETURN_COLUMN
529 #ifdef PC_REGNUM
530 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
531 #else
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
533 #endif
534 #endif
535
536 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
537    default, we just provide columns for all registers.  */
538 #ifndef DWARF_FRAME_REGNUM
539 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
540 #endif
541 \f
542 /* Hook used by __throw.  */
543
544 rtx
545 expand_builtin_dwarf_sp_column (void)
546 {
547   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
548   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
549 }
550
551 /* Return a pointer to a copy of the section string name S with all
552    attributes stripped off, and an asterisk prepended (for assemble_name).  */
553
554 static inline char *
555 stripattributes (const char *s)
556 {
557   char *stripped = XNEWVEC (char, strlen (s) + 2);
558   char *p = stripped;
559
560   *p++ = '*';
561
562   while (*s && *s != ',')
563     *p++ = *s++;
564
565   *p = '\0';
566   return stripped;
567 }
568
569 /* MEM is a memory reference for the register size table, each element of
570    which has mode MODE.  Initialize column C as a return address column.  */
571
572 static void
573 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
574 {
575   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
576   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
577   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
578 }
579
580 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
581
582 static inline HOST_WIDE_INT
583 div_data_align (HOST_WIDE_INT off)
584 {
585   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
586   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
587   return r;
588 }
589
590 /* Return true if we need a signed version of a given opcode
591    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
592
593 static inline bool
594 need_data_align_sf_opcode (HOST_WIDE_INT off)
595 {
596   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
597 }
598
599 /* Generate code to initialize the register size table.  */
600
601 void
602 expand_builtin_init_dwarf_reg_sizes (tree address)
603 {
604   unsigned int i;
605   enum machine_mode mode = TYPE_MODE (char_type_node);
606   rtx addr = expand_normal (address);
607   rtx mem = gen_rtx_MEM (BLKmode, addr);
608   bool wrote_return_column = false;
609
610   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
611     {
612       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
613
614       if (rnum < DWARF_FRAME_REGISTERS)
615         {
616           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
617           enum machine_mode save_mode = reg_raw_mode[i];
618           HOST_WIDE_INT size;
619
620           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
621             save_mode = choose_hard_reg_mode (i, 1, true);
622           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
623             {
624               if (save_mode == VOIDmode)
625                 continue;
626               wrote_return_column = true;
627             }
628           size = GET_MODE_SIZE (save_mode);
629           if (offset < 0)
630             continue;
631
632           emit_move_insn (adjust_address (mem, mode, offset),
633                           gen_int_mode (size, mode));
634         }
635     }
636
637   if (!wrote_return_column)
638     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
639
640 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
641   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
642 #endif
643
644   targetm.init_dwarf_reg_sizes_extra (address);
645 }
646
647 /* Convert a DWARF call frame info. operation to its string name */
648
649 static const char *
650 dwarf_cfi_name (unsigned int cfi_opc)
651 {
652   switch (cfi_opc)
653     {
654     case DW_CFA_advance_loc:
655       return "DW_CFA_advance_loc";
656     case DW_CFA_offset:
657       return "DW_CFA_offset";
658     case DW_CFA_restore:
659       return "DW_CFA_restore";
660     case DW_CFA_nop:
661       return "DW_CFA_nop";
662     case DW_CFA_set_loc:
663       return "DW_CFA_set_loc";
664     case DW_CFA_advance_loc1:
665       return "DW_CFA_advance_loc1";
666     case DW_CFA_advance_loc2:
667       return "DW_CFA_advance_loc2";
668     case DW_CFA_advance_loc4:
669       return "DW_CFA_advance_loc4";
670     case DW_CFA_offset_extended:
671       return "DW_CFA_offset_extended";
672     case DW_CFA_restore_extended:
673       return "DW_CFA_restore_extended";
674     case DW_CFA_undefined:
675       return "DW_CFA_undefined";
676     case DW_CFA_same_value:
677       return "DW_CFA_same_value";
678     case DW_CFA_register:
679       return "DW_CFA_register";
680     case DW_CFA_remember_state:
681       return "DW_CFA_remember_state";
682     case DW_CFA_restore_state:
683       return "DW_CFA_restore_state";
684     case DW_CFA_def_cfa:
685       return "DW_CFA_def_cfa";
686     case DW_CFA_def_cfa_register:
687       return "DW_CFA_def_cfa_register";
688     case DW_CFA_def_cfa_offset:
689       return "DW_CFA_def_cfa_offset";
690
691     /* DWARF 3 */
692     case DW_CFA_def_cfa_expression:
693       return "DW_CFA_def_cfa_expression";
694     case DW_CFA_expression:
695       return "DW_CFA_expression";
696     case DW_CFA_offset_extended_sf:
697       return "DW_CFA_offset_extended_sf";
698     case DW_CFA_def_cfa_sf:
699       return "DW_CFA_def_cfa_sf";
700     case DW_CFA_def_cfa_offset_sf:
701       return "DW_CFA_def_cfa_offset_sf";
702
703     /* SGI/MIPS specific */
704     case DW_CFA_MIPS_advance_loc8:
705       return "DW_CFA_MIPS_advance_loc8";
706
707     /* GNU extensions */
708     case DW_CFA_GNU_window_save:
709       return "DW_CFA_GNU_window_save";
710     case DW_CFA_GNU_args_size:
711       return "DW_CFA_GNU_args_size";
712     case DW_CFA_GNU_negative_offset_extended:
713       return "DW_CFA_GNU_negative_offset_extended";
714
715     default:
716       return "DW_CFA_<unknown>";
717     }
718 }
719
720 /* Return a pointer to a newly allocated Call Frame Instruction.  */
721
722 static inline dw_cfi_ref
723 new_cfi (void)
724 {
725   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
726
727   cfi->dw_cfi_next = NULL;
728   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
729   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
730
731   return cfi;
732 }
733
734 /* Add a Call Frame Instruction to list of instructions.  */
735
736 static inline void
737 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
738 {
739   dw_cfi_ref *p;
740   dw_fde_ref fde = current_fde ();
741
742   /* When DRAP is used, CFA is defined with an expression.  Redefine
743      CFA may lead to a different CFA value.   */
744   /* ??? Of course, this heuristic fails when we're annotating epilogues,
745      because of course we'll always want to redefine the CFA back to the
746      stack pointer on the way out.  Where should we move this check?  */
747   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
748     switch (cfi->dw_cfi_opc)
749       {
750         case DW_CFA_def_cfa_register:
751         case DW_CFA_def_cfa_offset:
752         case DW_CFA_def_cfa_offset_sf:
753         case DW_CFA_def_cfa:
754         case DW_CFA_def_cfa_sf:
755           gcc_unreachable ();
756
757         default:
758           break;
759       }
760
761   /* Find the end of the chain.  */
762   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
763     ;
764
765   *p = cfi;
766 }
767
768 /* Generate a new label for the CFI info to refer to.  FORCE is true
769    if a label needs to be output even when using .cfi_* directives.  */
770
771 char *
772 dwarf2out_cfi_label (bool force)
773 {
774   static char label[20];
775
776   if (!force && dwarf2out_do_cfi_asm ())
777     {
778       /* In this case, we will be emitting the asm directive instead of
779          the label, so just return a placeholder to keep the rest of the
780          interfaces happy.  */
781       strcpy (label, "<do not output>");
782     }
783   else
784     {
785       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
786       ASM_OUTPUT_LABEL (asm_out_file, label);
787     }
788
789   return label;
790 }
791
792 /* True if remember_state should be emitted before following CFI directive.  */
793 static bool emit_cfa_remember;
794
795 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
796    or to the CIE if LABEL is NULL.  */
797
798 static void
799 add_fde_cfi (const char *label, dw_cfi_ref cfi)
800 {
801   dw_cfi_ref *list_head;
802
803   if (emit_cfa_remember)
804     {
805       dw_cfi_ref cfi_remember;
806
807       /* Emit the state save.  */
808       emit_cfa_remember = false;
809       cfi_remember = new_cfi (); 
810       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
811       add_fde_cfi (label, cfi_remember);
812     }
813
814   list_head = &cie_cfi_head;
815
816   if (dwarf2out_do_cfi_asm ())
817     {
818       if (label)
819         {
820           dw_fde_ref fde = current_fde ();
821
822           gcc_assert (fde != NULL);
823
824           /* We still have to add the cfi to the list so that lookup_cfa
825              works later on.  When -g2 and above we even need to force
826              emitting of CFI labels and add to list a DW_CFA_set_loc for
827              convert_cfa_to_fb_loc_list purposes.  If we're generating
828              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
829              convert_cfa_to_fb_loc_list.  */
830           if (dwarf_version == 2
831               && debug_info_level > DINFO_LEVEL_TERSE
832               && (write_symbols == DWARF2_DEBUG
833                   || write_symbols == VMS_AND_DWARF2_DEBUG))
834             {
835               switch (cfi->dw_cfi_opc)
836                 {
837                 case DW_CFA_def_cfa_offset:
838                 case DW_CFA_def_cfa_offset_sf:
839                 case DW_CFA_def_cfa_register:
840                 case DW_CFA_def_cfa:
841                 case DW_CFA_def_cfa_sf:
842                 case DW_CFA_def_cfa_expression:
843                 case DW_CFA_restore_state:
844                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
845                     label = dwarf2out_cfi_label (true);
846
847                   if (fde->dw_fde_current_label == NULL
848                       || strcmp (label, fde->dw_fde_current_label) != 0)
849                     {
850                       dw_cfi_ref xcfi;
851
852                       label = xstrdup (label);
853
854                       /* Set the location counter to the new label.  */
855                       xcfi = new_cfi ();
856                       /* It doesn't metter whether DW_CFA_set_loc
857                          or DW_CFA_advance_loc4 is added here, those aren't
858                          emitted into assembly, only looked up by
859                          convert_cfa_to_fb_loc_list.  */
860                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
861                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
862                       add_cfi (&fde->dw_fde_cfi, xcfi);
863                       fde->dw_fde_current_label = label;
864                     }
865                   break;
866                 default:
867                   break;
868                 }
869             }
870
871           output_cfi_directive (cfi);
872
873           list_head = &fde->dw_fde_cfi;
874         }
875       /* ??? If this is a CFI for the CIE, we don't emit.  This
876          assumes that the standard CIE contents that the assembler
877          uses matches the standard CIE contents that the compiler
878          uses.  This is probably a bad assumption.  I'm not quite
879          sure how to address this for now.  */
880     }
881   else if (label)
882     {
883       dw_fde_ref fde = current_fde ();
884
885       gcc_assert (fde != NULL);
886
887       if (*label == 0)
888         label = dwarf2out_cfi_label (false);
889
890       if (fde->dw_fde_current_label == NULL
891           || strcmp (label, fde->dw_fde_current_label) != 0)
892         {
893           dw_cfi_ref xcfi;
894
895           label = xstrdup (label);
896
897           /* Set the location counter to the new label.  */
898           xcfi = new_cfi ();
899           /* If we have a current label, advance from there, otherwise
900              set the location directly using set_loc.  */
901           xcfi->dw_cfi_opc = fde->dw_fde_current_label
902                              ? DW_CFA_advance_loc4
903                              : DW_CFA_set_loc;
904           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
905           add_cfi (&fde->dw_fde_cfi, xcfi);
906
907           fde->dw_fde_current_label = label;
908         }
909
910       list_head = &fde->dw_fde_cfi;
911     }
912
913   add_cfi (list_head, cfi);
914 }
915
916 /* Subroutine of lookup_cfa.  */
917
918 static void
919 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
920 {
921   switch (cfi->dw_cfi_opc)
922     {
923     case DW_CFA_def_cfa_offset:
924     case DW_CFA_def_cfa_offset_sf:
925       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
926       break;
927     case DW_CFA_def_cfa_register:
928       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
929       break;
930     case DW_CFA_def_cfa:
931     case DW_CFA_def_cfa_sf:
932       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
933       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
934       break;
935     case DW_CFA_def_cfa_expression:
936       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
937       break;
938
939     case DW_CFA_remember_state:
940       gcc_assert (!remember->in_use);
941       *remember = *loc;
942       remember->in_use = 1;
943       break;
944     case DW_CFA_restore_state:
945       gcc_assert (remember->in_use);
946       *loc = *remember;
947       remember->in_use = 0;
948       break;
949
950     default:
951       break;
952     }
953 }
954
955 /* Find the previous value for the CFA.  */
956
957 static void
958 lookup_cfa (dw_cfa_location *loc)
959 {
960   dw_cfi_ref cfi;
961   dw_fde_ref fde;
962   dw_cfa_location remember;
963
964   memset (loc, 0, sizeof (*loc));
965   loc->reg = INVALID_REGNUM;
966   remember = *loc;
967
968   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
969     lookup_cfa_1 (cfi, loc, &remember);
970
971   fde = current_fde ();
972   if (fde)
973     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
974       lookup_cfa_1 (cfi, loc, &remember);
975 }
976
977 /* The current rule for calculating the DWARF2 canonical frame address.  */
978 static dw_cfa_location cfa;
979
980 /* The register used for saving registers to the stack, and its offset
981    from the CFA.  */
982 static dw_cfa_location cfa_store;
983
984 /* The current save location around an epilogue.  */
985 static dw_cfa_location cfa_remember;
986
987 /* The running total of the size of arguments pushed onto the stack.  */
988 static HOST_WIDE_INT args_size;
989
990 /* The last args_size we actually output.  */
991 static HOST_WIDE_INT old_args_size;
992
993 /* Entry point to update the canonical frame address (CFA).
994    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
995    calculated from REG+OFFSET.  */
996
997 void
998 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
999 {
1000   dw_cfa_location loc;
1001   loc.indirect = 0;
1002   loc.base_offset = 0;
1003   loc.reg = reg;
1004   loc.offset = offset;
1005   def_cfa_1 (label, &loc);
1006 }
1007
1008 /* Determine if two dw_cfa_location structures define the same data.  */
1009
1010 static bool
1011 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1012 {
1013   return (loc1->reg == loc2->reg
1014           && loc1->offset == loc2->offset
1015           && loc1->indirect == loc2->indirect
1016           && (loc1->indirect == 0
1017               || loc1->base_offset == loc2->base_offset));
1018 }
1019
1020 /* This routine does the actual work.  The CFA is now calculated from
1021    the dw_cfa_location structure.  */
1022
1023 static void
1024 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1025 {
1026   dw_cfi_ref cfi;
1027   dw_cfa_location old_cfa, loc;
1028
1029   cfa = *loc_p;
1030   loc = *loc_p;
1031
1032   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1033     cfa_store.offset = loc.offset;
1034
1035   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1036   lookup_cfa (&old_cfa);
1037
1038   /* If nothing changed, no need to issue any call frame instructions.  */
1039   if (cfa_equal_p (&loc, &old_cfa))
1040     return;
1041
1042   cfi = new_cfi ();
1043
1044   if (loc.reg == old_cfa.reg && !loc.indirect)
1045     {
1046       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1047          the CFA register did not change but the offset did.  The data 
1048          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1049          in the assembler via the .cfi_def_cfa_offset directive.  */
1050       if (loc.offset < 0)
1051         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1052       else
1053         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1054       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1055     }
1056
1057 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1058   else if (loc.offset == old_cfa.offset
1059            && old_cfa.reg != INVALID_REGNUM
1060            && !loc.indirect)
1061     {
1062       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1063          indicating the CFA register has changed to <register> but the
1064          offset has not changed.  */
1065       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1066       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1067     }
1068 #endif
1069
1070   else if (loc.indirect == 0)
1071     {
1072       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1073          indicating the CFA register has changed to <register> with
1074          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1075          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1076          directive.  */
1077       if (loc.offset < 0)
1078         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1079       else
1080         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1081       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1082       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1083     }
1084   else
1085     {
1086       /* Construct a DW_CFA_def_cfa_expression instruction to
1087          calculate the CFA using a full location expression since no
1088          register-offset pair is available.  */
1089       struct dw_loc_descr_struct *loc_list;
1090
1091       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1092       loc_list = build_cfa_loc (&loc, 0);
1093       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1094     }
1095
1096   add_fde_cfi (label, cfi);
1097 }
1098
1099 /* Add the CFI for saving a register.  REG is the CFA column number.
1100    LABEL is passed to add_fde_cfi.
1101    If SREG is -1, the register is saved at OFFSET from the CFA;
1102    otherwise it is saved in SREG.  */
1103
1104 static void
1105 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1106 {
1107   dw_cfi_ref cfi = new_cfi ();
1108   dw_fde_ref fde = current_fde ();
1109
1110   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1111
1112   /* When stack is aligned, store REG using DW_CFA_expression with
1113      FP.  */
1114   if (fde
1115       && fde->stack_realign
1116       && sreg == INVALID_REGNUM)
1117     {
1118       cfi->dw_cfi_opc = DW_CFA_expression;
1119       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1120       cfi->dw_cfi_oprnd1.dw_cfi_loc
1121         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1122     }
1123   else if (sreg == INVALID_REGNUM)
1124     {
1125       if (need_data_align_sf_opcode (offset))
1126         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1127       else if (reg & ~0x3f)
1128         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1129       else
1130         cfi->dw_cfi_opc = DW_CFA_offset;
1131       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1132     }
1133   else if (sreg == reg)
1134     cfi->dw_cfi_opc = DW_CFA_same_value;
1135   else
1136     {
1137       cfi->dw_cfi_opc = DW_CFA_register;
1138       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1139     }
1140
1141   add_fde_cfi (label, cfi);
1142 }
1143
1144 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1145    This CFI tells the unwinder that it needs to restore the window registers
1146    from the previous frame's window save area.
1147
1148    ??? Perhaps we should note in the CIE where windows are saved (instead of
1149    assuming 0(cfa)) and what registers are in the window.  */
1150
1151 void
1152 dwarf2out_window_save (const char *label)
1153 {
1154   dw_cfi_ref cfi = new_cfi ();
1155
1156   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1157   add_fde_cfi (label, cfi);
1158 }
1159
1160 /* Add a CFI to update the running total of the size of arguments
1161    pushed onto the stack.  */
1162
1163 void
1164 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1165 {
1166   dw_cfi_ref cfi;
1167
1168   if (size == old_args_size)
1169     return;
1170
1171   old_args_size = size;
1172
1173   cfi = new_cfi ();
1174   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1175   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1176   add_fde_cfi (label, cfi);
1177 }
1178
1179 /* Entry point for saving a register to the stack.  REG is the GCC register
1180    number.  LABEL and OFFSET are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1184 {
1185   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1186 }
1187
1188 /* Entry point for saving the return address in the stack.
1189    LABEL and OFFSET are passed to reg_save.  */
1190
1191 void
1192 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1193 {
1194   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1195 }
1196
1197 /* Entry point for saving the return address in a register.
1198    LABEL and SREG are passed to reg_save.  */
1199
1200 void
1201 dwarf2out_return_reg (const char *label, unsigned int sreg)
1202 {
1203   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1204 }
1205
1206 #ifdef DWARF2_UNWIND_INFO
1207 /* Record the initial position of the return address.  RTL is
1208    INCOMING_RETURN_ADDR_RTX.  */
1209
1210 static void
1211 initial_return_save (rtx rtl)
1212 {
1213   unsigned int reg = INVALID_REGNUM;
1214   HOST_WIDE_INT offset = 0;
1215
1216   switch (GET_CODE (rtl))
1217     {
1218     case REG:
1219       /* RA is in a register.  */
1220       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1221       break;
1222
1223     case MEM:
1224       /* RA is on the stack.  */
1225       rtl = XEXP (rtl, 0);
1226       switch (GET_CODE (rtl))
1227         {
1228         case REG:
1229           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1230           offset = 0;
1231           break;
1232
1233         case PLUS:
1234           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1235           offset = INTVAL (XEXP (rtl, 1));
1236           break;
1237
1238         case MINUS:
1239           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1240           offset = -INTVAL (XEXP (rtl, 1));
1241           break;
1242
1243         default:
1244           gcc_unreachable ();
1245         }
1246
1247       break;
1248
1249     case PLUS:
1250       /* The return address is at some offset from any value we can
1251          actually load.  For instance, on the SPARC it is in %i7+8. Just
1252          ignore the offset for now; it doesn't matter for unwinding frames.  */
1253       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1254       initial_return_save (XEXP (rtl, 0));
1255       return;
1256
1257     default:
1258       gcc_unreachable ();
1259     }
1260
1261   if (reg != DWARF_FRAME_RETURN_COLUMN)
1262     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1263 }
1264 #endif
1265
1266 /* Given a SET, calculate the amount of stack adjustment it
1267    contains.  */
1268
1269 static HOST_WIDE_INT
1270 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1271                      HOST_WIDE_INT cur_offset)
1272 {
1273   const_rtx src = SET_SRC (pattern);
1274   const_rtx dest = SET_DEST (pattern);
1275   HOST_WIDE_INT offset = 0;
1276   enum rtx_code code;
1277
1278   if (dest == stack_pointer_rtx)
1279     {
1280       code = GET_CODE (src);
1281
1282       /* Assume (set (reg sp) (reg whatever)) sets args_size
1283          level to 0.  */
1284       if (code == REG && src != stack_pointer_rtx)
1285         {
1286           offset = -cur_args_size;
1287 #ifndef STACK_GROWS_DOWNWARD
1288           offset = -offset;
1289 #endif
1290           return offset - cur_offset;
1291         }
1292
1293       if (! (code == PLUS || code == MINUS)
1294           || XEXP (src, 0) != stack_pointer_rtx
1295           || !CONST_INT_P (XEXP (src, 1)))
1296         return 0;
1297
1298       /* (set (reg sp) (plus (reg sp) (const_int))) */
1299       offset = INTVAL (XEXP (src, 1));
1300       if (code == PLUS)
1301         offset = -offset;
1302       return offset;
1303     }
1304
1305   if (MEM_P (src) && !MEM_P (dest))
1306     dest = src;
1307   if (MEM_P (dest))
1308     {
1309       /* (set (mem (pre_dec (reg sp))) (foo)) */
1310       src = XEXP (dest, 0);
1311       code = GET_CODE (src);
1312
1313       switch (code)
1314         {
1315         case PRE_MODIFY:
1316         case POST_MODIFY:
1317           if (XEXP (src, 0) == stack_pointer_rtx)
1318             {
1319               rtx val = XEXP (XEXP (src, 1), 1);
1320               /* We handle only adjustments by constant amount.  */
1321               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1322                           && CONST_INT_P (val));
1323               offset = -INTVAL (val);
1324               break;
1325             }
1326           return 0;
1327
1328         case PRE_DEC:
1329         case POST_DEC:
1330           if (XEXP (src, 0) == stack_pointer_rtx)
1331             {
1332               offset = GET_MODE_SIZE (GET_MODE (dest));
1333               break;
1334             }
1335           return 0;
1336
1337         case PRE_INC:
1338         case POST_INC:
1339           if (XEXP (src, 0) == stack_pointer_rtx)
1340             {
1341               offset = -GET_MODE_SIZE (GET_MODE (dest));
1342               break;
1343             }
1344           return 0;
1345
1346         default:
1347           return 0;
1348         }
1349     }
1350   else
1351     return 0;
1352
1353   return offset;
1354 }
1355
1356 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1357    indexed by INSN_UID.  */
1358
1359 static HOST_WIDE_INT *barrier_args_size;
1360
1361 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1362
1363 static HOST_WIDE_INT
1364 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1365                              VEC (rtx, heap) **next)
1366 {
1367   HOST_WIDE_INT offset = 0;
1368   int i;
1369
1370   if (! RTX_FRAME_RELATED_P (insn))
1371     {
1372       if (prologue_epilogue_contains (insn))
1373         /* Nothing */;
1374       else if (GET_CODE (PATTERN (insn)) == SET)
1375         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1376       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1377                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1378         {
1379           /* There may be stack adjustments inside compound insns.  Search
1380              for them.  */
1381           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1382             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1383               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1384                                              cur_args_size, offset);
1385         }
1386     }
1387   else
1388     {
1389       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1390
1391       if (expr)
1392         {
1393           expr = XEXP (expr, 0);
1394           if (GET_CODE (expr) == PARALLEL
1395               || GET_CODE (expr) == SEQUENCE)
1396             for (i = 1; i < XVECLEN (expr, 0); i++)
1397               {
1398                 rtx elem = XVECEXP (expr, 0, i);
1399
1400                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1401                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1402               }
1403         }
1404     }
1405
1406 #ifndef STACK_GROWS_DOWNWARD
1407   offset = -offset;
1408 #endif
1409
1410   cur_args_size += offset;
1411   if (cur_args_size < 0)
1412     cur_args_size = 0;
1413
1414   if (JUMP_P (insn))
1415     {
1416       rtx dest = JUMP_LABEL (insn);
1417
1418       if (dest)
1419         {
1420           if (barrier_args_size [INSN_UID (dest)] < 0)
1421             {
1422               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1423               VEC_safe_push (rtx, heap, *next, dest);
1424             }
1425         }
1426     }
1427
1428   return cur_args_size;
1429 }
1430
1431 /* Walk the whole function and compute args_size on BARRIERs.  */
1432
1433 static void
1434 compute_barrier_args_size (void)
1435 {
1436   int max_uid = get_max_uid (), i;
1437   rtx insn;
1438   VEC (rtx, heap) *worklist, *next, *tmp;
1439
1440   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1441   for (i = 0; i < max_uid; i++)
1442     barrier_args_size[i] = -1;
1443
1444   worklist = VEC_alloc (rtx, heap, 20);
1445   next = VEC_alloc (rtx, heap, 20);
1446   insn = get_insns ();
1447   barrier_args_size[INSN_UID (insn)] = 0;
1448   VEC_quick_push (rtx, worklist, insn);
1449   for (;;)
1450     {
1451       while (!VEC_empty (rtx, worklist))
1452         {
1453           rtx prev, body, first_insn;
1454           HOST_WIDE_INT cur_args_size;
1455
1456           first_insn = insn = VEC_pop (rtx, worklist);
1457           cur_args_size = barrier_args_size[INSN_UID (insn)];
1458           prev = prev_nonnote_insn (insn);
1459           if (prev && BARRIER_P (prev))
1460             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1461
1462           for (; insn; insn = NEXT_INSN (insn))
1463             {
1464               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1465                 continue;
1466               if (BARRIER_P (insn))
1467                 break;
1468
1469               if (LABEL_P (insn))
1470                 {
1471                   if (insn == first_insn)
1472                     continue;
1473                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1474                     {
1475                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1476                       continue;
1477                     }
1478                   else
1479                     {
1480                       /* The insns starting with this label have been
1481                          already scanned or are in the worklist.  */
1482                       break;
1483                     }
1484                 }
1485
1486               body = PATTERN (insn);
1487               if (GET_CODE (body) == SEQUENCE)
1488                 {
1489                   HOST_WIDE_INT dest_args_size = cur_args_size;
1490                   for (i = 1; i < XVECLEN (body, 0); i++)
1491                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1492                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1493                       dest_args_size
1494                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1495                                                        dest_args_size, &next);
1496                     else
1497                       cur_args_size
1498                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1499                                                        cur_args_size, &next);
1500
1501                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1502                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1503                                                  dest_args_size, &next);
1504                   else
1505                     cur_args_size
1506                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1507                                                      cur_args_size, &next);
1508                 }
1509               else
1510                 cur_args_size
1511                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1512             }
1513         }
1514
1515       if (VEC_empty (rtx, next))
1516         break;
1517
1518       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1519       tmp = next;
1520       next = worklist;
1521       worklist = tmp;
1522       VEC_truncate (rtx, next, 0);
1523     }
1524
1525   VEC_free (rtx, heap, worklist);
1526   VEC_free (rtx, heap, next);
1527 }
1528
1529
1530 /* Check INSN to see if it looks like a push or a stack adjustment, and
1531    make a note of it if it does.  EH uses this information to find out how
1532    much extra space it needs to pop off the stack.  */
1533
1534 static void
1535 dwarf2out_stack_adjust (rtx insn, bool after_p)
1536 {
1537   HOST_WIDE_INT offset;
1538   const char *label;
1539   int i;
1540
1541   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1542      with this function.  Proper support would require all frame-related
1543      insns to be marked, and to be able to handle saving state around
1544      epilogues textually in the middle of the function.  */
1545   if (prologue_epilogue_contains (insn))
1546     return;
1547
1548   /* If INSN is an instruction from target of an annulled branch, the
1549      effects are for the target only and so current argument size
1550      shouldn't change at all.  */
1551   if (final_sequence
1552       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1553       && INSN_FROM_TARGET_P (insn))
1554     return;
1555
1556   /* If only calls can throw, and we have a frame pointer,
1557      save up adjustments until we see the CALL_INSN.  */
1558   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1559     {
1560       if (CALL_P (insn) && !after_p)
1561         {
1562           /* Extract the size of the args from the CALL rtx itself.  */
1563           insn = PATTERN (insn);
1564           if (GET_CODE (insn) == PARALLEL)
1565             insn = XVECEXP (insn, 0, 0);
1566           if (GET_CODE (insn) == SET)
1567             insn = SET_SRC (insn);
1568           gcc_assert (GET_CODE (insn) == CALL);
1569           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1570         }
1571       return;
1572     }
1573
1574   if (CALL_P (insn) && !after_p)
1575     {
1576       if (!flag_asynchronous_unwind_tables)
1577         dwarf2out_args_size ("", args_size);
1578       return;
1579     }
1580   else if (BARRIER_P (insn))
1581     {
1582       /* Don't call compute_barrier_args_size () if the only
1583          BARRIER is at the end of function.  */
1584       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1585         compute_barrier_args_size ();
1586       if (barrier_args_size == NULL)
1587         offset = 0;
1588       else
1589         {
1590           offset = barrier_args_size[INSN_UID (insn)];
1591           if (offset < 0)
1592             offset = 0;
1593         }
1594
1595       offset -= args_size;
1596 #ifndef STACK_GROWS_DOWNWARD
1597       offset = -offset;
1598 #endif
1599     }
1600   else if (GET_CODE (PATTERN (insn)) == SET)
1601     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1602   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1603            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1604     {
1605       /* There may be stack adjustments inside compound insns.  Search
1606          for them.  */
1607       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1608         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1609           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1610                                          args_size, offset);
1611     }
1612   else
1613     return;
1614
1615   if (offset == 0)
1616     return;
1617
1618   label = dwarf2out_cfi_label (false);
1619   dwarf2out_args_size_adjust (offset, label);
1620 }
1621
1622 /* Adjust args_size based on stack adjustment OFFSET.  */
1623
1624 static void
1625 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1626 {
1627   if (cfa.reg == STACK_POINTER_REGNUM)
1628     cfa.offset += offset;
1629
1630   if (cfa_store.reg == STACK_POINTER_REGNUM)
1631     cfa_store.offset += offset;
1632
1633 #ifndef STACK_GROWS_DOWNWARD
1634   offset = -offset;
1635 #endif
1636
1637   args_size += offset;
1638   if (args_size < 0)
1639     args_size = 0;
1640
1641   def_cfa_1 (label, &cfa);
1642   if (flag_asynchronous_unwind_tables)
1643     dwarf2out_args_size (label, args_size);
1644 }
1645
1646 #endif
1647
1648 /* We delay emitting a register save until either (a) we reach the end
1649    of the prologue or (b) the register is clobbered.  This clusters
1650    register saves so that there are fewer pc advances.  */
1651
1652 struct GTY(()) queued_reg_save {
1653   struct queued_reg_save *next;
1654   rtx reg;
1655   HOST_WIDE_INT cfa_offset;
1656   rtx saved_reg;
1657 };
1658
1659 static GTY(()) struct queued_reg_save *queued_reg_saves;
1660
1661 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1662 struct GTY(()) reg_saved_in_data {
1663   rtx orig_reg;
1664   rtx saved_in_reg;
1665 };
1666
1667 /* A list of registers saved in other registers.
1668    The list intentionally has a small maximum capacity of 4; if your
1669    port needs more than that, you might consider implementing a
1670    more efficient data structure.  */
1671 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1672 static GTY(()) size_t num_regs_saved_in_regs;
1673
1674 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1675 static const char *last_reg_save_label;
1676
1677 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1678    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1679
1680 static void
1681 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1682 {
1683   struct queued_reg_save *q;
1684
1685   /* Duplicates waste space, but it's also necessary to remove them
1686      for correctness, since the queue gets output in reverse
1687      order.  */
1688   for (q = queued_reg_saves; q != NULL; q = q->next)
1689     if (REGNO (q->reg) == REGNO (reg))
1690       break;
1691
1692   if (q == NULL)
1693     {
1694       q = GGC_NEW (struct queued_reg_save);
1695       q->next = queued_reg_saves;
1696       queued_reg_saves = q;
1697     }
1698
1699   q->reg = reg;
1700   q->cfa_offset = offset;
1701   q->saved_reg = sreg;
1702
1703   last_reg_save_label = label;
1704 }
1705
1706 /* Output all the entries in QUEUED_REG_SAVES.  */
1707
1708 static void
1709 flush_queued_reg_saves (void)
1710 {
1711   struct queued_reg_save *q;
1712
1713   for (q = queued_reg_saves; q; q = q->next)
1714     {
1715       size_t i;
1716       unsigned int reg, sreg;
1717
1718       for (i = 0; i < num_regs_saved_in_regs; i++)
1719         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1720           break;
1721       if (q->saved_reg && i == num_regs_saved_in_regs)
1722         {
1723           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1724           num_regs_saved_in_regs++;
1725         }
1726       if (i != num_regs_saved_in_regs)
1727         {
1728           regs_saved_in_regs[i].orig_reg = q->reg;
1729           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1730         }
1731
1732       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1733       if (q->saved_reg)
1734         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1735       else
1736         sreg = INVALID_REGNUM;
1737       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1738     }
1739
1740   queued_reg_saves = NULL;
1741   last_reg_save_label = NULL;
1742 }
1743
1744 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1745    location for?  Or, does it clobber a register which we've previously
1746    said that some other register is saved in, and for which we now
1747    have a new location for?  */
1748
1749 static bool
1750 clobbers_queued_reg_save (const_rtx insn)
1751 {
1752   struct queued_reg_save *q;
1753
1754   for (q = queued_reg_saves; q; q = q->next)
1755     {
1756       size_t i;
1757       if (modified_in_p (q->reg, insn))
1758         return true;
1759       for (i = 0; i < num_regs_saved_in_regs; i++)
1760         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1761             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1762           return true;
1763     }
1764
1765   return false;
1766 }
1767
1768 /* Entry point for saving the first register into the second.  */
1769
1770 void
1771 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1772 {
1773   size_t i;
1774   unsigned int regno, sregno;
1775
1776   for (i = 0; i < num_regs_saved_in_regs; i++)
1777     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1778       break;
1779   if (i == num_regs_saved_in_regs)
1780     {
1781       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1782       num_regs_saved_in_regs++;
1783     }
1784   regs_saved_in_regs[i].orig_reg = reg;
1785   regs_saved_in_regs[i].saved_in_reg = sreg;
1786
1787   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1788   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1789   reg_save (label, regno, sregno, 0);
1790 }
1791
1792 /* What register, if any, is currently saved in REG?  */
1793
1794 static rtx
1795 reg_saved_in (rtx reg)
1796 {
1797   unsigned int regn = REGNO (reg);
1798   size_t i;
1799   struct queued_reg_save *q;
1800
1801   for (q = queued_reg_saves; q; q = q->next)
1802     if (q->saved_reg && regn == REGNO (q->saved_reg))
1803       return q->reg;
1804
1805   for (i = 0; i < num_regs_saved_in_regs; i++)
1806     if (regs_saved_in_regs[i].saved_in_reg
1807         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1808       return regs_saved_in_regs[i].orig_reg;
1809
1810   return NULL_RTX;
1811 }
1812
1813
1814 /* A temporary register holding an integral value used in adjusting SP
1815    or setting up the store_reg.  The "offset" field holds the integer
1816    value, not an offset.  */
1817 static dw_cfa_location cfa_temp;
1818
1819 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1820
1821 static void
1822 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1823 {
1824   memset (&cfa, 0, sizeof (cfa));
1825
1826   switch (GET_CODE (pat))
1827     {
1828     case PLUS:
1829       cfa.reg = REGNO (XEXP (pat, 0));
1830       cfa.offset = INTVAL (XEXP (pat, 1));
1831       break;
1832
1833     case REG:
1834       cfa.reg = REGNO (pat);
1835       break;
1836
1837     default:
1838       /* Recurse and define an expression.  */
1839       gcc_unreachable ();
1840     }
1841
1842   def_cfa_1 (label, &cfa);
1843 }
1844
1845 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1846
1847 static void
1848 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1849 {
1850   rtx src, dest;
1851
1852   gcc_assert (GET_CODE (pat) == SET);
1853   dest = XEXP (pat, 0);
1854   src = XEXP (pat, 1);
1855
1856   switch (GET_CODE (src))
1857     {
1858     case PLUS:
1859       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1860       cfa.offset -= INTVAL (XEXP (src, 1));
1861       break;
1862
1863     case REG:
1864         break;
1865
1866     default:
1867         gcc_unreachable ();
1868     }
1869
1870   cfa.reg = REGNO (dest);
1871   gcc_assert (cfa.indirect == 0);
1872
1873   def_cfa_1 (label, &cfa);
1874 }
1875
1876 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1877
1878 static void
1879 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1880 {
1881   HOST_WIDE_INT offset;
1882   rtx src, addr, span;
1883
1884   src = XEXP (set, 1);
1885   addr = XEXP (set, 0);
1886   gcc_assert (MEM_P (addr));
1887   addr = XEXP (addr, 0);
1888   
1889   /* As documented, only consider extremely simple addresses.  */
1890   switch (GET_CODE (addr))
1891     {
1892     case REG:
1893       gcc_assert (REGNO (addr) == cfa.reg);
1894       offset = -cfa.offset;
1895       break;
1896     case PLUS:
1897       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1898       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1899       break;
1900     default:
1901       gcc_unreachable ();
1902     }
1903
1904   span = targetm.dwarf_register_span (src);
1905
1906   /* ??? We'd like to use queue_reg_save, but we need to come up with
1907      a different flushing heuristic for epilogues.  */
1908   if (!span)
1909     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1910   else
1911     {
1912       /* We have a PARALLEL describing where the contents of SRC live.
1913          Queue register saves for each piece of the PARALLEL.  */
1914       int par_index;
1915       int limit;
1916       HOST_WIDE_INT span_offset = offset;
1917
1918       gcc_assert (GET_CODE (span) == PARALLEL);
1919
1920       limit = XVECLEN (span, 0);
1921       for (par_index = 0; par_index < limit; par_index++)
1922         {
1923           rtx elem = XVECEXP (span, 0, par_index);
1924
1925           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1926                     INVALID_REGNUM, span_offset);
1927           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1928         }
1929     }
1930 }
1931
1932 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1933
1934 static void
1935 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1936 {
1937   rtx src, dest;
1938   unsigned sregno, dregno;
1939
1940   src = XEXP (set, 1);
1941   dest = XEXP (set, 0);
1942
1943   if (src == pc_rtx)
1944     sregno = DWARF_FRAME_RETURN_COLUMN;
1945   else
1946     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1947
1948   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1949
1950   /* ??? We'd like to use queue_reg_save, but we need to come up with
1951      a different flushing heuristic for epilogues.  */
1952   reg_save (label, sregno, dregno, 0);
1953 }
1954
1955 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1956
1957 static void
1958 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1959 {
1960   dw_cfi_ref cfi = new_cfi ();
1961   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1962
1963   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1964   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1965
1966   add_fde_cfi (label, cfi);
1967 }
1968
1969 /* Record call frame debugging information for an expression EXPR,
1970    which either sets SP or FP (adjusting how we calculate the frame
1971    address) or saves a register to the stack or another register.
1972    LABEL indicates the address of EXPR.
1973
1974    This function encodes a state machine mapping rtxes to actions on
1975    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1976    users need not read the source code.
1977
1978   The High-Level Picture
1979
1980   Changes in the register we use to calculate the CFA: Currently we
1981   assume that if you copy the CFA register into another register, we
1982   should take the other one as the new CFA register; this seems to
1983   work pretty well.  If it's wrong for some target, it's simple
1984   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1985
1986   Changes in the register we use for saving registers to the stack:
1987   This is usually SP, but not always.  Again, we deduce that if you
1988   copy SP into another register (and SP is not the CFA register),
1989   then the new register is the one we will be using for register
1990   saves.  This also seems to work.
1991
1992   Register saves: There's not much guesswork about this one; if
1993   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1994   register save, and the register used to calculate the destination
1995   had better be the one we think we're using for this purpose.
1996   It's also assumed that a copy from a call-saved register to another
1997   register is saving that register if RTX_FRAME_RELATED_P is set on
1998   that instruction.  If the copy is from a call-saved register to
1999   the *same* register, that means that the register is now the same
2000   value as in the caller.
2001
2002   Except: If the register being saved is the CFA register, and the
2003   offset is nonzero, we are saving the CFA, so we assume we have to
2004   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2005   the intent is to save the value of SP from the previous frame.
2006
2007   In addition, if a register has previously been saved to a different
2008   register,
2009
2010   Invariants / Summaries of Rules
2011
2012   cfa          current rule for calculating the CFA.  It usually
2013                consists of a register and an offset.
2014   cfa_store    register used by prologue code to save things to the stack
2015                cfa_store.offset is the offset from the value of
2016                cfa_store.reg to the actual CFA
2017   cfa_temp     register holding an integral value.  cfa_temp.offset
2018                stores the value, which will be used to adjust the
2019                stack pointer.  cfa_temp is also used like cfa_store,
2020                to track stores to the stack via fp or a temp reg.
2021
2022   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2023                with cfa.reg as the first operand changes the cfa.reg and its
2024                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2025                cfa_temp.offset.
2026
2027   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2028                expression yielding a constant.  This sets cfa_temp.reg
2029                and cfa_temp.offset.
2030
2031   Rule 5:      Create a new register cfa_store used to save items to the
2032                stack.
2033
2034   Rules 10-14: Save a register to the stack.  Define offset as the
2035                difference of the original location and cfa_store's
2036                location (or cfa_temp's location if cfa_temp is used).
2037
2038   Rules 16-20: If AND operation happens on sp in prologue, we assume
2039                stack is realigned.  We will use a group of DW_OP_XXX
2040                expressions to represent the location of the stored
2041                register instead of CFA+offset.
2042
2043   The Rules
2044
2045   "{a,b}" indicates a choice of a xor b.
2046   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2047
2048   Rule 1:
2049   (set <reg1> <reg2>:cfa.reg)
2050   effects: cfa.reg = <reg1>
2051            cfa.offset unchanged
2052            cfa_temp.reg = <reg1>
2053            cfa_temp.offset = cfa.offset
2054
2055   Rule 2:
2056   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2057                               {<const_int>,<reg>:cfa_temp.reg}))
2058   effects: cfa.reg = sp if fp used
2059            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2060            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2061              if cfa_store.reg==sp
2062
2063   Rule 3:
2064   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2065   effects: cfa.reg = fp
2066            cfa_offset += +/- <const_int>
2067
2068   Rule 4:
2069   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2070   constraints: <reg1> != fp
2071                <reg1> != sp
2072   effects: cfa.reg = <reg1>
2073            cfa_temp.reg = <reg1>
2074            cfa_temp.offset = cfa.offset
2075
2076   Rule 5:
2077   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2078   constraints: <reg1> != fp
2079                <reg1> != sp
2080   effects: cfa_store.reg = <reg1>
2081            cfa_store.offset = cfa.offset - cfa_temp.offset
2082
2083   Rule 6:
2084   (set <reg> <const_int>)
2085   effects: cfa_temp.reg = <reg>
2086            cfa_temp.offset = <const_int>
2087
2088   Rule 7:
2089   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2090   effects: cfa_temp.reg = <reg1>
2091            cfa_temp.offset |= <const_int>
2092
2093   Rule 8:
2094   (set <reg> (high <exp>))
2095   effects: none
2096
2097   Rule 9:
2098   (set <reg> (lo_sum <exp> <const_int>))
2099   effects: cfa_temp.reg = <reg>
2100            cfa_temp.offset = <const_int>
2101
2102   Rule 10:
2103   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2104   effects: cfa_store.offset -= <const_int>
2105            cfa.offset = cfa_store.offset if cfa.reg == sp
2106            cfa.reg = sp
2107            cfa.base_offset = -cfa_store.offset
2108
2109   Rule 11:
2110   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2111   effects: cfa_store.offset += -/+ mode_size(mem)
2112            cfa.offset = cfa_store.offset if cfa.reg == sp
2113            cfa.reg = sp
2114            cfa.base_offset = -cfa_store.offset
2115
2116   Rule 12:
2117   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2118
2119        <reg2>)
2120   effects: cfa.reg = <reg1>
2121            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2122
2123   Rule 13:
2124   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2125   effects: cfa.reg = <reg1>
2126            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2127
2128   Rule 14:
2129   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2130   effects: cfa.reg = <reg1>
2131            cfa.base_offset = -cfa_temp.offset
2132            cfa_temp.offset -= mode_size(mem)
2133
2134   Rule 15:
2135   (set <reg> {unspec, unspec_volatile})
2136   effects: target-dependent
2137
2138   Rule 16:
2139   (set sp (and: sp <const_int>))
2140   constraints: cfa_store.reg == sp
2141   effects: current_fde.stack_realign = 1
2142            cfa_store.offset = 0
2143            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2144
2145   Rule 17:
2146   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2147   effects: cfa_store.offset += -/+ mode_size(mem)
2148
2149   Rule 18:
2150   (set (mem ({pre_inc, pre_dec} sp)) fp)
2151   constraints: fde->stack_realign == 1
2152   effects: cfa_store.offset = 0
2153            cfa.reg != HARD_FRAME_POINTER_REGNUM
2154
2155   Rule 19:
2156   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2157   constraints: fde->stack_realign == 1
2158                && cfa.offset == 0
2159                && cfa.indirect == 0
2160                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2161   effects: Use DW_CFA_def_cfa_expression to define cfa
2162            cfa.reg == fde->drap_reg
2163
2164   Rule 20:
2165   (set reg fde->drap_reg)
2166   constraints: fde->vdrap_reg == INVALID_REGNUM
2167   effects: fde->vdrap_reg = reg.
2168   (set mem fde->drap_reg)
2169   constraints: fde->drap_reg_saved == 1
2170   effects: none.  */
2171
2172 static void
2173 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2174 {
2175   rtx src, dest, span;
2176   HOST_WIDE_INT offset;
2177   dw_fde_ref fde;
2178
2179   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2180      the PARALLEL independently. The first element is always processed if
2181      it is a SET. This is for backward compatibility.   Other elements
2182      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2183      flag is set in them.  */
2184   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2185     {
2186       int par_index;
2187       int limit = XVECLEN (expr, 0);
2188       rtx elem;
2189
2190       /* PARALLELs have strict read-modify-write semantics, so we
2191          ought to evaluate every rvalue before changing any lvalue.
2192          It's cumbersome to do that in general, but there's an
2193          easy approximation that is enough for all current users:
2194          handle register saves before register assignments.  */
2195       if (GET_CODE (expr) == PARALLEL)
2196         for (par_index = 0; par_index < limit; par_index++)
2197           {
2198             elem = XVECEXP (expr, 0, par_index);
2199             if (GET_CODE (elem) == SET
2200                 && MEM_P (SET_DEST (elem))
2201                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2202               dwarf2out_frame_debug_expr (elem, label);
2203           }
2204
2205       for (par_index = 0; par_index < limit; par_index++)
2206         {
2207           elem = XVECEXP (expr, 0, par_index);
2208           if (GET_CODE (elem) == SET
2209               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2210               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2211             dwarf2out_frame_debug_expr (elem, label);
2212           else if (GET_CODE (elem) == SET
2213                    && par_index != 0
2214                    && !RTX_FRAME_RELATED_P (elem))
2215             {
2216               /* Stack adjustment combining might combine some post-prologue
2217                  stack adjustment into a prologue stack adjustment.  */
2218               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2219
2220               if (offset != 0)
2221                 dwarf2out_args_size_adjust (offset, label);
2222             }
2223         }
2224       return;
2225     }
2226
2227   gcc_assert (GET_CODE (expr) == SET);
2228
2229   src = SET_SRC (expr);
2230   dest = SET_DEST (expr);
2231
2232   if (REG_P (src))
2233     {
2234       rtx rsi = reg_saved_in (src);
2235       if (rsi)
2236         src = rsi;
2237     }
2238
2239   fde = current_fde ();
2240
2241   if (REG_P (src)
2242       && fde
2243       && fde->drap_reg == REGNO (src)
2244       && (fde->drap_reg_saved
2245           || REG_P (dest)))
2246     {
2247       /* Rule 20 */
2248       /* If we are saving dynamic realign argument pointer to a
2249          register, the destination is virtual dynamic realign
2250          argument pointer.  It may be used to access argument.  */
2251       if (REG_P (dest))
2252         {
2253           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2254           fde->vdrap_reg = REGNO (dest);
2255         }
2256       return;
2257     }
2258
2259   switch (GET_CODE (dest))
2260     {
2261     case REG:
2262       switch (GET_CODE (src))
2263         {
2264           /* Setting FP from SP.  */
2265         case REG:
2266           if (cfa.reg == (unsigned) REGNO (src))
2267             {
2268               /* Rule 1 */
2269               /* Update the CFA rule wrt SP or FP.  Make sure src is
2270                  relative to the current CFA register.
2271
2272                  We used to require that dest be either SP or FP, but the
2273                  ARM copies SP to a temporary register, and from there to
2274                  FP.  So we just rely on the backends to only set
2275                  RTX_FRAME_RELATED_P on appropriate insns.  */
2276               cfa.reg = REGNO (dest);
2277               cfa_temp.reg = cfa.reg;
2278               cfa_temp.offset = cfa.offset;
2279             }
2280           else
2281             {
2282               /* Saving a register in a register.  */
2283               gcc_assert (!fixed_regs [REGNO (dest)]
2284                           /* For the SPARC and its register window.  */
2285                           || (DWARF_FRAME_REGNUM (REGNO (src))
2286                               == DWARF_FRAME_RETURN_COLUMN));
2287
2288               /* After stack is aligned, we can only save SP in FP
2289                  if drap register is used.  In this case, we have
2290                  to restore stack pointer with the CFA value and we
2291                  don't generate this DWARF information.  */
2292               if (fde
2293                   && fde->stack_realign
2294                   && REGNO (src) == STACK_POINTER_REGNUM)
2295                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2296                             && fde->drap_reg != INVALID_REGNUM
2297                             && cfa.reg != REGNO (src));
2298               else
2299                 queue_reg_save (label, src, dest, 0);
2300             }
2301           break;
2302
2303         case PLUS:
2304         case MINUS:
2305         case LO_SUM:
2306           if (dest == stack_pointer_rtx)
2307             {
2308               /* Rule 2 */
2309               /* Adjusting SP.  */
2310               switch (GET_CODE (XEXP (src, 1)))
2311                 {
2312                 case CONST_INT:
2313                   offset = INTVAL (XEXP (src, 1));
2314                   break;
2315                 case REG:
2316                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2317                               == cfa_temp.reg);
2318                   offset = cfa_temp.offset;
2319                   break;
2320                 default:
2321                   gcc_unreachable ();
2322                 }
2323
2324               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2325                 {
2326                   /* Restoring SP from FP in the epilogue.  */
2327                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2328                   cfa.reg = STACK_POINTER_REGNUM;
2329                 }
2330               else if (GET_CODE (src) == LO_SUM)
2331                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2332                 ;
2333               else
2334                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2335
2336               if (GET_CODE (src) != MINUS)
2337                 offset = -offset;
2338               if (cfa.reg == STACK_POINTER_REGNUM)
2339                 cfa.offset += offset;
2340               if (cfa_store.reg == STACK_POINTER_REGNUM)
2341                 cfa_store.offset += offset;
2342             }
2343           else if (dest == hard_frame_pointer_rtx)
2344             {
2345               /* Rule 3 */
2346               /* Either setting the FP from an offset of the SP,
2347                  or adjusting the FP */
2348               gcc_assert (frame_pointer_needed);
2349
2350               gcc_assert (REG_P (XEXP (src, 0))
2351                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2352                           && CONST_INT_P (XEXP (src, 1)));
2353               offset = INTVAL (XEXP (src, 1));
2354               if (GET_CODE (src) != MINUS)
2355                 offset = -offset;
2356               cfa.offset += offset;
2357               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2358             }
2359           else
2360             {
2361               gcc_assert (GET_CODE (src) != MINUS);
2362
2363               /* Rule 4 */
2364               if (REG_P (XEXP (src, 0))
2365                   && REGNO (XEXP (src, 0)) == cfa.reg
2366                   && CONST_INT_P (XEXP (src, 1)))
2367                 {
2368                   /* Setting a temporary CFA register that will be copied
2369                      into the FP later on.  */
2370                   offset = - INTVAL (XEXP (src, 1));
2371                   cfa.offset += offset;
2372                   cfa.reg = REGNO (dest);
2373                   /* Or used to save regs to the stack.  */
2374                   cfa_temp.reg = cfa.reg;
2375                   cfa_temp.offset = cfa.offset;
2376                 }
2377
2378               /* Rule 5 */
2379               else if (REG_P (XEXP (src, 0))
2380                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2381                        && XEXP (src, 1) == stack_pointer_rtx)
2382                 {
2383                   /* Setting a scratch register that we will use instead
2384                      of SP for saving registers to the stack.  */
2385                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2386                   cfa_store.reg = REGNO (dest);
2387                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2388                 }
2389
2390               /* Rule 9 */
2391               else if (GET_CODE (src) == LO_SUM
2392                        && CONST_INT_P (XEXP (src, 1)))
2393                 {
2394                   cfa_temp.reg = REGNO (dest);
2395                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2396                 }
2397               else
2398                 gcc_unreachable ();
2399             }
2400           break;
2401
2402           /* Rule 6 */
2403         case CONST_INT:
2404           cfa_temp.reg = REGNO (dest);
2405           cfa_temp.offset = INTVAL (src);
2406           break;
2407
2408           /* Rule 7 */
2409         case IOR:
2410           gcc_assert (REG_P (XEXP (src, 0))
2411                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2412                       && CONST_INT_P (XEXP (src, 1)));
2413
2414           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2415             cfa_temp.reg = REGNO (dest);
2416           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2417           break;
2418
2419           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2420              which will fill in all of the bits.  */
2421           /* Rule 8 */
2422         case HIGH:
2423           break;
2424
2425           /* Rule 15 */
2426         case UNSPEC:
2427         case UNSPEC_VOLATILE:
2428           gcc_assert (targetm.dwarf_handle_frame_unspec);
2429           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2430           return;
2431
2432           /* Rule 16 */
2433         case AND:
2434           /* If this AND operation happens on stack pointer in prologue,
2435              we assume the stack is realigned and we extract the
2436              alignment.  */
2437           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2438             {
2439               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2440               fde->stack_realign = 1;
2441               fde->stack_realignment = INTVAL (XEXP (src, 1));
2442               cfa_store.offset = 0;
2443
2444               if (cfa.reg != STACK_POINTER_REGNUM
2445                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2446                 fde->drap_reg = cfa.reg;
2447             }
2448           return;
2449
2450         default:
2451           gcc_unreachable ();
2452         }
2453
2454       def_cfa_1 (label, &cfa);
2455       break;
2456
2457     case MEM:
2458
2459       /* Saving a register to the stack.  Make sure dest is relative to the
2460          CFA register.  */
2461       switch (GET_CODE (XEXP (dest, 0)))
2462         {
2463           /* Rule 10 */
2464           /* With a push.  */
2465         case PRE_MODIFY:
2466           /* We can't handle variable size modifications.  */
2467           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2468                       == CONST_INT);
2469           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2470
2471           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2472                       && cfa_store.reg == STACK_POINTER_REGNUM);
2473
2474           cfa_store.offset += offset;
2475           if (cfa.reg == STACK_POINTER_REGNUM)
2476             cfa.offset = cfa_store.offset;
2477
2478           offset = -cfa_store.offset;
2479           break;
2480
2481           /* Rule 11 */
2482         case PRE_INC:
2483         case PRE_DEC:
2484           offset = GET_MODE_SIZE (GET_MODE (dest));
2485           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2486             offset = -offset;
2487
2488           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2489                        == STACK_POINTER_REGNUM)
2490                       && cfa_store.reg == STACK_POINTER_REGNUM);
2491
2492           cfa_store.offset += offset;
2493
2494           /* Rule 18: If stack is aligned, we will use FP as a
2495              reference to represent the address of the stored
2496              regiser.  */
2497           if (fde
2498               && fde->stack_realign
2499               && src == hard_frame_pointer_rtx)
2500             {
2501               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2502               cfa_store.offset = 0;
2503             }
2504
2505           if (cfa.reg == STACK_POINTER_REGNUM)
2506             cfa.offset = cfa_store.offset;
2507
2508           offset = -cfa_store.offset;
2509           break;
2510
2511           /* Rule 12 */
2512           /* With an offset.  */
2513         case PLUS:
2514         case MINUS:
2515         case LO_SUM:
2516           {
2517             int regno;
2518
2519             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2520                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2521             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2522             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2523               offset = -offset;
2524
2525             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2526
2527             if (cfa_store.reg == (unsigned) regno)
2528               offset -= cfa_store.offset;
2529             else
2530               {
2531                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2532                 offset -= cfa_temp.offset;
2533               }
2534           }
2535           break;
2536
2537           /* Rule 13 */
2538           /* Without an offset.  */
2539         case REG:
2540           {
2541             int regno = REGNO (XEXP (dest, 0));
2542
2543             if (cfa_store.reg == (unsigned) regno)
2544               offset = -cfa_store.offset;
2545             else
2546               {
2547                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2548                 offset = -cfa_temp.offset;
2549               }
2550           }
2551           break;
2552
2553           /* Rule 14 */
2554         case POST_INC:
2555           gcc_assert (cfa_temp.reg
2556                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2557           offset = -cfa_temp.offset;
2558           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2559           break;
2560
2561         default:
2562           gcc_unreachable ();
2563         }
2564
2565         /* Rule 17 */
2566         /* If the source operand of this MEM operation is not a
2567            register, basically the source is return address.  Here
2568            we only care how much stack grew and we don't save it.  */
2569       if (!REG_P (src))
2570         break;
2571
2572       if (REGNO (src) != STACK_POINTER_REGNUM
2573           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2574           && (unsigned) REGNO (src) == cfa.reg)
2575         {
2576           /* We're storing the current CFA reg into the stack.  */
2577
2578           if (cfa.offset == 0)
2579             {
2580               /* Rule 19 */
2581               /* If stack is aligned, putting CFA reg into stack means
2582                  we can no longer use reg + offset to represent CFA.
2583                  Here we use DW_CFA_def_cfa_expression instead.  The
2584                  result of this expression equals to the original CFA
2585                  value.  */
2586               if (fde
2587                   && fde->stack_realign
2588                   && cfa.indirect == 0
2589                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2590                 {
2591                   dw_cfa_location cfa_exp;
2592
2593                   gcc_assert (fde->drap_reg == cfa.reg);
2594
2595                   cfa_exp.indirect = 1;
2596                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2597                   cfa_exp.base_offset = offset;
2598                   cfa_exp.offset = 0;
2599
2600                   fde->drap_reg_saved = 1;
2601
2602                   def_cfa_1 (label, &cfa_exp);
2603                   break;
2604                 }
2605
2606               /* If the source register is exactly the CFA, assume
2607                  we're saving SP like any other register; this happens
2608                  on the ARM.  */
2609               def_cfa_1 (label, &cfa);
2610               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2611               break;
2612             }
2613           else
2614             {
2615               /* Otherwise, we'll need to look in the stack to
2616                  calculate the CFA.  */
2617               rtx x = XEXP (dest, 0);
2618
2619               if (!REG_P (x))
2620                 x = XEXP (x, 0);
2621               gcc_assert (REG_P (x));
2622
2623               cfa.reg = REGNO (x);
2624               cfa.base_offset = offset;
2625               cfa.indirect = 1;
2626               def_cfa_1 (label, &cfa);
2627               break;
2628             }
2629         }
2630
2631       def_cfa_1 (label, &cfa);
2632       {
2633         span = targetm.dwarf_register_span (src);
2634
2635         if (!span)
2636           queue_reg_save (label, src, NULL_RTX, offset);
2637         else
2638           {
2639             /* We have a PARALLEL describing where the contents of SRC
2640                live.  Queue register saves for each piece of the
2641                PARALLEL.  */
2642             int par_index;
2643             int limit;
2644             HOST_WIDE_INT span_offset = offset;
2645
2646             gcc_assert (GET_CODE (span) == PARALLEL);
2647
2648             limit = XVECLEN (span, 0);
2649             for (par_index = 0; par_index < limit; par_index++)
2650               {
2651                 rtx elem = XVECEXP (span, 0, par_index);
2652
2653                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2654                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2655               }
2656           }
2657       }
2658       break;
2659
2660     default:
2661       gcc_unreachable ();
2662     }
2663 }
2664
2665 /* Record call frame debugging information for INSN, which either
2666    sets SP or FP (adjusting how we calculate the frame address) or saves a
2667    register to the stack.  If INSN is NULL_RTX, initialize our state.
2668
2669    If AFTER_P is false, we're being called before the insn is emitted,
2670    otherwise after.  Call instructions get invoked twice.  */
2671
2672 void
2673 dwarf2out_frame_debug (rtx insn, bool after_p)
2674 {
2675   const char *label;
2676   rtx note, n;
2677   bool handled_one = false;
2678
2679   if (insn == NULL_RTX)
2680     {
2681       size_t i;
2682
2683       /* Flush any queued register saves.  */
2684       flush_queued_reg_saves ();
2685
2686       /* Set up state for generating call frame debug info.  */
2687       lookup_cfa (&cfa);
2688       gcc_assert (cfa.reg
2689                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2690
2691       cfa.reg = STACK_POINTER_REGNUM;
2692       cfa_store = cfa;
2693       cfa_temp.reg = -1;
2694       cfa_temp.offset = 0;
2695
2696       for (i = 0; i < num_regs_saved_in_regs; i++)
2697         {
2698           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2699           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2700         }
2701       num_regs_saved_in_regs = 0;
2702
2703       if (barrier_args_size)
2704         {
2705           XDELETEVEC (barrier_args_size);
2706           barrier_args_size = NULL;
2707         }
2708       return;
2709     }
2710
2711   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2712     flush_queued_reg_saves ();
2713
2714   if (! RTX_FRAME_RELATED_P (insn))
2715     {
2716       if (!ACCUMULATE_OUTGOING_ARGS)
2717         dwarf2out_stack_adjust (insn, after_p);
2718       return;
2719     }
2720
2721   label = dwarf2out_cfi_label (false);
2722
2723   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2724     switch (REG_NOTE_KIND (note))
2725       {
2726       case REG_FRAME_RELATED_EXPR:
2727         insn = XEXP (note, 0);
2728         goto found;
2729
2730       case REG_CFA_DEF_CFA:
2731         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2732         handled_one = true;
2733         break;
2734
2735       case REG_CFA_ADJUST_CFA:
2736         n = XEXP (note, 0);
2737         if (n == NULL)
2738           {
2739             n = PATTERN (insn);
2740             if (GET_CODE (n) == PARALLEL)
2741               n = XVECEXP (n, 0, 0);
2742           }
2743         dwarf2out_frame_debug_adjust_cfa (n, label);
2744         handled_one = true;
2745         break;
2746
2747       case REG_CFA_OFFSET:
2748         n = XEXP (note, 0);
2749         if (n == NULL)
2750           n = single_set (insn);
2751         dwarf2out_frame_debug_cfa_offset (n, label);
2752         handled_one = true;
2753         break;
2754
2755       case REG_CFA_REGISTER:
2756         n = XEXP (note, 0);
2757         if (n == NULL)
2758           {
2759             n = PATTERN (insn);
2760             if (GET_CODE (n) == PARALLEL)
2761               n = XVECEXP (n, 0, 0);
2762           }
2763         dwarf2out_frame_debug_cfa_register (n, label);
2764         handled_one = true;
2765         break;
2766
2767       case REG_CFA_RESTORE:
2768         n = XEXP (note, 0);
2769         if (n == NULL)
2770           {
2771             n = PATTERN (insn);
2772             if (GET_CODE (n) == PARALLEL)
2773               n = XVECEXP (n, 0, 0);
2774             n = XEXP (n, 0);
2775           }
2776         dwarf2out_frame_debug_cfa_restore (n, label);
2777         handled_one = true;
2778         break;
2779
2780       default:
2781         break;
2782       }
2783   if (handled_one)
2784     return;
2785
2786   insn = PATTERN (insn);
2787  found:
2788   dwarf2out_frame_debug_expr (insn, label);
2789 }
2790
2791 /* Determine if we need to save and restore CFI information around this
2792    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2793    we do need to save/restore, then emit the save now, and insert a
2794    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2795
2796 void
2797 dwarf2out_begin_epilogue (rtx insn)
2798 {
2799   bool saw_frp = false;
2800   rtx i;
2801
2802   /* Scan forward to the return insn, noticing if there are possible
2803      frame related insns.  */
2804   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2805     {
2806       if (!INSN_P (i))
2807         continue;
2808
2809       /* Look for both regular and sibcalls to end the block.  */
2810       if (returnjump_p (i))
2811         break;
2812       if (CALL_P (i) && SIBLING_CALL_P (i))
2813         break;
2814
2815       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2816         {
2817           int idx;
2818           rtx seq = PATTERN (i);
2819
2820           if (returnjump_p (XVECEXP (seq, 0, 0)))
2821             break;
2822           if (CALL_P (XVECEXP (seq, 0, 0))
2823               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2824             break;
2825
2826           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2827             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2828               saw_frp = true;
2829         }
2830
2831       if (RTX_FRAME_RELATED_P (i))
2832         saw_frp = true;
2833     }
2834
2835   /* If the port doesn't emit epilogue unwind info, we don't need a
2836      save/restore pair.  */
2837   if (!saw_frp)
2838     return;
2839
2840   /* Otherwise, search forward to see if the return insn was the last
2841      basic block of the function.  If so, we don't need save/restore.  */
2842   gcc_assert (i != NULL);
2843   i = next_real_insn (i);
2844   if (i == NULL)
2845     return;
2846
2847   /* Insert the restore before that next real insn in the stream, and before
2848      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2849      properly nested.  This should be after any label or alignment.  This
2850      will be pushed into the CFI stream by the function below.  */
2851   while (1)
2852     {
2853       rtx p = PREV_INSN (i);
2854       if (!NOTE_P (p))
2855         break;
2856       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2857         break;
2858       i = p;
2859     }
2860   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2861
2862   emit_cfa_remember = true;
2863
2864   /* And emulate the state save.  */
2865   gcc_assert (!cfa_remember.in_use);
2866   cfa_remember = cfa;
2867   cfa_remember.in_use = 1;
2868 }
2869
2870 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2871
2872 void
2873 dwarf2out_frame_debug_restore_state (void)
2874 {
2875   dw_cfi_ref cfi = new_cfi (); 
2876   const char *label = dwarf2out_cfi_label (false);
2877
2878   cfi->dw_cfi_opc = DW_CFA_restore_state;
2879   add_fde_cfi (label, cfi);
2880
2881   gcc_assert (cfa_remember.in_use);
2882   cfa = cfa_remember;
2883   cfa_remember.in_use = 0;
2884 }
2885
2886 #endif
2887
2888 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2889 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2890  (enum dwarf_call_frame_info cfi);
2891
2892 static enum dw_cfi_oprnd_type
2893 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2894 {
2895   switch (cfi)
2896     {
2897     case DW_CFA_nop:
2898     case DW_CFA_GNU_window_save:
2899     case DW_CFA_remember_state:
2900     case DW_CFA_restore_state:
2901       return dw_cfi_oprnd_unused;
2902
2903     case DW_CFA_set_loc:
2904     case DW_CFA_advance_loc1:
2905     case DW_CFA_advance_loc2:
2906     case DW_CFA_advance_loc4:
2907     case DW_CFA_MIPS_advance_loc8:
2908       return dw_cfi_oprnd_addr;
2909
2910     case DW_CFA_offset:
2911     case DW_CFA_offset_extended:
2912     case DW_CFA_def_cfa:
2913     case DW_CFA_offset_extended_sf:
2914     case DW_CFA_def_cfa_sf:
2915     case DW_CFA_restore:
2916     case DW_CFA_restore_extended:
2917     case DW_CFA_undefined:
2918     case DW_CFA_same_value:
2919     case DW_CFA_def_cfa_register:
2920     case DW_CFA_register:
2921       return dw_cfi_oprnd_reg_num;
2922
2923     case DW_CFA_def_cfa_offset:
2924     case DW_CFA_GNU_args_size:
2925     case DW_CFA_def_cfa_offset_sf:
2926       return dw_cfi_oprnd_offset;
2927
2928     case DW_CFA_def_cfa_expression:
2929     case DW_CFA_expression:
2930       return dw_cfi_oprnd_loc;
2931
2932     default:
2933       gcc_unreachable ();
2934     }
2935 }
2936
2937 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2938 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2939  (enum dwarf_call_frame_info cfi);
2940
2941 static enum dw_cfi_oprnd_type
2942 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2943 {
2944   switch (cfi)
2945     {
2946     case DW_CFA_def_cfa:
2947     case DW_CFA_def_cfa_sf:
2948     case DW_CFA_offset:
2949     case DW_CFA_offset_extended_sf:
2950     case DW_CFA_offset_extended:
2951       return dw_cfi_oprnd_offset;
2952
2953     case DW_CFA_register:
2954       return dw_cfi_oprnd_reg_num;
2955
2956     default:
2957       return dw_cfi_oprnd_unused;
2958     }
2959 }
2960
2961 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2962
2963 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2964    switch to the data section instead, and write out a synthetic start label
2965    for collect2 the first time around.  */
2966
2967 static void
2968 switch_to_eh_frame_section (bool back)
2969 {
2970   tree label;
2971
2972 #ifdef EH_FRAME_SECTION_NAME
2973   if (eh_frame_section == 0)
2974     {
2975       int flags;
2976
2977       if (EH_TABLES_CAN_BE_READ_ONLY)
2978         {
2979           int fde_encoding;
2980           int per_encoding;
2981           int lsda_encoding;
2982
2983           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2984                                                        /*global=*/0);
2985           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2986                                                        /*global=*/1);
2987           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2988                                                         /*global=*/0);
2989           flags = ((! flag_pic
2990                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2991                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2992                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2993                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2994                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2995                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2996                    ? 0 : SECTION_WRITE);
2997         }
2998       else
2999         flags = SECTION_WRITE;
3000       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3001     }
3002 #endif
3003
3004   if (eh_frame_section)
3005     switch_to_section (eh_frame_section);
3006   else
3007     {
3008       /* We have no special eh_frame section.  Put the information in
3009          the data section and emit special labels to guide collect2.  */
3010       switch_to_section (data_section);
3011
3012       if (!back)
3013         {
3014           label = get_file_function_name ("F");
3015           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3016           targetm.asm_out.globalize_label (asm_out_file,
3017                                            IDENTIFIER_POINTER (label));
3018           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3019         }
3020     }
3021 }
3022
3023 /* Switch [BACK] to the eh or debug frame table section, depending on
3024    FOR_EH.  */
3025
3026 static void
3027 switch_to_frame_table_section (int for_eh, bool back)
3028 {
3029   if (for_eh)
3030     switch_to_eh_frame_section (back);
3031   else
3032     {
3033       if (!debug_frame_section)
3034         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3035                                            SECTION_DEBUG, NULL);
3036       switch_to_section (debug_frame_section);
3037     }
3038 }
3039
3040 /* Output a Call Frame Information opcode and its operand(s).  */
3041
3042 static void
3043 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3044 {
3045   unsigned long r;
3046   HOST_WIDE_INT off;
3047
3048   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3049     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3050                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3051                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3052                          ((unsigned HOST_WIDE_INT)
3053                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3054   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3055     {
3056       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3057       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3058                            "DW_CFA_offset, column 0x%lx", r);
3059       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3060       dw2_asm_output_data_uleb128 (off, NULL);
3061     }
3062   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3063     {
3064       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3065       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3066                            "DW_CFA_restore, column 0x%lx", r);
3067     }
3068   else
3069     {
3070       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3071                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3072
3073       switch (cfi->dw_cfi_opc)
3074         {
3075         case DW_CFA_set_loc:
3076           if (for_eh)
3077             dw2_asm_output_encoded_addr_rtx (
3078                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3079                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3080                 false, NULL);
3081           else
3082             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3083                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3084           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3085           break;
3086
3087         case DW_CFA_advance_loc1:
3088           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3089                                 fde->dw_fde_current_label, NULL);
3090           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3091           break;
3092
3093         case DW_CFA_advance_loc2:
3094           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3095                                 fde->dw_fde_current_label, NULL);
3096           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3097           break;
3098
3099         case DW_CFA_advance_loc4:
3100           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3101                                 fde->dw_fde_current_label, NULL);
3102           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3103           break;
3104
3105         case DW_CFA_MIPS_advance_loc8:
3106           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3107                                 fde->dw_fde_current_label, NULL);
3108           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3109           break;
3110
3111         case DW_CFA_offset_extended:
3112           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3113           dw2_asm_output_data_uleb128 (r, NULL);
3114           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3115           dw2_asm_output_data_uleb128 (off, NULL);
3116           break;
3117
3118         case DW_CFA_def_cfa:
3119           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3120           dw2_asm_output_data_uleb128 (r, NULL);
3121           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3122           break;
3123
3124         case DW_CFA_offset_extended_sf:
3125           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3126           dw2_asm_output_data_uleb128 (r, NULL);
3127           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3128           dw2_asm_output_data_sleb128 (off, NULL);
3129           break;
3130
3131         case DW_CFA_def_cfa_sf:
3132           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3133           dw2_asm_output_data_uleb128 (r, NULL);
3134           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3135           dw2_asm_output_data_sleb128 (off, NULL);
3136           break;
3137
3138         case DW_CFA_restore_extended:
3139         case DW_CFA_undefined:
3140         case DW_CFA_same_value:
3141         case DW_CFA_def_cfa_register:
3142           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3143           dw2_asm_output_data_uleb128 (r, NULL);
3144           break;
3145
3146         case DW_CFA_register:
3147           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3148           dw2_asm_output_data_uleb128 (r, NULL);
3149           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3150           dw2_asm_output_data_uleb128 (r, NULL);
3151           break;
3152
3153         case DW_CFA_def_cfa_offset:
3154         case DW_CFA_GNU_args_size:
3155           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3156           break;
3157
3158         case DW_CFA_def_cfa_offset_sf:
3159           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3160           dw2_asm_output_data_sleb128 (off, NULL);
3161           break;
3162
3163         case DW_CFA_GNU_window_save:
3164           break;
3165
3166         case DW_CFA_def_cfa_expression:
3167         case DW_CFA_expression:
3168           output_cfa_loc (cfi);
3169           break;
3170
3171         case DW_CFA_GNU_negative_offset_extended:
3172           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3173           gcc_unreachable ();
3174
3175         default:
3176           break;
3177         }
3178     }
3179 }
3180
3181 /* Similar, but do it via assembler directives instead.  */
3182
3183 static void
3184 output_cfi_directive (dw_cfi_ref cfi)
3185 {
3186   unsigned long r, r2;
3187
3188   switch (cfi->dw_cfi_opc)
3189     {
3190     case DW_CFA_advance_loc:
3191     case DW_CFA_advance_loc1:
3192     case DW_CFA_advance_loc2:
3193     case DW_CFA_advance_loc4:
3194     case DW_CFA_MIPS_advance_loc8:
3195     case DW_CFA_set_loc:
3196       /* Should only be created by add_fde_cfi in a code path not
3197          followed when emitting via directives.  The assembler is
3198          going to take care of this for us.  */
3199       gcc_unreachable ();
3200
3201     case DW_CFA_offset:
3202     case DW_CFA_offset_extended:
3203     case DW_CFA_offset_extended_sf:
3204       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3205       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3206                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3207       break;
3208
3209     case DW_CFA_restore:
3210     case DW_CFA_restore_extended:
3211       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3212       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3213       break;
3214
3215     case DW_CFA_undefined:
3216       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3217       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3218       break;
3219
3220     case DW_CFA_same_value:
3221       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3222       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3223       break;
3224
3225     case DW_CFA_def_cfa:
3226     case DW_CFA_def_cfa_sf:
3227       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3228       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3229                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3230       break;
3231
3232     case DW_CFA_def_cfa_register:
3233       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3234       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3235       break;
3236
3237     case DW_CFA_register:
3238       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3239       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3240       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3241       break;
3242
3243     case DW_CFA_def_cfa_offset:
3244     case DW_CFA_def_cfa_offset_sf:
3245       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3246                HOST_WIDE_INT_PRINT_DEC"\n",
3247                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3248       break;
3249
3250     case DW_CFA_remember_state:
3251       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3252       break;
3253     case DW_CFA_restore_state:
3254       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3255       break;
3256
3257     case DW_CFA_GNU_args_size:
3258       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3259       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3260       if (flag_debug_asm)
3261         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3262                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3263       fputc ('\n', asm_out_file);
3264       break;
3265
3266     case DW_CFA_GNU_window_save:
3267       fprintf (asm_out_file, "\t.cfi_window_save\n");
3268       break;
3269
3270     case DW_CFA_def_cfa_expression:
3271     case DW_CFA_expression:
3272       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3273       output_cfa_loc_raw (cfi);
3274       fputc ('\n', asm_out_file);
3275       break;
3276
3277     default:
3278       gcc_unreachable ();
3279     }
3280 }
3281
3282 DEF_VEC_P (dw_cfi_ref);
3283 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3284
3285 /* Output CFIs to bring current FDE to the same state as after executing
3286    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3287    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3288    other arguments to pass to output_cfi.  */
3289
3290 static void
3291 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3292 {
3293   struct dw_cfi_struct cfi_buf;
3294   dw_cfi_ref cfi2;
3295   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3296   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3297   unsigned int len, idx;
3298
3299   for (;; cfi = cfi->dw_cfi_next)
3300     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3301       {
3302       case DW_CFA_advance_loc:
3303       case DW_CFA_advance_loc1:
3304       case DW_CFA_advance_loc2:
3305       case DW_CFA_advance_loc4:
3306       case DW_CFA_MIPS_advance_loc8:
3307       case DW_CFA_set_loc:
3308         /* All advances should be ignored.  */
3309         break;
3310       case DW_CFA_remember_state:
3311         {
3312           dw_cfi_ref args_size = cfi_args_size;
3313
3314           /* Skip everything between .cfi_remember_state and
3315              .cfi_restore_state.  */
3316           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3317             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3318               break;
3319             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3320               args_size = cfi2;
3321             else
3322               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3323
3324           if (cfi2 == NULL)
3325             goto flush_all;
3326           else
3327             {
3328               cfi = cfi2;
3329               cfi_args_size = args_size;
3330             }
3331           break;
3332         }
3333       case DW_CFA_GNU_args_size:
3334         cfi_args_size = cfi;
3335         break;
3336       case DW_CFA_GNU_window_save:
3337         goto flush_all;
3338       case DW_CFA_offset:
3339       case DW_CFA_offset_extended:
3340       case DW_CFA_offset_extended_sf:
3341       case DW_CFA_restore:
3342       case DW_CFA_restore_extended:
3343       case DW_CFA_undefined:
3344       case DW_CFA_same_value:
3345       case DW_CFA_register:
3346       case DW_CFA_val_offset:
3347       case DW_CFA_val_offset_sf:
3348       case DW_CFA_expression:
3349       case DW_CFA_val_expression:
3350       case DW_CFA_GNU_negative_offset_extended:
3351         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3352           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3353                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3354         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3355         break;
3356       case DW_CFA_def_cfa:
3357       case DW_CFA_def_cfa_sf:
3358       case DW_CFA_def_cfa_expression:
3359         cfi_cfa = cfi;
3360         cfi_cfa_offset = cfi;
3361         break;
3362       case DW_CFA_def_cfa_register:
3363         cfi_cfa = cfi;
3364         break;
3365       case DW_CFA_def_cfa_offset:
3366       case DW_CFA_def_cfa_offset_sf:
3367         cfi_cfa_offset = cfi;
3368         break;
3369       case DW_CFA_nop:
3370         gcc_assert (cfi == NULL);
3371       flush_all:
3372         len = VEC_length (dw_cfi_ref, regs);
3373         for (idx = 0; idx < len; idx++)
3374           {
3375             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3376             if (cfi2 != NULL
3377                 && cfi2->dw_cfi_opc != DW_CFA_restore
3378                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3379               {
3380                 if (do_cfi_asm)
3381                   output_cfi_directive (cfi2);
3382                 else
3383                   output_cfi (cfi2, fde, for_eh);
3384               }
3385           }
3386         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3387           {
3388             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3389             cfi_buf = *cfi_cfa;
3390             switch (cfi_cfa_offset->dw_cfi_opc)
3391               {
3392               case DW_CFA_def_cfa_offset:
3393                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3394                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3395                 break;
3396               case DW_CFA_def_cfa_offset_sf:
3397                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3398                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3399                 break;
3400               case DW_CFA_def_cfa:
3401               case DW_CFA_def_cfa_sf:
3402                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3403                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3404                 break;
3405               default:
3406                 gcc_unreachable ();
3407               }
3408             cfi_cfa = &cfi_buf;
3409           }
3410         else if (cfi_cfa_offset)
3411           cfi_cfa = cfi_cfa_offset;
3412         if (cfi_cfa)
3413           {
3414             if (do_cfi_asm)
3415               output_cfi_directive (cfi_cfa);
3416             else
3417               output_cfi (cfi_cfa, fde, for_eh);
3418           }
3419         cfi_cfa = NULL;
3420         cfi_cfa_offset = NULL;
3421         if (cfi_args_size
3422             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3423           {
3424             if (do_cfi_asm)
3425               output_cfi_directive (cfi_args_size);
3426             else
3427               output_cfi (cfi_args_size, fde, for_eh);
3428           }
3429         cfi_args_size = NULL;
3430         if (cfi == NULL)
3431           {
3432             VEC_free (dw_cfi_ref, heap, regs);
3433             return;
3434           }
3435         else if (do_cfi_asm)
3436           output_cfi_directive (cfi);
3437         else
3438           output_cfi (cfi, fde, for_eh);
3439         break;
3440       default:
3441         gcc_unreachable ();
3442     }
3443 }
3444
3445 /* Output one FDE.  */
3446
3447 static void
3448 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3449             char *section_start_label, int fde_encoding, char *augmentation,
3450             bool any_lsda_needed, int lsda_encoding)
3451 {
3452   const char *begin, *end;
3453   static unsigned int j;
3454   char l1[20], l2[20];
3455   dw_cfi_ref cfi;
3456
3457   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3458                                 /* empty */ 0);
3459   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3460                                   for_eh + j);
3461   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3462   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3463   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3464     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3465                          " indicating 64-bit DWARF extension");
3466   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3467                         "FDE Length");
3468   ASM_OUTPUT_LABEL (asm_out_file, l1);
3469
3470   if (for_eh)
3471     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3472   else
3473     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3474                            debug_frame_section, "FDE CIE offset");
3475
3476   if (!fde->dw_fde_switched_sections)
3477     {
3478       begin = fde->dw_fde_begin;
3479       end = fde->dw_fde_end;
3480     }
3481   else
3482     {
3483       /* For the first section, prefer dw_fde_begin over
3484          dw_fde_{hot,cold}_section_label, as the latter
3485          might be separated from the real start of the
3486          function by alignment padding.  */
3487       if (!second)
3488         begin = fde->dw_fde_begin;
3489       else if (fde->dw_fde_switched_cold_to_hot)
3490         begin = fde->dw_fde_hot_section_label;
3491       else
3492         begin = fde->dw_fde_unlikely_section_label;
3493       if (second ^ fde->dw_fde_switched_cold_to_hot)
3494         end = fde->dw_fde_unlikely_section_end_label;
3495       else
3496         end = fde->dw_fde_hot_section_end_label;
3497     }
3498
3499   if (for_eh)
3500     {
3501       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3502       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3503       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3504                                        "FDE initial location");
3505       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3506                             end, begin, "FDE address range");
3507     }
3508   else
3509     {
3510       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3511       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3512     }
3513
3514   if (augmentation[0])
3515     {
3516       if (any_lsda_needed)
3517         {
3518           int size = size_of_encoded_value (lsda_encoding);
3519
3520           if (lsda_encoding == DW_EH_PE_aligned)
3521             {
3522               int offset = (  4         /* Length */
3523                             + 4         /* CIE offset */
3524                             + 2 * size_of_encoded_value (fde_encoding)
3525                             + 1         /* Augmentation size */ );
3526               int pad = -offset & (PTR_SIZE - 1);
3527
3528               size += pad;
3529               gcc_assert (size_of_uleb128 (size) == 1);
3530             }
3531
3532           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3533
3534           if (fde->uses_eh_lsda)
3535             {
3536               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3537                                            fde->funcdef_number);
3538               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3539                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3540                                                false,
3541                                                "Language Specific Data Area");
3542             }
3543           else
3544             {
3545               if (lsda_encoding == DW_EH_PE_aligned)
3546                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3547               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3548                                    "Language Specific Data Area (none)");
3549             }
3550         }
3551       else
3552         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3553     }
3554
3555   /* Loop through the Call Frame Instructions associated with
3556      this FDE.  */
3557   fde->dw_fde_current_label = begin;
3558   if (!fde->dw_fde_switched_sections)
3559     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3560       output_cfi (cfi, fde, for_eh);
3561   else if (!second)
3562     {
3563       if (fde->dw_fde_switch_cfi)
3564         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3565           {
3566             output_cfi (cfi, fde, for_eh);
3567             if (cfi == fde->dw_fde_switch_cfi)
3568               break;
3569           }
3570     }
3571   else
3572     {
3573       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3574
3575       if (fde->dw_fde_switch_cfi)
3576         {
3577           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3578           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3579           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3580           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3581         }
3582       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3583         output_cfi (cfi, fde, for_eh);
3584     }
3585
3586   /* If we are to emit a ref/link from function bodies to their frame tables,
3587      do it now.  This is typically performed to make sure that tables
3588      associated with functions are dragged with them and not discarded in
3589      garbage collecting links. We need to do this on a per function basis to
3590      cope with -ffunction-sections.  */
3591
3592 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3593   /* Switch to the function section, emit the ref to the tables, and
3594      switch *back* into the table section.  */
3595   switch_to_section (function_section (fde->decl));
3596   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3597   switch_to_frame_table_section (for_eh, true);
3598 #endif
3599
3600   /* Pad the FDE out to an address sized boundary.  */
3601   ASM_OUTPUT_ALIGN (asm_out_file,
3602                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3603   ASM_OUTPUT_LABEL (asm_out_file, l2);
3604
3605   j += 2;
3606 }
3607
3608 /* Output the call frame information used to record information
3609    that relates to calculating the frame pointer, and records the
3610    location of saved registers.  */
3611
3612 static void
3613 output_call_frame_info (int for_eh)
3614 {
3615   unsigned int i;
3616   dw_fde_ref fde;
3617   dw_cfi_ref cfi;
3618   char l1[20], l2[20], section_start_label[20];
3619   bool any_lsda_needed = false;
3620   char augmentation[6];
3621   int augmentation_size;
3622   int fde_encoding = DW_EH_PE_absptr;
3623   int per_encoding = DW_EH_PE_absptr;
3624   int lsda_encoding = DW_EH_PE_absptr;
3625   int return_reg;
3626   rtx personality = NULL;
3627   int dw_cie_version;
3628
3629   /* Don't emit a CIE if there won't be any FDEs.  */
3630   if (fde_table_in_use == 0)
3631     return;
3632
3633   /* Nothing to do if the assembler's doing it all.  */
3634   if (dwarf2out_do_cfi_asm ())
3635     return;
3636
3637   /* If we make FDEs linkonce, we may have to emit an empty label for
3638      an FDE that wouldn't otherwise be emitted.  We want to avoid
3639      having an FDE kept around when the function it refers to is
3640      discarded.  Example where this matters: a primary function
3641      template in C++ requires EH information, but an explicit
3642      specialization doesn't.  */
3643   if (TARGET_USES_WEAK_UNWIND_INFO
3644       && ! flag_asynchronous_unwind_tables
3645       && flag_exceptions
3646       && for_eh)
3647     for (i = 0; i < fde_table_in_use; i++)
3648       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3649           && !fde_table[i].uses_eh_lsda
3650           && ! DECL_WEAK (fde_table[i].decl))
3651         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3652                                       for_eh, /* empty */ 1);
3653
3654   /* If we don't have any functions we'll want to unwind out of, don't
3655      emit any EH unwind information.  Note that if exceptions aren't
3656      enabled, we won't have collected nothrow information, and if we
3657      asked for asynchronous tables, we always want this info.  */
3658   if (for_eh)
3659     {
3660       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3661
3662       for (i = 0; i < fde_table_in_use; i++)
3663         if (fde_table[i].uses_eh_lsda)
3664           any_eh_needed = any_lsda_needed = true;
3665         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3666           any_eh_needed = true;
3667         else if (! fde_table[i].nothrow
3668                  && ! fde_table[i].all_throwers_are_sibcalls)
3669           any_eh_needed = true;
3670
3671       if (! any_eh_needed)
3672         return;
3673     }
3674
3675   /* We're going to be generating comments, so turn on app.  */
3676   if (flag_debug_asm)
3677     app_enable ();
3678
3679   /* Switch to the proper frame section, first time.  */
3680   switch_to_frame_table_section (for_eh, false);
3681
3682   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3683   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3684
3685   /* Output the CIE.  */
3686   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3687   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3688   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3689     dw2_asm_output_data (4, 0xffffffff,
3690       "Initial length escape value indicating 64-bit DWARF extension");
3691   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3692                         "Length of Common Information Entry");
3693   ASM_OUTPUT_LABEL (asm_out_file, l1);
3694
3695   /* Now that the CIE pointer is PC-relative for EH,
3696      use 0 to identify the CIE.  */
3697   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3698                        (for_eh ? 0 : DWARF_CIE_ID),
3699                        "CIE Identifier Tag");
3700
3701   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3702      use CIE version 1, unless that would produce incorrect results
3703      due to overflowing the return register column.  */
3704   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3705   dw_cie_version = 1;
3706   if (return_reg >= 256 || dwarf_version > 2)
3707     dw_cie_version = 3;
3708   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3709
3710   augmentation[0] = 0;
3711   augmentation_size = 0;
3712
3713   personality = current_unit_personality;
3714   if (for_eh)
3715     {
3716       char *p;
3717
3718       /* Augmentation:
3719          z      Indicates that a uleb128 is present to size the
3720                 augmentation section.
3721          L      Indicates the encoding (and thus presence) of
3722                 an LSDA pointer in the FDE augmentation.
3723          R      Indicates a non-default pointer encoding for
3724                 FDE code pointers.
3725          P      Indicates the presence of an encoding + language
3726                 personality routine in the CIE augmentation.  */
3727
3728       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3729       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3730       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3731
3732       p = augmentation + 1;
3733       if (personality)
3734         {
3735           *p++ = 'P';
3736           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3737           assemble_external_libcall (personality);
3738         }
3739       if (any_lsda_needed)
3740         {
3741           *p++ = 'L';
3742           augmentation_size += 1;
3743         }
3744       if (fde_encoding != DW_EH_PE_absptr)
3745         {
3746           *p++ = 'R';
3747           augmentation_size += 1;
3748         }
3749       if (p > augmentation + 1)
3750         {
3751           augmentation[0] = 'z';
3752           *p = '\0';
3753         }
3754
3755       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3756       if (personality && per_encoding == DW_EH_PE_aligned)
3757         {
3758           int offset = (  4             /* Length */
3759                         + 4             /* CIE Id */
3760                         + 1             /* CIE version */
3761                         + strlen (augmentation) + 1     /* Augmentation */
3762                         + size_of_uleb128 (1)           /* Code alignment */
3763                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3764                         + 1             /* RA column */
3765                         + 1             /* Augmentation size */
3766                         + 1             /* Personality encoding */ );
3767           int pad = -offset & (PTR_SIZE - 1);
3768
3769           augmentation_size += pad;
3770
3771           /* Augmentations should be small, so there's scarce need to
3772              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3773           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3774         }
3775     }
3776
3777   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3778   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3779   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3780                                "CIE Data Alignment Factor");
3781
3782   if (dw_cie_version == 1)
3783     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3784   else
3785     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3786
3787   if (augmentation[0])
3788     {
3789       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3790       if (personality)
3791         {
3792           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3793                                eh_data_format_name (per_encoding));
3794           dw2_asm_output_encoded_addr_rtx (per_encoding,
3795                                            personality,
3796                                            true, NULL);
3797         }
3798
3799       if (any_lsda_needed)
3800         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3801                              eh_data_format_name (lsda_encoding));
3802
3803       if (fde_encoding != DW_EH_PE_absptr)
3804         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3805                              eh_data_format_name (fde_encoding));
3806     }
3807
3808   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3809     output_cfi (cfi, NULL, for_eh);
3810
3811   /* Pad the CIE out to an address sized boundary.  */
3812   ASM_OUTPUT_ALIGN (asm_out_file,
3813                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3814   ASM_OUTPUT_LABEL (asm_out_file, l2);
3815
3816   /* Loop through all of the FDE's.  */
3817   for (i = 0; i < fde_table_in_use; i++)
3818     {
3819       unsigned int k;
3820       fde = &fde_table[i];
3821
3822       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3823       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3824           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3825           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3826           && !fde->uses_eh_lsda)
3827         continue;
3828
3829       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3830         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3831                     augmentation, any_lsda_needed, lsda_encoding);
3832     }
3833
3834   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3835     dw2_asm_output_data (4, 0, "End of Table");
3836 #ifdef MIPS_DEBUGGING_INFO
3837   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3838      get a value of 0.  Putting .align 0 after the label fixes it.  */
3839   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3840 #endif
3841
3842   /* Turn off app to make assembly quicker.  */
3843   if (flag_debug_asm)
3844     app_disable ();
3845 }
3846
3847 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3848
3849 static void
3850 dwarf2out_do_cfi_startproc (bool second)
3851 {
3852   int enc;
3853   rtx ref;
3854   rtx personality = get_personality_function (current_function_decl);
3855
3856   fprintf (asm_out_file, "\t.cfi_startproc\n");
3857
3858   if (personality)
3859     {
3860       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3861       ref = personality;
3862
3863       /* ??? The GAS support isn't entirely consistent.  We have to
3864          handle indirect support ourselves, but PC-relative is done
3865          in the assembler.  Further, the assembler can't handle any
3866          of the weirder relocation types.  */
3867       if (enc & DW_EH_PE_indirect)
3868         ref = dw2_force_const_mem (ref, true);
3869
3870       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3871       output_addr_const (asm_out_file, ref);
3872       fputc ('\n', asm_out_file);
3873     }
3874
3875   if (crtl->uses_eh_lsda)
3876     {
3877       char lab[20];
3878
3879       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3880       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3881                                    current_function_funcdef_no);
3882       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3883       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3884
3885       if (enc & DW_EH_PE_indirect)
3886         ref = dw2_force_const_mem (ref, true);
3887
3888       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3889       output_addr_const (asm_out_file, ref);
3890       fputc ('\n', asm_out_file);
3891     }
3892 }
3893
3894 /* Output a marker (i.e. a label) for the beginning of a function, before
3895    the prologue.  */
3896
3897 void
3898 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3899                           const char *file ATTRIBUTE_UNUSED)
3900 {
3901   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3902   char * dup_label;
3903   dw_fde_ref fde;
3904   section *fnsec;
3905
3906   current_function_func_begin_label = NULL;
3907
3908 #ifdef TARGET_UNWIND_INFO
3909   /* ??? current_function_func_begin_label is also used by except.c
3910      for call-site information.  We must emit this label if it might
3911      be used.  */
3912   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3913       && ! dwarf2out_do_frame ())
3914     return;
3915 #else
3916   if (! dwarf2out_do_frame ())
3917     return;
3918 #endif
3919
3920   fnsec = function_section (current_function_decl);
3921   switch_to_section (fnsec);
3922   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3923                                current_function_funcdef_no);
3924   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3925                           current_function_funcdef_no);
3926   dup_label = xstrdup (label);
3927   current_function_func_begin_label = dup_label;
3928
3929 #ifdef TARGET_UNWIND_INFO
3930   /* We can elide the fde allocation if we're not emitting debug info.  */
3931   if (! dwarf2out_do_frame ())
3932     return;
3933 #endif
3934
3935   /* Expand the fde table if necessary.  */
3936   if (fde_table_in_use == fde_table_allocated)
3937     {
3938       fde_table_allocated += FDE_TABLE_INCREMENT;
3939       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3940       memset (fde_table + fde_table_in_use, 0,
3941               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3942     }
3943
3944   /* Record the FDE associated with this function.  */
3945   current_funcdef_fde = fde_table_in_use;
3946
3947   /* Add the new FDE at the end of the fde_table.  */
3948   fde = &fde_table[fde_table_in_use++];
3949   fde->decl = current_function_decl;
3950   fde->dw_fde_begin = dup_label;
3951   fde->dw_fde_current_label = dup_label;
3952   fde->dw_fde_hot_section_label = NULL;
3953   fde->dw_fde_hot_section_end_label = NULL;
3954   fde->dw_fde_unlikely_section_label = NULL;
3955   fde->dw_fde_unlikely_section_end_label = NULL;
3956   fde->dw_fde_switched_sections = 0;
3957   fde->dw_fde_switched_cold_to_hot = 0;
3958   fde->dw_fde_end = NULL;
3959   fde->dw_fde_cfi = NULL;
3960   fde->dw_fde_switch_cfi = NULL;
3961   fde->funcdef_number = current_function_funcdef_no;
3962   fde->nothrow = crtl->nothrow;
3963   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3964   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3965   fde->drap_reg = INVALID_REGNUM;
3966   fde->vdrap_reg = INVALID_REGNUM;
3967   if (flag_reorder_blocks_and_partition)
3968     {
3969       section *unlikelysec;
3970       if (first_function_block_is_cold)
3971         fde->in_std_section = 1;
3972       else
3973         fde->in_std_section
3974           = (fnsec == text_section
3975              || (cold_text_section && fnsec == cold_text_section));
3976       unlikelysec = unlikely_text_section ();
3977       fde->cold_in_std_section
3978         = (unlikelysec == text_section
3979            || (cold_text_section && unlikelysec == cold_text_section));
3980     }
3981   else
3982     {
3983       fde->in_std_section
3984         = (fnsec == text_section
3985            || (cold_text_section && fnsec == cold_text_section));
3986       fde->cold_in_std_section = 0;
3987     }
3988
3989   args_size = old_args_size = 0;
3990
3991   /* We only want to output line number information for the genuine dwarf2
3992      prologue case, not the eh frame case.  */
3993 #ifdef DWARF2_DEBUGGING_INFO
3994   if (file)
3995     dwarf2out_source_line (line, file, 0, true);
3996 #endif
3997
3998   if (dwarf2out_do_cfi_asm ())
3999     dwarf2out_do_cfi_startproc (false);
4000   else
4001     {
4002       rtx personality = get_personality_function (current_function_decl);
4003       if (!current_unit_personality)
4004         current_unit_personality = personality;
4005
4006       /* We cannot keep a current personality per function as without CFI
4007          asm at the point where we emit the CFI data there is no current
4008          function anymore.  */
4009       if (personality
4010           && current_unit_personality != personality)
4011         sorry ("Multiple EH personalities are supported only with assemblers "
4012                "supporting .cfi.personality directive.");
4013     }
4014 }
4015
4016 /* Output a marker (i.e. a label) for the absolute end of the generated code
4017    for a function definition.  This gets called *after* the epilogue code has
4018    been generated.  */
4019
4020 void
4021 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4022                         const char *file ATTRIBUTE_UNUSED)
4023 {
4024   dw_fde_ref fde;
4025   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4026
4027 #ifdef DWARF2_DEBUGGING_INFO
4028   last_var_location_insn = NULL_RTX;
4029 #endif
4030
4031   if (dwarf2out_do_cfi_asm ())
4032     fprintf (asm_out_file, "\t.cfi_endproc\n");
4033
4034   /* Output a label to mark the endpoint of the code generated for this
4035      function.  */
4036   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4037                                current_function_funcdef_no);
4038   ASM_OUTPUT_LABEL (asm_out_file, label);
4039   fde = current_fde ();
4040   gcc_assert (fde != NULL);
4041   fde->dw_fde_end = xstrdup (label);
4042 }
4043
4044 void
4045 dwarf2out_frame_init (void)
4046 {
4047   /* Allocate the initial hunk of the fde_table.  */
4048   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4049   fde_table_allocated = FDE_TABLE_INCREMENT;
4050   fde_table_in_use = 0;
4051
4052   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4053      sake of lookup_cfa.  */
4054
4055   /* On entry, the Canonical Frame Address is at SP.  */
4056   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4057
4058 #ifdef DWARF2_UNWIND_INFO
4059   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4060     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4061 #endif
4062 }
4063
4064 void
4065 dwarf2out_frame_finish (void)
4066 {
4067   /* Output call frame information.  */
4068   if (DWARF2_FRAME_INFO)
4069     output_call_frame_info (0);
4070
4071 #ifndef TARGET_UNWIND_INFO
4072   /* Output another copy for the unwinder.  */
4073   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4074     output_call_frame_info (1);
4075 #endif
4076 }
4077
4078 /* Note that the current function section is being used for code.  */
4079
4080 static void
4081 dwarf2out_note_section_used (void)
4082 {
4083   section *sec = current_function_section ();
4084   if (sec == text_section)
4085     text_section_used = true;
4086   else if (sec == cold_text_section)
4087     cold_text_section_used = true;
4088 }
4089
4090 void
4091 dwarf2out_switch_text_section (void)
4092 {
4093   dw_fde_ref fde = current_fde ();
4094
4095   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4096
4097   fde->dw_fde_switched_sections = 1;
4098   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4099
4100   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4101   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4102   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4103   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4104   have_multiple_function_sections = true;
4105
4106   /* Reset the current label on switching text sections, so that we
4107      don't attempt to advance_loc4 between labels in different sections.  */
4108   fde->dw_fde_current_label = NULL;
4109
4110   /* There is no need to mark used sections when not debugging.  */
4111   if (cold_text_section != NULL)
4112     dwarf2out_note_section_used ();
4113
4114   if (dwarf2out_do_cfi_asm ())
4115     fprintf (asm_out_file, "\t.cfi_endproc\n");
4116
4117   /* Now do the real section switch.  */
4118   switch_to_section (current_function_section ());
4119
4120   if (dwarf2out_do_cfi_asm ())
4121     {
4122       dwarf2out_do_cfi_startproc (true);
4123       /* As this is a different FDE, insert all current CFI instructions
4124          again.  */
4125       output_cfis (fde->dw_fde_cfi, true, fde, true);
4126     }
4127   else
4128     {
4129       dw_cfi_ref cfi = fde->dw_fde_cfi;
4130
4131       cfi = fde->dw_fde_cfi;
4132       if (cfi)
4133         while (cfi->dw_cfi_next != NULL)
4134           cfi = cfi->dw_cfi_next;
4135       fde->dw_fde_switch_cfi = cfi;
4136     }
4137 }
4138 #endif
4139 \f
4140 /* And now, the subset of the debugging information support code necessary
4141    for emitting location expressions.  */
4142
4143 /* Data about a single source file.  */
4144 struct GTY(()) dwarf_file_data {
4145   const char * filename;
4146   int emitted_number;
4147 };
4148
4149 typedef struct dw_val_struct *dw_val_ref;
4150 typedef struct die_struct *dw_die_ref;
4151 typedef const struct die_struct *const_dw_die_ref;
4152 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4153 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4154
4155 typedef struct GTY(()) deferred_locations_struct
4156 {
4157   tree variable;
4158   dw_die_ref die;
4159 } deferred_locations;
4160
4161 DEF_VEC_O(deferred_locations);
4162 DEF_VEC_ALLOC_O(deferred_locations,gc);
4163
4164 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4165
4166 DEF_VEC_P(dw_die_ref);
4167 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4168
4169 /* Each DIE may have a series of attribute/value pairs.  Values
4170    can take on several forms.  The forms that are used in this
4171    implementation are listed below.  */
4172
4173 enum dw_val_class
4174 {
4175   dw_val_class_addr,
4176   dw_val_class_offset,
4177   dw_val_class_loc,
4178   dw_val_class_loc_list,
4179   dw_val_class_range_list,
4180   dw_val_class_const,
4181   dw_val_class_unsigned_const,
4182   dw_val_class_const_double,
4183   dw_val_class_vec,
4184   dw_val_class_flag,
4185   dw_val_class_die_ref,
4186   dw_val_class_fde_ref,
4187   dw_val_class_lbl_id,
4188   dw_val_class_lineptr,
4189   dw_val_class_str,
4190   dw_val_class_macptr,
4191   dw_val_class_file,
4192   dw_val_class_data8
4193 };
4194
4195 /* Describe a floating point constant value, or a vector constant value.  */
4196
4197 typedef struct GTY(()) dw_vec_struct {
4198   unsigned char * GTY((length ("%h.length"))) array;
4199   unsigned length;
4200   unsigned elt_size;
4201 }
4202 dw_vec_const;
4203
4204 /* The dw_val_node describes an attribute's value, as it is
4205    represented internally.  */
4206
4207 typedef struct GTY(()) dw_val_struct {
4208   enum dw_val_class val_class;
4209   union dw_val_struct_union
4210     {
4211       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4212       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4213       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4214       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4215       HOST_WIDE_INT GTY ((default)) val_int;
4216       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4217       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4218       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4219       struct dw_val_die_union
4220         {
4221           dw_die_ref die;
4222           int external;
4223         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4224       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4225       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4226       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4227       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4228       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4229       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4230     }
4231   GTY ((desc ("%1.val_class"))) v;
4232 }
4233 dw_val_node;
4234
4235 /* Locations in memory are described using a sequence of stack machine
4236    operations.  */
4237
4238 typedef struct GTY(()) dw_loc_descr_struct {
4239   dw_loc_descr_ref dw_loc_next;
4240   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4241   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4242      from DW_OP_addr with a dtp-relative symbol relocation.  */
4243   unsigned int dtprel : 1;
4244   int dw_loc_addr;
4245   dw_val_node dw_loc_oprnd1;
4246   dw_val_node dw_loc_oprnd2;
4247 }
4248 dw_loc_descr_node;
4249
4250 /* Location lists are ranges + location descriptions for that range,
4251    so you can track variables that are in different places over
4252    their entire life.  */
4253 typedef struct GTY(()) dw_loc_list_struct {
4254   dw_loc_list_ref dw_loc_next;
4255   const char *begin; /* Label for begin address of range */
4256   const char *end;  /* Label for end address of range */
4257   char *ll_symbol; /* Label for beginning of location list.
4258                       Only on head of list */
4259   const char *section; /* Section this loclist is relative to */
4260   dw_loc_descr_ref expr;
4261 } dw_loc_list_node;
4262
4263 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4264
4265 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4266
4267 /* Convert a DWARF stack opcode into its string name.  */
4268
4269 static const char *
4270 dwarf_stack_op_name (unsigned int op)
4271 {
4272   switch (op)
4273     {
4274     case DW_OP_addr:
4275       return "DW_OP_addr";
4276     case DW_OP_deref:
4277       return "DW_OP_deref";
4278     case DW_OP_const1u:
4279       return "DW_OP_const1u";
4280     case DW_OP_const1s:
4281       return "DW_OP_const1s";
4282     case DW_OP_const2u:
4283       return "DW_OP_const2u";
4284     case DW_OP_const2s:
4285       return "DW_OP_const2s";
4286     case DW_OP_const4u:
4287       return "DW_OP_const4u";
4288     case DW_OP_const4s:
4289       return "DW_OP_const4s";
4290     case DW_OP_const8u:
4291       return "DW_OP_const8u";
4292     case DW_OP_const8s:
4293       return "DW_OP_const8s";
4294     case DW_OP_constu:
4295       return "DW_OP_constu";
4296     case DW_OP_consts:
4297       return "DW_OP_consts";
4298     case DW_OP_dup:
4299       return "DW_OP_dup";
4300     case DW_OP_drop:
4301       return "DW_OP_drop";
4302     case DW_OP_over:
4303       return "DW_OP_over";
4304     case DW_OP_pick:
4305       return "DW_OP_pick";
4306     case DW_OP_swap:
4307       return "DW_OP_swap";
4308     case DW_OP_rot:
4309       return "DW_OP_rot";
4310     case DW_OP_xderef:
4311       return "DW_OP_xderef";
4312     case DW_OP_abs:
4313       return "DW_OP_abs";
4314     case DW_OP_and:
4315       return "DW_OP_and";
4316     case DW_OP_div:
4317       return "DW_OP_div";
4318     case DW_OP_minus:
4319       return "DW_OP_minus";
4320     case DW_OP_mod:
4321       return "DW_OP_mod";
4322     case DW_OP_mul:
4323       return "DW_OP_mul";
4324     case DW_OP_neg:
4325       return "DW_OP_neg";
4326     case DW_OP_not:
4327       return "DW_OP_not";
4328     case DW_OP_or:
4329       return "DW_OP_or";
4330     case DW_OP_plus:
4331       return "DW_OP_plus";
4332     case DW_OP_plus_uconst:
4333       return "DW_OP_plus_uconst";
4334     case DW_OP_shl:
4335       return "DW_OP_shl";
4336     case DW_OP_shr:
4337       return "DW_OP_shr";
4338     case DW_OP_shra:
4339       return "DW_OP_shra";
4340     case DW_OP_xor:
4341       return "DW_OP_xor";
4342     case DW_OP_bra:
4343       return "DW_OP_bra";
4344     case DW_OP_eq:
4345       return "DW_OP_eq";
4346     case DW_OP_ge:
4347       return "DW_OP_ge";
4348     case DW_OP_gt:
4349       return "DW_OP_gt";
4350     case DW_OP_le:
4351       return "DW_OP_le";
4352     case DW_OP_lt:
4353       return "DW_OP_lt";
4354     case DW_OP_ne:
4355       return "DW_OP_ne";
4356     case DW_OP_skip:
4357       return "DW_OP_skip";
4358     case DW_OP_lit0:
4359       return "DW_OP_lit0";
4360     case DW_OP_lit1:
4361       return "DW_OP_lit1";
4362     case DW_OP_lit2:
4363       return "DW_OP_lit2";
4364     case DW_OP_lit3:
4365       return "DW_OP_lit3";
4366     case DW_OP_lit4:
4367       return "DW_OP_lit4";
4368     case DW_OP_lit5:
4369       return "DW_OP_lit5";
4370     case DW_OP_lit6:
4371       return "DW_OP_lit6";
4372     case DW_OP_lit7:
4373       return "DW_OP_lit7";
4374     case DW_OP_lit8:
4375       return "DW_OP_lit8";
4376     case DW_OP_lit9:
4377       return "DW_OP_lit9";
4378     case DW_OP_lit10:
4379       return "DW_OP_lit10";
4380     case DW_OP_lit11:
4381       return "DW_OP_lit11";
4382     case DW_OP_lit12:
4383       return "DW_OP_lit12";
4384     case DW_OP_lit13:
4385       return "DW_OP_lit13";
4386     case DW_OP_lit14:
4387       return "DW_OP_lit14";
4388     case DW_OP_lit15:
4389       return "DW_OP_lit15";
4390     case DW_OP_lit16:
4391       return "DW_OP_lit16";
4392     case DW_OP_lit17:
4393       return "DW_OP_lit17";
4394     case DW_OP_lit18:
4395       return "DW_OP_lit18";
4396     case DW_OP_lit19:
4397       return "DW_OP_lit19";
4398     case DW_OP_lit20:
4399       return "DW_OP_lit20";
4400     case DW_OP_lit21:
4401       return "DW_OP_lit21";
4402     case DW_OP_lit22:
4403       return "DW_OP_lit22";
4404     case DW_OP_lit23:
4405       return "DW_OP_lit23";
4406     case DW_OP_lit24:
4407       return "DW_OP_lit24";
4408     case DW_OP_lit25:
4409       return "DW_OP_lit25";
4410     case DW_OP_lit26:
4411       return "DW_OP_lit26";
4412     case DW_OP_lit27:
4413       return "DW_OP_lit27";
4414     case DW_OP_lit28:
4415       return "DW_OP_lit28";
4416     case DW_OP_lit29:
4417       return "DW_OP_lit29";
4418     case DW_OP_lit30:
4419       return "DW_OP_lit30";
4420     case DW_OP_lit31:
4421       return "DW_OP_lit31";
4422     case DW_OP_reg0:
4423       return "DW_OP_reg0";
4424     case DW_OP_reg1:
4425       return "DW_OP_reg1";
4426     case DW_OP_reg2:
4427       return "DW_OP_reg2";
4428     case DW_OP_reg3:
4429       return "DW_OP_reg3";
4430     case DW_OP_reg4:
4431       return "DW_OP_reg4";
4432     case DW_OP_reg5:
4433       return "DW_OP_reg5";
4434     case DW_OP_reg6:
4435       return "DW_OP_reg6";
4436     case DW_OP_reg7:
4437       return "DW_OP_reg7";
4438     case DW_OP_reg8:
4439       return "DW_OP_reg8";
4440     case DW_OP_reg9:
4441       return "DW_OP_reg9";
4442     case DW_OP_reg10:
4443       return "DW_OP_reg10";
4444     case DW_OP_reg11:
4445       return "DW_OP_reg11";
4446     case DW_OP_reg12:
4447       return "DW_OP_reg12";
4448     case DW_OP_reg13:
4449       return "DW_OP_reg13";
4450     case DW_OP_reg14:
4451       return "DW_OP_reg14";
4452     case DW_OP_reg15:
4453       return "DW_OP_reg15";
4454     case DW_OP_reg16:
4455       return "DW_OP_reg16";
4456     case DW_OP_reg17:
4457       return "DW_OP_reg17";
4458     case DW_OP_reg18:
4459       return "DW_OP_reg18";
4460     case DW_OP_reg19:
4461       return "DW_OP_reg19";
4462     case DW_OP_reg20:
4463       return "DW_OP_reg20";
4464     case DW_OP_reg21:
4465       return "DW_OP_reg21";
4466     case DW_OP_reg22:
4467       return "DW_OP_reg22";
4468     case DW_OP_reg23:
4469       return "DW_OP_reg23";
4470     case DW_OP_reg24:
4471       return "DW_OP_reg24";
4472     case DW_OP_reg25:
4473       return "DW_OP_reg25";
4474     case DW_OP_reg26:
4475       return "DW_OP_reg26";
4476     case DW_OP_reg27:
4477       return "DW_OP_reg27";
4478     case DW_OP_reg28:
4479       return "DW_OP_reg28";
4480     case DW_OP_reg29:
4481       return "DW_OP_reg29";
4482     case DW_OP_reg30:
4483       return "DW_OP_reg30";
4484     case DW_OP_reg31:
4485       return "DW_OP_reg31";
4486     case DW_OP_breg0:
4487       return "DW_OP_breg0";
4488     case DW_OP_breg1:
4489       return "DW_OP_breg1";
4490     case DW_OP_breg2:
4491       return "DW_OP_breg2";
4492     case DW_OP_breg3:
4493       return "DW_OP_breg3";
4494     case DW_OP_breg4:
4495       return "DW_OP_breg4";
4496     case DW_OP_breg5:
4497       return "DW_OP_breg5";
4498     case DW_OP_breg6:
4499       return "DW_OP_breg6";
4500     case DW_OP_breg7:
4501       return "DW_OP_breg7";
4502     case DW_OP_breg8:
4503       return "DW_OP_breg8";
4504     case DW_OP_breg9:
4505       return "DW_OP_breg9";
4506     case DW_OP_breg10:
4507       return "DW_OP_breg10";
4508     case DW_OP_breg11:
4509       return "DW_OP_breg11";
4510     case DW_OP_breg12:
4511       return "DW_OP_breg12";
4512     case DW_OP_breg13:
4513       return "DW_OP_breg13";
4514     case DW_OP_breg14:
4515       return "DW_OP_breg14";
4516     case DW_OP_breg15:
4517       return "DW_OP_breg15";
4518     case DW_OP_breg16:
4519       return "DW_OP_breg16";
4520     case DW_OP_breg17:
4521       return "DW_OP_breg17";
4522     case DW_OP_breg18:
4523       return "DW_OP_breg18";
4524     case DW_OP_breg19:
4525       return "DW_OP_breg19";
4526     case DW_OP_breg20:
4527       return "DW_OP_breg20";
4528     case DW_OP_breg21:
4529       return "DW_OP_breg21";
4530     case DW_OP_breg22:
4531       return "DW_OP_breg22";
4532     case DW_OP_breg23:
4533       return "DW_OP_breg23";
4534     case DW_OP_breg24:
4535       return "DW_OP_breg24";
4536     case DW_OP_breg25:
4537       return "DW_OP_breg25";
4538     case DW_OP_breg26:
4539       return "DW_OP_breg26";
4540     case DW_OP_breg27:
4541       return "DW_OP_breg27";
4542     case DW_OP_breg28:
4543       return "DW_OP_breg28";
4544     case DW_OP_breg29:
4545       return "DW_OP_breg29";
4546     case DW_OP_breg30:
4547       return "DW_OP_breg30";
4548     case DW_OP_breg31:
4549       return "DW_OP_breg31";
4550     case DW_OP_regx:
4551       return "DW_OP_regx";
4552     case DW_OP_fbreg:
4553       return "DW_OP_fbreg";
4554     case DW_OP_bregx:
4555       return "DW_OP_bregx";
4556     case DW_OP_piece:
4557       return "DW_OP_piece";
4558     case DW_OP_deref_size:
4559       return "DW_OP_deref_size";
4560     case DW_OP_xderef_size:
4561       return "DW_OP_xderef_size";
4562     case DW_OP_nop:
4563       return "DW_OP_nop";
4564
4565     case DW_OP_push_object_address:
4566       return "DW_OP_push_object_address";
4567     case DW_OP_call2:
4568       return "DW_OP_call2";
4569     case DW_OP_call4:
4570       return "DW_OP_call4";
4571     case DW_OP_call_ref:
4572       return "DW_OP_call_ref";
4573     case DW_OP_implicit_value:
4574       return "DW_OP_implicit_value";
4575     case DW_OP_stack_value:
4576       return "DW_OP_stack_value";
4577     case DW_OP_form_tls_address:
4578       return "DW_OP_form_tls_address";
4579     case DW_OP_call_frame_cfa:
4580       return "DW_OP_call_frame_cfa";
4581     case DW_OP_bit_piece:
4582       return "DW_OP_bit_piece";
4583
4584     case DW_OP_GNU_push_tls_address:
4585       return "DW_OP_GNU_push_tls_address";
4586     case DW_OP_GNU_uninit:
4587       return "DW_OP_GNU_uninit";
4588     case DW_OP_GNU_encoded_addr:
4589       return "DW_OP_GNU_encoded_addr";
4590
4591     default:
4592       return "OP_<unknown>";
4593     }
4594 }
4595
4596 /* Return a pointer to a newly allocated location description.  Location
4597    descriptions are simple expression terms that can be strung
4598    together to form more complicated location (address) descriptions.  */
4599
4600 static inline dw_loc_descr_ref
4601 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4602                unsigned HOST_WIDE_INT oprnd2)
4603 {
4604   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4605
4606   descr->dw_loc_opc = op;
4607   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4608   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4609   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4610   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4611
4612   return descr;
4613 }
4614
4615 /* Return a pointer to a newly allocated location description for
4616    REG and OFFSET.  */
4617
4618 static inline dw_loc_descr_ref
4619 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4620 {
4621   if (reg <= 31)
4622     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4623                           offset, 0);
4624   else
4625     return new_loc_descr (DW_OP_bregx, reg, offset);
4626 }
4627
4628 /* Add a location description term to a location description expression.  */
4629
4630 static inline void
4631 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4632 {
4633   dw_loc_descr_ref *d;
4634
4635   /* Find the end of the chain.  */
4636   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4637     ;
4638
4639   *d = descr;
4640 }
4641
4642 /* Add a constant OFFSET to a location expression.  */
4643
4644 static void
4645 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4646 {
4647   dw_loc_descr_ref loc;
4648   HOST_WIDE_INT *p;
4649
4650   gcc_assert (*list_head != NULL);
4651
4652   if (!offset)
4653     return;
4654
4655   /* Find the end of the chain.  */
4656   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4657     ;
4658
4659   p = NULL;
4660   if (loc->dw_loc_opc == DW_OP_fbreg
4661       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4662     p = &loc->dw_loc_oprnd1.v.val_int;
4663   else if (loc->dw_loc_opc == DW_OP_bregx)
4664     p = &loc->dw_loc_oprnd2.v.val_int;
4665
4666   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4667      offset.  Don't optimize if an signed integer overflow would happen.  */
4668   if (p != NULL
4669       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4670           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4671     *p += offset;
4672
4673   else if (offset > 0)
4674     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4675
4676   else
4677     {
4678       loc->dw_loc_next = int_loc_descriptor (offset);
4679       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4680     }
4681 }
4682
4683 #ifdef DWARF2_DEBUGGING_INFO
4684 /* Add a constant OFFSET to a location list.  */
4685
4686 static void
4687 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4688 {
4689   dw_loc_list_ref d;
4690   for (d = list_head; d != NULL; d = d->dw_loc_next)
4691     loc_descr_plus_const (&d->expr, offset);
4692 }
4693 #endif
4694
4695 /* Return the size of a location descriptor.  */
4696
4697 static unsigned long
4698 size_of_loc_descr (dw_loc_descr_ref loc)
4699 {
4700   unsigned long size = 1;
4701
4702   switch (loc->dw_loc_opc)
4703     {
4704     case DW_OP_addr:
4705       size += DWARF2_ADDR_SIZE;
4706       break;
4707     case DW_OP_const1u:
4708     case DW_OP_const1s:
4709       size += 1;
4710       break;
4711     case DW_OP_const2u:
4712     case DW_OP_const2s:
4713       size += 2;
4714       break;
4715     case DW_OP_const4u:
4716     case DW_OP_const4s:
4717       size += 4;
4718       break;
4719     case DW_OP_const8u:
4720     case DW_OP_const8s:
4721       size += 8;
4722       break;
4723     case DW_OP_constu:
4724       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4725       break;
4726     case DW_OP_consts:
4727       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4728       break;
4729     case DW_OP_pick:
4730       size += 1;
4731       break;
4732     case DW_OP_plus_uconst:
4733       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4734       break;
4735     case DW_OP_skip:
4736     case DW_OP_bra:
4737       size += 2;
4738       break;
4739     case DW_OP_breg0:
4740     case DW_OP_breg1:
4741     case DW_OP_breg2:
4742     case DW_OP_breg3:
4743     case DW_OP_breg4:
4744     case DW_OP_breg5:
4745     case DW_OP_breg6:
4746     case DW_OP_breg7:
4747     case DW_OP_breg8:
4748     case DW_OP_breg9:
4749     case DW_OP_breg10:
4750     case DW_OP_breg11:
4751     case DW_OP_breg12:
4752     case DW_OP_breg13:
4753     case DW_OP_breg14:
4754     case DW_OP_breg15:
4755     case DW_OP_breg16:
4756     case DW_OP_breg17:
4757     case DW_OP_breg18:
4758     case DW_OP_breg19:
4759     case DW_OP_breg20:
4760     case DW_OP_breg21:
4761     case DW_OP_breg22:
4762     case DW_OP_breg23:
4763     case DW_OP_breg24:
4764     case DW_OP_breg25:
4765     case DW_OP_breg26:
4766     case DW_OP_breg27:
4767     case DW_OP_breg28:
4768     case DW_OP_breg29:
4769     case DW_OP_breg30:
4770     case DW_OP_breg31:
4771       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4772       break;
4773     case DW_OP_regx:
4774       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4775       break;
4776     case DW_OP_fbreg:
4777       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4778       break;
4779     case DW_OP_bregx:
4780       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4781       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4782       break;
4783     case DW_OP_piece:
4784       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4785       break;
4786     case DW_OP_deref_size:
4787     case DW_OP_xderef_size:
4788       size += 1;
4789       break;
4790     case DW_OP_call2:
4791       size += 2;
4792       break;
4793     case DW_OP_call4:
4794       size += 4;
4795       break;
4796     case DW_OP_call_ref:
4797       size += DWARF2_ADDR_SIZE;
4798       break;
4799     case DW_OP_implicit_value:
4800       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4801               + loc->dw_loc_oprnd1.v.val_unsigned;
4802       break;
4803     default:
4804       break;
4805     }
4806
4807   return size;
4808 }
4809
4810 /* Return the size of a series of location descriptors.  */
4811
4812 static unsigned long
4813 size_of_locs (dw_loc_descr_ref loc)
4814 {
4815   dw_loc_descr_ref l;
4816   unsigned long size;
4817
4818   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4819      field, to avoid writing to a PCH file.  */
4820   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4821     {
4822       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4823         break;
4824       size += size_of_loc_descr (l);
4825     }
4826   if (! l)
4827     return size;
4828
4829   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4830     {
4831       l->dw_loc_addr = size;
4832       size += size_of_loc_descr (l);
4833     }
4834
4835   return size;
4836 }
4837
4838 #ifdef DWARF2_DEBUGGING_INFO
4839 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4840 #endif
4841
4842 /* Output location description stack opcode's operands (if any).  */
4843
4844 static void
4845 output_loc_operands (dw_loc_descr_ref loc)
4846 {
4847   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4848   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4849
4850   switch (loc->dw_loc_opc)
4851     {
4852 #ifdef DWARF2_DEBUGGING_INFO
4853     case DW_OP_const2u:
4854     case DW_OP_const2s:
4855       dw2_asm_output_data (2, val1->v.val_int, NULL);
4856       break;
4857     case DW_OP_const4u:
4858     case DW_OP_const4s:
4859       dw2_asm_output_data (4, val1->v.val_int, NULL);
4860       break;
4861     case DW_OP_const8u:
4862     case DW_OP_const8s:
4863       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4864       dw2_asm_output_data (8, val1->v.val_int, NULL);
4865       break;
4866     case DW_OP_skip:
4867     case DW_OP_bra:
4868       {
4869         int offset;
4870
4871         gcc_assert (val1->val_class == dw_val_class_loc);
4872         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4873
4874         dw2_asm_output_data (2, offset, NULL);
4875       }
4876       break;
4877     case DW_OP_implicit_value:
4878       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4879       switch (val2->val_class)
4880         {
4881         case dw_val_class_const:
4882           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4883           break;
4884         case dw_val_class_vec:
4885           {
4886             unsigned int elt_size = val2->v.val_vec.elt_size;
4887             unsigned int len = val2->v.val_vec.length;
4888             unsigned int i;
4889             unsigned char *p;
4890
4891             if (elt_size > sizeof (HOST_WIDE_INT))
4892               {
4893                 elt_size /= 2;
4894                 len *= 2;
4895               }
4896             for (i = 0, p = val2->v.val_vec.array;
4897                  i < len;
4898                  i++, p += elt_size)
4899               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4900                                    "fp or vector constant word %u", i);
4901           }
4902           break;
4903         case dw_val_class_const_double:
4904           {
4905             unsigned HOST_WIDE_INT first, second;
4906
4907             if (WORDS_BIG_ENDIAN)
4908               {
4909                 first = val2->v.val_double.high;
4910                 second = val2->v.val_double.low;
4911               }
4912             else
4913               {
4914                 first = val2->v.val_double.low;
4915                 second = val2->v.val_double.high;
4916               }
4917             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4918                                  first, NULL);
4919             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4920                                  second, NULL);
4921           }
4922           break;
4923         case dw_val_class_addr:
4924           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4925           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4926           break;
4927         default:
4928           gcc_unreachable ();
4929         }
4930       break;
4931 #else
4932     case DW_OP_const2u:
4933     case DW_OP_const2s:
4934     case DW_OP_const4u:
4935     case DW_OP_const4s:
4936     case DW_OP_const8u:
4937     case DW_OP_const8s:
4938     case DW_OP_skip:
4939     case DW_OP_bra:
4940     case DW_OP_implicit_value:
4941       /* We currently don't make any attempt to make sure these are
4942          aligned properly like we do for the main unwind info, so
4943          don't support emitting things larger than a byte if we're
4944          only doing unwinding.  */
4945       gcc_unreachable ();
4946 #endif
4947     case DW_OP_const1u:
4948     case DW_OP_const1s:
4949       dw2_asm_output_data (1, val1->v.val_int, NULL);
4950       break;
4951     case DW_OP_constu:
4952       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4953       break;
4954     case DW_OP_consts:
4955       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4956       break;
4957     case DW_OP_pick:
4958       dw2_asm_output_data (1, val1->v.val_int, NULL);
4959       break;
4960     case DW_OP_plus_uconst:
4961       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4962       break;
4963     case DW_OP_breg0:
4964     case DW_OP_breg1:
4965     case DW_OP_breg2:
4966     case DW_OP_breg3:
4967     case DW_OP_breg4:
4968     case DW_OP_breg5:
4969     case DW_OP_breg6:
4970     case DW_OP_breg7:
4971     case DW_OP_breg8:
4972     case DW_OP_breg9:
4973     case DW_OP_breg10:
4974     case DW_OP_breg11:
4975     case DW_OP_breg12:
4976     case DW_OP_breg13:
4977     case DW_OP_breg14:
4978     case DW_OP_breg15:
4979     case DW_OP_breg16:
4980     case DW_OP_breg17:
4981     case DW_OP_breg18:
4982     case DW_OP_breg19:
4983     case DW_OP_breg20:
4984     case DW_OP_breg21:
4985     case DW_OP_breg22:
4986     case DW_OP_breg23:
4987     case DW_OP_breg24:
4988     case DW_OP_breg25:
4989     case DW_OP_breg26:
4990     case DW_OP_breg27:
4991     case DW_OP_breg28:
4992     case DW_OP_breg29:
4993     case DW_OP_breg30:
4994     case DW_OP_breg31:
4995       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4996       break;
4997     case DW_OP_regx:
4998       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4999       break;
5000     case DW_OP_fbreg:
5001       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5002       break;
5003     case DW_OP_bregx:
5004       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5005       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5006       break;
5007     case DW_OP_piece:
5008       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5009       break;
5010     case DW_OP_deref_size:
5011     case DW_OP_xderef_size:
5012       dw2_asm_output_data (1, val1->v.val_int, NULL);
5013       break;
5014
5015     case DW_OP_addr:
5016       if (loc->dtprel)
5017         {
5018           if (targetm.asm_out.output_dwarf_dtprel)
5019             {
5020               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5021                                                    DWARF2_ADDR_SIZE,
5022                                                    val1->v.val_addr);
5023               fputc ('\n', asm_out_file);
5024             }
5025           else
5026             gcc_unreachable ();
5027         }
5028       else
5029         {
5030 #ifdef DWARF2_DEBUGGING_INFO
5031           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5032 #else
5033           gcc_unreachable ();
5034 #endif
5035         }
5036       break;
5037
5038     default:
5039       /* Other codes have no operands.  */
5040       break;
5041     }
5042 }
5043
5044 /* Output a sequence of location operations.  */
5045
5046 static void
5047 output_loc_sequence (dw_loc_descr_ref loc)
5048 {
5049   for (; loc != NULL; loc = loc->dw_loc_next)
5050     {
5051       /* Output the opcode.  */
5052       dw2_asm_output_data (1, loc->dw_loc_opc,
5053                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5054
5055       /* Output the operand(s) (if any).  */
5056       output_loc_operands (loc);
5057     }
5058 }
5059
5060 /* Output location description stack opcode's operands (if any).
5061    The output is single bytes on a line, suitable for .cfi_escape.  */
5062
5063 static void
5064 output_loc_operands_raw (dw_loc_descr_ref loc)
5065 {
5066   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5067   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5068
5069   switch (loc->dw_loc_opc)
5070     {
5071     case DW_OP_addr:
5072     case DW_OP_implicit_value:
5073       /* We cannot output addresses in .cfi_escape, only bytes.  */
5074       gcc_unreachable ();
5075
5076     case DW_OP_const1u:
5077     case DW_OP_const1s:
5078     case DW_OP_pick:
5079     case DW_OP_deref_size:
5080     case DW_OP_xderef_size:
5081       fputc (',', asm_out_file);
5082       dw2_asm_output_data_raw (1, val1->v.val_int);
5083       break;
5084
5085     case DW_OP_const2u:
5086     case DW_OP_const2s:
5087       fputc (',', asm_out_file);
5088       dw2_asm_output_data_raw (2, val1->v.val_int);
5089       break;
5090
5091     case DW_OP_const4u:
5092     case DW_OP_const4s:
5093       fputc (',', asm_out_file);
5094       dw2_asm_output_data_raw (4, val1->v.val_int);
5095       break;
5096
5097     case DW_OP_const8u:
5098     case DW_OP_const8s:
5099       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5100       fputc (',', asm_out_file);
5101       dw2_asm_output_data_raw (8, val1->v.val_int);
5102       break;
5103
5104     case DW_OP_skip:
5105     case DW_OP_bra:
5106       {
5107         int offset;
5108
5109         gcc_assert (val1->val_class == dw_val_class_loc);
5110         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5111
5112         fputc (',', asm_out_file);
5113         dw2_asm_output_data_raw (2, offset);
5114       }
5115       break;
5116
5117     case DW_OP_constu:
5118     case DW_OP_plus_uconst:
5119     case DW_OP_regx:
5120     case DW_OP_piece:
5121       fputc (',', asm_out_file);
5122       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5123       break;
5124
5125     case DW_OP_consts:
5126     case DW_OP_breg0:
5127     case DW_OP_breg1:
5128     case DW_OP_breg2:
5129     case DW_OP_breg3:
5130     case DW_OP_breg4:
5131     case DW_OP_breg5:
5132     case DW_OP_breg6:
5133     case DW_OP_breg7:
5134     case DW_OP_breg8:
5135     case DW_OP_breg9:
5136     case DW_OP_breg10:
5137     case DW_OP_breg11:
5138     case DW_OP_breg12:
5139     case DW_OP_breg13:
5140     case DW_OP_breg14:
5141     case DW_OP_breg15:
5142     case DW_OP_breg16:
5143     case DW_OP_breg17:
5144     case DW_OP_breg18:
5145     case DW_OP_breg19:
5146     case DW_OP_breg20:
5147     case DW_OP_breg21:
5148     case DW_OP_breg22:
5149     case DW_OP_breg23:
5150     case DW_OP_breg24:
5151     case DW_OP_breg25:
5152     case DW_OP_breg26:
5153     case DW_OP_breg27:
5154     case DW_OP_breg28:
5155     case DW_OP_breg29:
5156     case DW_OP_breg30:
5157     case DW_OP_breg31:
5158     case DW_OP_fbreg:
5159       fputc (',', asm_out_file);
5160       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5161       break;
5162
5163     case DW_OP_bregx:
5164       fputc (',', asm_out_file);
5165       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5166       fputc (',', asm_out_file);
5167       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5168       break;
5169
5170     default:
5171       /* Other codes have no operands.  */
5172       break;
5173     }
5174 }
5175
5176 static void
5177 output_loc_sequence_raw (dw_loc_descr_ref loc)
5178 {
5179   while (1)
5180     {
5181       /* Output the opcode.  */
5182       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5183       output_loc_operands_raw (loc);
5184
5185       if (!loc->dw_loc_next)
5186         break;
5187       loc = loc->dw_loc_next;
5188
5189       fputc (',', asm_out_file);
5190     }
5191 }
5192
5193 /* This routine will generate the correct assembly data for a location
5194    description based on a cfi entry with a complex address.  */
5195
5196 static void
5197 output_cfa_loc (dw_cfi_ref cfi)
5198 {
5199   dw_loc_descr_ref loc;
5200   unsigned long size;
5201
5202   if (cfi->dw_cfi_opc == DW_CFA_expression)
5203     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5204
5205   /* Output the size of the block.  */
5206   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5207   size = size_of_locs (loc);
5208   dw2_asm_output_data_uleb128 (size, NULL);
5209
5210   /* Now output the operations themselves.  */
5211   output_loc_sequence (loc);
5212 }
5213
5214 /* Similar, but used for .cfi_escape.  */
5215
5216 static void
5217 output_cfa_loc_raw (dw_cfi_ref cfi)
5218 {
5219   dw_loc_descr_ref loc;
5220   unsigned long size;
5221
5222   if (cfi->dw_cfi_opc == DW_CFA_expression)
5223     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5224
5225   /* Output the size of the block.  */
5226   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5227   size = size_of_locs (loc);
5228   dw2_asm_output_data_uleb128_raw (size);
5229   fputc (',', asm_out_file);
5230
5231   /* Now output the operations themselves.  */
5232   output_loc_sequence_raw (loc);
5233 }
5234
5235 /* This function builds a dwarf location descriptor sequence from a
5236    dw_cfa_location, adding the given OFFSET to the result of the
5237    expression.  */
5238
5239 static struct dw_loc_descr_struct *
5240 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5241 {
5242   struct dw_loc_descr_struct *head, *tmp;
5243
5244   offset += cfa->offset;
5245
5246   if (cfa->indirect)
5247     {
5248       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5249       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5250       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5251       add_loc_descr (&head, tmp);
5252       if (offset != 0)
5253         {
5254           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5255           add_loc_descr (&head, tmp);
5256         }
5257     }
5258   else
5259     head = new_reg_loc_descr (cfa->reg, offset);
5260
5261   return head;
5262 }
5263
5264 /* This function builds a dwarf location descriptor sequence for
5265    the address at OFFSET from the CFA when stack is aligned to
5266    ALIGNMENT byte.  */
5267
5268 static struct dw_loc_descr_struct *
5269 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5270 {
5271   struct dw_loc_descr_struct *head;
5272   unsigned int dwarf_fp
5273     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5274
5275  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5276   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5277     {
5278       head = new_reg_loc_descr (dwarf_fp, 0);
5279       add_loc_descr (&head, int_loc_descriptor (alignment));
5280       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5281       loc_descr_plus_const (&head, offset);
5282     }
5283   else
5284     head = new_reg_loc_descr (dwarf_fp, offset);
5285   return head;
5286 }
5287
5288 /* This function fills in aa dw_cfa_location structure from a dwarf location
5289    descriptor sequence.  */
5290
5291 static void
5292 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5293 {
5294   struct dw_loc_descr_struct *ptr;
5295   cfa->offset = 0;
5296   cfa->base_offset = 0;
5297   cfa->indirect = 0;
5298   cfa->reg = -1;
5299
5300   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5301     {
5302       enum dwarf_location_atom op = ptr->dw_loc_opc;
5303
5304       switch (op)
5305         {
5306         case DW_OP_reg0:
5307         case DW_OP_reg1:
5308         case DW_OP_reg2:
5309         case DW_OP_reg3:
5310         case DW_OP_reg4:
5311         case DW_OP_reg5:
5312         case DW_OP_reg6:
5313         case DW_OP_reg7:
5314         case DW_OP_reg8:
5315         case DW_OP_reg9:
5316         case DW_OP_reg10:
5317         case DW_OP_reg11:
5318         case DW_OP_reg12:
5319         case DW_OP_reg13:
5320         case DW_OP_reg14:
5321         case DW_OP_reg15:
5322         case DW_OP_reg16:
5323         case DW_OP_reg17:
5324         case DW_OP_reg18:
5325         case DW_OP_reg19:
5326         case DW_OP_reg20:
5327         case DW_OP_reg21:
5328         case DW_OP_reg22:
5329         case DW_OP_reg23:
5330         case DW_OP_reg24:
5331         case DW_OP_reg25:
5332         case DW_OP_reg26:
5333         case DW_OP_reg27:
5334         case DW_OP_reg28:
5335         case DW_OP_reg29:
5336         case DW_OP_reg30:
5337         case DW_OP_reg31:
5338           cfa->reg = op - DW_OP_reg0;
5339           break;
5340         case DW_OP_regx:
5341           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5342           break;
5343         case DW_OP_breg0:
5344         case DW_OP_breg1:
5345         case DW_OP_breg2:
5346         case DW_OP_breg3:
5347         case DW_OP_breg4:
5348         case DW_OP_breg5:
5349         case DW_OP_breg6:
5350         case DW_OP_breg7:
5351         case DW_OP_breg8:
5352         case DW_OP_breg9:
5353         case DW_OP_breg10:
5354         case DW_OP_breg11:
5355         case DW_OP_breg12:
5356         case DW_OP_breg13:
5357         case DW_OP_breg14:
5358         case DW_OP_breg15:
5359         case DW_OP_breg16:
5360         case DW_OP_breg17:
5361         case DW_OP_breg18:
5362         case DW_OP_breg19:
5363         case DW_OP_breg20:
5364         case DW_OP_breg21:
5365         case DW_OP_breg22:
5366         case DW_OP_breg23:
5367         case DW_OP_breg24:
5368         case DW_OP_breg25:
5369         case DW_OP_breg26:
5370         case DW_OP_breg27:
5371         case DW_OP_breg28:
5372         case DW_OP_breg29:
5373         case DW_OP_breg30:
5374         case DW_OP_breg31:
5375           cfa->reg = op - DW_OP_breg0;
5376           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5377           break;
5378         case DW_OP_bregx:
5379           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5380           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5381           break;
5382         case DW_OP_deref:
5383           cfa->indirect = 1;
5384           break;
5385         case DW_OP_plus_uconst:
5386           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5387           break;
5388         default:
5389           internal_error ("DW_LOC_OP %s not implemented",
5390                           dwarf_stack_op_name (ptr->dw_loc_opc));
5391         }
5392     }
5393 }
5394 #endif /* .debug_frame support */
5395 \f
5396 /* And now, the support for symbolic debugging information.  */
5397 #ifdef DWARF2_DEBUGGING_INFO
5398
5399 /* .debug_str support.  */
5400 static int output_indirect_string (void **, void *);
5401
5402 static void dwarf2out_init (const char *);
5403 static void dwarf2out_finish (const char *);
5404 static void dwarf2out_assembly_start (void);
5405 static void dwarf2out_define (unsigned int, const char *);
5406 static void dwarf2out_undef (unsigned int, const char *);
5407 static void dwarf2out_start_source_file (unsigned, const char *);
5408 static void dwarf2out_end_source_file (unsigned);
5409 static void dwarf2out_begin_block (unsigned, unsigned);
5410 static void dwarf2out_end_block (unsigned, unsigned);
5411 static bool dwarf2out_ignore_block (const_tree);
5412 static void dwarf2out_global_decl (tree);
5413 static void dwarf2out_type_decl (tree, int);
5414 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5415 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5416                                                  dw_die_ref);
5417 static void dwarf2out_abstract_function (tree);
5418 static void dwarf2out_var_location (rtx);
5419 static void dwarf2out_direct_call (tree);
5420 static void dwarf2out_virtual_call_token (tree, int);
5421 static void dwarf2out_copy_call_info (rtx, rtx);
5422 static void dwarf2out_virtual_call (int);
5423 static void dwarf2out_begin_function (tree);
5424 static void dwarf2out_set_name (tree, tree);
5425
5426 /* The debug hooks structure.  */
5427
5428 const struct gcc_debug_hooks dwarf2_debug_hooks =
5429 {
5430   dwarf2out_init,
5431   dwarf2out_finish,
5432   dwarf2out_assembly_start,
5433   dwarf2out_define,
5434   dwarf2out_undef,
5435   dwarf2out_start_source_file,
5436   dwarf2out_end_source_file,
5437   dwarf2out_begin_block,
5438   dwarf2out_end_block,
5439   dwarf2out_ignore_block,
5440   dwarf2out_source_line,
5441   dwarf2out_begin_prologue,
5442   debug_nothing_int_charstar,   /* end_prologue */
5443   dwarf2out_end_epilogue,
5444   dwarf2out_begin_function,
5445   debug_nothing_int,            /* end_function */
5446   dwarf2out_decl,               /* function_decl */
5447   dwarf2out_global_decl,
5448   dwarf2out_type_decl,          /* type_decl */
5449   dwarf2out_imported_module_or_decl,
5450   debug_nothing_tree,           /* deferred_inline_function */
5451   /* The DWARF 2 backend tries to reduce debugging bloat by not
5452      emitting the abstract description of inline functions until
5453      something tries to reference them.  */
5454   dwarf2out_abstract_function,  /* outlining_inline_function */
5455   debug_nothing_rtx,            /* label */
5456   debug_nothing_int,            /* handle_pch */
5457   dwarf2out_var_location,
5458   dwarf2out_switch_text_section,
5459   dwarf2out_direct_call,
5460   dwarf2out_virtual_call_token,
5461   dwarf2out_copy_call_info,
5462   dwarf2out_virtual_call,
5463   dwarf2out_set_name,
5464   1                             /* start_end_main_source_file */
5465 };
5466 #endif
5467 \f
5468 /* NOTE: In the comments in this file, many references are made to
5469    "Debugging Information Entries".  This term is abbreviated as `DIE'
5470    throughout the remainder of this file.  */
5471
5472 /* An internal representation of the DWARF output is built, and then
5473    walked to generate the DWARF debugging info.  The walk of the internal
5474    representation is done after the entire program has been compiled.
5475    The types below are used to describe the internal representation.  */
5476
5477 /* Various DIE's use offsets relative to the beginning of the
5478    .debug_info section to refer to each other.  */
5479
5480 typedef long int dw_offset;
5481
5482 /* Define typedefs here to avoid circular dependencies.  */
5483
5484 typedef struct dw_attr_struct *dw_attr_ref;
5485 typedef struct dw_line_info_struct *dw_line_info_ref;
5486 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5487 typedef struct pubname_struct *pubname_ref;
5488 typedef struct dw_ranges_struct *dw_ranges_ref;
5489 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5490 typedef struct comdat_type_struct *comdat_type_node_ref;
5491
5492 /* Each entry in the line_info_table maintains the file and
5493    line number associated with the label generated for that
5494    entry.  The label gives the PC value associated with
5495    the line number entry.  */
5496
5497 typedef struct GTY(()) dw_line_info_struct {
5498   unsigned long dw_file_num;
5499   unsigned long dw_line_num;
5500 }
5501 dw_line_info_entry;
5502
5503 /* Line information for functions in separate sections; each one gets its
5504    own sequence.  */
5505 typedef struct GTY(()) dw_separate_line_info_struct {
5506   unsigned long dw_file_num;
5507   unsigned long dw_line_num;
5508   unsigned long function;
5509 }
5510 dw_separate_line_info_entry;
5511
5512 /* Each DIE attribute has a field specifying the attribute kind,
5513    a link to the next attribute in the chain, and an attribute value.
5514    Attributes are typically linked below the DIE they modify.  */
5515
5516 typedef struct GTY(()) dw_attr_struct {
5517   enum dwarf_attribute dw_attr;
5518   dw_val_node dw_attr_val;
5519 }
5520 dw_attr_node;
5521
5522 DEF_VEC_O(dw_attr_node);
5523 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5524
5525 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5526    The children of each node form a circular list linked by
5527    die_sib.  die_child points to the node *before* the "first" child node.  */
5528
5529 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5530   enum dwarf_tag die_tag;
5531   union die_symbol_or_type_node
5532     {
5533       char * GTY ((tag ("0"))) die_symbol;
5534       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5535     }
5536   GTY ((desc ("dwarf_version >= 4"))) die_id;
5537   VEC(dw_attr_node,gc) * die_attr;
5538   dw_die_ref die_parent;
5539   dw_die_ref die_child;
5540   dw_die_ref die_sib;
5541   dw_die_ref die_definition; /* ref from a specification to its definition */
5542   dw_offset die_offset;
5543   unsigned long die_abbrev;
5544   int die_mark;
5545   /* Die is used and must not be pruned as unused.  */
5546   int die_perennial_p;
5547   unsigned int decl_id;
5548 }
5549 die_node;
5550
5551 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5552 #define FOR_EACH_CHILD(die, c, expr) do {       \
5553   c = die->die_child;                           \
5554   if (c) do {                                   \
5555     c = c->die_sib;                             \
5556     expr;                                       \
5557   } while (c != die->die_child);                \
5558 } while (0)
5559
5560 /* The pubname structure */
5561
5562 typedef struct GTY(()) pubname_struct {
5563   dw_die_ref die;
5564   const char *name;
5565 }
5566 pubname_entry;
5567
5568 DEF_VEC_O(pubname_entry);
5569 DEF_VEC_ALLOC_O(pubname_entry, gc);
5570
5571 struct GTY(()) dw_ranges_struct {
5572   /* If this is positive, it's a block number, otherwise it's a
5573      bitwise-negated index into dw_ranges_by_label.  */
5574   int num;
5575 };
5576
5577 struct GTY(()) dw_ranges_by_label_struct {
5578   const char *begin;
5579   const char *end;
5580 };
5581
5582 /* The comdat type node structure.  */
5583 typedef struct GTY(()) comdat_type_struct
5584 {
5585   dw_die_ref root_die;
5586   dw_die_ref type_die;
5587   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5588   struct comdat_type_struct *next;
5589 }
5590 comdat_type_node;
5591
5592 /* The limbo die list structure.  */
5593 typedef struct GTY(()) limbo_die_struct {
5594   dw_die_ref die;
5595   tree created_for;
5596   struct limbo_die_struct *next;
5597 }
5598 limbo_die_node;
5599
5600 typedef struct GTY(()) skeleton_chain_struct
5601 {
5602   dw_die_ref old_die;
5603   dw_die_ref new_die;
5604   struct skeleton_chain_struct *parent;
5605 }
5606 skeleton_chain_node;
5607
5608 /* How to start an assembler comment.  */
5609 #ifndef ASM_COMMENT_START
5610 #define ASM_COMMENT_START ";#"
5611 #endif
5612
5613 /* Define a macro which returns nonzero for a TYPE_DECL which was
5614    implicitly generated for a tagged type.
5615
5616    Note that unlike the gcc front end (which generates a NULL named
5617    TYPE_DECL node for each complete tagged type, each array type, and
5618    each function type node created) the g++ front end generates a
5619    _named_ TYPE_DECL node for each tagged type node created.
5620    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5621    generate a DW_TAG_typedef DIE for them.  */
5622
5623 #define TYPE_DECL_IS_STUB(decl)                         \
5624   (DECL_NAME (decl) == NULL_TREE                        \
5625    || (DECL_ARTIFICIAL (decl)                           \
5626        && is_tagged_type (TREE_TYPE (decl))             \
5627        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5628            /* This is necessary for stub decls that     \
5629               appear in nested inline functions.  */    \
5630            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5631                && (decl_ultimate_origin (decl)          \
5632                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5633
5634 /* Information concerning the compilation unit's programming
5635    language, and compiler version.  */
5636
5637 /* Fixed size portion of the DWARF compilation unit header.  */
5638 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5639   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5640
5641 /* Fixed size portion of the DWARF comdat type unit header.  */
5642 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5643   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5644    + DWARF_OFFSET_SIZE)
5645
5646 /* Fixed size portion of public names info.  */
5647 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5648
5649 /* Fixed size portion of the address range info.  */
5650 #define DWARF_ARANGES_HEADER_SIZE                                       \
5651   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5652                 DWARF2_ADDR_SIZE * 2)                                   \
5653    - DWARF_INITIAL_LENGTH_SIZE)
5654
5655 /* Size of padding portion in the address range info.  It must be
5656    aligned to twice the pointer size.  */
5657 #define DWARF_ARANGES_PAD_SIZE \
5658   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5659                 DWARF2_ADDR_SIZE * 2)                              \
5660    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5661
5662 /* Use assembler line directives if available.  */
5663 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5664 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5665 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5666 #else
5667 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5668 #endif
5669 #endif
5670
5671 /* Minimum line offset in a special line info. opcode.
5672    This value was chosen to give a reasonable range of values.  */
5673 #define DWARF_LINE_BASE  -10
5674
5675 /* First special line opcode - leave room for the standard opcodes.  */
5676 #define DWARF_LINE_OPCODE_BASE  10
5677
5678 /* Range of line offsets in a special line info. opcode.  */
5679 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5680
5681 /* Flag that indicates the initial value of the is_stmt_start flag.
5682    In the present implementation, we do not mark any lines as
5683    the beginning of a source statement, because that information
5684    is not made available by the GCC front-end.  */
5685 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5686
5687 #ifdef DWARF2_DEBUGGING_INFO
5688 /* This location is used by calc_die_sizes() to keep track
5689    the offset of each DIE within the .debug_info section.  */
5690 static unsigned long next_die_offset;
5691 #endif
5692
5693 /* Record the root of the DIE's built for the current compilation unit.  */
5694 static GTY(()) dw_die_ref comp_unit_die;
5695
5696 /* A list of type DIEs that have been separated into comdat sections.  */
5697 static GTY(()) comdat_type_node *comdat_type_list;
5698
5699 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5700 static GTY(()) limbo_die_node *limbo_die_list;
5701
5702 /* A list of DIEs for which we may have to generate
5703    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5704    set.  */
5705 static GTY(()) limbo_die_node *deferred_asm_name;
5706
5707 /* Filenames referenced by this compilation unit.  */
5708 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5709
5710 /* A hash table of references to DIE's that describe declarations.
5711    The key is a DECL_UID() which is a unique number identifying each decl.  */
5712 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5713
5714 /* A hash table of references to DIE's that describe COMMON blocks.
5715    The key is DECL_UID() ^ die_parent.  */
5716 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5717
5718 typedef struct GTY(()) die_arg_entry_struct {
5719     dw_die_ref die;
5720     tree arg;
5721 } die_arg_entry;
5722
5723 DEF_VEC_O(die_arg_entry);
5724 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5725
5726 /* Node of the variable location list.  */
5727 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5728   rtx GTY (()) var_loc_note;
5729   const char * GTY (()) label;
5730   const char * GTY (()) section_label;
5731   struct var_loc_node * GTY (()) next;
5732 };
5733
5734 /* Variable location list.  */
5735 struct GTY (()) var_loc_list_def {
5736   struct var_loc_node * GTY (()) first;
5737
5738   /* Do not mark the last element of the chained list because
5739      it is marked through the chain.  */
5740   struct var_loc_node * GTY ((skip ("%h"))) last;
5741
5742   /* DECL_UID of the variable decl.  */
5743   unsigned int decl_id;
5744 };
5745 typedef struct var_loc_list_def var_loc_list;
5746
5747
5748 /* Table of decl location linked lists.  */
5749 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5750
5751 /* A pointer to the base of a list of references to DIE's that
5752    are uniquely identified by their tag, presence/absence of
5753    children DIE's, and list of attribute/value pairs.  */
5754 static GTY((length ("abbrev_die_table_allocated")))
5755   dw_die_ref *abbrev_die_table;
5756
5757 /* Number of elements currently allocated for abbrev_die_table.  */
5758 static GTY(()) unsigned abbrev_die_table_allocated;
5759
5760 /* Number of elements in type_die_table currently in use.  */
5761 static GTY(()) unsigned abbrev_die_table_in_use;
5762
5763 /* Size (in elements) of increments by which we may expand the
5764    abbrev_die_table.  */
5765 #define ABBREV_DIE_TABLE_INCREMENT 256
5766
5767 /* A pointer to the base of a table that contains line information
5768    for each source code line in .text in the compilation unit.  */
5769 static GTY((length ("line_info_table_allocated")))
5770      dw_line_info_ref line_info_table;
5771
5772 /* Number of elements currently allocated for line_info_table.  */
5773 static GTY(()) unsigned line_info_table_allocated;
5774
5775 /* Number of elements in line_info_table currently in use.  */
5776 static GTY(()) unsigned line_info_table_in_use;
5777
5778 /* A pointer to the base of a table that contains line information
5779    for each source code line outside of .text in the compilation unit.  */
5780 static GTY ((length ("separate_line_info_table_allocated")))
5781      dw_separate_line_info_ref separate_line_info_table;
5782
5783 /* Number of elements currently allocated for separate_line_info_table.  */
5784 static GTY(()) unsigned separate_line_info_table_allocated;
5785
5786 /* Number of elements in separate_line_info_table currently in use.  */
5787 static GTY(()) unsigned separate_line_info_table_in_use;
5788
5789 /* Size (in elements) of increments by which we may expand the
5790    line_info_table.  */
5791 #define LINE_INFO_TABLE_INCREMENT 1024
5792
5793 /* A pointer to the base of a table that contains a list of publicly
5794    accessible names.  */
5795 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5796
5797 /* A pointer to the base of a table that contains a list of publicly
5798    accessible types.  */
5799 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5800
5801 /* Array of dies for which we should generate .debug_arange info.  */
5802 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5803
5804 /* Number of elements currently allocated for arange_table.  */
5805 static GTY(()) unsigned arange_table_allocated;
5806
5807 /* Number of elements in arange_table currently in use.  */
5808 static GTY(()) unsigned arange_table_in_use;
5809
5810 /* Size (in elements) of increments by which we may expand the
5811    arange_table.  */
5812 #define ARANGE_TABLE_INCREMENT 64
5813
5814 /* Array of dies for which we should generate .debug_ranges info.  */
5815 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5816
5817 /* Number of elements currently allocated for ranges_table.  */
5818 static GTY(()) unsigned ranges_table_allocated;
5819
5820 /* Number of elements in ranges_table currently in use.  */
5821 static GTY(()) unsigned ranges_table_in_use;
5822
5823 /* Array of pairs of labels referenced in ranges_table.  */
5824 static GTY ((length ("ranges_by_label_allocated")))
5825      dw_ranges_by_label_ref ranges_by_label;
5826
5827 /* Number of elements currently allocated for ranges_by_label.  */
5828 static GTY(()) unsigned ranges_by_label_allocated;
5829
5830 /* Number of elements in ranges_by_label currently in use.  */
5831 static GTY(()) unsigned ranges_by_label_in_use;
5832
5833 /* Size (in elements) of increments by which we may expand the
5834    ranges_table.  */
5835 #define RANGES_TABLE_INCREMENT 64
5836
5837 /* Whether we have location lists that need outputting */
5838 static GTY(()) bool have_location_lists;
5839
5840 /* Unique label counter.  */
5841 static GTY(()) unsigned int loclabel_num;
5842
5843 /* Unique label counter for point-of-call tables.  */
5844 static GTY(()) unsigned int poc_label_num;
5845
5846 /* The direct call table structure.  */
5847
5848 typedef struct GTY(()) dcall_struct {
5849   unsigned int poc_label_num;
5850   tree poc_decl;
5851   dw_die_ref targ_die;
5852 }
5853 dcall_entry;
5854
5855 DEF_VEC_O(dcall_entry);
5856 DEF_VEC_ALLOC_O(dcall_entry, gc);
5857
5858 /* The virtual call table structure.  */
5859
5860 typedef struct GTY(()) vcall_struct {
5861   unsigned int poc_label_num;
5862   unsigned int vtable_slot;
5863 }
5864 vcall_entry;
5865
5866 DEF_VEC_O(vcall_entry);
5867 DEF_VEC_ALLOC_O(vcall_entry, gc);
5868
5869 /* Pointers to the direct and virtual call tables.  */
5870 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
5871 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
5872
5873 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
5874
5875 struct GTY (()) vcall_insn {
5876   int insn_uid;
5877   unsigned int vtable_slot;
5878 };
5879
5880 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
5881
5882 #ifdef DWARF2_DEBUGGING_INFO
5883 /* Record whether the function being analyzed contains inlined functions.  */
5884 static int current_function_has_inlines;
5885 #endif
5886 #if 0 && defined (MIPS_DEBUGGING_INFO)
5887 static int comp_unit_has_inlines;
5888 #endif
5889
5890 /* The last file entry emitted by maybe_emit_file().  */
5891 static GTY(()) struct dwarf_file_data * last_emitted_file;
5892
5893 /* Number of internal labels generated by gen_internal_sym().  */
5894 static GTY(()) int label_num;
5895
5896 /* Cached result of previous call to lookup_filename.  */
5897 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5898
5899 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5900
5901 #ifdef DWARF2_DEBUGGING_INFO
5902
5903 /* Offset from the "steady-state frame pointer" to the frame base,
5904    within the current function.  */
5905 static HOST_WIDE_INT frame_pointer_fb_offset;
5906
5907 /* Forward declarations for functions defined in this file.  */
5908
5909 static int is_pseudo_reg (const_rtx);
5910 static tree type_main_variant (tree);
5911 static int is_tagged_type (const_tree);
5912 static const char *dwarf_tag_name (unsigned);
5913 static const char *dwarf_attr_name (unsigned);
5914 static const char *dwarf_form_name (unsigned);
5915 static tree decl_ultimate_origin (const_tree);
5916 static tree decl_class_context (tree);
5917 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5918 static inline enum dw_val_class AT_class (dw_attr_ref);
5919 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5920 static inline unsigned AT_flag (dw_attr_ref);
5921 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5922 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5923 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5924 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5925 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5926                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5927 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5928                                unsigned int, unsigned char *);
5929 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
5930 static hashval_t debug_str_do_hash (const void *);
5931 static int debug_str_eq (const void *, const void *);
5932 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5933 static inline const char *AT_string (dw_attr_ref);
5934 static enum dwarf_form AT_string_form (dw_attr_ref);
5935 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5936 static void add_AT_specification (dw_die_ref, dw_die_ref);
5937 static inline dw_die_ref AT_ref (dw_attr_ref);
5938 static inline int AT_ref_external (dw_attr_ref);
5939 static inline void set_AT_ref_external (dw_attr_ref, int);
5940 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5941 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5942 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5943 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5944                              dw_loc_list_ref);
5945 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5946 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5947 static inline rtx AT_addr (dw_attr_ref);
5948 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5949 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5950 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5951 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5952                            unsigned HOST_WIDE_INT);
5953 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5954                                unsigned long);
5955 static inline const char *AT_lbl (dw_attr_ref);
5956 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5957 static const char *get_AT_low_pc (dw_die_ref);
5958 static const char *get_AT_hi_pc (dw_die_ref);
5959 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5960 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5961 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5962 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5963 static bool is_c_family (void);
5964 static bool is_cxx (void);
5965 static bool is_java (void);
5966 static bool is_fortran (void);
5967 static bool is_ada (void);
5968 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5969 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5970 static void add_child_die (dw_die_ref, dw_die_ref);
5971 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5972 static dw_die_ref lookup_type_die (tree);
5973 static void equate_type_number_to_die (tree, dw_die_ref);
5974 static hashval_t decl_die_table_hash (const void *);
5975 static int decl_die_table_eq (const void *, const void *);
5976 static dw_die_ref lookup_decl_die (tree);
5977 static hashval_t common_block_die_table_hash (const void *);
5978 static int common_block_die_table_eq (const void *, const void *);
5979 static hashval_t decl_loc_table_hash (const void *);
5980 static int decl_loc_table_eq (const void *, const void *);
5981 static var_loc_list *lookup_decl_loc (const_tree);
5982 static void equate_decl_number_to_die (tree, dw_die_ref);
5983 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5984 static void print_spaces (FILE *);
5985 static void print_die (dw_die_ref, FILE *);
5986 static void print_dwarf_line_table (FILE *);
5987 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5988 static dw_die_ref pop_compile_unit (dw_die_ref);
5989 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5990 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5991 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5992 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
5993 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
5994 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
5995 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
5996                                    struct md5_ctx *, int *);
5997 struct checksum_attributes;
5998 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
5999 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6000 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6001 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6002 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6003 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6004 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6005 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6006 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6007 static void compute_section_prefix (dw_die_ref);
6008 static int is_type_die (dw_die_ref);
6009 static int is_comdat_die (dw_die_ref);
6010 static int is_symbol_die (dw_die_ref);
6011 static void assign_symbol_names (dw_die_ref);
6012 static void break_out_includes (dw_die_ref);
6013 static int is_declaration_die (dw_die_ref);
6014 static int should_move_die_to_comdat (dw_die_ref);
6015 static dw_die_ref clone_as_declaration (dw_die_ref);
6016 static dw_die_ref clone_die (dw_die_ref);
6017 static dw_die_ref clone_tree (dw_die_ref);
6018 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6019 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6020 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6021 static dw_die_ref generate_skeleton (dw_die_ref);
6022 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6023                                                          dw_die_ref);
6024 static void break_out_comdat_types (dw_die_ref);
6025 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6026 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6027 static void copy_decls_for_unworthy_types (dw_die_ref);
6028
6029 static hashval_t htab_cu_hash (const void *);
6030 static int htab_cu_eq (const void *, const void *);
6031 static void htab_cu_del (void *);
6032 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6033 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6034 static void add_sibling_attributes (dw_die_ref);
6035 static void build_abbrev_table (dw_die_ref);
6036 static void output_location_lists (dw_die_ref);
6037 static int constant_size (unsigned HOST_WIDE_INT);
6038 static unsigned long size_of_die (dw_die_ref);
6039 static void calc_die_sizes (dw_die_ref);
6040 static void mark_dies (dw_die_ref);
6041 static void unmark_dies (dw_die_ref);
6042 static void unmark_all_dies (dw_die_ref);
6043 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6044 static unsigned long size_of_aranges (void);
6045 static enum dwarf_form value_format (dw_attr_ref);
6046 static void output_value_format (dw_attr_ref);
6047 static void output_abbrev_section (void);
6048 static void output_die_symbol (dw_die_ref);
6049 static void output_die (dw_die_ref);
6050 static void output_compilation_unit_header (void);
6051 static void output_comp_unit (dw_die_ref, int);
6052 static void output_comdat_type_unit (comdat_type_node *);
6053 static const char *dwarf2_name (tree, int);
6054 static void add_pubname (tree, dw_die_ref);
6055 static void add_pubname_string (const char *, dw_die_ref);
6056 static void add_pubtype (tree, dw_die_ref);
6057 static void output_pubnames (VEC (pubname_entry,gc) *);
6058 static void add_arange (tree, dw_die_ref);
6059 static void output_aranges (void);
6060 static unsigned int add_ranges_num (int);
6061 static unsigned int add_ranges (const_tree);
6062 static unsigned int add_ranges_by_labels (const char *, const char *);
6063 static void output_ranges (void);
6064 static void output_line_info (void);
6065 static void output_file_names (void);
6066 static dw_die_ref base_type_die (tree);
6067 static int is_base_type (tree);
6068 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6069 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6070 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6071 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6072 static int type_is_enum (const_tree);
6073 static unsigned int dbx_reg_number (const_rtx);
6074 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6075 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6076 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6077                                                 enum var_init_status);
6078 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6079                                                      enum var_init_status);
6080 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6081                                          enum var_init_status);
6082 static int is_based_loc (const_rtx);
6083 static int resolve_one_addr (rtx *, void *);
6084 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6085                                             enum var_init_status);
6086 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6087                                                enum var_init_status);
6088 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6089                                         enum var_init_status);
6090 static dw_loc_list_ref loc_list_from_tree (tree, int);
6091 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6092 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6093 static tree field_type (const_tree);
6094 static unsigned int simple_type_align_in_bits (const_tree);
6095 static unsigned int simple_decl_align_in_bits (const_tree);
6096 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6097 static HOST_WIDE_INT field_byte_offset (const_tree);
6098 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6099                                          dw_loc_list_ref);
6100 static void add_data_member_location_attribute (dw_die_ref, tree);
6101 static bool add_const_value_attribute (dw_die_ref, rtx);
6102 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6103 static void insert_float (const_rtx, unsigned char *);
6104 static rtx rtl_for_decl_location (tree);
6105 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6106                                                    enum dwarf_attribute);
6107 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6108 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6109 static void add_name_attribute (dw_die_ref, const char *);
6110 static void add_comp_dir_attribute (dw_die_ref);
6111 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6112 static void add_subscript_info (dw_die_ref, tree, bool);
6113 static void add_byte_size_attribute (dw_die_ref, tree);
6114 static void add_bit_offset_attribute (dw_die_ref, tree);
6115 static void add_bit_size_attribute (dw_die_ref, tree);
6116 static void add_prototyped_attribute (dw_die_ref, tree);
6117 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6118 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6119 static void add_src_coords_attributes (dw_die_ref, tree);
6120 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6121 static void push_decl_scope (tree);
6122 static void pop_decl_scope (void);
6123 static dw_die_ref scope_die_for (tree, dw_die_ref);
6124 static inline int local_scope_p (dw_die_ref);
6125 static inline int class_scope_p (dw_die_ref);
6126 static inline int class_or_namespace_scope_p (dw_die_ref);
6127 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6128 static void add_calling_convention_attribute (dw_die_ref, tree);
6129 static const char *type_tag (const_tree);
6130 static tree member_declared_type (const_tree);
6131 #if 0
6132 static const char *decl_start_label (tree);
6133 #endif
6134 static void gen_array_type_die (tree, dw_die_ref);
6135 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6136 #if 0
6137 static void gen_entry_point_die (tree, dw_die_ref);
6138 #endif
6139 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6140 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6141 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6142 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6143 static void gen_formal_types_die (tree, dw_die_ref);
6144 static void gen_subprogram_die (tree, dw_die_ref);
6145 static void gen_variable_die (tree, tree, dw_die_ref);
6146 static void gen_const_die (tree, dw_die_ref);
6147 static void gen_label_die (tree, dw_die_ref);
6148 static void gen_lexical_block_die (tree, dw_die_ref, int);
6149 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6150 static void gen_field_die (tree, dw_die_ref);
6151 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6152 static dw_die_ref gen_compile_unit_die (const char *);
6153 static void gen_inheritance_die (tree, tree, dw_die_ref);
6154 static void gen_member_die (tree, dw_die_ref);
6155 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6156                                                 enum debug_info_usage);
6157 static void gen_subroutine_type_die (tree, dw_die_ref);
6158 static void gen_typedef_die (tree, dw_die_ref);
6159 static void gen_type_die (tree, dw_die_ref);
6160 static void gen_block_die (tree, dw_die_ref, int);
6161 static void decls_for_scope (tree, dw_die_ref, int);
6162 static int is_redundant_typedef (const_tree);
6163 static inline dw_die_ref get_context_die (tree);
6164 static void gen_namespace_die (tree, dw_die_ref);
6165 static void gen_decl_die (tree, tree, dw_die_ref);
6166 static dw_die_ref force_decl_die (tree);
6167 static dw_die_ref force_type_die (tree);
6168 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6169 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6170 static struct dwarf_file_data * lookup_filename (const char *);
6171 static void retry_incomplete_types (void);
6172 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6173 static void gen_generic_params_dies (tree);
6174 static void splice_child_die (dw_die_ref, dw_die_ref);
6175 static int file_info_cmp (const void *, const void *);
6176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6177                                      const char *, const char *, unsigned);
6178 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6179                                        const char *, const char *,
6180                                        const char *);
6181 static void output_loc_list (dw_loc_list_ref);
6182 static char *gen_internal_sym (const char *);
6183
6184 static void prune_unmark_dies (dw_die_ref);
6185 static void prune_unused_types_mark (dw_die_ref, int);
6186 static void prune_unused_types_walk (dw_die_ref);
6187 static void prune_unused_types_walk_attribs (dw_die_ref);
6188 static void prune_unused_types_prune (dw_die_ref);
6189 static void prune_unused_types (void);
6190 static int maybe_emit_file (struct dwarf_file_data *fd);
6191 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6192 static void gen_remaining_tmpl_value_param_die_attribute (void);
6193
6194 /* Section names used to hold DWARF debugging information.  */
6195 #ifndef DEBUG_INFO_SECTION
6196 #define DEBUG_INFO_SECTION      ".debug_info"
6197 #endif
6198 #ifndef DEBUG_ABBREV_SECTION
6199 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6200 #endif
6201 #ifndef DEBUG_ARANGES_SECTION
6202 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6203 #endif
6204 #ifndef DEBUG_MACINFO_SECTION
6205 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6206 #endif
6207 #ifndef DEBUG_LINE_SECTION
6208 #define DEBUG_LINE_SECTION      ".debug_line"
6209 #endif
6210 #ifndef DEBUG_LOC_SECTION
6211 #define DEBUG_LOC_SECTION       ".debug_loc"
6212 #endif
6213 #ifndef DEBUG_PUBNAMES_SECTION
6214 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6215 #endif
6216 #ifndef DEBUG_PUBTYPES_SECTION
6217 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6218 #endif
6219 #ifndef DEBUG_DCALL_SECTION
6220 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6221 #endif
6222 #ifndef DEBUG_VCALL_SECTION
6223 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6224 #endif
6225 #ifndef DEBUG_STR_SECTION
6226 #define DEBUG_STR_SECTION       ".debug_str"
6227 #endif
6228 #ifndef DEBUG_RANGES_SECTION
6229 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6230 #endif
6231
6232 /* Standard ELF section names for compiled code and data.  */
6233 #ifndef TEXT_SECTION_NAME
6234 #define TEXT_SECTION_NAME       ".text"
6235 #endif
6236
6237 /* Section flags for .debug_str section.  */
6238 #define DEBUG_STR_SECTION_FLAGS \
6239   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6240    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6241    : SECTION_DEBUG)
6242
6243 /* Labels we insert at beginning sections we can reference instead of
6244    the section names themselves.  */
6245
6246 #ifndef TEXT_SECTION_LABEL
6247 #define TEXT_SECTION_LABEL              "Ltext"
6248 #endif
6249 #ifndef COLD_TEXT_SECTION_LABEL
6250 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6251 #endif
6252 #ifndef DEBUG_LINE_SECTION_LABEL
6253 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6254 #endif
6255 #ifndef DEBUG_INFO_SECTION_LABEL
6256 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6257 #endif
6258 #ifndef DEBUG_ABBREV_SECTION_LABEL
6259 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6260 #endif
6261 #ifndef DEBUG_LOC_SECTION_LABEL
6262 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6263 #endif
6264 #ifndef DEBUG_RANGES_SECTION_LABEL
6265 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6266 #endif
6267 #ifndef DEBUG_MACINFO_SECTION_LABEL
6268 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6269 #endif
6270
6271 /* Definitions of defaults for formats and names of various special
6272    (artificial) labels which may be generated within this file (when the -g
6273    options is used and DWARF2_DEBUGGING_INFO is in effect.
6274    If necessary, these may be overridden from within the tm.h file, but
6275    typically, overriding these defaults is unnecessary.  */
6276
6277 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6278 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6279 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6280 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6281 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6282 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6283 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6284 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6285 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6286 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6287
6288 #ifndef TEXT_END_LABEL
6289 #define TEXT_END_LABEL          "Letext"
6290 #endif
6291 #ifndef COLD_END_LABEL
6292 #define COLD_END_LABEL          "Letext_cold"
6293 #endif
6294 #ifndef BLOCK_BEGIN_LABEL
6295 #define BLOCK_BEGIN_LABEL       "LBB"
6296 #endif
6297 #ifndef BLOCK_END_LABEL
6298 #define BLOCK_END_LABEL         "LBE"
6299 #endif
6300 #ifndef LINE_CODE_LABEL
6301 #define LINE_CODE_LABEL         "LM"
6302 #endif
6303 #ifndef SEPARATE_LINE_CODE_LABEL
6304 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6305 #endif
6306
6307 \f
6308 /* We allow a language front-end to designate a function that is to be
6309    called to "demangle" any name before it is put into a DIE.  */
6310
6311 static const char *(*demangle_name_func) (const char *);
6312
6313 void
6314 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6315 {
6316   demangle_name_func = func;
6317 }
6318
6319 /* Test if rtl node points to a pseudo register.  */
6320
6321 static inline int
6322 is_pseudo_reg (const_rtx rtl)
6323 {
6324   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6325           || (GET_CODE (rtl) == SUBREG
6326               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6327 }
6328
6329 /* Return a reference to a type, with its const and volatile qualifiers
6330    removed.  */
6331
6332 static inline tree
6333 type_main_variant (tree type)
6334 {
6335   type = TYPE_MAIN_VARIANT (type);
6336
6337   /* ??? There really should be only one main variant among any group of
6338      variants of a given type (and all of the MAIN_VARIANT values for all
6339      members of the group should point to that one type) but sometimes the C
6340      front-end messes this up for array types, so we work around that bug
6341      here.  */
6342   if (TREE_CODE (type) == ARRAY_TYPE)
6343     while (type != TYPE_MAIN_VARIANT (type))
6344       type = TYPE_MAIN_VARIANT (type);
6345
6346   return type;
6347 }
6348
6349 /* Return nonzero if the given type node represents a tagged type.  */
6350
6351 static inline int
6352 is_tagged_type (const_tree type)
6353 {
6354   enum tree_code code = TREE_CODE (type);
6355
6356   return (code == RECORD_TYPE || code == UNION_TYPE
6357           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6358 }
6359
6360 /* Convert a DIE tag into its string name.  */
6361
6362 static const char *
6363 dwarf_tag_name (unsigned int tag)
6364 {
6365   switch (tag)
6366     {
6367     case DW_TAG_padding:
6368       return "DW_TAG_padding";
6369     case DW_TAG_array_type:
6370       return "DW_TAG_array_type";
6371     case DW_TAG_class_type:
6372       return "DW_TAG_class_type";
6373     case DW_TAG_entry_point:
6374       return "DW_TAG_entry_point";
6375     case DW_TAG_enumeration_type:
6376       return "DW_TAG_enumeration_type";
6377     case DW_TAG_formal_parameter:
6378       return "DW_TAG_formal_parameter";
6379     case DW_TAG_imported_declaration:
6380       return "DW_TAG_imported_declaration";
6381     case DW_TAG_label:
6382       return "DW_TAG_label";
6383     case DW_TAG_lexical_block:
6384       return "DW_TAG_lexical_block";
6385     case DW_TAG_member:
6386       return "DW_TAG_member";
6387     case DW_TAG_pointer_type:
6388       return "DW_TAG_pointer_type";
6389     case DW_TAG_reference_type:
6390       return "DW_TAG_reference_type";
6391     case DW_TAG_compile_unit:
6392       return "DW_TAG_compile_unit";
6393     case DW_TAG_string_type:
6394       return "DW_TAG_string_type";
6395     case DW_TAG_structure_type:
6396       return "DW_TAG_structure_type";
6397     case DW_TAG_subroutine_type:
6398       return "DW_TAG_subroutine_type";
6399     case DW_TAG_typedef:
6400       return "DW_TAG_typedef";
6401     case DW_TAG_union_type:
6402       return "DW_TAG_union_type";
6403     case DW_TAG_unspecified_parameters:
6404       return "DW_TAG_unspecified_parameters";
6405     case DW_TAG_variant:
6406       return "DW_TAG_variant";
6407     case DW_TAG_common_block:
6408       return "DW_TAG_common_block";
6409     case DW_TAG_common_inclusion:
6410       return "DW_TAG_common_inclusion";
6411     case DW_TAG_inheritance:
6412       return "DW_TAG_inheritance";
6413     case DW_TAG_inlined_subroutine:
6414       return "DW_TAG_inlined_subroutine";
6415     case DW_TAG_module:
6416       return "DW_TAG_module";
6417     case DW_TAG_ptr_to_member_type:
6418       return "DW_TAG_ptr_to_member_type";
6419     case DW_TAG_set_type:
6420       return "DW_TAG_set_type";
6421     case DW_TAG_subrange_type:
6422       return "DW_TAG_subrange_type";
6423     case DW_TAG_with_stmt:
6424       return "DW_TAG_with_stmt";
6425     case DW_TAG_access_declaration:
6426       return "DW_TAG_access_declaration";
6427     case DW_TAG_base_type:
6428       return "DW_TAG_base_type";
6429     case DW_TAG_catch_block:
6430       return "DW_TAG_catch_block";
6431     case DW_TAG_const_type:
6432       return "DW_TAG_const_type";
6433     case DW_TAG_constant:
6434       return "DW_TAG_constant";
6435     case DW_TAG_enumerator:
6436       return "DW_TAG_enumerator";
6437     case DW_TAG_file_type:
6438       return "DW_TAG_file_type";
6439     case DW_TAG_friend:
6440       return "DW_TAG_friend";
6441     case DW_TAG_namelist:
6442       return "DW_TAG_namelist";
6443     case DW_TAG_namelist_item:
6444       return "DW_TAG_namelist_item";
6445     case DW_TAG_packed_type:
6446       return "DW_TAG_packed_type";
6447     case DW_TAG_subprogram:
6448       return "DW_TAG_subprogram";
6449     case DW_TAG_template_type_param:
6450       return "DW_TAG_template_type_param";
6451     case DW_TAG_template_value_param:
6452       return "DW_TAG_template_value_param";
6453     case DW_TAG_thrown_type:
6454       return "DW_TAG_thrown_type";
6455     case DW_TAG_try_block:
6456       return "DW_TAG_try_block";
6457     case DW_TAG_variant_part:
6458       return "DW_TAG_variant_part";
6459     case DW_TAG_variable:
6460       return "DW_TAG_variable";
6461     case DW_TAG_volatile_type:
6462       return "DW_TAG_volatile_type";
6463     case DW_TAG_dwarf_procedure:
6464       return "DW_TAG_dwarf_procedure";
6465     case DW_TAG_restrict_type:
6466       return "DW_TAG_restrict_type";
6467     case DW_TAG_interface_type:
6468       return "DW_TAG_interface_type";
6469     case DW_TAG_namespace:
6470       return "DW_TAG_namespace";
6471     case DW_TAG_imported_module:
6472       return "DW_TAG_imported_module";
6473     case DW_TAG_unspecified_type:
6474       return "DW_TAG_unspecified_type";
6475     case DW_TAG_partial_unit:
6476       return "DW_TAG_partial_unit";
6477     case DW_TAG_imported_unit:
6478       return "DW_TAG_imported_unit";
6479     case DW_TAG_condition:
6480       return "DW_TAG_condition";
6481     case DW_TAG_shared_type:
6482       return "DW_TAG_shared_type";
6483     case DW_TAG_type_unit:
6484       return "DW_TAG_type_unit";
6485     case DW_TAG_rvalue_reference_type:
6486       return "DW_TAG_rvalue_reference_type";
6487     case DW_TAG_template_alias:
6488       return "DW_TAG_template_alias";
6489     case DW_TAG_GNU_template_parameter_pack:
6490       return "DW_TAG_GNU_template_parameter_pack";
6491     case DW_TAG_GNU_formal_parameter_pack:
6492       return "DW_TAG_GNU_formal_parameter_pack";
6493     case DW_TAG_MIPS_loop:
6494       return "DW_TAG_MIPS_loop";
6495     case DW_TAG_format_label:
6496       return "DW_TAG_format_label";
6497     case DW_TAG_function_template:
6498       return "DW_TAG_function_template";
6499     case DW_TAG_class_template:
6500       return "DW_TAG_class_template";
6501     case DW_TAG_GNU_BINCL:
6502       return "DW_TAG_GNU_BINCL";
6503     case DW_TAG_GNU_EINCL:
6504       return "DW_TAG_GNU_EINCL";
6505     case DW_TAG_GNU_template_template_param:
6506       return "DW_TAG_GNU_template_template_param";
6507     default:
6508       return "DW_TAG_<unknown>";
6509     }
6510 }
6511
6512 /* Convert a DWARF attribute code into its string name.  */
6513
6514 static const char *
6515 dwarf_attr_name (unsigned int attr)
6516 {
6517   switch (attr)
6518     {
6519     case DW_AT_sibling:
6520       return "DW_AT_sibling";
6521     case DW_AT_location:
6522       return "DW_AT_location";
6523     case DW_AT_name:
6524       return "DW_AT_name";
6525     case DW_AT_ordering:
6526       return "DW_AT_ordering";
6527     case DW_AT_subscr_data:
6528       return "DW_AT_subscr_data";
6529     case DW_AT_byte_size:
6530       return "DW_AT_byte_size";
6531     case DW_AT_bit_offset:
6532       return "DW_AT_bit_offset";
6533     case DW_AT_bit_size:
6534       return "DW_AT_bit_size";
6535     case DW_AT_element_list:
6536       return "DW_AT_element_list";
6537     case DW_AT_stmt_list:
6538       return "DW_AT_stmt_list";
6539     case DW_AT_low_pc:
6540       return "DW_AT_low_pc";
6541     case DW_AT_high_pc:
6542       return "DW_AT_high_pc";
6543     case DW_AT_language:
6544       return "DW_AT_language";
6545     case DW_AT_member:
6546       return "DW_AT_member";
6547     case DW_AT_discr:
6548       return "DW_AT_discr";
6549     case DW_AT_discr_value:
6550       return "DW_AT_discr_value";
6551     case DW_AT_visibility:
6552       return "DW_AT_visibility";
6553     case DW_AT_import:
6554       return "DW_AT_import";
6555     case DW_AT_string_length:
6556       return "DW_AT_string_length";
6557     case DW_AT_common_reference:
6558       return "DW_AT_common_reference";
6559     case DW_AT_comp_dir:
6560       return "DW_AT_comp_dir";
6561     case DW_AT_const_value:
6562       return "DW_AT_const_value";
6563     case DW_AT_containing_type:
6564       return "DW_AT_containing_type";
6565     case DW_AT_default_value:
6566       return "DW_AT_default_value";
6567     case DW_AT_inline:
6568       return "DW_AT_inline";
6569     case DW_AT_is_optional:
6570       return "DW_AT_is_optional";
6571     case DW_AT_lower_bound:
6572       return "DW_AT_lower_bound";
6573     case DW_AT_producer:
6574       return "DW_AT_producer";
6575     case DW_AT_prototyped:
6576       return "DW_AT_prototyped";
6577     case DW_AT_return_addr:
6578       return "DW_AT_return_addr";
6579     case DW_AT_start_scope:
6580       return "DW_AT_start_scope";
6581     case DW_AT_bit_stride:
6582       return "DW_AT_bit_stride";
6583     case DW_AT_upper_bound:
6584       return "DW_AT_upper_bound";
6585     case DW_AT_abstract_origin:
6586       return "DW_AT_abstract_origin";
6587     case DW_AT_accessibility:
6588       return "DW_AT_accessibility";
6589     case DW_AT_address_class:
6590       return "DW_AT_address_class";
6591     case DW_AT_artificial:
6592       return "DW_AT_artificial";
6593     case DW_AT_base_types:
6594       return "DW_AT_base_types";
6595     case DW_AT_calling_convention:
6596       return "DW_AT_calling_convention";
6597     case DW_AT_count:
6598       return "DW_AT_count";
6599     case DW_AT_data_member_location:
6600       return "DW_AT_data_member_location";
6601     case DW_AT_decl_column:
6602       return "DW_AT_decl_column";
6603     case DW_AT_decl_file:
6604       return "DW_AT_decl_file";
6605     case DW_AT_decl_line:
6606       return "DW_AT_decl_line";
6607     case DW_AT_declaration:
6608       return "DW_AT_declaration";
6609     case DW_AT_discr_list:
6610       return "DW_AT_discr_list";
6611     case DW_AT_encoding:
6612       return "DW_AT_encoding";
6613     case DW_AT_external:
6614       return "DW_AT_external";
6615     case DW_AT_explicit:
6616       return "DW_AT_explicit";
6617     case DW_AT_frame_base:
6618       return "DW_AT_frame_base";
6619     case DW_AT_friend:
6620       return "DW_AT_friend";
6621     case DW_AT_identifier_case:
6622       return "DW_AT_identifier_case";
6623     case DW_AT_macro_info:
6624       return "DW_AT_macro_info";
6625     case DW_AT_namelist_items:
6626       return "DW_AT_namelist_items";
6627     case DW_AT_priority:
6628       return "DW_AT_priority";
6629     case DW_AT_segment:
6630       return "DW_AT_segment";
6631     case DW_AT_specification:
6632       return "DW_AT_specification";
6633     case DW_AT_static_link:
6634       return "DW_AT_static_link";
6635     case DW_AT_type:
6636       return "DW_AT_type";
6637     case DW_AT_use_location:
6638       return "DW_AT_use_location";
6639     case DW_AT_variable_parameter:
6640       return "DW_AT_variable_parameter";
6641     case DW_AT_virtuality:
6642       return "DW_AT_virtuality";
6643     case DW_AT_vtable_elem_location:
6644       return "DW_AT_vtable_elem_location";
6645
6646     case DW_AT_allocated:
6647       return "DW_AT_allocated";
6648     case DW_AT_associated:
6649       return "DW_AT_associated";
6650     case DW_AT_data_location:
6651       return "DW_AT_data_location";
6652     case DW_AT_byte_stride:
6653       return "DW_AT_byte_stride";
6654     case DW_AT_entry_pc:
6655       return "DW_AT_entry_pc";
6656     case DW_AT_use_UTF8:
6657       return "DW_AT_use_UTF8";
6658     case DW_AT_extension:
6659       return "DW_AT_extension";
6660     case DW_AT_ranges:
6661       return "DW_AT_ranges";
6662     case DW_AT_trampoline:
6663       return "DW_AT_trampoline";
6664     case DW_AT_call_column:
6665       return "DW_AT_call_column";
6666     case DW_AT_call_file:
6667       return "DW_AT_call_file";
6668     case DW_AT_call_line:
6669       return "DW_AT_call_line";
6670
6671     case DW_AT_signature:
6672       return "DW_AT_signature";
6673     case DW_AT_main_subprogram:
6674       return "DW_AT_main_subprogram";
6675     case DW_AT_data_bit_offset:
6676       return "DW_AT_data_bit_offset";
6677     case DW_AT_const_expr:
6678       return "DW_AT_const_expr";
6679     case DW_AT_enum_class:
6680       return "DW_AT_enum_class";
6681     case DW_AT_linkage_name:
6682       return "DW_AT_linkage_name";
6683
6684     case DW_AT_MIPS_fde:
6685       return "DW_AT_MIPS_fde";
6686     case DW_AT_MIPS_loop_begin:
6687       return "DW_AT_MIPS_loop_begin";
6688     case DW_AT_MIPS_tail_loop_begin:
6689       return "DW_AT_MIPS_tail_loop_begin";
6690     case DW_AT_MIPS_epilog_begin:
6691       return "DW_AT_MIPS_epilog_begin";
6692     case DW_AT_MIPS_loop_unroll_factor:
6693       return "DW_AT_MIPS_loop_unroll_factor";
6694     case DW_AT_MIPS_software_pipeline_depth:
6695       return "DW_AT_MIPS_software_pipeline_depth";
6696     case DW_AT_MIPS_linkage_name:
6697       return "DW_AT_MIPS_linkage_name";
6698     case DW_AT_MIPS_stride:
6699       return "DW_AT_MIPS_stride";
6700     case DW_AT_MIPS_abstract_name:
6701       return "DW_AT_MIPS_abstract_name";
6702     case DW_AT_MIPS_clone_origin:
6703       return "DW_AT_MIPS_clone_origin";
6704     case DW_AT_MIPS_has_inlines:
6705       return "DW_AT_MIPS_has_inlines";
6706
6707     case DW_AT_sf_names:
6708       return "DW_AT_sf_names";
6709     case DW_AT_src_info:
6710       return "DW_AT_src_info";
6711     case DW_AT_mac_info:
6712       return "DW_AT_mac_info";
6713     case DW_AT_src_coords:
6714       return "DW_AT_src_coords";
6715     case DW_AT_body_begin:
6716       return "DW_AT_body_begin";
6717     case DW_AT_body_end:
6718       return "DW_AT_body_end";
6719     case DW_AT_GNU_vector:
6720       return "DW_AT_GNU_vector";
6721     case DW_AT_GNU_guarded_by:
6722       return "DW_AT_GNU_guarded_by";
6723     case DW_AT_GNU_pt_guarded_by:
6724       return "DW_AT_GNU_pt_guarded_by";
6725     case DW_AT_GNU_guarded:
6726       return "DW_AT_GNU_guarded";
6727     case DW_AT_GNU_pt_guarded:
6728       return "DW_AT_GNU_pt_guarded";
6729     case DW_AT_GNU_locks_excluded:
6730       return "DW_AT_GNU_locks_excluded";
6731     case DW_AT_GNU_exclusive_locks_required:
6732       return "DW_AT_GNU_exclusive_locks_required";
6733     case DW_AT_GNU_shared_locks_required:
6734       return "DW_AT_GNU_shared_locks_required";
6735     case DW_AT_GNU_odr_signature:
6736       return "DW_AT_GNU_odr_signature";
6737     case DW_AT_GNU_template_name:
6738       return "DW_AT_GNU_template_name";
6739
6740     case DW_AT_VMS_rtnbeg_pd_address:
6741       return "DW_AT_VMS_rtnbeg_pd_address";
6742
6743     default:
6744       return "DW_AT_<unknown>";
6745     }
6746 }
6747
6748 /* Convert a DWARF value form code into its string name.  */
6749
6750 static const char *
6751 dwarf_form_name (unsigned int form)
6752 {
6753   switch (form)
6754     {
6755     case DW_FORM_addr:
6756       return "DW_FORM_addr";
6757     case DW_FORM_block2:
6758       return "DW_FORM_block2";
6759     case DW_FORM_block4:
6760       return "DW_FORM_block4";
6761     case DW_FORM_data2:
6762       return "DW_FORM_data2";
6763     case DW_FORM_data4:
6764       return "DW_FORM_data4";
6765     case DW_FORM_data8:
6766       return "DW_FORM_data8";
6767     case DW_FORM_string:
6768       return "DW_FORM_string";
6769     case DW_FORM_block:
6770       return "DW_FORM_block";
6771     case DW_FORM_block1:
6772       return "DW_FORM_block1";
6773     case DW_FORM_data1:
6774       return "DW_FORM_data1";
6775     case DW_FORM_flag:
6776       return "DW_FORM_flag";
6777     case DW_FORM_sdata:
6778       return "DW_FORM_sdata";
6779     case DW_FORM_strp:
6780       return "DW_FORM_strp";
6781     case DW_FORM_udata:
6782       return "DW_FORM_udata";
6783     case DW_FORM_ref_addr:
6784       return "DW_FORM_ref_addr";
6785     case DW_FORM_ref1:
6786       return "DW_FORM_ref1";
6787     case DW_FORM_ref2:
6788       return "DW_FORM_ref2";
6789     case DW_FORM_ref4:
6790       return "DW_FORM_ref4";
6791     case DW_FORM_ref8:
6792       return "DW_FORM_ref8";
6793     case DW_FORM_ref_udata:
6794       return "DW_FORM_ref_udata";
6795     case DW_FORM_indirect:
6796       return "DW_FORM_indirect";
6797     case DW_FORM_sec_offset:
6798       return "DW_FORM_sec_offset";
6799     case DW_FORM_exprloc:
6800       return "DW_FORM_exprloc";
6801     case DW_FORM_flag_present:
6802       return "DW_FORM_flag_present";
6803     case DW_FORM_ref_sig8:
6804       return "DW_FORM_ref_sig8";
6805     default:
6806       return "DW_FORM_<unknown>";
6807     }
6808 }
6809 \f
6810 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6811    instance of an inlined instance of a decl which is local to an inline
6812    function, so we have to trace all of the way back through the origin chain
6813    to find out what sort of node actually served as the original seed for the
6814    given block.  */
6815
6816 static tree
6817 decl_ultimate_origin (const_tree decl)
6818 {
6819   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6820     return NULL_TREE;
6821
6822   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6823      nodes in the function to point to themselves; ignore that if
6824      we're trying to output the abstract instance of this function.  */
6825   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6826     return NULL_TREE;
6827
6828   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6829      most distant ancestor, this should never happen.  */
6830   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6831
6832   return DECL_ABSTRACT_ORIGIN (decl);
6833 }
6834
6835 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6836    of a virtual function may refer to a base class, so we check the 'this'
6837    parameter.  */
6838
6839 static tree
6840 decl_class_context (tree decl)
6841 {
6842   tree context = NULL_TREE;
6843
6844   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6845     context = DECL_CONTEXT (decl);
6846   else
6847     context = TYPE_MAIN_VARIANT
6848       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6849
6850   if (context && !TYPE_P (context))
6851     context = NULL_TREE;
6852
6853   return context;
6854 }
6855 \f
6856 /* Add an attribute/value pair to a DIE.  */
6857
6858 static inline void
6859 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6860 {
6861   /* Maybe this should be an assert?  */
6862   if (die == NULL)
6863     return;
6864
6865   if (die->die_attr == NULL)
6866     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6867   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6868 }
6869
6870 static inline enum dw_val_class
6871 AT_class (dw_attr_ref a)
6872 {
6873   return a->dw_attr_val.val_class;
6874 }
6875
6876 /* Add a flag value attribute to a DIE.  */
6877
6878 static inline void
6879 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6880 {
6881   dw_attr_node attr;
6882
6883   attr.dw_attr = attr_kind;
6884   attr.dw_attr_val.val_class = dw_val_class_flag;
6885   attr.dw_attr_val.v.val_flag = flag;
6886   add_dwarf_attr (die, &attr);
6887 }
6888
6889 static inline unsigned
6890 AT_flag (dw_attr_ref a)
6891 {
6892   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6893   return a->dw_attr_val.v.val_flag;
6894 }
6895
6896 /* Add a signed integer attribute value to a DIE.  */
6897
6898 static inline void
6899 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6900 {
6901   dw_attr_node attr;
6902
6903   attr.dw_attr = attr_kind;
6904   attr.dw_attr_val.val_class = dw_val_class_const;
6905   attr.dw_attr_val.v.val_int = int_val;
6906   add_dwarf_attr (die, &attr);
6907 }
6908
6909 static inline HOST_WIDE_INT
6910 AT_int (dw_attr_ref a)
6911 {
6912   gcc_assert (a && AT_class (a) == dw_val_class_const);
6913   return a->dw_attr_val.v.val_int;
6914 }
6915
6916 /* Add an unsigned integer attribute value to a DIE.  */
6917
6918 static inline void
6919 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6920                  unsigned HOST_WIDE_INT unsigned_val)
6921 {
6922   dw_attr_node attr;
6923
6924   attr.dw_attr = attr_kind;
6925   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6926   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6927   add_dwarf_attr (die, &attr);
6928 }
6929
6930 static inline unsigned HOST_WIDE_INT
6931 AT_unsigned (dw_attr_ref a)
6932 {
6933   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6934   return a->dw_attr_val.v.val_unsigned;
6935 }
6936
6937 /* Add an unsigned double integer attribute value to a DIE.  */
6938
6939 static inline void
6940 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6941                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6942 {
6943   dw_attr_node attr;
6944
6945   attr.dw_attr = attr_kind;
6946   attr.dw_attr_val.val_class = dw_val_class_const_double;
6947   attr.dw_attr_val.v.val_double.high = high;
6948   attr.dw_attr_val.v.val_double.low = low;
6949   add_dwarf_attr (die, &attr);
6950 }
6951
6952 /* Add a floating point attribute value to a DIE and return it.  */
6953
6954 static inline void
6955 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6956             unsigned int length, unsigned int elt_size, unsigned char *array)
6957 {
6958   dw_attr_node attr;
6959
6960   attr.dw_attr = attr_kind;
6961   attr.dw_attr_val.val_class = dw_val_class_vec;
6962   attr.dw_attr_val.v.val_vec.length = length;
6963   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6964   attr.dw_attr_val.v.val_vec.array = array;
6965   add_dwarf_attr (die, &attr);
6966 }
6967
6968 /* Add an 8-byte data attribute value to a DIE.  */
6969
6970 static inline void
6971 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
6972               unsigned char data8[8])
6973 {
6974   dw_attr_node attr;
6975
6976   attr.dw_attr = attr_kind;
6977   attr.dw_attr_val.val_class = dw_val_class_data8;
6978   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
6979   add_dwarf_attr (die, &attr);
6980 }
6981
6982 /* Hash and equality functions for debug_str_hash.  */
6983
6984 static hashval_t
6985 debug_str_do_hash (const void *x)
6986 {
6987   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6988 }
6989
6990 static int
6991 debug_str_eq (const void *x1, const void *x2)
6992 {
6993   return strcmp ((((const struct indirect_string_node *)x1)->str),
6994                  (const char *)x2) == 0;
6995 }
6996
6997 /* Add STR to the indirect string hash table.  */
6998
6999 static struct indirect_string_node *
7000 find_AT_string (const char *str)
7001 {
7002   struct indirect_string_node *node;
7003   void **slot;
7004
7005   if (! debug_str_hash)
7006     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7007                                       debug_str_eq, NULL);
7008
7009   slot = htab_find_slot_with_hash (debug_str_hash, str,
7010                                    htab_hash_string (str), INSERT);
7011   if (*slot == NULL)
7012     {
7013       node = (struct indirect_string_node *)
7014                ggc_alloc_cleared (sizeof (struct indirect_string_node));
7015       node->str = ggc_strdup (str);
7016       *slot = node;
7017     }
7018   else
7019     node = (struct indirect_string_node *) *slot;
7020
7021   node->refcount++;
7022   return node;
7023 }
7024
7025 /* Add a string attribute value to a DIE.  */
7026
7027 static inline void
7028 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7029 {
7030   dw_attr_node attr;
7031   struct indirect_string_node *node;
7032
7033   node = find_AT_string (str);
7034
7035   attr.dw_attr = attr_kind;
7036   attr.dw_attr_val.val_class = dw_val_class_str;
7037   attr.dw_attr_val.v.val_str = node;
7038   add_dwarf_attr (die, &attr);
7039 }
7040
7041 /* Create a label for an indirect string node, ensuring it is going to
7042    be output, unless its reference count goes down to zero.  */
7043
7044 static inline void
7045 gen_label_for_indirect_string (struct indirect_string_node *node)
7046 {
7047   char label[32];
7048
7049   if (node->label)
7050     return;
7051
7052   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7053   ++dw2_string_counter;
7054   node->label = xstrdup (label);
7055 }
7056
7057 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7058    debug string STR.  */
7059
7060 static inline rtx
7061 get_debug_string_label (const char *str)
7062 {
7063   struct indirect_string_node *node = find_AT_string (str);
7064
7065   debug_str_hash_forced = true;
7066
7067   gen_label_for_indirect_string (node);
7068
7069   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7070 }
7071
7072 static inline const char *
7073 AT_string (dw_attr_ref a)
7074 {
7075   gcc_assert (a && AT_class (a) == dw_val_class_str);
7076   return a->dw_attr_val.v.val_str->str;
7077 }
7078
7079 /* Find out whether a string should be output inline in DIE
7080    or out-of-line in .debug_str section.  */
7081
7082 static enum dwarf_form
7083 AT_string_form (dw_attr_ref a)
7084 {
7085   struct indirect_string_node *node;
7086   unsigned int len;
7087
7088   gcc_assert (a && AT_class (a) == dw_val_class_str);
7089
7090   node = a->dw_attr_val.v.val_str;
7091   if (node->form)
7092     return node->form;
7093
7094   len = strlen (node->str) + 1;
7095
7096   /* If the string is shorter or equal to the size of the reference, it is
7097      always better to put it inline.  */
7098   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7099     return node->form = DW_FORM_string;
7100
7101   /* If we cannot expect the linker to merge strings in .debug_str
7102      section, only put it into .debug_str if it is worth even in this
7103      single module.  */
7104   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7105       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7106       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7107     return node->form = DW_FORM_string;
7108
7109   gen_label_for_indirect_string (node);
7110
7111   return node->form = DW_FORM_strp;
7112 }
7113
7114 /* Add a DIE reference attribute value to a DIE.  */
7115
7116 static inline void
7117 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7118 {
7119   dw_attr_node attr;
7120
7121   attr.dw_attr = attr_kind;
7122   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7123   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7124   attr.dw_attr_val.v.val_die_ref.external = 0;
7125   add_dwarf_attr (die, &attr);
7126 }
7127
7128 /* Add an AT_specification attribute to a DIE, and also make the back
7129    pointer from the specification to the definition.  */
7130
7131 static inline void
7132 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7133 {
7134   add_AT_die_ref (die, DW_AT_specification, targ_die);
7135   gcc_assert (!targ_die->die_definition);
7136   targ_die->die_definition = die;
7137 }
7138
7139 static inline dw_die_ref
7140 AT_ref (dw_attr_ref a)
7141 {
7142   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7143   return a->dw_attr_val.v.val_die_ref.die;
7144 }
7145
7146 static inline int
7147 AT_ref_external (dw_attr_ref a)
7148 {
7149   if (a && AT_class (a) == dw_val_class_die_ref)
7150     return a->dw_attr_val.v.val_die_ref.external;
7151
7152   return 0;
7153 }
7154
7155 static inline void
7156 set_AT_ref_external (dw_attr_ref a, int i)
7157 {
7158   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7159   a->dw_attr_val.v.val_die_ref.external = i;
7160 }
7161
7162 /* Add an FDE reference attribute value to a DIE.  */
7163
7164 static inline void
7165 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7166 {
7167   dw_attr_node attr;
7168
7169   attr.dw_attr = attr_kind;
7170   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7171   attr.dw_attr_val.v.val_fde_index = targ_fde;
7172   add_dwarf_attr (die, &attr);
7173 }
7174
7175 /* Add a location description attribute value to a DIE.  */
7176
7177 static inline void
7178 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7179 {
7180   dw_attr_node attr;
7181
7182   attr.dw_attr = attr_kind;
7183   attr.dw_attr_val.val_class = dw_val_class_loc;
7184   attr.dw_attr_val.v.val_loc = loc;
7185   add_dwarf_attr (die, &attr);
7186 }
7187
7188 static inline dw_loc_descr_ref
7189 AT_loc (dw_attr_ref a)
7190 {
7191   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7192   return a->dw_attr_val.v.val_loc;
7193 }
7194
7195 static inline void
7196 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7197 {
7198   dw_attr_node attr;
7199
7200   attr.dw_attr = attr_kind;
7201   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7202   attr.dw_attr_val.v.val_loc_list = loc_list;
7203   add_dwarf_attr (die, &attr);
7204   have_location_lists = true;
7205 }
7206
7207 static inline dw_loc_list_ref
7208 AT_loc_list (dw_attr_ref a)
7209 {
7210   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7211   return a->dw_attr_val.v.val_loc_list;
7212 }
7213
7214 /* Add an address constant attribute value to a DIE.  */
7215
7216 static inline void
7217 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7218 {
7219   dw_attr_node attr;
7220
7221   attr.dw_attr = attr_kind;
7222   attr.dw_attr_val.val_class = dw_val_class_addr;
7223   attr.dw_attr_val.v.val_addr = addr;
7224   add_dwarf_attr (die, &attr);
7225 }
7226
7227 /* Get the RTX from to an address DIE attribute.  */
7228
7229 static inline rtx
7230 AT_addr (dw_attr_ref a)
7231 {
7232   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7233   return a->dw_attr_val.v.val_addr;
7234 }
7235
7236 /* Add a file attribute value to a DIE.  */
7237
7238 static inline void
7239 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7240              struct dwarf_file_data *fd)
7241 {
7242   dw_attr_node attr;
7243
7244   attr.dw_attr = attr_kind;
7245   attr.dw_attr_val.val_class = dw_val_class_file;
7246   attr.dw_attr_val.v.val_file = fd;
7247   add_dwarf_attr (die, &attr);
7248 }
7249
7250 /* Get the dwarf_file_data from a file DIE attribute.  */
7251
7252 static inline struct dwarf_file_data *
7253 AT_file (dw_attr_ref a)
7254 {
7255   gcc_assert (a && AT_class (a) == dw_val_class_file);
7256   return a->dw_attr_val.v.val_file;
7257 }
7258
7259 /* Add a label identifier attribute value to a DIE.  */
7260
7261 static inline void
7262 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7263 {
7264   dw_attr_node attr;
7265
7266   attr.dw_attr = attr_kind;
7267   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7268   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7269   add_dwarf_attr (die, &attr);
7270 }
7271
7272 /* Add a section offset attribute value to a DIE, an offset into the
7273    debug_line section.  */
7274
7275 static inline void
7276 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7277                 const char *label)
7278 {
7279   dw_attr_node attr;
7280
7281   attr.dw_attr = attr_kind;
7282   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7283   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7284   add_dwarf_attr (die, &attr);
7285 }
7286
7287 /* Add a section offset attribute value to a DIE, an offset into the
7288    debug_macinfo section.  */
7289
7290 static inline void
7291 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7292                const char *label)
7293 {
7294   dw_attr_node attr;
7295
7296   attr.dw_attr = attr_kind;
7297   attr.dw_attr_val.val_class = dw_val_class_macptr;
7298   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7299   add_dwarf_attr (die, &attr);
7300 }
7301
7302 /* Add an offset attribute value to a DIE.  */
7303
7304 static inline void
7305 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7306                unsigned HOST_WIDE_INT offset)
7307 {
7308   dw_attr_node attr;
7309
7310   attr.dw_attr = attr_kind;
7311   attr.dw_attr_val.val_class = dw_val_class_offset;
7312   attr.dw_attr_val.v.val_offset = offset;
7313   add_dwarf_attr (die, &attr);
7314 }
7315
7316 /* Add an range_list attribute value to a DIE.  */
7317
7318 static void
7319 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7320                    long unsigned int offset)
7321 {
7322   dw_attr_node attr;
7323
7324   attr.dw_attr = attr_kind;
7325   attr.dw_attr_val.val_class = dw_val_class_range_list;
7326   attr.dw_attr_val.v.val_offset = offset;
7327   add_dwarf_attr (die, &attr);
7328 }
7329
7330 static inline const char *
7331 AT_lbl (dw_attr_ref a)
7332 {
7333   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7334                     || AT_class (a) == dw_val_class_lineptr
7335                     || AT_class (a) == dw_val_class_macptr));
7336   return a->dw_attr_val.v.val_lbl_id;
7337 }
7338
7339 /* Get the attribute of type attr_kind.  */
7340
7341 static dw_attr_ref
7342 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7343 {
7344   dw_attr_ref a;
7345   unsigned ix;
7346   dw_die_ref spec = NULL;
7347
7348   if (! die)
7349     return NULL;
7350
7351   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7352     if (a->dw_attr == attr_kind)
7353       return a;
7354     else if (a->dw_attr == DW_AT_specification
7355              || a->dw_attr == DW_AT_abstract_origin)
7356       spec = AT_ref (a);
7357
7358   if (spec)
7359     return get_AT (spec, attr_kind);
7360
7361   return NULL;
7362 }
7363
7364 /* Return the "low pc" attribute value, typically associated with a subprogram
7365    DIE.  Return null if the "low pc" attribute is either not present, or if it
7366    cannot be represented as an assembler label identifier.  */
7367
7368 static inline const char *
7369 get_AT_low_pc (dw_die_ref die)
7370 {
7371   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7372
7373   return a ? AT_lbl (a) : NULL;
7374 }
7375
7376 /* Return the "high pc" attribute value, typically associated with a subprogram
7377    DIE.  Return null if the "high pc" attribute is either not present, or if it
7378    cannot be represented as an assembler label identifier.  */
7379
7380 static inline const char *
7381 get_AT_hi_pc (dw_die_ref die)
7382 {
7383   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7384
7385   return a ? AT_lbl (a) : NULL;
7386 }
7387
7388 /* Return the value of the string attribute designated by ATTR_KIND, or
7389    NULL if it is not present.  */
7390
7391 static inline const char *
7392 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7393 {
7394   dw_attr_ref a = get_AT (die, attr_kind);
7395
7396   return a ? AT_string (a) : NULL;
7397 }
7398
7399 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7400    if it is not present.  */
7401
7402 static inline int
7403 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7404 {
7405   dw_attr_ref a = get_AT (die, attr_kind);
7406
7407   return a ? AT_flag (a) : 0;
7408 }
7409
7410 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7411    if it is not present.  */
7412
7413 static inline unsigned
7414 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7415 {
7416   dw_attr_ref a = get_AT (die, attr_kind);
7417
7418   return a ? AT_unsigned (a) : 0;
7419 }
7420
7421 static inline dw_die_ref
7422 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7423 {
7424   dw_attr_ref a = get_AT (die, attr_kind);
7425
7426   return a ? AT_ref (a) : NULL;
7427 }
7428
7429 static inline struct dwarf_file_data *
7430 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7431 {
7432   dw_attr_ref a = get_AT (die, attr_kind);
7433
7434   return a ? AT_file (a) : NULL;
7435 }
7436
7437 /* Return TRUE if the language is C or C++.  */
7438
7439 static inline bool
7440 is_c_family (void)
7441 {
7442   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7443
7444   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7445           || lang == DW_LANG_C99
7446           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7447 }
7448
7449 /* Return TRUE if the language is C++.  */
7450
7451 static inline bool
7452 is_cxx (void)
7453 {
7454   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7455
7456   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7457 }
7458
7459 /* Return TRUE if the language is Fortran.  */
7460
7461 static inline bool
7462 is_fortran (void)
7463 {
7464   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7465
7466   return (lang == DW_LANG_Fortran77
7467           || lang == DW_LANG_Fortran90
7468           || lang == DW_LANG_Fortran95);
7469 }
7470
7471 /* Return TRUE if the language is Java.  */
7472
7473 static inline bool
7474 is_java (void)
7475 {
7476   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7477
7478   return lang == DW_LANG_Java;
7479 }
7480
7481 /* Return TRUE if the language is Ada.  */
7482
7483 static inline bool
7484 is_ada (void)
7485 {
7486   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7487
7488   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7489 }
7490
7491 /* Remove the specified attribute if present.  */
7492
7493 static void
7494 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7495 {
7496   dw_attr_ref a;
7497   unsigned ix;
7498
7499   if (! die)
7500     return;
7501
7502   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7503     if (a->dw_attr == attr_kind)
7504       {
7505         if (AT_class (a) == dw_val_class_str)
7506           if (a->dw_attr_val.v.val_str->refcount)
7507             a->dw_attr_val.v.val_str->refcount--;
7508
7509         /* VEC_ordered_remove should help reduce the number of abbrevs
7510            that are needed.  */
7511         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7512         return;
7513       }
7514 }
7515
7516 /* Remove CHILD from its parent.  PREV must have the property that
7517    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7518
7519 static void
7520 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7521 {
7522   gcc_assert (child->die_parent == prev->die_parent);
7523   gcc_assert (prev->die_sib == child);
7524   if (prev == child)
7525     {
7526       gcc_assert (child->die_parent->die_child == child);
7527       prev = NULL;
7528     }
7529   else
7530     prev->die_sib = child->die_sib;
7531   if (child->die_parent->die_child == child)
7532     child->die_parent->die_child = prev;
7533 }
7534
7535 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7536    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7537
7538 static void
7539 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7540 {
7541   dw_die_ref parent = old_child->die_parent;
7542
7543   gcc_assert (parent == prev->die_parent);
7544   gcc_assert (prev->die_sib == old_child);
7545
7546   new_child->die_parent = parent;
7547   if (prev == old_child)
7548     {
7549       gcc_assert (parent->die_child == old_child);
7550       new_child->die_sib = new_child;
7551     }
7552   else
7553     {
7554       prev->die_sib = new_child;
7555       new_child->die_sib = old_child->die_sib;
7556     }
7557   if (old_child->die_parent->die_child == old_child)
7558     old_child->die_parent->die_child = new_child;
7559 }
7560
7561 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7562
7563 static void
7564 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7565 {
7566   dw_die_ref c;
7567   new_parent->die_child = old_parent->die_child;
7568   old_parent->die_child = NULL;
7569   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7570 }
7571
7572 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7573    matches TAG.  */
7574
7575 static void
7576 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7577 {
7578   dw_die_ref c;
7579
7580   c = die->die_child;
7581   if (c) do {
7582     dw_die_ref prev = c;
7583     c = c->die_sib;
7584     while (c->die_tag == tag)
7585       {
7586         remove_child_with_prev (c, prev);
7587         /* Might have removed every child.  */
7588         if (c == c->die_sib)
7589           return;
7590         c = c->die_sib;
7591       }
7592   } while (c != die->die_child);
7593 }
7594
7595 /* Add a CHILD_DIE as the last child of DIE.  */
7596
7597 static void
7598 add_child_die (dw_die_ref die, dw_die_ref child_die)
7599 {
7600   /* FIXME this should probably be an assert.  */
7601   if (! die || ! child_die)
7602     return;
7603   gcc_assert (die != child_die);
7604
7605   child_die->die_parent = die;
7606   if (die->die_child)
7607     {
7608       child_die->die_sib = die->die_child->die_sib;
7609       die->die_child->die_sib = child_die;
7610     }
7611   else
7612     child_die->die_sib = child_die;
7613   die->die_child = child_die;
7614 }
7615
7616 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7617    is the specification, to the end of PARENT's list of children.
7618    This is done by removing and re-adding it.  */
7619
7620 static void
7621 splice_child_die (dw_die_ref parent, dw_die_ref child)
7622 {
7623   dw_die_ref p;
7624
7625   /* We want the declaration DIE from inside the class, not the
7626      specification DIE at toplevel.  */
7627   if (child->die_parent != parent)
7628     {
7629       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7630
7631       if (tmp)
7632         child = tmp;
7633     }
7634
7635   gcc_assert (child->die_parent == parent
7636               || (child->die_parent
7637                   == get_AT_ref (parent, DW_AT_specification)));
7638
7639   for (p = child->die_parent->die_child; ; p = p->die_sib)
7640     if (p->die_sib == child)
7641       {
7642         remove_child_with_prev (child, p);
7643         break;
7644       }
7645
7646   add_child_die (parent, child);
7647 }
7648
7649 /* Return a pointer to a newly created DIE node.  */
7650
7651 static inline dw_die_ref
7652 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7653 {
7654   dw_die_ref die = GGC_CNEW (die_node);
7655
7656   die->die_tag = tag_value;
7657
7658   if (parent_die != NULL)
7659     add_child_die (parent_die, die);
7660   else
7661     {
7662       limbo_die_node *limbo_node;
7663
7664       limbo_node = GGC_CNEW (limbo_die_node);
7665       limbo_node->die = die;
7666       limbo_node->created_for = t;
7667       limbo_node->next = limbo_die_list;
7668       limbo_die_list = limbo_node;
7669     }
7670
7671   return die;
7672 }
7673
7674 /* Return the DIE associated with the given type specifier.  */
7675
7676 static inline dw_die_ref
7677 lookup_type_die (tree type)
7678 {
7679   return TYPE_SYMTAB_DIE (type);
7680 }
7681
7682 /* Equate a DIE to a given type specifier.  */
7683
7684 static inline void
7685 equate_type_number_to_die (tree type, dw_die_ref type_die)
7686 {
7687   TYPE_SYMTAB_DIE (type) = type_die;
7688 }
7689
7690 /* Returns a hash value for X (which really is a die_struct).  */
7691
7692 static hashval_t
7693 decl_die_table_hash (const void *x)
7694 {
7695   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7696 }
7697
7698 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7699
7700 static int
7701 decl_die_table_eq (const void *x, const void *y)
7702 {
7703   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7704 }
7705
7706 /* Return the DIE associated with a given declaration.  */
7707
7708 static inline dw_die_ref
7709 lookup_decl_die (tree decl)
7710 {
7711   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7712 }
7713
7714 /* Returns a hash value for X (which really is a var_loc_list).  */
7715
7716 static hashval_t
7717 decl_loc_table_hash (const void *x)
7718 {
7719   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7720 }
7721
7722 /* Return nonzero if decl_id of var_loc_list X is the same as
7723    UID of decl *Y.  */
7724
7725 static int
7726 decl_loc_table_eq (const void *x, const void *y)
7727 {
7728   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7729 }
7730
7731 /* Return the var_loc list associated with a given declaration.  */
7732
7733 static inline var_loc_list *
7734 lookup_decl_loc (const_tree decl)
7735 {
7736   if (!decl_loc_table)
7737     return NULL;
7738   return (var_loc_list *)
7739     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7740 }
7741
7742 /* Equate a DIE to a particular declaration.  */
7743
7744 static void
7745 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7746 {
7747   unsigned int decl_id = DECL_UID (decl);
7748   void **slot;
7749
7750   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7751   *slot = decl_die;
7752   decl_die->decl_id = decl_id;
7753 }
7754
7755 /* Add a variable location node to the linked list for DECL.  */
7756
7757 static void
7758 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7759 {
7760   unsigned int decl_id = DECL_UID (decl);
7761   var_loc_list *temp;
7762   void **slot;
7763
7764   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7765   if (*slot == NULL)
7766     {
7767       temp = GGC_CNEW (var_loc_list);
7768       temp->decl_id = decl_id;
7769       *slot = temp;
7770     }
7771   else
7772     temp = (var_loc_list *) *slot;
7773
7774   if (temp->last)
7775     {
7776       /* If the current location is the same as the end of the list,
7777          and either both or neither of the locations is uninitialized,
7778          we have nothing to do.  */
7779       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7780                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7781           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7782                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7783               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7784                    == VAR_INIT_STATUS_UNINITIALIZED)
7785                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7786                       == VAR_INIT_STATUS_UNINITIALIZED))))
7787         {
7788           /* Add LOC to the end of list and update LAST.  */
7789           temp->last->next = loc;
7790           temp->last = loc;
7791         }
7792     }
7793   /* Do not add empty location to the beginning of the list.  */
7794   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7795     {
7796       temp->first = loc;
7797       temp->last = loc;
7798     }
7799 }
7800 \f
7801 /* Keep track of the number of spaces used to indent the
7802    output of the debugging routines that print the structure of
7803    the DIE internal representation.  */
7804 static int print_indent;
7805
7806 /* Indent the line the number of spaces given by print_indent.  */
7807
7808 static inline void
7809 print_spaces (FILE *outfile)
7810 {
7811   fprintf (outfile, "%*s", print_indent, "");
7812 }
7813
7814 /* Print a type signature in hex.  */
7815
7816 static inline void
7817 print_signature (FILE *outfile, char *sig)
7818 {
7819   int i;
7820
7821   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7822     fprintf (outfile, "%02x", sig[i] & 0xff);
7823 }
7824
7825 /* Print the information associated with a given DIE, and its children.
7826    This routine is a debugging aid only.  */
7827
7828 static void
7829 print_die (dw_die_ref die, FILE *outfile)
7830 {
7831   dw_attr_ref a;
7832   dw_die_ref c;
7833   unsigned ix;
7834
7835   print_spaces (outfile);
7836   fprintf (outfile, "DIE %4ld: %s\n",
7837            die->die_offset, dwarf_tag_name (die->die_tag));
7838   print_spaces (outfile);
7839   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7840   fprintf (outfile, " offset: %ld\n", die->die_offset);
7841   if (dwarf_version >= 4 && die->die_id.die_type_node)
7842     {
7843       print_spaces (outfile);
7844       fprintf (outfile, "  signature: ");
7845       print_signature (outfile, die->die_id.die_type_node->signature);
7846       fprintf (outfile, "\n");
7847     }
7848
7849   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7850     {
7851       print_spaces (outfile);
7852       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7853
7854       switch (AT_class (a))
7855         {
7856         case dw_val_class_addr:
7857           fprintf (outfile, "address");
7858           break;
7859         case dw_val_class_offset:
7860           fprintf (outfile, "offset");
7861           break;
7862         case dw_val_class_loc:
7863           fprintf (outfile, "location descriptor");
7864           break;
7865         case dw_val_class_loc_list:
7866           fprintf (outfile, "location list -> label:%s",
7867                    AT_loc_list (a)->ll_symbol);
7868           break;
7869         case dw_val_class_range_list:
7870           fprintf (outfile, "range list");
7871           break;
7872         case dw_val_class_const:
7873           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7874           break;
7875         case dw_val_class_unsigned_const:
7876           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7877           break;
7878         case dw_val_class_const_double:
7879           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7880                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7881                    a->dw_attr_val.v.val_double.high,
7882                    a->dw_attr_val.v.val_double.low);
7883           break;
7884         case dw_val_class_vec:
7885           fprintf (outfile, "floating-point or vector constant");
7886           break;
7887         case dw_val_class_flag:
7888           fprintf (outfile, "%u", AT_flag (a));
7889           break;
7890         case dw_val_class_die_ref:
7891           if (AT_ref (a) != NULL)
7892             {
7893               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
7894                 {
7895                   fprintf (outfile, "die -> signature: ");
7896                   print_signature (outfile,
7897                                    AT_ref (a)->die_id.die_type_node->signature);
7898                 }
7899               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
7900                 fprintf (outfile, "die -> label: %s",
7901                          AT_ref (a)->die_id.die_symbol);
7902               else
7903                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7904             }
7905           else
7906             fprintf (outfile, "die -> <null>");
7907           break;
7908         case dw_val_class_lbl_id:
7909         case dw_val_class_lineptr:
7910         case dw_val_class_macptr:
7911           fprintf (outfile, "label: %s", AT_lbl (a));
7912           break;
7913         case dw_val_class_str:
7914           if (AT_string (a) != NULL)
7915             fprintf (outfile, "\"%s\"", AT_string (a));
7916           else
7917             fprintf (outfile, "<null>");
7918           break;
7919         case dw_val_class_file:
7920           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7921                    AT_file (a)->emitted_number);
7922           break;
7923         case dw_val_class_data8:
7924           {
7925             int i;
7926
7927             for (i = 0; i < 8; i++)
7928               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
7929             break;
7930           }
7931         default:
7932           break;
7933         }
7934
7935       fprintf (outfile, "\n");
7936     }
7937
7938   if (die->die_child != NULL)
7939     {
7940       print_indent += 4;
7941       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7942       print_indent -= 4;
7943     }
7944   if (print_indent == 0)
7945     fprintf (outfile, "\n");
7946 }
7947
7948 /* Print the contents of the source code line number correspondence table.
7949    This routine is a debugging aid only.  */
7950
7951 static void
7952 print_dwarf_line_table (FILE *outfile)
7953 {
7954   unsigned i;
7955   dw_line_info_ref line_info;
7956
7957   fprintf (outfile, "\n\nDWARF source line information\n");
7958   for (i = 1; i < line_info_table_in_use; i++)
7959     {
7960       line_info = &line_info_table[i];
7961       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7962                line_info->dw_file_num,
7963                line_info->dw_line_num);
7964     }
7965
7966   fprintf (outfile, "\n\n");
7967 }
7968
7969 /* Print the information collected for a given DIE.  */
7970
7971 void
7972 debug_dwarf_die (dw_die_ref die)
7973 {
7974   print_die (die, stderr);
7975 }
7976
7977 /* Print all DWARF information collected for the compilation unit.
7978    This routine is a debugging aid only.  */
7979
7980 void
7981 debug_dwarf (void)
7982 {
7983   print_indent = 0;
7984   print_die (comp_unit_die, stderr);
7985   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7986     print_dwarf_line_table (stderr);
7987 }
7988 \f
7989 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7990    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7991    DIE that marks the start of the DIEs for this include file.  */
7992
7993 static dw_die_ref
7994 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7995 {
7996   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7997   dw_die_ref new_unit = gen_compile_unit_die (filename);
7998
7999   new_unit->die_sib = old_unit;
8000   return new_unit;
8001 }
8002
8003 /* Close an include-file CU and reopen the enclosing one.  */
8004
8005 static dw_die_ref
8006 pop_compile_unit (dw_die_ref old_unit)
8007 {
8008   dw_die_ref new_unit = old_unit->die_sib;
8009
8010   old_unit->die_sib = NULL;
8011   return new_unit;
8012 }
8013
8014 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8015 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8016
8017 /* Calculate the checksum of a location expression.  */
8018
8019 static inline void
8020 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8021 {
8022   int tem;
8023
8024   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8025   CHECKSUM (tem);
8026   CHECKSUM (loc->dw_loc_oprnd1);
8027   CHECKSUM (loc->dw_loc_oprnd2);
8028 }
8029
8030 /* Calculate the checksum of an attribute.  */
8031
8032 static void
8033 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8034 {
8035   dw_loc_descr_ref loc;
8036   rtx r;
8037
8038   CHECKSUM (at->dw_attr);
8039
8040   /* We don't care that this was compiled with a different compiler
8041      snapshot; if the output is the same, that's what matters.  */
8042   if (at->dw_attr == DW_AT_producer)
8043     return;
8044
8045   switch (AT_class (at))
8046     {
8047     case dw_val_class_const:
8048       CHECKSUM (at->dw_attr_val.v.val_int);
8049       break;
8050     case dw_val_class_unsigned_const:
8051       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8052       break;
8053     case dw_val_class_const_double:
8054       CHECKSUM (at->dw_attr_val.v.val_double);
8055       break;
8056     case dw_val_class_vec:
8057       CHECKSUM (at->dw_attr_val.v.val_vec);
8058       break;
8059     case dw_val_class_flag:
8060       CHECKSUM (at->dw_attr_val.v.val_flag);
8061       break;
8062     case dw_val_class_str:
8063       CHECKSUM_STRING (AT_string (at));
8064       break;
8065
8066     case dw_val_class_addr:
8067       r = AT_addr (at);
8068       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8069       CHECKSUM_STRING (XSTR (r, 0));
8070       break;
8071
8072     case dw_val_class_offset:
8073       CHECKSUM (at->dw_attr_val.v.val_offset);
8074       break;
8075
8076     case dw_val_class_loc:
8077       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8078         loc_checksum (loc, ctx);
8079       break;
8080
8081     case dw_val_class_die_ref:
8082       die_checksum (AT_ref (at), ctx, mark);
8083       break;
8084
8085     case dw_val_class_fde_ref:
8086     case dw_val_class_lbl_id:
8087     case dw_val_class_lineptr:
8088     case dw_val_class_macptr:
8089       break;
8090
8091     case dw_val_class_file:
8092       CHECKSUM_STRING (AT_file (at)->filename);
8093       break;
8094
8095     case dw_val_class_data8:
8096       CHECKSUM (at->dw_attr_val.v.val_data8);
8097       break;
8098
8099     default:
8100       break;
8101     }
8102 }
8103
8104 /* Calculate the checksum of a DIE.  */
8105
8106 static void
8107 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8108 {
8109   dw_die_ref c;
8110   dw_attr_ref a;
8111   unsigned ix;
8112
8113   /* To avoid infinite recursion.  */
8114   if (die->die_mark)
8115     {
8116       CHECKSUM (die->die_mark);
8117       return;
8118     }
8119   die->die_mark = ++(*mark);
8120
8121   CHECKSUM (die->die_tag);
8122
8123   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8124     attr_checksum (a, ctx, mark);
8125
8126   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8127 }
8128
8129 #undef CHECKSUM
8130 #undef CHECKSUM_STRING
8131
8132 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8133 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8134 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8135 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8136 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8137 #define CHECKSUM_ATTR(FOO) \
8138   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8139
8140 /* Calculate the checksum of a number in signed LEB128 format.  */
8141
8142 static void
8143 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8144 {
8145   unsigned char byte;
8146   bool more;
8147
8148   while (1)
8149     {
8150       byte = (value & 0x7f);
8151       value >>= 7;
8152       more = !((value == 0 && (byte & 0x40) == 0)
8153                 || (value == -1 && (byte & 0x40) != 0));
8154       if (more)
8155         byte |= 0x80;
8156       CHECKSUM (byte);
8157       if (!more)
8158         break;
8159     }
8160 }
8161
8162 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8163
8164 static void
8165 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8166 {
8167   while (1)
8168     {
8169       unsigned char byte = (value & 0x7f);
8170       value >>= 7;
8171       if (value != 0)
8172         /* More bytes to follow.  */
8173         byte |= 0x80;
8174       CHECKSUM (byte);
8175       if (value == 0)
8176         break;
8177     }
8178 }
8179
8180 /* Checksum the context of the DIE.  This adds the names of any
8181    surrounding namespaces or structures to the checksum.  */
8182
8183 static void
8184 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8185 {
8186   const char *name;
8187   dw_die_ref spec;
8188   int tag = die->die_tag;
8189
8190   if (tag != DW_TAG_namespace
8191       && tag != DW_TAG_structure_type
8192       && tag != DW_TAG_class_type)
8193     return;
8194
8195   name = get_AT_string (die, DW_AT_name);
8196
8197   spec = get_AT_ref (die, DW_AT_specification);
8198   if (spec != NULL)
8199     die = spec;
8200
8201   if (die->die_parent != NULL)
8202     checksum_die_context (die->die_parent, ctx);
8203
8204   CHECKSUM_ULEB128 ('C');
8205   CHECKSUM_ULEB128 (tag);
8206   if (name != NULL)
8207     CHECKSUM_STRING (name);
8208 }
8209
8210 /* Calculate the checksum of a location expression.  */
8211
8212 static inline void
8213 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8214 {
8215   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8216      were emitted as a DW_FORM_sdata instead of a location expression.  */
8217   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8218     {
8219       CHECKSUM_ULEB128 (DW_FORM_sdata);
8220       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8221       return;
8222     }
8223
8224   /* Otherwise, just checksum the raw location expression.  */
8225   while (loc != NULL)
8226     {
8227       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8228       CHECKSUM (loc->dw_loc_oprnd1);
8229       CHECKSUM (loc->dw_loc_oprnd2);
8230       loc = loc->dw_loc_next;
8231     }
8232 }
8233
8234 /* Calculate the checksum of an attribute.  */
8235
8236 static void
8237 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8238                        struct md5_ctx *ctx, int *mark)
8239 {
8240   dw_loc_descr_ref loc;
8241   rtx r;
8242
8243   if (AT_class (at) == dw_val_class_die_ref)
8244     {
8245       dw_die_ref target_die = AT_ref (at);
8246
8247       /* For pointer and reference types, we checksum only the (qualified)
8248          name of the target type (if there is a name).  For friend entries,
8249          we checksum only the (qualified) name of the target type or function.
8250          This allows the checksum to remain the same whether the target type
8251          is complete or not.  */
8252       if ((at->dw_attr == DW_AT_type
8253            && (tag == DW_TAG_pointer_type
8254                || tag == DW_TAG_reference_type
8255                || tag == DW_TAG_ptr_to_member_type))
8256           || (at->dw_attr == DW_AT_friend
8257               && tag == DW_TAG_friend))
8258         {
8259           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8260
8261           if (name_attr != NULL)
8262             {
8263               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8264
8265               if (decl == NULL)
8266                 decl = target_die;
8267               CHECKSUM_ULEB128 ('N');
8268               CHECKSUM_ULEB128 (at->dw_attr);
8269               if (decl->die_parent != NULL)
8270                 checksum_die_context (decl->die_parent, ctx);
8271               CHECKSUM_ULEB128 ('E');
8272               CHECKSUM_STRING (AT_string (name_attr));
8273               return;
8274             }
8275         }
8276
8277       /* For all other references to another DIE, we check to see if the
8278          target DIE has already been visited.  If it has, we emit a
8279          backward reference; if not, we descend recursively.  */
8280       if (target_die->die_mark > 0)
8281         {
8282           CHECKSUM_ULEB128 ('R');
8283           CHECKSUM_ULEB128 (at->dw_attr);
8284           CHECKSUM_ULEB128 (target_die->die_mark);
8285         }
8286       else
8287         {
8288           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8289
8290           if (decl == NULL)
8291             decl = target_die;
8292           target_die->die_mark = ++(*mark);
8293           CHECKSUM_ULEB128 ('T');
8294           CHECKSUM_ULEB128 (at->dw_attr);
8295           if (decl->die_parent != NULL)
8296             checksum_die_context (decl->die_parent, ctx);
8297           die_checksum_ordered (target_die, ctx, mark);
8298         }
8299       return;
8300     }
8301
8302   CHECKSUM_ULEB128 ('A');
8303   CHECKSUM_ULEB128 (at->dw_attr);
8304
8305   switch (AT_class (at))
8306     {
8307     case dw_val_class_const:
8308       CHECKSUM_ULEB128 (DW_FORM_sdata);
8309       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8310       break;
8311
8312     case dw_val_class_unsigned_const:
8313       CHECKSUM_ULEB128 (DW_FORM_sdata);
8314       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8315       break;
8316
8317     case dw_val_class_const_double:
8318       CHECKSUM_ULEB128 (DW_FORM_block);
8319       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8320       CHECKSUM (at->dw_attr_val.v.val_double);
8321       break;
8322
8323     case dw_val_class_vec:
8324       CHECKSUM_ULEB128 (DW_FORM_block);
8325       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8326       CHECKSUM (at->dw_attr_val.v.val_vec);
8327       break;
8328
8329     case dw_val_class_flag:
8330       CHECKSUM_ULEB128 (DW_FORM_flag);
8331       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8332       break;
8333
8334     case dw_val_class_str:
8335       CHECKSUM_ULEB128 (DW_FORM_string);
8336       CHECKSUM_STRING (AT_string (at));
8337       break;
8338
8339     case dw_val_class_addr:
8340       r = AT_addr (at);
8341       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8342       CHECKSUM_ULEB128 (DW_FORM_string);
8343       CHECKSUM_STRING (XSTR (r, 0));
8344       break;
8345
8346     case dw_val_class_offset:
8347       CHECKSUM_ULEB128 (DW_FORM_sdata);
8348       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8349       break;
8350
8351     case dw_val_class_loc:
8352       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8353         loc_checksum_ordered (loc, ctx);
8354       break;
8355
8356     case dw_val_class_fde_ref:
8357     case dw_val_class_lbl_id:
8358     case dw_val_class_lineptr:
8359     case dw_val_class_macptr:
8360       break;
8361
8362     case dw_val_class_file:
8363       CHECKSUM_ULEB128 (DW_FORM_string);
8364       CHECKSUM_STRING (AT_file (at)->filename);
8365       break;
8366
8367     case dw_val_class_data8:
8368       CHECKSUM (at->dw_attr_val.v.val_data8);
8369       break;
8370
8371     default:
8372       break;
8373     }
8374 }
8375
8376 struct checksum_attributes
8377 {
8378   dw_attr_ref at_name;
8379   dw_attr_ref at_type;
8380   dw_attr_ref at_friend;
8381   dw_attr_ref at_accessibility;
8382   dw_attr_ref at_address_class;
8383   dw_attr_ref at_allocated;
8384   dw_attr_ref at_artificial;
8385   dw_attr_ref at_associated;
8386   dw_attr_ref at_binary_scale;
8387   dw_attr_ref at_bit_offset;
8388   dw_attr_ref at_bit_size;
8389   dw_attr_ref at_bit_stride;
8390   dw_attr_ref at_byte_size;
8391   dw_attr_ref at_byte_stride;
8392   dw_attr_ref at_const_value;
8393   dw_attr_ref at_containing_type;
8394   dw_attr_ref at_count;
8395   dw_attr_ref at_data_location;
8396   dw_attr_ref at_data_member_location;
8397   dw_attr_ref at_decimal_scale;
8398   dw_attr_ref at_decimal_sign;
8399   dw_attr_ref at_default_value;
8400   dw_attr_ref at_digit_count;
8401   dw_attr_ref at_discr;
8402   dw_attr_ref at_discr_list;
8403   dw_attr_ref at_discr_value;
8404   dw_attr_ref at_encoding;
8405   dw_attr_ref at_endianity;
8406   dw_attr_ref at_explicit;
8407   dw_attr_ref at_is_optional;
8408   dw_attr_ref at_location;
8409   dw_attr_ref at_lower_bound;
8410   dw_attr_ref at_mutable;
8411   dw_attr_ref at_ordering;
8412   dw_attr_ref at_picture_string;
8413   dw_attr_ref at_prototyped;
8414   dw_attr_ref at_small;
8415   dw_attr_ref at_segment;
8416   dw_attr_ref at_string_length;
8417   dw_attr_ref at_threads_scaled;
8418   dw_attr_ref at_upper_bound;
8419   dw_attr_ref at_use_location;
8420   dw_attr_ref at_use_UTF8;
8421   dw_attr_ref at_variable_parameter;
8422   dw_attr_ref at_virtuality;
8423   dw_attr_ref at_visibility;
8424   dw_attr_ref at_vtable_elem_location;
8425 };
8426
8427 /* Collect the attributes that we will want to use for the checksum.  */
8428
8429 static void
8430 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8431 {
8432   dw_attr_ref a;
8433   unsigned ix;
8434
8435   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8436     {
8437       switch (a->dw_attr)
8438         {
8439         case DW_AT_name:
8440           attrs->at_name = a;
8441           break;
8442         case DW_AT_type:
8443           attrs->at_type = a;
8444           break;
8445         case DW_AT_friend:
8446           attrs->at_friend = a;
8447           break;
8448         case DW_AT_accessibility:
8449           attrs->at_accessibility = a;
8450           break;
8451         case DW_AT_address_class:
8452           attrs->at_address_class = a;
8453           break;
8454         case DW_AT_allocated:
8455           attrs->at_allocated = a;
8456           break;
8457         case DW_AT_artificial:
8458           attrs->at_artificial = a;
8459           break;
8460         case DW_AT_associated:
8461           attrs->at_associated = a;
8462           break;
8463         case DW_AT_binary_scale:
8464           attrs->at_binary_scale = a;
8465           break;
8466         case DW_AT_bit_offset:
8467           attrs->at_bit_offset = a;
8468           break;
8469         case DW_AT_bit_size:
8470           attrs->at_bit_size = a;
8471           break;
8472         case DW_AT_bit_stride:
8473           attrs->at_bit_stride = a;
8474           break;
8475         case DW_AT_byte_size:
8476           attrs->at_byte_size = a;
8477           break;
8478         case DW_AT_byte_stride:
8479           attrs->at_byte_stride = a;
8480           break;
8481         case DW_AT_const_value:
8482           attrs->at_const_value = a;
8483           break;
8484         case DW_AT_containing_type:
8485           attrs->at_containing_type = a;
8486           break;
8487         case DW_AT_count:
8488           attrs->at_count = a;
8489           break;
8490         case DW_AT_data_location:
8491           attrs->at_data_location = a;
8492           break;
8493         case DW_AT_data_member_location:
8494           attrs->at_data_member_location = a;
8495           break;
8496         case DW_AT_decimal_scale:
8497           attrs->at_decimal_scale = a;
8498           break;
8499         case DW_AT_decimal_sign:
8500           attrs->at_decimal_sign = a;
8501           break;
8502         case DW_AT_default_value:
8503           attrs->at_default_value = a;
8504           break;
8505         case DW_AT_digit_count:
8506           attrs->at_digit_count = a;
8507           break;
8508         case DW_AT_discr:
8509           attrs->at_discr = a;
8510           break;
8511         case DW_AT_discr_list:
8512           attrs->at_discr_list = a;
8513           break;
8514         case DW_AT_discr_value:
8515           attrs->at_discr_value = a;
8516           break;
8517         case DW_AT_encoding:
8518           attrs->at_encoding = a;
8519           break;
8520         case DW_AT_endianity:
8521           attrs->at_endianity = a;
8522           break;
8523         case DW_AT_explicit:
8524           attrs->at_explicit = a;
8525           break;
8526         case DW_AT_is_optional:
8527           attrs->at_is_optional = a;
8528           break;
8529         case DW_AT_location:
8530           attrs->at_location = a;
8531           break;
8532         case DW_AT_lower_bound:
8533           attrs->at_lower_bound = a;
8534           break;
8535         case DW_AT_mutable:
8536           attrs->at_mutable = a;
8537           break;
8538         case DW_AT_ordering:
8539           attrs->at_ordering = a;
8540           break;
8541         case DW_AT_picture_string:
8542           attrs->at_picture_string = a;
8543           break;
8544         case DW_AT_prototyped:
8545           attrs->at_prototyped = a;
8546           break;
8547         case DW_AT_small:
8548           attrs->at_small = a;
8549           break;
8550         case DW_AT_segment:
8551           attrs->at_segment = a;
8552           break;
8553         case DW_AT_string_length:
8554           attrs->at_string_length = a;
8555           break;
8556         case DW_AT_threads_scaled:
8557           attrs->at_threads_scaled = a;
8558           break;
8559         case DW_AT_upper_bound:
8560           attrs->at_upper_bound = a;
8561           break;
8562         case DW_AT_use_location:
8563           attrs->at_use_location = a;
8564           break;
8565         case DW_AT_use_UTF8:
8566           attrs->at_use_UTF8 = a;
8567           break;
8568         case DW_AT_variable_parameter:
8569           attrs->at_variable_parameter = a;
8570           break;
8571         case DW_AT_virtuality:
8572           attrs->at_virtuality = a;
8573           break;
8574         case DW_AT_visibility:
8575           attrs->at_visibility = a;
8576           break;
8577         case DW_AT_vtable_elem_location:
8578           attrs->at_vtable_elem_location = a;
8579           break;
8580         default:
8581           break;
8582         }
8583     }
8584 }
8585
8586 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8587
8588 static void
8589 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8590 {
8591   dw_die_ref c;
8592   dw_die_ref decl;
8593   struct checksum_attributes attrs;
8594
8595   CHECKSUM_ULEB128 ('D');
8596   CHECKSUM_ULEB128 (die->die_tag);
8597
8598   memset (&attrs, 0, sizeof (attrs));
8599
8600   decl = get_AT_ref (die, DW_AT_specification);
8601   if (decl != NULL)
8602     collect_checksum_attributes (&attrs, decl);
8603   collect_checksum_attributes (&attrs, die);
8604
8605   CHECKSUM_ATTR (attrs.at_name);
8606   CHECKSUM_ATTR (attrs.at_accessibility);
8607   CHECKSUM_ATTR (attrs.at_address_class);
8608   CHECKSUM_ATTR (attrs.at_allocated);
8609   CHECKSUM_ATTR (attrs.at_artificial);
8610   CHECKSUM_ATTR (attrs.at_associated);
8611   CHECKSUM_ATTR (attrs.at_binary_scale);
8612   CHECKSUM_ATTR (attrs.at_bit_offset);
8613   CHECKSUM_ATTR (attrs.at_bit_size);
8614   CHECKSUM_ATTR (attrs.at_bit_stride);
8615   CHECKSUM_ATTR (attrs.at_byte_size);
8616   CHECKSUM_ATTR (attrs.at_byte_stride);
8617   CHECKSUM_ATTR (attrs.at_const_value);
8618   CHECKSUM_ATTR (attrs.at_containing_type);
8619   CHECKSUM_ATTR (attrs.at_count);
8620   CHECKSUM_ATTR (attrs.at_data_location);
8621   CHECKSUM_ATTR (attrs.at_data_member_location);
8622   CHECKSUM_ATTR (attrs.at_decimal_scale);
8623   CHECKSUM_ATTR (attrs.at_decimal_sign);
8624   CHECKSUM_ATTR (attrs.at_default_value);
8625   CHECKSUM_ATTR (attrs.at_digit_count);
8626   CHECKSUM_ATTR (attrs.at_discr);
8627   CHECKSUM_ATTR (attrs.at_discr_list);
8628   CHECKSUM_ATTR (attrs.at_discr_value);
8629   CHECKSUM_ATTR (attrs.at_encoding);
8630   CHECKSUM_ATTR (attrs.at_endianity);
8631   CHECKSUM_ATTR (attrs.at_explicit);
8632   CHECKSUM_ATTR (attrs.at_is_optional);
8633   CHECKSUM_ATTR (attrs.at_location);
8634   CHECKSUM_ATTR (attrs.at_lower_bound);
8635   CHECKSUM_ATTR (attrs.at_mutable);
8636   CHECKSUM_ATTR (attrs.at_ordering);
8637   CHECKSUM_ATTR (attrs.at_picture_string);
8638   CHECKSUM_ATTR (attrs.at_prototyped);
8639   CHECKSUM_ATTR (attrs.at_small);
8640   CHECKSUM_ATTR (attrs.at_segment);
8641   CHECKSUM_ATTR (attrs.at_string_length);
8642   CHECKSUM_ATTR (attrs.at_threads_scaled);
8643   CHECKSUM_ATTR (attrs.at_upper_bound);
8644   CHECKSUM_ATTR (attrs.at_use_location);
8645   CHECKSUM_ATTR (attrs.at_use_UTF8);
8646   CHECKSUM_ATTR (attrs.at_variable_parameter);
8647   CHECKSUM_ATTR (attrs.at_virtuality);
8648   CHECKSUM_ATTR (attrs.at_visibility);
8649   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
8650   CHECKSUM_ATTR (attrs.at_type);
8651   CHECKSUM_ATTR (attrs.at_friend);
8652
8653   /* Checksum the child DIEs, except for nested types and member functions.  */
8654   c = die->die_child;
8655   if (c) do {
8656     dw_attr_ref name_attr;
8657
8658     c = c->die_sib;
8659     name_attr = get_AT (c, DW_AT_name);
8660     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
8661         && name_attr != NULL)
8662       {
8663         CHECKSUM_ULEB128 ('S');
8664         CHECKSUM_ULEB128 (c->die_tag);
8665         CHECKSUM_STRING (AT_string (name_attr));
8666       }
8667     else
8668       {
8669         /* Mark this DIE so it gets processed when unmarking.  */
8670         if (c->die_mark == 0)
8671           c->die_mark = -1;
8672         die_checksum_ordered (c, ctx, mark);
8673       }
8674   } while (c != die->die_child);
8675
8676   CHECKSUM_ULEB128 (0);
8677 }
8678
8679 #undef CHECKSUM
8680 #undef CHECKSUM_STRING
8681 #undef CHECKSUM_ATTR
8682 #undef CHECKSUM_LEB128
8683 #undef CHECKSUM_ULEB128
8684
8685 /* Generate the type signature for DIE.  This is computed by generating an
8686    MD5 checksum over the DIE's tag, its relevant attributes, and its
8687    children.  Attributes that are references to other DIEs are processed
8688    by recursion, using the MARK field to prevent infinite recursion.
8689    If the DIE is nested inside a namespace or another type, we also
8690    need to include that context in the signature.  The lower 64 bits
8691    of the resulting MD5 checksum comprise the signature.  */
8692
8693 static void
8694 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
8695 {
8696   int mark;
8697   const char *name;
8698   unsigned char checksum[16];
8699   struct md5_ctx ctx;
8700   dw_die_ref decl;
8701
8702   name = get_AT_string (die, DW_AT_name);
8703   decl = get_AT_ref (die, DW_AT_specification);
8704
8705   /* First, compute a signature for just the type name (and its surrounding
8706      context, if any.  This is stored in the type unit DIE for link-time
8707      ODR (one-definition rule) checking.  */
8708
8709   if (is_cxx() && name != NULL)
8710     {
8711       md5_init_ctx (&ctx);
8712
8713       /* Checksum the names of surrounding namespaces and structures.  */
8714       if (decl != NULL && decl->die_parent != NULL)
8715         checksum_die_context (decl->die_parent, &ctx);
8716
8717       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
8718       md5_process_bytes (name, strlen (name) + 1, &ctx);
8719       md5_finish_ctx (&ctx, checksum);
8720
8721       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
8722     }
8723
8724   /* Next, compute the complete type signature.  */
8725
8726   md5_init_ctx (&ctx);
8727   mark = 1;
8728   die->die_mark = mark;
8729
8730   /* Checksum the names of surrounding namespaces and structures.  */
8731   if (decl != NULL && decl->die_parent != NULL)
8732     checksum_die_context (decl->die_parent, &ctx);
8733
8734   /* Checksum the DIE and its children.  */
8735   die_checksum_ordered (die, &ctx, &mark);
8736   unmark_all_dies (die);
8737   md5_finish_ctx (&ctx, checksum);
8738
8739   /* Store the signature in the type node and link the type DIE and the
8740      type node together.  */
8741   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
8742           DWARF_TYPE_SIGNATURE_SIZE);
8743   die->die_id.die_type_node = type_node;
8744   type_node->type_die = die;
8745
8746   /* If the DIE is a specification, link its declaration to the type node
8747      as well.  */
8748   if (decl != NULL)
8749     decl->die_id.die_type_node = type_node;
8750 }
8751
8752 /* Do the location expressions look same?  */
8753 static inline int
8754 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
8755 {
8756   return loc1->dw_loc_opc == loc2->dw_loc_opc
8757          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
8758          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
8759 }
8760
8761 /* Do the values look the same?  */
8762 static int
8763 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
8764 {
8765   dw_loc_descr_ref loc1, loc2;
8766   rtx r1, r2;
8767
8768   if (v1->val_class != v2->val_class)
8769     return 0;
8770
8771   switch (v1->val_class)
8772     {
8773     case dw_val_class_const:
8774       return v1->v.val_int == v2->v.val_int;
8775     case dw_val_class_unsigned_const:
8776       return v1->v.val_unsigned == v2->v.val_unsigned;
8777     case dw_val_class_const_double:
8778       return v1->v.val_double.high == v2->v.val_double.high
8779              && v1->v.val_double.low == v2->v.val_double.low;
8780     case dw_val_class_vec:
8781       if (v1->v.val_vec.length != v2->v.val_vec.length
8782           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
8783         return 0;
8784       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
8785                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
8786         return 0;
8787       return 1;
8788     case dw_val_class_flag:
8789       return v1->v.val_flag == v2->v.val_flag;
8790     case dw_val_class_str:
8791       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
8792
8793     case dw_val_class_addr:
8794       r1 = v1->v.val_addr;
8795       r2 = v2->v.val_addr;
8796       if (GET_CODE (r1) != GET_CODE (r2))
8797         return 0;
8798       return !rtx_equal_p (r1, r2);
8799
8800     case dw_val_class_offset:
8801       return v1->v.val_offset == v2->v.val_offset;
8802
8803     case dw_val_class_loc:
8804       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
8805            loc1 && loc2;
8806            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
8807         if (!same_loc_p (loc1, loc2, mark))
8808           return 0;
8809       return !loc1 && !loc2;
8810
8811     case dw_val_class_die_ref:
8812       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
8813
8814     case dw_val_class_fde_ref:
8815     case dw_val_class_lbl_id:
8816     case dw_val_class_lineptr:
8817     case dw_val_class_macptr:
8818       return 1;
8819
8820     case dw_val_class_file:
8821       return v1->v.val_file == v2->v.val_file;
8822
8823     case dw_val_class_data8:
8824       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
8825
8826     default:
8827       return 1;
8828     }
8829 }
8830
8831 /* Do the attributes look the same?  */
8832
8833 static int
8834 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
8835 {
8836   if (at1->dw_attr != at2->dw_attr)
8837     return 0;
8838
8839   /* We don't care that this was compiled with a different compiler
8840      snapshot; if the output is the same, that's what matters. */
8841   if (at1->dw_attr == DW_AT_producer)
8842     return 1;
8843
8844   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
8845 }
8846
8847 /* Do the dies look the same?  */
8848
8849 static int
8850 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
8851 {
8852   dw_die_ref c1, c2;
8853   dw_attr_ref a1;
8854   unsigned ix;
8855
8856   /* To avoid infinite recursion.  */
8857   if (die1->die_mark)
8858     return die1->die_mark == die2->die_mark;
8859   die1->die_mark = die2->die_mark = ++(*mark);
8860
8861   if (die1->die_tag != die2->die_tag)
8862     return 0;
8863
8864   if (VEC_length (dw_attr_node, die1->die_attr)
8865       != VEC_length (dw_attr_node, die2->die_attr))
8866     return 0;
8867
8868   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
8869     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
8870       return 0;
8871
8872   c1 = die1->die_child;
8873   c2 = die2->die_child;
8874   if (! c1)
8875     {
8876       if (c2)
8877         return 0;
8878     }
8879   else
8880     for (;;)
8881       {
8882         if (!same_die_p (c1, c2, mark))
8883           return 0;
8884         c1 = c1->die_sib;
8885         c2 = c2->die_sib;
8886         if (c1 == die1->die_child)
8887           {
8888             if (c2 == die2->die_child)
8889               break;
8890             else
8891               return 0;
8892           }
8893     }
8894
8895   return 1;
8896 }
8897
8898 /* Do the dies look the same?  Wrapper around same_die_p.  */
8899
8900 static int
8901 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8902 {
8903   int mark = 0;
8904   int ret = same_die_p (die1, die2, &mark);
8905
8906   unmark_all_dies (die1);
8907   unmark_all_dies (die2);
8908
8909   return ret;
8910 }
8911
8912 /* The prefix to attach to symbols on DIEs in the current comdat debug
8913    info section.  */
8914 static char *comdat_symbol_id;
8915
8916 /* The index of the current symbol within the current comdat CU.  */
8917 static unsigned int comdat_symbol_number;
8918
8919 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8920    children, and set comdat_symbol_id accordingly.  */
8921
8922 static void
8923 compute_section_prefix (dw_die_ref unit_die)
8924 {
8925   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8926   const char *base = die_name ? lbasename (die_name) : "anonymous";
8927   char *name = XALLOCAVEC (char, strlen (base) + 64);
8928   char *p;
8929   int i, mark;
8930   unsigned char checksum[16];
8931   struct md5_ctx ctx;
8932
8933   /* Compute the checksum of the DIE, then append part of it as hex digits to
8934      the name filename of the unit.  */
8935
8936   md5_init_ctx (&ctx);
8937   mark = 0;
8938   die_checksum (unit_die, &ctx, &mark);
8939   unmark_all_dies (unit_die);
8940   md5_finish_ctx (&ctx, checksum);
8941
8942   sprintf (name, "%s.", base);
8943   clean_symbol_name (name);
8944
8945   p = name + strlen (name);
8946   for (i = 0; i < 4; i++)
8947     {
8948       sprintf (p, "%.2x", checksum[i]);
8949       p += 2;
8950     }
8951
8952   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
8953   comdat_symbol_number = 0;
8954 }
8955
8956 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8957
8958 static int
8959 is_type_die (dw_die_ref die)
8960 {
8961   switch (die->die_tag)
8962     {
8963     case DW_TAG_array_type:
8964     case DW_TAG_class_type:
8965     case DW_TAG_interface_type:
8966     case DW_TAG_enumeration_type:
8967     case DW_TAG_pointer_type:
8968     case DW_TAG_reference_type:
8969     case DW_TAG_string_type:
8970     case DW_TAG_structure_type:
8971     case DW_TAG_subroutine_type:
8972     case DW_TAG_union_type:
8973     case DW_TAG_ptr_to_member_type:
8974     case DW_TAG_set_type:
8975     case DW_TAG_subrange_type:
8976     case DW_TAG_base_type:
8977     case DW_TAG_const_type:
8978     case DW_TAG_file_type:
8979     case DW_TAG_packed_type:
8980     case DW_TAG_volatile_type:
8981     case DW_TAG_typedef:
8982       return 1;
8983     default:
8984       return 0;
8985     }
8986 }
8987
8988 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8989    Basically, we want to choose the bits that are likely to be shared between
8990    compilations (types) and leave out the bits that are specific to individual
8991    compilations (functions).  */
8992
8993 static int
8994 is_comdat_die (dw_die_ref c)
8995 {
8996   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8997      we do for stabs.  The advantage is a greater likelihood of sharing between
8998      objects that don't include headers in the same order (and therefore would
8999      put the base types in a different comdat).  jason 8/28/00 */
9000
9001   if (c->die_tag == DW_TAG_base_type)
9002     return 0;
9003
9004   if (c->die_tag == DW_TAG_pointer_type
9005       || c->die_tag == DW_TAG_reference_type
9006       || c->die_tag == DW_TAG_const_type
9007       || c->die_tag == DW_TAG_volatile_type)
9008     {
9009       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9010
9011       return t ? is_comdat_die (t) : 0;
9012     }
9013
9014   return is_type_die (c);
9015 }
9016
9017 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9018    compilation unit.  */
9019
9020 static int
9021 is_symbol_die (dw_die_ref c)
9022 {
9023   return (is_type_die (c)
9024           || is_declaration_die (c)
9025           || c->die_tag == DW_TAG_namespace
9026           || c->die_tag == DW_TAG_module);
9027 }
9028
9029 static char *
9030 gen_internal_sym (const char *prefix)
9031 {
9032   char buf[256];
9033
9034   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9035   return xstrdup (buf);
9036 }
9037
9038 /* Assign symbols to all worthy DIEs under DIE.  */
9039
9040 static void
9041 assign_symbol_names (dw_die_ref die)
9042 {
9043   dw_die_ref c;
9044
9045   if (is_symbol_die (die))
9046     {
9047       if (comdat_symbol_id)
9048         {
9049           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9050
9051           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9052                    comdat_symbol_id, comdat_symbol_number++);
9053           die->die_id.die_symbol = xstrdup (p);
9054         }
9055       else
9056         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9057     }
9058
9059   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9060 }
9061
9062 struct cu_hash_table_entry
9063 {
9064   dw_die_ref cu;
9065   unsigned min_comdat_num, max_comdat_num;
9066   struct cu_hash_table_entry *next;
9067 };
9068
9069 /* Routines to manipulate hash table of CUs.  */
9070 static hashval_t
9071 htab_cu_hash (const void *of)
9072 {
9073   const struct cu_hash_table_entry *const entry =
9074     (const struct cu_hash_table_entry *) of;
9075
9076   return htab_hash_string (entry->cu->die_id.die_symbol);
9077 }
9078
9079 static int
9080 htab_cu_eq (const void *of1, const void *of2)
9081 {
9082   const struct cu_hash_table_entry *const entry1 =
9083     (const struct cu_hash_table_entry *) of1;
9084   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9085
9086   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9087 }
9088
9089 static void
9090 htab_cu_del (void *what)
9091 {
9092   struct cu_hash_table_entry *next,
9093     *entry = (struct cu_hash_table_entry *) what;
9094
9095   while (entry)
9096     {
9097       next = entry->next;
9098       free (entry);
9099       entry = next;
9100     }
9101 }
9102
9103 /* Check whether we have already seen this CU and set up SYM_NUM
9104    accordingly.  */
9105 static int
9106 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9107 {
9108   struct cu_hash_table_entry dummy;
9109   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9110
9111   dummy.max_comdat_num = 0;
9112
9113   slot = (struct cu_hash_table_entry **)
9114     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9115         INSERT);
9116   entry = *slot;
9117
9118   for (; entry; last = entry, entry = entry->next)
9119     {
9120       if (same_die_p_wrap (cu, entry->cu))
9121         break;
9122     }
9123
9124   if (entry)
9125     {
9126       *sym_num = entry->min_comdat_num;
9127       return 1;
9128     }
9129
9130   entry = XCNEW (struct cu_hash_table_entry);
9131   entry->cu = cu;
9132   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9133   entry->next = *slot;
9134   *slot = entry;
9135
9136   return 0;
9137 }
9138
9139 /* Record SYM_NUM to record of CU in HTABLE.  */
9140 static void
9141 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9142 {
9143   struct cu_hash_table_entry **slot, *entry;
9144
9145   slot = (struct cu_hash_table_entry **)
9146     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9147         NO_INSERT);
9148   entry = *slot;
9149
9150   entry->max_comdat_num = sym_num;
9151 }
9152
9153 /* Traverse the DIE (which is always comp_unit_die), and set up
9154    additional compilation units for each of the include files we see
9155    bracketed by BINCL/EINCL.  */
9156
9157 static void
9158 break_out_includes (dw_die_ref die)
9159 {
9160   dw_die_ref c;
9161   dw_die_ref unit = NULL;
9162   limbo_die_node *node, **pnode;
9163   htab_t cu_hash_table;
9164
9165   c = die->die_child;
9166   if (c) do {
9167     dw_die_ref prev = c;
9168     c = c->die_sib;
9169     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9170            || (unit && is_comdat_die (c)))
9171       {
9172         dw_die_ref next = c->die_sib;
9173
9174         /* This DIE is for a secondary CU; remove it from the main one.  */
9175         remove_child_with_prev (c, prev);
9176
9177         if (c->die_tag == DW_TAG_GNU_BINCL)
9178           unit = push_new_compile_unit (unit, c);
9179         else if (c->die_tag == DW_TAG_GNU_EINCL)
9180           unit = pop_compile_unit (unit);
9181         else
9182           add_child_die (unit, c);
9183         c = next;
9184         if (c == die->die_child)
9185           break;
9186       }
9187   } while (c != die->die_child);
9188
9189 #if 0
9190   /* We can only use this in debugging, since the frontend doesn't check
9191      to make sure that we leave every include file we enter.  */
9192   gcc_assert (!unit);
9193 #endif
9194
9195   assign_symbol_names (die);
9196   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9197   for (node = limbo_die_list, pnode = &limbo_die_list;
9198        node;
9199        node = node->next)
9200     {
9201       int is_dupl;
9202
9203       compute_section_prefix (node->die);
9204       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9205                         &comdat_symbol_number);
9206       assign_symbol_names (node->die);
9207       if (is_dupl)
9208         *pnode = node->next;
9209       else
9210         {
9211           pnode = &node->next;
9212           record_comdat_symbol_number (node->die, cu_hash_table,
9213                 comdat_symbol_number);
9214         }
9215     }
9216   htab_delete (cu_hash_table);
9217 }
9218
9219 /* Return non-zero if this DIE is a declaration.  */
9220
9221 static int
9222 is_declaration_die (dw_die_ref die)
9223 {
9224   dw_attr_ref a;
9225   unsigned ix;
9226
9227   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9228     if (a->dw_attr == DW_AT_declaration)
9229       return 1;
9230
9231   return 0;
9232 }
9233
9234 /* Return non-zero if this is a type DIE that should be moved to a
9235    COMDAT .debug_types section.  */
9236
9237 static int
9238 should_move_die_to_comdat (dw_die_ref die)
9239 {
9240   switch (die->die_tag)
9241     {
9242     case DW_TAG_class_type:
9243     case DW_TAG_structure_type:
9244     case DW_TAG_enumeration_type:
9245     case DW_TAG_union_type:
9246       /* Don't move declarations or inlined instances.  */
9247       if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin))
9248         return 0;
9249       return 1;
9250     case DW_TAG_array_type:
9251     case DW_TAG_interface_type:
9252     case DW_TAG_pointer_type:
9253     case DW_TAG_reference_type:
9254     case DW_TAG_string_type:
9255     case DW_TAG_subroutine_type:
9256     case DW_TAG_ptr_to_member_type:
9257     case DW_TAG_set_type:
9258     case DW_TAG_subrange_type:
9259     case DW_TAG_base_type:
9260     case DW_TAG_const_type:
9261     case DW_TAG_file_type:
9262     case DW_TAG_packed_type:
9263     case DW_TAG_volatile_type:
9264     case DW_TAG_typedef:
9265     default:
9266       return 0;
9267     }
9268 }
9269
9270 /* Make a clone of DIE.  */
9271
9272 static dw_die_ref
9273 clone_die (dw_die_ref die)
9274 {
9275   dw_die_ref clone;
9276   dw_attr_ref a;
9277   unsigned ix;
9278
9279   clone = GGC_CNEW (die_node);
9280   clone->die_tag = die->die_tag;
9281
9282   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9283     add_dwarf_attr (clone, a);
9284
9285   return clone;
9286 }
9287
9288 /* Make a clone of the tree rooted at DIE.  */
9289
9290 static dw_die_ref
9291 clone_tree (dw_die_ref die)
9292 {
9293   dw_die_ref c;
9294   dw_die_ref clone = clone_die (die);
9295
9296   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9297
9298   return clone;
9299 }
9300
9301 /* Make a clone of DIE as a declaration.  */
9302
9303 static dw_die_ref
9304 clone_as_declaration (dw_die_ref die)
9305 {
9306   dw_die_ref clone;
9307   dw_die_ref decl;
9308   dw_attr_ref a;
9309   unsigned ix;
9310
9311   /* If the DIE is already a declaration, just clone it.  */
9312   if (is_declaration_die (die))
9313     return clone_die (die);
9314
9315   /* If the DIE is a specification, just clone its declaration DIE.  */
9316   decl = get_AT_ref (die, DW_AT_specification);
9317   if (decl != NULL)
9318     return clone_die (decl);
9319
9320   clone = GGC_CNEW (die_node);
9321   clone->die_tag = die->die_tag;
9322
9323   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9324     {
9325       /* We don't want to copy over all attributes.
9326          For example we don't want DW_AT_byte_size because otherwise we will no
9327          longer have a declaration and GDB will treat it as a definition.  */
9328
9329       switch (a->dw_attr)
9330         {
9331         case DW_AT_artificial:
9332         case DW_AT_containing_type:
9333         case DW_AT_external:
9334         case DW_AT_name:
9335         case DW_AT_type:
9336         case DW_AT_virtuality:
9337         case DW_AT_MIPS_linkage_name:
9338           add_dwarf_attr (clone, a);
9339           break;
9340         case DW_AT_byte_size:
9341         default:
9342           break;
9343         }
9344     }
9345
9346   if (die->die_id.die_type_node)
9347     add_AT_die_ref (clone, DW_AT_signature, die);
9348
9349   add_AT_flag (clone, DW_AT_declaration, 1);
9350   return clone;
9351 }
9352
9353 /* Copy the declaration context to the new compile unit DIE.  This includes
9354    any surrounding namespace or type declarations.  If the DIE has an
9355    AT_specification attribute, it also includes attributes and children
9356    attached to the specification.  */
9357
9358 static void
9359 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9360 {
9361   dw_die_ref decl;
9362   dw_die_ref new_decl;
9363
9364   decl = get_AT_ref (die, DW_AT_specification);
9365   if (decl == NULL)
9366     decl = die;
9367   else
9368     {
9369       unsigned ix;
9370       dw_die_ref c;
9371       dw_attr_ref a;
9372
9373       /* Copy the type node pointer from the new DIE to the original
9374          declaration DIE so we can forward references later.  */
9375       decl->die_id.die_type_node = die->die_id.die_type_node;
9376
9377       remove_AT (die, DW_AT_specification);
9378
9379       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9380         {
9381           if (a->dw_attr != DW_AT_name
9382               && a->dw_attr != DW_AT_declaration
9383               && a->dw_attr != DW_AT_external)
9384             add_dwarf_attr (die, a);
9385         }
9386
9387       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9388     }
9389
9390   if (decl->die_parent != NULL
9391       && decl->die_parent->die_tag != DW_TAG_compile_unit
9392       && decl->die_parent->die_tag != DW_TAG_type_unit)
9393     {
9394       new_decl = copy_ancestor_tree (unit, decl, NULL);
9395       if (new_decl != NULL)
9396         {
9397           remove_AT (new_decl, DW_AT_signature);
9398           add_AT_specification (die, new_decl);
9399         }
9400     }
9401 }
9402
9403 /* Generate the skeleton ancestor tree for the given NODE, then clone
9404    the DIE and add the clone into the tree.  */
9405
9406 static void
9407 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9408 {
9409   if (node->new_die != NULL)
9410     return;
9411
9412   node->new_die = clone_as_declaration (node->old_die);
9413
9414   if (node->parent != NULL)
9415     {
9416       generate_skeleton_ancestor_tree (node->parent);
9417       add_child_die (node->parent->new_die, node->new_die);
9418     }
9419 }
9420
9421 /* Generate a skeleton tree of DIEs containing any declarations that are
9422    found in the original tree.  We traverse the tree looking for declaration
9423    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9424
9425 static void
9426 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9427 {
9428   skeleton_chain_node node;
9429   dw_die_ref c;
9430   dw_die_ref first;
9431   dw_die_ref prev = NULL;
9432   dw_die_ref next = NULL;
9433
9434   node.parent = parent;
9435
9436   first = c = parent->old_die->die_child;
9437   if (c)
9438     next = c->die_sib;
9439   if (c) do {
9440     if (prev == NULL || prev->die_sib == c)
9441       prev = c;
9442     c = next;
9443     next = (c == first ? NULL : c->die_sib);
9444     node.old_die = c;
9445     node.new_die = NULL;
9446     if (is_declaration_die (c))
9447       {
9448         /* Clone the existing DIE, move the original to the skeleton
9449            tree (which is in the main CU), and put the clone, with
9450            all the original's children, where the original came from.  */
9451         dw_die_ref clone = clone_die (c);
9452         move_all_children (c, clone);
9453
9454         replace_child (c, clone, prev);
9455         generate_skeleton_ancestor_tree (parent);
9456         add_child_die (parent->new_die, c);
9457         node.new_die = c;
9458         c = clone;
9459       }
9460     generate_skeleton_bottom_up (&node);
9461   } while (next != NULL);
9462 }
9463
9464 /* Wrapper function for generate_skeleton_bottom_up.  */
9465
9466 static dw_die_ref
9467 generate_skeleton (dw_die_ref die)
9468 {
9469   skeleton_chain_node node;
9470
9471   node.old_die = die;
9472   node.new_die = NULL;
9473   node.parent = NULL;
9474
9475   /* If this type definition is nested inside another type,
9476      always leave at least a declaration in its place.  */
9477   if (die->die_parent != NULL && is_type_die (die->die_parent))
9478     node.new_die = clone_as_declaration (die);
9479
9480   generate_skeleton_bottom_up (&node);
9481   return node.new_die;
9482 }
9483
9484 /* Remove the DIE from its parent, possibly replacing it with a cloned
9485    declaration.  The original DIE will be moved to a new compile unit
9486    so that existing references to it follow it to the new location.  If
9487    any of the original DIE's descendants is a declaration, we need to
9488    replace the original DIE with a skeleton tree and move the
9489    declarations back into the skeleton tree.  */
9490
9491 static dw_die_ref
9492 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9493 {
9494   dw_die_ref skeleton;
9495
9496   skeleton = generate_skeleton (child);
9497   if (skeleton == NULL)
9498     remove_child_with_prev (child, prev);
9499   else
9500     {
9501       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9502       replace_child (child, skeleton, prev);
9503     }
9504
9505   return skeleton;
9506 }
9507
9508 /* Traverse the DIE and set up additional .debug_types sections for each
9509    type worthy of being placed in a COMDAT section.  */
9510
9511 static void
9512 break_out_comdat_types (dw_die_ref die)
9513 {
9514   dw_die_ref c;
9515   dw_die_ref first;
9516   dw_die_ref prev = NULL;
9517   dw_die_ref next = NULL;
9518   dw_die_ref unit = NULL;
9519
9520   first = c = die->die_child;
9521   if (c)
9522     next = c->die_sib;
9523   if (c) do {
9524     if (prev == NULL || prev->die_sib == c)
9525       prev = c;
9526     c = next;
9527     next = (c == first ? NULL : c->die_sib);
9528     if (should_move_die_to_comdat (c))
9529       {
9530         dw_die_ref replacement;
9531         comdat_type_node_ref type_node;
9532
9533         /* Create a new type unit DIE as the root for the new tree, and
9534            add it to the list of comdat types.  */
9535         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9536         add_AT_unsigned (unit, DW_AT_language,
9537                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9538         type_node = GGC_CNEW (comdat_type_node);
9539         type_node->root_die = unit;
9540         type_node->next = comdat_type_list;
9541         comdat_type_list = type_node;
9542
9543         /* Generate the type signature.  */
9544         generate_type_signature (c, type_node);
9545
9546         /* Copy the declaration context, attributes, and children of the
9547            declaration into the new compile unit DIE.  */
9548         copy_declaration_context (unit, c);
9549
9550         /* Remove this DIE from the main CU.  */
9551         replacement = remove_child_or_replace_with_skeleton (c, prev);
9552
9553         /* Break out nested types into their own type units.  */
9554         break_out_comdat_types (c);
9555
9556         /* Add the DIE to the new compunit.  */
9557         add_child_die (unit, c);
9558
9559         if (replacement != NULL)
9560           c = replacement;
9561       }
9562     else if (c->die_tag == DW_TAG_namespace
9563              || c->die_tag == DW_TAG_class_type
9564              || c->die_tag == DW_TAG_structure_type
9565              || c->die_tag == DW_TAG_union_type)
9566       {
9567         /* Look for nested types that can be broken out.  */
9568         break_out_comdat_types (c);
9569       }
9570   } while (next != NULL);
9571 }
9572
9573 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9574
9575 struct decl_table_entry
9576 {
9577   dw_die_ref orig;
9578   dw_die_ref copy;
9579 };
9580
9581 /* Routines to manipulate hash table of copied declarations.  */
9582
9583 static hashval_t
9584 htab_decl_hash (const void *of)
9585 {
9586   const struct decl_table_entry *const entry =
9587     (const struct decl_table_entry *) of;
9588
9589   return htab_hash_pointer (entry->orig);
9590 }
9591
9592 static int
9593 htab_decl_eq (const void *of1, const void *of2)
9594 {
9595   const struct decl_table_entry *const entry1 =
9596     (const struct decl_table_entry *) of1;
9597   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9598
9599   return entry1->orig == entry2;
9600 }
9601
9602 static void
9603 htab_decl_del (void *what)
9604 {
9605   struct decl_table_entry *entry = (struct decl_table_entry *) what;
9606
9607   free (entry);
9608 }
9609
9610 /* Copy DIE and its ancestors, up to, but not including, the compile unit 
9611    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
9612    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
9613    to check if the ancestor has already been copied into UNIT.  */
9614
9615 static dw_die_ref
9616 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9617 {
9618   dw_die_ref parent = die->die_parent;
9619   dw_die_ref new_parent = unit;
9620   dw_die_ref copy;
9621   void **slot = NULL;
9622   struct decl_table_entry *entry = NULL;
9623
9624   if (decl_table)
9625     {
9626       /* Check if the entry has already been copied to UNIT.  */
9627       slot = htab_find_slot_with_hash (decl_table, die,
9628                                        htab_hash_pointer (die), INSERT);
9629       if (*slot != HTAB_EMPTY_ENTRY)
9630         {
9631           entry = (struct decl_table_entry *) *slot;
9632           return entry->copy;
9633         }
9634
9635       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
9636       entry = XCNEW (struct decl_table_entry);
9637       entry->orig = die;
9638       entry->copy = NULL;
9639       *slot = entry;
9640     }
9641
9642   if (parent != NULL)
9643     {
9644       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
9645       if (spec != NULL)
9646         parent = spec;
9647       if (parent->die_tag != DW_TAG_compile_unit
9648           && parent->die_tag != DW_TAG_type_unit)
9649         new_parent = copy_ancestor_tree (unit, parent, decl_table);
9650     }
9651
9652   copy = clone_as_declaration (die);
9653   add_child_die (new_parent, copy);
9654
9655   if (decl_table != NULL)
9656     {
9657       /* Make sure the copy is marked as part of the type unit.  */
9658       copy->die_mark = 1;
9659       /* Record the pointer to the copy.  */
9660       entry->copy = copy;
9661     }
9662
9663   return copy;
9664 }
9665
9666 /* Walk the DIE and its children, looking for references to incomplete
9667    or trivial types that are unmarked (i.e., that are not in the current
9668    type_unit).  */
9669
9670 static void
9671 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9672 {
9673   dw_die_ref c;
9674   dw_attr_ref a;
9675   unsigned ix;
9676
9677   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9678     {
9679       if (AT_class (a) == dw_val_class_die_ref)
9680         {
9681           dw_die_ref targ = AT_ref (a);
9682           comdat_type_node_ref type_node = targ->die_id.die_type_node;
9683           void **slot;
9684           struct decl_table_entry *entry;
9685
9686           if (targ->die_mark != 0 || type_node != NULL)
9687             continue;
9688
9689           slot = htab_find_slot_with_hash (decl_table, targ,
9690                                            htab_hash_pointer (targ), INSERT);
9691
9692           if (*slot != HTAB_EMPTY_ENTRY)
9693             {
9694               /* TARG has already been copied, so we just need to
9695                  modify the reference to point to the copy.  */
9696               entry = (struct decl_table_entry *) *slot;
9697               a->dw_attr_val.v.val_die_ref.die = entry->copy;
9698             }
9699           else
9700             {
9701               dw_die_ref parent = unit;
9702               dw_die_ref copy = clone_tree (targ);
9703
9704               /* Make sure the cloned tree is marked as part of the
9705                  type unit.  */
9706               mark_dies (copy);
9707
9708               /* Record in DECL_TABLE that TARG has been copied.
9709                  Need to do this now, before the recursive call,
9710                  because DECL_TABLE may be expanded and SLOT
9711                  would no longer be a valid pointer.  */
9712               entry = XCNEW (struct decl_table_entry);
9713               entry->orig = targ;
9714               entry->copy = copy;
9715               *slot = entry;
9716
9717               /* If TARG has surrounding context, copy its ancestor tree
9718                  into the new type unit.  */
9719               if (targ->die_parent != NULL
9720                   && targ->die_parent->die_tag != DW_TAG_compile_unit
9721                   && targ->die_parent->die_tag != DW_TAG_type_unit)
9722                 parent = copy_ancestor_tree (unit, targ->die_parent,
9723                                              decl_table);
9724
9725               add_child_die (parent, copy);
9726               a->dw_attr_val.v.val_die_ref.die = copy;
9727
9728               /* Make sure the newly-copied DIE is walked.  If it was
9729                  installed in a previously-added context, it won't
9730                  get visited otherwise.  */
9731               if (parent != unit)
9732                 copy_decls_walk (unit, parent, decl_table);
9733             }
9734         }
9735     }
9736
9737   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
9738 }
9739
9740 /* Copy declarations for "unworthy" types into the new comdat section.
9741    Incomplete types, modified types, and certain other types aren't broken
9742    out into comdat sections of their own, so they don't have a signature,
9743    and we need to copy the declaration into the same section so that we
9744    don't have an external reference.  */
9745
9746 static void
9747 copy_decls_for_unworthy_types (dw_die_ref unit)
9748 {
9749   htab_t decl_table;
9750
9751   mark_dies (unit);
9752   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
9753   copy_decls_walk (unit, unit, decl_table);
9754   htab_delete (decl_table);
9755   unmark_dies (unit);
9756 }
9757
9758 /* Traverse the DIE and add a sibling attribute if it may have the
9759    effect of speeding up access to siblings.  To save some space,
9760    avoid generating sibling attributes for DIE's without children.  */
9761
9762 static void
9763 add_sibling_attributes (dw_die_ref die)
9764 {
9765   dw_die_ref c;
9766
9767   if (! die->die_child)
9768     return;
9769
9770   if (die->die_parent && die != die->die_parent->die_child)
9771     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
9772
9773   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
9774 }
9775
9776 /* Output all location lists for the DIE and its children.  */
9777
9778 static void
9779 output_location_lists (dw_die_ref die)
9780 {
9781   dw_die_ref c;
9782   dw_attr_ref a;
9783   unsigned ix;
9784
9785   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9786     if (AT_class (a) == dw_val_class_loc_list)
9787       output_loc_list (AT_loc_list (a));
9788
9789   FOR_EACH_CHILD (die, c, output_location_lists (c));
9790 }
9791
9792 /* The format of each DIE (and its attribute value pairs) is encoded in an
9793    abbreviation table.  This routine builds the abbreviation table and assigns
9794    a unique abbreviation id for each abbreviation entry.  The children of each
9795    die are visited recursively.  */
9796
9797 static void
9798 build_abbrev_table (dw_die_ref die)
9799 {
9800   unsigned long abbrev_id;
9801   unsigned int n_alloc;
9802   dw_die_ref c;
9803   dw_attr_ref a;
9804   unsigned ix;
9805
9806   /* Scan the DIE references, and mark as external any that refer to
9807      DIEs from other CUs (i.e. those which are not marked).  */
9808   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9809     if (AT_class (a) == dw_val_class_die_ref
9810         && AT_ref (a)->die_mark == 0)
9811       {
9812         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
9813         set_AT_ref_external (a, 1);
9814       }
9815
9816   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
9817     {
9818       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
9819       dw_attr_ref die_a, abbrev_a;
9820       unsigned ix;
9821       bool ok = true;
9822
9823       if (abbrev->die_tag != die->die_tag)
9824         continue;
9825       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9826         continue;
9827
9828       if (VEC_length (dw_attr_node, abbrev->die_attr)
9829           != VEC_length (dw_attr_node, die->die_attr))
9830         continue;
9831
9832       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
9833         {
9834           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
9835           if ((abbrev_a->dw_attr != die_a->dw_attr)
9836               || (value_format (abbrev_a) != value_format (die_a)))
9837             {
9838               ok = false;
9839               break;
9840             }
9841         }
9842       if (ok)
9843         break;
9844     }
9845
9846   if (abbrev_id >= abbrev_die_table_in_use)
9847     {
9848       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
9849         {
9850           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
9851           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
9852                                             n_alloc);
9853
9854           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
9855                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
9856           abbrev_die_table_allocated = n_alloc;
9857         }
9858
9859       ++abbrev_die_table_in_use;
9860       abbrev_die_table[abbrev_id] = die;
9861     }
9862
9863   die->die_abbrev = abbrev_id;
9864   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
9865 }
9866 \f
9867 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9868
9869 static int
9870 constant_size (unsigned HOST_WIDE_INT value)
9871 {
9872   int log;
9873
9874   if (value == 0)
9875     log = 0;
9876   else
9877     log = floor_log2 (value);
9878
9879   log = log / 8;
9880   log = 1 << (floor_log2 (log) + 1);
9881
9882   return log;
9883 }
9884
9885 /* Return the size of a DIE as it is represented in the
9886    .debug_info section.  */
9887
9888 static unsigned long
9889 size_of_die (dw_die_ref die)
9890 {
9891   unsigned long size = 0;
9892   dw_attr_ref a;
9893   unsigned ix;
9894
9895   size += size_of_uleb128 (die->die_abbrev);
9896   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9897     {
9898       switch (AT_class (a))
9899         {
9900         case dw_val_class_addr:
9901           size += DWARF2_ADDR_SIZE;
9902           break;
9903         case dw_val_class_offset:
9904           size += DWARF_OFFSET_SIZE;
9905           break;
9906         case dw_val_class_loc:
9907           {
9908             unsigned long lsize = size_of_locs (AT_loc (a));
9909
9910             /* Block length.  */
9911             size += constant_size (lsize);
9912             size += lsize;
9913           }
9914           break;
9915         case dw_val_class_loc_list:
9916           size += DWARF_OFFSET_SIZE;
9917           break;
9918         case dw_val_class_range_list:
9919           size += DWARF_OFFSET_SIZE;
9920           break;
9921         case dw_val_class_const:
9922           size += size_of_sleb128 (AT_int (a));
9923           break;
9924         case dw_val_class_unsigned_const:
9925           size += constant_size (AT_unsigned (a));
9926           break;
9927         case dw_val_class_const_double:
9928           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9929           if (HOST_BITS_PER_WIDE_INT >= 64)
9930             size++; /* block */
9931           break;
9932         case dw_val_class_vec:
9933           size += constant_size (a->dw_attr_val.v.val_vec.length
9934                                  * a->dw_attr_val.v.val_vec.elt_size)
9935                   + a->dw_attr_val.v.val_vec.length
9936                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
9937           break;
9938         case dw_val_class_flag:
9939           size += 1;
9940           break;
9941         case dw_val_class_die_ref:
9942           if (AT_ref_external (a))
9943             {
9944               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
9945                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9946                  is sized by target address length, whereas in DWARF3
9947                  it's always sized as an offset.  */
9948               if (dwarf_version >= 4)
9949                 size += DWARF_TYPE_SIGNATURE_SIZE;
9950               else if (dwarf_version == 2)
9951                 size += DWARF2_ADDR_SIZE;
9952               else
9953                 size += DWARF_OFFSET_SIZE;
9954             }
9955           else
9956             size += DWARF_OFFSET_SIZE;
9957           break;
9958         case dw_val_class_fde_ref:
9959           size += DWARF_OFFSET_SIZE;
9960           break;
9961         case dw_val_class_lbl_id:
9962           size += DWARF2_ADDR_SIZE;
9963           break;
9964         case dw_val_class_lineptr:
9965         case dw_val_class_macptr:
9966           size += DWARF_OFFSET_SIZE;
9967           break;
9968         case dw_val_class_str:
9969           if (AT_string_form (a) == DW_FORM_strp)
9970             size += DWARF_OFFSET_SIZE;
9971           else
9972             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9973           break;
9974         case dw_val_class_file:
9975           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9976           break;
9977         case dw_val_class_data8:
9978           size += 8;
9979           break;
9980         default:
9981           gcc_unreachable ();
9982         }
9983     }
9984
9985   return size;
9986 }
9987
9988 /* Size the debugging information associated with a given DIE.  Visits the
9989    DIE's children recursively.  Updates the global variable next_die_offset, on
9990    each time through.  Uses the current value of next_die_offset to update the
9991    die_offset field in each DIE.  */
9992
9993 static void
9994 calc_die_sizes (dw_die_ref die)
9995 {
9996   dw_die_ref c;
9997
9998   die->die_offset = next_die_offset;
9999   next_die_offset += size_of_die (die);
10000
10001   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10002
10003   if (die->die_child != NULL)
10004     /* Count the null byte used to terminate sibling lists.  */
10005     next_die_offset += 1;
10006 }
10007
10008 /* Set the marks for a die and its children.  We do this so
10009    that we know whether or not a reference needs to use FORM_ref_addr; only
10010    DIEs in the same CU will be marked.  We used to clear out the offset
10011    and use that as the flag, but ran into ordering problems.  */
10012
10013 static void
10014 mark_dies (dw_die_ref die)
10015 {
10016   dw_die_ref c;
10017
10018   gcc_assert (!die->die_mark);
10019
10020   die->die_mark = 1;
10021   FOR_EACH_CHILD (die, c, mark_dies (c));
10022 }
10023
10024 /* Clear the marks for a die and its children.  */
10025
10026 static void
10027 unmark_dies (dw_die_ref die)
10028 {
10029   dw_die_ref c;
10030
10031   if (dwarf_version < 4)
10032     gcc_assert (die->die_mark);
10033
10034   die->die_mark = 0;
10035   FOR_EACH_CHILD (die, c, unmark_dies (c));
10036 }
10037
10038 /* Clear the marks for a die, its children and referred dies.  */
10039
10040 static void
10041 unmark_all_dies (dw_die_ref die)
10042 {
10043   dw_die_ref c;
10044   dw_attr_ref a;
10045   unsigned ix;
10046
10047   if (!die->die_mark)
10048     return;
10049   die->die_mark = 0;
10050
10051   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10052
10053   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10054     if (AT_class (a) == dw_val_class_die_ref)
10055       unmark_all_dies (AT_ref (a));
10056 }
10057
10058 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10059    generated for the compilation unit.  */
10060
10061 static unsigned long
10062 size_of_pubnames (VEC (pubname_entry, gc) * names)
10063 {
10064   unsigned long size;
10065   unsigned i;
10066   pubname_ref p;
10067
10068   size = DWARF_PUBNAMES_HEADER_SIZE;
10069   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10070     if (names != pubtype_table
10071         || p->die->die_offset != 0
10072         || !flag_eliminate_unused_debug_types)
10073       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10074
10075   size += DWARF_OFFSET_SIZE;
10076   return size;
10077 }
10078
10079 /* Return the size of the information in the .debug_aranges section.  */
10080
10081 static unsigned long
10082 size_of_aranges (void)
10083 {
10084   unsigned long size;
10085
10086   size = DWARF_ARANGES_HEADER_SIZE;
10087
10088   /* Count the address/length pair for this compilation unit.  */
10089   if (text_section_used)
10090     size += 2 * DWARF2_ADDR_SIZE;
10091   if (cold_text_section_used)
10092     size += 2 * DWARF2_ADDR_SIZE;
10093   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10094
10095   /* Count the two zero words used to terminated the address range table.  */
10096   size += 2 * DWARF2_ADDR_SIZE;
10097   return size;
10098 }
10099 \f
10100 /* Select the encoding of an attribute value.  */
10101
10102 static enum dwarf_form
10103 value_format (dw_attr_ref a)
10104 {
10105   switch (a->dw_attr_val.val_class)
10106     {
10107     case dw_val_class_addr:
10108       /* Only very few attributes allow DW_FORM_addr.  */
10109       switch (a->dw_attr)
10110         {
10111         case DW_AT_low_pc:
10112         case DW_AT_high_pc:
10113         case DW_AT_entry_pc:
10114         case DW_AT_trampoline:
10115           return DW_FORM_addr;
10116         default:
10117           break;
10118         }
10119       switch (DWARF2_ADDR_SIZE)
10120         {
10121         case 1:
10122           return DW_FORM_data1;
10123         case 2:
10124           return DW_FORM_data2;
10125         case 4:
10126           return DW_FORM_data4;
10127         case 8:
10128           return DW_FORM_data8;
10129         default:
10130           gcc_unreachable ();
10131         }
10132     case dw_val_class_range_list:
10133     case dw_val_class_offset:
10134     case dw_val_class_loc_list:
10135       switch (DWARF_OFFSET_SIZE)
10136         {
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_loc:
10145       switch (constant_size (size_of_locs (AT_loc (a))))
10146         {
10147         case 1:
10148           return DW_FORM_block1;
10149         case 2:
10150           return DW_FORM_block2;
10151         default:
10152           gcc_unreachable ();
10153         }
10154     case dw_val_class_const:
10155       return DW_FORM_sdata;
10156     case dw_val_class_unsigned_const:
10157       switch (constant_size (AT_unsigned (a)))
10158         {
10159         case 1:
10160           return DW_FORM_data1;
10161         case 2:
10162           return DW_FORM_data2;
10163         case 4:
10164           return DW_FORM_data4;
10165         case 8:
10166           return DW_FORM_data8;
10167         default:
10168           gcc_unreachable ();
10169         }
10170     case dw_val_class_const_double:
10171       switch (HOST_BITS_PER_WIDE_INT)
10172         {
10173         case 8:
10174           return DW_FORM_data2;
10175         case 16:
10176           return DW_FORM_data4;
10177         case 32:
10178           return DW_FORM_data8;
10179         case 64:
10180         default:
10181           return DW_FORM_block1;
10182         }
10183     case dw_val_class_vec:
10184       switch (constant_size (a->dw_attr_val.v.val_vec.length
10185                              * a->dw_attr_val.v.val_vec.elt_size))
10186         {
10187         case 1:
10188           return DW_FORM_block1;
10189         case 2:
10190           return DW_FORM_block2;
10191         case 4:
10192           return DW_FORM_block4;
10193         default:
10194           gcc_unreachable ();
10195         }
10196     case dw_val_class_flag:
10197       return DW_FORM_flag;
10198     case dw_val_class_die_ref:
10199       if (AT_ref_external (a))
10200         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10201       else
10202         return DW_FORM_ref;
10203     case dw_val_class_fde_ref:
10204       return DW_FORM_data;
10205     case dw_val_class_lbl_id:
10206       return DW_FORM_addr;
10207     case dw_val_class_lineptr:
10208     case dw_val_class_macptr:
10209       return DW_FORM_data;
10210     case dw_val_class_str:
10211       return AT_string_form (a);
10212     case dw_val_class_file:
10213       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10214         {
10215         case 1:
10216           return DW_FORM_data1;
10217         case 2:
10218           return DW_FORM_data2;
10219         case 4:
10220           return DW_FORM_data4;
10221         default:
10222           gcc_unreachable ();
10223         }
10224
10225     case dw_val_class_data8:
10226       return DW_FORM_data8;
10227
10228     default:
10229       gcc_unreachable ();
10230     }
10231 }
10232
10233 /* Output the encoding of an attribute value.  */
10234
10235 static void
10236 output_value_format (dw_attr_ref a)
10237 {
10238   enum dwarf_form form = value_format (a);
10239
10240   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10241 }
10242
10243 /* Output the .debug_abbrev section which defines the DIE abbreviation
10244    table.  */
10245
10246 static void
10247 output_abbrev_section (void)
10248 {
10249   unsigned long abbrev_id;
10250
10251   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10252     {
10253       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10254       unsigned ix;
10255       dw_attr_ref a_attr;
10256
10257       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10258       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10259                                    dwarf_tag_name (abbrev->die_tag));
10260
10261       if (abbrev->die_child != NULL)
10262         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10263       else
10264         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10265
10266       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10267            ix++)
10268         {
10269           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10270                                        dwarf_attr_name (a_attr->dw_attr));
10271           output_value_format (a_attr);
10272         }
10273
10274       dw2_asm_output_data (1, 0, NULL);
10275       dw2_asm_output_data (1, 0, NULL);
10276     }
10277
10278   /* Terminate the table.  */
10279   dw2_asm_output_data (1, 0, NULL);
10280 }
10281
10282 /* Output a symbol we can use to refer to this DIE from another CU.  */
10283
10284 static inline void
10285 output_die_symbol (dw_die_ref die)
10286 {
10287   char *sym = die->die_id.die_symbol;
10288
10289   if (sym == 0)
10290     return;
10291
10292   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10293     /* We make these global, not weak; if the target doesn't support
10294        .linkonce, it doesn't support combining the sections, so debugging
10295        will break.  */
10296     targetm.asm_out.globalize_label (asm_out_file, sym);
10297
10298   ASM_OUTPUT_LABEL (asm_out_file, sym);
10299 }
10300
10301 /* Return a new location list, given the begin and end range, and the
10302    expression. gensym tells us whether to generate a new internal symbol for
10303    this location list node, which is done for the head of the list only.  */
10304
10305 static inline dw_loc_list_ref
10306 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10307               const char *section, unsigned int gensym)
10308 {
10309   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
10310
10311   retlist->begin = begin;
10312   retlist->end = end;
10313   retlist->expr = expr;
10314   retlist->section = section;
10315   if (gensym)
10316     retlist->ll_symbol = gen_internal_sym ("LLST");
10317
10318   return retlist;
10319 }
10320
10321 /* Add a location description expression to a location list.  */
10322
10323 static inline void
10324 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
10325                            const char *begin, const char *end,
10326                            const char *section)
10327 {
10328   dw_loc_list_ref *d;
10329
10330   /* Find the end of the chain.  */
10331   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
10332     ;
10333
10334   /* Add a new location list node to the list.  */
10335   *d = new_loc_list (descr, begin, end, section, 0);
10336 }
10337
10338 /* Output the location list given to us.  */
10339
10340 static void
10341 output_loc_list (dw_loc_list_ref list_head)
10342 {
10343   dw_loc_list_ref curr = list_head;
10344
10345   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10346
10347   /* Walk the location list, and output each range + expression.  */
10348   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10349     {
10350       unsigned long size;
10351       /* Don't output an entry that starts and ends at the same address.  */
10352       if (strcmp (curr->begin, curr->end) == 0)
10353         continue;
10354       if (!have_multiple_function_sections)
10355         {
10356           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10357                                 "Location list begin address (%s)",
10358                                 list_head->ll_symbol);
10359           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10360                                 "Location list end address (%s)",
10361                                 list_head->ll_symbol);
10362         }
10363       else
10364         {
10365           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10366                                "Location list begin address (%s)",
10367                                list_head->ll_symbol);
10368           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10369                                "Location list end address (%s)",
10370                                list_head->ll_symbol);
10371         }
10372       size = size_of_locs (curr->expr);
10373
10374       /* Output the block length for this list of location operations.  */
10375       gcc_assert (size <= 0xffff);
10376       dw2_asm_output_data (2, size, "%s", "Location expression size");
10377
10378       output_loc_sequence (curr->expr);
10379     }
10380
10381   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10382                        "Location list terminator begin (%s)",
10383                        list_head->ll_symbol);
10384   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10385                        "Location list terminator end (%s)",
10386                        list_head->ll_symbol);
10387 }
10388
10389 /* Output a type signature.  */
10390
10391 static inline void
10392 output_signature (const char *sig, const char *name)
10393 {
10394   int i;
10395
10396   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10397     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10398 }
10399
10400 /* Output the DIE and its attributes.  Called recursively to generate
10401    the definitions of each child DIE.  */
10402
10403 static void
10404 output_die (dw_die_ref die)
10405 {
10406   dw_attr_ref a;
10407   dw_die_ref c;
10408   unsigned long size;
10409   unsigned ix;
10410
10411   /* If someone in another CU might refer to us, set up a symbol for
10412      them to point to.  */
10413   if (dwarf_version < 4 && die->die_id.die_symbol)
10414     output_die_symbol (die);
10415
10416   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
10417                                (unsigned long)die->die_offset,
10418                                dwarf_tag_name (die->die_tag));
10419
10420   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10421     {
10422       const char *name = dwarf_attr_name (a->dw_attr);
10423
10424       switch (AT_class (a))
10425         {
10426         case dw_val_class_addr:
10427           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10428           break;
10429
10430         case dw_val_class_offset:
10431           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10432                                "%s", name);
10433           break;
10434
10435         case dw_val_class_range_list:
10436           {
10437             char *p = strchr (ranges_section_label, '\0');
10438
10439             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10440                      a->dw_attr_val.v.val_offset);
10441             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10442                                    debug_ranges_section, "%s", name);
10443             *p = '\0';
10444           }
10445           break;
10446
10447         case dw_val_class_loc:
10448           size = size_of_locs (AT_loc (a));
10449
10450           /* Output the block length for this list of location operations.  */
10451           dw2_asm_output_data (constant_size (size), size, "%s", name);
10452
10453           output_loc_sequence (AT_loc (a));
10454           break;
10455
10456         case dw_val_class_const:
10457           /* ??? It would be slightly more efficient to use a scheme like is
10458              used for unsigned constants below, but gdb 4.x does not sign
10459              extend.  Gdb 5.x does sign extend.  */
10460           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10461           break;
10462
10463         case dw_val_class_unsigned_const:
10464           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10465                                AT_unsigned (a), "%s", name);
10466           break;
10467
10468         case dw_val_class_const_double:
10469           {
10470             unsigned HOST_WIDE_INT first, second;
10471
10472             if (HOST_BITS_PER_WIDE_INT >= 64)
10473               dw2_asm_output_data (1,
10474                                    2 * HOST_BITS_PER_WIDE_INT
10475                                    / HOST_BITS_PER_CHAR,
10476                                    NULL);
10477
10478             if (WORDS_BIG_ENDIAN)
10479               {
10480                 first = a->dw_attr_val.v.val_double.high;
10481                 second = a->dw_attr_val.v.val_double.low;
10482               }
10483             else
10484               {
10485                 first = a->dw_attr_val.v.val_double.low;
10486                 second = a->dw_attr_val.v.val_double.high;
10487               }
10488
10489             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10490                                  first, name);
10491             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10492                                  second, NULL);
10493           }
10494           break;
10495
10496         case dw_val_class_vec:
10497           {
10498             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10499             unsigned int len = a->dw_attr_val.v.val_vec.length;
10500             unsigned int i;
10501             unsigned char *p;
10502
10503             dw2_asm_output_data (constant_size (len * elt_size),
10504                                  len * elt_size, "%s", name);
10505             if (elt_size > sizeof (HOST_WIDE_INT))
10506               {
10507                 elt_size /= 2;
10508                 len *= 2;
10509               }
10510             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10511                  i < len;
10512                  i++, p += elt_size)
10513               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10514                                    "fp or vector constant word %u", i);
10515             break;
10516           }
10517
10518         case dw_val_class_flag:
10519           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10520           break;
10521
10522         case dw_val_class_loc_list:
10523           {
10524             char *sym = AT_loc_list (a)->ll_symbol;
10525
10526             gcc_assert (sym);
10527             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10528                                    "%s", name);
10529           }
10530           break;
10531
10532         case dw_val_class_die_ref:
10533           if (AT_ref_external (a))
10534             {
10535               if (dwarf_version >= 4)
10536                 {
10537                   comdat_type_node_ref type_node =
10538                     AT_ref (a)->die_id.die_type_node;
10539
10540                   gcc_assert (type_node);
10541                   output_signature (type_node->signature, name);
10542                 }
10543               else
10544                 {
10545                   char *sym = AT_ref (a)->die_id.die_symbol;
10546                   int size;
10547
10548                   gcc_assert (sym);
10549                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
10550                      length, whereas in DWARF3 it's always sized as an
10551                      offset.  */
10552                   if (dwarf_version == 2)
10553                     size = DWARF2_ADDR_SIZE;
10554                   else
10555                     size = DWARF_OFFSET_SIZE;
10556                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10557                                          name);
10558                 }
10559             }
10560           else
10561             {
10562               gcc_assert (AT_ref (a)->die_offset);
10563               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10564                                    "%s", name);
10565             }
10566           break;
10567
10568         case dw_val_class_fde_ref:
10569           {
10570             char l1[20];
10571
10572             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10573                                          a->dw_attr_val.v.val_fde_index * 2);
10574             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10575                                    "%s", name);
10576           }
10577           break;
10578
10579         case dw_val_class_lbl_id:
10580           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10581           break;
10582
10583         case dw_val_class_lineptr:
10584           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10585                                  debug_line_section, "%s", name);
10586           break;
10587
10588         case dw_val_class_macptr:
10589           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10590                                  debug_macinfo_section, "%s", name);
10591           break;
10592
10593         case dw_val_class_str:
10594           if (AT_string_form (a) == DW_FORM_strp)
10595             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10596                                    a->dw_attr_val.v.val_str->label,
10597                                    debug_str_section,
10598                                    "%s: \"%s\"", name, AT_string (a));
10599           else
10600             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10601           break;
10602
10603         case dw_val_class_file:
10604           {
10605             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10606
10607             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10608                                  a->dw_attr_val.v.val_file->filename);
10609             break;
10610           }
10611
10612         case dw_val_class_data8:
10613           {
10614             int i;
10615
10616             for (i = 0; i < 8; i++)
10617               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10618                                    i == 0 ? "%s" : NULL, name);
10619             break;
10620           }
10621
10622         default:
10623           gcc_unreachable ();
10624         }
10625     }
10626
10627   FOR_EACH_CHILD (die, c, output_die (c));
10628
10629   /* Add null byte to terminate sibling list.  */
10630   if (die->die_child != NULL)
10631     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
10632                          (unsigned long) die->die_offset);
10633 }
10634
10635 /* Output the compilation unit that appears at the beginning of the
10636    .debug_info section, and precedes the DIE descriptions.  */
10637
10638 static void
10639 output_compilation_unit_header (void)
10640 {
10641   int ver = dwarf_version;
10642
10643   /* Don't mark the output as DWARF-4 until we make full use of the
10644      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
10645      selects only a few extensions from the DWARF-4 spec.  */
10646   if (ver > 3)
10647     ver = 3;
10648   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10649     dw2_asm_output_data (4, 0xffffffff,
10650       "Initial length escape value indicating 64-bit DWARF extension");
10651   dw2_asm_output_data (DWARF_OFFSET_SIZE,
10652                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10653                        "Length of Compilation Unit Info");
10654   dw2_asm_output_data (2, ver, "DWARF version number");
10655   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10656                          debug_abbrev_section,
10657                          "Offset Into Abbrev. Section");
10658   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10659 }
10660
10661 /* Output the compilation unit DIE and its children.  */
10662
10663 static void
10664 output_comp_unit (dw_die_ref die, int output_if_empty)
10665 {
10666   const char *secname;
10667   char *oldsym, *tmp;
10668
10669   /* Unless we are outputting main CU, we may throw away empty ones.  */
10670   if (!output_if_empty && die->die_child == NULL)
10671     return;
10672
10673   /* Even if there are no children of this DIE, we must output the information
10674      about the compilation unit.  Otherwise, on an empty translation unit, we
10675      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
10676      will then complain when examining the file.  First mark all the DIEs in
10677      this CU so we know which get local refs.  */
10678   mark_dies (die);
10679
10680   build_abbrev_table (die);
10681
10682   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10683   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10684   calc_die_sizes (die);
10685
10686   oldsym = die->die_id.die_symbol;
10687   if (oldsym)
10688     {
10689       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10690
10691       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10692       secname = tmp;
10693       die->die_id.die_symbol = NULL;
10694       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10695     }
10696   else
10697     switch_to_section (debug_info_section);
10698
10699   /* Output debugging information.  */
10700   output_compilation_unit_header ();
10701   output_die (die);
10702
10703   /* Leave the marks on the main CU, so we can check them in
10704      output_pubnames.  */
10705   if (oldsym)
10706     {
10707       unmark_dies (die);
10708       die->die_id.die_symbol = oldsym;
10709     }
10710 }
10711
10712 /* Output a comdat type unit DIE and its children.  */
10713
10714 static void
10715 output_comdat_type_unit (comdat_type_node *node)
10716 {
10717   const char *secname;
10718   char *tmp;
10719   int i;
10720 #if defined (OBJECT_FORMAT_ELF)
10721   tree comdat_key;
10722 #endif
10723
10724   /* First mark all the DIEs in this CU so we know which get local refs.  */
10725   mark_dies (node->root_die);
10726
10727   build_abbrev_table (node->root_die);
10728
10729   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10730   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10731   calc_die_sizes (node->root_die);
10732
10733 #if defined (OBJECT_FORMAT_ELF)
10734   secname = ".debug_types";
10735   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10736   sprintf (tmp, "wt.");
10737   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10738     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10739   comdat_key = get_identifier (tmp);
10740   targetm.asm_out.named_section (secname,
10741                                  SECTION_DEBUG | SECTION_LINKONCE,
10742                                  comdat_key);
10743 #else
10744   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10745   sprintf (tmp, ".gnu.linkonce.wt.");
10746   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10747     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10748   secname = tmp;
10749   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10750 #endif
10751
10752   /* Output debugging information.  */
10753   output_compilation_unit_header ();
10754   output_signature (node->signature, "Type Signature");
10755   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10756                        "Offset to Type DIE");
10757   output_die (node->root_die);
10758
10759   unmark_dies (node->root_die);
10760 }
10761
10762 /* Return the DWARF2/3 pubname associated with a decl.  */
10763
10764 static const char *
10765 dwarf2_name (tree decl, int scope)
10766 {
10767   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10768 }
10769
10770 /* Add a new entry to .debug_pubnames if appropriate.  */
10771
10772 static void
10773 add_pubname_string (const char *str, dw_die_ref die)
10774 {
10775   pubname_entry e;
10776
10777   e.die = die;
10778   e.name = xstrdup (str);
10779   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
10780 }
10781
10782 static void
10783 add_pubname (tree decl, dw_die_ref die)
10784 {
10785   if (TREE_PUBLIC (decl))
10786     {
10787       const char *name = dwarf2_name (decl, 1);
10788       if (name)
10789         add_pubname_string (name, die);
10790     }
10791 }
10792
10793 /* Add a new entry to .debug_pubtypes if appropriate.  */
10794
10795 static void
10796 add_pubtype (tree decl, dw_die_ref die)
10797 {
10798   pubname_entry e;
10799
10800   e.name = NULL;
10801   if ((TREE_PUBLIC (decl)
10802        || die->die_parent == comp_unit_die)
10803       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10804     {
10805       e.die = die;
10806       if (TYPE_P (decl))
10807         {
10808           if (TYPE_NAME (decl))
10809             {
10810               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
10811                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
10812               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
10813                        && DECL_NAME (TYPE_NAME (decl)))
10814                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
10815               else
10816                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
10817             }
10818         }
10819       else
10820         {
10821           e.name = dwarf2_name (decl, 1);
10822           if (e.name)
10823             e.name = xstrdup (e.name);
10824         }
10825
10826       /* If we don't have a name for the type, there's no point in adding
10827          it to the table.  */
10828       if (e.name && e.name[0] != '\0')
10829         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
10830     }
10831 }
10832
10833 /* Output the public names table used to speed up access to externally
10834    visible names; or the public types table used to find type definitions.  */
10835
10836 static void
10837 output_pubnames (VEC (pubname_entry, gc) * names)
10838 {
10839   unsigned i;
10840   unsigned long pubnames_length = size_of_pubnames (names);
10841   pubname_ref pub;
10842
10843   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10844     dw2_asm_output_data (4, 0xffffffff,
10845       "Initial length escape value indicating 64-bit DWARF extension");
10846   if (names == pubname_table)
10847     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10848                          "Length of Public Names Info");
10849   else
10850     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10851                          "Length of Public Type Names Info");
10852   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
10853   dw2_asm_output_data (2, 2, "DWARF Version");
10854   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10855                          debug_info_section,
10856                          "Offset of Compilation Unit Info");
10857   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10858                        "Compilation Unit Length");
10859
10860   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
10861     {
10862       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
10863       if (names == pubname_table)
10864         gcc_assert (pub->die->die_mark);
10865
10866       if (names != pubtype_table
10867           || pub->die->die_offset != 0
10868           || !flag_eliminate_unused_debug_types)
10869         {
10870           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
10871                                "DIE offset");
10872
10873           dw2_asm_output_nstring (pub->name, -1, "external name");
10874         }
10875     }
10876
10877   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10878 }
10879
10880 /* Add a new entry to .debug_aranges if appropriate.  */
10881
10882 static void
10883 add_arange (tree decl, dw_die_ref die)
10884 {
10885   if (! DECL_SECTION_NAME (decl))
10886     return;
10887
10888   if (arange_table_in_use == arange_table_allocated)
10889     {
10890       arange_table_allocated += ARANGE_TABLE_INCREMENT;
10891       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
10892                                     arange_table_allocated);
10893       memset (arange_table + arange_table_in_use, 0,
10894               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
10895     }
10896
10897   arange_table[arange_table_in_use++] = die;
10898 }
10899
10900 /* Output the information that goes into the .debug_aranges table.
10901    Namely, define the beginning and ending address range of the
10902    text section generated for this compilation unit.  */
10903
10904 static void
10905 output_aranges (void)
10906 {
10907   unsigned i;
10908   unsigned long aranges_length = size_of_aranges ();
10909
10910   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10911     dw2_asm_output_data (4, 0xffffffff,
10912       "Initial length escape value indicating 64-bit DWARF extension");
10913   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10914                        "Length of Address Ranges Info");
10915   /* Version number for aranges is still 2, even in DWARF3.  */
10916   dw2_asm_output_data (2, 2, "DWARF Version");
10917   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10918                          debug_info_section,
10919                          "Offset of Compilation Unit Info");
10920   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10921   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10922
10923   /* We need to align to twice the pointer size here.  */
10924   if (DWARF_ARANGES_PAD_SIZE)
10925     {
10926       /* Pad using a 2 byte words so that padding is correct for any
10927          pointer size.  */
10928       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10929                            2 * DWARF2_ADDR_SIZE);
10930       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10931         dw2_asm_output_data (2, 0, NULL);
10932     }
10933
10934   /* It is necessary not to output these entries if the sections were
10935      not used; if the sections were not used, the length will be 0 and
10936      the address may end up as 0 if the section is discarded by ld
10937      --gc-sections, leaving an invalid (0, 0) entry that can be
10938      confused with the terminator.  */
10939   if (text_section_used)
10940     {
10941       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10942       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10943                             text_section_label, "Length");
10944     }
10945   if (cold_text_section_used)
10946     {
10947       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10948                            "Address");
10949       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10950                             cold_text_section_label, "Length");
10951     }
10952
10953   for (i = 0; i < arange_table_in_use; i++)
10954     {
10955       dw_die_ref die = arange_table[i];
10956
10957       /* We shouldn't see aranges for DIEs outside of the main CU.  */
10958       gcc_assert (die->die_mark);
10959
10960       if (die->die_tag == DW_TAG_subprogram)
10961         {
10962           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
10963                                "Address");
10964           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
10965                                 get_AT_low_pc (die), "Length");
10966         }
10967       else
10968         {
10969           /* A static variable; extract the symbol from DW_AT_location.
10970              Note that this code isn't currently hit, as we only emit
10971              aranges for functions (jason 9/23/99).  */
10972           dw_attr_ref a = get_AT (die, DW_AT_location);
10973           dw_loc_descr_ref loc;
10974
10975           gcc_assert (a && AT_class (a) == dw_val_class_loc);
10976
10977           loc = AT_loc (a);
10978           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
10979
10980           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
10981                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
10982           dw2_asm_output_data (DWARF2_ADDR_SIZE,
10983                                get_AT_unsigned (die, DW_AT_byte_size),
10984                                "Length");
10985         }
10986     }
10987
10988   /* Output the terminator words.  */
10989   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10990   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10991 }
10992
10993 /* Add a new entry to .debug_ranges.  Return the offset at which it
10994    was placed.  */
10995
10996 static unsigned int
10997 add_ranges_num (int num)
10998 {
10999   unsigned int in_use = ranges_table_in_use;
11000
11001   if (in_use == ranges_table_allocated)
11002     {
11003       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11004       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11005                                     ranges_table_allocated);
11006       memset (ranges_table + ranges_table_in_use, 0,
11007               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11008     }
11009
11010   ranges_table[in_use].num = num;
11011   ranges_table_in_use = in_use + 1;
11012
11013   return in_use * 2 * DWARF2_ADDR_SIZE;
11014 }
11015
11016 /* Add a new entry to .debug_ranges corresponding to a block, or a
11017    range terminator if BLOCK is NULL.  */
11018
11019 static unsigned int
11020 add_ranges (const_tree block)
11021 {
11022   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11023 }
11024
11025 /* Add a new entry to .debug_ranges corresponding to a pair of
11026    labels.  */
11027
11028 static unsigned int
11029 add_ranges_by_labels (const char *begin, const char *end)
11030 {
11031   unsigned int in_use = ranges_by_label_in_use;
11032
11033   if (in_use == ranges_by_label_allocated)
11034     {
11035       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11036       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11037                                        ranges_by_label,
11038                                        ranges_by_label_allocated);
11039       memset (ranges_by_label + ranges_by_label_in_use, 0,
11040               RANGES_TABLE_INCREMENT
11041               * sizeof (struct dw_ranges_by_label_struct));
11042     }
11043
11044   ranges_by_label[in_use].begin = begin;
11045   ranges_by_label[in_use].end = end;
11046   ranges_by_label_in_use = in_use + 1;
11047
11048   return add_ranges_num (-(int)in_use - 1);
11049 }
11050
11051 static void
11052 output_ranges (void)
11053 {
11054   unsigned i;
11055   static const char *const start_fmt = "Offset 0x%x";
11056   const char *fmt = start_fmt;
11057
11058   for (i = 0; i < ranges_table_in_use; i++)
11059     {
11060       int block_num = ranges_table[i].num;
11061
11062       if (block_num > 0)
11063         {
11064           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11065           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11066
11067           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11068           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11069
11070           /* If all code is in the text section, then the compilation
11071              unit base address defaults to DW_AT_low_pc, which is the
11072              base of the text section.  */
11073           if (!have_multiple_function_sections)
11074             {
11075               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11076                                     text_section_label,
11077                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11078               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11079                                     text_section_label, NULL);
11080             }
11081
11082           /* Otherwise, the compilation unit base address is zero,
11083              which allows us to use absolute addresses, and not worry
11084              about whether the target supports cross-section
11085              arithmetic.  */
11086           else
11087             {
11088               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11089                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11090               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11091             }
11092
11093           fmt = NULL;
11094         }
11095
11096       /* Negative block_num stands for an index into ranges_by_label.  */
11097       else if (block_num < 0)
11098         {
11099           int lab_idx = - block_num - 1;
11100
11101           if (!have_multiple_function_sections)
11102             {
11103               gcc_unreachable ();
11104 #if 0
11105               /* If we ever use add_ranges_by_labels () for a single
11106                  function section, all we have to do is to take out
11107                  the #if 0 above.  */
11108               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11109                                     ranges_by_label[lab_idx].begin,
11110                                     text_section_label,
11111                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11112               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11113                                     ranges_by_label[lab_idx].end,
11114                                     text_section_label, NULL);
11115 #endif
11116             }
11117           else
11118             {
11119               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11120                                    ranges_by_label[lab_idx].begin,
11121                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11122               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11123                                    ranges_by_label[lab_idx].end,
11124                                    NULL);
11125             }
11126         }
11127       else
11128         {
11129           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11130           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11131           fmt = start_fmt;
11132         }
11133     }
11134 }
11135
11136 /* Data structure containing information about input files.  */
11137 struct file_info
11138 {
11139   const char *path;     /* Complete file name.  */
11140   const char *fname;    /* File name part.  */
11141   int length;           /* Length of entire string.  */
11142   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11143   int dir_idx;          /* Index in directory table.  */
11144 };
11145
11146 /* Data structure containing information about directories with source
11147    files.  */
11148 struct dir_info
11149 {
11150   const char *path;     /* Path including directory name.  */
11151   int length;           /* Path length.  */
11152   int prefix;           /* Index of directory entry which is a prefix.  */
11153   int count;            /* Number of files in this directory.  */
11154   int dir_idx;          /* Index of directory used as base.  */
11155 };
11156
11157 /* Callback function for file_info comparison.  We sort by looking at
11158    the directories in the path.  */
11159
11160 static int
11161 file_info_cmp (const void *p1, const void *p2)
11162 {
11163   const struct file_info *const s1 = (const struct file_info *) p1;
11164   const struct file_info *const s2 = (const struct file_info *) p2;
11165   const unsigned char *cp1;
11166   const unsigned char *cp2;
11167
11168   /* Take care of file names without directories.  We need to make sure that
11169      we return consistent values to qsort since some will get confused if
11170      we return the same value when identical operands are passed in opposite
11171      orders.  So if neither has a directory, return 0 and otherwise return
11172      1 or -1 depending on which one has the directory.  */
11173   if ((s1->path == s1->fname || s2->path == s2->fname))
11174     return (s2->path == s2->fname) - (s1->path == s1->fname);
11175
11176   cp1 = (const unsigned char *) s1->path;
11177   cp2 = (const unsigned char *) s2->path;
11178
11179   while (1)
11180     {
11181       ++cp1;
11182       ++cp2;
11183       /* Reached the end of the first path?  If so, handle like above.  */
11184       if ((cp1 == (const unsigned char *) s1->fname)
11185           || (cp2 == (const unsigned char *) s2->fname))
11186         return ((cp2 == (const unsigned char *) s2->fname)
11187                 - (cp1 == (const unsigned char *) s1->fname));
11188
11189       /* Character of current path component the same?  */
11190       else if (*cp1 != *cp2)
11191         return *cp1 - *cp2;
11192     }
11193 }
11194
11195 struct file_name_acquire_data
11196 {
11197   struct file_info *files;
11198   int used_files;
11199   int max_files;
11200 };
11201
11202 /* Traversal function for the hash table.  */
11203
11204 static int
11205 file_name_acquire (void ** slot, void *data)
11206 {
11207   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11208   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11209   struct file_info *fi;
11210   const char *f;
11211
11212   gcc_assert (fnad->max_files >= d->emitted_number);
11213
11214   if (! d->emitted_number)
11215     return 1;
11216
11217   gcc_assert (fnad->max_files != fnad->used_files);
11218
11219   fi = fnad->files + fnad->used_files++;
11220
11221   /* Skip all leading "./".  */
11222   f = d->filename;
11223   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11224     f += 2;
11225
11226   /* Create a new array entry.  */
11227   fi->path = f;
11228   fi->length = strlen (f);
11229   fi->file_idx = d;
11230
11231   /* Search for the file name part.  */
11232   f = strrchr (f, DIR_SEPARATOR);
11233 #if defined (DIR_SEPARATOR_2)
11234   {
11235     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11236
11237     if (g != NULL)
11238       {
11239         if (f == NULL || f < g)
11240           f = g;
11241       }
11242   }
11243 #endif
11244
11245   fi->fname = f == NULL ? fi->path : f + 1;
11246   return 1;
11247 }
11248
11249 /* Output the directory table and the file name table.  We try to minimize
11250    the total amount of memory needed.  A heuristic is used to avoid large
11251    slowdowns with many input files.  */
11252
11253 static void
11254 output_file_names (void)
11255 {
11256   struct file_name_acquire_data fnad;
11257   int numfiles;
11258   struct file_info *files;
11259   struct dir_info *dirs;
11260   int *saved;
11261   int *savehere;
11262   int *backmap;
11263   int ndirs;
11264   int idx_offset;
11265   int i;
11266   int idx;
11267
11268   if (!last_emitted_file)
11269     {
11270       dw2_asm_output_data (1, 0, "End directory table");
11271       dw2_asm_output_data (1, 0, "End file name table");
11272       return;
11273     }
11274
11275   numfiles = last_emitted_file->emitted_number;
11276
11277   /* Allocate the various arrays we need.  */
11278   files = XALLOCAVEC (struct file_info, numfiles);
11279   dirs = XALLOCAVEC (struct dir_info, numfiles);
11280
11281   fnad.files = files;
11282   fnad.used_files = 0;
11283   fnad.max_files = numfiles;
11284   htab_traverse (file_table, file_name_acquire, &fnad);
11285   gcc_assert (fnad.used_files == fnad.max_files);
11286
11287   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11288
11289   /* Find all the different directories used.  */
11290   dirs[0].path = files[0].path;
11291   dirs[0].length = files[0].fname - files[0].path;
11292   dirs[0].prefix = -1;
11293   dirs[0].count = 1;
11294   dirs[0].dir_idx = 0;
11295   files[0].dir_idx = 0;
11296   ndirs = 1;
11297
11298   for (i = 1; i < numfiles; i++)
11299     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11300         && memcmp (dirs[ndirs - 1].path, files[i].path,
11301                    dirs[ndirs - 1].length) == 0)
11302       {
11303         /* Same directory as last entry.  */
11304         files[i].dir_idx = ndirs - 1;
11305         ++dirs[ndirs - 1].count;
11306       }
11307     else
11308       {
11309         int j;
11310
11311         /* This is a new directory.  */
11312         dirs[ndirs].path = files[i].path;
11313         dirs[ndirs].length = files[i].fname - files[i].path;
11314         dirs[ndirs].count = 1;
11315         dirs[ndirs].dir_idx = ndirs;
11316         files[i].dir_idx = ndirs;
11317
11318         /* Search for a prefix.  */
11319         dirs[ndirs].prefix = -1;
11320         for (j = 0; j < ndirs; j++)
11321           if (dirs[j].length < dirs[ndirs].length
11322               && dirs[j].length > 1
11323               && (dirs[ndirs].prefix == -1
11324                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11325               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11326             dirs[ndirs].prefix = j;
11327
11328         ++ndirs;
11329       }
11330
11331   /* Now to the actual work.  We have to find a subset of the directories which
11332      allow expressing the file name using references to the directory table
11333      with the least amount of characters.  We do not do an exhaustive search
11334      where we would have to check out every combination of every single
11335      possible prefix.  Instead we use a heuristic which provides nearly optimal
11336      results in most cases and never is much off.  */
11337   saved = XALLOCAVEC (int, ndirs);
11338   savehere = XALLOCAVEC (int, ndirs);
11339
11340   memset (saved, '\0', ndirs * sizeof (saved[0]));
11341   for (i = 0; i < ndirs; i++)
11342     {
11343       int j;
11344       int total;
11345
11346       /* We can always save some space for the current directory.  But this
11347          does not mean it will be enough to justify adding the directory.  */
11348       savehere[i] = dirs[i].length;
11349       total = (savehere[i] - saved[i]) * dirs[i].count;
11350
11351       for (j = i + 1; j < ndirs; j++)
11352         {
11353           savehere[j] = 0;
11354           if (saved[j] < dirs[i].length)
11355             {
11356               /* Determine whether the dirs[i] path is a prefix of the
11357                  dirs[j] path.  */
11358               int k;
11359
11360               k = dirs[j].prefix;
11361               while (k != -1 && k != (int) i)
11362                 k = dirs[k].prefix;
11363
11364               if (k == (int) i)
11365                 {
11366                   /* Yes it is.  We can possibly save some memory by
11367                      writing the filenames in dirs[j] relative to
11368                      dirs[i].  */
11369                   savehere[j] = dirs[i].length;
11370                   total += (savehere[j] - saved[j]) * dirs[j].count;
11371                 }
11372             }
11373         }
11374
11375       /* Check whether we can save enough to justify adding the dirs[i]
11376          directory.  */
11377       if (total > dirs[i].length + 1)
11378         {
11379           /* It's worthwhile adding.  */
11380           for (j = i; j < ndirs; j++)
11381             if (savehere[j] > 0)
11382               {
11383                 /* Remember how much we saved for this directory so far.  */
11384                 saved[j] = savehere[j];
11385
11386                 /* Remember the prefix directory.  */
11387                 dirs[j].dir_idx = i;
11388               }
11389         }
11390     }
11391
11392   /* Emit the directory name table.  */
11393   idx = 1;
11394   idx_offset = dirs[0].length > 0 ? 1 : 0;
11395   for (i = 1 - idx_offset; i < ndirs; i++)
11396     dw2_asm_output_nstring (dirs[i].path,
11397                             dirs[i].length
11398                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11399                             "Directory Entry: 0x%x", i + idx_offset);
11400
11401   dw2_asm_output_data (1, 0, "End directory table");
11402
11403   /* We have to emit them in the order of emitted_number since that's
11404      used in the debug info generation.  To do this efficiently we
11405      generate a back-mapping of the indices first.  */
11406   backmap = XALLOCAVEC (int, numfiles);
11407   for (i = 0; i < numfiles; i++)
11408     backmap[files[i].file_idx->emitted_number - 1] = i;
11409
11410   /* Now write all the file names.  */
11411   for (i = 0; i < numfiles; i++)
11412     {
11413       int file_idx = backmap[i];
11414       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11415
11416 #ifdef VMS_DEBUGGING_INFO
11417 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11418
11419       /* Setting these fields can lead to debugger miscomparisons,
11420          but VMS Debug requires them to be set correctly.  */
11421
11422       int ver;
11423       long long cdt;
11424       long siz;
11425       int maxfilelen = strlen (files[file_idx].path)
11426                                + dirs[dir_idx].length
11427                                + MAX_VMS_VERSION_LEN + 1;
11428       char *filebuf = XALLOCAVEC (char, maxfilelen);
11429
11430       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11431       snprintf (filebuf, maxfilelen, "%s;%d",
11432                 files[file_idx].path + dirs[dir_idx].length, ver);
11433
11434       dw2_asm_output_nstring
11435         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
11436
11437       /* Include directory index.  */
11438       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11439
11440       /* Modification time.  */
11441       dw2_asm_output_data_uleb128
11442         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11443           ? cdt : 0,
11444          NULL);
11445
11446       /* File length in bytes.  */
11447       dw2_asm_output_data_uleb128
11448         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11449           ? siz : 0,
11450          NULL);
11451 #else
11452       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11453                               "File Entry: 0x%x", (unsigned) i + 1);
11454
11455       /* Include directory index.  */
11456       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11457
11458       /* Modification time.  */
11459       dw2_asm_output_data_uleb128 (0, NULL);
11460
11461       /* File length in bytes.  */
11462       dw2_asm_output_data_uleb128 (0, NULL);
11463 #endif
11464     }
11465
11466   dw2_asm_output_data (1, 0, "End file name table");
11467 }
11468
11469
11470 /* Output the source line number correspondence information.  This
11471    information goes into the .debug_line section.  */
11472
11473 static void
11474 output_line_info (void)
11475 {
11476   char l1[20], l2[20], p1[20], p2[20];
11477   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11478   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11479   unsigned opc;
11480   unsigned n_op_args;
11481   unsigned long lt_index;
11482   unsigned long current_line;
11483   long line_offset;
11484   long line_delta;
11485   unsigned long current_file;
11486   unsigned long function;
11487   int ver = dwarf_version;
11488
11489   /* Don't mark the output as DWARF-4 until we make full use of the
11490      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
11491      selects only a few extensions from the DWARF-4 spec.  */
11492   if (ver > 3)
11493     ver = 3;
11494
11495   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11496   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11497   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11498   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11499
11500   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11501     dw2_asm_output_data (4, 0xffffffff,
11502       "Initial length escape value indicating 64-bit DWARF extension");
11503   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11504                         "Length of Source Line Info");
11505   ASM_OUTPUT_LABEL (asm_out_file, l1);
11506
11507   dw2_asm_output_data (2, ver, "DWARF Version");
11508   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11509   ASM_OUTPUT_LABEL (asm_out_file, p1);
11510
11511   /* Define the architecture-dependent minimum instruction length (in
11512    bytes).  In this implementation of DWARF, this field is used for
11513    information purposes only.  Since GCC generates assembly language,
11514    we have no a priori knowledge of how many instruction bytes are
11515    generated for each source line, and therefore can use only the
11516    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11517    commands.  Accordingly, we fix this as `1', which is "correct
11518    enough" for all architectures, and don't let the target override.  */
11519   dw2_asm_output_data (1, 1,
11520                        "Minimum Instruction Length");
11521
11522   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11523                        "Default is_stmt_start flag");
11524   dw2_asm_output_data (1, DWARF_LINE_BASE,
11525                        "Line Base Value (Special Opcodes)");
11526   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11527                        "Line Range Value (Special Opcodes)");
11528   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11529                        "Special Opcode Base");
11530
11531   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11532     {
11533       switch (opc)
11534         {
11535         case DW_LNS_advance_pc:
11536         case DW_LNS_advance_line:
11537         case DW_LNS_set_file:
11538         case DW_LNS_set_column:
11539         case DW_LNS_fixed_advance_pc:
11540           n_op_args = 1;
11541           break;
11542         default:
11543           n_op_args = 0;
11544           break;
11545         }
11546
11547       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
11548                            opc, n_op_args);
11549     }
11550
11551   /* Write out the information about the files we use.  */
11552   output_file_names ();
11553   ASM_OUTPUT_LABEL (asm_out_file, p2);
11554
11555   /* We used to set the address register to the first location in the text
11556      section here, but that didn't accomplish anything since we already
11557      have a line note for the opening brace of the first function.  */
11558
11559   /* Generate the line number to PC correspondence table, encoded as
11560      a series of state machine operations.  */
11561   current_file = 1;
11562   current_line = 1;
11563
11564   if (cfun && in_cold_section_p)
11565     strcpy (prev_line_label, crtl->subsections.cold_section_label);
11566   else
11567     strcpy (prev_line_label, text_section_label);
11568   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
11569     {
11570       dw_line_info_ref line_info = &line_info_table[lt_index];
11571
11572 #if 0
11573       /* Disable this optimization for now; GDB wants to see two line notes
11574          at the beginning of a function so it can find the end of the
11575          prologue.  */
11576
11577       /* Don't emit anything for redundant notes.  Just updating the
11578          address doesn't accomplish anything, because we already assume
11579          that anything after the last address is this line.  */
11580       if (line_info->dw_line_num == current_line
11581           && line_info->dw_file_num == current_file)
11582         continue;
11583 #endif
11584
11585       /* Emit debug info for the address of the current line.
11586
11587          Unfortunately, we have little choice here currently, and must always
11588          use the most general form.  GCC does not know the address delta
11589          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
11590          attributes which will give an upper bound on the address range.  We
11591          could perhaps use length attributes to determine when it is safe to
11592          use DW_LNS_fixed_advance_pc.  */
11593
11594       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
11595       if (0)
11596         {
11597           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
11598           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11599                                "DW_LNS_fixed_advance_pc");
11600           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11601         }
11602       else
11603         {
11604           /* This can handle any delta.  This takes
11605              4+DWARF2_ADDR_SIZE bytes.  */
11606           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11607           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11608           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11609           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11610         }
11611
11612       strcpy (prev_line_label, line_label);
11613
11614       /* Emit debug info for the source file of the current line, if
11615          different from the previous line.  */
11616       if (line_info->dw_file_num != current_file)
11617         {
11618           current_file = line_info->dw_file_num;
11619           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11620           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11621         }
11622
11623       /* Emit debug info for the current line number, choosing the encoding
11624          that uses the least amount of space.  */
11625       if (line_info->dw_line_num != current_line)
11626         {
11627           line_offset = line_info->dw_line_num - current_line;
11628           line_delta = line_offset - DWARF_LINE_BASE;
11629           current_line = line_info->dw_line_num;
11630           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11631             /* This can handle deltas from -10 to 234, using the current
11632                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
11633                takes 1 byte.  */
11634             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11635                                  "line %lu", current_line);
11636           else
11637             {
11638               /* This can handle any delta.  This takes at least 4 bytes,
11639                  depending on the value being encoded.  */
11640               dw2_asm_output_data (1, DW_LNS_advance_line,
11641                                    "advance to line %lu", current_line);
11642               dw2_asm_output_data_sleb128 (line_offset, NULL);
11643               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11644             }
11645         }
11646       else
11647         /* We still need to start a new row, so output a copy insn.  */
11648         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11649     }
11650
11651   /* Emit debug info for the address of the end of the function.  */
11652   if (0)
11653     {
11654       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11655                            "DW_LNS_fixed_advance_pc");
11656       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
11657     }
11658   else
11659     {
11660       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11661       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11662       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11663       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
11664     }
11665
11666   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11667   dw2_asm_output_data_uleb128 (1, NULL);
11668   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11669
11670   function = 0;
11671   current_file = 1;
11672   current_line = 1;
11673   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
11674     {
11675       dw_separate_line_info_ref line_info
11676         = &separate_line_info_table[lt_index];
11677
11678 #if 0
11679       /* Don't emit anything for redundant notes.  */
11680       if (line_info->dw_line_num == current_line
11681           && line_info->dw_file_num == current_file
11682           && line_info->function == function)
11683         goto cont;
11684 #endif
11685
11686       /* Emit debug info for the address of the current line.  If this is
11687          a new function, or the first line of a function, then we need
11688          to handle it differently.  */
11689       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
11690                                    lt_index);
11691       if (function != line_info->function)
11692         {
11693           function = line_info->function;
11694
11695           /* Set the address register to the first line in the function.  */
11696           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11697           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11698           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11699           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11700         }
11701       else
11702         {
11703           /* ??? See the DW_LNS_advance_pc comment above.  */
11704           if (0)
11705             {
11706               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11707                                    "DW_LNS_fixed_advance_pc");
11708               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11709             }
11710           else
11711             {
11712               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11713               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11714               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11715               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11716             }
11717         }
11718
11719       strcpy (prev_line_label, line_label);
11720
11721       /* Emit debug info for the source file of the current line, if
11722          different from the previous line.  */
11723       if (line_info->dw_file_num != current_file)
11724         {
11725           current_file = line_info->dw_file_num;
11726           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11727           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11728         }
11729
11730       /* Emit debug info for the current line number, choosing the encoding
11731          that uses the least amount of space.  */
11732       if (line_info->dw_line_num != current_line)
11733         {
11734           line_offset = line_info->dw_line_num - current_line;
11735           line_delta = line_offset - DWARF_LINE_BASE;
11736           current_line = line_info->dw_line_num;
11737           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11738             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11739                                  "line %lu", current_line);
11740           else
11741             {
11742               dw2_asm_output_data (1, DW_LNS_advance_line,
11743                                    "advance to line %lu", current_line);
11744               dw2_asm_output_data_sleb128 (line_offset, NULL);
11745               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11746             }
11747         }
11748       else
11749         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11750
11751 #if 0
11752     cont:
11753 #endif
11754
11755       lt_index++;
11756
11757       /* If we're done with a function, end its sequence.  */
11758       if (lt_index == separate_line_info_table_in_use
11759           || separate_line_info_table[lt_index].function != function)
11760         {
11761           current_file = 1;
11762           current_line = 1;
11763
11764           /* Emit debug info for the address of the end of the function.  */
11765           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
11766           if (0)
11767             {
11768               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11769                                    "DW_LNS_fixed_advance_pc");
11770               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11771             }
11772           else
11773             {
11774               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11775               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11776               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11777               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11778             }
11779
11780           /* Output the marker for the end of this sequence.  */
11781           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11782           dw2_asm_output_data_uleb128 (1, NULL);
11783           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11784         }
11785     }
11786
11787   /* Output the marker for the end of the line number info.  */
11788   ASM_OUTPUT_LABEL (asm_out_file, l2);
11789 }
11790
11791 /* Return the size of the .debug_dcall table for the compilation unit.  */
11792
11793 static unsigned long
11794 size_of_dcall_table (void)
11795 {
11796   unsigned long size;
11797   unsigned int i;
11798   dcall_entry *p;
11799   tree last_poc_decl = NULL;
11800
11801   /* Header:  version + debug info section pointer + pointer size.  */
11802   size = 2 + DWARF_OFFSET_SIZE + 1;
11803
11804   /* Each entry:  code label + DIE offset.  */
11805   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11806     {
11807       gcc_assert (p->targ_die != NULL);
11808       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11809       if (p->poc_decl != last_poc_decl)
11810         {
11811           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11812           gcc_assert (poc_die);
11813           last_poc_decl = p->poc_decl;
11814           if (poc_die)
11815             size += (DWARF_OFFSET_SIZE
11816                      + size_of_uleb128 (poc_die->die_offset));
11817         }
11818       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
11819     }
11820
11821   return size;
11822 }
11823
11824 /* Output the direct call table used to disambiguate PC values when
11825    identical function have been merged.  */
11826
11827 static void
11828 output_dcall_table (void)
11829 {
11830   unsigned i;
11831   unsigned long dcall_length = size_of_dcall_table ();
11832   dcall_entry *p;
11833   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11834   tree last_poc_decl = NULL;
11835
11836   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11837     dw2_asm_output_data (4, 0xffffffff,
11838       "Initial length escape value indicating 64-bit DWARF extension");
11839   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
11840                        "Length of Direct Call Table");
11841   dw2_asm_output_data (2, 4, "Version number");
11842   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11843                          debug_info_section,
11844                          "Offset of Compilation Unit Info");
11845   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11846
11847   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11848     {
11849       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11850       if (p->poc_decl != last_poc_decl)
11851         {
11852           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11853           last_poc_decl = p->poc_decl;
11854           if (poc_die)
11855             {
11856               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
11857               dw2_asm_output_data_uleb128 (poc_die->die_offset,
11858                                            "Caller DIE offset");
11859             }
11860         }
11861       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11862       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11863       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
11864                                    "Callee DIE offset");
11865     }
11866 }
11867 \f
11868 /* Return the size of the .debug_vcall table for the compilation unit.  */
11869
11870 static unsigned long
11871 size_of_vcall_table (void)
11872 {
11873   unsigned long size;
11874   unsigned int i;
11875   vcall_entry *p;
11876
11877   /* Header:  version + pointer size.  */
11878   size = 2 + 1;
11879
11880   /* Each entry:  code label + vtable slot index.  */
11881   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11882     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
11883
11884   return size;
11885 }
11886
11887 /* Output the virtual call table used to disambiguate PC values when
11888    identical function have been merged.  */
11889
11890 static void
11891 output_vcall_table (void)
11892 {
11893   unsigned i;
11894   unsigned long vcall_length = size_of_vcall_table ();
11895   vcall_entry *p;
11896   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11897
11898   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11899     dw2_asm_output_data (4, 0xffffffff,
11900       "Initial length escape value indicating 64-bit DWARF extension");
11901   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
11902                        "Length of Virtual Call Table");
11903   dw2_asm_output_data (2, 4, "Version number");
11904   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11905
11906   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11907     {
11908       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11909       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11910       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
11911     }
11912 }
11913 \f
11914 /* Given a pointer to a tree node for some base type, return a pointer to
11915    a DIE that describes the given type.
11916
11917    This routine must only be called for GCC type nodes that correspond to
11918    Dwarf base (fundamental) types.  */
11919
11920 static dw_die_ref
11921 base_type_die (tree type)
11922 {
11923   dw_die_ref base_type_result;
11924   enum dwarf_type encoding;
11925
11926   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11927     return 0;
11928
11929   /* If this is a subtype that should not be emitted as a subrange type,
11930      use the base type.  See subrange_type_for_debug_p.  */
11931   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11932     type = TREE_TYPE (type);
11933
11934   switch (TREE_CODE (type))
11935     {
11936     case INTEGER_TYPE:
11937       if (TYPE_STRING_FLAG (type))
11938         {
11939           if (TYPE_UNSIGNED (type))
11940             encoding = DW_ATE_unsigned_char;
11941           else
11942             encoding = DW_ATE_signed_char;
11943         }
11944       else if (TYPE_UNSIGNED (type))
11945         encoding = DW_ATE_unsigned;
11946       else
11947         encoding = DW_ATE_signed;
11948       break;
11949
11950     case REAL_TYPE:
11951       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
11952         {
11953           if (dwarf_version >= 3 || !dwarf_strict)
11954             encoding = DW_ATE_decimal_float;
11955           else
11956             encoding = DW_ATE_lo_user;
11957         }
11958       else
11959         encoding = DW_ATE_float;
11960       break;
11961
11962     case FIXED_POINT_TYPE:
11963       if (!(dwarf_version >= 3 || !dwarf_strict))
11964         encoding = DW_ATE_lo_user;
11965       else if (TYPE_UNSIGNED (type))
11966         encoding = DW_ATE_unsigned_fixed;
11967       else
11968         encoding = DW_ATE_signed_fixed;
11969       break;
11970
11971       /* Dwarf2 doesn't know anything about complex ints, so use
11972          a user defined type for it.  */
11973     case COMPLEX_TYPE:
11974       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
11975         encoding = DW_ATE_complex_float;
11976       else
11977         encoding = DW_ATE_lo_user;
11978       break;
11979
11980     case BOOLEAN_TYPE:
11981       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
11982       encoding = DW_ATE_boolean;
11983       break;
11984
11985     default:
11986       /* No other TREE_CODEs are Dwarf fundamental types.  */
11987       gcc_unreachable ();
11988     }
11989
11990   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
11991
11992   /* This probably indicates a bug.  */
11993   if (! TYPE_NAME (type))
11994     add_name_attribute (base_type_result, "__unknown__");
11995
11996   add_AT_unsigned (base_type_result, DW_AT_byte_size,
11997                    int_size_in_bytes (type));
11998   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
11999
12000   return base_type_result;
12001 }
12002
12003 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12004    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12005
12006 static inline int
12007 is_base_type (tree type)
12008 {
12009   switch (TREE_CODE (type))
12010     {
12011     case ERROR_MARK:
12012     case VOID_TYPE:
12013     case INTEGER_TYPE:
12014     case REAL_TYPE:
12015     case FIXED_POINT_TYPE:
12016     case COMPLEX_TYPE:
12017     case BOOLEAN_TYPE:
12018       return 1;
12019
12020     case ARRAY_TYPE:
12021     case RECORD_TYPE:
12022     case UNION_TYPE:
12023     case QUAL_UNION_TYPE:
12024     case ENUMERAL_TYPE:
12025     case FUNCTION_TYPE:
12026     case METHOD_TYPE:
12027     case POINTER_TYPE:
12028     case REFERENCE_TYPE:
12029     case OFFSET_TYPE:
12030     case LANG_TYPE:
12031     case VECTOR_TYPE:
12032       return 0;
12033
12034     default:
12035       gcc_unreachable ();
12036     }
12037
12038   return 0;
12039 }
12040
12041 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12042    node, return the size in bits for the type if it is a constant, or else
12043    return the alignment for the type if the type's size is not constant, or
12044    else return BITS_PER_WORD if the type actually turns out to be an
12045    ERROR_MARK node.  */
12046
12047 static inline unsigned HOST_WIDE_INT
12048 simple_type_size_in_bits (const_tree type)
12049 {
12050   if (TREE_CODE (type) == ERROR_MARK)
12051     return BITS_PER_WORD;
12052   else if (TYPE_SIZE (type) == NULL_TREE)
12053     return 0;
12054   else if (host_integerp (TYPE_SIZE (type), 1))
12055     return tree_low_cst (TYPE_SIZE (type), 1);
12056   else
12057     return TYPE_ALIGN (type);
12058 }
12059
12060 /*  Given a pointer to a tree node for a subrange type, return a pointer
12061     to a DIE that describes the given type.  */
12062
12063 static dw_die_ref
12064 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12065 {
12066   dw_die_ref subrange_die;
12067   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12068
12069   if (context_die == NULL)
12070     context_die = comp_unit_die;
12071
12072   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12073
12074   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12075     {
12076       /* The size of the subrange type and its base type do not match,
12077          so we need to generate a size attribute for the subrange type.  */
12078       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12079     }
12080
12081   if (low)
12082     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12083   if (high)
12084     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12085
12086   return subrange_die;
12087 }
12088
12089 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12090    entry that chains various modifiers in front of the given type.  */
12091
12092 static dw_die_ref
12093 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12094                    dw_die_ref context_die)
12095 {
12096   enum tree_code code = TREE_CODE (type);
12097   dw_die_ref mod_type_die;
12098   dw_die_ref sub_die = NULL;
12099   tree item_type = NULL;
12100   tree qualified_type;
12101   tree name, low, high;
12102
12103   if (code == ERROR_MARK)
12104     return NULL;
12105
12106   /* See if we already have the appropriately qualified variant of
12107      this type.  */
12108   qualified_type
12109     = get_qualified_type (type,
12110                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12111                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12112
12113   /* If we do, then we can just use its DIE, if it exists.  */
12114   if (qualified_type)
12115     {
12116       mod_type_die = lookup_type_die (qualified_type);
12117       if (mod_type_die)
12118         return mod_type_die;
12119     }
12120
12121   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12122
12123   /* Handle C typedef types.  */
12124   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
12125     {
12126       tree dtype = TREE_TYPE (name);
12127
12128       if (qualified_type == dtype)
12129         {
12130           /* For a named type, use the typedef.  */
12131           gen_type_die (qualified_type, context_die);
12132           return lookup_type_die (qualified_type);
12133         }
12134       else if (is_const_type < TYPE_READONLY (dtype)
12135                || is_volatile_type < TYPE_VOLATILE (dtype)
12136                || (is_const_type <= TYPE_READONLY (dtype)
12137                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12138                    && DECL_ORIGINAL_TYPE (name) != type))
12139         /* cv-unqualified version of named type.  Just use the unnamed
12140            type to which it refers.  */
12141         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12142                                   is_const_type, is_volatile_type,
12143                                   context_die);
12144       /* Else cv-qualified version of named type; fall through.  */
12145     }
12146
12147   if (is_const_type)
12148     {
12149       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12150       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12151     }
12152   else if (is_volatile_type)
12153     {
12154       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12155       sub_die = modified_type_die (type, 0, 0, context_die);
12156     }
12157   else if (code == POINTER_TYPE)
12158     {
12159       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12160       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12161                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12162       item_type = TREE_TYPE (type);
12163       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12164         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12165                          TYPE_ADDR_SPACE (item_type));
12166     }
12167   else if (code == REFERENCE_TYPE)
12168     {
12169       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12170       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12171                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12172       item_type = TREE_TYPE (type);
12173       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12174         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12175                          TYPE_ADDR_SPACE (item_type));
12176     }
12177   else if (code == INTEGER_TYPE
12178            && TREE_TYPE (type) != NULL_TREE
12179            && subrange_type_for_debug_p (type, &low, &high))
12180     {
12181       mod_type_die = subrange_type_die (type, low, high, context_die);
12182       item_type = TREE_TYPE (type);
12183     }
12184   else if (is_base_type (type))
12185     mod_type_die = base_type_die (type);
12186   else
12187     {
12188       gen_type_die (type, context_die);
12189
12190       /* We have to get the type_main_variant here (and pass that to the
12191          `lookup_type_die' routine) because the ..._TYPE node we have
12192          might simply be a *copy* of some original type node (where the
12193          copy was created to help us keep track of typedef names) and
12194          that copy might have a different TYPE_UID from the original
12195          ..._TYPE node.  */
12196       if (TREE_CODE (type) != VECTOR_TYPE)
12197         return lookup_type_die (type_main_variant (type));
12198       else
12199         /* Vectors have the debugging information in the type,
12200            not the main variant.  */
12201         return lookup_type_die (type);
12202     }
12203
12204   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12205      don't output a DW_TAG_typedef, since there isn't one in the
12206      user's program; just attach a DW_AT_name to the type.
12207      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12208      if the base type already has the same name.  */
12209   if (name
12210       && ((TREE_CODE (name) != TYPE_DECL
12211            && (qualified_type == TYPE_MAIN_VARIANT (type)
12212                || (!is_const_type && !is_volatile_type)))
12213           || (TREE_CODE (name) == TYPE_DECL
12214               && TREE_TYPE (name) == qualified_type
12215               && DECL_NAME (name))))
12216     {
12217       if (TREE_CODE (name) == TYPE_DECL)
12218         /* Could just call add_name_and_src_coords_attributes here,
12219            but since this is a builtin type it doesn't have any
12220            useful source coordinates anyway.  */
12221         name = DECL_NAME (name);
12222       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12223     }
12224
12225   if (qualified_type)
12226     equate_type_number_to_die (qualified_type, mod_type_die);
12227
12228   if (item_type)
12229     /* We must do this after the equate_type_number_to_die call, in case
12230        this is a recursive type.  This ensures that the modified_type_die
12231        recursion will terminate even if the type is recursive.  Recursive
12232        types are possible in Ada.  */
12233     sub_die = modified_type_die (item_type,
12234                                  TYPE_READONLY (item_type),
12235                                  TYPE_VOLATILE (item_type),
12236                                  context_die);
12237
12238   if (sub_die != NULL)
12239     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12240
12241   return mod_type_die;
12242 }
12243
12244 /* Generate DIEs for the generic parameters of T.
12245    T must be either a generic type or a generic function.
12246    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12247
12248 static void
12249 gen_generic_params_dies (tree t)
12250 {
12251   tree parms, args;
12252   int parms_num, i;
12253   dw_die_ref die = NULL;
12254
12255   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12256     return;
12257
12258   if (TYPE_P (t))
12259     die = lookup_type_die (t);
12260   else if (DECL_P (t))
12261     die = lookup_decl_die (t);
12262
12263   gcc_assert (die);
12264
12265   parms = lang_hooks.get_innermost_generic_parms (t);
12266   if (!parms)
12267     /* T has no generic parameter. It means T is neither a generic type
12268        or function. End of story.  */
12269     return;
12270
12271   parms_num = TREE_VEC_LENGTH (parms);
12272   args = lang_hooks.get_innermost_generic_args (t);
12273   for (i = 0; i < parms_num; i++)
12274     {
12275       tree parm, arg, arg_pack_elems;
12276
12277       parm = TREE_VEC_ELT (parms, i);
12278       arg = TREE_VEC_ELT (args, i);
12279       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12280       gcc_assert (parm && TREE_VALUE (parm) && arg);
12281
12282       if (parm && TREE_VALUE (parm) && arg)
12283         {
12284           /* If PARM represents a template parameter pack,
12285              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12286              by DW_TAG_template_*_parameter DIEs for the argument
12287              pack elements of ARG. Note that ARG would then be
12288              an argument pack.  */
12289           if (arg_pack_elems)
12290             template_parameter_pack_die (TREE_VALUE (parm),
12291                                          arg_pack_elems,
12292                                          die);
12293           else
12294             generic_parameter_die (TREE_VALUE (parm), arg,
12295                                    true /* Emit DW_AT_name */, die);
12296         }
12297     }
12298 }
12299
12300 /* Create and return a DIE for PARM which should be
12301    the representation of a generic type parameter.
12302    For instance, in the C++ front end, PARM would be a template parameter.
12303    ARG is the argument to PARM.
12304    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12305    name of the PARM.
12306    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12307    as a child node.  */
12308
12309 static dw_die_ref
12310 generic_parameter_die (tree parm, tree arg,
12311                        bool emit_name_p,
12312                        dw_die_ref parent_die)
12313 {
12314   dw_die_ref tmpl_die = NULL;
12315   const char *name = NULL;
12316
12317   if (!parm || !DECL_NAME (parm) || !arg)
12318     return NULL;
12319
12320   /* We support non-type generic parameters and arguments,
12321      type generic parameters and arguments, as well as
12322      generic generic parameters (a.k.a. template template parameters in C++)
12323      and arguments.  */
12324   if (TREE_CODE (parm) == PARM_DECL)
12325     /* PARM is a nontype generic parameter  */
12326     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12327   else if (TREE_CODE (parm) == TYPE_DECL)
12328     /* PARM is a type generic parameter.  */
12329     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12330   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12331     /* PARM is a generic generic parameter.
12332        Its DIE is a GNU extension. It shall have a
12333        DW_AT_name attribute to represent the name of the template template
12334        parameter, and a DW_AT_GNU_template_name attribute to represent the
12335        name of the template template argument.  */
12336     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12337                         parent_die, parm);
12338   else
12339     gcc_unreachable ();
12340
12341   if (tmpl_die)
12342     {
12343       tree tmpl_type;
12344
12345       /* If PARM is a generic parameter pack, it means we are
12346          emitting debug info for a template argument pack element.
12347          In other terms, ARG is a template argument pack element.
12348          In that case, we don't emit any DW_AT_name attribute for
12349          the die.  */
12350       if (emit_name_p)
12351         {
12352           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12353           gcc_assert (name);
12354           add_AT_string (tmpl_die, DW_AT_name, name);
12355         }
12356
12357       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12358         {
12359           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12360              TMPL_DIE should have a child DW_AT_type attribute that is set
12361              to the type of the argument to PARM, which is ARG.
12362              If PARM is a type generic parameter, TMPL_DIE should have a
12363              child DW_AT_type that is set to ARG.  */
12364           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12365           add_type_attribute (tmpl_die, tmpl_type, 0,
12366                               TREE_THIS_VOLATILE (tmpl_type),
12367                               parent_die);
12368         }
12369       else
12370         {
12371           /* So TMPL_DIE is a DIE representing a
12372              a generic generic template parameter, a.k.a template template
12373              parameter in C++ and arg is a template.  */
12374
12375           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12376              to the name of the argument.  */
12377           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12378           if (name)
12379             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12380         }
12381
12382       if (TREE_CODE (parm) == PARM_DECL)
12383         /* So PARM is a non-type generic parameter.
12384            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12385            attribute of TMPL_DIE which value represents the value
12386            of ARG.
12387            We must be careful here:
12388            The value of ARG might reference some function decls.
12389            We might currently be emitting debug info for a generic
12390            type and types are emitted before function decls, we don't
12391            know if the function decls referenced by ARG will actually be
12392            emitted after cgraph computations.
12393            So must defer the generation of the DW_AT_const_value to
12394            after cgraph is ready.  */
12395         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12396     }
12397
12398   return tmpl_die;
12399 }
12400
12401 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12402    PARM_PACK must be a template parameter pack. The returned DIE
12403    will be child DIE of PARENT_DIE.  */
12404
12405 static dw_die_ref
12406 template_parameter_pack_die (tree parm_pack,
12407                              tree parm_pack_args,
12408                              dw_die_ref parent_die)
12409 {
12410   dw_die_ref die;
12411   int j;
12412
12413   gcc_assert (parent_die && parm_pack);
12414
12415   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12416   add_name_and_src_coords_attributes (die, parm_pack);
12417   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12418     generic_parameter_die (parm_pack,
12419                            TREE_VEC_ELT (parm_pack_args, j),
12420                            false /* Don't emit DW_AT_name */,
12421                            die);
12422   return die;
12423 }
12424
12425 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12426    an enumerated type.  */
12427
12428 static inline int
12429 type_is_enum (const_tree type)
12430 {
12431   return TREE_CODE (type) == ENUMERAL_TYPE;
12432 }
12433
12434 /* Return the DBX register number described by a given RTL node.  */
12435
12436 static unsigned int
12437 dbx_reg_number (const_rtx rtl)
12438 {
12439   unsigned regno = REGNO (rtl);
12440
12441   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12442
12443 #ifdef LEAF_REG_REMAP
12444   if (current_function_uses_only_leaf_regs)
12445     {
12446       int leaf_reg = LEAF_REG_REMAP (regno);
12447       if (leaf_reg != -1)
12448         regno = (unsigned) leaf_reg;
12449     }
12450 #endif
12451
12452   return DBX_REGISTER_NUMBER (regno);
12453 }
12454
12455 /* Optionally add a DW_OP_piece term to a location description expression.
12456    DW_OP_piece is only added if the location description expression already
12457    doesn't end with DW_OP_piece.  */
12458
12459 static void
12460 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12461 {
12462   dw_loc_descr_ref loc;
12463
12464   if (*list_head != NULL)
12465     {
12466       /* Find the end of the chain.  */
12467       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12468         ;
12469
12470       if (loc->dw_loc_opc != DW_OP_piece)
12471         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12472     }
12473 }
12474
12475 /* Return a location descriptor that designates a machine register or
12476    zero if there is none.  */
12477
12478 static dw_loc_descr_ref
12479 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12480 {
12481   rtx regs;
12482
12483   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12484     return 0;
12485
12486   regs = targetm.dwarf_register_span (rtl);
12487
12488   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
12489     return multiple_reg_loc_descriptor (rtl, regs, initialized);
12490   else
12491     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
12492 }
12493
12494 /* Return a location descriptor that designates a machine register for
12495    a given hard register number.  */
12496
12497 static dw_loc_descr_ref
12498 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12499 {
12500   dw_loc_descr_ref reg_loc_descr;
12501
12502   if (regno <= 31)
12503     reg_loc_descr
12504       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12505   else
12506     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12507
12508   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12509     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12510
12511   return reg_loc_descr;
12512 }
12513
12514 /* Given an RTL of a register, return a location descriptor that
12515    designates a value that spans more than one register.  */
12516
12517 static dw_loc_descr_ref
12518 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12519                              enum var_init_status initialized)
12520 {
12521   int nregs, size, i;
12522   unsigned reg;
12523   dw_loc_descr_ref loc_result = NULL;
12524
12525   reg = REGNO (rtl);
12526 #ifdef LEAF_REG_REMAP
12527   if (current_function_uses_only_leaf_regs)
12528     {
12529       int leaf_reg = LEAF_REG_REMAP (reg);
12530       if (leaf_reg != -1)
12531         reg = (unsigned) leaf_reg;
12532     }
12533 #endif
12534   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12535   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
12536
12537   /* Simple, contiguous registers.  */
12538   if (regs == NULL_RTX)
12539     {
12540       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12541
12542       loc_result = NULL;
12543       while (nregs--)
12544         {
12545           dw_loc_descr_ref t;
12546
12547           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12548                                       VAR_INIT_STATUS_INITIALIZED);
12549           add_loc_descr (&loc_result, t);
12550           add_loc_descr_op_piece (&loc_result, size);
12551           ++reg;
12552         }
12553       return loc_result;
12554     }
12555
12556   /* Now onto stupid register sets in non contiguous locations.  */
12557
12558   gcc_assert (GET_CODE (regs) == PARALLEL);
12559
12560   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12561   loc_result = NULL;
12562
12563   for (i = 0; i < XVECLEN (regs, 0); ++i)
12564     {
12565       dw_loc_descr_ref t;
12566
12567       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
12568                                   VAR_INIT_STATUS_INITIALIZED);
12569       add_loc_descr (&loc_result, t);
12570       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12571       add_loc_descr_op_piece (&loc_result, size);
12572     }
12573
12574   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12575     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12576   return loc_result;
12577 }
12578
12579 #endif /* DWARF2_DEBUGGING_INFO */
12580
12581 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
12582
12583 /* Return a location descriptor that designates a constant.  */
12584
12585 static dw_loc_descr_ref
12586 int_loc_descriptor (HOST_WIDE_INT i)
12587 {
12588   enum dwarf_location_atom op;
12589
12590   /* Pick the smallest representation of a constant, rather than just
12591      defaulting to the LEB encoding.  */
12592   if (i >= 0)
12593     {
12594       if (i <= 31)
12595         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
12596       else if (i <= 0xff)
12597         op = DW_OP_const1u;
12598       else if (i <= 0xffff)
12599         op = DW_OP_const2u;
12600       else if (HOST_BITS_PER_WIDE_INT == 32
12601                || i <= 0xffffffff)
12602         op = DW_OP_const4u;
12603       else
12604         op = DW_OP_constu;
12605     }
12606   else
12607     {
12608       if (i >= -0x80)
12609         op = DW_OP_const1s;
12610       else if (i >= -0x8000)
12611         op = DW_OP_const2s;
12612       else if (HOST_BITS_PER_WIDE_INT == 32
12613                || i >= -0x80000000)
12614         op = DW_OP_const4s;
12615       else
12616         op = DW_OP_consts;
12617     }
12618
12619   return new_loc_descr (op, i, 0);
12620 }
12621 #endif
12622
12623 #ifdef DWARF2_DEBUGGING_INFO
12624 /* Return loc description representing "address" of integer value.
12625    This can appear only as toplevel expression.  */
12626
12627 static dw_loc_descr_ref
12628 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12629 {
12630   int litsize;
12631   dw_loc_descr_ref loc_result = NULL;
12632
12633   if (!(dwarf_version >= 4 || !dwarf_strict))
12634     return NULL;
12635
12636   if (i >= 0)
12637     {
12638       if (i <= 31)
12639         litsize = 1;
12640       else if (i <= 0xff)
12641         litsize = 2;
12642       else if (i <= 0xffff)
12643         litsize = 3;
12644       else if (HOST_BITS_PER_WIDE_INT == 32
12645                || i <= 0xffffffff)
12646         litsize = 5;
12647       else
12648         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12649     }
12650   else
12651     {
12652       if (i >= -0x80)
12653         litsize = 2;
12654       else if (i >= -0x8000)
12655         litsize = 3;
12656       else if (HOST_BITS_PER_WIDE_INT == 32
12657                || i >= -0x80000000)
12658         litsize = 5;
12659       else
12660         litsize = 1 + size_of_sleb128 (i);
12661     }
12662   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12663      is more compact.  For DW_OP_stack_value we need:
12664      litsize + 1 (DW_OP_stack_value)
12665      and for DW_OP_implicit_value:
12666      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12667   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12668     {
12669       loc_result = int_loc_descriptor (i);
12670       add_loc_descr (&loc_result,
12671                      new_loc_descr (DW_OP_stack_value, 0, 0));
12672       return loc_result;
12673     }
12674
12675   loc_result = new_loc_descr (DW_OP_implicit_value,
12676                               size, 0);
12677   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12678   loc_result->dw_loc_oprnd2.v.val_int = i;
12679   return loc_result;
12680 }
12681
12682 /* Return a location descriptor that designates a base+offset location.  */
12683
12684 static dw_loc_descr_ref
12685 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12686                  enum var_init_status initialized)
12687 {
12688   unsigned int regno;
12689   dw_loc_descr_ref result;
12690   dw_fde_ref fde = current_fde ();
12691
12692   /* We only use "frame base" when we're sure we're talking about the
12693      post-prologue local stack frame.  We do this by *not* running
12694      register elimination until this point, and recognizing the special
12695      argument pointer and soft frame pointer rtx's.  */
12696   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12697     {
12698       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12699
12700       if (elim != reg)
12701         {
12702           if (GET_CODE (elim) == PLUS)
12703             {
12704               offset += INTVAL (XEXP (elim, 1));
12705               elim = XEXP (elim, 0);
12706             }
12707           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12708                        && (elim == hard_frame_pointer_rtx
12709                            || elim == stack_pointer_rtx))
12710                       || elim == (frame_pointer_needed
12711                                   ? hard_frame_pointer_rtx
12712                                   : stack_pointer_rtx));
12713
12714           /* If drap register is used to align stack, use frame
12715              pointer + offset to access stack variables.  If stack
12716              is aligned without drap, use stack pointer + offset to
12717              access stack variables.  */
12718           if (crtl->stack_realign_tried
12719               && reg == frame_pointer_rtx)
12720             {
12721               int base_reg
12722                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12723                                       ? HARD_FRAME_POINTER_REGNUM
12724                                       : STACK_POINTER_REGNUM);
12725               return new_reg_loc_descr (base_reg, offset);
12726             }
12727
12728           offset += frame_pointer_fb_offset;
12729           return new_loc_descr (DW_OP_fbreg, offset, 0);
12730         }
12731     }
12732   else if (fde
12733            && fde->drap_reg != INVALID_REGNUM
12734            && (fde->drap_reg == REGNO (reg)
12735                || fde->vdrap_reg == REGNO (reg)))
12736     {
12737       /* Use cfa+offset to represent the location of arguments passed
12738          on stack when drap is used to align stack.  */
12739       return new_loc_descr (DW_OP_fbreg, offset, 0);
12740     }
12741
12742   regno = dbx_reg_number (reg);
12743   if (regno <= 31)
12744     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12745                             offset, 0);
12746   else
12747     result = new_loc_descr (DW_OP_bregx, regno, offset);
12748
12749   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12750     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12751
12752   return result;
12753 }
12754
12755 /* Return true if this RTL expression describes a base+offset calculation.  */
12756
12757 static inline int
12758 is_based_loc (const_rtx rtl)
12759 {
12760   return (GET_CODE (rtl) == PLUS
12761           && ((REG_P (XEXP (rtl, 0))
12762                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12763                && CONST_INT_P (XEXP (rtl, 1)))));
12764 }
12765
12766 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12767    failed.  */
12768
12769 static dw_loc_descr_ref
12770 tls_mem_loc_descriptor (rtx mem)
12771 {
12772   tree base;
12773   dw_loc_descr_ref loc_result;
12774
12775   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
12776     return NULL;
12777
12778   base = get_base_address (MEM_EXPR (mem));
12779   if (base == NULL
12780       || TREE_CODE (base) != VAR_DECL
12781       || !DECL_THREAD_LOCAL_P (base))
12782     return NULL;
12783
12784   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
12785   if (loc_result == NULL)
12786     return NULL;
12787
12788   if (INTVAL (MEM_OFFSET (mem)))
12789     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
12790
12791   return loc_result;
12792 }
12793
12794 /* Output debug info about reason why we failed to expand expression as dwarf
12795    expression.  */
12796
12797 static void
12798 expansion_failed (tree expr, rtx rtl, char const *reason)
12799 {
12800   if (dump_file && (dump_flags & TDF_DETAILS))
12801     {
12802       fprintf (dump_file, "Failed to expand as dwarf: ");
12803       if (expr)
12804         print_generic_expr (dump_file, expr, dump_flags);
12805       if (rtl)
12806         {
12807           fprintf (dump_file, "\n");
12808           print_rtl (dump_file, rtl);
12809         }
12810       fprintf (dump_file, "\nReason: %s\n", reason);
12811     }
12812 }
12813
12814 /* Helper function for const_ok_for_output, called either directly
12815    or via for_each_rtx.  */
12816
12817 static int
12818 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
12819 {
12820   rtx rtl = *rtlp;
12821
12822   if (GET_CODE (rtl) != SYMBOL_REF)
12823     return 0;
12824
12825   if (CONSTANT_POOL_ADDRESS_P (rtl))
12826     {
12827       bool marked;
12828       get_pool_constant_mark (rtl, &marked);
12829       /* If all references to this pool constant were optimized away,
12830          it was not output and thus we can't represent it.  */
12831       if (!marked)
12832         {
12833           expansion_failed (NULL_TREE, rtl,
12834                             "Constant was removed from constant pool.\n");
12835           return 1;
12836         }
12837     }
12838
12839   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12840     return 1;
12841
12842   /* Avoid references to external symbols in debug info, on several targets
12843      the linker might even refuse to link when linking a shared library,
12844      and in many other cases the relocations for .debug_info/.debug_loc are
12845      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12846      to be defined within the same shared library or executable are fine.  */
12847   if (SYMBOL_REF_EXTERNAL_P (rtl))
12848     {
12849       tree decl = SYMBOL_REF_DECL (rtl);
12850
12851       if (decl == NULL || !targetm.binds_local_p (decl))
12852         {
12853           expansion_failed (NULL_TREE, rtl,
12854                             "Symbol not defined in current TU.\n");
12855           return 1;
12856         }
12857     }
12858
12859   return 0;
12860 }
12861
12862 /* Return true if constant RTL can be emitted in DW_OP_addr or
12863    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12864    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12865
12866 static bool
12867 const_ok_for_output (rtx rtl)
12868 {
12869   if (GET_CODE (rtl) == SYMBOL_REF)
12870     return const_ok_for_output_1 (&rtl, NULL) == 0;
12871
12872   if (GET_CODE (rtl) == CONST)
12873     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
12874
12875   return true;
12876 }
12877
12878 /* The following routine converts the RTL for a variable or parameter
12879    (resident in memory) into an equivalent Dwarf representation of a
12880    mechanism for getting the address of that same variable onto the top of a
12881    hypothetical "address evaluation" stack.
12882
12883    When creating memory location descriptors, we are effectively transforming
12884    the RTL for a memory-resident object into its Dwarf postfix expression
12885    equivalent.  This routine recursively descends an RTL tree, turning
12886    it into Dwarf postfix code as it goes.
12887
12888    MODE is the mode of the memory reference, needed to handle some
12889    autoincrement addressing modes.
12890
12891    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
12892    location list for RTL.
12893
12894    Return 0 if we can't represent the location.  */
12895
12896 static dw_loc_descr_ref
12897 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12898                     enum var_init_status initialized)
12899 {
12900   dw_loc_descr_ref mem_loc_result = NULL;
12901   enum dwarf_location_atom op;
12902   dw_loc_descr_ref op0, op1;
12903
12904   /* Note that for a dynamically sized array, the location we will generate a
12905      description of here will be the lowest numbered location which is
12906      actually within the array.  That's *not* necessarily the same as the
12907      zeroth element of the array.  */
12908
12909   rtl = targetm.delegitimize_address (rtl);
12910
12911   switch (GET_CODE (rtl))
12912     {
12913     case POST_INC:
12914     case POST_DEC:
12915     case POST_MODIFY:
12916       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
12917
12918     case SUBREG:
12919       /* The case of a subreg may arise when we have a local (register)
12920          variable or a formal (register) parameter which doesn't quite fill
12921          up an entire register.  For now, just assume that it is
12922          legitimate to make the Dwarf info refer to the whole register which
12923          contains the given subreg.  */
12924       if (!subreg_lowpart_p (rtl))
12925         break;
12926       rtl = SUBREG_REG (rtl);
12927       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
12928         break;
12929       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
12930         break;
12931       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
12932       break;
12933
12934     case REG:
12935       /* Whenever a register number forms a part of the description of the
12936          method for calculating the (dynamic) address of a memory resident
12937          object, DWARF rules require the register number be referred to as
12938          a "base register".  This distinction is not based in any way upon
12939          what category of register the hardware believes the given register
12940          belongs to.  This is strictly DWARF terminology we're dealing with
12941          here. Note that in cases where the location of a memory-resident
12942          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12943          OP_CONST (0)) the actual DWARF location descriptor that we generate
12944          may just be OP_BASEREG (basereg).  This may look deceptively like
12945          the object in question was allocated to a register (rather than in
12946          memory) so DWARF consumers need to be aware of the subtle
12947          distinction between OP_REG and OP_BASEREG.  */
12948       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12949         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12950       else if (stack_realign_drap
12951                && crtl->drap_reg
12952                && crtl->args.internal_arg_pointer == rtl
12953                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12954         {
12955           /* If RTL is internal_arg_pointer, which has been optimized
12956              out, use DRAP instead.  */
12957           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12958                                             VAR_INIT_STATUS_INITIALIZED);
12959         }
12960       break;
12961
12962     case SIGN_EXTEND:
12963     case ZERO_EXTEND:
12964       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12965                                 VAR_INIT_STATUS_INITIALIZED);
12966       if (op0 == 0)
12967         break;
12968       else
12969         {
12970           int shift = DWARF2_ADDR_SIZE
12971                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12972           shift *= BITS_PER_UNIT;
12973           if (GET_CODE (rtl) == SIGN_EXTEND)
12974             op = DW_OP_shra;
12975           else
12976             op = DW_OP_shr;
12977           mem_loc_result = op0;
12978           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12979           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12980           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12981           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12982         }
12983       break;
12984
12985     case MEM:
12986       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
12987                                            VAR_INIT_STATUS_INITIALIZED);
12988       if (mem_loc_result == NULL)
12989         mem_loc_result = tls_mem_loc_descriptor (rtl);
12990       if (mem_loc_result != 0)
12991         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12992       break;
12993
12994     case LO_SUM:
12995          rtl = XEXP (rtl, 1);
12996
12997       /* ... fall through ...  */
12998
12999     case LABEL_REF:
13000       /* Some ports can transform a symbol ref into a label ref, because
13001          the symbol ref is too far away and has to be dumped into a constant
13002          pool.  */
13003     case CONST:
13004     case SYMBOL_REF:
13005       /* Alternatively, the symbol in the constant pool might be referenced
13006          by a different symbol.  */
13007       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
13008         {
13009           bool marked;
13010           rtx tmp = get_pool_constant_mark (rtl, &marked);
13011
13012           if (GET_CODE (tmp) == SYMBOL_REF)
13013             {
13014               rtl = tmp;
13015               if (CONSTANT_POOL_ADDRESS_P (tmp))
13016                 get_pool_constant_mark (tmp, &marked);
13017               else
13018                 marked = true;
13019             }
13020
13021           /* If all references to this pool constant were optimized away,
13022              it was not output and thus we can't represent it.
13023              FIXME: might try to use DW_OP_const_value here, though
13024              DW_OP_piece complicates it.  */
13025           if (!marked)
13026             {
13027               expansion_failed (NULL_TREE, rtl,
13028                                 "Constant was removed from constant pool.\n");
13029               return 0;
13030             }
13031         }
13032
13033       if (GET_CODE (rtl) == SYMBOL_REF
13034           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13035         {
13036           dw_loc_descr_ref temp;
13037
13038           /* If this is not defined, we have no way to emit the data.  */
13039           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13040             break;
13041
13042           temp = new_loc_descr (DW_OP_addr, 0, 0);
13043           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13044           temp->dw_loc_oprnd1.v.val_addr = rtl;
13045           temp->dtprel = true;
13046
13047           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13048           add_loc_descr (&mem_loc_result, temp);
13049
13050           break;
13051         }
13052
13053       if (!const_ok_for_output (rtl))
13054         break;
13055
13056     symref:
13057       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13058       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13059       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13060       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13061       break;
13062
13063     case CONCAT:
13064     case CONCATN:
13065     case VAR_LOCATION:
13066       expansion_failed (NULL_TREE, rtl,
13067                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13068       return 0;
13069
13070     case PRE_MODIFY:
13071       /* Extract the PLUS expression nested inside and fall into
13072          PLUS code below.  */
13073       rtl = XEXP (rtl, 1);
13074       goto plus;
13075
13076     case PRE_INC:
13077     case PRE_DEC:
13078       /* Turn these into a PLUS expression and fall into the PLUS code
13079          below.  */
13080       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13081                           GEN_INT (GET_CODE (rtl) == PRE_INC
13082                                    ? GET_MODE_UNIT_SIZE (mode)
13083                                    : -GET_MODE_UNIT_SIZE (mode)));
13084
13085       /* ... fall through ...  */
13086
13087     case PLUS:
13088     plus:
13089       if (is_based_loc (rtl))
13090         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13091                                           INTVAL (XEXP (rtl, 1)),
13092                                           VAR_INIT_STATUS_INITIALIZED);
13093       else
13094         {
13095           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13096                                                VAR_INIT_STATUS_INITIALIZED);
13097           if (mem_loc_result == 0)
13098             break;
13099
13100           if (CONST_INT_P (XEXP (rtl, 1)))
13101             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13102           else
13103             {
13104               dw_loc_descr_ref mem_loc_result2
13105                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13106                                       VAR_INIT_STATUS_INITIALIZED);
13107               if (mem_loc_result2 == 0)
13108                 break;
13109               add_loc_descr (&mem_loc_result, mem_loc_result2);
13110               add_loc_descr (&mem_loc_result,
13111                              new_loc_descr (DW_OP_plus, 0, 0));
13112             }
13113         }
13114       break;
13115
13116     /* If a pseudo-reg is optimized away, it is possible for it to
13117        be replaced with a MEM containing a multiply or shift.  */
13118     case MINUS:
13119       op = DW_OP_minus;
13120       goto do_binop;
13121
13122     case MULT:
13123       op = DW_OP_mul;
13124       goto do_binop;
13125
13126     case DIV:
13127       op = DW_OP_div;
13128       goto do_binop;
13129
13130     case MOD:
13131       op = DW_OP_mod;
13132       goto do_binop;
13133
13134     case ASHIFT:
13135       op = DW_OP_shl;
13136       goto do_binop;
13137
13138     case ASHIFTRT:
13139       op = DW_OP_shra;
13140       goto do_binop;
13141
13142     case LSHIFTRT:
13143       op = DW_OP_shr;
13144       goto do_binop;
13145
13146     case AND:
13147       op = DW_OP_and;
13148       goto do_binop;
13149
13150     case IOR:
13151       op = DW_OP_or;
13152       goto do_binop;
13153
13154     case XOR:
13155       op = DW_OP_xor;
13156       goto do_binop;
13157
13158     do_binop:
13159       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13160                                 VAR_INIT_STATUS_INITIALIZED);
13161       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13162                                 VAR_INIT_STATUS_INITIALIZED);
13163
13164       if (op0 == 0 || op1 == 0)
13165         break;
13166
13167       mem_loc_result = op0;
13168       add_loc_descr (&mem_loc_result, op1);
13169       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13170       break;
13171
13172     case NOT:
13173       op = DW_OP_not;
13174       goto do_unop;
13175
13176     case ABS:
13177       op = DW_OP_abs;
13178       goto do_unop;
13179
13180     case NEG:
13181       op = DW_OP_neg;
13182       goto do_unop;
13183
13184     do_unop:
13185       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13186                                 VAR_INIT_STATUS_INITIALIZED);
13187
13188       if (op0 == 0)
13189         break;
13190
13191       mem_loc_result = op0;
13192       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13193       break;
13194
13195     case CONST_INT:
13196       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13197       break;
13198
13199     case EQ:
13200       op = DW_OP_eq;
13201       goto do_scompare;
13202
13203     case GE:
13204       op = DW_OP_ge;
13205       goto do_scompare;
13206
13207     case GT:
13208       op = DW_OP_gt;
13209       goto do_scompare;
13210
13211     case LE:
13212       op = DW_OP_le;
13213       goto do_scompare;
13214
13215     case LT:
13216       op = DW_OP_lt;
13217       goto do_scompare;
13218
13219     case NE:
13220       op = DW_OP_ne;
13221       goto do_scompare;
13222
13223     do_scompare:
13224       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13225           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13226           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13227         break;
13228
13229       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13230                                 VAR_INIT_STATUS_INITIALIZED);
13231       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13232                                 VAR_INIT_STATUS_INITIALIZED);
13233
13234       if (op0 == 0 || op1 == 0)
13235         break;
13236
13237       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13238         {
13239           int shift = DWARF2_ADDR_SIZE
13240                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13241           shift *= BITS_PER_UNIT;
13242           add_loc_descr (&op0, int_loc_descriptor (shift));
13243           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13244           if (CONST_INT_P (XEXP (rtl, 1)))
13245             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13246           else
13247             {
13248               add_loc_descr (&op1, int_loc_descriptor (shift));
13249               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13250             }
13251         }
13252
13253     do_compare:
13254       mem_loc_result = op0;
13255       add_loc_descr (&mem_loc_result, op1);
13256       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13257       if (STORE_FLAG_VALUE != 1)
13258         {
13259           add_loc_descr (&mem_loc_result,
13260                          int_loc_descriptor (STORE_FLAG_VALUE));
13261           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13262         }
13263       break;
13264
13265     case GEU:
13266       op = DW_OP_ge;
13267       goto do_ucompare;
13268
13269     case GTU:
13270       op = DW_OP_gt;
13271       goto do_ucompare;
13272
13273     case LEU:
13274       op = DW_OP_le;
13275       goto do_ucompare;
13276
13277     case LTU:
13278       op = DW_OP_lt;
13279       goto do_ucompare;
13280
13281     do_ucompare:
13282       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13283           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13284           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13285         break;
13286
13287       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13288                                 VAR_INIT_STATUS_INITIALIZED);
13289       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13290                                 VAR_INIT_STATUS_INITIALIZED);
13291
13292       if (op0 == 0 || op1 == 0)
13293         break;
13294
13295       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13296         {
13297           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13298           add_loc_descr (&op0, int_loc_descriptor (mask));
13299           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13300           if (CONST_INT_P (XEXP (rtl, 1)))
13301             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13302           else
13303             {
13304               add_loc_descr (&op1, int_loc_descriptor (mask));
13305               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13306             }
13307         }
13308       else
13309         {
13310           HOST_WIDE_INT bias = 1;
13311           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13312           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13313           if (CONST_INT_P (XEXP (rtl, 1)))
13314             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13315                                       + INTVAL (XEXP (rtl, 1)));
13316           else
13317             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13318         }
13319       goto do_compare;
13320
13321     case SMIN:
13322     case SMAX:
13323     case UMIN:
13324     case UMAX:
13325       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13326           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13327           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13328         break;
13329
13330       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13331                                 VAR_INIT_STATUS_INITIALIZED);
13332       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13333                                 VAR_INIT_STATUS_INITIALIZED);
13334
13335       if (op0 == 0 || op1 == 0)
13336         break;
13337
13338       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13339       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13340       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13341       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13342         {
13343           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13344             {
13345               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13346               add_loc_descr (&op0, int_loc_descriptor (mask));
13347               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13348               add_loc_descr (&op1, int_loc_descriptor (mask));
13349               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13350             }
13351           else
13352             {
13353               HOST_WIDE_INT bias = 1;
13354               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13355               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13356               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13357             }
13358         }
13359       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13360         {
13361           int shift = DWARF2_ADDR_SIZE
13362                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13363           shift *= BITS_PER_UNIT;
13364           add_loc_descr (&op0, int_loc_descriptor (shift));
13365           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13366           add_loc_descr (&op1, int_loc_descriptor (shift));
13367           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13368         }
13369
13370       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13371         op = DW_OP_lt;
13372       else
13373         op = DW_OP_gt;
13374       mem_loc_result = op0;
13375       add_loc_descr (&mem_loc_result, op1);
13376       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13377       {
13378         dw_loc_descr_ref bra_node, drop_node;
13379
13380         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13381         add_loc_descr (&mem_loc_result, bra_node);
13382         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13383         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13384         add_loc_descr (&mem_loc_result, drop_node);
13385         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13386         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13387       }
13388       break;
13389
13390     case ZERO_EXTRACT:
13391     case SIGN_EXTRACT:
13392       if (CONST_INT_P (XEXP (rtl, 1))
13393           && CONST_INT_P (XEXP (rtl, 2))
13394           && ((unsigned) INTVAL (XEXP (rtl, 1))
13395               + (unsigned) INTVAL (XEXP (rtl, 2))
13396               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
13397           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13398           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13399         {
13400           int shift, size;
13401           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13402                                     VAR_INIT_STATUS_INITIALIZED);
13403           if (op0 == 0)
13404             break;
13405           if (GET_CODE (rtl) == SIGN_EXTRACT)
13406             op = DW_OP_shra;
13407           else
13408             op = DW_OP_shr;
13409           mem_loc_result = op0;
13410           size = INTVAL (XEXP (rtl, 1));
13411           shift = INTVAL (XEXP (rtl, 2));
13412           if (BITS_BIG_ENDIAN)
13413             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13414                     - shift - size;
13415           if (shift + size != (int) DWARF2_ADDR_SIZE)
13416             {
13417               add_loc_descr (&mem_loc_result,
13418                              int_loc_descriptor (DWARF2_ADDR_SIZE
13419                                                  - shift - size));
13420               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13421             }
13422           if (size != (int) DWARF2_ADDR_SIZE)
13423             {
13424               add_loc_descr (&mem_loc_result,
13425                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13426               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13427             }
13428         }
13429       break;
13430
13431     case COMPARE:
13432     case IF_THEN_ELSE:
13433     case ROTATE:
13434     case ROTATERT:
13435     case TRUNCATE:
13436       /* In theory, we could implement the above.  */
13437       /* DWARF cannot represent the unsigned compare operations
13438          natively.  */
13439     case SS_MULT:
13440     case US_MULT:
13441     case SS_DIV:
13442     case US_DIV:
13443     case UDIV:
13444     case UMOD:
13445     case UNORDERED:
13446     case ORDERED:
13447     case UNEQ:
13448     case UNGE:
13449     case UNGT:
13450     case UNLE:
13451     case UNLT:
13452     case LTGT:
13453     case FLOAT_EXTEND:
13454     case FLOAT_TRUNCATE:
13455     case FLOAT:
13456     case UNSIGNED_FLOAT:
13457     case FIX:
13458     case UNSIGNED_FIX:
13459     case FRACT_CONVERT:
13460     case UNSIGNED_FRACT_CONVERT:
13461     case SAT_FRACT:
13462     case UNSIGNED_SAT_FRACT:
13463     case SQRT:
13464     case BSWAP:
13465     case FFS:
13466     case CLZ:
13467     case CTZ:
13468     case POPCOUNT:
13469     case PARITY:
13470     case ASM_OPERANDS:
13471     case UNSPEC:
13472     case HIGH:
13473       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13474          can't express it in the debug info.  This can happen e.g. with some
13475          TLS UNSPECs.  */
13476       break;
13477
13478     case CONST_STRING:
13479       resolve_one_addr (&rtl, NULL);
13480       goto symref;
13481
13482     default:
13483 #ifdef ENABLE_CHECKING
13484       print_rtl (stderr, rtl);
13485       gcc_unreachable ();
13486 #else
13487       break;
13488 #endif
13489     }
13490
13491   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13492     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13493
13494   return mem_loc_result;
13495 }
13496
13497 /* Return a descriptor that describes the concatenation of two locations.
13498    This is typically a complex variable.  */
13499
13500 static dw_loc_descr_ref
13501 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13502 {
13503   dw_loc_descr_ref cc_loc_result = NULL;
13504   dw_loc_descr_ref x0_ref
13505     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13506   dw_loc_descr_ref x1_ref
13507     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13508
13509   if (x0_ref == 0 || x1_ref == 0)
13510     return 0;
13511
13512   cc_loc_result = x0_ref;
13513   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13514
13515   add_loc_descr (&cc_loc_result, x1_ref);
13516   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13517
13518   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13519     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13520
13521   return cc_loc_result;
13522 }
13523
13524 /* Return a descriptor that describes the concatenation of N
13525    locations.  */
13526
13527 static dw_loc_descr_ref
13528 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13529 {
13530   unsigned int i;
13531   dw_loc_descr_ref cc_loc_result = NULL;
13532   unsigned int n = XVECLEN (concatn, 0);
13533
13534   for (i = 0; i < n; ++i)
13535     {
13536       dw_loc_descr_ref ref;
13537       rtx x = XVECEXP (concatn, 0, i);
13538
13539       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13540       if (ref == NULL)
13541         return NULL;
13542
13543       add_loc_descr (&cc_loc_result, ref);
13544       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13545     }
13546
13547   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13548     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13549
13550   return cc_loc_result;
13551 }
13552
13553 /* Output a proper Dwarf location descriptor for a variable or parameter
13554    which is either allocated in a register or in a memory location.  For a
13555    register, we just generate an OP_REG and the register number.  For a
13556    memory location we provide a Dwarf postfix expression describing how to
13557    generate the (dynamic) address of the object onto the address stack.
13558
13559    MODE is mode of the decl if this loc_descriptor is going to be used in
13560    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13561    allowed, VOIDmode otherwise.
13562
13563    If we don't know how to describe it, return 0.  */
13564
13565 static dw_loc_descr_ref
13566 loc_descriptor (rtx rtl, enum machine_mode mode,
13567                 enum var_init_status initialized)
13568 {
13569   dw_loc_descr_ref loc_result = NULL;
13570
13571   switch (GET_CODE (rtl))
13572     {
13573     case SUBREG:
13574       /* The case of a subreg may arise when we have a local (register)
13575          variable or a formal (register) parameter which doesn't quite fill
13576          up an entire register.  For now, just assume that it is
13577          legitimate to make the Dwarf info refer to the whole register which
13578          contains the given subreg.  */
13579       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
13580       break;
13581
13582     case REG:
13583       loc_result = reg_loc_descriptor (rtl, initialized);
13584       break;
13585
13586     case SIGN_EXTEND:
13587     case ZERO_EXTEND:
13588       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13589       break;
13590
13591     case MEM:
13592       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13593                                        initialized);
13594       if (loc_result == NULL)
13595         loc_result = tls_mem_loc_descriptor (rtl);
13596       break;
13597
13598     case CONCAT:
13599       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13600                                           initialized);
13601       break;
13602
13603     case CONCATN:
13604       loc_result = concatn_loc_descriptor (rtl, initialized);
13605       break;
13606
13607     case VAR_LOCATION:
13608       /* Single part.  */
13609       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13610         {
13611           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
13612                                        initialized);
13613           break;
13614         }
13615
13616       rtl = XEXP (rtl, 1);
13617       /* FALLTHRU */
13618
13619     case PARALLEL:
13620       {
13621         rtvec par_elems = XVEC (rtl, 0);
13622         int num_elem = GET_NUM_ELEM (par_elems);
13623         enum machine_mode mode;
13624         int i;
13625
13626         /* Create the first one, so we have something to add to.  */
13627         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13628                                      VOIDmode, initialized);
13629         if (loc_result == NULL)
13630           return NULL;
13631         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13632         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13633         for (i = 1; i < num_elem; i++)
13634           {
13635             dw_loc_descr_ref temp;
13636
13637             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13638                                    VOIDmode, initialized);
13639             if (temp == NULL)
13640               return NULL;
13641             add_loc_descr (&loc_result, temp);
13642             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13643             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13644           }
13645       }
13646       break;
13647
13648     case CONST_INT:
13649       if (mode != VOIDmode && mode != BLKmode)
13650         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13651                                                     INTVAL (rtl));
13652       break;
13653
13654     case CONST_DOUBLE:
13655       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13656         {
13657           /* Note that a CONST_DOUBLE rtx could represent either an integer
13658              or a floating-point constant.  A CONST_DOUBLE is used whenever
13659              the constant requires more than one word in order to be
13660              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13661           if (GET_MODE (rtl) != VOIDmode)
13662             mode = GET_MODE (rtl);
13663
13664           loc_result = new_loc_descr (DW_OP_implicit_value,
13665                                       GET_MODE_SIZE (mode), 0);
13666           if (SCALAR_FLOAT_MODE_P (mode))
13667             {
13668               unsigned int length = GET_MODE_SIZE (mode);
13669               unsigned char *array = GGC_NEWVEC (unsigned char, length);
13670
13671               insert_float (rtl, array);
13672               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13673               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13674               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13675               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13676             }
13677           else
13678             {
13679               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13680               loc_result->dw_loc_oprnd2.v.val_double.high
13681                 = CONST_DOUBLE_HIGH (rtl);
13682               loc_result->dw_loc_oprnd2.v.val_double.low
13683                 = CONST_DOUBLE_LOW (rtl);
13684             }
13685         }
13686       break;
13687
13688     case CONST_VECTOR:
13689       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13690         {
13691           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13692           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13693           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13694           unsigned int i;
13695           unsigned char *p;
13696
13697           mode = GET_MODE (rtl);
13698           switch (GET_MODE_CLASS (mode))
13699             {
13700             case MODE_VECTOR_INT:
13701               for (i = 0, p = array; i < length; i++, p += elt_size)
13702                 {
13703                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13704                   HOST_WIDE_INT lo, hi;
13705
13706                   switch (GET_CODE (elt))
13707                     {
13708                     case CONST_INT:
13709                       lo = INTVAL (elt);
13710                       hi = -(lo < 0);
13711                       break;
13712
13713                     case CONST_DOUBLE:
13714                       lo = CONST_DOUBLE_LOW (elt);
13715                       hi = CONST_DOUBLE_HIGH (elt);
13716                       break;
13717
13718                     default:
13719                       gcc_unreachable ();
13720                     }
13721
13722                   if (elt_size <= sizeof (HOST_WIDE_INT))
13723                     insert_int (lo, elt_size, p);
13724                   else
13725                     {
13726                       unsigned char *p0 = p;
13727                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13728
13729                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13730                       if (WORDS_BIG_ENDIAN)
13731                         {
13732                           p0 = p1;
13733                           p1 = p;
13734                         }
13735                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13736                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13737                     }
13738                 }
13739               break;
13740
13741             case MODE_VECTOR_FLOAT:
13742               for (i = 0, p = array; i < length; i++, p += elt_size)
13743                 {
13744                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13745                   insert_float (elt, p);
13746                 }
13747               break;
13748
13749             default:
13750               gcc_unreachable ();
13751             }
13752
13753           loc_result = new_loc_descr (DW_OP_implicit_value,
13754                                       length * elt_size, 0);
13755           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13756           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13757           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13758           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13759         }
13760       break;
13761
13762     case CONST:
13763       if (mode == VOIDmode
13764           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13765           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13766           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13767         {
13768           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13769           break;
13770         }
13771       /* FALLTHROUGH */
13772     case SYMBOL_REF:
13773       if (!const_ok_for_output (rtl))
13774         break;
13775     case LABEL_REF:
13776       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13777           && (dwarf_version >= 4 || !dwarf_strict))
13778         {
13779           loc_result = new_loc_descr (DW_OP_implicit_value,
13780                                       DWARF2_ADDR_SIZE, 0);
13781           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
13782           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
13783           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13784         }
13785       break;
13786
13787     default:
13788       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13789           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13790           && (dwarf_version >= 4 || !dwarf_strict))
13791         {
13792           /* Value expression.  */
13793           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13794           if (loc_result)
13795             add_loc_descr (&loc_result,
13796                            new_loc_descr (DW_OP_stack_value, 0, 0));
13797         }
13798       break;
13799     }
13800
13801   return loc_result;
13802 }
13803
13804 /* We need to figure out what section we should use as the base for the
13805    address ranges where a given location is valid.
13806    1. If this particular DECL has a section associated with it, use that.
13807    2. If this function has a section associated with it, use that.
13808    3. Otherwise, use the text section.
13809    XXX: If you split a variable across multiple sections, we won't notice.  */
13810
13811 static const char *
13812 secname_for_decl (const_tree decl)
13813 {
13814   const char *secname;
13815
13816   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13817     {
13818       tree sectree = DECL_SECTION_NAME (decl);
13819       secname = TREE_STRING_POINTER (sectree);
13820     }
13821   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13822     {
13823       tree sectree = DECL_SECTION_NAME (current_function_decl);
13824       secname = TREE_STRING_POINTER (sectree);
13825     }
13826   else if (cfun && in_cold_section_p)
13827     secname = crtl->subsections.cold_section_label;
13828   else
13829     secname = text_section_label;
13830
13831   return secname;
13832 }
13833
13834 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13835
13836 static bool
13837 decl_by_reference_p (tree decl)
13838 {
13839   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13840            || TREE_CODE (decl) == VAR_DECL)
13841           && DECL_BY_REFERENCE (decl));
13842 }
13843
13844 /* Return single element location list containing loc descr REF.  */
13845
13846 static dw_loc_list_ref
13847 single_element_loc_list (dw_loc_descr_ref ref)
13848 {
13849   return new_loc_list (ref, NULL, NULL, NULL, 0);
13850 }
13851
13852 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13853    for VARLOC.  */
13854
13855 static dw_loc_descr_ref
13856 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13857                enum var_init_status initialized)
13858 {
13859   int have_address = 0;
13860   dw_loc_descr_ref descr;
13861   enum machine_mode mode;
13862
13863   if (want_address != 2)
13864     {
13865       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13866       /* Single part.  */
13867       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
13868         {
13869           varloc = XEXP (XEXP (varloc, 1), 0);
13870           mode = GET_MODE (varloc);
13871           if (MEM_P (varloc))
13872             {
13873               varloc = XEXP (varloc, 0);
13874               have_address = 1;
13875             }
13876           descr = mem_loc_descriptor (varloc, mode, initialized);
13877         }
13878       else
13879         return 0;
13880     }
13881   else
13882     {
13883       descr = loc_descriptor (varloc, DECL_MODE (loc), initialized);
13884       have_address = 1;
13885     }
13886
13887   if (!descr)
13888     return 0;
13889
13890   if (want_address == 2 && !have_address
13891       && (dwarf_version >= 4 || !dwarf_strict))
13892     {
13893       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13894         {
13895           expansion_failed (loc, NULL_RTX,
13896                             "DWARF address size mismatch");
13897           return 0;
13898         }
13899       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13900       have_address = 1;
13901     }
13902   /* Show if we can't fill the request for an address.  */
13903   if (want_address && !have_address)
13904     {
13905       expansion_failed (loc, NULL_RTX,
13906                         "Want address and only have value");
13907       return 0;
13908     }
13909
13910   /* If we've got an address and don't want one, dereference.  */
13911   if (!want_address && have_address)
13912     {
13913       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13914       enum dwarf_location_atom op;
13915
13916       if (size > DWARF2_ADDR_SIZE || size == -1)
13917         {
13918           expansion_failed (loc, NULL_RTX,
13919                             "DWARF address size mismatch");
13920           return 0;
13921         }
13922       else if (size == DWARF2_ADDR_SIZE)
13923         op = DW_OP_deref;
13924       else
13925         op = DW_OP_deref_size;
13926
13927       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13928     }
13929
13930   return descr;
13931 }
13932
13933 /* Return dwarf representation of location list representing for
13934    LOC_LIST of DECL.  WANT_ADDRESS has the same meaning as in
13935    loc_list_from_tree function.  */
13936
13937 static dw_loc_list_ref
13938 dw_loc_list (var_loc_list * loc_list, tree decl, int want_address)
13939 {
13940   const char *endname, *secname;
13941   dw_loc_list_ref list;
13942   rtx varloc;
13943   enum var_init_status initialized;
13944   struct var_loc_node *node;
13945   dw_loc_descr_ref descr;
13946   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13947
13948   /* Now that we know what section we are using for a base,
13949      actually construct the list of locations.
13950      The first location information is what is passed to the
13951      function that creates the location list, and the remaining
13952      locations just get added on to that list.
13953      Note that we only know the start address for a location
13954      (IE location changes), so to build the range, we use
13955      the range [current location start, next location start].
13956      This means we have to special case the last node, and generate
13957      a range of [last location start, end of function label].  */
13958
13959   node = loc_list->first;
13960   secname = secname_for_decl (decl);
13961
13962   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13963     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13964   else
13965     initialized = VAR_INIT_STATUS_INITIALIZED;
13966   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13967   descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13968
13969   if (loc_list && loc_list->first != loc_list->last)
13970     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13971   else
13972     return single_element_loc_list (descr);
13973   node = node->next;
13974
13975   if (!node)
13976     return NULL;
13977
13978   for (; node->next; node = node->next)
13979     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13980       {
13981         /* The variable has a location between NODE->LABEL and
13982            NODE->NEXT->LABEL.  */
13983         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13984         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13985         descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13986         add_loc_descr_to_loc_list (&list, descr,
13987                                    node->label, node->next->label, secname);
13988       }
13989
13990   /* If the variable has a location at the last label
13991      it keeps its location until the end of function.  */
13992   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13993     {
13994       if (!current_function_decl)
13995         endname = text_end_label;
13996       else
13997         {
13998           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13999                                        current_function_funcdef_no);
14000           endname = ggc_strdup (label_id);
14001         }
14002
14003       initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14004       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
14005       descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14006       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
14007     }
14008   return list;
14009 }
14010
14011 /* Return if the loc_list has only single element and thus can be represented
14012    as location description.   */
14013
14014 static bool
14015 single_element_loc_list_p (dw_loc_list_ref list)
14016 {
14017   return (!list->dw_loc_next && !list->begin && !list->end);
14018 }
14019
14020 /* To each location in list LIST add loc descr REF.  */
14021
14022 static void
14023 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14024 {
14025   dw_loc_descr_ref copy;
14026   add_loc_descr (&list->expr, ref);
14027   list = list->dw_loc_next;
14028   while (list)
14029     {
14030       copy = GGC_CNEW (dw_loc_descr_node);
14031       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14032       add_loc_descr (&list->expr, copy);
14033       while (copy->dw_loc_next)
14034         {
14035           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
14036           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14037           copy->dw_loc_next = new_copy;
14038           copy = new_copy;
14039         }
14040       list = list->dw_loc_next;
14041     }
14042 }
14043
14044 /* Given two lists RET and LIST
14045    produce location list that is result of adding expression in LIST
14046    to expression in RET on each possition in program.
14047    Might be destructive on both RET and LIST.
14048
14049    TODO: We handle only simple cases of RET or LIST having at most one
14050    element. General case would inolve sorting the lists in program order
14051    and merging them that will need some additional work.  
14052    Adding that will improve quality of debug info especially for SRA-ed
14053    structures.  */
14054
14055 static void
14056 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14057 {
14058   if (!list)
14059     return;
14060   if (!*ret)
14061     {
14062       *ret = list;
14063       return;
14064     }
14065   if (!list->dw_loc_next)
14066     {
14067       add_loc_descr_to_each (*ret, list->expr);
14068       return;
14069     }
14070   if (!(*ret)->dw_loc_next)
14071     {
14072       add_loc_descr_to_each (list, (*ret)->expr);
14073       *ret = list;
14074       return;
14075     }
14076   expansion_failed (NULL_TREE, NULL_RTX,
14077                     "Don't know how to merge two non-trivial"
14078                     " location lists.\n");
14079   *ret = NULL;
14080   return;
14081 }
14082
14083 /* LOC is constant expression.  Try a luck, look it up in constant
14084    pool and return its loc_descr of its address.  */
14085
14086 static dw_loc_descr_ref
14087 cst_pool_loc_descr (tree loc)
14088 {
14089   /* Get an RTL for this, if something has been emitted.  */
14090   rtx rtl = lookup_constant_def (loc);
14091   enum machine_mode mode;
14092
14093   if (!rtl || !MEM_P (rtl))
14094     {
14095       gcc_assert (!rtl);
14096       return 0;
14097     }
14098   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14099
14100   /* TODO: We might get more coverage if we was actually delaying expansion
14101      of all expressions till end of compilation when constant pools are fully
14102      populated.  */
14103   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14104     {
14105       expansion_failed (loc, NULL_RTX,
14106                         "CST value in contant pool but not marked.");
14107       return 0;
14108     }
14109   mode = GET_MODE (rtl);
14110   rtl = XEXP (rtl, 0);
14111   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14112 }
14113
14114 /* Return dw_loc_list representing address of addr_expr LOC
14115    by looking for innder INDIRECT_REF expression and turing it
14116    into simple arithmetics.  */
14117
14118 static dw_loc_list_ref
14119 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14120 {
14121   tree obj, offset;
14122   HOST_WIDE_INT bitsize, bitpos, bytepos;
14123   enum machine_mode mode;
14124   int volatilep;
14125   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14126   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14127
14128   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14129                              &bitsize, &bitpos, &offset, &mode,
14130                              &unsignedp, &volatilep, false);
14131   STRIP_NOPS (obj);
14132   if (bitpos % BITS_PER_UNIT)
14133     {
14134       expansion_failed (loc, NULL_RTX, "bitfield access");
14135       return 0;
14136     }
14137   if (!INDIRECT_REF_P (obj))
14138     {
14139       expansion_failed (obj,
14140                         NULL_RTX, "no indirect ref in inner refrence");
14141       return 0;
14142     }
14143   if (!offset && !bitpos)
14144     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14145   else if (toplev
14146            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14147            && (dwarf_version >= 4 || !dwarf_strict))
14148     {
14149       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14150       if (!list_ret)
14151         return 0;
14152       if (offset)
14153         {
14154           /* Variable offset.  */
14155           list_ret1 = loc_list_from_tree (offset, 0);
14156           if (list_ret1 == 0)
14157             return 0;
14158           add_loc_list (&list_ret, list_ret1);
14159           if (!list_ret)
14160             return 0;
14161           add_loc_descr_to_each (list_ret,
14162                                  new_loc_descr (DW_OP_plus, 0, 0));
14163         }
14164       bytepos = bitpos / BITS_PER_UNIT;
14165       if (bytepos > 0)
14166         add_loc_descr_to_each (list_ret,
14167                                new_loc_descr (DW_OP_plus_uconst,
14168                                               bytepos, 0));
14169       else if (bytepos < 0)
14170         loc_list_plus_const (list_ret, bytepos);
14171       add_loc_descr_to_each (list_ret,
14172                              new_loc_descr (DW_OP_stack_value, 0, 0));
14173     }
14174   return list_ret;
14175 }
14176
14177
14178 /* Generate Dwarf location list representing LOC.
14179    If WANT_ADDRESS is false, expression computing LOC will be computed
14180    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14181    if WANT_ADDRESS is 2, expression computing address useable in location
14182      will be returned (i.e. DW_OP_reg can be used
14183      to refer to register values).  */
14184
14185 static dw_loc_list_ref
14186 loc_list_from_tree (tree loc, int want_address)
14187 {
14188   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14189   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14190   int have_address = 0;
14191   enum dwarf_location_atom op;
14192
14193   /* ??? Most of the time we do not take proper care for sign/zero
14194      extending the values properly.  Hopefully this won't be a real
14195      problem...  */
14196
14197   switch (TREE_CODE (loc))
14198     {
14199     case ERROR_MARK:
14200       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14201       return 0;
14202
14203     case PLACEHOLDER_EXPR:
14204       /* This case involves extracting fields from an object to determine the
14205          position of other fields.  We don't try to encode this here.  The
14206          only user of this is Ada, which encodes the needed information using
14207          the names of types.  */
14208       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14209       return 0;
14210
14211     case CALL_EXPR:
14212       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14213       /* There are no opcodes for these operations.  */
14214       return 0;
14215
14216     case PREINCREMENT_EXPR:
14217     case PREDECREMENT_EXPR:
14218     case POSTINCREMENT_EXPR:
14219     case POSTDECREMENT_EXPR:
14220       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14221       /* There are no opcodes for these operations.  */
14222       return 0;
14223
14224     case ADDR_EXPR:
14225       /* If we already want an address, see if there is INDIRECT_REF inside
14226          e.g. for &this->field.  */
14227       if (want_address)
14228         {
14229           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14230                        (loc, want_address == 2);
14231           if (list_ret)
14232             have_address = 1;
14233           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14234                    && (ret = cst_pool_loc_descr (loc)))
14235             have_address = 1;
14236         }
14237         /* Otherwise, process the argument and look for the address.  */
14238       if (!list_ret && !ret)
14239         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14240       else
14241         {
14242           if (want_address)
14243             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14244           return NULL;
14245         }
14246       break;
14247
14248     case VAR_DECL:
14249       if (DECL_THREAD_LOCAL_P (loc))
14250         {
14251           rtx rtl;
14252           enum dwarf_location_atom first_op;
14253           enum dwarf_location_atom second_op;
14254           bool dtprel = false;
14255
14256           if (targetm.have_tls)
14257             {
14258               /* If this is not defined, we have no way to emit the
14259                  data.  */
14260               if (!targetm.asm_out.output_dwarf_dtprel)
14261                 return 0;
14262
14263                /* The way DW_OP_GNU_push_tls_address is specified, we
14264                   can only look up addresses of objects in the current
14265                   module.  */
14266               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14267                 return 0;
14268               first_op = DW_OP_addr;
14269               dtprel = true;
14270               second_op = DW_OP_GNU_push_tls_address;
14271             }
14272           else
14273             {
14274               if (!targetm.emutls.debug_form_tls_address
14275                   || !(dwarf_version >= 3 || !dwarf_strict))
14276                 return 0;
14277               loc = emutls_decl (loc);
14278               first_op = DW_OP_addr;
14279               second_op = DW_OP_form_tls_address;
14280             }
14281
14282           rtl = rtl_for_decl_location (loc);
14283           if (rtl == NULL_RTX)
14284             return 0;
14285
14286           if (!MEM_P (rtl))
14287             return 0;
14288           rtl = XEXP (rtl, 0);
14289           if (! CONSTANT_P (rtl))
14290             return 0;
14291
14292           ret = new_loc_descr (first_op, 0, 0);
14293           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14294           ret->dw_loc_oprnd1.v.val_addr = rtl;
14295           ret->dtprel = dtprel;
14296
14297           ret1 = new_loc_descr (second_op, 0, 0);
14298           add_loc_descr (&ret, ret1);
14299
14300           have_address = 1;
14301           break;
14302         }
14303       /* FALLTHRU */
14304
14305     case PARM_DECL:
14306       if (DECL_HAS_VALUE_EXPR_P (loc))
14307         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14308                                    want_address);
14309       /* FALLTHRU */
14310
14311     case RESULT_DECL:
14312     case FUNCTION_DECL:
14313       {
14314         rtx rtl = rtl_for_decl_location (loc);
14315         var_loc_list *loc_list = lookup_decl_loc (loc);
14316
14317         if (loc_list && loc_list->first
14318             && (list_ret = dw_loc_list (loc_list, loc, want_address)))
14319           have_address = want_address != 0;
14320         else if (rtl == NULL_RTX)
14321           {
14322             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14323             return 0;
14324           }
14325         else if (CONST_INT_P (rtl))
14326           {
14327             HOST_WIDE_INT val = INTVAL (rtl);
14328             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14329               val &= GET_MODE_MASK (DECL_MODE (loc));
14330             ret = int_loc_descriptor (val);
14331           }
14332         else if (GET_CODE (rtl) == CONST_STRING)
14333           {
14334             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14335             return 0;
14336           }
14337         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14338           {
14339             ret = new_loc_descr (DW_OP_addr, 0, 0);
14340             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14341             ret->dw_loc_oprnd1.v.val_addr = rtl;
14342           }
14343         else
14344           {
14345             enum machine_mode mode;
14346
14347             /* Certain constructs can only be represented at top-level.  */
14348             if (want_address == 2)
14349               {
14350                 ret = loc_descriptor (rtl, VOIDmode,
14351                                       VAR_INIT_STATUS_INITIALIZED);
14352                 have_address = 1;
14353               }
14354             else
14355               {
14356                 mode = GET_MODE (rtl);
14357                 if (MEM_P (rtl))
14358                   {
14359                     rtl = XEXP (rtl, 0);
14360                     have_address = 1;
14361                   }
14362                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14363               }
14364             if (!ret)
14365               expansion_failed (loc, rtl,
14366                                 "failed to produce loc descriptor for rtl");
14367           }
14368       }
14369       break;
14370
14371     case INDIRECT_REF:
14372     case ALIGN_INDIRECT_REF:
14373     case MISALIGNED_INDIRECT_REF:
14374       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14375       have_address = 1;
14376       break;
14377
14378     case COMPOUND_EXPR:
14379       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14380
14381     CASE_CONVERT:
14382     case VIEW_CONVERT_EXPR:
14383     case SAVE_EXPR:
14384     case MODIFY_EXPR:
14385       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14386
14387     case COMPONENT_REF:
14388     case BIT_FIELD_REF:
14389     case ARRAY_REF:
14390     case ARRAY_RANGE_REF:
14391     case REALPART_EXPR:
14392     case IMAGPART_EXPR:
14393       {
14394         tree obj, offset;
14395         HOST_WIDE_INT bitsize, bitpos, bytepos;
14396         enum machine_mode mode;
14397         int volatilep;
14398         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14399
14400         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14401                                    &unsignedp, &volatilep, false);
14402
14403         gcc_assert (obj != loc);
14404
14405         list_ret = loc_list_from_tree (obj,
14406                                        want_address == 2
14407                                        && !bitpos && !offset ? 2 : 1);
14408         /* TODO: We can extract value of the small expression via shifting even
14409            for nonzero bitpos.  */
14410         if (list_ret == 0)
14411           return 0;
14412         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14413           {
14414             expansion_failed (loc, NULL_RTX,
14415                               "bitfield access");
14416             return 0;
14417           }
14418
14419         if (offset != NULL_TREE)
14420           {
14421             /* Variable offset.  */
14422             list_ret1 = loc_list_from_tree (offset, 0);
14423             if (list_ret1 == 0)
14424               return 0;
14425             add_loc_list (&list_ret, list_ret1);
14426             if (!list_ret)
14427               return 0;
14428             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14429           }
14430
14431         bytepos = bitpos / BITS_PER_UNIT;
14432         if (bytepos > 0)
14433           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14434         else if (bytepos < 0)
14435           loc_list_plus_const (list_ret, bytepos); 
14436
14437         have_address = 1;
14438         break;
14439       }
14440
14441     case INTEGER_CST:
14442       if ((want_address || !host_integerp (loc, 0))
14443           && (ret = cst_pool_loc_descr (loc)))
14444         have_address = 1;
14445       else if (want_address == 2
14446                && host_integerp (loc, 0)
14447                && (ret = address_of_int_loc_descriptor
14448                            (int_size_in_bytes (TREE_TYPE (loc)),
14449                             tree_low_cst (loc, 0))))
14450         have_address = 1;
14451       else if (host_integerp (loc, 0))
14452         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14453       else
14454         {
14455           expansion_failed (loc, NULL_RTX,
14456                             "Integer operand is not host integer");
14457           return 0;
14458         }
14459       break;
14460
14461     case CONSTRUCTOR:
14462     case REAL_CST:
14463     case STRING_CST:
14464     case COMPLEX_CST:
14465       if ((ret = cst_pool_loc_descr (loc)))
14466         have_address = 1;
14467       else
14468       /* We can construct small constants here using int_loc_descriptor.  */
14469         expansion_failed (loc, NULL_RTX,
14470                           "constructor or constant not in constant pool");
14471       break;
14472
14473     case TRUTH_AND_EXPR:
14474     case TRUTH_ANDIF_EXPR:
14475     case BIT_AND_EXPR:
14476       op = DW_OP_and;
14477       goto do_binop;
14478
14479     case TRUTH_XOR_EXPR:
14480     case BIT_XOR_EXPR:
14481       op = DW_OP_xor;
14482       goto do_binop;
14483
14484     case TRUTH_OR_EXPR:
14485     case TRUTH_ORIF_EXPR:
14486     case BIT_IOR_EXPR:
14487       op = DW_OP_or;
14488       goto do_binop;
14489
14490     case FLOOR_DIV_EXPR:
14491     case CEIL_DIV_EXPR:
14492     case ROUND_DIV_EXPR:
14493     case TRUNC_DIV_EXPR:
14494       op = DW_OP_div;
14495       goto do_binop;
14496
14497     case MINUS_EXPR:
14498       op = DW_OP_minus;
14499       goto do_binop;
14500
14501     case FLOOR_MOD_EXPR:
14502     case CEIL_MOD_EXPR:
14503     case ROUND_MOD_EXPR:
14504     case TRUNC_MOD_EXPR:
14505       op = DW_OP_mod;
14506       goto do_binop;
14507
14508     case MULT_EXPR:
14509       op = DW_OP_mul;
14510       goto do_binop;
14511
14512     case LSHIFT_EXPR:
14513       op = DW_OP_shl;
14514       goto do_binop;
14515
14516     case RSHIFT_EXPR:
14517       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14518       goto do_binop;
14519
14520     case POINTER_PLUS_EXPR:
14521     case PLUS_EXPR:
14522       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
14523           && host_integerp (TREE_OPERAND (loc, 1), 0))
14524         {
14525           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14526           if (list_ret == 0)
14527             return 0;
14528
14529           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14530           break;
14531         }
14532
14533       op = DW_OP_plus;
14534       goto do_binop;
14535
14536     case LE_EXPR:
14537       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14538         return 0;
14539
14540       op = DW_OP_le;
14541       goto do_binop;
14542
14543     case GE_EXPR:
14544       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14545         return 0;
14546
14547       op = DW_OP_ge;
14548       goto do_binop;
14549
14550     case LT_EXPR:
14551       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14552         return 0;
14553
14554       op = DW_OP_lt;
14555       goto do_binop;
14556
14557     case GT_EXPR:
14558       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14559         return 0;
14560
14561       op = DW_OP_gt;
14562       goto do_binop;
14563
14564     case EQ_EXPR:
14565       op = DW_OP_eq;
14566       goto do_binop;
14567
14568     case NE_EXPR:
14569       op = DW_OP_ne;
14570       goto do_binop;
14571
14572     do_binop:
14573       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14574       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14575       if (list_ret == 0 || list_ret1 == 0)
14576         return 0;
14577
14578       add_loc_list (&list_ret, list_ret1);
14579       if (list_ret == 0)
14580         return 0;
14581       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14582       break;
14583
14584     case TRUTH_NOT_EXPR:
14585     case BIT_NOT_EXPR:
14586       op = DW_OP_not;
14587       goto do_unop;
14588
14589     case ABS_EXPR:
14590       op = DW_OP_abs;
14591       goto do_unop;
14592
14593     case NEGATE_EXPR:
14594       op = DW_OP_neg;
14595       goto do_unop;
14596
14597     do_unop:
14598       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14599       if (list_ret == 0)
14600         return 0;
14601
14602       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14603       break;
14604
14605     case MIN_EXPR:
14606     case MAX_EXPR:
14607       {
14608         const enum tree_code code =
14609           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14610
14611         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14612                       build2 (code, integer_type_node,
14613                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14614                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14615       }
14616
14617       /* ... fall through ...  */
14618
14619     case COND_EXPR:
14620       {
14621         dw_loc_descr_ref lhs
14622           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14623         dw_loc_list_ref rhs
14624           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14625         dw_loc_descr_ref bra_node, jump_node, tmp;
14626
14627         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14628         if (list_ret == 0 || lhs == 0 || rhs == 0)
14629           return 0;
14630
14631         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14632         add_loc_descr_to_each (list_ret, bra_node);
14633
14634         add_loc_list (&list_ret, rhs);
14635         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14636         add_loc_descr_to_each (list_ret, jump_node);
14637
14638         add_loc_descr_to_each (list_ret, lhs);
14639         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14640         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14641
14642         /* ??? Need a node to point the skip at.  Use a nop.  */
14643         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14644         add_loc_descr_to_each (list_ret, tmp);
14645         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14646         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14647       }
14648       break;
14649
14650     case FIX_TRUNC_EXPR:
14651       return 0;
14652
14653     default:
14654       /* Leave front-end specific codes as simply unknown.  This comes
14655          up, for instance, with the C STMT_EXPR.  */
14656       if ((unsigned int) TREE_CODE (loc)
14657           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14658         {
14659           expansion_failed (loc, NULL_RTX,
14660                             "language specific tree node");
14661           return 0;
14662         }
14663
14664 #ifdef ENABLE_CHECKING
14665       /* Otherwise this is a generic code; we should just lists all of
14666          these explicitly.  We forgot one.  */
14667       gcc_unreachable ();
14668 #else
14669       /* In a release build, we want to degrade gracefully: better to
14670          generate incomplete debugging information than to crash.  */
14671       return NULL;
14672 #endif
14673     }
14674
14675   if (!ret && !list_ret)
14676     return 0;
14677
14678   if (want_address == 2 && !have_address
14679       && (dwarf_version >= 4 || !dwarf_strict))
14680     {
14681       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14682         {
14683           expansion_failed (loc, NULL_RTX,
14684                             "DWARF address size mismatch");
14685           return 0;
14686         }
14687       if (ret)
14688         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14689       else
14690         add_loc_descr_to_each (list_ret,
14691                                new_loc_descr (DW_OP_stack_value, 0, 0));
14692       have_address = 1;
14693     }
14694   /* Show if we can't fill the request for an address.  */
14695   if (want_address && !have_address)
14696     {
14697       expansion_failed (loc, NULL_RTX,
14698                         "Want address and only have value");
14699       return 0;
14700     }
14701
14702   gcc_assert (!ret || !list_ret);
14703
14704   /* If we've got an address and don't want one, dereference.  */
14705   if (!want_address && have_address)
14706     {
14707       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14708
14709       if (size > DWARF2_ADDR_SIZE || size == -1)
14710         {
14711           expansion_failed (loc, NULL_RTX,
14712                             "DWARF address size mismatch");
14713           return 0;
14714         }
14715       else if (size == DWARF2_ADDR_SIZE)
14716         op = DW_OP_deref;
14717       else
14718         op = DW_OP_deref_size;
14719
14720       if (ret)
14721         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14722       else
14723         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14724     }
14725   if (ret)
14726     list_ret = single_element_loc_list (ret);
14727
14728   return list_ret;
14729 }
14730
14731 /* Same as above but return only single location expression.  */
14732 static dw_loc_descr_ref
14733 loc_descriptor_from_tree (tree loc, int want_address)
14734 {
14735   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14736   if (!ret)
14737     return NULL;
14738   if (ret->dw_loc_next)
14739     {
14740       expansion_failed (loc, NULL_RTX,
14741                         "Location list where only loc descriptor needed");
14742       return NULL;
14743     }
14744   return ret->expr;
14745 }
14746
14747 /* Given a value, round it up to the lowest multiple of `boundary'
14748    which is not less than the value itself.  */
14749
14750 static inline HOST_WIDE_INT
14751 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14752 {
14753   return (((value + boundary - 1) / boundary) * boundary);
14754 }
14755
14756 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14757    pointer to the declared type for the relevant field variable, or return
14758    `integer_type_node' if the given node turns out to be an
14759    ERROR_MARK node.  */
14760
14761 static inline tree
14762 field_type (const_tree decl)
14763 {
14764   tree type;
14765
14766   if (TREE_CODE (decl) == ERROR_MARK)
14767     return integer_type_node;
14768
14769   type = DECL_BIT_FIELD_TYPE (decl);
14770   if (type == NULL_TREE)
14771     type = TREE_TYPE (decl);
14772
14773   return type;
14774 }
14775
14776 /* Given a pointer to a tree node, return the alignment in bits for
14777    it, or else return BITS_PER_WORD if the node actually turns out to
14778    be an ERROR_MARK node.  */
14779
14780 static inline unsigned
14781 simple_type_align_in_bits (const_tree type)
14782 {
14783   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14784 }
14785
14786 static inline unsigned
14787 simple_decl_align_in_bits (const_tree decl)
14788 {
14789   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14790 }
14791
14792 /* Return the result of rounding T up to ALIGN.  */
14793
14794 static inline HOST_WIDE_INT
14795 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
14796 {
14797   /* We must be careful if T is negative because HOST_WIDE_INT can be
14798      either "above" or "below" unsigned int as per the C promotion
14799      rules, depending on the host, thus making the signedness of the
14800      direct multiplication and division unpredictable.  */
14801   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
14802
14803   u += align - 1;
14804   u /= align;
14805   u *= align;
14806
14807   return (HOST_WIDE_INT) u;
14808 }
14809
14810 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14811    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14812    or return 0 if we are unable to determine what that offset is, either
14813    because the argument turns out to be a pointer to an ERROR_MARK node, or
14814    because the offset is actually variable.  (We can't handle the latter case
14815    just yet).  */
14816
14817 static HOST_WIDE_INT
14818 field_byte_offset (const_tree decl)
14819 {
14820   HOST_WIDE_INT object_offset_in_bits;
14821   HOST_WIDE_INT bitpos_int;
14822
14823   if (TREE_CODE (decl) == ERROR_MARK)
14824     return 0;
14825
14826   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14827
14828   /* We cannot yet cope with fields whose positions are variable, so
14829      for now, when we see such things, we simply return 0.  Someday, we may
14830      be able to handle such cases, but it will be damn difficult.  */
14831   if (! host_integerp (bit_position (decl), 0))
14832     return 0;
14833
14834   bitpos_int = int_bit_position (decl);
14835
14836 #ifdef PCC_BITFIELD_TYPE_MATTERS
14837   if (PCC_BITFIELD_TYPE_MATTERS)
14838     {
14839       tree type;
14840       tree field_size_tree;
14841       HOST_WIDE_INT deepest_bitpos;
14842       unsigned HOST_WIDE_INT field_size_in_bits;
14843       unsigned int type_align_in_bits;
14844       unsigned int decl_align_in_bits;
14845       unsigned HOST_WIDE_INT type_size_in_bits;
14846
14847       type = field_type (decl);
14848       type_size_in_bits = simple_type_size_in_bits (type);
14849       type_align_in_bits = simple_type_align_in_bits (type);
14850
14851       field_size_tree = DECL_SIZE (decl);
14852
14853       /* The size could be unspecified if there was an error, or for
14854          a flexible array member.  */
14855       if (!field_size_tree)
14856         field_size_tree = bitsize_zero_node;
14857
14858       /* If the size of the field is not constant, use the type size.  */
14859       if (host_integerp (field_size_tree, 1))
14860         field_size_in_bits = tree_low_cst (field_size_tree, 1);
14861       else
14862         field_size_in_bits = type_size_in_bits;
14863
14864       decl_align_in_bits = simple_decl_align_in_bits (decl);
14865
14866       /* The GCC front-end doesn't make any attempt to keep track of the
14867          starting bit offset (relative to the start of the containing
14868          structure type) of the hypothetical "containing object" for a
14869          bit-field.  Thus, when computing the byte offset value for the
14870          start of the "containing object" of a bit-field, we must deduce
14871          this information on our own. This can be rather tricky to do in
14872          some cases.  For example, handling the following structure type
14873          definition when compiling for an i386/i486 target (which only
14874          aligns long long's to 32-bit boundaries) can be very tricky:
14875
14876          struct S { int field1; long long field2:31; };
14877
14878          Fortunately, there is a simple rule-of-thumb which can be used
14879          in such cases.  When compiling for an i386/i486, GCC will
14880          allocate 8 bytes for the structure shown above.  It decides to
14881          do this based upon one simple rule for bit-field allocation.
14882          GCC allocates each "containing object" for each bit-field at
14883          the first (i.e. lowest addressed) legitimate alignment boundary
14884          (based upon the required minimum alignment for the declared
14885          type of the field) which it can possibly use, subject to the
14886          condition that there is still enough available space remaining
14887          in the containing object (when allocated at the selected point)
14888          to fully accommodate all of the bits of the bit-field itself.
14889
14890          This simple rule makes it obvious why GCC allocates 8 bytes for
14891          each object of the structure type shown above.  When looking
14892          for a place to allocate the "containing object" for `field2',
14893          the compiler simply tries to allocate a 64-bit "containing
14894          object" at each successive 32-bit boundary (starting at zero)
14895          until it finds a place to allocate that 64- bit field such that
14896          at least 31 contiguous (and previously unallocated) bits remain
14897          within that selected 64 bit field.  (As it turns out, for the
14898          example above, the compiler finds it is OK to allocate the
14899          "containing object" 64-bit field at bit-offset zero within the
14900          structure type.)
14901
14902          Here we attempt to work backwards from the limited set of facts
14903          we're given, and we try to deduce from those facts, where GCC
14904          must have believed that the containing object started (within
14905          the structure type). The value we deduce is then used (by the
14906          callers of this routine) to generate DW_AT_location and
14907          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14908          the case of DW_AT_location, regular fields as well).  */
14909
14910       /* Figure out the bit-distance from the start of the structure to
14911          the "deepest" bit of the bit-field.  */
14912       deepest_bitpos = bitpos_int + field_size_in_bits;
14913
14914       /* This is the tricky part.  Use some fancy footwork to deduce
14915          where the lowest addressed bit of the containing object must
14916          be.  */
14917       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14918
14919       /* Round up to type_align by default.  This works best for
14920          bitfields.  */
14921       object_offset_in_bits
14922         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14923
14924       if (object_offset_in_bits > bitpos_int)
14925         {
14926           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14927
14928           /* Round up to decl_align instead.  */
14929           object_offset_in_bits
14930             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14931         }
14932     }
14933   else
14934 #endif
14935     object_offset_in_bits = bitpos_int;
14936
14937   return object_offset_in_bits / BITS_PER_UNIT;
14938 }
14939 \f
14940 /* The following routines define various Dwarf attributes and any data
14941    associated with them.  */
14942
14943 /* Add a location description attribute value to a DIE.
14944
14945    This emits location attributes suitable for whole variables and
14946    whole parameters.  Note that the location attributes for struct fields are
14947    generated by the routine `data_member_location_attribute' below.  */
14948
14949 static inline void
14950 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14951                              dw_loc_list_ref descr)
14952 {
14953   if (descr == 0)
14954     return;
14955   if (single_element_loc_list_p (descr))
14956     add_AT_loc (die, attr_kind, descr->expr);
14957   else
14958     add_AT_loc_list (die, attr_kind, descr);
14959 }
14960
14961 /* Attach the specialized form of location attribute used for data members of
14962    struct and union types.  In the special case of a FIELD_DECL node which
14963    represents a bit-field, the "offset" part of this special location
14964    descriptor must indicate the distance in bytes from the lowest-addressed
14965    byte of the containing struct or union type to the lowest-addressed byte of
14966    the "containing object" for the bit-field.  (See the `field_byte_offset'
14967    function above).
14968
14969    For any given bit-field, the "containing object" is a hypothetical object
14970    (of some integral or enum type) within which the given bit-field lives.  The
14971    type of this hypothetical "containing object" is always the same as the
14972    declared type of the individual bit-field itself (for GCC anyway... the
14973    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14974    bytes) of the hypothetical "containing object" which will be given in the
14975    DW_AT_byte_size attribute for this bit-field.  (See the
14976    `byte_size_attribute' function below.)  It is also used when calculating the
14977    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14978    function below.)  */
14979
14980 static void
14981 add_data_member_location_attribute (dw_die_ref die, tree decl)
14982 {
14983   HOST_WIDE_INT offset;
14984   dw_loc_descr_ref loc_descr = 0;
14985
14986   if (TREE_CODE (decl) == TREE_BINFO)
14987     {
14988       /* We're working on the TAG_inheritance for a base class.  */
14989       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14990         {
14991           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14992              aren't at a fixed offset from all (sub)objects of the same
14993              type.  We need to extract the appropriate offset from our
14994              vtable.  The following dwarf expression means
14995
14996                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14997
14998              This is specific to the V3 ABI, of course.  */
14999
15000           dw_loc_descr_ref tmp;
15001
15002           /* Make a copy of the object address.  */
15003           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15004           add_loc_descr (&loc_descr, tmp);
15005
15006           /* Extract the vtable address.  */
15007           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15008           add_loc_descr (&loc_descr, tmp);
15009
15010           /* Calculate the address of the offset.  */
15011           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15012           gcc_assert (offset < 0);
15013
15014           tmp = int_loc_descriptor (-offset);
15015           add_loc_descr (&loc_descr, tmp);
15016           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15017           add_loc_descr (&loc_descr, tmp);
15018
15019           /* Extract the offset.  */
15020           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15021           add_loc_descr (&loc_descr, tmp);
15022
15023           /* Add it to the object address.  */
15024           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15025           add_loc_descr (&loc_descr, tmp);
15026         }
15027       else
15028         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15029     }
15030   else
15031     offset = field_byte_offset (decl);
15032
15033   if (! loc_descr)
15034     {
15035       if (dwarf_version > 2)
15036         {
15037           /* Don't need to output a location expression, just the constant. */
15038           add_AT_int (die, DW_AT_data_member_location, offset);
15039           return;
15040         }
15041       else
15042         {
15043           enum dwarf_location_atom op;
15044           
15045           /* The DWARF2 standard says that we should assume that the structure
15046              address is already on the stack, so we can specify a structure
15047              field address by using DW_OP_plus_uconst.  */
15048           
15049 #ifdef MIPS_DEBUGGING_INFO
15050           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15051              operator correctly.  It works only if we leave the offset on the
15052              stack.  */
15053           op = DW_OP_constu;
15054 #else
15055           op = DW_OP_plus_uconst;
15056 #endif
15057           
15058           loc_descr = new_loc_descr (op, offset, 0);
15059         }
15060     }
15061
15062   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15063 }
15064
15065 /* Writes integer values to dw_vec_const array.  */
15066
15067 static void
15068 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15069 {
15070   while (size != 0)
15071     {
15072       *dest++ = val & 0xff;
15073       val >>= 8;
15074       --size;
15075     }
15076 }
15077
15078 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15079
15080 static HOST_WIDE_INT
15081 extract_int (const unsigned char *src, unsigned int size)
15082 {
15083   HOST_WIDE_INT val = 0;
15084
15085   src += size;
15086   while (size != 0)
15087     {
15088       val <<= 8;
15089       val |= *--src & 0xff;
15090       --size;
15091     }
15092   return val;
15093 }
15094
15095 /* Writes floating point values to dw_vec_const array.  */
15096
15097 static void
15098 insert_float (const_rtx rtl, unsigned char *array)
15099 {
15100   REAL_VALUE_TYPE rv;
15101   long val[4];
15102   int i;
15103
15104   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15105   real_to_target (val, &rv, GET_MODE (rtl));
15106
15107   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15108   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15109     {
15110       insert_int (val[i], 4, array);
15111       array += 4;
15112     }
15113 }
15114
15115 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15116    does not have a "location" either in memory or in a register.  These
15117    things can arise in GNU C when a constant is passed as an actual parameter
15118    to an inlined function.  They can also arise in C++ where declared
15119    constants do not necessarily get memory "homes".  */
15120
15121 static bool
15122 add_const_value_attribute (dw_die_ref die, rtx rtl)
15123 {
15124   switch (GET_CODE (rtl))
15125     {
15126     case CONST_INT:
15127       {
15128         HOST_WIDE_INT val = INTVAL (rtl);
15129
15130         if (val < 0)
15131           add_AT_int (die, DW_AT_const_value, val);
15132         else
15133           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15134       }
15135       return true;
15136
15137     case CONST_DOUBLE:
15138       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15139          floating-point constant.  A CONST_DOUBLE is used whenever the
15140          constant requires more than one word in order to be adequately
15141          represented.  */
15142       {
15143         enum machine_mode mode = GET_MODE (rtl);
15144
15145         if (SCALAR_FLOAT_MODE_P (mode))
15146           {
15147             unsigned int length = GET_MODE_SIZE (mode);
15148             unsigned char *array = GGC_NEWVEC (unsigned char, length);
15149
15150             insert_float (rtl, array);
15151             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15152           }
15153         else
15154           add_AT_double (die, DW_AT_const_value,
15155                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15156       }
15157       return true;
15158
15159     case CONST_VECTOR:
15160       {
15161         enum machine_mode mode = GET_MODE (rtl);
15162         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15163         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15164         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
15165         unsigned int i;
15166         unsigned char *p;
15167
15168         switch (GET_MODE_CLASS (mode))
15169           {
15170           case MODE_VECTOR_INT:
15171             for (i = 0, p = array; i < length; i++, p += elt_size)
15172               {
15173                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15174                 HOST_WIDE_INT lo, hi;
15175
15176                 switch (GET_CODE (elt))
15177                   {
15178                   case CONST_INT:
15179                     lo = INTVAL (elt);
15180                     hi = -(lo < 0);
15181                     break;
15182
15183                   case CONST_DOUBLE:
15184                     lo = CONST_DOUBLE_LOW (elt);
15185                     hi = CONST_DOUBLE_HIGH (elt);
15186                     break;
15187
15188                   default:
15189                     gcc_unreachable ();
15190                   }
15191
15192                 if (elt_size <= sizeof (HOST_WIDE_INT))
15193                   insert_int (lo, elt_size, p);
15194                 else
15195                   {
15196                     unsigned char *p0 = p;
15197                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
15198
15199                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15200                     if (WORDS_BIG_ENDIAN)
15201                       {
15202                         p0 = p1;
15203                         p1 = p;
15204                       }
15205                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
15206                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
15207                   }
15208               }
15209             break;
15210
15211           case MODE_VECTOR_FLOAT:
15212             for (i = 0, p = array; i < length; i++, p += elt_size)
15213               {
15214                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15215                 insert_float (elt, p);
15216               }
15217             break;
15218
15219           default:
15220             gcc_unreachable ();
15221           }
15222
15223         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15224       }
15225       return true;
15226
15227     case CONST_STRING:
15228       resolve_one_addr (&rtl, NULL);
15229       add_AT_addr (die, DW_AT_const_value, rtl);
15230       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15231       return true;
15232
15233     case CONST:
15234       if (CONSTANT_P (XEXP (rtl, 0)))
15235         return add_const_value_attribute (die, XEXP (rtl, 0));
15236       /* FALLTHROUGH */
15237     case SYMBOL_REF:
15238       if (!const_ok_for_output (rtl))
15239         return false;
15240     case LABEL_REF:
15241       add_AT_addr (die, DW_AT_const_value, rtl);
15242       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15243       return true;
15244
15245     case PLUS:
15246       /* In cases where an inlined instance of an inline function is passed
15247          the address of an `auto' variable (which is local to the caller) we
15248          can get a situation where the DECL_RTL of the artificial local
15249          variable (for the inlining) which acts as a stand-in for the
15250          corresponding formal parameter (of the inline function) will look
15251          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15252          exactly a compile-time constant expression, but it isn't the address
15253          of the (artificial) local variable either.  Rather, it represents the
15254          *value* which the artificial local variable always has during its
15255          lifetime.  We currently have no way to represent such quasi-constant
15256          values in Dwarf, so for now we just punt and generate nothing.  */
15257       return false;
15258
15259     case HIGH:
15260     case CONST_FIXED:
15261       return false;
15262
15263     case MEM:
15264       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15265           && MEM_READONLY_P (rtl)
15266           && GET_MODE (rtl) == BLKmode)
15267         {
15268           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15269           return true;
15270         }
15271       return false;
15272
15273     default:
15274       /* No other kinds of rtx should be possible here.  */
15275       gcc_unreachable ();
15276     }
15277   return false;
15278 }
15279
15280 /* Determine whether the evaluation of EXPR references any variables
15281    or functions which aren't otherwise used (and therefore may not be
15282    output).  */
15283 static tree
15284 reference_to_unused (tree * tp, int * walk_subtrees,
15285                      void * data ATTRIBUTE_UNUSED)
15286 {
15287   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15288     *walk_subtrees = 0;
15289
15290   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15291       && ! TREE_ASM_WRITTEN (*tp))
15292     return *tp;
15293   /* ???  The C++ FE emits debug information for using decls, so
15294      putting gcc_unreachable here falls over.  See PR31899.  For now
15295      be conservative.  */
15296   else if (!cgraph_global_info_ready
15297            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15298     return *tp;
15299   else if (TREE_CODE (*tp) == VAR_DECL)
15300     {
15301       struct varpool_node *node = varpool_node (*tp);
15302       if (!node->needed)
15303         return *tp;
15304     }
15305   else if (TREE_CODE (*tp) == FUNCTION_DECL
15306            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15307     {
15308       /* The call graph machinery must have finished analyzing,
15309          optimizing and gimplifying the CU by now.
15310          So if *TP has no call graph node associated
15311          to it, it means *TP will not be emitted.  */
15312       if (!cgraph_get_node (*tp))
15313         return *tp;
15314     }
15315   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15316     return *tp;
15317
15318   return NULL_TREE;
15319 }
15320
15321 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15322    for use in a later add_const_value_attribute call.  */
15323
15324 static rtx
15325 rtl_for_decl_init (tree init, tree type)
15326 {
15327   rtx rtl = NULL_RTX;
15328
15329   /* If a variable is initialized with a string constant without embedded
15330      zeros, build CONST_STRING.  */
15331   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15332     {
15333       tree enttype = TREE_TYPE (type);
15334       tree domain = TYPE_DOMAIN (type);
15335       enum machine_mode mode = TYPE_MODE (enttype);
15336
15337       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15338           && domain
15339           && integer_zerop (TYPE_MIN_VALUE (domain))
15340           && compare_tree_int (TYPE_MAX_VALUE (domain),
15341                                TREE_STRING_LENGTH (init) - 1) == 0
15342           && ((size_t) TREE_STRING_LENGTH (init)
15343               == strlen (TREE_STRING_POINTER (init)) + 1))
15344         {
15345           rtl = gen_rtx_CONST_STRING (VOIDmode,
15346                                       ggc_strdup (TREE_STRING_POINTER (init)));
15347           rtl = gen_rtx_MEM (BLKmode, rtl);
15348           MEM_READONLY_P (rtl) = 1;
15349         }
15350     }
15351   /* Other aggregates, and complex values, could be represented using
15352      CONCAT: FIXME!  */
15353   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
15354     ;
15355   /* Vectors only work if their mode is supported by the target.
15356      FIXME: generic vectors ought to work too.  */
15357   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
15358     ;
15359   /* If the initializer is something that we know will expand into an
15360      immediate RTL constant, expand it now.  We must be careful not to
15361      reference variables which won't be output.  */
15362   else if (initializer_constant_valid_p (init, type)
15363            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15364     {
15365       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15366          possible.  */
15367       if (TREE_CODE (type) == VECTOR_TYPE)
15368         switch (TREE_CODE (init))
15369           {
15370           case VECTOR_CST:
15371             break;
15372           case CONSTRUCTOR:
15373             if (TREE_CONSTANT (init))
15374               {
15375                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
15376                 bool constant_p = true;
15377                 tree value;
15378                 unsigned HOST_WIDE_INT ix;
15379
15380                 /* Even when ctor is constant, it might contain non-*_CST
15381                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15382                    belong into VECTOR_CST nodes.  */
15383                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15384                   if (!CONSTANT_CLASS_P (value))
15385                     {
15386                       constant_p = false;
15387                       break;
15388                     }
15389
15390                 if (constant_p)
15391                   {
15392                     init = build_vector_from_ctor (type, elts);
15393                     break;
15394                   }
15395               }
15396             /* FALLTHRU */
15397
15398           default:
15399             return NULL;
15400           }
15401
15402       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15403
15404       /* If expand_expr returns a MEM, it wasn't immediate.  */
15405       gcc_assert (!rtl || !MEM_P (rtl));
15406     }
15407
15408   return rtl;
15409 }
15410
15411 /* Generate RTL for the variable DECL to represent its location.  */
15412
15413 static rtx
15414 rtl_for_decl_location (tree decl)
15415 {
15416   rtx rtl;
15417
15418   /* Here we have to decide where we are going to say the parameter "lives"
15419      (as far as the debugger is concerned).  We only have a couple of
15420      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15421
15422      DECL_RTL normally indicates where the parameter lives during most of the
15423      activation of the function.  If optimization is enabled however, this
15424      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15425      that the parameter doesn't really live anywhere (as far as the code
15426      generation parts of GCC are concerned) during most of the function's
15427      activation.  That will happen (for example) if the parameter is never
15428      referenced within the function.
15429
15430      We could just generate a location descriptor here for all non-NULL
15431      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15432      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15433      where DECL_RTL is NULL or is a pseudo-reg.
15434
15435      Note however that we can only get away with using DECL_INCOMING_RTL as
15436      a backup substitute for DECL_RTL in certain limited cases.  In cases
15437      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15438      we can be sure that the parameter was passed using the same type as it is
15439      declared to have within the function, and that its DECL_INCOMING_RTL
15440      points us to a place where a value of that type is passed.
15441
15442      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15443      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15444      because in these cases DECL_INCOMING_RTL points us to a value of some
15445      type which is *different* from the type of the parameter itself.  Thus,
15446      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15447      such cases, the debugger would end up (for example) trying to fetch a
15448      `float' from a place which actually contains the first part of a
15449      `double'.  That would lead to really incorrect and confusing
15450      output at debug-time.
15451
15452      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15453      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15454      are a couple of exceptions however.  On little-endian machines we can
15455      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15456      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15457      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15458      when (on a little-endian machine) a non-prototyped function has a
15459      parameter declared to be of type `short' or `char'.  In such cases,
15460      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15461      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15462      passed `int' value.  If the debugger then uses that address to fetch
15463      a `short' or a `char' (on a little-endian machine) the result will be
15464      the correct data, so we allow for such exceptional cases below.
15465
15466      Note that our goal here is to describe the place where the given formal
15467      parameter lives during most of the function's activation (i.e. between the
15468      end of the prologue and the start of the epilogue).  We'll do that as best
15469      as we can. Note however that if the given formal parameter is modified
15470      sometime during the execution of the function, then a stack backtrace (at
15471      debug-time) will show the function as having been called with the *new*
15472      value rather than the value which was originally passed in.  This happens
15473      rarely enough that it is not a major problem, but it *is* a problem, and
15474      I'd like to fix it.
15475
15476      A future version of dwarf2out.c may generate two additional attributes for
15477      any given DW_TAG_formal_parameter DIE which will describe the "passed
15478      type" and the "passed location" for the given formal parameter in addition
15479      to the attributes we now generate to indicate the "declared type" and the
15480      "active location" for each parameter.  This additional set of attributes
15481      could be used by debuggers for stack backtraces. Separately, note that
15482      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15483      This happens (for example) for inlined-instances of inline function formal
15484      parameters which are never referenced.  This really shouldn't be
15485      happening.  All PARM_DECL nodes should get valid non-NULL
15486      DECL_INCOMING_RTL values.  FIXME.  */
15487
15488   /* Use DECL_RTL as the "location" unless we find something better.  */
15489   rtl = DECL_RTL_IF_SET (decl);
15490
15491   /* When generating abstract instances, ignore everything except
15492      constants, symbols living in memory, and symbols living in
15493      fixed registers.  */
15494   if (! reload_completed)
15495     {
15496       if (rtl
15497           && (CONSTANT_P (rtl)
15498               || (MEM_P (rtl)
15499                   && CONSTANT_P (XEXP (rtl, 0)))
15500               || (REG_P (rtl)
15501                   && TREE_CODE (decl) == VAR_DECL
15502                   && TREE_STATIC (decl))))
15503         {
15504           rtl = targetm.delegitimize_address (rtl);
15505           return rtl;
15506         }
15507       rtl = NULL_RTX;
15508     }
15509   else if (TREE_CODE (decl) == PARM_DECL)
15510     {
15511       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
15512         {
15513           tree declared_type = TREE_TYPE (decl);
15514           tree passed_type = DECL_ARG_TYPE (decl);
15515           enum machine_mode dmode = TYPE_MODE (declared_type);
15516           enum machine_mode pmode = TYPE_MODE (passed_type);
15517
15518           /* This decl represents a formal parameter which was optimized out.
15519              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15520              all cases where (rtl == NULL_RTX) just below.  */
15521           if (dmode == pmode)
15522             rtl = DECL_INCOMING_RTL (decl);
15523           else if (SCALAR_INT_MODE_P (dmode)
15524                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15525                    && DECL_INCOMING_RTL (decl))
15526             {
15527               rtx inc = DECL_INCOMING_RTL (decl);
15528               if (REG_P (inc))
15529                 rtl = inc;
15530               else if (MEM_P (inc))
15531                 {
15532                   if (BYTES_BIG_ENDIAN)
15533                     rtl = adjust_address_nv (inc, dmode,
15534                                              GET_MODE_SIZE (pmode)
15535                                              - GET_MODE_SIZE (dmode));
15536                   else
15537                     rtl = inc;
15538                 }
15539             }
15540         }
15541
15542       /* If the parm was passed in registers, but lives on the stack, then
15543          make a big endian correction if the mode of the type of the
15544          parameter is not the same as the mode of the rtl.  */
15545       /* ??? This is the same series of checks that are made in dbxout.c before
15546          we reach the big endian correction code there.  It isn't clear if all
15547          of these checks are necessary here, but keeping them all is the safe
15548          thing to do.  */
15549       else if (MEM_P (rtl)
15550                && XEXP (rtl, 0) != const0_rtx
15551                && ! CONSTANT_P (XEXP (rtl, 0))
15552                /* Not passed in memory.  */
15553                && !MEM_P (DECL_INCOMING_RTL (decl))
15554                /* Not passed by invisible reference.  */
15555                && (!REG_P (XEXP (rtl, 0))
15556                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15557                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15558 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
15559                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15560 #endif
15561                      )
15562                /* Big endian correction check.  */
15563                && BYTES_BIG_ENDIAN
15564                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15565                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15566                    < UNITS_PER_WORD))
15567         {
15568           int offset = (UNITS_PER_WORD
15569                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15570
15571           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15572                              plus_constant (XEXP (rtl, 0), offset));
15573         }
15574     }
15575   else if (TREE_CODE (decl) == VAR_DECL
15576            && rtl
15577            && MEM_P (rtl)
15578            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15579            && BYTES_BIG_ENDIAN)
15580     {
15581       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15582       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15583
15584       /* If a variable is declared "register" yet is smaller than
15585          a register, then if we store the variable to memory, it
15586          looks like we're storing a register-sized value, when in
15587          fact we are not.  We need to adjust the offset of the
15588          storage location to reflect the actual value's bytes,
15589          else gdb will not be able to display it.  */
15590       if (rsize > dsize)
15591         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15592                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15593     }
15594
15595   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15596      and will have been substituted directly into all expressions that use it.
15597      C does not have such a concept, but C++ and other languages do.  */
15598   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15599     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15600
15601   if (rtl)
15602     rtl = targetm.delegitimize_address (rtl);
15603
15604   /* If we don't look past the constant pool, we risk emitting a
15605      reference to a constant pool entry that isn't referenced from
15606      code, and thus is not emitted.  */
15607   if (rtl)
15608     rtl = avoid_constant_pool_reference (rtl);
15609
15610   return rtl;
15611 }
15612
15613 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15614    returned.  If so, the decl for the COMMON block is returned, and the
15615    value is the offset into the common block for the symbol.  */
15616
15617 static tree
15618 fortran_common (tree decl, HOST_WIDE_INT *value)
15619 {
15620   tree val_expr, cvar;
15621   enum machine_mode mode;
15622   HOST_WIDE_INT bitsize, bitpos;
15623   tree offset;
15624   int volatilep = 0, unsignedp = 0;
15625
15626   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15627      it does not have a value (the offset into the common area), or if it
15628      is thread local (as opposed to global) then it isn't common, and shouldn't
15629      be handled as such.  */
15630   if (TREE_CODE (decl) != VAR_DECL
15631       || !TREE_STATIC (decl)
15632       || !DECL_HAS_VALUE_EXPR_P (decl)
15633       || !is_fortran ())
15634     return NULL_TREE;
15635
15636   val_expr = DECL_VALUE_EXPR (decl);
15637   if (TREE_CODE (val_expr) != COMPONENT_REF)
15638     return NULL_TREE;
15639
15640   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15641                               &mode, &unsignedp, &volatilep, true);
15642
15643   if (cvar == NULL_TREE
15644       || TREE_CODE (cvar) != VAR_DECL
15645       || DECL_ARTIFICIAL (cvar)
15646       || !TREE_PUBLIC (cvar))
15647     return NULL_TREE;
15648
15649   *value = 0;
15650   if (offset != NULL)
15651     {
15652       if (!host_integerp (offset, 0))
15653         return NULL_TREE;
15654       *value = tree_low_cst (offset, 0);
15655     }
15656   if (bitpos != 0)
15657     *value += bitpos / BITS_PER_UNIT;
15658
15659   return cvar;
15660 }
15661
15662 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15663    data attribute for a variable or a parameter.  We generate the
15664    DW_AT_const_value attribute only in those cases where the given variable
15665    or parameter does not have a true "location" either in memory or in a
15666    register.  This can happen (for example) when a constant is passed as an
15667    actual argument in a call to an inline function.  (It's possible that
15668    these things can crop up in other ways also.)  Note that one type of
15669    constant value which can be passed into an inlined function is a constant
15670    pointer.  This can happen for example if an actual argument in an inlined
15671    function call evaluates to a compile-time constant address.  */
15672
15673 static bool
15674 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
15675                                        enum dwarf_attribute attr)
15676 {
15677   rtx rtl;
15678   dw_loc_list_ref list;
15679   var_loc_list *loc_list;
15680
15681   if (TREE_CODE (decl) == ERROR_MARK)
15682     return false;
15683
15684   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15685               || TREE_CODE (decl) == RESULT_DECL);
15686
15687   /* Try to get some constant RTL for this decl, and use that as the value of
15688      the location.  */
15689
15690   rtl = rtl_for_decl_location (decl);
15691   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15692       && add_const_value_attribute (die, rtl))
15693     return true;
15694
15695   /* See if we have single element location list that is equivalent to
15696      a constant value.  That way we are better to use add_const_value_attribute
15697      rather than expanding constant value equivalent.  */
15698   loc_list = lookup_decl_loc (decl);
15699   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
15700     {
15701       enum var_init_status status;
15702       struct var_loc_node *node;
15703
15704       node = loc_list->first;
15705       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
15706       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
15707       if (GET_CODE (rtl) == VAR_LOCATION
15708           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
15709         rtl = XEXP (XEXP (rtl, 1), 0);
15710       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15711           && add_const_value_attribute (die, rtl))
15712          return true;
15713     }
15714   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15715   if (list)
15716     {
15717       add_AT_location_description (die, attr, list);
15718       return true;
15719     }
15720   /* None of that worked, so it must not really have a location;
15721      try adding a constant value attribute from the DECL_INITIAL.  */
15722   return tree_add_const_value_attribute_for_decl (die, decl);
15723 }
15724
15725 /* Add VARIABLE and DIE into deferred locations list.  */
15726
15727 static void
15728 defer_location (tree variable, dw_die_ref die)
15729 {
15730   deferred_locations entry;
15731   entry.variable = variable;
15732   entry.die = die;
15733   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15734 }
15735
15736 /* Helper function for tree_add_const_value_attribute.  Natively encode
15737    initializer INIT into an array.  Return true if successful.  */
15738
15739 static bool
15740 native_encode_initializer (tree init, unsigned char *array, int size)
15741 {
15742   tree type;
15743
15744   if (init == NULL_TREE)
15745     return false;
15746
15747   STRIP_NOPS (init);
15748   switch (TREE_CODE (init))
15749     {
15750     case STRING_CST:
15751       type = TREE_TYPE (init);
15752       if (TREE_CODE (type) == ARRAY_TYPE)
15753         {
15754           tree enttype = TREE_TYPE (type);
15755           enum machine_mode mode = TYPE_MODE (enttype);
15756
15757           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15758             return false;
15759           if (int_size_in_bytes (type) != size)
15760             return false;
15761           if (size > TREE_STRING_LENGTH (init))
15762             {
15763               memcpy (array, TREE_STRING_POINTER (init),
15764                       TREE_STRING_LENGTH (init));
15765               memset (array + TREE_STRING_LENGTH (init),
15766                       '\0', size - TREE_STRING_LENGTH (init));
15767             }
15768           else
15769             memcpy (array, TREE_STRING_POINTER (init), size);
15770           return true;
15771         }
15772       return false;
15773     case CONSTRUCTOR:
15774       type = TREE_TYPE (init);
15775       if (int_size_in_bytes (type) != size)
15776         return false;
15777       if (TREE_CODE (type) == ARRAY_TYPE)
15778         {
15779           HOST_WIDE_INT min_index;
15780           unsigned HOST_WIDE_INT cnt;
15781           int curpos = 0, fieldsize;
15782           constructor_elt *ce;
15783
15784           if (TYPE_DOMAIN (type) == NULL_TREE
15785               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15786             return false;
15787
15788           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15789           if (fieldsize <= 0)
15790             return false;
15791
15792           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15793           memset (array, '\0', size);
15794           for (cnt = 0;
15795                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15796                cnt++)
15797             {
15798               tree val = ce->value;
15799               tree index = ce->index;
15800               int pos = curpos;
15801               if (index && TREE_CODE (index) == RANGE_EXPR)
15802                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15803                       * fieldsize;
15804               else if (index)
15805                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15806
15807               if (val)
15808                 {
15809                   STRIP_NOPS (val);
15810                   if (!native_encode_initializer (val, array + pos, fieldsize))
15811                     return false;
15812                 }
15813               curpos = pos + fieldsize;
15814               if (index && TREE_CODE (index) == RANGE_EXPR)
15815                 {
15816                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15817                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15818                   while (count > 0)
15819                     {
15820                       if (val)
15821                         memcpy (array + curpos, array + pos, fieldsize);
15822                       curpos += fieldsize;
15823                     }
15824                 }
15825               gcc_assert (curpos <= size);
15826             }
15827           return true;
15828         }
15829       else if (TREE_CODE (type) == RECORD_TYPE
15830                || TREE_CODE (type) == UNION_TYPE)
15831         {
15832           tree field = NULL_TREE;
15833           unsigned HOST_WIDE_INT cnt;
15834           constructor_elt *ce;
15835
15836           if (int_size_in_bytes (type) != size)
15837             return false;
15838
15839           if (TREE_CODE (type) == RECORD_TYPE)
15840             field = TYPE_FIELDS (type);
15841
15842           for (cnt = 0;
15843                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15844                cnt++, field = field ? TREE_CHAIN (field) : 0)
15845             {
15846               tree val = ce->value;
15847               int pos, fieldsize;
15848
15849               if (ce->index != 0)
15850                 field = ce->index;
15851
15852               if (val)
15853                 STRIP_NOPS (val);
15854
15855               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15856                 return false;
15857
15858               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15859                   && TYPE_DOMAIN (TREE_TYPE (field))
15860                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15861                 return false;
15862               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15863                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15864                 return false;
15865               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15866               pos = int_byte_position (field);
15867               gcc_assert (pos + fieldsize <= size);
15868               if (val
15869                   && !native_encode_initializer (val, array + pos, fieldsize))
15870                 return false;
15871             }
15872           return true;
15873         }
15874       return false;
15875     case VIEW_CONVERT_EXPR:
15876     case NON_LVALUE_EXPR:
15877       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15878     default:
15879       return native_encode_expr (init, array, size) == size;
15880     }
15881 }
15882
15883 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15884    attribute is the const value T.  */
15885
15886 static bool
15887 tree_add_const_value_attribute (dw_die_ref die, tree t)
15888 {
15889   tree init;
15890   tree type = TREE_TYPE (t);
15891   rtx rtl;
15892
15893   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15894     return false;
15895
15896   init = t;
15897   gcc_assert (!DECL_P (init));
15898
15899   rtl = rtl_for_decl_init (init, type);
15900   if (rtl)
15901     return add_const_value_attribute (die, rtl);
15902   /* If the host and target are sane, try harder.  */
15903   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15904            && initializer_constant_valid_p (init, type))
15905     {
15906       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15907       if (size > 0 && (int) size == size)
15908         {
15909           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
15910
15911           if (native_encode_initializer (init, array, size))
15912             {
15913               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15914               return true;
15915             }
15916         }
15917     }
15918   return false;
15919 }
15920
15921 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15922    attribute is the const value of T, where T is an integral constant
15923    variable with static storage duration
15924    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15925
15926 static bool
15927 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15928 {
15929
15930   if (!decl
15931       || (TREE_CODE (decl) != VAR_DECL
15932           && TREE_CODE (decl) != CONST_DECL))
15933     return false;
15934
15935     if (TREE_READONLY (decl)
15936         && ! TREE_THIS_VOLATILE (decl)
15937         && DECL_INITIAL (decl))
15938       /* OK */;
15939     else
15940       return false;
15941
15942   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15943   if (get_AT (var_die, DW_AT_const_value))
15944     return false;
15945
15946   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15947 }
15948
15949 /* Convert the CFI instructions for the current function into a
15950    location list.  This is used for DW_AT_frame_base when we targeting
15951    a dwarf2 consumer that does not support the dwarf3
15952    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15953    expressions.  */
15954
15955 static dw_loc_list_ref
15956 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15957 {
15958   dw_fde_ref fde;
15959   dw_loc_list_ref list, *list_tail;
15960   dw_cfi_ref cfi;
15961   dw_cfa_location last_cfa, next_cfa;
15962   const char *start_label, *last_label, *section;
15963   dw_cfa_location remember;
15964
15965   fde = current_fde ();
15966   gcc_assert (fde != NULL);
15967
15968   section = secname_for_decl (current_function_decl);
15969   list_tail = &list;
15970   list = NULL;
15971
15972   memset (&next_cfa, 0, sizeof (next_cfa));
15973   next_cfa.reg = INVALID_REGNUM;
15974   remember = next_cfa;
15975
15976   start_label = fde->dw_fde_begin;
15977
15978   /* ??? Bald assumption that the CIE opcode list does not contain
15979      advance opcodes.  */
15980   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
15981     lookup_cfa_1 (cfi, &next_cfa, &remember);
15982
15983   last_cfa = next_cfa;
15984   last_label = start_label;
15985
15986   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
15987     switch (cfi->dw_cfi_opc)
15988       {
15989       case DW_CFA_set_loc:
15990       case DW_CFA_advance_loc1:
15991       case DW_CFA_advance_loc2:
15992       case DW_CFA_advance_loc4:
15993         if (!cfa_equal_p (&last_cfa, &next_cfa))
15994           {
15995             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15996                                        start_label, last_label, section,
15997                                        list == NULL);
15998
15999             list_tail = &(*list_tail)->dw_loc_next;
16000             last_cfa = next_cfa;
16001             start_label = last_label;
16002           }
16003         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16004         break;
16005
16006       case DW_CFA_advance_loc:
16007         /* The encoding is complex enough that we should never emit this.  */
16008         gcc_unreachable ();
16009
16010       default:
16011         lookup_cfa_1 (cfi, &next_cfa, &remember);
16012         break;
16013       }
16014
16015   if (!cfa_equal_p (&last_cfa, &next_cfa))
16016     {
16017       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16018                                  start_label, last_label, section,
16019                                  list == NULL);
16020       list_tail = &(*list_tail)->dw_loc_next;
16021       start_label = last_label;
16022     }
16023   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16024                              start_label, fde->dw_fde_end, section,
16025                              list == NULL);
16026
16027   return list;
16028 }
16029
16030 /* Compute a displacement from the "steady-state frame pointer" to the
16031    frame base (often the same as the CFA), and store it in
16032    frame_pointer_fb_offset.  OFFSET is added to the displacement
16033    before the latter is negated.  */
16034
16035 static void
16036 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16037 {
16038   rtx reg, elim;
16039
16040 #ifdef FRAME_POINTER_CFA_OFFSET
16041   reg = frame_pointer_rtx;
16042   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16043 #else
16044   reg = arg_pointer_rtx;
16045   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16046 #endif
16047
16048   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16049   if (GET_CODE (elim) == PLUS)
16050     {
16051       offset += INTVAL (XEXP (elim, 1));
16052       elim = XEXP (elim, 0);
16053     }
16054
16055   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16056                && (elim == hard_frame_pointer_rtx
16057                    || elim == stack_pointer_rtx))
16058               || elim == (frame_pointer_needed
16059                           ? hard_frame_pointer_rtx
16060                           : stack_pointer_rtx));
16061
16062   frame_pointer_fb_offset = -offset;
16063 }
16064
16065 /* Generate a DW_AT_name attribute given some string value to be included as
16066    the value of the attribute.  */
16067
16068 static void
16069 add_name_attribute (dw_die_ref die, const char *name_string)
16070 {
16071   if (name_string != NULL && *name_string != 0)
16072     {
16073       if (demangle_name_func)
16074         name_string = (*demangle_name_func) (name_string);
16075
16076       add_AT_string (die, DW_AT_name, name_string);
16077     }
16078 }
16079
16080 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16081
16082 static void
16083 add_comp_dir_attribute (dw_die_ref die)
16084 {
16085   const char *wd = get_src_pwd ();
16086   char *wd1;
16087
16088   if (wd == NULL)
16089     return;
16090
16091   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16092     {
16093       int wdlen;
16094
16095       wdlen = strlen (wd);
16096       wd1 = GGC_NEWVEC (char, wdlen + 2);
16097       strcpy (wd1, wd);
16098       wd1 [wdlen] = DIR_SEPARATOR;
16099       wd1 [wdlen + 1] = 0;
16100       wd = wd1;
16101     }
16102
16103     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16104 }
16105
16106 /* Given a tree node describing an array bound (either lower or upper) output
16107    a representation for that bound.  */
16108
16109 static void
16110 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16111 {
16112   switch (TREE_CODE (bound))
16113     {
16114     case ERROR_MARK:
16115       return;
16116
16117     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
16118     case INTEGER_CST:
16119       {
16120         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16121
16122         /* Use the default if possible.  */
16123         if (bound_attr == DW_AT_lower_bound
16124             && (((is_c_family () || is_java ()) && integer_zerop (bound))
16125                 || (is_fortran () && integer_onep (bound))))
16126           ;
16127
16128         /* Otherwise represent the bound as an unsigned value with the
16129            precision of its type.  The precision and signedness of the
16130            type will be necessary to re-interpret it unambiguously.  */
16131         else if (prec < HOST_BITS_PER_WIDE_INT)
16132           {
16133             unsigned HOST_WIDE_INT mask
16134               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16135             add_AT_unsigned (subrange_die, bound_attr,
16136                              TREE_INT_CST_LOW (bound) & mask);
16137           }
16138         else if (prec == HOST_BITS_PER_WIDE_INT
16139                  || TREE_INT_CST_HIGH (bound) == 0)
16140           add_AT_unsigned (subrange_die, bound_attr,
16141                            TREE_INT_CST_LOW (bound));
16142         else
16143           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16144                          TREE_INT_CST_LOW (bound));
16145       }
16146       break;
16147
16148     CASE_CONVERT:
16149     case VIEW_CONVERT_EXPR:
16150       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16151       break;
16152
16153     case SAVE_EXPR:
16154       break;
16155
16156     case VAR_DECL:
16157     case PARM_DECL:
16158     case RESULT_DECL:
16159       {
16160         dw_die_ref decl_die = lookup_decl_die (bound);
16161         dw_loc_list_ref loc;
16162
16163         /* ??? Can this happen, or should the variable have been bound
16164            first?  Probably it can, since I imagine that we try to create
16165            the types of parameters in the order in which they exist in
16166            the list, and won't have created a forward reference to a
16167            later parameter.  */
16168         if (decl_die != NULL)
16169           add_AT_die_ref (subrange_die, bound_attr, decl_die);
16170         else
16171           {
16172             loc = loc_list_from_tree (bound, 0);
16173             add_AT_location_description (subrange_die, bound_attr, loc);
16174           }
16175         break;
16176       }
16177
16178     default:
16179       {
16180         /* Otherwise try to create a stack operation procedure to
16181            evaluate the value of the array bound.  */
16182
16183         dw_die_ref ctx, decl_die;
16184         dw_loc_list_ref list;
16185
16186         list = loc_list_from_tree (bound, 2);
16187         if (list == NULL)
16188           break;
16189
16190         if (current_function_decl == 0)
16191           ctx = comp_unit_die;
16192         else
16193           ctx = lookup_decl_die (current_function_decl);
16194
16195         decl_die = new_die (DW_TAG_variable, ctx, bound);
16196         add_AT_flag (decl_die, DW_AT_artificial, 1);
16197         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16198         if (list->dw_loc_next)
16199           add_AT_loc_list (decl_die, DW_AT_location, list);
16200         else
16201           add_AT_loc (decl_die, DW_AT_location, list->expr);
16202
16203         add_AT_die_ref (subrange_die, bound_attr, decl_die);
16204         break;
16205       }
16206     }
16207 }
16208
16209 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16210    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16211    Note that the block of subscript information for an array type also
16212    includes information about the element type of the given array type.  */
16213
16214 static void
16215 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16216 {
16217   unsigned dimension_number;
16218   tree lower, upper;
16219   dw_die_ref subrange_die;
16220
16221   for (dimension_number = 0;
16222        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16223        type = TREE_TYPE (type), dimension_number++)
16224     {
16225       tree domain = TYPE_DOMAIN (type);
16226
16227       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16228         break;
16229
16230       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16231          and (in GNU C only) variable bounds.  Handle all three forms
16232          here.  */
16233       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16234       if (domain)
16235         {
16236           /* We have an array type with specified bounds.  */
16237           lower = TYPE_MIN_VALUE (domain);
16238           upper = TYPE_MAX_VALUE (domain);
16239
16240           /* Define the index type.  */
16241           if (TREE_TYPE (domain))
16242             {
16243               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16244                  TREE_TYPE field.  We can't emit debug info for this
16245                  because it is an unnamed integral type.  */
16246               if (TREE_CODE (domain) == INTEGER_TYPE
16247                   && TYPE_NAME (domain) == NULL_TREE
16248                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16249                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16250                 ;
16251               else
16252                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16253                                     type_die);
16254             }
16255
16256           /* ??? If upper is NULL, the array has unspecified length,
16257              but it does have a lower bound.  This happens with Fortran
16258                dimension arr(N:*)
16259              Since the debugger is definitely going to need to know N
16260              to produce useful results, go ahead and output the lower
16261              bound solo, and hope the debugger can cope.  */
16262
16263           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16264           if (upper)
16265             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16266         }
16267
16268       /* Otherwise we have an array type with an unspecified length.  The
16269          DWARF-2 spec does not say how to handle this; let's just leave out the
16270          bounds.  */
16271     }
16272 }
16273
16274 static void
16275 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16276 {
16277   unsigned size;
16278
16279   switch (TREE_CODE (tree_node))
16280     {
16281     case ERROR_MARK:
16282       size = 0;
16283       break;
16284     case ENUMERAL_TYPE:
16285     case RECORD_TYPE:
16286     case UNION_TYPE:
16287     case QUAL_UNION_TYPE:
16288       size = int_size_in_bytes (tree_node);
16289       break;
16290     case FIELD_DECL:
16291       /* For a data member of a struct or union, the DW_AT_byte_size is
16292          generally given as the number of bytes normally allocated for an
16293          object of the *declared* type of the member itself.  This is true
16294          even for bit-fields.  */
16295       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16296       break;
16297     default:
16298       gcc_unreachable ();
16299     }
16300
16301   /* Note that `size' might be -1 when we get to this point.  If it is, that
16302      indicates that the byte size of the entity in question is variable.  We
16303      have no good way of expressing this fact in Dwarf at the present time,
16304      so just let the -1 pass on through.  */
16305   add_AT_unsigned (die, DW_AT_byte_size, size);
16306 }
16307
16308 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16309    which specifies the distance in bits from the highest order bit of the
16310    "containing object" for the bit-field to the highest order bit of the
16311    bit-field itself.
16312
16313    For any given bit-field, the "containing object" is a hypothetical object
16314    (of some integral or enum type) within which the given bit-field lives.  The
16315    type of this hypothetical "containing object" is always the same as the
16316    declared type of the individual bit-field itself.  The determination of the
16317    exact location of the "containing object" for a bit-field is rather
16318    complicated.  It's handled by the `field_byte_offset' function (above).
16319
16320    Note that it is the size (in bytes) of the hypothetical "containing object"
16321    which will be given in the DW_AT_byte_size attribute for this bit-field.
16322    (See `byte_size_attribute' above).  */
16323
16324 static inline void
16325 add_bit_offset_attribute (dw_die_ref die, tree decl)
16326 {
16327   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16328   tree type = DECL_BIT_FIELD_TYPE (decl);
16329   HOST_WIDE_INT bitpos_int;
16330   HOST_WIDE_INT highest_order_object_bit_offset;
16331   HOST_WIDE_INT highest_order_field_bit_offset;
16332   HOST_WIDE_INT unsigned bit_offset;
16333
16334   /* Must be a field and a bit field.  */
16335   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16336
16337   /* We can't yet handle bit-fields whose offsets are variable, so if we
16338      encounter such things, just return without generating any attribute
16339      whatsoever.  Likewise for variable or too large size.  */
16340   if (! host_integerp (bit_position (decl), 0)
16341       || ! host_integerp (DECL_SIZE (decl), 1))
16342     return;
16343
16344   bitpos_int = int_bit_position (decl);
16345
16346   /* Note that the bit offset is always the distance (in bits) from the
16347      highest-order bit of the "containing object" to the highest-order bit of
16348      the bit-field itself.  Since the "high-order end" of any object or field
16349      is different on big-endian and little-endian machines, the computation
16350      below must take account of these differences.  */
16351   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16352   highest_order_field_bit_offset = bitpos_int;
16353
16354   if (! BYTES_BIG_ENDIAN)
16355     {
16356       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16357       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16358     }
16359
16360   bit_offset
16361     = (! BYTES_BIG_ENDIAN
16362        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16363        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16364
16365   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
16366 }
16367
16368 /* For a FIELD_DECL node which represents a bit field, output an attribute
16369    which specifies the length in bits of the given field.  */
16370
16371 static inline void
16372 add_bit_size_attribute (dw_die_ref die, tree decl)
16373 {
16374   /* Must be a field and a bit field.  */
16375   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16376               && DECL_BIT_FIELD_TYPE (decl));
16377
16378   if (host_integerp (DECL_SIZE (decl), 1))
16379     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16380 }
16381
16382 /* If the compiled language is ANSI C, then add a 'prototyped'
16383    attribute, if arg types are given for the parameters of a function.  */
16384
16385 static inline void
16386 add_prototyped_attribute (dw_die_ref die, tree func_type)
16387 {
16388   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
16389       && TYPE_ARG_TYPES (func_type) != NULL)
16390     add_AT_flag (die, DW_AT_prototyped, 1);
16391 }
16392
16393 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16394    by looking in either the type declaration or object declaration
16395    equate table.  */
16396
16397 static inline dw_die_ref
16398 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16399 {
16400   dw_die_ref origin_die = NULL;
16401
16402   if (TREE_CODE (origin) != FUNCTION_DECL)
16403     {
16404       /* We may have gotten separated from the block for the inlined
16405          function, if we're in an exception handler or some such; make
16406          sure that the abstract function has been written out.
16407
16408          Doing this for nested functions is wrong, however; functions are
16409          distinct units, and our context might not even be inline.  */
16410       tree fn = origin;
16411
16412       if (TYPE_P (fn))
16413         fn = TYPE_STUB_DECL (fn);
16414
16415       fn = decl_function_context (fn);
16416       if (fn)
16417         dwarf2out_abstract_function (fn);
16418     }
16419
16420   if (DECL_P (origin))
16421     origin_die = lookup_decl_die (origin);
16422   else if (TYPE_P (origin))
16423     origin_die = lookup_type_die (origin);
16424
16425   /* XXX: Functions that are never lowered don't always have correct block
16426      trees (in the case of java, they simply have no block tree, in some other
16427      languages).  For these functions, there is nothing we can really do to
16428      output correct debug info for inlined functions in all cases.  Rather
16429      than die, we'll just produce deficient debug info now, in that we will
16430      have variables without a proper abstract origin.  In the future, when all
16431      functions are lowered, we should re-add a gcc_assert (origin_die)
16432      here.  */
16433
16434   if (origin_die)
16435     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16436   return origin_die;
16437 }
16438
16439 /* We do not currently support the pure_virtual attribute.  */
16440
16441 static inline void
16442 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16443 {
16444   if (DECL_VINDEX (func_decl))
16445     {
16446       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16447
16448       if (host_integerp (DECL_VINDEX (func_decl), 0))
16449         add_AT_loc (die, DW_AT_vtable_elem_location,
16450                     new_loc_descr (DW_OP_constu,
16451                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16452                                    0));
16453
16454       /* GNU extension: Record what type this method came from originally.  */
16455       if (debug_info_level > DINFO_LEVEL_TERSE)
16456         add_AT_die_ref (die, DW_AT_containing_type,
16457                         lookup_type_die (DECL_CONTEXT (func_decl)));
16458     }
16459 }
16460 \f
16461 /* Add source coordinate attributes for the given decl.  */
16462
16463 static void
16464 add_src_coords_attributes (dw_die_ref die, tree decl)
16465 {
16466   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16467
16468   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16469   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16470 }
16471
16472 /* Add a DW_AT_name attribute and source coordinate attribute for the
16473    given decl, but only if it actually has a name.  */
16474
16475 static void
16476 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16477 {
16478   tree decl_name;
16479
16480   decl_name = DECL_NAME (decl);
16481   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16482     {
16483       const char *name = dwarf2_name (decl, 0);
16484       if (name)
16485         add_name_attribute (die, name);
16486       if (! DECL_ARTIFICIAL (decl))
16487         add_src_coords_attributes (die, decl);
16488
16489       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16490           && TREE_PUBLIC (decl)
16491           && !DECL_ABSTRACT (decl)
16492           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16493           && !is_fortran ())
16494         {
16495           /* Defer until we have an assembler name set.  */
16496           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16497             {
16498               limbo_die_node *asm_name;
16499
16500               asm_name = GGC_CNEW (limbo_die_node);
16501               asm_name->die = die;
16502               asm_name->created_for = decl;
16503               asm_name->next = deferred_asm_name;
16504               deferred_asm_name = asm_name;
16505             }
16506           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16507             add_AT_string (die, DW_AT_MIPS_linkage_name,
16508                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16509         }
16510     }
16511
16512 #ifdef VMS_DEBUGGING_INFO
16513   /* Get the function's name, as described by its RTL.  This may be different
16514      from the DECL_NAME name used in the source file.  */
16515   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16516     {
16517       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16518                    XEXP (DECL_RTL (decl), 0));
16519       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16520     }
16521 #endif
16522 }
16523
16524 /* Push a new declaration scope.  */
16525
16526 static void
16527 push_decl_scope (tree scope)
16528 {
16529   VEC_safe_push (tree, gc, decl_scope_table, scope);
16530 }
16531
16532 /* Pop a declaration scope.  */
16533
16534 static inline void
16535 pop_decl_scope (void)
16536 {
16537   VEC_pop (tree, decl_scope_table);
16538 }
16539
16540 /* Return the DIE for the scope that immediately contains this type.
16541    Non-named types get global scope.  Named types nested in other
16542    types get their containing scope if it's open, or global scope
16543    otherwise.  All other types (i.e. function-local named types) get
16544    the current active scope.  */
16545
16546 static dw_die_ref
16547 scope_die_for (tree t, dw_die_ref context_die)
16548 {
16549   dw_die_ref scope_die = NULL;
16550   tree containing_scope;
16551   int i;
16552
16553   /* Non-types always go in the current scope.  */
16554   gcc_assert (TYPE_P (t));
16555
16556   containing_scope = TYPE_CONTEXT (t);
16557
16558   /* Use the containing namespace if it was passed in (for a declaration).  */
16559   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16560     {
16561       if (context_die == lookup_decl_die (containing_scope))
16562         /* OK */;
16563       else
16564         containing_scope = NULL_TREE;
16565     }
16566
16567   /* Ignore function type "scopes" from the C frontend.  They mean that
16568      a tagged type is local to a parmlist of a function declarator, but
16569      that isn't useful to DWARF.  */
16570   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16571     containing_scope = NULL_TREE;
16572
16573   if (containing_scope == NULL_TREE)
16574     scope_die = comp_unit_die;
16575   else if (TYPE_P (containing_scope))
16576     {
16577       /* For types, we can just look up the appropriate DIE.  But
16578          first we check to see if we're in the middle of emitting it
16579          so we know where the new DIE should go.  */
16580       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16581         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16582           break;
16583
16584       if (i < 0)
16585         {
16586           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16587                       || TREE_ASM_WRITTEN (containing_scope));
16588
16589           /* If none of the current dies are suitable, we get file scope.  */
16590           scope_die = comp_unit_die;
16591         }
16592       else
16593         scope_die = lookup_type_die (containing_scope);
16594     }
16595   else
16596     scope_die = context_die;
16597
16598   return scope_die;
16599 }
16600
16601 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16602
16603 static inline int
16604 local_scope_p (dw_die_ref context_die)
16605 {
16606   for (; context_die; context_die = context_die->die_parent)
16607     if (context_die->die_tag == DW_TAG_inlined_subroutine
16608         || context_die->die_tag == DW_TAG_subprogram)
16609       return 1;
16610
16611   return 0;
16612 }
16613
16614 /* Returns nonzero if CONTEXT_DIE is a class.  */
16615
16616 static inline int
16617 class_scope_p (dw_die_ref context_die)
16618 {
16619   return (context_die
16620           && (context_die->die_tag == DW_TAG_structure_type
16621               || context_die->die_tag == DW_TAG_class_type
16622               || context_die->die_tag == DW_TAG_interface_type
16623               || context_die->die_tag == DW_TAG_union_type));
16624 }
16625
16626 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16627    whether or not to treat a DIE in this context as a declaration.  */
16628
16629 static inline int
16630 class_or_namespace_scope_p (dw_die_ref context_die)
16631 {
16632   return (class_scope_p (context_die)
16633           || (context_die && context_die->die_tag == DW_TAG_namespace));
16634 }
16635
16636 /* Many forms of DIEs require a "type description" attribute.  This
16637    routine locates the proper "type descriptor" die for the type given
16638    by 'type', and adds a DW_AT_type attribute below the given die.  */
16639
16640 static void
16641 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16642                     int decl_volatile, dw_die_ref context_die)
16643 {
16644   enum tree_code code  = TREE_CODE (type);
16645   dw_die_ref type_die  = NULL;
16646
16647   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16648      or fixed-point type, use the inner type.  This is because we have no
16649      support for unnamed types in base_type_die.  This can happen if this is
16650      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16651   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16652       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16653     type = TREE_TYPE (type), code = TREE_CODE (type);
16654
16655   if (code == ERROR_MARK
16656       /* Handle a special case.  For functions whose return type is void, we
16657          generate *no* type attribute.  (Note that no object may have type
16658          `void', so this only applies to function return types).  */
16659       || code == VOID_TYPE)
16660     return;
16661
16662   type_die = modified_type_die (type,
16663                                 decl_const || TYPE_READONLY (type),
16664                                 decl_volatile || TYPE_VOLATILE (type),
16665                                 context_die);
16666
16667   if (type_die != NULL)
16668     add_AT_die_ref (object_die, DW_AT_type, type_die);
16669 }
16670
16671 /* Given an object die, add the calling convention attribute for the
16672    function call type.  */
16673 static void
16674 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16675 {
16676   enum dwarf_calling_convention value = DW_CC_normal;
16677
16678   value = ((enum dwarf_calling_convention)
16679            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16680
16681   /* DWARF doesn't provide a way to identify a program's source-level
16682      entry point.  DW_AT_calling_convention attributes are only meant
16683      to describe functions' calling conventions.  However, lacking a
16684      better way to signal the Fortran main program, we use this for the
16685      time being, following existing custom.  */
16686   if (is_fortran ()
16687       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16688     value = DW_CC_program;
16689
16690   /* Only add the attribute if the backend requests it, and
16691      is not DW_CC_normal.  */
16692   if (value && (value != DW_CC_normal))
16693     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16694 }
16695
16696 /* Given a tree pointer to a struct, class, union, or enum type node, return
16697    a pointer to the (string) tag name for the given type, or zero if the type
16698    was declared without a tag.  */
16699
16700 static const char *
16701 type_tag (const_tree type)
16702 {
16703   const char *name = 0;
16704
16705   if (TYPE_NAME (type) != 0)
16706     {
16707       tree t = 0;
16708
16709       /* Find the IDENTIFIER_NODE for the type name.  */
16710       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
16711         t = TYPE_NAME (type);
16712
16713       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16714          a TYPE_DECL node, regardless of whether or not a `typedef' was
16715          involved.  */
16716       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16717                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16718         {
16719           /* We want to be extra verbose.  Don't call dwarf_name if
16720              DECL_NAME isn't set.  The default hook for decl_printable_name
16721              doesn't like that, and in this context it's correct to return
16722              0, instead of "<anonymous>" or the like.  */
16723           if (DECL_NAME (TYPE_NAME (type)))
16724             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16725         }
16726
16727       /* Now get the name as a string, or invent one.  */
16728       if (!name && t != 0)
16729         name = IDENTIFIER_POINTER (t);
16730     }
16731
16732   return (name == 0 || *name == '\0') ? 0 : name;
16733 }
16734
16735 /* Return the type associated with a data member, make a special check
16736    for bit field types.  */
16737
16738 static inline tree
16739 member_declared_type (const_tree member)
16740 {
16741   return (DECL_BIT_FIELD_TYPE (member)
16742           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16743 }
16744
16745 /* Get the decl's label, as described by its RTL. This may be different
16746    from the DECL_NAME name used in the source file.  */
16747
16748 #if 0
16749 static const char *
16750 decl_start_label (tree decl)
16751 {
16752   rtx x;
16753   const char *fnname;
16754
16755   x = DECL_RTL (decl);
16756   gcc_assert (MEM_P (x));
16757
16758   x = XEXP (x, 0);
16759   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16760
16761   fnname = XSTR (x, 0);
16762   return fnname;
16763 }
16764 #endif
16765 \f
16766 /* These routines generate the internal representation of the DIE's for
16767    the compilation unit.  Debugging information is collected by walking
16768    the declaration trees passed in from dwarf2out_decl().  */
16769
16770 static void
16771 gen_array_type_die (tree type, dw_die_ref context_die)
16772 {
16773   dw_die_ref scope_die = scope_die_for (type, context_die);
16774   dw_die_ref array_die;
16775
16776   /* GNU compilers represent multidimensional array types as sequences of one
16777      dimensional array types whose element types are themselves array types.
16778      We sometimes squish that down to a single array_type DIE with multiple
16779      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16780      say that we are allowed to do this kind of compression in C, because
16781      there is no difference between an array of arrays and a multidimensional
16782      array.  We don't do this for Ada to remain as close as possible to the
16783      actual representation, which is especially important against the language
16784      flexibilty wrt arrays of variable size.  */
16785
16786   bool collapse_nested_arrays = !is_ada ();
16787   tree element_type;
16788
16789   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16790      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16791   if (TYPE_STRING_FLAG (type)
16792       && TREE_CODE (type) == ARRAY_TYPE
16793       && is_fortran ()
16794       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16795     {
16796       HOST_WIDE_INT size;
16797
16798       array_die = new_die (DW_TAG_string_type, scope_die, type);
16799       add_name_attribute (array_die, type_tag (type));
16800       equate_type_number_to_die (type, array_die);
16801       size = int_size_in_bytes (type);
16802       if (size >= 0)
16803         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16804       else if (TYPE_DOMAIN (type) != NULL_TREE
16805                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16806                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16807         {
16808           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16809           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16810
16811           size = int_size_in_bytes (TREE_TYPE (szdecl));
16812           if (loc && size > 0)
16813             {
16814               add_AT_location_description (array_die, DW_AT_string_length, loc);
16815               if (size != DWARF2_ADDR_SIZE)
16816                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16817             }
16818         }
16819       return;
16820     }
16821
16822   /* ??? The SGI dwarf reader fails for array of array of enum types
16823      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16824      array type comes before the outer array type.  We thus call gen_type_die
16825      before we new_die and must prevent nested array types collapsing for this
16826      target.  */
16827
16828 #ifdef MIPS_DEBUGGING_INFO
16829   gen_type_die (TREE_TYPE (type), context_die);
16830   collapse_nested_arrays = false;
16831 #endif
16832
16833   array_die = new_die (DW_TAG_array_type, scope_die, type);
16834   add_name_attribute (array_die, type_tag (type));
16835   equate_type_number_to_die (type, array_die);
16836
16837   if (TREE_CODE (type) == VECTOR_TYPE)
16838     {
16839       /* The frontend feeds us a representation for the vector as a struct
16840          containing an array.  Pull out the array type.  */
16841       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
16842       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16843     }
16844
16845   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16846   if (is_fortran ()
16847       && TREE_CODE (type) == ARRAY_TYPE
16848       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16849       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16850     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16851
16852 #if 0
16853   /* We default the array ordering.  SDB will probably do
16854      the right things even if DW_AT_ordering is not present.  It's not even
16855      an issue until we start to get into multidimensional arrays anyway.  If
16856      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16857      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16858      and when we find out that we need to put these in, we will only do so
16859      for multidimensional arrays.  */
16860   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16861 #endif
16862
16863 #ifdef MIPS_DEBUGGING_INFO
16864   /* The SGI compilers handle arrays of unknown bound by setting
16865      AT_declaration and not emitting any subrange DIEs.  */
16866   if (! TYPE_DOMAIN (type))
16867     add_AT_flag (array_die, DW_AT_declaration, 1);
16868   else
16869 #endif
16870     add_subscript_info (array_die, type, collapse_nested_arrays);
16871
16872   /* Add representation of the type of the elements of this array type and
16873      emit the corresponding DIE if we haven't done it already.  */  
16874   element_type = TREE_TYPE (type);
16875   if (collapse_nested_arrays)
16876     while (TREE_CODE (element_type) == ARRAY_TYPE)
16877       {
16878         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16879           break;
16880         element_type = TREE_TYPE (element_type);
16881       }
16882
16883 #ifndef MIPS_DEBUGGING_INFO
16884   gen_type_die (element_type, context_die);
16885 #endif
16886
16887   add_type_attribute (array_die, element_type, 0, 0, context_die);
16888
16889   if (get_AT (array_die, DW_AT_name))
16890     add_pubtype (type, array_die);
16891 }
16892
16893 static dw_loc_descr_ref
16894 descr_info_loc (tree val, tree base_decl)
16895 {
16896   HOST_WIDE_INT size;
16897   dw_loc_descr_ref loc, loc2;
16898   enum dwarf_location_atom op;
16899
16900   if (val == base_decl)
16901     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16902
16903   switch (TREE_CODE (val))
16904     {
16905     CASE_CONVERT:
16906       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16907     case VAR_DECL:
16908       return loc_descriptor_from_tree (val, 0);
16909     case INTEGER_CST:
16910       if (host_integerp (val, 0))
16911         return int_loc_descriptor (tree_low_cst (val, 0));
16912       break;
16913     case INDIRECT_REF:
16914       size = int_size_in_bytes (TREE_TYPE (val));
16915       if (size < 0)
16916         break;
16917       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16918       if (!loc)
16919         break;
16920       if (size == DWARF2_ADDR_SIZE)
16921         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16922       else
16923         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16924       return loc;
16925     case POINTER_PLUS_EXPR:
16926     case PLUS_EXPR:
16927       if (host_integerp (TREE_OPERAND (val, 1), 1)
16928           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16929              < 16384)
16930         {
16931           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16932           if (!loc)
16933             break;
16934           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16935         }
16936       else
16937         {
16938           op = DW_OP_plus;
16939         do_binop:
16940           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16941           if (!loc)
16942             break;
16943           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16944           if (!loc2)
16945             break;
16946           add_loc_descr (&loc, loc2);
16947           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16948         }
16949       return loc;
16950     case MINUS_EXPR:
16951       op = DW_OP_minus;
16952       goto do_binop;
16953     case MULT_EXPR:
16954       op = DW_OP_mul;
16955       goto do_binop;
16956     case EQ_EXPR:
16957       op = DW_OP_eq;
16958       goto do_binop;
16959     case NE_EXPR:
16960       op = DW_OP_ne;
16961       goto do_binop;
16962     default:
16963       break;
16964     }
16965   return NULL;
16966 }
16967
16968 static void
16969 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16970                       tree val, tree base_decl)
16971 {
16972   dw_loc_descr_ref loc;
16973
16974   if (host_integerp (val, 0))
16975     {
16976       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16977       return;
16978     }
16979
16980   loc = descr_info_loc (val, base_decl);
16981   if (!loc)
16982     return;
16983
16984   add_AT_loc (die, attr, loc);
16985 }
16986
16987 /* This routine generates DIE for array with hidden descriptor, details
16988    are filled into *info by a langhook.  */
16989
16990 static void
16991 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16992                           dw_die_ref context_die)
16993 {
16994   dw_die_ref scope_die = scope_die_for (type, context_die);
16995   dw_die_ref array_die;
16996   int dim;
16997
16998   array_die = new_die (DW_TAG_array_type, scope_die, type);
16999   add_name_attribute (array_die, type_tag (type));
17000   equate_type_number_to_die (type, array_die);
17001
17002   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17003   if (is_fortran ()
17004       && info->ndimensions >= 2)
17005     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17006
17007   if (info->data_location)
17008     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17009                           info->base_decl);
17010   if (info->associated)
17011     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17012                           info->base_decl);
17013   if (info->allocated)
17014     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17015                           info->base_decl);
17016
17017   for (dim = 0; dim < info->ndimensions; dim++)
17018     {
17019       dw_die_ref subrange_die
17020         = new_die (DW_TAG_subrange_type, array_die, NULL);
17021
17022       if (info->dimen[dim].lower_bound)
17023         {
17024           /* If it is the default value, omit it.  */
17025           if ((is_c_family () || is_java ())
17026               && integer_zerop (info->dimen[dim].lower_bound))
17027             ;
17028           else if (is_fortran ()
17029                    && integer_onep (info->dimen[dim].lower_bound))
17030             ;
17031           else
17032             add_descr_info_field (subrange_die, DW_AT_lower_bound,
17033                                   info->dimen[dim].lower_bound,
17034                                   info->base_decl);
17035         }
17036       if (info->dimen[dim].upper_bound)
17037         add_descr_info_field (subrange_die, DW_AT_upper_bound,
17038                               info->dimen[dim].upper_bound,
17039                               info->base_decl);
17040       if (info->dimen[dim].stride)
17041         add_descr_info_field (subrange_die, DW_AT_byte_stride,
17042                               info->dimen[dim].stride,
17043                               info->base_decl);
17044     }
17045
17046   gen_type_die (info->element_type, context_die);
17047   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17048
17049   if (get_AT (array_die, DW_AT_name))
17050     add_pubtype (type, array_die);
17051 }
17052
17053 #if 0
17054 static void
17055 gen_entry_point_die (tree decl, dw_die_ref context_die)
17056 {
17057   tree origin = decl_ultimate_origin (decl);
17058   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17059
17060   if (origin != NULL)
17061     add_abstract_origin_attribute (decl_die, origin);
17062   else
17063     {
17064       add_name_and_src_coords_attributes (decl_die, decl);
17065       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17066                           0, 0, context_die);
17067     }
17068
17069   if (DECL_ABSTRACT (decl))
17070     equate_decl_number_to_die (decl, decl_die);
17071   else
17072     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17073 }
17074 #endif
17075
17076 /* Walk through the list of incomplete types again, trying once more to
17077    emit full debugging info for them.  */
17078
17079 static void
17080 retry_incomplete_types (void)
17081 {
17082   int i;
17083
17084   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
17085     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
17086 }
17087
17088 /* Determine what tag to use for a record type.  */
17089
17090 static enum dwarf_tag
17091 record_type_tag (tree type)
17092 {
17093   if (! lang_hooks.types.classify_record)
17094     return DW_TAG_structure_type;
17095
17096   switch (lang_hooks.types.classify_record (type))
17097     {
17098     case RECORD_IS_STRUCT:
17099       return DW_TAG_structure_type;
17100
17101     case RECORD_IS_CLASS:
17102       return DW_TAG_class_type;
17103
17104     case RECORD_IS_INTERFACE:
17105       if (dwarf_version >= 3 || !dwarf_strict)
17106         return DW_TAG_interface_type;
17107       return DW_TAG_structure_type;
17108
17109     default:
17110       gcc_unreachable ();
17111     }
17112 }
17113
17114 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17115    include all of the information about the enumeration values also. Each
17116    enumerated type name/value is listed as a child of the enumerated type
17117    DIE.  */
17118
17119 static dw_die_ref
17120 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17121 {
17122   dw_die_ref type_die = lookup_type_die (type);
17123
17124   if (type_die == NULL)
17125     {
17126       type_die = new_die (DW_TAG_enumeration_type,
17127                           scope_die_for (type, context_die), type);
17128       equate_type_number_to_die (type, type_die);
17129       add_name_attribute (type_die, type_tag (type));
17130     }
17131   else if (! TYPE_SIZE (type))
17132     return type_die;
17133   else
17134     remove_AT (type_die, DW_AT_declaration);
17135
17136   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17137      given enum type is incomplete, do not generate the DW_AT_byte_size
17138      attribute or the DW_AT_element_list attribute.  */
17139   if (TYPE_SIZE (type))
17140     {
17141       tree link;
17142
17143       TREE_ASM_WRITTEN (type) = 1;
17144       add_byte_size_attribute (type_die, type);
17145       if (TYPE_STUB_DECL (type) != NULL_TREE)
17146         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17147
17148       /* If the first reference to this type was as the return type of an
17149          inline function, then it may not have a parent.  Fix this now.  */
17150       if (type_die->die_parent == NULL)
17151         add_child_die (scope_die_for (type, context_die), type_die);
17152
17153       for (link = TYPE_VALUES (type);
17154            link != NULL; link = TREE_CHAIN (link))
17155         {
17156           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17157           tree value = TREE_VALUE (link);
17158
17159           add_name_attribute (enum_die,
17160                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17161
17162           if (TREE_CODE (value) == CONST_DECL)
17163             value = DECL_INITIAL (value);
17164
17165           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17166             /* DWARF2 does not provide a way of indicating whether or
17167                not enumeration constants are signed or unsigned.  GDB
17168                always assumes the values are signed, so we output all
17169                values as if they were signed.  That means that
17170                enumeration constants with very large unsigned values
17171                will appear to have negative values in the debugger.  */
17172             add_AT_int (enum_die, DW_AT_const_value,
17173                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17174         }
17175     }
17176   else
17177     add_AT_flag (type_die, DW_AT_declaration, 1);
17178
17179   if (get_AT (type_die, DW_AT_name))
17180     add_pubtype (type, type_die);
17181
17182   return type_die;
17183 }
17184
17185 /* Generate a DIE to represent either a real live formal parameter decl or to
17186    represent just the type of some formal parameter position in some function
17187    type.
17188
17189    Note that this routine is a bit unusual because its argument may be a
17190    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17191    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17192    node.  If it's the former then this function is being called to output a
17193    DIE to represent a formal parameter object (or some inlining thereof).  If
17194    it's the latter, then this function is only being called to output a
17195    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17196    argument type of some subprogram type.
17197    If EMIT_NAME_P is true, name and source coordinate attributes
17198    are emitted.  */
17199
17200 static dw_die_ref
17201 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17202                           dw_die_ref context_die)
17203 {
17204   tree node_or_origin = node ? node : origin;
17205   dw_die_ref parm_die
17206     = new_die (DW_TAG_formal_parameter, context_die, node);
17207
17208   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17209     {
17210     case tcc_declaration:
17211       if (!origin)
17212         origin = decl_ultimate_origin (node);
17213       if (origin != NULL)
17214         add_abstract_origin_attribute (parm_die, origin);
17215       else
17216         {
17217           tree type = TREE_TYPE (node);
17218           if (emit_name_p)
17219             add_name_and_src_coords_attributes (parm_die, node);
17220           if (decl_by_reference_p (node))
17221             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17222                                 context_die);
17223           else
17224             add_type_attribute (parm_die, type,
17225                                 TREE_READONLY (node),
17226                                 TREE_THIS_VOLATILE (node),
17227                                 context_die);
17228           if (DECL_ARTIFICIAL (node))
17229             add_AT_flag (parm_die, DW_AT_artificial, 1);
17230         }
17231
17232       if (node && node != origin)
17233         equate_decl_number_to_die (node, parm_die);
17234       if (! DECL_ABSTRACT (node_or_origin))
17235         add_location_or_const_value_attribute (parm_die, node_or_origin,
17236                                                DW_AT_location);
17237
17238       break;
17239
17240     case tcc_type:
17241       /* We were called with some kind of a ..._TYPE node.  */
17242       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17243       break;
17244
17245     default:
17246       gcc_unreachable ();
17247     }
17248
17249   return parm_die;
17250 }
17251
17252 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17253    children DW_TAG_formal_parameter DIEs representing the arguments of the
17254    parameter pack.
17255
17256    PARM_PACK must be a function parameter pack.
17257    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17258    must point to the subsequent arguments of the function PACK_ARG belongs to.
17259    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17260    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17261    following the last one for which a DIE was generated.  */
17262
17263 static dw_die_ref
17264 gen_formal_parameter_pack_die  (tree parm_pack,
17265                                 tree pack_arg,
17266                                 dw_die_ref subr_die,
17267                                 tree *next_arg)
17268 {
17269   tree arg;
17270   dw_die_ref parm_pack_die;
17271
17272   gcc_assert (parm_pack
17273               && lang_hooks.function_parameter_pack_p (parm_pack)
17274               && subr_die);
17275
17276   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17277   add_src_coords_attributes (parm_pack_die, parm_pack);
17278
17279   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
17280     {
17281       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17282                                                                  parm_pack))
17283         break;
17284       gen_formal_parameter_die (arg, NULL,
17285                                 false /* Don't emit name attribute.  */,
17286                                 parm_pack_die);
17287     }
17288   if (next_arg)
17289     *next_arg = arg;
17290   return parm_pack_die;
17291 }
17292
17293 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17294    at the end of an (ANSI prototyped) formal parameters list.  */
17295
17296 static void
17297 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17298 {
17299   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17300 }
17301
17302 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17303    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17304    parameters as specified in some function type specification (except for
17305    those which appear as part of a function *definition*).  */
17306
17307 static void
17308 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17309 {
17310   tree link;
17311   tree formal_type = NULL;
17312   tree first_parm_type;
17313   tree arg;
17314
17315   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17316     {
17317       arg = DECL_ARGUMENTS (function_or_method_type);
17318       function_or_method_type = TREE_TYPE (function_or_method_type);
17319     }
17320   else
17321     arg = NULL_TREE;
17322
17323   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17324
17325   /* Make our first pass over the list of formal parameter types and output a
17326      DW_TAG_formal_parameter DIE for each one.  */
17327   for (link = first_parm_type; link; )
17328     {
17329       dw_die_ref parm_die;
17330
17331       formal_type = TREE_VALUE (link);
17332       if (formal_type == void_type_node)
17333         break;
17334
17335       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17336       parm_die = gen_formal_parameter_die (formal_type, NULL,
17337                                            true /* Emit name attribute.  */,
17338                                            context_die);
17339       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
17340            && link == first_parm_type)
17341           || (arg && DECL_ARTIFICIAL (arg)))
17342         add_AT_flag (parm_die, DW_AT_artificial, 1);
17343
17344       link = TREE_CHAIN (link);
17345       if (arg)
17346         arg = TREE_CHAIN (arg);
17347     }
17348
17349   /* If this function type has an ellipsis, add a
17350      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17351   if (formal_type != void_type_node)
17352     gen_unspecified_parameters_die (function_or_method_type, context_die);
17353
17354   /* Make our second (and final) pass over the list of formal parameter types
17355      and output DIEs to represent those types (as necessary).  */
17356   for (link = TYPE_ARG_TYPES (function_or_method_type);
17357        link && TREE_VALUE (link);
17358        link = TREE_CHAIN (link))
17359     gen_type_die (TREE_VALUE (link), context_die);
17360 }
17361
17362 /* We want to generate the DIE for TYPE so that we can generate the
17363    die for MEMBER, which has been defined; we will need to refer back
17364    to the member declaration nested within TYPE.  If we're trying to
17365    generate minimal debug info for TYPE, processing TYPE won't do the
17366    trick; we need to attach the member declaration by hand.  */
17367
17368 static void
17369 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17370 {
17371   gen_type_die (type, context_die);
17372
17373   /* If we're trying to avoid duplicate debug info, we may not have
17374      emitted the member decl for this function.  Emit it now.  */
17375   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17376       && ! lookup_decl_die (member))
17377     {
17378       dw_die_ref type_die;
17379       gcc_assert (!decl_ultimate_origin (member));
17380
17381       push_decl_scope (type);
17382       type_die = lookup_type_die (type);
17383       if (TREE_CODE (member) == FUNCTION_DECL)
17384         gen_subprogram_die (member, type_die);
17385       else if (TREE_CODE (member) == FIELD_DECL)
17386         {
17387           /* Ignore the nameless fields that are used to skip bits but handle
17388              C++ anonymous unions and structs.  */
17389           if (DECL_NAME (member) != NULL_TREE
17390               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17391               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17392             {
17393               gen_type_die (member_declared_type (member), type_die);
17394               gen_field_die (member, type_die);
17395             }
17396         }
17397       else
17398         gen_variable_die (member, NULL_TREE, type_die);
17399
17400       pop_decl_scope ();
17401     }
17402 }
17403
17404 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17405    may later generate inlined and/or out-of-line instances of.  */
17406
17407 static void
17408 dwarf2out_abstract_function (tree decl)
17409 {
17410   dw_die_ref old_die;
17411   tree save_fn;
17412   tree context;
17413   int was_abstract = DECL_ABSTRACT (decl);
17414   htab_t old_decl_loc_table;
17415
17416   /* Make sure we have the actual abstract inline, not a clone.  */
17417   decl = DECL_ORIGIN (decl);
17418
17419   old_die = lookup_decl_die (decl);
17420   if (old_die && get_AT (old_die, DW_AT_inline))
17421     /* We've already generated the abstract instance.  */
17422     return;
17423
17424   /* We can be called while recursively when seeing block defining inlined subroutine
17425      DIE.  Be sure to not clobber the outer location table nor use it or we would
17426      get locations in abstract instantces.  */
17427   old_decl_loc_table = decl_loc_table;
17428   decl_loc_table = NULL;
17429
17430   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17431      we don't get confused by DECL_ABSTRACT.  */
17432   if (debug_info_level > DINFO_LEVEL_TERSE)
17433     {
17434       context = decl_class_context (decl);
17435       if (context)
17436         gen_type_die_for_member
17437           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
17438     }
17439
17440   /* Pretend we've just finished compiling this function.  */
17441   save_fn = current_function_decl;
17442   current_function_decl = decl;
17443   push_cfun (DECL_STRUCT_FUNCTION (decl));
17444
17445   set_decl_abstract_flags (decl, 1);
17446   dwarf2out_decl (decl);
17447   if (! was_abstract)
17448     set_decl_abstract_flags (decl, 0);
17449
17450   current_function_decl = save_fn;
17451   decl_loc_table = old_decl_loc_table;
17452   pop_cfun ();
17453 }
17454
17455 /* Helper function of premark_used_types() which gets called through
17456    htab_traverse.
17457
17458    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17459    marked as unused by prune_unused_types.  */
17460
17461 static int
17462 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17463 {
17464   tree type;
17465   dw_die_ref die;
17466
17467   type = (tree) *slot;
17468   die = lookup_type_die (type);
17469   if (die != NULL)
17470     die->die_perennial_p = 1;
17471   return 1;
17472 }
17473
17474 /* Helper function of premark_types_used_by_global_vars which gets called
17475    through htab_traverse.
17476
17477    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17478    marked as unused by prune_unused_types. The DIE of the type is marked
17479    only if the global variable using the type will actually be emitted.  */
17480
17481 static int
17482 premark_types_used_by_global_vars_helper (void **slot,
17483                                           void *data ATTRIBUTE_UNUSED)
17484 {
17485   struct types_used_by_vars_entry *entry;
17486   dw_die_ref die;
17487
17488   entry = (struct types_used_by_vars_entry *) *slot;
17489   gcc_assert (entry->type != NULL
17490               && entry->var_decl != NULL);
17491   die = lookup_type_die (entry->type);
17492   if (die)
17493     {
17494       /* Ask cgraph if the global variable really is to be emitted.
17495          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17496       struct varpool_node *node = varpool_node (entry->var_decl);
17497       if (node->needed)
17498         {
17499           die->die_perennial_p = 1;
17500           /* Keep the parent DIEs as well.  */
17501           while ((die = die->die_parent) && die->die_perennial_p == 0)
17502             die->die_perennial_p = 1;
17503         }
17504     }
17505   return 1;
17506 }
17507
17508 /* Mark all members of used_types_hash as perennial.  */
17509
17510 static void
17511 premark_used_types (void)
17512 {
17513   if (cfun && cfun->used_types_hash)
17514     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17515 }
17516
17517 /* Mark all members of types_used_by_vars_entry as perennial.  */
17518
17519 static void
17520 premark_types_used_by_global_vars (void)
17521 {
17522   if (types_used_by_vars_hash)
17523     htab_traverse (types_used_by_vars_hash,
17524                    premark_types_used_by_global_vars_helper, NULL);
17525 }
17526
17527 /* Generate a DIE to represent a declared function (either file-scope or
17528    block-local).  */
17529
17530 static void
17531 gen_subprogram_die (tree decl, dw_die_ref context_die)
17532 {
17533   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17534   tree origin = decl_ultimate_origin (decl);
17535   dw_die_ref subr_die;
17536   tree fn_arg_types;
17537   tree outer_scope;
17538   dw_die_ref old_die = lookup_decl_die (decl);
17539   int declaration = (current_function_decl != decl
17540                      || class_or_namespace_scope_p (context_die));
17541
17542   premark_used_types ();
17543
17544   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17545      started to generate the abstract instance of an inline, decided to output
17546      its containing class, and proceeded to emit the declaration of the inline
17547      from the member list for the class.  If so, DECLARATION takes priority;
17548      we'll get back to the abstract instance when done with the class.  */
17549
17550   /* The class-scope declaration DIE must be the primary DIE.  */
17551   if (origin && declaration && class_or_namespace_scope_p (context_die))
17552     {
17553       origin = NULL;
17554       gcc_assert (!old_die);
17555     }
17556
17557   /* Now that the C++ front end lazily declares artificial member fns, we
17558      might need to retrofit the declaration into its class.  */
17559   if (!declaration && !origin && !old_die
17560       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17561       && !class_or_namespace_scope_p (context_die)
17562       && debug_info_level > DINFO_LEVEL_TERSE)
17563     old_die = force_decl_die (decl);
17564
17565   if (origin != NULL)
17566     {
17567       gcc_assert (!declaration || local_scope_p (context_die));
17568
17569       /* Fixup die_parent for the abstract instance of a nested
17570          inline function.  */
17571       if (old_die && old_die->die_parent == NULL)
17572         add_child_die (context_die, old_die);
17573
17574       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17575       add_abstract_origin_attribute (subr_die, origin);
17576     }
17577   else if (old_die)
17578     {
17579       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17580       struct dwarf_file_data * file_index = lookup_filename (s.file);
17581
17582       if (!get_AT_flag (old_die, DW_AT_declaration)
17583           /* We can have a normal definition following an inline one in the
17584              case of redefinition of GNU C extern inlines.
17585              It seems reasonable to use AT_specification in this case.  */
17586           && !get_AT (old_die, DW_AT_inline))
17587         {
17588           /* Detect and ignore this case, where we are trying to output
17589              something we have already output.  */
17590           return;
17591         }
17592
17593       /* If the definition comes from the same place as the declaration,
17594          maybe use the old DIE.  We always want the DIE for this function
17595          that has the *_pc attributes to be under comp_unit_die so the
17596          debugger can find it.  We also need to do this for abstract
17597          instances of inlines, since the spec requires the out-of-line copy
17598          to have the same parent.  For local class methods, this doesn't
17599          apply; we just use the old DIE.  */
17600       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
17601           && (DECL_ARTIFICIAL (decl)
17602               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17603                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17604                       == (unsigned) s.line))))
17605         {
17606           subr_die = old_die;
17607
17608           /* Clear out the declaration attribute and the formal parameters.
17609              Do not remove all children, because it is possible that this
17610              declaration die was forced using force_decl_die(). In such
17611              cases die that forced declaration die (e.g. TAG_imported_module)
17612              is one of the children that we do not want to remove.  */
17613           remove_AT (subr_die, DW_AT_declaration);
17614           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17615         }
17616       else
17617         {
17618           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17619           add_AT_specification (subr_die, old_die);
17620           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17621             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17622           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17623             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17624         }
17625     }
17626   else
17627     {
17628       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17629
17630       if (TREE_PUBLIC (decl))
17631         add_AT_flag (subr_die, DW_AT_external, 1);
17632
17633       add_name_and_src_coords_attributes (subr_die, decl);
17634       if (debug_info_level > DINFO_LEVEL_TERSE)
17635         {
17636           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17637           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17638                               0, 0, context_die);
17639         }
17640
17641       add_pure_or_virtual_attribute (subr_die, decl);
17642       if (DECL_ARTIFICIAL (decl))
17643         add_AT_flag (subr_die, DW_AT_artificial, 1);
17644
17645       if (TREE_PROTECTED (decl))
17646         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
17647       else if (TREE_PRIVATE (decl))
17648         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
17649     }
17650
17651   if (declaration)
17652     {
17653       if (!old_die || !get_AT (old_die, DW_AT_inline))
17654         {
17655           add_AT_flag (subr_die, DW_AT_declaration, 1);
17656
17657           /* If this is an explicit function declaration then generate
17658              a DW_AT_explicit attribute.  */
17659           if (lang_hooks.decls.function_decl_explicit_p (decl)
17660               && (dwarf_version >= 3 || !dwarf_strict))
17661             add_AT_flag (subr_die, DW_AT_explicit, 1);
17662
17663           /* The first time we see a member function, it is in the context of
17664              the class to which it belongs.  We make sure of this by emitting
17665              the class first.  The next time is the definition, which is
17666              handled above.  The two may come from the same source text.
17667
17668              Note that force_decl_die() forces function declaration die. It is
17669              later reused to represent definition.  */
17670           equate_decl_number_to_die (decl, subr_die);
17671         }
17672     }
17673   else if (DECL_ABSTRACT (decl))
17674     {
17675       if (DECL_DECLARED_INLINE_P (decl))
17676         {
17677           if (cgraph_function_possibly_inlined_p (decl))
17678             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17679           else
17680             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17681         }
17682       else
17683         {
17684           if (cgraph_function_possibly_inlined_p (decl))
17685             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17686           else
17687             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17688         }
17689
17690       if (DECL_DECLARED_INLINE_P (decl)
17691           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17692         add_AT_flag (subr_die, DW_AT_artificial, 1);
17693
17694       equate_decl_number_to_die (decl, subr_die);
17695     }
17696   else if (!DECL_EXTERNAL (decl))
17697     {
17698       HOST_WIDE_INT cfa_fb_offset;
17699
17700       if (!old_die || !get_AT (old_die, DW_AT_inline))
17701         equate_decl_number_to_die (decl, subr_die);
17702
17703       if (!flag_reorder_blocks_and_partition)
17704         {
17705           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17706                                        current_function_funcdef_no);
17707           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17708           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17709                                        current_function_funcdef_no);
17710           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17711
17712           add_pubname (decl, subr_die);
17713           add_arange (decl, subr_die);
17714         }
17715       else
17716         {  /* Do nothing for now; maybe need to duplicate die, one for
17717               hot section and one for cold section, then use the hot/cold
17718               section begin/end labels to generate the aranges...  */
17719           /*
17720             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
17721             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
17722             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
17723             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
17724
17725             add_pubname (decl, subr_die);
17726             add_arange (decl, subr_die);
17727             add_arange (decl, subr_die);
17728            */
17729         }
17730
17731 #ifdef MIPS_DEBUGGING_INFO
17732       /* Add a reference to the FDE for this routine.  */
17733       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17734 #endif
17735
17736       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17737
17738       /* We define the "frame base" as the function's CFA.  This is more
17739          convenient for several reasons: (1) It's stable across the prologue
17740          and epilogue, which makes it better than just a frame pointer,
17741          (2) With dwarf3, there exists a one-byte encoding that allows us
17742          to reference the .debug_frame data by proxy, but failing that,
17743          (3) We can at least reuse the code inspection and interpretation
17744          code that determines the CFA position at various points in the
17745          function.  */
17746       if (dwarf_version >= 3)
17747         {
17748           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17749           add_AT_loc (subr_die, DW_AT_frame_base, op);
17750         }
17751       else
17752         {
17753           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17754           if (list->dw_loc_next)
17755             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17756           else
17757             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17758         }
17759
17760       /* Compute a displacement from the "steady-state frame pointer" to
17761          the CFA.  The former is what all stack slots and argument slots
17762          will reference in the rtl; the later is what we've told the
17763          debugger about.  We'll need to adjust all frame_base references
17764          by this displacement.  */
17765       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17766
17767       if (cfun->static_chain_decl)
17768         add_AT_location_description (subr_die, DW_AT_static_link,
17769                  loc_list_from_tree (cfun->static_chain_decl, 2));
17770     }
17771
17772   /* Generate child dies for template paramaters.  */
17773   if (debug_info_level > DINFO_LEVEL_TERSE)
17774     gen_generic_params_dies (decl);
17775
17776   /* Now output descriptions of the arguments for this function. This gets
17777      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17778      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17779      `...' at the end of the formal parameter list.  In order to find out if
17780      there was a trailing ellipsis or not, we must instead look at the type
17781      associated with the FUNCTION_DECL.  This will be a node of type
17782      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17783      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17784      an ellipsis at the end.  */
17785
17786   /* In the case where we are describing a mere function declaration, all we
17787      need to do here (and all we *can* do here) is to describe the *types* of
17788      its formal parameters.  */
17789   if (debug_info_level <= DINFO_LEVEL_TERSE)
17790     ;
17791   else if (declaration)
17792     gen_formal_types_die (decl, subr_die);
17793   else
17794     {
17795       /* Generate DIEs to represent all known formal parameters.  */
17796       tree parm = DECL_ARGUMENTS (decl);
17797       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17798       tree generic_decl_parm = generic_decl
17799                                 ? DECL_ARGUMENTS (generic_decl)
17800                                 : NULL;
17801
17802       /* Now we want to walk the list of parameters of the function and
17803          emit their relevant DIEs.
17804
17805          We consider the case of DECL being an instance of a generic function
17806          as well as it being a normal function.
17807
17808          If DECL is an instance of a generic function we walk the
17809          parameters of the generic function declaration _and_ the parameters of
17810          DECL itself. This is useful because we want to emit specific DIEs for
17811          function parameter packs and those are declared as part of the
17812          generic function declaration. In that particular case,
17813          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17814          That DIE has children DIEs representing the set of arguments
17815          of the pack. Note that the set of pack arguments can be empty.
17816          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17817          children DIE.
17818         
17819          Otherwise, we just consider the parameters of DECL.  */
17820       while (generic_decl_parm || parm)
17821         {
17822           if (generic_decl_parm
17823               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17824             gen_formal_parameter_pack_die (generic_decl_parm,
17825                                            parm, subr_die,
17826                                            &parm);
17827           else if (parm)
17828             {
17829               gen_decl_die (parm, NULL, subr_die);
17830               parm = TREE_CHAIN (parm);
17831             }
17832
17833           if (generic_decl_parm)
17834             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
17835         }
17836
17837       /* Decide whether we need an unspecified_parameters DIE at the end.
17838          There are 2 more cases to do this for: 1) the ansi ... declaration -
17839          this is detectable when the end of the arg list is not a
17840          void_type_node 2) an unprototyped function declaration (not a
17841          definition).  This just means that we have no info about the
17842          parameters at all.  */
17843       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
17844       if (fn_arg_types != NULL)
17845         {
17846           /* This is the prototyped case, check for....  */
17847           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
17848             gen_unspecified_parameters_die (decl, subr_die);
17849         }
17850       else if (DECL_INITIAL (decl) == NULL_TREE)
17851         gen_unspecified_parameters_die (decl, subr_die);
17852     }
17853
17854   /* Output Dwarf info for all of the stuff within the body of the function
17855      (if it has one - it may be just a declaration).  */
17856   outer_scope = DECL_INITIAL (decl);
17857
17858   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17859      a function.  This BLOCK actually represents the outermost binding contour
17860      for the function, i.e. the contour in which the function's formal
17861      parameters and labels get declared. Curiously, it appears that the front
17862      end doesn't actually put the PARM_DECL nodes for the current function onto
17863      the BLOCK_VARS list for this outer scope, but are strung off of the
17864      DECL_ARGUMENTS list for the function instead.
17865
17866      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17867      the LABEL_DECL nodes for the function however, and we output DWARF info
17868      for those in decls_for_scope.  Just within the `outer_scope' there will be
17869      a BLOCK node representing the function's outermost pair of curly braces,
17870      and any blocks used for the base and member initializers of a C++
17871      constructor function.  */
17872   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17873     {
17874       /* Emit a DW_TAG_variable DIE for a named return value.  */
17875       if (DECL_NAME (DECL_RESULT (decl)))
17876         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17877
17878       current_function_has_inlines = 0;
17879       decls_for_scope (outer_scope, subr_die, 0);
17880
17881 #if 0 && defined (MIPS_DEBUGGING_INFO)
17882       if (current_function_has_inlines)
17883         {
17884           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
17885           if (! comp_unit_has_inlines)
17886             {
17887               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
17888               comp_unit_has_inlines = 1;
17889             }
17890         }
17891 #endif
17892     }
17893   /* Add the calling convention attribute if requested.  */
17894   add_calling_convention_attribute (subr_die, decl);
17895
17896 }
17897
17898 /* Returns a hash value for X (which really is a die_struct).  */
17899
17900 static hashval_t
17901 common_block_die_table_hash (const void *x)
17902 {
17903   const_dw_die_ref d = (const_dw_die_ref) x;
17904   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17905 }
17906
17907 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17908    as decl_id and die_parent of die_struct Y.  */
17909
17910 static int
17911 common_block_die_table_eq (const void *x, const void *y)
17912 {
17913   const_dw_die_ref d = (const_dw_die_ref) x;
17914   const_dw_die_ref e = (const_dw_die_ref) y;
17915   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17916 }
17917
17918 /* Generate a DIE to represent a declared data object.
17919    Either DECL or ORIGIN must be non-null.  */
17920
17921 static void
17922 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17923 {
17924   HOST_WIDE_INT off;
17925   tree com_decl;
17926   tree decl_or_origin = decl ? decl : origin;
17927   dw_die_ref var_die;
17928   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17929   dw_die_ref origin_die;
17930   int declaration = (DECL_EXTERNAL (decl_or_origin)
17931                      /* If DECL is COMDAT and has not actually been
17932                         emitted, we cannot take its address; there
17933                         might end up being no definition anywhere in
17934                         the program.  For example, consider the C++
17935                         test case:
17936
17937                           template <class T>
17938                           struct S { static const int i = 7; };
17939
17940                           template <class T>
17941                           const int S<T>::i;
17942
17943                           int f() { return S<int>::i; }
17944
17945                         Here, S<int>::i is not DECL_EXTERNAL, but no
17946                         definition is required, so the compiler will
17947                         not emit a definition.  */
17948                      || (TREE_CODE (decl_or_origin) == VAR_DECL
17949                          && DECL_COMDAT (decl_or_origin)
17950                          && !TREE_ASM_WRITTEN (decl_or_origin))
17951                      || class_or_namespace_scope_p (context_die));
17952
17953   if (!origin)
17954     origin = decl_ultimate_origin (decl);
17955
17956   com_decl = fortran_common (decl_or_origin, &off);
17957
17958   /* Symbol in common gets emitted as a child of the common block, in the form
17959      of a data member.  */
17960   if (com_decl)
17961     {
17962       tree field;
17963       dw_die_ref com_die;
17964       dw_loc_list_ref loc;
17965       die_node com_die_arg;
17966
17967       var_die = lookup_decl_die (decl_or_origin);
17968       if (var_die)
17969         {
17970           if (get_AT (var_die, DW_AT_location) == NULL)
17971             {
17972               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17973               if (loc)
17974                 {
17975                   if (off)
17976                     {
17977                       /* Optimize the common case.  */
17978                       if (single_element_loc_list_p (loc)
17979                           && loc->expr->dw_loc_opc == DW_OP_addr
17980                           && loc->expr->dw_loc_next == NULL
17981                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17982                              == SYMBOL_REF)
17983                         loc->expr->dw_loc_oprnd1.v.val_addr
17984                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17985                         else
17986                           loc_list_plus_const (loc, off);
17987                     }
17988                   add_AT_location_description (var_die, DW_AT_location, loc);
17989                   remove_AT (var_die, DW_AT_declaration);
17990                 }
17991             }
17992           return;
17993         }
17994
17995       if (common_block_die_table == NULL)
17996         common_block_die_table
17997           = htab_create_ggc (10, common_block_die_table_hash,
17998                              common_block_die_table_eq, NULL);
17999
18000       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
18001       com_die_arg.decl_id = DECL_UID (com_decl);
18002       com_die_arg.die_parent = context_die;
18003       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18004       loc = loc_list_from_tree (com_decl, 2);
18005       if (com_die == NULL)
18006         {
18007           const char *cnam
18008             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18009           void **slot;
18010
18011           com_die = new_die (DW_TAG_common_block, context_die, decl);
18012           add_name_and_src_coords_attributes (com_die, com_decl);
18013           if (loc)
18014             {
18015               add_AT_location_description (com_die, DW_AT_location, loc);
18016               /* Avoid sharing the same loc descriptor between
18017                  DW_TAG_common_block and DW_TAG_variable.  */
18018               loc = loc_list_from_tree (com_decl, 2);
18019             }
18020           else if (DECL_EXTERNAL (decl))
18021             add_AT_flag (com_die, DW_AT_declaration, 1);
18022           add_pubname_string (cnam, com_die); /* ??? needed? */
18023           com_die->decl_id = DECL_UID (com_decl);
18024           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18025           *slot = (void *) com_die;
18026         }
18027       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18028         {
18029           add_AT_location_description (com_die, DW_AT_location, loc);
18030           loc = loc_list_from_tree (com_decl, 2);
18031           remove_AT (com_die, DW_AT_declaration);
18032         }
18033       var_die = new_die (DW_TAG_variable, com_die, decl);
18034       add_name_and_src_coords_attributes (var_die, decl);
18035       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18036                           TREE_THIS_VOLATILE (decl), context_die);
18037       add_AT_flag (var_die, DW_AT_external, 1);
18038       if (loc)
18039         {
18040           if (off)
18041             {
18042               /* Optimize the common case.  */
18043               if (single_element_loc_list_p (loc)
18044                   && loc->expr->dw_loc_opc == DW_OP_addr
18045                   && loc->expr->dw_loc_next == NULL
18046                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18047                 loc->expr->dw_loc_oprnd1.v.val_addr
18048                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18049               else
18050                 loc_list_plus_const (loc, off);
18051             }
18052           add_AT_location_description (var_die, DW_AT_location, loc);
18053         }
18054       else if (DECL_EXTERNAL (decl))
18055         add_AT_flag (var_die, DW_AT_declaration, 1);
18056       equate_decl_number_to_die (decl, var_die);
18057       return;
18058     }
18059
18060   /* If the compiler emitted a definition for the DECL declaration
18061      and if we already emitted a DIE for it, don't emit a second
18062      DIE for it again.  */
18063   if (old_die
18064       && declaration
18065       && old_die->die_parent == context_die)
18066     return;
18067
18068   /* For static data members, the declaration in the class is supposed
18069      to have DW_TAG_member tag; the specification should still be
18070      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18071   if (declaration && class_scope_p (context_die))
18072     var_die = new_die (DW_TAG_member, context_die, decl);
18073   else
18074     var_die = new_die (DW_TAG_variable, context_die, decl);
18075
18076   origin_die = NULL;
18077   if (origin != NULL)
18078     origin_die = add_abstract_origin_attribute (var_die, origin);
18079
18080   /* Loop unrolling can create multiple blocks that refer to the same
18081      static variable, so we must test for the DW_AT_declaration flag.
18082
18083      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18084      copy decls and set the DECL_ABSTRACT flag on them instead of
18085      sharing them.
18086
18087      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18088
18089      ??? The declare_in_namespace support causes us to get two DIEs for one
18090      variable, both of which are declarations.  We want to avoid considering
18091      one to be a specification, so we must test that this DIE is not a
18092      declaration.  */
18093   else if (old_die && TREE_STATIC (decl) && ! declaration
18094            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18095     {
18096       /* This is a definition of a C++ class level static.  */
18097       add_AT_specification (var_die, old_die);
18098       if (DECL_NAME (decl))
18099         {
18100           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18101           struct dwarf_file_data * file_index = lookup_filename (s.file);
18102
18103           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18104             add_AT_file (var_die, DW_AT_decl_file, file_index);
18105
18106           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18107             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18108         }
18109     }
18110   else
18111     {
18112       tree type = TREE_TYPE (decl);
18113
18114       add_name_and_src_coords_attributes (var_die, decl);
18115       if (decl_by_reference_p (decl))
18116         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18117       else
18118         add_type_attribute (var_die, type, TREE_READONLY (decl),
18119                             TREE_THIS_VOLATILE (decl), context_die);
18120
18121       if (TREE_PUBLIC (decl))
18122         add_AT_flag (var_die, DW_AT_external, 1);
18123
18124       if (DECL_ARTIFICIAL (decl))
18125         add_AT_flag (var_die, DW_AT_artificial, 1);
18126
18127       if (TREE_PROTECTED (decl))
18128         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
18129       else if (TREE_PRIVATE (decl))
18130         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
18131     }
18132
18133   if (declaration)
18134     add_AT_flag (var_die, DW_AT_declaration, 1);
18135
18136   if (decl && (DECL_ABSTRACT (decl) || declaration))
18137     equate_decl_number_to_die (decl, var_die);
18138
18139   if (! declaration
18140       && (! DECL_ABSTRACT (decl_or_origin)
18141           /* Local static vars are shared between all clones/inlines,
18142              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18143              already set.  */
18144           || (TREE_CODE (decl_or_origin) == VAR_DECL
18145               && TREE_STATIC (decl_or_origin)
18146               && DECL_RTL_SET_P (decl_or_origin)))
18147       /* When abstract origin already has DW_AT_location attribute, no need
18148          to add it again.  */
18149       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18150     {
18151       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18152           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18153         defer_location (decl_or_origin, var_die);
18154       else
18155         add_location_or_const_value_attribute (var_die,
18156                                                decl_or_origin,
18157                                                DW_AT_location);
18158       add_pubname (decl_or_origin, var_die);
18159     }
18160   else
18161     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18162 }
18163
18164 /* Generate a DIE to represent a named constant.  */
18165
18166 static void
18167 gen_const_die (tree decl, dw_die_ref context_die)
18168 {
18169   dw_die_ref const_die;
18170   tree type = TREE_TYPE (decl);
18171
18172   const_die = new_die (DW_TAG_constant, context_die, decl);
18173   add_name_and_src_coords_attributes (const_die, decl);
18174   add_type_attribute (const_die, type, 1, 0, context_die);
18175   if (TREE_PUBLIC (decl))
18176     add_AT_flag (const_die, DW_AT_external, 1);
18177   if (DECL_ARTIFICIAL (decl))
18178     add_AT_flag (const_die, DW_AT_artificial, 1);
18179   tree_add_const_value_attribute_for_decl (const_die, decl);
18180 }
18181
18182 /* Generate a DIE to represent a label identifier.  */
18183
18184 static void
18185 gen_label_die (tree decl, dw_die_ref context_die)
18186 {
18187   tree origin = decl_ultimate_origin (decl);
18188   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18189   rtx insn;
18190   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18191
18192   if (origin != NULL)
18193     add_abstract_origin_attribute (lbl_die, origin);
18194   else
18195     add_name_and_src_coords_attributes (lbl_die, decl);
18196
18197   if (DECL_ABSTRACT (decl))
18198     equate_decl_number_to_die (decl, lbl_die);
18199   else
18200     {
18201       insn = DECL_RTL_IF_SET (decl);
18202
18203       /* Deleted labels are programmer specified labels which have been
18204          eliminated because of various optimizations.  We still emit them
18205          here so that it is possible to put breakpoints on them.  */
18206       if (insn
18207           && (LABEL_P (insn)
18208               || ((NOTE_P (insn)
18209                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18210         {
18211           /* When optimization is enabled (via -O) some parts of the compiler
18212              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18213              represent source-level labels which were explicitly declared by
18214              the user.  This really shouldn't be happening though, so catch
18215              it if it ever does happen.  */
18216           gcc_assert (!INSN_DELETED_P (insn));
18217
18218           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18219           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18220         }
18221     }
18222 }
18223
18224 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18225    attributes to the DIE for a block STMT, to describe where the inlined
18226    function was called from.  This is similar to add_src_coords_attributes.  */
18227
18228 static inline void
18229 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18230 {
18231   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18232
18233   if (dwarf_version >= 3 || !dwarf_strict)
18234     {
18235       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18236       add_AT_unsigned (die, DW_AT_call_line, s.line);
18237     }
18238 }
18239
18240
18241 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18242    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18243
18244 static inline void
18245 add_high_low_attributes (tree stmt, dw_die_ref die)
18246 {
18247   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18248
18249   if (BLOCK_FRAGMENT_CHAIN (stmt)
18250       && (dwarf_version >= 3 || !dwarf_strict))
18251     {
18252       tree chain;
18253
18254       if (inlined_function_outer_scope_p (stmt))
18255         {
18256           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18257                                        BLOCK_NUMBER (stmt));
18258           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18259         }
18260
18261       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18262
18263       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18264       do
18265         {
18266           add_ranges (chain);
18267           chain = BLOCK_FRAGMENT_CHAIN (chain);
18268         }
18269       while (chain);
18270       add_ranges (NULL);
18271     }
18272   else
18273     {
18274       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18275                                    BLOCK_NUMBER (stmt));
18276       add_AT_lbl_id (die, DW_AT_low_pc, label);
18277       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18278                                    BLOCK_NUMBER (stmt));
18279       add_AT_lbl_id (die, DW_AT_high_pc, label);
18280     }
18281 }
18282
18283 /* Generate a DIE for a lexical block.  */
18284
18285 static void
18286 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18287 {
18288   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18289
18290   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18291     add_high_low_attributes (stmt, stmt_die);
18292
18293   decls_for_scope (stmt, stmt_die, depth);
18294 }
18295
18296 /* Generate a DIE for an inlined subprogram.  */
18297
18298 static void
18299 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18300 {
18301   tree decl;
18302
18303   /* The instance of function that is effectively being inlined shall not
18304      be abstract.  */
18305   gcc_assert (! BLOCK_ABSTRACT (stmt));
18306
18307   decl = block_ultimate_origin (stmt);
18308
18309   /* Emit info for the abstract instance first, if we haven't yet.  We
18310      must emit this even if the block is abstract, otherwise when we
18311      emit the block below (or elsewhere), we may end up trying to emit
18312      a die whose origin die hasn't been emitted, and crashing.  */
18313   dwarf2out_abstract_function (decl);
18314
18315   if (! BLOCK_ABSTRACT (stmt))
18316     {
18317       dw_die_ref subr_die
18318         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18319
18320       add_abstract_origin_attribute (subr_die, decl);
18321       if (TREE_ASM_WRITTEN (stmt))
18322         add_high_low_attributes (stmt, subr_die);
18323       add_call_src_coords_attributes (stmt, subr_die);
18324
18325       decls_for_scope (stmt, subr_die, depth);
18326       current_function_has_inlines = 1;
18327     }
18328 }
18329
18330 /* Generate a DIE for a field in a record, or structure.  */
18331
18332 static void
18333 gen_field_die (tree decl, dw_die_ref context_die)
18334 {
18335   dw_die_ref decl_die;
18336
18337   if (TREE_TYPE (decl) == error_mark_node)
18338     return;
18339
18340   decl_die = new_die (DW_TAG_member, context_die, decl);
18341   add_name_and_src_coords_attributes (decl_die, decl);
18342   add_type_attribute (decl_die, member_declared_type (decl),
18343                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18344                       context_die);
18345
18346   if (DECL_BIT_FIELD_TYPE (decl))
18347     {
18348       add_byte_size_attribute (decl_die, decl);
18349       add_bit_size_attribute (decl_die, decl);
18350       add_bit_offset_attribute (decl_die, decl);
18351     }
18352
18353   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18354     add_data_member_location_attribute (decl_die, decl);
18355
18356   if (DECL_ARTIFICIAL (decl))
18357     add_AT_flag (decl_die, DW_AT_artificial, 1);
18358
18359   if (TREE_PROTECTED (decl))
18360     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
18361   else if (TREE_PRIVATE (decl))
18362     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
18363
18364   /* Equate decl number to die, so that we can look up this decl later on.  */
18365   equate_decl_number_to_die (decl, decl_die);
18366 }
18367
18368 #if 0
18369 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18370    Use modified_type_die instead.
18371    We keep this code here just in case these types of DIEs may be needed to
18372    represent certain things in other languages (e.g. Pascal) someday.  */
18373
18374 static void
18375 gen_pointer_type_die (tree type, dw_die_ref context_die)
18376 {
18377   dw_die_ref ptr_die
18378     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18379
18380   equate_type_number_to_die (type, ptr_die);
18381   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18382   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18383 }
18384
18385 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18386    Use modified_type_die instead.
18387    We keep this code here just in case these types of DIEs may be needed to
18388    represent certain things in other languages (e.g. Pascal) someday.  */
18389
18390 static void
18391 gen_reference_type_die (tree type, dw_die_ref context_die)
18392 {
18393   dw_die_ref ref_die
18394     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
18395
18396   equate_type_number_to_die (type, ref_die);
18397   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18398   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18399 }
18400 #endif
18401
18402 /* Generate a DIE for a pointer to a member type.  */
18403
18404 static void
18405 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18406 {
18407   dw_die_ref ptr_die
18408     = new_die (DW_TAG_ptr_to_member_type,
18409                scope_die_for (type, context_die), type);
18410
18411   equate_type_number_to_die (type, ptr_die);
18412   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18413                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18414   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18415 }
18416
18417 /* Generate the DIE for the compilation unit.  */
18418
18419 static dw_die_ref
18420 gen_compile_unit_die (const char *filename)
18421 {
18422   dw_die_ref die;
18423   char producer[250];
18424   const char *language_string = lang_hooks.name;
18425   int language;
18426
18427   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18428
18429   if (filename)
18430     {
18431       add_name_attribute (die, filename);
18432       /* Don't add cwd for <built-in>.  */
18433       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18434         add_comp_dir_attribute (die);
18435     }
18436
18437   sprintf (producer, "%s %s", language_string, version_string);
18438
18439 #ifdef MIPS_DEBUGGING_INFO
18440   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18441      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18442      not appear in the producer string, the debugger reaches the conclusion
18443      that the object file is stripped and has no debugging information.
18444      To get the MIPS/SGI debugger to believe that there is debugging
18445      information in the object file, we add a -g to the producer string.  */
18446   if (debug_info_level > DINFO_LEVEL_TERSE)
18447     strcat (producer, " -g");
18448 #endif
18449
18450   add_AT_string (die, DW_AT_producer, producer);
18451
18452   language = DW_LANG_C89;
18453   if (strcmp (language_string, "GNU C++") == 0)
18454     language = DW_LANG_C_plus_plus;
18455   else if (strcmp (language_string, "GNU F77") == 0)
18456     language = DW_LANG_Fortran77;
18457   else if (strcmp (language_string, "GNU Pascal") == 0)
18458     language = DW_LANG_Pascal83;
18459   else if (dwarf_version >= 3 || !dwarf_strict)
18460     {
18461       if (strcmp (language_string, "GNU Ada") == 0)
18462         language = DW_LANG_Ada95;
18463       else if (strcmp (language_string, "GNU Fortran") == 0)
18464         language = DW_LANG_Fortran95;
18465       else if (strcmp (language_string, "GNU Java") == 0)
18466         language = DW_LANG_Java;
18467       else if (strcmp (language_string, "GNU Objective-C") == 0)
18468         language = DW_LANG_ObjC;
18469       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18470         language = DW_LANG_ObjC_plus_plus;
18471     }
18472
18473   add_AT_unsigned (die, DW_AT_language, language);
18474   return die;
18475 }
18476
18477 /* Generate the DIE for a base class.  */
18478
18479 static void
18480 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18481 {
18482   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18483
18484   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18485   add_data_member_location_attribute (die, binfo);
18486
18487   if (BINFO_VIRTUAL_P (binfo))
18488     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18489
18490   if (access == access_public_node)
18491     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18492   else if (access == access_protected_node)
18493     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18494 }
18495
18496 /* Generate a DIE for a class member.  */
18497
18498 static void
18499 gen_member_die (tree type, dw_die_ref context_die)
18500 {
18501   tree member;
18502   tree binfo = TYPE_BINFO (type);
18503   dw_die_ref child;
18504
18505   /* If this is not an incomplete type, output descriptions of each of its
18506      members. Note that as we output the DIEs necessary to represent the
18507      members of this record or union type, we will also be trying to output
18508      DIEs to represent the *types* of those members. However the `type'
18509      function (above) will specifically avoid generating type DIEs for member
18510      types *within* the list of member DIEs for this (containing) type except
18511      for those types (of members) which are explicitly marked as also being
18512      members of this (containing) type themselves.  The g++ front- end can
18513      force any given type to be treated as a member of some other (containing)
18514      type by setting the TYPE_CONTEXT of the given (member) type to point to
18515      the TREE node representing the appropriate (containing) type.  */
18516
18517   /* First output info about the base classes.  */
18518   if (binfo)
18519     {
18520       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18521       int i;
18522       tree base;
18523
18524       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18525         gen_inheritance_die (base,
18526                              (accesses ? VEC_index (tree, accesses, i)
18527                               : access_public_node), context_die);
18528     }
18529
18530   /* Now output info about the data members and type members.  */
18531   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
18532     {
18533       /* If we thought we were generating minimal debug info for TYPE
18534          and then changed our minds, some of the member declarations
18535          may have already been defined.  Don't define them again, but
18536          do put them in the right order.  */
18537
18538       child = lookup_decl_die (member);
18539       if (child)
18540         splice_child_die (context_die, child);
18541       else
18542         gen_decl_die (member, NULL, context_die);
18543     }
18544
18545   /* Now output info about the function members (if any).  */
18546   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
18547     {
18548       /* Don't include clones in the member list.  */
18549       if (DECL_ABSTRACT_ORIGIN (member))
18550         continue;
18551
18552       child = lookup_decl_die (member);
18553       if (child)
18554         splice_child_die (context_die, child);
18555       else
18556         gen_decl_die (member, NULL, context_die);
18557     }
18558 }
18559
18560 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18561    is set, we pretend that the type was never defined, so we only get the
18562    member DIEs needed by later specification DIEs.  */
18563
18564 static void
18565 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18566                                 enum debug_info_usage usage)
18567 {
18568   dw_die_ref type_die = lookup_type_die (type);
18569   dw_die_ref scope_die = 0;
18570   int nested = 0;
18571   int complete = (TYPE_SIZE (type)
18572                   && (! TYPE_STUB_DECL (type)
18573                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18574   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18575   complete = complete && should_emit_struct_debug (type, usage);
18576
18577   if (type_die && ! complete)
18578     return;
18579
18580   if (TYPE_CONTEXT (type) != NULL_TREE
18581       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18582           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18583     nested = 1;
18584
18585   scope_die = scope_die_for (type, context_die);
18586
18587   if (! type_die || (nested && scope_die == comp_unit_die))
18588     /* First occurrence of type or toplevel definition of nested class.  */
18589     {
18590       dw_die_ref old_die = type_die;
18591
18592       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18593                           ? record_type_tag (type) : DW_TAG_union_type,
18594                           scope_die, type);
18595       equate_type_number_to_die (type, type_die);
18596       if (old_die)
18597         add_AT_specification (type_die, old_die);
18598       else
18599         add_name_attribute (type_die, type_tag (type));
18600     }
18601   else
18602     remove_AT (type_die, DW_AT_declaration);
18603
18604   /* Generate child dies for template paramaters.  */
18605   if (debug_info_level > DINFO_LEVEL_TERSE
18606       && COMPLETE_TYPE_P (type))
18607     gen_generic_params_dies (type);
18608
18609   /* If this type has been completed, then give it a byte_size attribute and
18610      then give a list of members.  */
18611   if (complete && !ns_decl)
18612     {
18613       /* Prevent infinite recursion in cases where the type of some member of
18614          this type is expressed in terms of this type itself.  */
18615       TREE_ASM_WRITTEN (type) = 1;
18616       add_byte_size_attribute (type_die, type);
18617       if (TYPE_STUB_DECL (type) != NULL_TREE)
18618         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18619
18620       /* If the first reference to this type was as the return type of an
18621          inline function, then it may not have a parent.  Fix this now.  */
18622       if (type_die->die_parent == NULL)
18623         add_child_die (scope_die, type_die);
18624
18625       push_decl_scope (type);
18626       gen_member_die (type, type_die);
18627       pop_decl_scope ();
18628
18629       /* GNU extension: Record what type our vtable lives in.  */
18630       if (TYPE_VFIELD (type))
18631         {
18632           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18633
18634           gen_type_die (vtype, context_die);
18635           add_AT_die_ref (type_die, DW_AT_containing_type,
18636                           lookup_type_die (vtype));
18637         }
18638     }
18639   else
18640     {
18641       add_AT_flag (type_die, DW_AT_declaration, 1);
18642
18643       /* We don't need to do this for function-local types.  */
18644       if (TYPE_STUB_DECL (type)
18645           && ! decl_function_context (TYPE_STUB_DECL (type)))
18646         VEC_safe_push (tree, gc, incomplete_types, type);
18647     }
18648
18649   if (get_AT (type_die, DW_AT_name))
18650     add_pubtype (type, type_die);
18651 }
18652
18653 /* Generate a DIE for a subroutine _type_.  */
18654
18655 static void
18656 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18657 {
18658   tree return_type = TREE_TYPE (type);
18659   dw_die_ref subr_die
18660     = new_die (DW_TAG_subroutine_type,
18661                scope_die_for (type, context_die), type);
18662
18663   equate_type_number_to_die (type, subr_die);
18664   add_prototyped_attribute (subr_die, type);
18665   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18666   gen_formal_types_die (type, subr_die);
18667
18668   if (get_AT (subr_die, DW_AT_name))
18669     add_pubtype (type, subr_die);
18670 }
18671
18672 /* Generate a DIE for a type definition.  */
18673
18674 static void
18675 gen_typedef_die (tree decl, dw_die_ref context_die)
18676 {
18677   dw_die_ref type_die;
18678   tree origin;
18679
18680   if (TREE_ASM_WRITTEN (decl))
18681     return;
18682
18683   TREE_ASM_WRITTEN (decl) = 1;
18684   type_die = new_die (DW_TAG_typedef, context_die, decl);
18685   origin = decl_ultimate_origin (decl);
18686   if (origin != NULL)
18687     add_abstract_origin_attribute (type_die, origin);
18688   else
18689     {
18690       tree type;
18691
18692       add_name_and_src_coords_attributes (type_die, decl);
18693       if (DECL_ORIGINAL_TYPE (decl))
18694         {
18695           type = DECL_ORIGINAL_TYPE (decl);
18696
18697           gcc_assert (type != TREE_TYPE (decl));
18698           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18699         }
18700       else
18701         type = TREE_TYPE (decl);
18702
18703       add_type_attribute (type_die, type, TREE_READONLY (decl),
18704                           TREE_THIS_VOLATILE (decl), context_die);
18705     }
18706
18707   if (DECL_ABSTRACT (decl))
18708     equate_decl_number_to_die (decl, type_die);
18709
18710   if (get_AT (type_die, DW_AT_name))
18711     add_pubtype (decl, type_die);
18712 }
18713
18714 /* Generate a type description DIE.  */
18715
18716 static void
18717 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18718                                 enum debug_info_usage usage)
18719 {
18720   int need_pop;
18721   struct array_descr_info info;
18722
18723   if (type == NULL_TREE || type == error_mark_node)
18724     return;
18725
18726   /* If TYPE is a typedef type variant, let's generate debug info
18727      for the parent typedef which TYPE is a type of.  */
18728   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18729       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18730     {
18731       if (TREE_ASM_WRITTEN (type))
18732         return;
18733
18734       /* Prevent broken recursion; we can't hand off to the same type.  */
18735       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18736
18737       /* Use the DIE of the containing namespace as the parent DIE of
18738          the type description DIE we want to generate.  */
18739       if (DECL_CONTEXT (TYPE_NAME (type))
18740           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18741         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18742
18743       TREE_ASM_WRITTEN (type) = 1;
18744       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18745       return;
18746     }
18747
18748   /* If this is an array type with hidden descriptor, handle it first.  */
18749   if (!TREE_ASM_WRITTEN (type)
18750       && lang_hooks.types.get_array_descr_info
18751       && lang_hooks.types.get_array_descr_info (type, &info)
18752       && (dwarf_version >= 3 || !dwarf_strict))
18753     {
18754       gen_descr_array_type_die (type, &info, context_die);
18755       TREE_ASM_WRITTEN (type) = 1;
18756       return;
18757     }
18758
18759   /* We are going to output a DIE to represent the unqualified version
18760      of this type (i.e. without any const or volatile qualifiers) so
18761      get the main variant (i.e. the unqualified version) of this type
18762      now.  (Vectors are special because the debugging info is in the
18763      cloned type itself).  */
18764   if (TREE_CODE (type) != VECTOR_TYPE)
18765     type = type_main_variant (type);
18766
18767   if (TREE_ASM_WRITTEN (type))
18768     return;
18769
18770   switch (TREE_CODE (type))
18771     {
18772     case ERROR_MARK:
18773       break;
18774
18775     case POINTER_TYPE:
18776     case REFERENCE_TYPE:
18777       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18778          ensures that the gen_type_die recursion will terminate even if the
18779          type is recursive.  Recursive types are possible in Ada.  */
18780       /* ??? We could perhaps do this for all types before the switch
18781          statement.  */
18782       TREE_ASM_WRITTEN (type) = 1;
18783
18784       /* For these types, all that is required is that we output a DIE (or a
18785          set of DIEs) to represent the "basis" type.  */
18786       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18787                                 DINFO_USAGE_IND_USE);
18788       break;
18789
18790     case OFFSET_TYPE:
18791       /* This code is used for C++ pointer-to-data-member types.
18792          Output a description of the relevant class type.  */
18793       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18794                                         DINFO_USAGE_IND_USE);
18795
18796       /* Output a description of the type of the object pointed to.  */
18797       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18798                                         DINFO_USAGE_IND_USE);
18799
18800       /* Now output a DIE to represent this pointer-to-data-member type
18801          itself.  */
18802       gen_ptr_to_mbr_type_die (type, context_die);
18803       break;
18804
18805     case FUNCTION_TYPE:
18806       /* Force out return type (in case it wasn't forced out already).  */
18807       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18808                                         DINFO_USAGE_DIR_USE);
18809       gen_subroutine_type_die (type, context_die);
18810       break;
18811
18812     case METHOD_TYPE:
18813       /* Force out return type (in case it wasn't forced out already).  */
18814       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18815                                         DINFO_USAGE_DIR_USE);
18816       gen_subroutine_type_die (type, context_die);
18817       break;
18818
18819     case ARRAY_TYPE:
18820       gen_array_type_die (type, context_die);
18821       break;
18822
18823     case VECTOR_TYPE:
18824       gen_array_type_die (type, context_die);
18825       break;
18826
18827     case ENUMERAL_TYPE:
18828     case RECORD_TYPE:
18829     case UNION_TYPE:
18830     case QUAL_UNION_TYPE:
18831       /* If this is a nested type whose containing class hasn't been written
18832          out yet, writing it out will cover this one, too.  This does not apply
18833          to instantiations of member class templates; they need to be added to
18834          the containing class as they are generated.  FIXME: This hurts the
18835          idea of combining type decls from multiple TUs, since we can't predict
18836          what set of template instantiations we'll get.  */
18837       if (TYPE_CONTEXT (type)
18838           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18839           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18840         {
18841           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18842
18843           if (TREE_ASM_WRITTEN (type))
18844             return;
18845
18846           /* If that failed, attach ourselves to the stub.  */
18847           push_decl_scope (TYPE_CONTEXT (type));
18848           context_die = lookup_type_die (TYPE_CONTEXT (type));
18849           need_pop = 1;
18850         }
18851       else if (TYPE_CONTEXT (type) != NULL_TREE
18852                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18853         {
18854           /* If this type is local to a function that hasn't been written
18855              out yet, use a NULL context for now; it will be fixed up in
18856              decls_for_scope.  */
18857           context_die = lookup_decl_die (TYPE_CONTEXT (type));
18858           need_pop = 0;
18859         }
18860       else
18861         {
18862           context_die = declare_in_namespace (type, context_die);
18863           need_pop = 0;
18864         }
18865
18866       if (TREE_CODE (type) == ENUMERAL_TYPE)
18867         {
18868           /* This might have been written out by the call to
18869              declare_in_namespace.  */
18870           if (!TREE_ASM_WRITTEN (type))
18871             gen_enumeration_type_die (type, context_die);
18872         }
18873       else
18874         gen_struct_or_union_type_die (type, context_die, usage);
18875
18876       if (need_pop)
18877         pop_decl_scope ();
18878
18879       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18880          it up if it is ever completed.  gen_*_type_die will set it for us
18881          when appropriate.  */
18882       return;
18883
18884     case VOID_TYPE:
18885     case INTEGER_TYPE:
18886     case REAL_TYPE:
18887     case FIXED_POINT_TYPE:
18888     case COMPLEX_TYPE:
18889     case BOOLEAN_TYPE:
18890       /* No DIEs needed for fundamental types.  */
18891       break;
18892
18893     case LANG_TYPE:
18894       /* No Dwarf representation currently defined.  */
18895       break;
18896
18897     default:
18898       gcc_unreachable ();
18899     }
18900
18901   TREE_ASM_WRITTEN (type) = 1;
18902 }
18903
18904 static void
18905 gen_type_die (tree type, dw_die_ref context_die)
18906 {
18907   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18908 }
18909
18910 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18911    things which are local to the given block.  */
18912
18913 static void
18914 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18915 {
18916   int must_output_die = 0;
18917   bool inlined_func;
18918
18919   /* Ignore blocks that are NULL.  */
18920   if (stmt == NULL_TREE)
18921     return;
18922
18923   inlined_func = inlined_function_outer_scope_p (stmt);
18924
18925   /* If the block is one fragment of a non-contiguous block, do not
18926      process the variables, since they will have been done by the
18927      origin block.  Do process subblocks.  */
18928   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18929     {
18930       tree sub;
18931
18932       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18933         gen_block_die (sub, context_die, depth + 1);
18934
18935       return;
18936     }
18937
18938   /* Determine if we need to output any Dwarf DIEs at all to represent this
18939      block.  */
18940   if (inlined_func)
18941     /* The outer scopes for inlinings *must* always be represented.  We
18942        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18943     must_output_die = 1;
18944   else
18945     {
18946       /* Determine if this block directly contains any "significant"
18947          local declarations which we will need to output DIEs for.  */
18948       if (debug_info_level > DINFO_LEVEL_TERSE)
18949         /* We are not in terse mode so *any* local declaration counts
18950            as being a "significant" one.  */
18951         must_output_die = ((BLOCK_VARS (stmt) != NULL
18952                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18953                            && (TREE_USED (stmt)
18954                                || TREE_ASM_WRITTEN (stmt)
18955                                || BLOCK_ABSTRACT (stmt)));
18956       else if ((TREE_USED (stmt)
18957                 || TREE_ASM_WRITTEN (stmt)
18958                 || BLOCK_ABSTRACT (stmt))
18959                && !dwarf2out_ignore_block (stmt))
18960         must_output_die = 1;
18961     }
18962
18963   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18964      DIE for any block which contains no significant local declarations at
18965      all.  Rather, in such cases we just call `decls_for_scope' so that any
18966      needed Dwarf info for any sub-blocks will get properly generated. Note
18967      that in terse mode, our definition of what constitutes a "significant"
18968      local declaration gets restricted to include only inlined function
18969      instances and local (nested) function definitions.  */
18970   if (must_output_die)
18971     {
18972       if (inlined_func)
18973         {
18974           /* If STMT block is abstract, that means we have been called
18975              indirectly from dwarf2out_abstract_function.
18976              That function rightfully marks the descendent blocks (of
18977              the abstract function it is dealing with) as being abstract,
18978              precisely to prevent us from emitting any
18979              DW_TAG_inlined_subroutine DIE as a descendent
18980              of an abstract function instance. So in that case, we should
18981              not call gen_inlined_subroutine_die.
18982
18983              Later though, when cgraph asks dwarf2out to emit info
18984              for the concrete instance of the function decl into which
18985              the concrete instance of STMT got inlined, the later will lead
18986              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18987           if (! BLOCK_ABSTRACT (stmt))
18988             gen_inlined_subroutine_die (stmt, context_die, depth);
18989         }
18990       else
18991         gen_lexical_block_die (stmt, context_die, depth);
18992     }
18993   else
18994     decls_for_scope (stmt, context_die, depth);
18995 }
18996
18997 /* Process variable DECL (or variable with origin ORIGIN) within
18998    block STMT and add it to CONTEXT_DIE.  */
18999 static void
19000 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19001 {
19002   dw_die_ref die;
19003   tree decl_or_origin = decl ? decl : origin;
19004   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
19005
19006   if (ultimate_origin)
19007     origin = ultimate_origin;
19008
19009   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19010     die = lookup_decl_die (decl_or_origin);
19011   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19012            && TYPE_DECL_IS_STUB (decl_or_origin))
19013     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19014   else
19015     die = NULL;
19016
19017   if (die != NULL && die->die_parent == NULL)
19018     add_child_die (context_die, die);
19019   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19020     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19021                                          stmt, context_die);
19022   else
19023     gen_decl_die (decl, origin, context_die);
19024 }
19025
19026 /* Generate all of the decls declared within a given scope and (recursively)
19027    all of its sub-blocks.  */
19028
19029 static void
19030 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19031 {
19032   tree decl;
19033   unsigned int i;
19034   tree subblocks;
19035
19036   /* Ignore NULL blocks.  */
19037   if (stmt == NULL_TREE)
19038     return;
19039
19040   /* Output the DIEs to represent all of the data objects and typedefs
19041      declared directly within this block but not within any nested
19042      sub-blocks.  Also, nested function and tag DIEs have been
19043      generated with a parent of NULL; fix that up now.  */
19044   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
19045     process_scope_var (stmt, decl, NULL_TREE, context_die);
19046   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19047     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19048                        context_die);
19049
19050   /* If we're at -g1, we're not interested in subblocks.  */
19051   if (debug_info_level <= DINFO_LEVEL_TERSE)
19052     return;
19053
19054   /* Output the DIEs to represent all sub-blocks (and the items declared
19055      therein) of this block.  */
19056   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19057        subblocks != NULL;
19058        subblocks = BLOCK_CHAIN (subblocks))
19059     gen_block_die (subblocks, context_die, depth + 1);
19060 }
19061
19062 /* Is this a typedef we can avoid emitting?  */
19063
19064 static inline int
19065 is_redundant_typedef (const_tree decl)
19066 {
19067   if (TYPE_DECL_IS_STUB (decl))
19068     return 1;
19069
19070   if (DECL_ARTIFICIAL (decl)
19071       && DECL_CONTEXT (decl)
19072       && is_tagged_type (DECL_CONTEXT (decl))
19073       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19074       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19075     /* Also ignore the artificial member typedef for the class name.  */
19076     return 1;
19077
19078   return 0;
19079 }
19080
19081 /* Returns the DIE for a context.  */
19082
19083 static inline dw_die_ref
19084 get_context_die (tree context)
19085 {
19086   if (context)
19087     {
19088       /* Find die that represents this context.  */
19089       if (TYPE_P (context))
19090         return force_type_die (context);
19091       else
19092         return force_decl_die (context);
19093     }
19094   return comp_unit_die;
19095 }
19096
19097 /* Returns the DIE for decl.  A DIE will always be returned.  */
19098
19099 static dw_die_ref
19100 force_decl_die (tree decl)
19101 {
19102   dw_die_ref decl_die;
19103   unsigned saved_external_flag;
19104   tree save_fn = NULL_TREE;
19105   decl_die = lookup_decl_die (decl);
19106   if (!decl_die)
19107     {
19108       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19109
19110       decl_die = lookup_decl_die (decl);
19111       if (decl_die)
19112         return decl_die;
19113
19114       switch (TREE_CODE (decl))
19115         {
19116         case FUNCTION_DECL:
19117           /* Clear current_function_decl, so that gen_subprogram_die thinks
19118              that this is a declaration. At this point, we just want to force
19119              declaration die.  */
19120           save_fn = current_function_decl;
19121           current_function_decl = NULL_TREE;
19122           gen_subprogram_die (decl, context_die);
19123           current_function_decl = save_fn;
19124           break;
19125
19126         case VAR_DECL:
19127           /* Set external flag to force declaration die. Restore it after
19128            gen_decl_die() call.  */
19129           saved_external_flag = DECL_EXTERNAL (decl);
19130           DECL_EXTERNAL (decl) = 1;
19131           gen_decl_die (decl, NULL, context_die);
19132           DECL_EXTERNAL (decl) = saved_external_flag;
19133           break;
19134
19135         case NAMESPACE_DECL:
19136           if (dwarf_version >= 3 || !dwarf_strict)
19137             dwarf2out_decl (decl);
19138           else
19139             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19140             decl_die = comp_unit_die;
19141           break;
19142
19143         default:
19144           gcc_unreachable ();
19145         }
19146
19147       /* We should be able to find the DIE now.  */
19148       if (!decl_die)
19149         decl_die = lookup_decl_die (decl);
19150       gcc_assert (decl_die);
19151     }
19152
19153   return decl_die;
19154 }
19155
19156 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19157    always returned.  */
19158
19159 static dw_die_ref
19160 force_type_die (tree type)
19161 {
19162   dw_die_ref type_die;
19163
19164   type_die = lookup_type_die (type);
19165   if (!type_die)
19166     {
19167       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19168
19169       type_die = modified_type_die (type, TYPE_READONLY (type),
19170                                     TYPE_VOLATILE (type), context_die);
19171       gcc_assert (type_die);
19172     }
19173   return type_die;
19174 }
19175
19176 /* Force out any required namespaces to be able to output DECL,
19177    and return the new context_die for it, if it's changed.  */
19178
19179 static dw_die_ref
19180 setup_namespace_context (tree thing, dw_die_ref context_die)
19181 {
19182   tree context = (DECL_P (thing)
19183                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19184   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19185     /* Force out the namespace.  */
19186     context_die = force_decl_die (context);
19187
19188   return context_die;
19189 }
19190
19191 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19192    type) within its namespace, if appropriate.
19193
19194    For compatibility with older debuggers, namespace DIEs only contain
19195    declarations; all definitions are emitted at CU scope.  */
19196
19197 static dw_die_ref
19198 declare_in_namespace (tree thing, dw_die_ref context_die)
19199 {
19200   dw_die_ref ns_context;
19201
19202   if (debug_info_level <= DINFO_LEVEL_TERSE)
19203     return context_die;
19204
19205   /* If this decl is from an inlined function, then don't try to emit it in its
19206      namespace, as we will get confused.  It would have already been emitted
19207      when the abstract instance of the inline function was emitted anyways.  */
19208   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19209     return context_die;
19210
19211   ns_context = setup_namespace_context (thing, context_die);
19212
19213   if (ns_context != context_die)
19214     {
19215       if (is_fortran ())
19216         return ns_context;
19217       if (DECL_P (thing))
19218         gen_decl_die (thing, NULL, ns_context);
19219       else
19220         gen_type_die (thing, ns_context);
19221     }
19222   return context_die;
19223 }
19224
19225 /* Generate a DIE for a namespace or namespace alias.  */
19226
19227 static void
19228 gen_namespace_die (tree decl, dw_die_ref context_die)
19229 {
19230   dw_die_ref namespace_die;
19231
19232   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19233      they are an alias of.  */
19234   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19235     {
19236       /* Output a real namespace or module.  */
19237       context_die = setup_namespace_context (decl, comp_unit_die);
19238       namespace_die = new_die (is_fortran ()
19239                                ? DW_TAG_module : DW_TAG_namespace,
19240                                context_die, decl);
19241       /* For Fortran modules defined in different CU don't add src coords.  */
19242       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19243         {
19244           const char *name = dwarf2_name (decl, 0);
19245           if (name)
19246             add_name_attribute (namespace_die, name);
19247         }
19248       else
19249         add_name_and_src_coords_attributes (namespace_die, decl);
19250       if (DECL_EXTERNAL (decl))
19251         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19252       equate_decl_number_to_die (decl, namespace_die);
19253     }
19254   else
19255     {
19256       /* Output a namespace alias.  */
19257
19258       /* Force out the namespace we are an alias of, if necessary.  */
19259       dw_die_ref origin_die
19260         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19261
19262       if (DECL_CONTEXT (decl) == NULL_TREE
19263           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19264         context_die = setup_namespace_context (decl, comp_unit_die);
19265       /* Now create the namespace alias DIE.  */
19266       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19267       add_name_and_src_coords_attributes (namespace_die, decl);
19268       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19269       equate_decl_number_to_die (decl, namespace_die);
19270     }
19271 }
19272
19273 /* Generate Dwarf debug information for a decl described by DECL.  */
19274
19275 static void
19276 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19277 {
19278   tree decl_or_origin = decl ? decl : origin;
19279   tree class_origin = NULL;
19280
19281   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19282     return;
19283
19284   switch (TREE_CODE (decl_or_origin))
19285     {
19286     case ERROR_MARK:
19287       break;
19288
19289     case CONST_DECL:
19290       if (!is_fortran ())
19291         {
19292           /* The individual enumerators of an enum type get output when we output
19293              the Dwarf representation of the relevant enum type itself.  */
19294           break;
19295         }
19296
19297       /* Emit its type.  */
19298       gen_type_die (TREE_TYPE (decl), context_die);
19299
19300       /* And its containing namespace.  */
19301       context_die = declare_in_namespace (decl, context_die);
19302
19303       gen_const_die (decl, context_die);
19304       break;
19305
19306     case FUNCTION_DECL:
19307       /* Don't output any DIEs to represent mere function declarations,
19308          unless they are class members or explicit block externs.  */
19309       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19310           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
19311           && (current_function_decl == NULL_TREE
19312               || DECL_ARTIFICIAL (decl_or_origin)))
19313         break;
19314
19315 #if 0
19316       /* FIXME */
19317       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19318          on local redeclarations of global functions.  That seems broken.  */
19319       if (current_function_decl != decl)
19320         /* This is only a declaration.  */;
19321 #endif
19322
19323       /* If we're emitting a clone, emit info for the abstract instance.  */
19324       if (origin || DECL_ORIGIN (decl) != decl)
19325         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
19326
19327       /* If we're emitting an out-of-line copy of an inline function,
19328          emit info for the abstract instance and set up to refer to it.  */
19329       else if (cgraph_function_possibly_inlined_p (decl)
19330                && ! DECL_ABSTRACT (decl)
19331                && ! class_or_namespace_scope_p (context_die)
19332                /* dwarf2out_abstract_function won't emit a die if this is just
19333                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19334                   that case, because that works only if we have a die.  */
19335                && DECL_INITIAL (decl) != NULL_TREE)
19336         {
19337           dwarf2out_abstract_function (decl);
19338           set_decl_origin_self (decl);
19339         }
19340
19341       /* Otherwise we're emitting the primary DIE for this decl.  */
19342       else if (debug_info_level > DINFO_LEVEL_TERSE)
19343         {
19344           /* Before we describe the FUNCTION_DECL itself, make sure that we
19345              have described its return type.  */
19346           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19347
19348           /* And its virtual context.  */
19349           if (DECL_VINDEX (decl) != NULL_TREE)
19350             gen_type_die (DECL_CONTEXT (decl), context_die);
19351
19352           /* And its containing type.  */
19353           if (!origin)
19354             origin = decl_class_context (decl);
19355           if (origin != NULL_TREE)
19356             gen_type_die_for_member (origin, decl, context_die);
19357
19358           /* And its containing namespace.  */
19359           context_die = declare_in_namespace (decl, context_die);
19360         }
19361
19362       /* Now output a DIE to represent the function itself.  */
19363       if (decl)
19364         gen_subprogram_die (decl, context_die);
19365       break;
19366
19367     case TYPE_DECL:
19368       /* If we are in terse mode, don't generate any DIEs to represent any
19369          actual typedefs.  */
19370       if (debug_info_level <= DINFO_LEVEL_TERSE)
19371         break;
19372
19373       /* In the special case of a TYPE_DECL node representing the declaration
19374          of some type tag, if the given TYPE_DECL is marked as having been
19375          instantiated from some other (original) TYPE_DECL node (e.g. one which
19376          was generated within the original definition of an inline function) we
19377          used to generate a special (abbreviated) DW_TAG_structure_type,
19378          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19379          should be actually referencing those DIEs, as variable DIEs with that
19380          type would be emitted already in the abstract origin, so it was always
19381          removed during unused type prunning.  Don't add anything in this
19382          case.  */
19383       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19384         break;
19385
19386       if (is_redundant_typedef (decl))
19387         gen_type_die (TREE_TYPE (decl), context_die);
19388       else
19389         /* Output a DIE to represent the typedef itself.  */
19390         gen_typedef_die (decl, context_die);
19391       break;
19392
19393     case LABEL_DECL:
19394       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19395         gen_label_die (decl, context_die);
19396       break;
19397
19398     case VAR_DECL:
19399     case RESULT_DECL:
19400       /* If we are in terse mode, don't generate any DIEs to represent any
19401          variable declarations or definitions.  */
19402       if (debug_info_level <= DINFO_LEVEL_TERSE)
19403         break;
19404
19405       /* Output any DIEs that are needed to specify the type of this data
19406          object.  */
19407       if (decl_by_reference_p (decl_or_origin))
19408         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19409       else
19410         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19411
19412       /* And its containing type.  */
19413       class_origin = decl_class_context (decl_or_origin);
19414       if (class_origin != NULL_TREE)
19415         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19416
19417       /* And its containing namespace.  */
19418       context_die = declare_in_namespace (decl_or_origin, context_die);
19419
19420       /* Now output the DIE to represent the data object itself.  This gets
19421          complicated because of the possibility that the VAR_DECL really
19422          represents an inlined instance of a formal parameter for an inline
19423          function.  */
19424       if (!origin)
19425         origin = decl_ultimate_origin (decl);
19426       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
19427         gen_formal_parameter_die (decl, origin,
19428                                   true /* Emit name attribute.  */,
19429                                   context_die);
19430       else
19431         gen_variable_die (decl, origin, context_die);
19432       break;
19433
19434     case FIELD_DECL:
19435       /* Ignore the nameless fields that are used to skip bits but handle C++
19436          anonymous unions and structs.  */
19437       if (DECL_NAME (decl) != NULL_TREE
19438           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19439           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19440         {
19441           gen_type_die (member_declared_type (decl), context_die);
19442           gen_field_die (decl, context_die);
19443         }
19444       break;
19445
19446     case PARM_DECL:
19447       if (DECL_BY_REFERENCE (decl_or_origin))
19448         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19449       else
19450         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19451       gen_formal_parameter_die (decl, origin,
19452                                 true /* Emit name attribute.  */,
19453                                 context_die);
19454       break;
19455
19456     case NAMESPACE_DECL:
19457     case IMPORTED_DECL:
19458       if (dwarf_version >= 3 || !dwarf_strict)
19459         gen_namespace_die (decl, context_die);
19460       break;
19461
19462     default:
19463       /* Probably some frontend-internal decl.  Assume we don't care.  */
19464       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19465       break;
19466     }
19467 }
19468 \f
19469 /* Output debug information for global decl DECL.  Called from toplev.c after
19470    compilation proper has finished.  */
19471
19472 static void
19473 dwarf2out_global_decl (tree decl)
19474 {
19475   /* Output DWARF2 information for file-scope tentative data object
19476      declarations, file-scope (extern) function declarations (which
19477      had no corresponding body) and file-scope tagged type declarations
19478      and definitions which have not yet been forced out.  */
19479   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19480     dwarf2out_decl (decl);
19481 }
19482
19483 /* Output debug information for type decl DECL.  Called from toplev.c
19484    and from language front ends (to record built-in types).  */
19485 static void
19486 dwarf2out_type_decl (tree decl, int local)
19487 {
19488   if (!local)
19489     dwarf2out_decl (decl);
19490 }
19491
19492 /* Output debug information for imported module or decl DECL.
19493    NAME is non-NULL name in the lexical block if the decl has been renamed.
19494    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19495    that DECL belongs to.
19496    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19497 static void
19498 dwarf2out_imported_module_or_decl_1 (tree decl,
19499                                      tree name,
19500                                      tree lexical_block,
19501                                      dw_die_ref lexical_block_die)
19502 {
19503   expanded_location xloc;
19504   dw_die_ref imported_die = NULL;
19505   dw_die_ref at_import_die;
19506
19507   if (TREE_CODE (decl) == IMPORTED_DECL)
19508     {
19509       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19510       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19511       gcc_assert (decl);
19512     }
19513   else
19514     xloc = expand_location (input_location);
19515
19516   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19517     {
19518       if (is_base_type (TREE_TYPE (decl)))
19519         at_import_die = base_type_die (TREE_TYPE (decl));
19520       else
19521         at_import_die = force_type_die (TREE_TYPE (decl));
19522       /* For namespace N { typedef void T; } using N::T; base_type_die
19523          returns NULL, but DW_TAG_imported_declaration requires
19524          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19525       if (!at_import_die)
19526         {
19527           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19528           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19529           at_import_die = lookup_type_die (TREE_TYPE (decl));
19530           gcc_assert (at_import_die);
19531         }
19532     }
19533   else
19534     {
19535       at_import_die = lookup_decl_die (decl);
19536       if (!at_import_die)
19537         {
19538           /* If we're trying to avoid duplicate debug info, we may not have
19539              emitted the member decl for this field.  Emit it now.  */
19540           if (TREE_CODE (decl) == FIELD_DECL)
19541             {
19542               tree type = DECL_CONTEXT (decl);
19543
19544               if (TYPE_CONTEXT (type)
19545                   && TYPE_P (TYPE_CONTEXT (type))
19546                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19547                                                 DINFO_USAGE_DIR_USE))
19548                 return;
19549               gen_type_die_for_member (type, decl,
19550                                        get_context_die (TYPE_CONTEXT (type)));
19551             }
19552           at_import_die = force_decl_die (decl);
19553         }
19554     }
19555
19556   if (TREE_CODE (decl) == NAMESPACE_DECL)
19557     {
19558       if (dwarf_version >= 3 || !dwarf_strict)
19559         imported_die = new_die (DW_TAG_imported_module,
19560                                 lexical_block_die,
19561                                 lexical_block);
19562       else
19563         return;
19564     }
19565   else
19566     imported_die = new_die (DW_TAG_imported_declaration,
19567                             lexical_block_die,
19568                             lexical_block);
19569
19570   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19571   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19572   if (name)
19573     add_AT_string (imported_die, DW_AT_name,
19574                    IDENTIFIER_POINTER (name));
19575   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19576 }
19577
19578 /* Output debug information for imported module or decl DECL.
19579    NAME is non-NULL name in context if the decl has been renamed.
19580    CHILD is true if decl is one of the renamed decls as part of
19581    importing whole module.  */
19582
19583 static void
19584 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19585                                    bool child)
19586 {
19587   /* dw_die_ref at_import_die;  */
19588   dw_die_ref scope_die;
19589
19590   if (debug_info_level <= DINFO_LEVEL_TERSE)
19591     return;
19592
19593   gcc_assert (decl);
19594
19595   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19596      We need decl DIE for reference and scope die. First, get DIE for the decl
19597      itself.  */
19598
19599   /* Get the scope die for decl context. Use comp_unit_die for global module
19600      or decl. If die is not found for non globals, force new die.  */
19601   if (context
19602       && TYPE_P (context)
19603       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19604     return;
19605
19606   if (!(dwarf_version >= 3 || !dwarf_strict))
19607     return;
19608
19609   scope_die = get_context_die (context);
19610
19611   if (child)
19612     {
19613       gcc_assert (scope_die->die_child);
19614       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19615       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19616       scope_die = scope_die->die_child;
19617     }
19618
19619   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19620   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19621
19622 }
19623
19624 /* Write the debugging output for DECL.  */
19625
19626 void
19627 dwarf2out_decl (tree decl)
19628 {
19629   dw_die_ref context_die = comp_unit_die;
19630
19631   switch (TREE_CODE (decl))
19632     {
19633     case ERROR_MARK:
19634       return;
19635
19636     case FUNCTION_DECL:
19637       /* What we would really like to do here is to filter out all mere
19638          file-scope declarations of file-scope functions which are never
19639          referenced later within this translation unit (and keep all of ones
19640          that *are* referenced later on) but we aren't clairvoyant, so we have
19641          no idea which functions will be referenced in the future (i.e. later
19642          on within the current translation unit). So here we just ignore all
19643          file-scope function declarations which are not also definitions.  If
19644          and when the debugger needs to know something about these functions,
19645          it will have to hunt around and find the DWARF information associated
19646          with the definition of the function.
19647
19648          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19649          nodes represent definitions and which ones represent mere
19650          declarations.  We have to check DECL_INITIAL instead. That's because
19651          the C front-end supports some weird semantics for "extern inline"
19652          function definitions.  These can get inlined within the current
19653          translation unit (and thus, we need to generate Dwarf info for their
19654          abstract instances so that the Dwarf info for the concrete inlined
19655          instances can have something to refer to) but the compiler never
19656          generates any out-of-lines instances of such things (despite the fact
19657          that they *are* definitions).
19658
19659          The important point is that the C front-end marks these "extern
19660          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19661          them anyway. Note that the C++ front-end also plays some similar games
19662          for inline function definitions appearing within include files which
19663          also contain `#pragma interface' pragmas.  */
19664       if (DECL_INITIAL (decl) == NULL_TREE)
19665         return;
19666
19667       /* If we're a nested function, initially use a parent of NULL; if we're
19668          a plain function, this will be fixed up in decls_for_scope.  If
19669          we're a method, it will be ignored, since we already have a DIE.  */
19670       if (decl_function_context (decl)
19671           /* But if we're in terse mode, we don't care about scope.  */
19672           && debug_info_level > DINFO_LEVEL_TERSE)
19673         context_die = NULL;
19674       break;
19675
19676     case VAR_DECL:
19677       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19678          declaration and if the declaration was never even referenced from
19679          within this entire compilation unit.  We suppress these DIEs in
19680          order to save space in the .debug section (by eliminating entries
19681          which are probably useless).  Note that we must not suppress
19682          block-local extern declarations (whether used or not) because that
19683          would screw-up the debugger's name lookup mechanism and cause it to
19684          miss things which really ought to be in scope at a given point.  */
19685       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19686         return;
19687
19688       /* For local statics lookup proper context die.  */
19689       if (TREE_STATIC (decl) && decl_function_context (decl))
19690         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19691
19692       /* If we are in terse mode, don't generate any DIEs to represent any
19693          variable declarations or definitions.  */
19694       if (debug_info_level <= DINFO_LEVEL_TERSE)
19695         return;
19696       break;
19697
19698     case CONST_DECL:
19699       if (debug_info_level <= DINFO_LEVEL_TERSE)
19700         return;
19701       if (!is_fortran ())
19702         return;
19703       if (TREE_STATIC (decl) && decl_function_context (decl))
19704         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19705       break;
19706
19707     case NAMESPACE_DECL:
19708     case IMPORTED_DECL:
19709       if (debug_info_level <= DINFO_LEVEL_TERSE)
19710         return;
19711       if (lookup_decl_die (decl) != NULL)
19712         return;
19713       break;
19714
19715     case TYPE_DECL:
19716       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19717       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19718         return;
19719
19720       /* Don't bother trying to generate any DIEs to represent any of the
19721          normal built-in types for the language we are compiling.  */
19722       if (DECL_IS_BUILTIN (decl))
19723         {
19724           /* OK, we need to generate one for `bool' so GDB knows what type
19725              comparisons have.  */
19726           if (is_cxx ()
19727               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
19728               && ! DECL_IGNORED_P (decl))
19729             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
19730
19731           return;
19732         }
19733
19734       /* If we are in terse mode, don't generate any DIEs for types.  */
19735       if (debug_info_level <= DINFO_LEVEL_TERSE)
19736         return;
19737
19738       /* If we're a function-scope tag, initially use a parent of NULL;
19739          this will be fixed up in decls_for_scope.  */
19740       if (decl_function_context (decl))
19741         context_die = NULL;
19742
19743       break;
19744
19745     default:
19746       return;
19747     }
19748
19749   gen_decl_die (decl, NULL, context_die);
19750 }
19751
19752 /* Output a marker (i.e. a label) for the beginning of the generated code for
19753    a lexical block.  */
19754
19755 static void
19756 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19757                        unsigned int blocknum)
19758 {
19759   switch_to_section (current_function_section ());
19760   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19761 }
19762
19763 /* Output a marker (i.e. a label) for the end of the generated code for a
19764    lexical block.  */
19765
19766 static void
19767 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19768 {
19769   switch_to_section (current_function_section ());
19770   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19771 }
19772
19773 /* Returns nonzero if it is appropriate not to emit any debugging
19774    information for BLOCK, because it doesn't contain any instructions.
19775
19776    Don't allow this for blocks with nested functions or local classes
19777    as we would end up with orphans, and in the presence of scheduling
19778    we may end up calling them anyway.  */
19779
19780 static bool
19781 dwarf2out_ignore_block (const_tree block)
19782 {
19783   tree decl;
19784   unsigned int i;
19785
19786   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
19787     if (TREE_CODE (decl) == FUNCTION_DECL
19788         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19789       return 0;
19790   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19791     {
19792       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19793       if (TREE_CODE (decl) == FUNCTION_DECL
19794           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19795       return 0;
19796     }
19797
19798   return 1;
19799 }
19800
19801 /* Hash table routines for file_hash.  */
19802
19803 static int
19804 file_table_eq (const void *p1_p, const void *p2_p)
19805 {
19806   const struct dwarf_file_data *const p1 =
19807     (const struct dwarf_file_data *) p1_p;
19808   const char *const p2 = (const char *) p2_p;
19809   return strcmp (p1->filename, p2) == 0;
19810 }
19811
19812 static hashval_t
19813 file_table_hash (const void *p_p)
19814 {
19815   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19816   return htab_hash_string (p->filename);
19817 }
19818
19819 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19820    dwarf2out.c) and return its "index".  The index of each (known) filename is
19821    just a unique number which is associated with only that one filename.  We
19822    need such numbers for the sake of generating labels (in the .debug_sfnames
19823    section) and references to those files numbers (in the .debug_srcinfo
19824    and.debug_macinfo sections).  If the filename given as an argument is not
19825    found in our current list, add it to the list and assign it the next
19826    available unique index number.  In order to speed up searches, we remember
19827    the index of the filename was looked up last.  This handles the majority of
19828    all searches.  */
19829
19830 static struct dwarf_file_data *
19831 lookup_filename (const char *file_name)
19832 {
19833   void ** slot;
19834   struct dwarf_file_data * created;
19835
19836   /* Check to see if the file name that was searched on the previous
19837      call matches this file name.  If so, return the index.  */
19838   if (file_table_last_lookup
19839       && (file_name == file_table_last_lookup->filename
19840           || strcmp (file_table_last_lookup->filename, file_name) == 0))
19841     return file_table_last_lookup;
19842
19843   /* Didn't match the previous lookup, search the table.  */
19844   slot = htab_find_slot_with_hash (file_table, file_name,
19845                                    htab_hash_string (file_name), INSERT);
19846   if (*slot)
19847     return (struct dwarf_file_data *) *slot;
19848
19849   created = GGC_NEW (struct dwarf_file_data);
19850   created->filename = file_name;
19851   created->emitted_number = 0;
19852   *slot = created;
19853   return created;
19854 }
19855
19856 /* If the assembler will construct the file table, then translate the compiler
19857    internal file table number into the assembler file table number, and emit
19858    a .file directive if we haven't already emitted one yet.  The file table
19859    numbers are different because we prune debug info for unused variables and
19860    types, which may include filenames.  */
19861
19862 static int
19863 maybe_emit_file (struct dwarf_file_data * fd)
19864 {
19865   if (! fd->emitted_number)
19866     {
19867       if (last_emitted_file)
19868         fd->emitted_number = last_emitted_file->emitted_number + 1;
19869       else
19870         fd->emitted_number = 1;
19871       last_emitted_file = fd;
19872
19873       if (DWARF2_ASM_LINE_DEBUG_INFO)
19874         {
19875           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19876           output_quoted_string (asm_out_file,
19877                                 remap_debug_filename (fd->filename));
19878           fputc ('\n', asm_out_file);
19879         }
19880     }
19881
19882   return fd->emitted_number;
19883 }
19884
19885 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19886    That generation should happen after function debug info has been
19887    generated. The value of the attribute is the constant value of ARG.  */
19888
19889 static void
19890 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19891 {
19892   die_arg_entry entry;
19893
19894   if (!die || !arg)
19895     return;
19896
19897   if (!tmpl_value_parm_die_table)
19898     tmpl_value_parm_die_table
19899       = VEC_alloc (die_arg_entry, gc, 32);
19900
19901   entry.die = die;
19902   entry.arg = arg;
19903   VEC_safe_push (die_arg_entry, gc,
19904                  tmpl_value_parm_die_table,
19905                  &entry);
19906 }
19907
19908 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19909    by append_entry_to_tmpl_value_parm_die_table. This function must
19910    be called after function DIEs have been generated.  */
19911
19912 static void
19913 gen_remaining_tmpl_value_param_die_attribute (void)
19914 {
19915   if (tmpl_value_parm_die_table)
19916     {
19917       unsigned i;
19918       die_arg_entry *e;
19919
19920       for (i = 0;
19921            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
19922            i++)
19923         tree_add_const_value_attribute (e->die, e->arg);
19924     }
19925 }
19926
19927
19928 /* Replace DW_AT_name for the decl with name.  */
19929  
19930 static void
19931 dwarf2out_set_name (tree decl, tree name)
19932 {
19933   dw_die_ref die;
19934   dw_attr_ref attr;
19935   const char *dname;
19936
19937   die = TYPE_SYMTAB_DIE (decl);
19938   if (!die)
19939     return;
19940
19941   dname = dwarf2_name (name, 0);
19942   if (!dname)
19943     return;
19944
19945   attr = get_AT (die, DW_AT_name);
19946   if (attr)
19947     {
19948       struct indirect_string_node *node;
19949
19950       node = find_AT_string (dname);
19951       /* replace the string.  */
19952       attr->dw_attr_val.v.val_str = node;
19953     }
19954
19955   else
19956     add_name_attribute (die, dname);
19957 }
19958
19959 /* Called by the final INSN scan whenever we see a direct function call.
19960    Make an entry into the direct call table, recording the point of call
19961    and a reference to the target function's debug entry.  */
19962
19963 static void
19964 dwarf2out_direct_call (tree targ)
19965 {
19966   dcall_entry e;
19967   tree origin = decl_ultimate_origin (targ);
19968
19969   /* If this is a clone, use the abstract origin as the target.  */
19970   if (origin)
19971     targ = origin;
19972
19973   e.poc_label_num = poc_label_num++;
19974   e.poc_decl = current_function_decl;
19975   e.targ_die = force_decl_die (targ);
19976   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
19977
19978   /* Drop a label at the return point to mark the point of call.  */
19979   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
19980 }
19981
19982 /* Returns a hash value for X (which really is a struct vcall_insn).  */
19983
19984 static hashval_t
19985 vcall_insn_table_hash (const void *x)
19986 {
19987   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
19988 }
19989
19990 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
19991    insnd_uid of *Y.  */
19992
19993 static int
19994 vcall_insn_table_eq (const void *x, const void *y)
19995 {
19996   return (((const struct vcall_insn *) x)->insn_uid
19997           == ((const struct vcall_insn *) y)->insn_uid);
19998 }
19999
20000 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
20001
20002 static void
20003 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
20004 {
20005   struct vcall_insn *item = GGC_NEW (struct vcall_insn);
20006   struct vcall_insn **slot;
20007
20008   gcc_assert (item);
20009   item->insn_uid = insn_uid;
20010   item->vtable_slot = vtable_slot;
20011   slot = (struct vcall_insn **)
20012       htab_find_slot_with_hash (vcall_insn_table, &item,
20013                                 (hashval_t) insn_uid, INSERT);
20014   *slot = item;
20015 }
20016
20017 /* Return the VTABLE_SLOT associated with INSN_UID.  */
20018
20019 static unsigned int
20020 lookup_vcall_insn (unsigned int insn_uid)
20021 {
20022   struct vcall_insn item;
20023   struct vcall_insn *p;
20024
20025   item.insn_uid = insn_uid;
20026   item.vtable_slot = 0;
20027   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
20028                                                  (void *) &item,
20029                                                  (hashval_t) insn_uid);
20030   if (p == NULL)
20031     return (unsigned int) -1;
20032   return p->vtable_slot;
20033 }
20034
20035
20036 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
20037    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
20038    is the vtable slot index that we will need to put in the virtual call
20039    table later.  */
20040
20041 static void
20042 dwarf2out_virtual_call_token (tree addr, int insn_uid)
20043 {
20044   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
20045     {
20046       tree token = OBJ_TYPE_REF_TOKEN (addr);
20047       if (TREE_CODE (token) == INTEGER_CST)
20048         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
20049     }
20050 }
20051
20052 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
20053    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
20054    with NEW_INSN.  */
20055
20056 static void
20057 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
20058 {
20059   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
20060
20061   if (vtable_slot != (unsigned int) -1)
20062     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
20063 }
20064
20065 /* Called by the final INSN scan whenever we see a virtual function call.
20066    Make an entry into the virtual call table, recording the point of call
20067    and the slot index of the vtable entry used to call the virtual member
20068    function.  The slot index was associated with the INSN_UID during the
20069    lowering to RTL.  */
20070
20071 static void
20072 dwarf2out_virtual_call (int insn_uid)
20073 {
20074   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
20075   vcall_entry e;
20076
20077   if (vtable_slot == (unsigned int) -1)
20078     return;
20079
20080   e.poc_label_num = poc_label_num++;
20081   e.vtable_slot = vtable_slot;
20082   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
20083
20084   /* Drop a label at the return point to mark the point of call.  */
20085   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
20086 }
20087
20088 /* Called by the final INSN scan whenever we see a var location.  We
20089    use it to drop labels in the right places, and throw the location in
20090    our lookup table.  */
20091
20092 static void
20093 dwarf2out_var_location (rtx loc_note)
20094 {
20095   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20096   struct var_loc_node *newloc;
20097   rtx next_real;
20098   static const char *last_label;
20099   static const char *last_postcall_label;
20100   static bool last_in_cold_section_p;
20101   tree decl;
20102
20103   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20104     return;
20105
20106   next_real = next_real_insn (loc_note);
20107   /* If there are no instructions which would be affected by this note,
20108      don't do anything.  */
20109   if (next_real == NULL_RTX)
20110     return;
20111
20112   newloc = GGC_CNEW (struct var_loc_node);
20113   /* If there were no real insns between note we processed last time
20114      and this note, use the label we emitted last time.  */
20115   if (last_var_location_insn == NULL_RTX
20116       || last_var_location_insn != next_real
20117       || last_in_cold_section_p != in_cold_section_p)
20118     {
20119       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20120       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20121       loclabel_num++;
20122       last_label = ggc_strdup (loclabel);
20123       last_postcall_label = NULL;
20124     }
20125   newloc->var_loc_note = loc_note;
20126   newloc->next = NULL;
20127
20128   if (!NOTE_DURING_CALL_P (loc_note))
20129     newloc->label = last_label;
20130   else
20131     {
20132       if (!last_postcall_label)
20133         {
20134           sprintf (loclabel, "%s-1", last_label);
20135           last_postcall_label = ggc_strdup (loclabel);
20136         }
20137       newloc->label = last_postcall_label;
20138     }
20139
20140   if (cfun && in_cold_section_p)
20141     newloc->section_label = crtl->subsections.cold_section_label;
20142   else
20143     newloc->section_label = text_section_label;
20144
20145   last_var_location_insn = next_real;
20146   last_in_cold_section_p = in_cold_section_p;
20147   decl = NOTE_VAR_LOCATION_DECL (loc_note);
20148   add_var_loc_to_decl (decl, newloc);
20149 }
20150
20151 /* We need to reset the locations at the beginning of each
20152    function. We can't do this in the end_function hook, because the
20153    declarations that use the locations won't have been output when
20154    that hook is called.  Also compute have_multiple_function_sections here.  */
20155
20156 static void
20157 dwarf2out_begin_function (tree fun)
20158 {
20159   htab_empty (decl_loc_table);
20160
20161   if (function_section (fun) != text_section)
20162     have_multiple_function_sections = true;
20163
20164   dwarf2out_note_section_used ();
20165 }
20166
20167 /* Output a label to mark the beginning of a source code line entry
20168    and record information relating to this source line, in
20169    'line_info_table' for later output of the .debug_line section.  */
20170
20171 static void
20172 dwarf2out_source_line (unsigned int line, const char *filename,
20173                        int discriminator, bool is_stmt)
20174 {
20175   static bool last_is_stmt = true;
20176
20177   if (debug_info_level >= DINFO_LEVEL_NORMAL
20178       && line != 0)
20179     {
20180       int file_num = maybe_emit_file (lookup_filename (filename));
20181
20182       switch_to_section (current_function_section ());
20183
20184       /* If requested, emit something human-readable.  */
20185       if (flag_debug_asm)
20186         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
20187                  filename, line);
20188
20189       if (DWARF2_ASM_LINE_DEBUG_INFO)
20190         {
20191           /* Emit the .loc directive understood by GNU as.  */
20192           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20193           if (is_stmt != last_is_stmt)
20194             {
20195               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20196               last_is_stmt = is_stmt;
20197             }
20198           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20199             fprintf (asm_out_file, " discriminator %d", discriminator);
20200           fputc ('\n', asm_out_file);
20201
20202           /* Indicate that line number info exists.  */
20203           line_info_table_in_use++;
20204         }
20205       else if (function_section (current_function_decl) != text_section)
20206         {
20207           dw_separate_line_info_ref line_info;
20208           targetm.asm_out.internal_label (asm_out_file,
20209                                           SEPARATE_LINE_CODE_LABEL,
20210                                           separate_line_info_table_in_use);
20211
20212           /* Expand the line info table if necessary.  */
20213           if (separate_line_info_table_in_use
20214               == separate_line_info_table_allocated)
20215             {
20216               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20217               separate_line_info_table
20218                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
20219                                  separate_line_info_table,
20220                                  separate_line_info_table_allocated);
20221               memset (separate_line_info_table
20222                        + separate_line_info_table_in_use,
20223                       0,
20224                       (LINE_INFO_TABLE_INCREMENT
20225                        * sizeof (dw_separate_line_info_entry)));
20226             }
20227
20228           /* Add the new entry at the end of the line_info_table.  */
20229           line_info
20230             = &separate_line_info_table[separate_line_info_table_in_use++];
20231           line_info->dw_file_num = file_num;
20232           line_info->dw_line_num = line;
20233           line_info->function = current_function_funcdef_no;
20234         }
20235       else
20236         {
20237           dw_line_info_ref line_info;
20238
20239           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
20240                                      line_info_table_in_use);
20241
20242           /* Expand the line info table if necessary.  */
20243           if (line_info_table_in_use == line_info_table_allocated)
20244             {
20245               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20246               line_info_table
20247                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
20248                                  line_info_table_allocated);
20249               memset (line_info_table + line_info_table_in_use, 0,
20250                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
20251             }
20252
20253           /* Add the new entry at the end of the line_info_table.  */
20254           line_info = &line_info_table[line_info_table_in_use++];
20255           line_info->dw_file_num = file_num;
20256           line_info->dw_line_num = line;
20257         }
20258     }
20259 }
20260
20261 /* Record the beginning of a new source file.  */
20262
20263 static void
20264 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20265 {
20266   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20267     {
20268       /* Record the beginning of the file for break_out_includes.  */
20269       dw_die_ref bincl_die;
20270
20271       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
20272       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20273     }
20274
20275   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20276     {
20277       int file_num = maybe_emit_file (lookup_filename (filename));
20278
20279       switch_to_section (debug_macinfo_section);
20280       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20281       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
20282                                    lineno);
20283
20284       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
20285     }
20286 }
20287
20288 /* Record the end of a source file.  */
20289
20290 static void
20291 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20292 {
20293   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20294     /* Record the end of the file for break_out_includes.  */
20295     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
20296
20297   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20298     {
20299       switch_to_section (debug_macinfo_section);
20300       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20301     }
20302 }
20303
20304 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20305    the tail part of the directive line, i.e. the part which is past the
20306    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20307
20308 static void
20309 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20310                   const char *buffer ATTRIBUTE_UNUSED)
20311 {
20312   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20313     {
20314       switch_to_section (debug_macinfo_section);
20315       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20316       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20317       dw2_asm_output_nstring (buffer, -1, "The macro");
20318     }
20319 }
20320
20321 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20322    the tail part of the directive line, i.e. the part which is past the
20323    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20324
20325 static void
20326 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20327                  const char *buffer ATTRIBUTE_UNUSED)
20328 {
20329   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20330     {
20331       switch_to_section (debug_macinfo_section);
20332       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20333       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20334       dw2_asm_output_nstring (buffer, -1, "The macro");
20335     }
20336 }
20337
20338 /* Set up for Dwarf output at the start of compilation.  */
20339
20340 static void
20341 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20342 {
20343   /* Allocate the file_table.  */
20344   file_table = htab_create_ggc (50, file_table_hash,
20345                                 file_table_eq, NULL);
20346
20347   /* Allocate the decl_die_table.  */
20348   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20349                                     decl_die_table_eq, NULL);
20350
20351   /* Allocate the decl_loc_table.  */
20352   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20353                                     decl_loc_table_eq, NULL);
20354
20355   /* Allocate the initial hunk of the decl_scope_table.  */
20356   decl_scope_table = VEC_alloc (tree, gc, 256);
20357
20358   /* Allocate the initial hunk of the abbrev_die_table.  */
20359   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
20360   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20361   /* Zero-th entry is allocated, but unused.  */
20362   abbrev_die_table_in_use = 1;
20363
20364   /* Allocate the initial hunk of the line_info_table.  */
20365   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
20366   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
20367
20368   /* Zero-th entry is allocated, but unused.  */
20369   line_info_table_in_use = 1;
20370
20371   /* Allocate the pubtypes and pubnames vectors.  */
20372   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20373   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20374
20375   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
20376   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
20377                                       vcall_insn_table_eq, NULL);
20378
20379   /* Generate the initial DIE for the .debug section.  Note that the (string)
20380      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
20381      will (typically) be a relative pathname and that this pathname should be
20382      taken as being relative to the directory from which the compiler was
20383      invoked when the given (base) source file was compiled.  We will fill
20384      in this value in dwarf2out_finish.  */
20385   comp_unit_die = gen_compile_unit_die (NULL);
20386
20387   incomplete_types = VEC_alloc (tree, gc, 64);
20388
20389   used_rtx_array = VEC_alloc (rtx, gc, 32);
20390
20391   debug_info_section = get_section (DEBUG_INFO_SECTION,
20392                                     SECTION_DEBUG, NULL);
20393   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20394                                       SECTION_DEBUG, NULL);
20395   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20396                                        SECTION_DEBUG, NULL);
20397   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20398                                        SECTION_DEBUG, NULL);
20399   debug_line_section = get_section (DEBUG_LINE_SECTION,
20400                                     SECTION_DEBUG, NULL);
20401   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20402                                    SECTION_DEBUG, NULL);
20403   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20404                                         SECTION_DEBUG, NULL);
20405   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20406                                         SECTION_DEBUG, NULL);
20407   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
20408                                      SECTION_DEBUG, NULL);
20409   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
20410                                      SECTION_DEBUG, NULL);
20411   debug_str_section = get_section (DEBUG_STR_SECTION,
20412                                    DEBUG_STR_SECTION_FLAGS, NULL);
20413   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20414                                       SECTION_DEBUG, NULL);
20415   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20416                                      SECTION_DEBUG, NULL);
20417
20418   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20419   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20420                                DEBUG_ABBREV_SECTION_LABEL, 0);
20421   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20422   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20423                                COLD_TEXT_SECTION_LABEL, 0);
20424   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20425
20426   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20427                                DEBUG_INFO_SECTION_LABEL, 0);
20428   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20429                                DEBUG_LINE_SECTION_LABEL, 0);
20430   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20431                                DEBUG_RANGES_SECTION_LABEL, 0);
20432   switch_to_section (debug_abbrev_section);
20433   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
20434   switch_to_section (debug_info_section);
20435   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
20436   switch_to_section (debug_line_section);
20437   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
20438
20439   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20440     {
20441       switch_to_section (debug_macinfo_section);
20442       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20443                                    DEBUG_MACINFO_SECTION_LABEL, 0);
20444       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
20445     }
20446
20447   switch_to_section (text_section);
20448   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20449   if (flag_reorder_blocks_and_partition)
20450     {
20451       cold_text_section = unlikely_text_section ();
20452       switch_to_section (cold_text_section);
20453       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20454     }
20455
20456 }
20457
20458 /* Called before cgraph_optimize starts outputtting functions, variables
20459    and toplevel asms into assembly.  */
20460
20461 static void
20462 dwarf2out_assembly_start (void)
20463 {
20464   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
20465     {
20466 #ifndef TARGET_UNWIND_INFO
20467       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
20468 #endif
20469         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20470     }
20471 }
20472
20473 /* A helper function for dwarf2out_finish called through
20474    htab_traverse.  Emit one queued .debug_str string.  */
20475
20476 static int
20477 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20478 {
20479   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20480
20481   if (node->label && node->refcount)
20482     {
20483       switch_to_section (debug_str_section);
20484       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20485       assemble_string (node->str, strlen (node->str) + 1);
20486     }
20487
20488   return 1;
20489 }
20490
20491 #if ENABLE_ASSERT_CHECKING
20492 /* Verify that all marks are clear.  */
20493
20494 static void
20495 verify_marks_clear (dw_die_ref die)
20496 {
20497   dw_die_ref c;
20498
20499   gcc_assert (! die->die_mark);
20500   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20501 }
20502 #endif /* ENABLE_ASSERT_CHECKING */
20503
20504 /* Clear the marks for a die and its children.
20505    Be cool if the mark isn't set.  */
20506
20507 static void
20508 prune_unmark_dies (dw_die_ref die)
20509 {
20510   dw_die_ref c;
20511
20512   if (die->die_mark)
20513     die->die_mark = 0;
20514   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20515 }
20516
20517 /* Given DIE that we're marking as used, find any other dies
20518    it references as attributes and mark them as used.  */
20519
20520 static void
20521 prune_unused_types_walk_attribs (dw_die_ref die)
20522 {
20523   dw_attr_ref a;
20524   unsigned ix;
20525
20526   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20527     {
20528       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20529         {
20530           /* A reference to another DIE.
20531              Make sure that it will get emitted.
20532              If it was broken out into a comdat group, don't follow it.  */
20533           if (dwarf_version < 4
20534               || a->dw_attr == DW_AT_specification
20535               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20536             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20537         }
20538       /* Set the string's refcount to 0 so that prune_unused_types_mark
20539          accounts properly for it.  */
20540       if (AT_class (a) == dw_val_class_str)
20541         a->dw_attr_val.v.val_str->refcount = 0;
20542     }
20543 }
20544
20545
20546 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20547    to DIE's children.  */
20548
20549 static void
20550 prune_unused_types_mark (dw_die_ref die, int dokids)
20551 {
20552   dw_die_ref c;
20553
20554   if (die->die_mark == 0)
20555     {
20556       /* We haven't done this node yet.  Mark it as used.  */
20557       die->die_mark = 1;
20558
20559       /* We also have to mark its parents as used.
20560          (But we don't want to mark our parents' kids due to this.)  */
20561       if (die->die_parent)
20562         prune_unused_types_mark (die->die_parent, 0);
20563
20564       /* Mark any referenced nodes.  */
20565       prune_unused_types_walk_attribs (die);
20566
20567       /* If this node is a specification,
20568          also mark the definition, if it exists.  */
20569       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20570         prune_unused_types_mark (die->die_definition, 1);
20571     }
20572
20573   if (dokids && die->die_mark != 2)
20574     {
20575       /* We need to walk the children, but haven't done so yet.
20576          Remember that we've walked the kids.  */
20577       die->die_mark = 2;
20578
20579       /* If this is an array type, we need to make sure our
20580          kids get marked, even if they're types.  If we're
20581          breaking out types into comdat sections, do this
20582          for all type definitions.  */
20583       if (die->die_tag == DW_TAG_array_type
20584           || (dwarf_version >= 4 
20585               && is_type_die (die) && ! is_declaration_die (die)))
20586         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20587       else
20588         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20589     }
20590 }
20591
20592 /* For local classes, look if any static member functions were emitted
20593    and if so, mark them.  */
20594
20595 static void
20596 prune_unused_types_walk_local_classes (dw_die_ref die)
20597 {
20598   dw_die_ref c;
20599
20600   if (die->die_mark == 2)
20601     return;
20602
20603   switch (die->die_tag)
20604     {
20605     case DW_TAG_structure_type:
20606     case DW_TAG_union_type:
20607     case DW_TAG_class_type:
20608       break;
20609
20610     case DW_TAG_subprogram:
20611       if (!get_AT_flag (die, DW_AT_declaration)
20612           || die->die_definition != NULL)
20613         prune_unused_types_mark (die, 1);
20614       return;
20615
20616     default:
20617       return;
20618     }
20619
20620   /* Mark children.  */
20621   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20622 }
20623
20624 /* Walk the tree DIE and mark types that we actually use.  */
20625
20626 static void
20627 prune_unused_types_walk (dw_die_ref die)
20628 {
20629   dw_die_ref c;
20630
20631   /* Don't do anything if this node is already marked and
20632      children have been marked as well.  */
20633   if (die->die_mark == 2)
20634     return;
20635
20636   switch (die->die_tag)
20637     {
20638     case DW_TAG_structure_type:
20639     case DW_TAG_union_type:
20640     case DW_TAG_class_type:
20641       if (die->die_perennial_p)
20642         break;
20643
20644       for (c = die->die_parent; c; c = c->die_parent)
20645         if (c->die_tag == DW_TAG_subprogram)
20646           break;
20647
20648       /* Finding used static member functions inside of classes
20649          is needed just for local classes, because for other classes
20650          static member function DIEs with DW_AT_specification
20651          are emitted outside of the DW_TAG_*_type.  If we ever change
20652          it, we'd need to call this even for non-local classes.  */
20653       if (c)
20654         prune_unused_types_walk_local_classes (die);
20655
20656       /* It's a type node --- don't mark it.  */
20657       return;
20658
20659     case DW_TAG_const_type:
20660     case DW_TAG_packed_type:
20661     case DW_TAG_pointer_type:
20662     case DW_TAG_reference_type:
20663     case DW_TAG_volatile_type:
20664     case DW_TAG_typedef:
20665     case DW_TAG_array_type:
20666     case DW_TAG_interface_type:
20667     case DW_TAG_friend:
20668     case DW_TAG_variant_part:
20669     case DW_TAG_enumeration_type:
20670     case DW_TAG_subroutine_type:
20671     case DW_TAG_string_type:
20672     case DW_TAG_set_type:
20673     case DW_TAG_subrange_type:
20674     case DW_TAG_ptr_to_member_type:
20675     case DW_TAG_file_type:
20676       if (die->die_perennial_p)
20677         break;
20678
20679       /* It's a type node --- don't mark it.  */
20680       return;
20681
20682     default:
20683       /* Mark everything else.  */
20684       break;
20685   }
20686
20687   if (die->die_mark == 0)
20688     {
20689       die->die_mark = 1;
20690
20691       /* Now, mark any dies referenced from here.  */
20692       prune_unused_types_walk_attribs (die);
20693     }
20694
20695   die->die_mark = 2;
20696
20697   /* Mark children.  */
20698   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20699 }
20700
20701 /* Increment the string counts on strings referred to from DIE's
20702    attributes.  */
20703
20704 static void
20705 prune_unused_types_update_strings (dw_die_ref die)
20706 {
20707   dw_attr_ref a;
20708   unsigned ix;
20709
20710   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20711     if (AT_class (a) == dw_val_class_str)
20712       {
20713         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20714         s->refcount++;
20715         /* Avoid unnecessarily putting strings that are used less than
20716            twice in the hash table.  */
20717         if (s->refcount
20718             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20719           {
20720             void ** slot;
20721             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20722                                              htab_hash_string (s->str),
20723                                              INSERT);
20724             gcc_assert (*slot == NULL);
20725             *slot = s;
20726           }
20727       }
20728 }
20729
20730 /* Remove from the tree DIE any dies that aren't marked.  */
20731
20732 static void
20733 prune_unused_types_prune (dw_die_ref die)
20734 {
20735   dw_die_ref c;
20736
20737   gcc_assert (die->die_mark);
20738   prune_unused_types_update_strings (die);
20739
20740   if (! die->die_child)
20741     return;
20742
20743   c = die->die_child;
20744   do {
20745     dw_die_ref prev = c;
20746     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20747       if (c == die->die_child)
20748         {
20749           /* No marked children between 'prev' and the end of the list.  */
20750           if (prev == c)
20751             /* No marked children at all.  */
20752             die->die_child = NULL;
20753           else
20754             {
20755               prev->die_sib = c->die_sib;
20756               die->die_child = prev;
20757             }
20758           return;
20759         }
20760
20761     if (c != prev->die_sib)
20762       prev->die_sib = c;
20763     prune_unused_types_prune (c);
20764   } while (c != die->die_child);
20765 }
20766
20767 /* A helper function for dwarf2out_finish called through
20768    htab_traverse.  Clear .debug_str strings that we haven't already
20769    decided to emit.  */
20770
20771 static int
20772 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20773 {
20774   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20775
20776   if (!node->label || !node->refcount)
20777     htab_clear_slot (debug_str_hash, h);
20778
20779   return 1;
20780 }
20781
20782 /* Remove dies representing declarations that we never use.  */
20783
20784 static void
20785 prune_unused_types (void)
20786 {
20787   unsigned int i;
20788   limbo_die_node *node;
20789   comdat_type_node *ctnode;
20790   pubname_ref pub;
20791   dcall_entry *dcall;
20792
20793 #if ENABLE_ASSERT_CHECKING
20794   /* All the marks should already be clear.  */
20795   verify_marks_clear (comp_unit_die);
20796   for (node = limbo_die_list; node; node = node->next)
20797     verify_marks_clear (node->die);
20798   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20799     verify_marks_clear (ctnode->root_die);
20800 #endif /* ENABLE_ASSERT_CHECKING */
20801
20802   /* Mark types that are used in global variables.  */
20803   premark_types_used_by_global_vars ();
20804
20805   /* Set the mark on nodes that are actually used.  */
20806   prune_unused_types_walk (comp_unit_die);
20807   for (node = limbo_die_list; node; node = node->next)
20808     prune_unused_types_walk (node->die);
20809   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20810     {
20811       prune_unused_types_walk (ctnode->root_die);
20812       prune_unused_types_mark (ctnode->type_die, 1);
20813     }
20814
20815   /* Also set the mark on nodes referenced from the
20816      pubname_table or arange_table.  */
20817   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
20818     prune_unused_types_mark (pub->die, 1);
20819   for (i = 0; i < arange_table_in_use; i++)
20820     prune_unused_types_mark (arange_table[i], 1);
20821
20822   /* Mark nodes referenced from the direct call table.  */
20823   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
20824     prune_unused_types_mark (dcall->targ_die, 1);
20825
20826   /* Get rid of nodes that aren't marked; and update the string counts.  */
20827   if (debug_str_hash && debug_str_hash_forced)
20828     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
20829   else if (debug_str_hash)
20830     htab_empty (debug_str_hash);
20831   prune_unused_types_prune (comp_unit_die);
20832   for (node = limbo_die_list; node; node = node->next)
20833     prune_unused_types_prune (node->die);
20834   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20835     prune_unused_types_prune (ctnode->root_die);
20836
20837   /* Leave the marks clear.  */
20838   prune_unmark_dies (comp_unit_die);
20839   for (node = limbo_die_list; node; node = node->next)
20840     prune_unmark_dies (node->die);
20841   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20842     prune_unmark_dies (ctnode->root_die);
20843 }
20844
20845 /* Set the parameter to true if there are any relative pathnames in
20846    the file table.  */
20847 static int
20848 file_table_relative_p (void ** slot, void *param)
20849 {
20850   bool *p = (bool *) param;
20851   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20852   if (!IS_ABSOLUTE_PATH (d->filename))
20853     {
20854       *p = true;
20855       return 0;
20856     }
20857   return 1;
20858 }
20859
20860 /* Routines to manipulate hash table of comdat type units.  */
20861
20862 static hashval_t
20863 htab_ct_hash (const void *of)
20864 {
20865   hashval_t h;
20866   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20867
20868   memcpy (&h, type_node->signature, sizeof (h));
20869   return h;
20870 }
20871
20872 static int
20873 htab_ct_eq (const void *of1, const void *of2)
20874 {
20875   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20876   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20877
20878   return (! memcmp (type_node_1->signature, type_node_2->signature,
20879                     DWARF_TYPE_SIGNATURE_SIZE));
20880 }
20881
20882 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
20883    to the location it would have been added, should we know its
20884    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20885    probably improve compactness of debug info, removing equivalent
20886    abbrevs, and hide any differences caused by deferring the
20887    computation of the assembler name, triggered by e.g. PCH.  */
20888
20889 static inline void
20890 move_linkage_attr (dw_die_ref die)
20891 {
20892   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20893   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
20894
20895   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
20896
20897   while (--ix > 0)
20898     {
20899       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
20900
20901       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
20902         break;
20903     }
20904
20905   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
20906     {
20907       VEC_pop (dw_attr_node, die->die_attr);
20908       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
20909     }
20910 }
20911
20912 /* Helper function for resolve_addr, attempt to resolve
20913    one CONST_STRING, return non-zero if not successful.  Similarly verify that
20914    SYMBOL_REFs refer to variables emitted in the current CU.  */
20915
20916 static int
20917 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
20918 {
20919   rtx rtl = *addr;
20920
20921   if (GET_CODE (rtl) == CONST_STRING)
20922     {
20923       size_t len = strlen (XSTR (rtl, 0)) + 1;
20924       tree t = build_string (len, XSTR (rtl, 0));
20925       tree tlen = build_int_cst (NULL_TREE, len - 1);
20926       TREE_TYPE (t)
20927         = build_array_type (char_type_node, build_index_type (tlen));
20928       rtl = lookup_constant_def (t);
20929       if (!rtl || !MEM_P (rtl))
20930         return 1;
20931       rtl = XEXP (rtl, 0);
20932       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
20933       *addr = rtl;
20934       return 0;
20935     }
20936
20937   if (GET_CODE (rtl) == SYMBOL_REF
20938       && SYMBOL_REF_DECL (rtl)
20939       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
20940       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
20941     return 1;
20942
20943   if (GET_CODE (rtl) == CONST
20944       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
20945     return 1;
20946
20947   return 0;
20948 }
20949
20950 /* Helper function for resolve_addr, handle one location
20951    expression, return false if at least one CONST_STRING or SYMBOL_REF in
20952    the location list couldn't be resolved.  */
20953
20954 static bool
20955 resolve_addr_in_expr (dw_loc_descr_ref loc)
20956 {
20957   for (; loc; loc = loc->dw_loc_next)
20958     if ((loc->dw_loc_opc == DW_OP_addr
20959          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
20960         || (loc->dw_loc_opc == DW_OP_implicit_value
20961             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
20962             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
20963       return false;
20964   return true;
20965 }
20966
20967 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
20968    an address in .rodata section if the string literal is emitted there,
20969    or remove the containing location list or replace DW_AT_const_value
20970    with DW_AT_location and empty location expression, if it isn't found
20971    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
20972    to something that has been emitted in the current CU.  */
20973
20974 static void
20975 resolve_addr (dw_die_ref die)
20976 {
20977   dw_die_ref c;
20978   dw_attr_ref a;
20979   dw_loc_list_ref curr;
20980   unsigned ix;
20981
20982   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20983     switch (AT_class (a))
20984       {
20985       case dw_val_class_loc_list:
20986         for (curr = AT_loc_list (a); curr != NULL; curr = curr->dw_loc_next)
20987           if (!resolve_addr_in_expr (curr->expr))
20988             curr->expr = NULL;
20989         break;
20990       case dw_val_class_loc:
20991         if (!resolve_addr_in_expr (AT_loc (a)))
20992           a->dw_attr_val.v.val_loc = NULL;
20993         break;
20994       case dw_val_class_addr:
20995         if (a->dw_attr == DW_AT_const_value
20996             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
20997           {
20998             a->dw_attr = DW_AT_location;
20999             a->dw_attr_val.val_class = dw_val_class_loc;
21000             a->dw_attr_val.v.val_loc = NULL;
21001           }
21002         break;
21003       default:
21004         break;
21005       }
21006
21007   FOR_EACH_CHILD (die, c, resolve_addr (c));
21008 }
21009
21010 /* Output stuff that dwarf requires at the end of every file,
21011    and generate the DWARF-2 debugging info.  */
21012
21013 static void
21014 dwarf2out_finish (const char *filename)
21015 {
21016   limbo_die_node *node, *next_node;
21017   comdat_type_node *ctnode;
21018   htab_t comdat_type_table;
21019   dw_die_ref die = 0;
21020   unsigned int i;
21021
21022   gen_remaining_tmpl_value_param_die_attribute ();
21023
21024   /* Add the name for the main input file now.  We delayed this from
21025      dwarf2out_init to avoid complications with PCH.  */
21026   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
21027   if (!IS_ABSOLUTE_PATH (filename))
21028     add_comp_dir_attribute (comp_unit_die);
21029   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
21030     {
21031       bool p = false;
21032       htab_traverse (file_table, file_table_relative_p, &p);
21033       if (p)
21034         add_comp_dir_attribute (comp_unit_die);
21035     }
21036
21037   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21038     {
21039       add_location_or_const_value_attribute (
21040         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21041         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21042         DW_AT_location);
21043     }
21044
21045   /* Traverse the limbo die list, and add parent/child links.  The only
21046      dies without parents that should be here are concrete instances of
21047      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21048      For concrete instances, we can get the parent die from the abstract
21049      instance.  */
21050   for (node = limbo_die_list; node; node = next_node)
21051     {
21052       next_node = node->next;
21053       die = node->die;
21054
21055       if (die->die_parent == NULL)
21056         {
21057           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21058
21059           if (origin)
21060             add_child_die (origin->die_parent, die);
21061           else if (die == comp_unit_die)
21062             ;
21063           else if (errorcount > 0 || sorrycount > 0)
21064             /* It's OK to be confused by errors in the input.  */
21065             add_child_die (comp_unit_die, die);
21066           else
21067             {
21068               /* In certain situations, the lexical block containing a
21069                  nested function can be optimized away, which results
21070                  in the nested function die being orphaned.  Likewise
21071                  with the return type of that nested function.  Force
21072                  this to be a child of the containing function.
21073
21074                  It may happen that even the containing function got fully
21075                  inlined and optimized out.  In that case we are lost and
21076                  assign the empty child.  This should not be big issue as
21077                  the function is likely unreachable too.  */
21078               tree context = NULL_TREE;
21079
21080               gcc_assert (node->created_for);
21081
21082               if (DECL_P (node->created_for))
21083                 context = DECL_CONTEXT (node->created_for);
21084               else if (TYPE_P (node->created_for))
21085                 context = TYPE_CONTEXT (node->created_for);
21086
21087               gcc_assert (context
21088                           && (TREE_CODE (context) == FUNCTION_DECL
21089                               || TREE_CODE (context) == NAMESPACE_DECL));
21090
21091               origin = lookup_decl_die (context);
21092               if (origin)
21093                 add_child_die (origin, die);
21094               else
21095                 add_child_die (comp_unit_die, die);
21096             }
21097         }
21098     }
21099
21100   limbo_die_list = NULL;
21101
21102   resolve_addr (comp_unit_die);
21103
21104   for (node = deferred_asm_name; node; node = node->next)
21105     {
21106       tree decl = node->created_for;
21107       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21108         {
21109           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
21110                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
21111           move_linkage_attr (node->die);
21112         }
21113     }
21114
21115   deferred_asm_name = NULL;
21116
21117   /* Walk through the list of incomplete types again, trying once more to
21118      emit full debugging info for them.  */
21119   retry_incomplete_types ();
21120
21121   if (flag_eliminate_unused_debug_types)
21122     prune_unused_types ();
21123
21124   /* Generate separate CUs for each of the include files we've seen.
21125      They will go into limbo_die_list.  */
21126   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21127     break_out_includes (comp_unit_die);
21128
21129   /* Generate separate COMDAT sections for type DIEs. */
21130   if (dwarf_version >= 4)
21131     {
21132       break_out_comdat_types (comp_unit_die);
21133
21134       /* Each new type_unit DIE was added to the limbo die list when created.
21135          Since these have all been added to comdat_type_list, clear the
21136          limbo die list.  */
21137       limbo_die_list = NULL;
21138
21139       /* For each new comdat type unit, copy declarations for incomplete
21140          types to make the new unit self-contained (i.e., no direct
21141          references to the main compile unit).  */
21142       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21143         copy_decls_for_unworthy_types (ctnode->root_die);
21144       copy_decls_for_unworthy_types (comp_unit_die);
21145
21146       /* In the process of copying declarations from one unit to another,
21147          we may have left some declarations behind that are no longer
21148          referenced.  Prune them.  */
21149       prune_unused_types ();
21150     }
21151
21152   /* Traverse the DIE's and add add sibling attributes to those DIE's
21153      that have children.  */
21154   add_sibling_attributes (comp_unit_die);
21155   for (node = limbo_die_list; node; node = node->next)
21156     add_sibling_attributes (node->die);
21157   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21158     add_sibling_attributes (ctnode->root_die);
21159
21160   /* Output a terminator label for the .text section.  */
21161   switch_to_section (text_section);
21162   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
21163   if (flag_reorder_blocks_and_partition)
21164     {
21165       switch_to_section (unlikely_text_section ());
21166       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
21167     }
21168
21169   /* We can only use the low/high_pc attributes if all of the code was
21170      in .text.  */
21171   if (!have_multiple_function_sections
21172       || !(dwarf_version >= 3 || !dwarf_strict))
21173     {
21174       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
21175       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
21176     }
21177
21178   else
21179     {
21180       unsigned fde_idx = 0;
21181
21182       /* We need to give .debug_loc and .debug_ranges an appropriate
21183          "base address".  Use zero so that these addresses become
21184          absolute.  Historically, we've emitted the unexpected
21185          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
21186          Emit both to give time for other tools to adapt.  */
21187       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
21188       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
21189
21190       add_AT_range_list (comp_unit_die, DW_AT_ranges,
21191                          add_ranges_by_labels (text_section_label,
21192                                                text_end_label));
21193       if (flag_reorder_blocks_and_partition)
21194         add_ranges_by_labels (cold_text_section_label,
21195                               cold_end_label);
21196
21197       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
21198         {
21199           dw_fde_ref fde = &fde_table[fde_idx];
21200
21201           if (fde->dw_fde_switched_sections)
21202             {
21203               if (!fde->in_std_section)
21204                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
21205                                       fde->dw_fde_hot_section_end_label);
21206               if (!fde->cold_in_std_section)
21207                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
21208                                       fde->dw_fde_unlikely_section_end_label);
21209             }
21210           else if (!fde->in_std_section)
21211             add_ranges_by_labels (fde->dw_fde_begin,
21212                                   fde->dw_fde_end);
21213         }
21214
21215       add_ranges (NULL);
21216     }
21217
21218   /* Output location list section if necessary.  */
21219   if (have_location_lists)
21220     {
21221       /* Output the location lists info.  */
21222       switch_to_section (debug_loc_section);
21223       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
21224                                    DEBUG_LOC_SECTION_LABEL, 0);
21225       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
21226       output_location_lists (die);
21227     }
21228
21229   if (debug_info_level >= DINFO_LEVEL_NORMAL)
21230     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
21231                     debug_line_section_label);
21232
21233   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21234     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
21235
21236   /* Output all of the compilation units.  We put the main one last so that
21237      the offsets are available to output_pubnames.  */
21238   for (node = limbo_die_list; node; node = node->next)
21239     output_comp_unit (node->die, 0);
21240
21241   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
21242   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21243     {
21244       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
21245
21246       /* Don't output duplicate types.  */
21247       if (*slot != HTAB_EMPTY_ENTRY)
21248         continue;
21249
21250       /* Add a pointer to the line table for the main compilation unit
21251          so that the debugger can make sense of DW_AT_decl_file
21252          attributes.  */
21253       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21254         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
21255                         debug_line_section_label);
21256
21257       output_comdat_type_unit (ctnode);
21258       *slot = ctnode;
21259     }
21260   htab_delete (comdat_type_table);
21261
21262   /* Output the main compilation unit if non-empty or if .debug_macinfo
21263      has been emitted.  */
21264   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
21265
21266   /* Output the abbreviation table.  */
21267   switch_to_section (debug_abbrev_section);
21268   output_abbrev_section ();
21269
21270   /* Output public names table if necessary.  */
21271   if (!VEC_empty (pubname_entry, pubname_table))
21272     {
21273       switch_to_section (debug_pubnames_section);
21274       output_pubnames (pubname_table);
21275     }
21276
21277   /* Output public types table if necessary.  */
21278   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
21279      It shouldn't hurt to emit it always, since pure DWARF2 consumers
21280      simply won't look for the section.  */
21281   if (!VEC_empty (pubname_entry, pubtype_table))
21282     {
21283       switch_to_section (debug_pubtypes_section);
21284       output_pubnames (pubtype_table);
21285     }
21286
21287   /* Output direct and virtual call tables if necessary.  */
21288   if (!VEC_empty (dcall_entry, dcall_table))
21289     {
21290       switch_to_section (debug_dcall_section);
21291       output_dcall_table ();
21292     }
21293   if (!VEC_empty (vcall_entry, vcall_table))
21294     {
21295       switch_to_section (debug_vcall_section);
21296       output_vcall_table ();
21297     }
21298
21299   /* Output the address range information.  We only put functions in the arange
21300      table, so don't write it out if we don't have any.  */
21301   if (fde_table_in_use)
21302     {
21303       switch_to_section (debug_aranges_section);
21304       output_aranges ();
21305     }
21306
21307   /* Output ranges section if necessary.  */
21308   if (ranges_table_in_use)
21309     {
21310       switch_to_section (debug_ranges_section);
21311       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
21312       output_ranges ();
21313     }
21314
21315   /* Output the source line correspondence table.  We must do this
21316      even if there is no line information.  Otherwise, on an empty
21317      translation unit, we will generate a present, but empty,
21318      .debug_info section.  IRIX 6.5 `nm' will then complain when
21319      examining the file.  This is done late so that any filenames
21320      used by the debug_info section are marked as 'used'.  */
21321   if (! DWARF2_ASM_LINE_DEBUG_INFO)
21322     {
21323       switch_to_section (debug_line_section);
21324       output_line_info ();
21325     }
21326
21327   /* Have to end the macro section.  */
21328   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21329     {
21330       switch_to_section (debug_macinfo_section);
21331       dw2_asm_output_data (1, 0, "End compilation unit");
21332     }
21333
21334   /* If we emitted any DW_FORM_strp form attribute, output the string
21335      table too.  */
21336   if (debug_str_hash)
21337     htab_traverse (debug_str_hash, output_indirect_string, NULL);
21338 }
21339 #else
21340
21341 /* This should never be used, but its address is needed for comparisons.  */
21342 const struct gcc_debug_hooks dwarf2_debug_hooks =
21343 {
21344   0,            /* init */
21345   0,            /* finish */
21346   0,            /* assembly_start */
21347   0,            /* define */
21348   0,            /* undef */
21349   0,            /* start_source_file */
21350   0,            /* end_source_file */
21351   0,            /* begin_block */
21352   0,            /* end_block */
21353   0,            /* ignore_block */
21354   0,            /* source_line */
21355   0,            /* begin_prologue */
21356   0,            /* end_prologue */
21357   0,            /* end_epilogue */
21358   0,            /* begin_function */
21359   0,            /* end_function */
21360   0,            /* function_decl */
21361   0,            /* global_decl */
21362   0,            /* type_decl */
21363   0,            /* imported_module_or_decl */
21364   0,            /* deferred_inline_function */
21365   0,            /* outlining_inline_function */
21366   0,            /* label */
21367   0,            /* handle_pch */
21368   0,            /* var_location */
21369   0,            /* switch_text_section */
21370   0,            /* direct_call */
21371   0,            /* virtual_call_token */
21372   0,            /* copy_call_info */
21373   0,            /* virtual_call */
21374   0,            /* set_name */
21375   0             /* start_end_main_source_file */
21376 };
21377
21378 #endif /* DWARF2_DEBUGGING_INFO */
21379
21380 #include "gt-dwarf2out.h"